//----------------------------------------
//Javascript file for GreenAsap
//greenasap.js
//Version 1
//November 2008
//----------------------------------------

//****************Globals
var http;
var haveerrors = 0;
var reportPath = "http://files.ecohostonline.co.uk/orders/"    //"ftp://files.ecohostonline.co.uk/";

//-------------------------------------------------------------LOGIN

//****************Validate form pre login on key press
function submitForm(e) { 
	evt = e || window.event;
	if (evt.keyCode == 13){ 
		validateLoginForm();
	}
} 

//****************Validate form pre login
function validateLoginForm() {

	document.getElementById("errormessage").innerHTML = "&nbsp;";		//Clear any error messages
	var hasErrors = false;										//Error flag
	
	if(document.frmLogin.email2.value.length < 1){
		document.getElementById("errormessage").innerHTML = "Please enter a user name.";
		hasErrors = true;
	}
	
	if(!hasErrors){
		if(document.frmLogin.email2.value.length >= 1){
			if((document.frmLogin.email2.value.search("@") == -1) || (document.frmLogin.email2.value.search("[.*]") == -1 )){
				document.getElementById("errormessage").innerHTML = "Please ensure your user name is a valid email address.";
				hasErrors = true;
			}
			if(!hasErrors){
				if(document.frmLogin.password2.value.length < 1){
					document.getElementById("errormessage").innerHTML = "Please enter a password.";
					hasErrors = true;
				}
				
				if(!hasErrors){
		        if(document.frmLogin.password2.value.length >= 1){		
			        if ((document.frmLogin.password2.value.length < 6)){
				       document.getElementById("errormessage").innerHTML = "Password must be at least 6 characters.";
                        hasErrors = true;  
				    	}  	
			    if(!hasErrors){	
					if(!document.frmLogin.password2.value.match("^[a-z|A-Z|0-9]*$")) {
						document.getElementById("errormessage").innerHTML = "Invalid Password";
					    hasErrors = true;   
						}
				      }
				   }
				}
				
			}	
		}
	}

	//No input error - run login script
	if(!hasErrors){
		runLogin();
	}
}

//****************Run login script
function runLogin(){	
		http = createRequestObject();
		http.open('get', 'runlogin.php?var1=' + document.frmLogin.password2.value + '&var2=' + document.frmLogin.email2.value); 
		http.onreadystatechange = handleLoginResponse;
		http.send(null);
}

//****************Handles the responce from running login
function handleLoginResponse() {
	if(http.readyState == 4){
		var response = http.responseText;
		var returnArray = response.split("|")
		if(returnArray[0] == "ok"){			
			document.frmProceed.action = "userdetails.php";
			document.frmProceed.submit();	
		}else{
			document.getElementById("errormessage").innerHTML = returnArray[1];
		}
	}
}

//****************header Login form   (Header login form)
function validateheaderLoginForm() {

	var re = /\w/; 
	var hasErrors = false;										//Error flag
	
	if(document.frmheaderLogin.email.value.length < 1){
		alert("Please enter your eamil");
		hasErrors = true;
	}
	
	if(!hasErrors){
		if(document.frmheaderLogin.email.value.length >= 1){
			if((document.frmheaderLogin.email.value.search("@") == -1) || (document.frmheaderLogin.email.value.search("[.*]") == -1 )){
				alert("Please ensure your user name is a valid email address"); 
				hasErrors = true;
			}
			if(!hasErrors){
				if(document.frmheaderLogin.password.value.length < 1){
				alert("Please enter a password");	 
					hasErrors = true;
			}
			if(!hasErrors){
		    if(document.frmheaderLogin.password.value.length >= 1){		
			if ((document.frmheaderLogin.password.value.length < 6)){
				        alert("Password must be at least 6 characters.");
                        hasErrors = true;  
				    	}  	
					if(!hasErrors){	
					 if(!document.frmheaderLogin.password.value.match("^[a-z|A-Z|0-9]*$")) {
							alert("Invalid Password.The password must contain alpha-numeric characters only.");
					    	hasErrors = true;   
						}
																						 
				      }
			
		           }
			         
				}				
			}	
		}
	}

	//No input error - run login script for header form
	if(!hasErrors){
		runLogin2();
	}
}

//****************Run login script for header form
function runLogin2(){	
		http = createRequestObject();
		http.open('get', 'runlogin.php?var1=' + document.frmheaderLogin.password.value + '&var2=' + document.frmheaderLogin.email.value); 
		http.onreadystatechange = handleLoginResponse2;
		http.send(null);
}

