// 2008/09/26 V0.01.00 New function readCookie(name) does what it says on the tin
// 2009/11/23 V0.01.01 New function validate postcodes
// 2011/07/06 V0.01.02 New fucntion alertinvalidEmail displays warning if email is wrong format
//
function textwrite() {
	document.write("hello world") ;
}

function toggle_detail(content_detail) {
	if (document.getElementById(content_detail).style.visibility  == "hidden") {
		document.getElementById(content_detail).style.visibility  = "visible" ;
	} else {
		document.getElementById(content_detail).style.visibility  = "hidden" ;
	}
}

function alertinvalidEmail(str) {
	if (!emailcheck(str)) {
		alert('WARNING: email appears to be an invalid format') ;
	}
}

function emailcheck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function deleteCookie(cookiename) {
	document.cookie = cookiename + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/;";
}

function clearCookies() {
	if (window.confirm('This will reset the cookies for this website, you may need to log in again')) {
		var thecookie = document.cookie.split(";")
		for (var i = 0;i < thecookie.length;i++) {
			document.cookie = cookiename + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT;";
		}
	}
}

function logOut() {
	var expirydate = new Date()
	expirydate.setTime(expirydate.getTime());
	document.cookie = "currentUserName=; expires=" + expirydate.toGMTString() + ";";
	document.cookie = "currentPassword=; expires=" + expirydate.toGMTString() + ";";
}

function validatePostcode(postcode) {
	postcode = postcode.toUpperCase() ;
	size = postcode.length ;
	while (postcode.slice(0,1) == " ") {		//Strip leading spaces
		postcode = postcode.substr(1,size-1) ;
		size = postcode.length ;
	 }
	 while(postcode.slice(size-1,size)== " ") {		//Strip trailing spaces
		 postcode = postcode.substr(0,size-1) ;
		 size = postcode.length ;
	 }
	postcode = postcode.split(' ').join('');
	postcode = postcode.slice(0, postcode.length - 3) + " " + postcode.slice(postcode.length - 3) ;

	positionOfSpace = postcode.indexOf(" ") ;

	// Test the three positions after the space are in the format naa
	if ((isNaN(postcode.charAt(positionOfSpace + 1))) || !(isNaN(postcode.charAt(positionOfSpace + 2))) || !(isNaN(postcode.charAt(positionOfSpace + 3)))) {
		return false ;
	} else {
		// test the last two alpha's are not illegal characters
		var illegalEnds = ["O", "K", "I"] ;
		for (var i=0; i<illegalEnds.length; i++) {
			if (postcode.charAt(positionOfSpace + 2) == illegalEnds[i] || postcode.charAt(positionOfSpace + 3) == illegalEnds[i]) {
				return false ;
			}
		}
	}

	var singleAreas = ["B", "E", "G", "L", "M", "N", "S", "W"] ;
	var doubleAreas = ["AB", "AL", "BA","BB","BD","BH","BL","BN","BR","BS","BT","CA","CB","CF","CH","CM","CO","CR","CT","CV","CW","DA","DD","DE","DG","DH","DT","DY","EC","EH","EN","EX","FK","FY","GL","GU","HA","HD","HG","HP","HR","HS","HU","HX","IG","IP","IV","KA","KT","KW","KY","LA","LD","LE","LL","LN","LS","LU","ME","MK","ML","NE","NG","NN","NP","NR","NW","OL","OX","PA","PE","PH","PL","PO","PR","RG","RH","RM","SA","SE","SG","SK","SL","SM","SN","SO","SP","SR","SS","ST","SW","SY","TA","TD","TF","TN","TQ","TR","TS","TW","UB","WA","WC","WD","WF","WN","WR","WS","WV","YO","ZE"] ;
	outwardalpha = false ;
	// If char at pos 2 is a digit
	if (!(isNaN(postcode.charAt(1)))) {
		for (var i=0; i<8; i++) {
			if (postcode.charAt(0) == singleAreas[i]) {
				outwardalpha = true ;
			}
		}
	// If char at pos 3 is a digit
	} else if (!(isNaN(postcode.charAt(2)))) {
		for (var i=0; i<doubleAreas.length; i++) {
			if (postcode.slice(0,2) == doubleAreas[i]) {
				outwardalpha = true ;
			}
		}
	}
	if (outwardalpha == false) {
		return false ;
	}
	 
	return postcode ;
	
}

function get_radio_value(radioOBJ) {
	for (var i=0; i < radioOBJ.length; i++) {
		if (radioOBJ[i].checked) {
			return radioOBJ[i].value ;
		}
	}
}

