function setPAPreferences() {
	minisitename = getMinisiteName();
	window.location = "subscribe_to_planning_alerts.php?minisitename="+minisitename;
}

function setEcoTextPreferences() {
	minisitename = getMinisiteName();
	window.location = "subscribe_to_ecotext_alerts.php?minisitename="+minisitename;
}

function loadSMSForm() {

		xhReq = createXMLHttpRequest();
		poststring = "tablename=sms_preferences";
		 poststring = poststring +"&action=loadSMSForm";
		 xhReq.open("POST", "ecoPADetails.php", true);
		 xhReq.onreadystatechange = loadSMSFormResponse;
		try {
		   xhReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		   //xhReq.setRequestHeader('Content-Type','text/html')
		 }
		 catch (e) {
			// this only works in internet explorer
		 }
		xhReq.send(poststring);

}

function loadSMSFormResponse() {
   if (xhReq.readyState != 4)  {
   		return;
   	}
	var result = xhReq.responseText;
	var htmlscript = result.split("|XX|");
	var headID = document.getElementsByTagName("head")[0];
	var newScript = document.createElement('script');
	newScript.type = 'text/javascript';

	newScript.text = htmlscript[1];
	headID.appendChild(newScript);

	document.getElementById('registerdiv').innerHTML = htmlscript[0];
		
}

function saveSMSChanges() {
	if (checkData()) {
		var postcode = document.getElementById("postcode*").value;
		//alert(postcode);
		//newdata = "false"; // this is an existing record
		checkSMSPostcode(postcode);
	}
}

function checkSMSPostcode(postcode) {

	      //alert(postcode);
	      xhReq = createXMLHttpRequest();
	
	 xhReq.open("GET", "checkPostcode.php?postcode="+postcode, true);
	 xhReq.onreadystatechange = sms_postcodeChecked;

	
	 try {
	   xhReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	   //xhReq.setRequestHeader('Content-Type','text/html')
	 }
	 catch (e) {
	 	// this only works in internet explorer
	 }
	//alert("presend");
	xhReq.send(null);
	

}

function sms_postcodeChecked() {
		if (xhReq.readyState != 4)  {
	   		return;
	   	}
		var response = xhReq.responseText;
		
		if (response == "Nomatch") {
			alert("Your postcode was not matched.  This may be because you typed it incorrectly, or because it is outside London.  Please try again");
			return;
		}
		
		if (check_details() == true) {
			// if we got to here we have a match
			var coords = response.split(":");
			var latitude = coords[0];
			var longitude = coords[1];
			//alert(latitude);
			//alert(longitude);

			// create a point using these coords
			var point = new GLatLng(latitude, longitude);
			var distance = document.getElementById("radius*").value;
			var pointstring = drawCircle(point,distance,4);
			//alert(pointstring);
			//var poststring 	 = getPostString("sms_preferences")+pointstring;
			//alert(poststring);
			//alert(newdata);

			poststring = getAlertPostString("sms_preferences");
			poststring = poststring+"&approved=0&"+pointstring;
			//sendSMSDataToServer(poststring,newdata);

			sendAlertDataToServer(poststring,"saveSMSChanges");
		}	

}


/* ========================
PLANNING ALERTS
=========================*/
function loadPAForm() {

		xhReq = createXMLHttpRequest();
		poststring = "tablename=pa_preferences";
		 poststring = poststring +"&action=loadPAForm";
		 xhReq.open("POST", "ecoPADetails.php", true);
		 xhReq.onreadystatechange = loadPAFormResponse;
		try {
		   xhReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		   //xhReq.setRequestHeader('Content-Type','text/html')
		 }
		 catch (e) {
			// this only works in internet explorer
		 }
		xhReq.send(poststring);

}

function loadPAFormResponse() {
   if (xhReq.readyState != 4)  {
   		return;
   	}
	var result = xhReq.responseText;
	var htmlscript = result.split("|XX|");
	var headID = document.getElementsByTagName("head")[0];
	var newScript = document.createElement('script');
	newScript.type = 'text/javascript';

	newScript.text = htmlscript[1];
	headID.appendChild(newScript);

	document.getElementById('registerdiv').innerHTML = htmlscript[0];
		
}

function savePAChanges() {
	if (checkData()) {
		var postcode = document.getElementById("postcode*").value;
		//alert(postcode);
		//newdata = "false"; // this is an existing record
		checkPAPostcode(postcode);
	}
}

function checkPAPostcode(postcode) {

	      //alert(postcode);
	      xhReq = createXMLHttpRequest();
	
	 xhReq.open("GET", "checkPostcode.php?postcode="+postcode, true);
	 xhReq.onreadystatechange = pa_postcodeChecked;

	
	 try {
	   xhReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	   //xhReq.setRequestHeader('Content-Type','text/html')
	 }
	 catch (e) {
	 	// this only works in internet explorer
	 }
	//alert("presend");
	xhReq.send(null);
	

}