//****************Handles the responce from running login (header login form)
function handleLoginResponse2() {
	if(http.readyState == 4){
		var response = http.responseText;
		var returnArray = response.split("|")
		if(returnArray[0] == "ok"){			
			document.frmProceed.action = "userdetails.php";
			document.frmProceed.submit();	
		}else{
			alert("Invalid login ");
		}
	}
}


//****************Email password reminder
function sendReminder(){
	if(document.frmLogin.email2.value.length < 1){
		document.getElementById("errormessage").innerHTML = "Please enter a user name as a valid email address.";
	}else{
		http = createRequestObject();
		http.open('get', 'sendreminder.php?var1=' + document.frmLogin.email2.value); 
		http.onreadystatechange = handleReminderResponse;
		http.send(null);
	}
}

//****************Handles the responce from email password reminder
function handleReminderResponse() {
	if(http.readyState == 4){
		var response = http.responseText;
		if(response == "ok"){			
			document.getElementById("errormessage").innerHTML = "Password reminder has been sent";
		}else{
			document.getElementById("errormessage").innerHTML = response;	
		}
	}
}

//-------------------------------------------------------------REGISTER

//****************Run register script
function registerUser(){
		http = createRequestObject();
		var params = "?email=" + document.frmRegister.email.value; 
		params += "&title=" + document.frmRegister.title.value; 
		params += "&mp=" + document.frmRegister.mp.value; 
		params += "&tp=" + document.frmRegister.tp.value; 
		params += "&comp=" + document.frmRegister.comp.value; 
		params += "&emailc=" + document.frmRegister.emailc.value; 
		params += "&passv=" + document.frmRegister.passv.value; 
		params += "&fname=" + document.frmRegister.fname.value; 
		params += "&sname=" + document.frmRegister.sname.value; 
		params += "&address1=" + document.frmRegister.address1.value; 
		params += "&address2=" + document.frmRegister.address2.value; 
		params += "&town=" + document.frmRegister.town.value; 
		params += "&pcode=" + document.frmRegister.pcode.value;
		params += "&county=" + document.frmRegister.county.value; 
		params += "&email=" + document.frmRegister.email.value; 
		params += "&pass=" + document.frmRegister.pass.value;	
		http.open('get', 'registeruser.php' + params ,true);	
		http.onreadystatechange = handleRegisterResponse;
		http.send(null);
}

//****************Handles the responce from running register
function handleRegisterResponse() {
	if(http.readyState == 4){
		var response = http.responseText;	
		var returnArray = response.split("|")
		if(returnArray[0] == "ok"){					
			document.frmProceed.action = "userdetails.php";
			document.frmProceed.submit();	
		}else{
			document.getElementById("registererror").innerHTML = returnArray[1];
		}
	}
}

//****************Register validation
function validateRegister() {
	isOK = true;
	document.getElementById("fnameerror").innerHTML = "";
	document.getElementById("snameerror").innerHTML = "";
	document.getElementById("address1error").innerHTML = "";
	document.getElementById("townerror").innerHTML = "";
	document.getElementById("countyerror").innerHTML = "";
	document.getElementById("pcodeerror").innerHTML = "";
	document.getElementById("tperror").innerHTML = "";
	document.getElementById("emailerror").innerHTML = "";
	document.getElementById("emailcerror").innerHTML = "";
	document.getElementById("passerror").innerHTML = "";
	document.getElementById("passverror").innerHTML = "";

	if(document.frmRegister.terms.checked){

		if(document.frmRegister.fname.value.length < 1){
			document.getElementById("fnameerror").innerHTML = "Please enter a first name";	
			isOK = false;
		}
		if(document.frmRegister.sname.value.length < 1){
			document.getElementById("snameerror").innerHTML = "Please enter a surname name";	
			isOK = false;
		}
		if(document.frmRegister.address1.value.length < 1){
			document.getElementById("address1error").innerHTML = "Please enter an address";	
			isOK = false;
		}	
		if(document.frmRegister.town.value.length < 1){
			document.getElementById("townerror").innerHTML = "Please enter a town";	
			isOK = false;
		}	
		if(document.frmRegister.county.value.length < 1){
			document.getElementById("countyerror").innerHTML = "Please enter a county";	
			isOK = false;
		}
		if(document.frmRegister.pcode.value.length < 1){
			document.getElementById("pcodeerror").innerHTML = "Please enter a postcode";	
			isOK = false;
		}
		if(document.frmRegister.tp.value.length < 1){
			document.getElementById("tperror").innerHTML = "Please enter a telephone number";	
			isOK = false;
		}
		if(document.frmRegister.email.value.length < 1){
			document.getElementById("emailerror").innerHTML = "Please enter an email address";	
			isOK = false;
		}else{	
			if((document.frmRegister.email.value.search("@") == -1) || (document.frmRegister.email.value.search("[.*]") == -1 )){
				document.getElementById("emailerror").innerHTML = "Please ensure the email address is valid";	
				isOK = false;
			}else if(document.frmRegister.emailc.value != document.frmRegister.email.value){
				document.getElementById("emailerror").innerHTML = "Please ensure the email address is verified correctly";	
				isOK = false;
			}	
		}
		if(document.frmRegister.pass.value.length < 1){
			document.getElementById("passerror").innerHTML = "Please enter an password";	
			isOK = false;
		}else{
			 if(document.frmRegister.pass.value.length < 6)
			 {
				 document.getElementById("passerror").innerHTML = "Password must be at least 6 characters";
				 	isOK = false;
		}else{
		      if(!document.frmRegister.pass.value.match("^[a-z|A-Z|0-9]*$")) 
			  {		  
			     document.getElementById("passerror").innerHTML = "Password need to be numbers or characters only";
				 	isOK = false;
	    }else{	 
		     if(document.frmRegister.pass.value != document.frmRegister.passv.value)
		     {
				document.getElementById("passverror").innerHTML = "Please ensure the password is verified correctly";	
				isOK = false;
		
		         }
			   }
			 }	
		   }
	}else{
		isOK = false;
		alert("Please indicate you agree to the terms and conditions"); 
	}
	
	//No input error - register
	if(isOK){
		registerUser();
	}
	
}

