/*-------------------------------------------------------------------
login.js
For CentreMapsLive Ver. 2
Code for login/registering 
Lovell Jonhs Ltd.
01/01/10
//------------------------------------------------------------------*/

//-------------------------------------------------------------------------LOGIN SCRIPTS

//*******************Set focus on the login form
function setFocus(){
	document.loginform.username.focus();
}
//****************Show/Hide login form
function showLogin(){
	showHideRegister("hide");									//Ensure register info hidden							
	showHideLoginContent("content");
	if(document.getElementById("loginPopup") != null){
		document.getElementById("loginPopup").style.display = 'block';
	}	
}
function hideLogin(){
	if(document.getElementById("loginPopup") != null){
		document.getElementById("loginPopup").style.display = 'none';
	}
}
//****************Show/Hide login box content
function showHideLoginContent(contentToShow){
	if(contentToShow == "content"){
		if(document.getElementById("loginContent") != null){
			document.getElementById("loginContent").style.display = 'block';
		}
		if(document.getElementById("loginMessage") != null){
			document.getElementById("loginMessage").style.display = 'none';
		}
	}else if(contentToShow == "message"){
		if(document.getElementById("loginContent") != null){
			document.getElementById("loginContent").style.display = 'none';
		}
		if(document.getElementById("loginMessage") != null){
			document.getElementById("loginMessage").style.display = 'block';
		}
	}
}
//****************Run Login
function runLogin(theAction){
	//Validate input
	var isOK = true;
	if(theAction == "in"){		
		isOK = validateEmail();																//Check the 'User Name' field	
		if(isOK){
			//Check the 'Password' field			
			if (document.loginform.pwd.value == "") {											//Password field must not be empty
				showDialog('Warning','Please enter a value for the "Password" field.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
				isOK = false;
			}else if(!document.loginform.pwd.value.match("^[a-z|A-Z|0-9]*$")) {
				showDialog('Warning','Invalid Password. Password must contain only numbers and letters.<br/>If you used this password in the previous site, please login here and update. Alternatively contact CentreMaps.<br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');				
				isOK = false;		
			}
		}	
	}	
	if(isOK){
		var qstring = "name=" + document.loginform.username.value + "&pwd=" + document.loginform.pwd.value + "&login=" + theAction + "&type=normal";				
		if(window.location.pathname.indexOf("login.php") != -1){					//Login from the login page
			var loginReq = new ajaxObject('runlogin.php', loginResponse2);
		}else{																		//Login from the map page
			var loginReq = new ajaxObject('runlogin.php', loginResponse);
		}
		loginReq.update(qstring,'POST');
	}	
}
//****************Response from running the login
function loginResponse(responseText, responseStatus){
	if(responseStatus == 200){	
		//Get the returned values
		document.varform.cid.value = "";				//Ensure no customer id is stored			
		if(responseText != ""){
			var tempArray = new Array();
			tempArray = responseText.split("|");	
			if(tempArray[0] == "ok"){			
				if(tempArray[1] == "out"){				//Logging OUT
					//Reload the page in order to clear everything (calls changeMappingRegion with existing region)
					changeMappingRegion(document.varform.region.value,"","change");
				}else{									//Logging IN
					//Update header
					document.getElementById("loginInfo").innerHTML = "<a href='javascript:runLogin(\"out\");'>Logout: " + tempArray[1] + "</a> | <a href='javascript:goToHome();'>Your Account</a> | <a href='#'>View Cart</a>";
					//Hide login or register box
					if(tempArray[3] == "register"){
						showHideRegister("hide");
					}else if(tempArray[3] == "normal"){;
						hideLogin();
						//Get the most ordered product if on the main map page
						if(document.getElementById("mostOrderedContainer") != null){
							var qstring = "thereg=" + document.varform.region.value;
							var mostOrderedReq = new ajaxObject('getMostOrdered.php', getMostOrderedResponse);
							mostOrderedReq.update(qstring,'POST');		
						}											
					}
					//Store customer info in hidden 
					document.varform.cid.value = tempArray[2];
					document.varform.companyid.value = tempArray[5];
					document.varform.tenkMapType.value = tempArray[6];
					document.varform.isadmin.value = tempArray[7];
					document.varform.isprivilege.value = tempArray[8];
					document.varform.currentViewCredit.value = tempArray[9];
					//If the current map scale is to show detail reload the mapping the show this (indicates the user has logged in to show detailed mapping)
					if(map.getScale() <= largeScaleCheckScale){
						map.setCenter(new OpenLayers.LonLat(map.getCenter().lon,map.getCenter().lat)); 		//Just redraw with the same centre	
					}
				}
			}else if(tempArray[0] == "pending"){			//More than 1 user found - show links to choose the user		
				showHideLoginContent("message");
				document.getElementById("loginMessage").innerHTML = "Please choose the account - <br/>" + tempArray[4] + "<br/><a href='javascript:showHideLoginContent(\"content\");'>&lt; Back to Login.</a>";		
			}else{
				//Show error message
				showHideLoginContent("message");
				if(tempArray[3] == "register"){
					document.getElementById("registerMessage").innerHTML = "<br/><p class='error'>Customer not found. Please try <a href='javascript:showHideLoginContent(\"content\");'>again.</a></p>";
				}else if(tempArray[3] == "normal"){
					document.getElementById("loginMessage").innerHTML = "<br/><p class='error'>Customer not found. Please try <a href='javascript:showHideLoginContent(\"content\");'>again.</a></p>";
				}							
			}
		}else{
			//Show error message
			showHideLoginContent("message");
			if(tempArray[3] == "register"){
				document.getElementById("registerMessage").innerHTML = "<br/><p class='error'>Customer not found. Please try <a href='javascript:showHideLoginContent(\"content\");'>again.</a></p>";
			}else if(tempArray[3] == "normal"){
				document.getElementById("loginMessage").innerHTML = "<br/><p class='error'>Customer not found. Please try <a href='javascript:showHideLoginContent(\"content\");'>again.</a></p>";
			}
		}
	}
}


//****************Response from running the login when run from login page
function loginResponse2(responseText, responseStatus){
	if(responseStatus == 200){	
		//Get the returned values
		document.varform.cid.value = "";				//Ensure no customer id is stored			
		if(responseText != ""){
			var tempArray = new Array();
			tempArray = responseText.split("|");	
			if(tempArray[0] == "ok"){			
				if(tempArray[1] == "out"){				//Logging OUT
				}else{									//Logging IN
					//Store customer info in hidden 
					document.varform.cid.value = tempArray[2];
					document.varform.companyid.value = tempArray[5];
					document.varform.tenkMapType.value = tempArray[6];
					document.varform.isadmin.value = tempArray[7];
					document.varform.isprivilege.value = tempArray[8];
					document.varform.currentViewCredit.value = tempArray[9];
					goToHome();
				}
			}else if(tempArray[0] == "pending"){			//More than 1 user found - show links to choose the user		
				document.getElementById("loginMessage").innerHTML = "Please choose the account - <br/>" + tempArray[4];		
			}else{
				//Show error message
				if(tempArray[3] == "register"){
					document.getElementById("registerMessage").innerHTML = "<br/><p class='error'>Customer not found. Please try again.</p>";
				}else if(tempArray[3] == "normal"){
					document.getElementById("loginMessage").innerHTML = "<br/><p class='error'>Customer not found. Please try again.</p>";
				}							
			}
		}else{
			//Show error message
			if(tempArray[3] == "register"){
				document.getElementById("registerMessage").innerHTML = "<br/><p class='error'>Customer not found. Please try again.</p>";
			}else if(tempArray[3] == "normal"){
				document.getElementById("loginMessage").innerHTML = "<br/><p class='error'>Customer not found. Please try again.</p>";
			}
		}
	}
}
//*******************Login from choice where user has more than one account with the same password and id
function reLogin(itemNo){
	var qstring = "cid=" + itemNo + "&type=normal";
	if(window.location.pathname.indexOf("login.php") != -1){					//Called from login page	
		var loginReq = new ajaxObject('runlogin.php', loginResponse2);
	}else{																		//Called from map page
		var loginReq = new ajaxObject('runlogin.php', loginResponse);
	}
	loginReq.update(qstring,'POST');					
}
//*******************Open login area when prompted to do so when trying to look at detailed mapping
function loginForMM(){
	closePreviewsInfoContent();
	showLogin();
}
//*******************Open register area when prompted to do so when trying to look at detailed mapping
function registerForMM(){
	closePreviewsInfoContent();
	showHideRegister("show");
}

//****************Response from retrieving the most ordered product
function getMostOrderedResponse(responseText, responseStatus){
	if(responseStatus == 200){			
		document.getElementById("mostOrderedContainer").innerHTML = responseText;
	}
}
//-------------------------------------------------------------------------REMINDER SCRIPTS
//****************Send Password Reminder to the user
function sendPasswordReminder(){
	var isOK = validateEmail();				//Validate input
	if(isOK){	
		var qstring = "name=" + document.loginform.username.value;
		var passwordReq = new ajaxObject('reminder.php', reminderResponse);
		passwordReq.update(qstring,'POST');
	}
}
//****************Response from sending password reminder
function reminderResponse(responseText, responseStatus){
	if(responseStatus == 200){
		showHideLoginContent("message");			
		if(window.location.pathname.indexOf("login.php") != -1){		//Run from login page
			if(responseText != ""){
				document.getElementById("loginMessage").innerHTML = "<br/><p>" + responseText + "</p>";
			}else{
				document.getElementById("loginMessage").innerHTML = "<br/><p>There has been a problem retrieving your password.<br/>Ensure your email address is correct or contact Centremaps.</p>";
			}
		}else{															//Run from map page
			if(responseText != ""){
				document.getElementById("loginMessage").innerHTML = "<br/><p>" + responseText + "</p><br/><a href='javascript:hideLogin();'>Close [x]</a>";
			}else{
				document.getElementById("loginMessage").innerHTML = "<br/><p>There has been a problem retrieving your password.<br/>Ensure your email address is correct or contact Centremaps.</p><br/><a href='javascript:hideLogin();'>Close [x]</a>";
			}	
		}		
	}
}

//-------------------------------------------------------------------------VALIDATION SCRIPTS

//****************Check required email is specified correctly (Simple check)
function validateEmail(){
	var isOK = true;
	if (document.loginform.username.value == "") {											//Name field must not be empty
		showDialog('Warning','Please enter your registered Email in the "Email Address" field.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
		isOK = false;
	}else if((document.loginform.username.value.search("@") == -1) || (document.loginform.username.value.search("[.*]") == -1 )){
		showDialog('Warning','Please ensure your "Email Address" is a valid email address.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
		isOK = false;
	}
	return isOK;
}
//*******************Email validity check (Full check)
function emailCheck (emailStr) {
	/* 1.1.4: Fixed a bug where upper ASCII characters (i.e. accented letters
	international characters) were allowed.	
	1.1.3: Added the restriction to only accept addresses ending in two
	letters (interpreted to be a country code) or one of the known
	TLDs (com, net, org, edu, int, mil, gov, arpa), including the
	new ones (biz, aero, name, coop, info, pro, museum).  One can
	easily update the list (if ICANN adds even more TLDs in the
	future) by updating the knownDomsPat variable near the
	top of the function.  Also, I added a variable at the top
	of the function that determines whether or not TLDs should be
	checked at all.  This is good if you are using this function
	internally (i.e. intranet site) where hostnames don't have to 
	conform to W3C standards and thus internal organization e-mail
	addresses don't have to either.
	Changed some of the logic so that the function will work properly
	with Netscape 6.	
	1.1.2: Fixed a bug where trailing . in e-mail address was passing
	(the bug is actually in the weak regexp engine of the browser; I
	simplified the regexps to make it work).	
	1.1.1: Removed restriction that countries must be preceded by a domain,
	so abc@host.uk is now legal.  However, there's still the 
	restriction that an address must end in a two or three letter
	word.	
	1.1: Rewrote most of the function to conform more closely to RFC 822.
	1.0: Original  */
	/* The following variable tells the rest of the function whether or not
	to verify that the address ends in a two-letter country or well-known
	TLD.  1 means check it, 0 means don't. */
	var checkTLD=1;
	/* The following is the list of known TLDs that an e-mail address must end with. */
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	/* The following pattern is used to check if the entered e-mail address
	fits the user@domain format.  It also is used to separate the username
	from the domain. */
	var emailPat=/^(.+)@(.+)$/;
	/* The following string represents the pattern for matching all special
	characters.  We don't want to allow special characters in the address. 
	These characters include ( ) < > @ , ; : \ " . [ ] */
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	/* The following string represents the range of characters allowed in a 
	username or domainname.  It really states which chars aren't allowed.*/
	var validChars="\[^\\s" + specialChars + "\]";
	/* The following pattern applies if the "user" is a quoted string (in
	which case, there are no rules about which characters are allowed
	and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	is a legal e-mail address. */
	var quotedUser="(\"[^\"]*\")";
	/* The following pattern applies for domains that are IP addresses,
	rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	e-mail address. NOTE: The square brackets are required. */
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	/* The following string represents an atom (basically a series of non-special characters.) */
	var atom=validChars + '+';
	/* The following string represents one word in the typical username.
	For example, in john.doe@somewhere.com, john and doe are words.
	Basically, a word is either an atom or quoted string. */
	var word="(" + atom + "|" + quotedUser + ")";
	// The following pattern describes the structure of the user
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	/* The following pattern describes the structure of a normal symbolic
	domain, as opposed to ipDomainPat, shown above. */
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	/* Finally, let's start trying to figure out if the supplied address is valid. */	
	/* Begin with the coarse pattern to simply break up user@domain into
	different pieces that are easy to analyze. */
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null) {
		/* Too many/few @'s or something; basically, this address doesn't
		even fit the general mould of a valid e-mail address. */
		showDialog('Warning','Email address seems incorrect (check @ and .\'s)<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	// Start by checking that only basic ASCII characters are in the strings (0-127).
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			showDialog('Warning','The username contains invalid characters.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
			return false;
		}
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			showDialog('Warning','The domain name contains invalid characters.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
			return false;
		}
	}
	// See if "user" is valid 
	if (user.match(userPat)==null) {
		// user is not valid
		showDialog('Warning','The username does not seem to be valid.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
		return false;
	}
	/* if the e-mail address is at an IP address (as opposed to a symbolic
	host name) make sure the IP address is valid. */
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		// this is an IP address
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				showDialog('Warning','Destination IP address is invalid!<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
				return false;
		   	}
		}
		return true;
	}	
	// Domain is symbolic name.  Check if it's valid.
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			showDialog('Warning','The domain name does not seem to be valid.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
			return false;
	   	}
	}
	/* domain name seems valid, but now make sure that it ends in a
	known top-level domain (like com, edu, gov) or a two-letter word,
	representing country (uk, nl), and that there's a hostname preceding 
	the domain or country. */
	if (checkTLD && domArr[domArr.length-1].length!=2 && 
		domArr[domArr.length-1].search(knownDomsPat)==-1) {
		showDialog('Warning','The address must end in a well-known domain or two letter country.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
		return false;
	}
	// Make sure there's a host name preceding the domain.
	if (len<2) {
		showDialog('Warning','This address is missing a hostname!<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
		return false;
	}
	// If we've gotten this far, everything's valid!
	return true;
}

//****************Validate user input (Registering)
function validateInput(theType){
	//Name field must not be empty
	if(document.getElementById("name").value == "") {
		showDialog('Warning','Please enter a value for the "Full Name" field.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
		document.getElementById("name").focus();
		return (false);
	}	
	//Email Address field must not be empty and valid address
	if(theType != "confirm"){														//No need to perform the check during order confirmation process
		if (document.getElementById("email").value==""){
			showDialog('Warning','Please enter a value for the "Email Address" field.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
			document.getElementById("email").focus();
			return (false);
		}	
		if(emailCheck(document.getElementById("email").value) == false){
			document.getElementById("email").focus();
			return (false);	
		}		
		//Password field must not be empty
		if (document.getElementById("pwd1").value == "") {
			showDialog('Warning','Please enter a value for the "Password" field.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
			document.getElementById("pwd1").focus();
			return (false);
		}	
		if((document.getElementById("pwd1").value.length > 20) || (document.getElementById("pwd1").value.length < 6) ){
			showDialog('Warning','Please ensure "Password" is between 6 and 20 characters.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
			document.getElementById("pwd1").focus();
			return (false);
		}
	}
	//Check the following as part of the confirmation process
	if(theType == "confirm"){
		//Address field must not be empty
		if(document.getElementById("address1").value == "") {
			showDialog('Warning','Please enter a value for the "Address" field.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
			document.getElementById("address1").focus();
			return (false);
		}
		//Town field must not be empty
		if(document.getElementById("town").value == "") {
			showDialog('Warning','Please enter a value for the "Town" field.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
			document.getElementById("town").focus();
			return (false);
		}
		//Post Code field must not be empty
		if(document.getElementById("postcode").value == "") {
			showDialog('Warning','Please enter a value for the "Post Code" field.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
			document.getElementById("postcode").focus();
			return (false);
		}
		//Check Telephone for telephone number
		if(document.getElementById("telephone").value == ""){
			showDialog('Warning','Please enter a value for the "Telephone" field.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
			document.getElementById("telephone").focus();
			return (false);
		}
		var checkOK = "0123456789-+() \t\r\n\f";
		var checkStr = document.getElementById("telephone").value;
		var allValid = true;
		for (i = 0;  i < checkStr.length;  i++){
			ch = checkStr.charAt(i);
			for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
			break;
			if (j == checkOK.length){
			allValid = false;
			break;
			}
		}		
		if (!allValid){
			showDialog('Warning','Please enter only digit, whitespace and "+()" characters in the "Telephone" field.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
			document.getElementById("telephone").focus();
			return (false);
		}
	}
	//If the further details are required check input is OK
	if(theType == "register"){
		//Check passwords match 		
		if(document.getElementById("pwd1").value != document.getElementById("pwd2").value) {
			showDialog('Warning','"Password" not verified correctly.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
			document.getElementById("pwd2").focus();
			document.getElementById("pwd2").value = "";
			return (false);
		}
		if(document.getElementById("regdetails").className == "bodyon"){	
			//Address field must not be empty
			if(document.getElementById("address1").value == "") {
				showDialog('Warning','Please enter a value for the "Address" field.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
				document.getElementById("address1").focus();
				return (false);
			}
			//Town field must not be empty
			if(document.getElementById("town").value == "") {
				showDialog('Warning','Please enter a value for the "Town" field.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
				document.getElementById("town").focus();
				return (false);
			}	
			//Post Code field must not be empty
			if(document.getElementById("postcode").value == "") {
				showDialog('Warning','Please enter a value for the "Post Code" field.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
				document.getElementById("postcode").focus();
				return (false);
			}	
			//Check Telephone for telephone number
			if(document.getElementById("telephone").value == ""){
				showDialog('Warning','Please enter a value for the "Telephone" field.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
				document.getElementById("telephone").focus();
				return (false);
			}
			var checkOK = "0123456789-+() \t\r\n\f";
			var checkStr = document.getElementById("telephone").value;
			var allValid = true;
			for (i = 0;  i < checkStr.length;  i++){
				ch = checkStr.charAt(i);
				for (j = 0;  j < checkOK.length;  j++)
				if (ch == checkOK.charAt(j))
				break;
				if (j == checkOK.length){
				allValid = false;
				break;
				}
			}		
			if (!allValid){
				showDialog('Warning','Please enter only digit, whitespace and "+()" characters in the "Telephone" field.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
				document.getElementById("telephone").focus();
				return (false);
			}
		}
	}	
	//All OK
	return (true);
}

//-------------------------------------------------------------------------REGISTER/UPDATE SCRIPTS

//****************Show/Hide register form
function showHideRegister(theAction){
	if(theAction == "show"){		
		hideLogin();															//Hide login form
		if(document.getElementById("searchOptionsContent") != null){			//Ensure serach options layer is invisible (as register maybe activated from this)		
			document.getElementById("searchOptionsContent").style.display = "none";	
		}
		var regDetailsReq = new ajaxObject('registerDetails.php', regDetailsResponse);
		regDetailsReq.update('','POST');	
	}else if(theAction == "hide"){
		if(document.getElementById("registerPopup2") != null){
			document.getElementById("registerPopup2").style.display = "none";		//Hide register form (from login page)
		}else{
			document.getElementById("registerPopup").style.display = "none";		//Hide register form (from map page)
		}
	}
}
//****************Response from getting register info
function regDetailsResponse(responseText, responseStatus){
	if(responseStatus == 200){
		if(document.getElementById("registerPopup2") != null){
			document.getElementById("registerContent2").innerHTML = responseText;		//Show register form
			document.getElementById("registerPopup2").style.display = "block";		//Show register form (from login page)
		}else{
			document.getElementById("registerContent").innerHTML = responseText;		//Show register form
			document.getElementById("registerPopup").style.display = "block";		//Show register form (from map page)
		}
		callCufon();	
	}
}
//****************Defines options from check boxes 
function setCheckOptions(theControl,theSource){
	if(theControl == "mmaccess"){
		if(document.getElementById("mmaccesschk").checked){
			document.getElementById("mmaccess").value = "pending";
			if(theSource == "new"){
				regShowHide("show");
			}
		}else{
			document.getElementById("mmaccess").value = "no";
			if(theSource == "new"){
				regShowHide("hide");
			}	
		}	
	}else if(theControl == "accaccess"){
		if(document.getElementById("accaccesschk").checked){
			document.getElementById("accaccess").value = "pending";
			if(theSource == "new"){
				regShowHide("show");
			}	
		}else{
			document.getElementById("accaccess").value = "no";
			if(theSource == "new"){
				regShowHide("hide");
			}
		}	
	}else if(theControl == "mailing1"){
		if(document.getElementById("mailing1chk").checked){
			document.getElementById("mailing1").value = "yes";	
		}else{
			document.getElementById("mailing1").value = "no";
		}	
	}else if(theControl == "mailing2"){
		if(document.getElementById("mailing2chk").checked){
			document.getElementById("mailing2").value = "yes";	
		}else{
			document.getElementById("mailing2").value = "no";
		}	
	}else if(theControl == "notifyadmin"){
		if(document.getElementById("notifyadminchk").checked){
			document.getElementById("notifyadmin").value = "yes";	
		}else{
			document.getElementById("notifyadmin").value = "no";
		}
	}else if(theControl == "notifyuser"){
		if(document.getElementById("notifyuserchk").checked){
			document.getElementById("notifyuser").value = "yes";	
		}else{
			document.getElementById("notifyuser").value = "no";
		}
	}
}
//*******************Defines options from check boxes (Company Details)
function setCompCheckOptions(theControl){
	if(theControl == "mmaccess"){
		if(document.compform.mmaccesschk.checked){
			document.compform.mmaccess.value = 'pending';	
		}else{
			document.compform.mmaccess.value = 'no';
		}	
	}else if(theControl == "accaccess"){
		if(document.compform.accaccesschk.checked){
			document.compform.accaccess.value = 'pending';	
		}else{
			document.compform.accaccess.value = 'no';
		}
	}else if(theControl == "paperinvoice"){
		if(document.compform.paperinvoicechk.checked){
			document.compform.paperinvoice.value = 'yes';	
		}else{
			document.compform.paperinvoice.value = 'no';
		}		
	}else if(theControl == "geninvoice"){
		if(document.compform.geninvoicechk.checked){
			document.compform.geninvoice.value = 'yes';	
		}else{
			document.compform.geninvoice.value = 'no';
		}
	}else if(theControl == "oneclickorder"){
		if(document.compform.oneclickorderchk.checked){
			document.compform.oneclickorder.value = 'yes';	
		}else{
			document.compform.oneclickorder.value = 'no';
		}
	}else if(theControl == "adminnotify"){
		if(document.compform.adminnotifychk.checked){
			document.compform.adminnotify.value = "yes";	
		}else{
			document.compform.adminnotify.value = "no";
		}
	}
}
//****************Hides or shows further registartion details
function regShowHide(theAction){
	if(theAction == "show"){
		document.getElementById("regdetails").style.display = "block";	
	}else{
		document.getElementById("regdetails").style.display = "none";			
	}
}
//****************Register the new user
function registerUser(){
	if(validateInput("register")){		//If all OK register the user
		//Define the query string to pass variables
		var qstring = "name=" + document.registerform.name.value;
		qstring += "&email=" + document.registerform.email.value;
		qstring += "&pwd1=" + document.registerform.pwd1.value;
		qstring += "&company=" + document.registerform.company.value;		
		qstring += "&address1=" + document.registerform.address1.value;
		qstring += "&address2=" + document.registerform.address2.value;
		qstring += "&town=" + document.registerform.town.value;
		qstring += "&county=" + document.registerform.county.value;
		qstring += "&postcode=" + document.registerform.postcode.value;
		qstring += "&country=" + document.registerform.country.value;
		qstring += "&telephone=" + document.registerform.telephone.value;
 		qstring += "&fax=" + document.registerform.fax.value;
 		qstring += "&outcode=" + document.registerform.outcode.value;
		qstring += "&mmaccess=" + document.registerform.mmaccess.value;
		qstring += "&accaccess=" + document.registerform.accaccess.value;
		qstring += "&mailing1=" + document.registerform.mailing1.value;
		qstring += "&mailing2=" + document.registerform.mailing2.value;
		qstring += "&code=" + document.registerform.code.value;
		qstring += "&cadd=register";
		var registerReq = new ajaxObject('upCustomer.php', registerResponse);
		registerReq.update(qstring,'POST');
	}
}
//****************Response from registering
function registerResponse(responseText, responseStatus){
	if(responseStatus == 200){	
		//Get the returned values
		if(responseText != ""){
			var tempArray = new Array();
			tempArray = responseText.split("|");
			if(tempArray[0] == "yes"){
				//Show message and run the login;
				showDialog('Success',tempArray[1] + '<br/><br/><input type="button" value="Continue" id="contButton" onclick="loginAfterRegister();" >','success');			
			}else{								//Could not register for some reason, show the error message
				showDialog('Error',tempArray[1] + '<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','errordialog');
			}		
		}else{
			showDialog('Error','There has been a problem registering. Please try again.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','errordialog');
		}
	}
}
//****************Login the user after registering
function loginAfterRegister(){
	hideDialog();
	var qstring = "name=" + document.registerform.email.value + "&pwd=" + document.registerform.pwd1.value + "&login=in&type=register";
	if(window.location.pathname.indexOf("login.php") != -1){					//Called from login page
		var loginReq = new ajaxObject('runlogin.php', loginResponse2);
	}else{																		//Called from map page
		var loginReq = new ajaxObject('runlogin.php', loginResponse);
	}
	loginReq.update(qstring,'POST');
}
//****************Update user info
function updateUserInfo(){
	if(validateInput("update")){		//If all OK register the user	
		//Define the query string to pass variables
		var qstring = "name=" + document.custform.name.value;
		qstring += "&email=" + document.custform.email.value;
		qstring += "&pwd1=" + document.custform.pwd1.value;
		qstring += "&company=" + document.custform.company.value;		
		qstring += "&address1=" + document.custform.address1.value;
		qstring += "&address2=" + document.custform.address2.value;
		qstring += "&town=" + document.custform.town.value;
		qstring += "&county=" + document.custform.county.value;
		qstring += "&postcode=" + document.custform.postcode.value;
		qstring += "&country=" + document.custform.country.value;
		qstring += "&telephone=" + document.custform.telephone.value;
 		qstring += "&fax=" + document.custform.fax.value;
 		qstring += "&outcode=" + document.custform.outcode.value;
		qstring += "&mmaccess=" + document.custform.mmaccess.value;
		qstring += "&accaccess=" + document.custform.accaccess.value;
		qstring += "&mailing1=" + document.custform.mailing1.value;
		qstring += "&mailing2=" + document.custform.mailing2.value;	
		qstring += "&notifyuser=" + document.custform.notifyuser.value;
		if(document.getElementById("notifyadmin") != null){
			qstring += "&notifyadmin=" + document.custform.notifyadmin.value;
		}	
		qstring += "&daddress1=" + document.custform.daddress1.value;
		qstring += "&daddress2=" + document.custform.daddress2.value;
		qstring += "&dtown=" + document.custform.dtown.value;
		qstring += "&dcounty=" + document.custform.dcounty.value;
		qstring += "&dpostcode=" + document.custform.dpostcode.value;
		qstring += "&dcountry=" + document.custform.dcountry.value;
		qstring += "&cadd=update";
		var loginReq = new ajaxObject('upCustomer.php', updateUserResponse);
		loginReq.update(qstring,'POST');
	}
}
//****************Response from updating user info
function updateUserResponse(responseText, responseStatus){
	if(responseStatus == 200){
		//Get the returned values
		if(responseText != ""){
			var tempArray = new Array();
			tempArray = responseText.split("|");
			//Show the message
			showDialog('',tempArray[1] + '<br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','prompt');	
		}else{
			showDialog('Error','There has been a problem updating. Please try again.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','errordialog');
		}
	}
}
//****************Add view credit to user/company account
function addViewCredit(theForm){
	if(isNaN(document.getElementById("addcredit").value)){
		showDialog('Warning','Please add a credit amount.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
	}else{
		var creditToAdd = Number(document.getElementById("addcredit").value);
		if(creditToAdd < 5){
			showDialog('Warning','The minimum amount of credit you can add is &pound;5<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
		}else{
			eval("document." + theForm + ".action = 'addCredit.php'");
			eval("document." + theForm + ".submit()");
		}
	}
}
//-------------------------------------------------End of file.