function pa_postcodeChecked() {
		if (xhReq.readyState != 4)  {
	   		return;
	   	}
		var response = xhReq.responseText;

		
		if (response == "Nomatch") {
			alert("Your postcode was not matched.  This may be because you typed it incorrectly, or because it is outside London.  Please try again");
			return;
		}
		
		if (check_details() == true) {
			// if we got to here we have a match
			var coords = response.split(":");
			var latitude = coords[0];
			var longitude = coords[1];
			//alert(latitude);
			//alert(longitude);

			// create a point using these coords
			var point = new GLatLng(latitude, longitude);
			var distance = document.getElementById("pa_distance*").value;
			var pointstring = drawCircle(point,distance,4);
			//alert(pointstring);
			//var poststring 	 = getPostString("sms_preferences")+pointstring;
			//alert(poststring);
			//alert(newdata);

			poststring = getAlertPostString("pa_preferences");
			poststring = poststring+"&approved=0&"+pointstring;

			//sendSMSDataToServer(poststring,newdata);

			sendAlertDataToServer(poststring,"savePAChanges");
		}	

}






/* ========================
SHARED CODE
==========================*/

function drawCircle(center, radius, numpoints) 
{ 
// Esa 2006 
        //calculating km/degree 
	
	var lng = parseFloat(center.lng());
	var lat = parseFloat(center.lat());
	
	var newlng = parseFloat(lng + 0.1);
	var newlat = parseFloat(lat + 0.1);


	// the divide by 100 is to convert into KM

        var latConv = center.distanceFrom(new GLatLng(newlat, lng))/100; 
        var lngConv = center.distanceFrom(new GLatLng(lat, newlng))/100; 

        //Loop 
        var points = []; 
        var pointstring = "";
        var step = parseInt(360/numpoints); 
             //alert(center.lat());
     //   alert(center.lng());

        for(var i=0; i<=360; i+=step) 
   
        { 
		
	        var pint = new GLatLng(lat + (radius/latConv * Math.sin(i * Math.PI/180)), lng + (radius/lngConv * Math.cos(i * Math.PI/180))); 
        	if (i == 0) {
        		pointstring=pointstring + "&maxlng="+parseFloat(pint.lng());
        	}
        	if (i == 90) {
        		pointstring=pointstring + "&maxlat="+parseFloat(pint.lat());
        	}
        	if (i == 180) {
        		pointstring=pointstring + "&minlng="+parseFloat(pint.lng());
        	}
        	if (i == 270) {
        		pointstring=pointstring + "&minlat="+parseFloat(pint.lat());
        	}
        } 
        return pointstring;
}

function getAlertPostString(tablename) {
	var postdata = formData("userDataCapture");
	var poststring = stripNonValidXMLCharacters(postdata);
	poststring = poststring.substr(0,poststring.length - 1);
	
	// now add the tablename
	poststring = poststring + "&tablename="+tablename;
	return poststring;
	
}



function sendAlertDataToServer(poststring,action) {
	xhReq = createXMLHttpRequest();
	
	// remove any dodgy characters
	poststring = poststring + "&action="+action;
	 xhReq.open("POST", "ecoPADetails.php", true);
	 xhReq.onreadystatechange = alertDataModerated;

	 //
	 try {
	   xhReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	   //xhReq.setRequestHeader('Content-Type','text/html')
	 }
	 catch (e) {
	 	// this only works in internet explorer
	 }
	xhReq.send(poststring);
}


function alertDataModerated() {
	if (xhReq.readyState != 4)  {
   		return;
   	}
	var result = xhReq.responseText;
//	alert(result);
	if (result.indexOf("Your changes have been saved") > -1) {
		alert("Your changes have been saved");
	}
	// go back to the user details page
	minisitename = getMinisiteName();
	window.location = "myDetails.php?minisitename="+minisitename;

	
}

function check_details(){

		// now check whether the user has provide a phone number and/or an e-mail depending on 
		// whether they want an e-mail or a phone alert
		
		
		
		// NOVEMBER 2009 TEMPORARY CODE
		//var alertType = document.getElementById("alert_type*").value;  // REINSTATE THIS LINE
		var alertType = 1;  // remove this line once we re-enable the sms alerts!!

		if (alertType == 1) {
			//E-mail
			var email = document.getElementById("e_mail_address").value;
			if (email.length == 0) {
				alert("Please enter an e-mail address");
				return false;
			}
		}
		if (alertType == 2) {
			//Text message
			var mobilenumber = document.getElementById("mobile_number").value;					
			if (mobilenumber.length == 0) {
				alert("Please enter a mobile number");
				return false;
			}
		}
		if (alertType == 3) {
			//Both e-mail and text message
			var email = document.getElementById("e_mail_address").value;					
			if (email.length == 0) {
				alert("Please enter an e-mail address");
				return false;
			}

			var mobilenumber = document.getElementById("mobile_number").value;					
			if (mobilenumber.length == 0) {
				alert("Please enter a mobile number");
				return false;
			}
		}

		return true;
}