//-------------------------------------------------------------USER DETAILS
//****************Password validation
function validateUpdatePassword(f) {
	document.getElementById("password1error").innerHTML = ""
	document.getElementById("password2error").innerHTML = ""	
	isOK = true;	
	if(f.pass1.value.length < 1){
		document.getElementById("password1error").innerHTML = "Please enter a new password.";	
		isOK = false;
	}
	if(f.pass1.value.length > 0){
		if(f.pass1.value.length < 6){
		
		document.getElementById("password1error").innerHTML = "At least 6 characters required.";	
		isOK = false;
		}
	}
	
	if(f.pass1.value.length > 0){
		if(!f.pass1.value.match("^[a-z|A-Z|0-9]*$")) {
		
		document.getElementById("password1error").innerHTML = "Numbers or characters only.";	
		isOK = false;
		}
	}
	
	if(f.pass2.value.length < 1){
		document.getElementById("password2error").innerHTML = "Please confirm the password.";
		isOK = false;
	}	
	if(f.pass2.value.length > 0){
		if((f.pass1.value) != (f.pass2.value)){
			document.getElementById("password2error").innerHTML = "Passwords don't match";
			isOK = false;	
		}
	}
	return (isOK);
}

//****************Contact details validation
function validateContactDetails(f) {

	document.getElementById("fnameerror").innerHTML = "";
	document.getElementById("snameerror").innerHTML = "";
	document.getElementById("address1error").innerHTML = "";
	document.getElementById("townerror").innerHTML = "";
	document.getElementById("countyerror").innerHTML = "";
	document.getElementById("postcodeerror").innerHTML = "";
	document.getElementById("tperror").innerHTML = "";
	document.getElementById("emailerror").innerHTML = "";
		
	isOK = true;	
	if(f.fname.value.length < 1){
		document.getElementById("fnameerror").innerHTML = "Please enter a first name";	
		isOK = false;
	}
	if(f.sname.value.length < 1){
		document.getElementById("snameerror").innerHTML = "Please enter a surname name";	
		isOK = false;
	}
	if(f.address1.value.length < 1){
		document.getElementById("address1error").innerHTML = "Please enter an address";	
		isOK = false;
	}	
	if(f.town.value.length < 1){
		document.getElementById("townerror").innerHTML = "Please enter a town";	
		isOK = false;
	}	
	if(f.county.value.length < 1){
		document.getElementById("countyerror").innerHTML = "Please enter a county";	
		isOK = false;
	}
	if(f.pcode.value.length < 1){
		document.getElementById("postcodeerror").innerHTML = "Please enter a postcode";	
		isOK = false;
	}
	if(f.tp.value.length < 1){
		document.getElementById("tperror").innerHTML = "Please enter a telephone number";	
		isOK = false;
	}
	if(f.email.value.length < 1){
		document.getElementById("emailerror").innerHTML = "Please enter an email address";	
		isOK = false;
	}else{	
		if((f.email.value.search("@") == -1) || (f.email.value.search("[.*]") == -1 )){
			document.getElementById("emailerror").innerHTML = "Please ensure the email address is valid";	
			isOK = false;
		}	
	}
	
	return (isOK);
}

//****************Phone numbers validation
function validatePhoneNumber(field) {
	value = field.value;
	if(value != ""){	
		while (value.charAt(value.length-1) == " "){value = value.substring(0,value.length-1);} 
		while(value.substring(0,1) ==" "){value = value.substring(1,value.length);}
		field.value = value;
		//Check Chars are valid
		var valid = " 0123456789"
		var ok = "yes";
		var temp;
		for (var i=0; i<field.value.length; i++) {
			temp = "" + field.value.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") ok = "no";
		}
		if (ok == "no") {
			alert("Please ensure numeric values are entered in the phone number fields.");
			field.focus();
			field.select();
		}
	}
}


//-------------------------------------------------------------ADDRESS SEARCHING
/*-------Shows the extra rows to perform a full address search
function showFullAddressInput(){
	var allMenus = new Array("streetrow","townrow");
	for(m=0; m < allMenus.length;m++){
		document.getElementById(allMenus[m]).className='addressrowon';
	}
	document.frmAddressSearch.searchtype.value = "full";
}*/

//-------Stores selected report type in hidden
function selectReportType(theType){
	document.frmAddressSearch.reporttypevalue.value = theType;
}

//-------Stores selected report purpose in hidden
function selectReportPurpose(theType){
	document.frmAddressSearch.reportpurposevalue.value = theType;
}

//-------Validate the address searching form
function validateAddressForm(){

	var isOK = true;
	var isCoordinateSearch = false;
	
	if((document.frmAddressSearch.streetname.value == "") && ((document.frmAddressSearch.townname.value == "") && (document.frmAddressSearch.pcode.value == "") && (document.frmAddressSearch.hn.value == "")&& (document.frmAddressSearch.oscoords.value == ""))){
		alert("Please ensure search criteria is entered.");
		isOK = false;
	
	}else if((document.frmAddressSearch.streetname.value != "") && ((document.frmAddressSearch.townname.value == "") && (document.frmAddressSearch.pcode.value == "") && (document.frmAddressSearch.hn.value == "")&& (document.frmAddressSearch.oscoords.value == ""))){
		alert("Please ensure search criteria is entered.");
		isOK = false;
	
	}else if((document.frmAddressSearch.hn.value != "") && ((document.frmAddressSearch.townname.value == "") && (document.frmAddressSearch.pcode.value == "") && (document.frmAddressSearch.streetname.value == "")&& (document.frmAddressSearch.oscoords.value == ""))){
		alert("Please ensure search criteria is entered.");
		isOK = false;
	
	/*if((document.frmAddressSearch.streetname.value == "") && (document.frmAddressSearch.townname.value == "") && (document.frmAddressSearch.pcode.value == "") && (document.frmAddressSearch.oscoords.value == "")){
		alert("Please ensure search criteria is entered.");
		isOK = false;
	}else if((document.frmAddressSearch.streetname.value != "") && ((document.frmAddressSearch.townname.value == "") && (document.frmAddressSearch.pcode.value == "") && (document.frmAddressSearch.hn.value == "")&& (document.frmAddressSearch.oscoords.value == ""))){
		alert("Please ensure search criteria is entered.");
		isOK = false;
	}else if((document.frmAddressSearch.townname.value != "") && (document.frmAddressSearch.streetname.value == "") && (document.frmAddressSearch.oscoords.value == "")){
		alert("Please ensure search criteria is entered.");
		isOK = false;
		*/

	}else{
		if(document.frmAddressSearch.oscoords.value != ""){			
			if(document.frmAddressSearch.oscoords.value.length == 8){			
				isOK = getOSCoordinate();
				if(isOK){
					document.frmAddressSearch.storedaddress.value = "Not Defined";
					storeOrder2();
					isCoordinateSearch = true;
				}else{
					alert("'"  + document.frmAddressSearch.oscoords.value + "' is not a valid OS coordinate.")				
				}
			}else{
				isOK = false;				
				alert("Please ensure a valid 8 figure OS coordinate is entered.")
			}	
							
		}else if(document.frmAddressSearch.pcode.value != ""){		
			//Check postcode is valid
			var thePcode = document.frmAddressSearch.pcode.value;
			if((thePcode.length < 5) || (thePcode.length > 8)){
				alert("Please ensure a full 'Postcode' is entered correctly.")
				isOK = false;
			}else{
				//Amend postcode if necessary (ie. add the space)			
				var thespace = thePcode.indexOf(" ");
				if(thespace != -1){			
					if((thePcode.length-4) != thespace){
						alert("Please ensure a full 'Postcode' is entered correctly.")
						isOK = false;
					}
				}else{					//Add space
					var amendedPcode = thePcode.substring(0,thePcode.length-3) + " " + thePcode.substring(thePcode.length-3, thePcode.length);
					document.frmAddressSearch.pcode.value  = amendedPcode;
					var thePcode = document.frmAddressSearch.pcode.value;	
				}
				
				//Check if postcode is English or Welsh (others are not allowed)
				var invalidPostcodes = new Array("AB","BT","DD","DG","EH","FK","GY","HS","IM","IV","JE","KA","KW","KY","ML","PA","PH","TD","ZE");
				var testPcode = thePcode.substring(0,2).toUpperCase();
				for(pc=0;pc < invalidPostcodes.length;pc++){
					if(testPcode == invalidPostcodes[pc]){
						alert("Please ensure a valid 'Postcode' for Enagland and Wales is entered.")
						isOK = false;
						break
					}
				}
				if(isOK){			//Specific check for Glasgow (G)			
					var testPcode1 = thePcode.substring(0,1).toUpperCase();
					var testPcode2 = thePcode.substring(1,2).toUpperCase();										
					if((testPcode1 == "G") && (isNaN(testPcode2) == false)){
						alert("Please ensure a valid 'Postcode' for England and Wales is entered.")
						isOK = false;
					}
				}
			}
		}	
	}	

	if((isOK) && (isCoordinateSearch == false)){
		document.frmAddressSearch.action = "searchresults.php"; 
		document.frmAddressSearch.submit();
	}
	
}

//****************Defines the OS coordinate from the reference enetered
function getOSCoordinate(){

	var enteredReference = document.frmAddressSearch.oscoords.value;
	var the100KCode = enteredReference.substring(0,2).toUpperCase();
	var theRest = enteredReference.substring(2,enteredReference.length);
	var isOK = true;

	if (the100KCode == "SV") {
		xs  =  "0"
		ys  =  "0"
	}else if(the100KCode == "SW") {
		xs  =  "1"
		ys  =  "0"
	}else if(the100KCode == "SX") {
		xs  =  "2"
		ys  =  "0"
	}else if(the100KCode == "SY") {
		xs  =  "3"
		ys  =  "0"
	}else if(the100KCode == "SZ") {
		xs  =  "4"
		ys  =  "0"
	}else if(the100KCode == "TV") {
		xs  =  "5"
		ys  =  "0"
	}else if(the100KCode == "TW") {
		xs  =  "6"
		ys  =  "0"
	}else if(the100KCode == "SR") {
		xs  =  "1"
		 ys  =  "1"
	}else if(the100KCode == "SS") {
		xs  =  "2"
		ys  =  "1"
	}else if(the100KCode == "ST") {
		xs  =  "3"
		ys  =  "1"
	}else if(the100KCode == "SU") {
		xs  =  "4"
		ys  =  "1"
	}else if(the100KCode == "TQ") {
		xs  =  "5"
		ys  =  "1"
	}else if(the100KCode == "TR") {
		xs  =  "6"
		ys  =  "1"
	}else if(the100KCode == "SM") {
		xs  =  "1"
		ys  =  "2"
	}else if(the100KCode == "SN") {
		xs  =  "2"
		ys  =  "2"
	}else if(the100KCode == "SO") {
		xs  =  "3"
		ys  =  "2"
	}else if(the100KCode == "SP") {
		xs  =  "4"
		ys  =  "2"
	}else if(the100KCode == "TL") {
		xs  =  "5"
		ys  =  "2"
	}else if(the100KCode == "TM") {
		xs  =  "6"
		ys  =  "2"
	}else if(the100KCode == "SH") {
		xs  =  "2"
		ys  =  "3"
	}else if(the100KCode == "SJ") {
		xs  =  "3"
		ys  =  "3"
	}else if(the100KCode == "SK") {
		xs  =  "4"
		ys  =  "3"
	}else if(the100KCode == "TF") {
		xs  =  "5"
		ys  =  "3"
	}else if(the100KCode == "TG") {
		xs  =  "6"
		ys  =  "3"
	}else if(the100KCode == "SA") {
		xs  =  "0"
		ys  =  "4"
	}else if(the100KCode == "SB") {
		xs  =  "1"
		ys  =  "4"
	}else if(the100KCode == "SC") {
		xs  =  "2"
		ys  =  "4"
	}else if(the100KCode == "SD") {
		xs  =  "3"
		ys  =  "4"
	}else if(the100KCode == "SE") {
		xs  =  "4"
		ys  =  "4"
	}else if(the100KCode == "TA") {
		xs  =  "5"
		ys  =  "4"
	}else if(the100KCode == "NW") {
		xs  =  "1"
		 ys  =  "5"
	}else if(the100KCode == "NX") {
		xs  =  "2"
		ys  =  "5"
	}else if(the100KCode == "NY") {
		xs  =  "3"
		ys  =  "5"
	}else if(the100KCode == "NZ") {
		xs  =  "4"
		ys  =  "5"
	}else if(the100KCode == "NR") {
		xs  =  "1"
		ys  =  "6"
	}else if(the100KCode == "NS") {
		xs  =  "2"
		ys  =  "6"
	}else if(the100KCode == "NT") {
		xs  =  "3"
		ys  =  "6"
	}else if(the100KCode == "NU") {
		xs  =  "4"
		ys  =  "6"
	}else if(the100KCode == "NL") {
		xs  =  "0"
		ys  =  "7"
	}else if(the100KCode == "NM") {
		xs  =  "1"
		ys  =  "7"
	}else if(the100KCode == "NN") {
		xs  =  "2"
		ys  =  "7"
	}else if(the100KCode == "NO") {
		xs  =  "3"
		ys  =  "7"
	}else if(the100KCode == "NF") {
		xs  =  "0"
		ys  =  "8"
	}else if(the100KCode == "NG") {
		xs  =  "1"
		ys  =  "8"
	}else if(the100KCode == "NH") {
		xs  =  "2"
		ys  =  "8"
	}else if(the100KCode == "NJ") {
		xs  =  "3"
		ys  =  "8"
	}else if(the100KCode == "NK") {
		xs  =  "4"
		ys  =  "8"
	}else if(the100KCode == "NA") {
		xs  =  "0"
		ys  =  "9"
	}else if(the100KCode == "NB") {
		xs  =  "1"
		ys  =  "9"
	}else if(the100KCode == "NC") {
		xs  =  "2"
		ys  =  "9"
	}else if(the100KCode == "ND") {
		xs  =  "3"
		ys  =  "9"
	}else if(the100KCode == "HW") {
		xs  =  "1"
		ys  =  "10"
	}else if(the100KCode == "HX") {
		xs  =  "2"
		ys  =  "10"
	}else if(the100KCode == "HY") {
		xs  =  "3"
		ys  =  "10"
	}else if(the100KCode == "HZ") {
		xs  =  "4"
		ys  =  "10"
	}else if(the100KCode == "HT") {
		xs  =  "3"
		ys  =  "11"
	}else if(the100KCode == "HU") {
		xs  =  "4"
		ys  =  "11"
	}else if(the100KCode == "HP") {
		xs  =  "4"
		ys  =  "12"
	}else if(the100KCode == "OV") {
		xs  =  "5"
		ys  =  "5"
	}else{
	   isOK = false;	
	}

	if(isOK){
		if(theRest.length == 6){
			xs = xs + theRest.substring(0,3) + "00";
			ys = ys + theRest.substring(3,theRest.length) + "00";	
			document.frmAddressSearch.storedx.value = xs;
			document.frmAddressSearch.storedy.value = ys;
		}else{
			isOK = false;	
		}
	}
	return isOK;
}



//-------------------------------------------------------------STORING ORDER

//****************Stores the information on the order (Address, report purpose and report type) - before going to the map page
function storeOrder(){

	//Get all information required	
	if((document.frmAddressMatches.addressMatches.value == "") ||(document.frmAddressMatches.storedreporttype.value == "") || (document.frmAddressMatches.storedreportpurpose.value == "")){
		alert("Please ensure that the address, report type and report purpose has been selected.")
	}else{
		var addressStringArray = document.frmAddressMatches.addressMatches.value.split("|");
		var coordinateString = addressStringArray[0];
		var coordinateStringArray = coordinateString.split(",");
		var addressString = addressStringArray[1];	
	
		//Store address string and coordinates in hidden fields
		document.frmAddressMatches.storedaddress.value = addressString;
		document.frmAddressMatches.storedx.value = coordinateStringArray[0];
		document.frmAddressMatches.storedy.value = coordinateStringArray[1];
	
		//Create new record in the orders database.
		var qstring = "type=" + document.frmAddressMatches.storedreporttype.value + "&address=" + addressString + "&purpose=" + document.frmAddressMatches.storedreportpurpose.value;
		http = createRequestObject();
		http.open('get', 'storeorder.php?' + qstring); 
		http.onreadystatechange = handleStoreOrderResponse;
		http.send(null);
	}

}

//****************Handles the response from storeorder.php to pass necessary information to map page
function handleStoreOrderResponse() {
	if(http.readyState == 4){
		var passedOrderNo = http.responseText;
		if(passedOrderNo != ""){		
			if(passedOrderNo.substring(0,4) == "ASAP"){		
				var theX = document.frmAddressMatches.storedx.value;				//Retrieve x-y from form
				var theY = document.frmAddressMatches.storedy.value;
				document.frmAddressMatches.storedorderno.value = passedOrderNo;		//Store order number on the form
				
				//Submit form to open map page
				document.frmAddressMatches.action = "map.php"; 
				document.frmAddressMatches.submit();
			}
		}else{
			alert("Could not initialize the order. Please try again")		
		}
	}
}

//****************Stores the information on the order (Address and report type) - before going to the map page (Called if user has searched by coordinates)
function storeOrder2(){
	//Get all information required	
	if((document.frmAddressSearch.storedaddress.value == "") ||(document.frmAddressSearch.reporttypevalue.value == "") || (document.frmAddressMatches.storedreportpurpose.value == "")){
		alert("Please ensure that the address or report type has been selected.")
	}else{	
		//Create new record in the orders database.
		var qstring = "type=" + document.frmAddressSearch.reporttypevalue.value + "&address=" + document.frmAddressSearch.storedaddress.value + "&purpose=" + document.frmAddressMatches.storedreportpurpose.value;
		http = createRequestObject();
		http.open('get', 'storeorder.php?' + qstring); 
		http.onreadystatechange = handleStoreOrderResponse2;
		http.send(null);
	}
}

//****************Handles the response from storeorder.php to pass necessary information to map page (Called if user has searched by coordinates)
function handleStoreOrderResponse2() {
	if(http.readyState == 4){
		var passedOrderNo = http.responseText;
		if(passedOrderNo != ""){		
			if(passedOrderNo.substring(0,4) == "ASAP"){
				var theX = document.frmAddressSearch.storedx.value;				//Retrieve x-y from form
				var theY = document.frmAddressSearch.storedy.value;
				document.frmAddressSearch.storedorderno.value = passedOrderNo;		//Store order number on the form

				//Submit form to open map page
				document.frmAddressSearch.action = "map.php"; 
				document.frmAddressSearch.submit();
			}
		}else{
			alert("Could not initialize the order. Please try again")		
		}
	}
}

//-------------------------------------------------------------OTHER SCRIPTS


//****************Validate map page - pass to privacy policy page if OK
function mapCheck(){
	var isOK = true;
	var mapRefreshed = false;
	
	if((document.frmMap.reportcorrect1[0].checked == false) && (document.frmMap.reportcorrect1[1].checked == false)){
		alert("Please ensure location is verified.")
		isOK = false;
	}
	if((document.frmMap.reportcorrect2[0].checked == false) && (document.frmMap.reportcorrect2[1].checked == false)){
		alert("Please ensure boundary is verified.")
		isOK = false;
	}
	
	if(document.frmMap.reportcorrect1[1].checked){
		alert("Please select the correct location using the ‘select location’ feature on the toolbar.")
		isOK = false;
		//Turn on digitise button by refressing the map
		var iframObj = document.getElementById('mapwin')		
		var sep = iframObj.src.indexOf("&AllowArea")
		if(sep == -1){
			iframObj.src = iframObj.src + "&AllowArea=true";
		}else{
			iframObj.src = iframObj.src.substring(0,sep) + "&AllowArea=true";
		}			
		mapRefreshed = true;
		
		//var buttonObj = window.frames[0].document.getElementById('btnPolygon')
		//buttonObj.style.display="inline";
		//alert(buttonObj)
		//btnPolygon to "display: inline;"
		//alert(window.frames['mapwin'].document.getElementById('btnPolygon'));
		//alert(window.frames[0].document.getElementById('btnPolygon'));
		
		//alert(parent.frames['mapwin'].document.getElementById('btnPolygon'));
		//alert(parent.document.getElementById('mapwin'))  //.document.getElementById('btnPolygon'))
		//alert(parent.frames['mapwin'])
		//Ext.getDom('mapwin').contentWindow.method();	
		//var obj = parent.document.getElementById('btnPolygon');
		//obj.style.display="inline";		
	}
	
	if(document.frmMap.reportcorrect2[1].checked){
		alert("Please redraw the boundary.")
		isOK = false;
		//Turn on digitise button by refressing the map if not done so
		if(mapRefreshed == false){
			var iframObj = document.getElementById('mapwin')
			var sep = iframObj.src.indexOf("&AllowArea")
			if(sep == -1){
				iframObj.src = iframObj.src + "&AllowArea=true";
			}else{
				iframObj.src = iframObj.src.substring(0,sep) + "&AllowArea=true";
			}
		}		
	}
	
	if(isOK){
		document.frmMap.submit();
	}	
}

//****************Validate privacy policy
function validatePPForm(){
	var isOK = true;
	if(document.frmPrivacyPolicy.privpolicy.checked == false){
		alert("Please ensure the 'Privacy Policy' is entered.")
		isOK = false;
	}
	if((document.frmPrivacyPolicy.currentprice.value == "0") || (document.frmPrivacyPolicy.currentprice.value == "")){
		alert("There has been a problem retrieving the report price.")
		isOK = false;
	}	
	return isOK;
}

//****************Pass to WorldPay payment
function makePayment(){
	document.frmToken.submit()
}

//****************Go back to payment confirmation page after a unauthorized payment
function redoPayment(){
	document.frmConfirm.submit()
}

//****************Create request object
function createRequestObject() {
	var ro;		
	if (window.XMLHttpRequest) { 				// Mozilla, Safari,...
		ro = new XMLHttpRequest();
		//if (ro.overrideMimeType) {
		//	ro.overrideMimeType('text/xml');
		//}
	}else if (window.ActiveXObject) { 			// IE
		try {
			ro = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
			ro = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	return ro;
		
}

//****************Show Help popup
function showHelp(theAction,theType,e){

	evt = e || window.event;
	var theX = evt.clientX;
	var theY = evt.clientY;

	if(theAction == "on"){
		if(theType == "type"){
			$('typehelp').appear({ duration: 1.0, from: 0, to: 1 });
			$('typehelp').style.left = evt.clientX + "px";;
			$('typehelp').style.top = evt.clientY + "px";;
		}else if(theType == "maphelp"){
			$('maphelp').appear({ duration: 1.0, from: 0, to: 1 });
			$('maphelp').style.left = evt.clientX + "px";;
			$('maphelp').style.top = evt.clientY + "px";;		
		}else{
			$('puposehelp').appear({ duration: 1.0, from: 0, to: 1 });
			$('puposehelp').style.left = evt.clientX + "px";
			$('puposehelp').style.top = evt.clientY + "px";
		}
	}else{
		if(theType == "type"){
			$('typehelp').fade({ duration: 1.0, from: 1, to: 0 });
		}else if(theType == "maphelp"){
			$('maphelp').fade({ duration: 1.0, from: 1, to: 0 });
		}else{
			$('puposehelp').fade({ duration: 1.0, from: 1, to: 0 });
		}
	}
}

//*****************Button hover scripts
function swapImage(theButton,theImage){
	if(theImage == "next"){
		theButton.src = "images/btnNextOver.gif";	
	}else if(theImage == "submit"){
		theButton.src = "images/btnSubmitOver.gif";
	}else if(theImage == "view"){
		theButton.src = "images/btnViewOver.gif";	
	}else if(theImage == "update"){
		theButton.src = "images/btnUpdateOver.gif";	
	}else if(theImage == "go"){
		theButton.src = "images/btnLoginOver.gif";
	}else if(theImage == "edit"){
		theButton.src = "images/btnEditOver.gif";
	}else if(theImage == "back"){
		theButton.src = "images/btnRefineSearchOver.gif";
	}else if(theImage == "payment"){
		theButton.src = "images/btnMakePaymentOver.gif";
	}else if(theImage == "finish"){
		theButton.src = "images/btnFinishOver.gif";
	}else if(theImage == "accountreturn"){
		theButton.src = "http://www.greenlightreport.co.uk/orders/images/btnReturnToAccountOver.gif";
	}else if(theImage == "homereturn"){
		theButton.src = "http://www.greenlightreport.co.uk/orders/images/btnReturnToHomepageOver.gif";
	}
}
function swapImage2(theButton,theImage){
	if(theImage == "next"){
		theButton.src = "images/btnNext.gif";	
	}else if(theImage == "submit"){
		theButton.src = "images/btnSubmit.gif";	
	}else if(theImage == "view"){
		theButton.src = "images/btnView.gif";		
	}else if(theImage == "update"){
		theButton.src = "images/btnUpdate.gif";	
	}else if(theImage == "go"){
		theButton.src = "images/btnLogin.gif";
	}else if(theImage == "edit"){
		theButton.src = "images/btnEdit.gif";
	}else if(theImage == "back"){
		theButton.src = "images/btnRefineSearch.gif";	
	}else if(theImage == "finish"){
		theButton.src = "images/btnFinish.gif";
	}else if(theImage == "payment"){
		theButton.src = "images/btnMakePayment.gif";
	}else if(theImage == "accountreturn"){
		theButton.src = "http://www.greenlightreport.co.uk/orders/images/btnReturnToAccount.gif";	
	}else if(theImage == "homereturn"){
		theButton.src = "http://www.greenlightreport.co.uk/orders/images/btnReturnToHomepage.gif";
	}
}

//*****************Show report in a new window
function showReport(theReportName){
	theURL = reportPath + theReportName + ".pdf";	
	winName = "report";
	features = "toolbar=no,scrollbars=yes,resizable=yes,width=700,height=800";
	var theWin = window.open(theURL,winName,features);		
	theWin.focus();
}


//-----------------End of File