	function convert(url)
  	{
  		var re = /&/g;
  		return url.replace(re, "^");
  	}  
  	
  	function open_win(page)
	{
		newwindow=window.open(srvurl+page,'name','width=550,height=450,top=200');
		if (window.focus) {newwindow.focus()};
		return false;
	}
	function killcache()
	{
		return "&dummy="+new Date().getTime();
	}
///////////////////
	function decURL(text) {
		try {
			return decodeURIComponent(text);
		} catch (ed1) {
			return text;
		}
	}
	function encURL(text) {
/*
//100112 replace CRLF with , if not ending with ,
		var spl=false;
		if(text.indexOf("\r\n") >= 0) {
			spl=true; splt="\r\n";
		} else if(text.indexOf("\n") >= 0) {
			spl=true; splt="\n";
		}
		if(spl) {
			var ml = text.split(splt);
			if(ml.length>1) {
				var strres=ml[0];
				for(i=1;i<ml.length;i++){
					if(ml[i-1].substr(ml[i-1].length-1,1)!=",") {
						strres+=",";
					}
					strres+="\n" + ml[i];  //abc, def, ghi
				}
				text=strres;
			}
		}
*/
//		text=convert(text);
  		text = text.replace(/\r\n/g, "\n");
  		text = text.replace(/'/g, "`");
//  		text = text.replace(/\"/g, "&quot;");
		text = encodeURIComponent(text);
		return text;
	}
	function addComma(str) {
		if(str.substr(str.length-1,1)!=",") {
			str+=",";
		}
		return str;
	}
////////////////////////////////////////////////////////    
    function createRequest() {
		var ajaxRequest;
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e1){
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}catch (e2){
				ajaxRequest = new XMLHttpRequest();
			}
		}
		return ajaxRequest;
	}
	// search for a code to View (maps/find) - get star rating, Res/Bus flag and Business info
	function lookupcode(code) {
		var ajax_connection = createRequest();
		ajax_connection.open('get', "/exmap/fetchlocation.php?pc="+code+"&dummy="+new Date().getTime());
        	ajax_connection.onreadystatechange = function() {
			if (ajax_connection.readyState == 4) {
    			if (ajax_connection.status == 200) {
      				var xmlDoc = ajax_connection.responseXML;
      				var latlngs=xmlDoc.documentElement.getElementsByTagName("location");
      				foundLatLng(code, latlngs);
      				return;
				}
			}
		} 
        	ajax_connection.send(null);      				
	}

	// creating a new code , discover if it exists, get address and email
	//100108
	function codeexists(code,mapType,rb) { //  9/13/10
		if(isProtectedcode(code)) return;
		var ajax_connection = createRequest();
		var params = "/exmap/fetchlocation.php?pc="+code+"&dummy="+new Date().getTime();
		if(rb) params += "&e="+rb;
		ajax_connection.open('get', params);
        	ajax_connection.onreadystatechange = function() {
			if (ajax_connection.readyState == 4) {
    			if (ajax_connection.status == 200) {
      				var xmlDoc = ajax_connection.responseXML;
      				var lls=xmlDoc.documentElement.getElementsByTagName("location");
      				if(lls.length==0) {
					savedata(code);
				/*
					if(rb==1){
						newrescode(code,mapType);
					}
					else {
						newbuscode(code,mapType);
					}
				*/
				}
				else {
					if(lls[0].getAttribute('status') == "unvalidated") {
						alert("Thank you. This location already has a Loc8 code: "+code
							+"\nIf you created it and want to make any changes to the"
							+"\naddress or exact location, look at the top of the page"
							+"\nand click on 'Validate a Loc8 Code' and enter "+code
							);
					} else {
						alert("Thank you. This location already has a Loc8 code: "+code
							+"\nIf you created it and want to make any changes to the"
							+"\naddress or other information, look at the top of the page"
							+"\nand click on 'Manage my Loc8 Codes'"
							);
					}
				//	hasDuplicateCode(code, mapType, lls);
				}
      				return;
			}
			}
		} 
        	ajax_connection.send(null);      				
	}
	//100108
        function hasDuplicateCode(code, mapType, locations) {
		var i;//do any of the dups have the same Business Name?
		if(usetype>2) {
			var bn=decURL(encURL(document.getElementById('field_busname').value)).toLowerCase();
			for(i=0;i<locations.length;i++) {
				var codeloc = locations[i];
				if(bn==decURL(codeloc.getAttribute('bus')).toLowerCase()) {
					alert("This Business has already been registered at this location");
					location=srvurl+code;
					return;
				}
			}
			if(usetype>3) {
				//changerating(code,2,1);
			   	newbuscode(code,mapType);
				return;
			}
		}
		if(usetype==2) {
			//changerating(code,1,1);
		   	newrescode(code,mapType);
			return;
		}
		// usetype 1 or 3
		isDuplicate = true;

		var em = document.getElementById('field_email').value.toLowerCase();//are any of the dups from the same email?
		for(i=0;i<locations.length;i++) {
			var codeloc = locations[i];
			if(em==codeloc.getAttribute('em') ) {
				alert("You have already created a Loc8 code for this Single Occupancy location");
				location=srvurl+code;
				return;
			}
		}
		var addressline1 = document.getElementById('field_address1').value.split("\n")[0].toLowerCase();
		while (addressline1.substr(addressline1.length-1)==" " 
			|| addressline1.substr(addressline1.length-1)=="\r"
			|| addressline1.substr(addressline1.length-1)==",") {
			addressline1=addressline1.substr(0,addressline1.length-1);
		}
		for(i=0;i<locations.length;i++) {
			var codeloc = locations[i];
			if(decURL(codeloc.getAttribute('addr')).toLowerCase().indexOf(addressline1)>=0) {
				var ok = confirm("You indicated this address is single occupancy.\nHowever we already have a code for\n"
					+decURL(codeloc.getAttribute('prop'))+"\n"+decURL(codeloc.getAttribute('addr'))+"\n\nIs this the same address as yours?");
				if(ok) {
		     			if(usetype<3)
			   			newrescode(code,mapType);
		     			else
			   			newbuscode(code,mapType);
					return;
				} else {
            				alert("Please choose another spot on your property");
					return;
				}
			}
		}		
		alert("A Loc8 code has already been assigned for this exact location.\nIf this is a mulitple occupancy, select that option\nor choose another spot on your property");
        }
	function isProtectedcode(code) { //  9/12/10
		if(code.substr(0,3)=="W8L" && code.substr(7,3)=="4YK") {
			var e = 1*code.substr(4,1); var n = 1*code.substr(5,1);
			if(e>=7 && (n>=2 && n<=5)) {
				alert("Sorry, this is a protected Loc8 code.");
				return true;
			}
		}
		return false;
	}
var emailaddr;
	//100108   
	function newrescode(code,mapType) {
		if(isProtectedcode(code)) return;
		var numother = 0; //(usetype==1 || usetype == 3) ? 0 : 1; //100108
		emailaddr = trim(document.getElementById('field_email').value.toLowerCase());
	ag1=1;ag2=1;ag3=1;
	if(emailaddr.length>0) {
        // if we have an email address, reads the status of the agreements in the validate dialog
        if (jQuery("div.options input.third_party").is(":checked"))
            ag1 = 1;
        else
            ag1 = 0;

        if (jQuery("div.options input.searchable").is(":checked"))
            ag2 = 1;
        else
            ag2 = 0;

        if (jQuery("div.options input.communicate").is(":checked"))
            ag3 = 1;
        else
            ag3 = 0;
	}
        if (document.getElementById('field_property').value=="Not your property number")
            document.getElementById('field_property').value = "";

        var pn = trim(document.getElementById('field_property').value);

	var pnlc=pn.toLowerCase();
        if (pn=="Not your property number" || pnlc=="n/a" || pnlc=="na"  || pnlc=="none" || pnlc=="nil")
            pn = "";

        //alert("2. prop name = >"+pn+"<");

          	var ajax_connection = createRequest();
	 	var params="b1=" + encodeURIComponent(country)
          + "&b2=" + county
          + "&b4=" + encURL(pn)
          + "&b5=" + encURL(document.getElementById('field_address').value)
          + "&b7=" + encodeURIComponent(oldpostcode)
          + "&b8=" + encodeURIComponent(town)
          + "&bj=" + emailaddr
          + "&bh=" + numother
          + "&ag1=" + ag1
          + "&ag2=" + ag2
          + "&ag3=" + ag3
          + "&fm=" + mapType
          + "&bl=" + mapCentre.latitude
          + "&bm=" + mapCentre.longitude
          + "&sc=" + Math.floor(Math.random()*10000000000000000)
          + "&pc=" + code;

          ajax_connection.open("GET", "/exmap/insres.php?"+params+killcache());
          
          ajax_connection.onreadystatechange = function() {
          	 if (ajax_connection.readyState == 4) {
    			if (ajax_connection.status == 200) {
      				var response = ajax_connection.responseText;
      				if(response.substr(0,2)=="ok" || response.substr(response.length-2)=="ok") {
      					var dmsg='<div id="dialogh1">Congratulations on creating Your Loc8 Code</div>';
						if(emailaddr.length>0) {
      					           dmsg+="<p>You will soon receive an e-mail to validate your Loc8 Code.</p>"
						   +"<p>The email will also provide a link for you to email your Loc8 Code to others<br />"
						   +"and create a desktop icon of your Loc8 Code.<br /><br /></p>";
						}
						dmsg+="<p><br />Please make a note of your new Loc8 Code:&nbsp;&nbsp;<b>" + code +"</b><br /></p>";
					if(mapType == 3) {
						dmsg+="<p><br />Since this Loc8 Code was obtained using our alternative map, you should use the link<br/>";
						dmsg+="in the email to make adjustments before obtaining a permanent copy of your Loc8 Code</p>";
					}
      					showPONCdialog(dmsg);
      					return;
		        	}
		        	else {
      				if(response.substr(0,2)=="no" || response.substr(response.length-2)=="no") {
		        			alert("You have entered an invalid security code.\nPlease try again.");
		        			return;
		        		}
		        		else {
			        		alert("Error:<"+response.substr(0, 500)+">");//limit in case of whole page
			       			location.reload();  // start over
		        		}
		        	}
    			} else {
      				alert(ajax_connection.status + ": Error occured\nPlease report to support@loc8code.com");
		       		//document.formdata.reset();
		       		location.reload();
    			}
  		}          
          }
          ajax_connection.send(null);      				
    }

    //100108
    function newbuscode(code,mapType) {
		if(isProtectedcode(code)) return;
        var numother = 0; //(usetype==1 || usetype == 3) ? 0 : 1; //100108
	emailaddr= trim(document.getElementById('field_email').value.toLowerCase());
        var ajax_connection = createRequest();

        // reads the status of the agreements in the validate dialog
        if (jQuery("div.options input.third_party").is(":checked"))
            ag1 = 1;
        else
            ag1 = 0;

        if (jQuery("div.options input.searchable").is(":checked"))
            ag2 = 1;
        else
            ag2 = 0;

        if (jQuery("div.options input.communicate").is(":checked"))
            ag3 = 1;
        else
            ag3 = 0;

	var params="b1=" + encodeURIComponent(country)
          + "&b2=" + county
          + "&b3=" + ''
          + "&cn=" + ''
          + "&b4=" + encURL(document.getElementById('field_property').value)
          + "&b5=" + encURL(document.getElementById('field_address').value)
          + "&b7=" + encodeURIComponent(oldpostcode)
          + "&b8=" + encodeURIComponent(town)
          + "&bj=" + emailaddr
          + "&bh=" + numother
          + "&ag1=" + ag1
          + "&fm=" + mapType
          + "&bl=" + mapCentre.latitude
          + "&bm=" + mapCentre.longitude
          + "&sc=" + Math.floor(Math.random()*10000000000000000)
          + "&pc=" + code;
          //+ "&b3=" + encURL(document.getElementById('field_busname').value)
          //+ "&cn=" + encURL(document.getElementById('field_contactname').value)

          ajax_connection.open("GET", "/exmap/insbus.php?"+params+killcache());
          ajax_connection.onreadystatechange = function() {
          	  if (ajax_connection.readyState == 4) {
    			if (ajax_connection.status == 200) {
      				var response = ajax_connection.responseText;
      				if(response.substr(0,2)=="ok" || response.substr(response.length-2)=="ok") {
      					var dmsg='<div id="dialogh1">Congratulations on creating Your Loc8 Code</div>';
						if(emailaddr.length>0) {
      					           dmsg+="<p>You will soon receive an e-mail confirming your Loc8 Code.</p>"
						   +"<p>The email will also provide a link for you to add your Business details,<br />email your Loc8 Code to others<br />"
						   +"	and create a desktop icon of your Loc8 Code.<br /><br /></p>";
						}
						dmsg+="<p><br />Please make a note of your new Loc8 Code:&nbsp;&nbsp;<b>" + code +"</b><br /></p>";
					if(mapType == 3) {
						dmsg+="<p>Since this Loc8 Code was obtained using our alternative map, you should use the link<br/>";
						dmsg+="in the email to make adjustments before obtaining a permanent copy of your Loc8 Code</p>";
					}
      					showPONCdialog(dmsg);
      					return;
		        	}
		        	else {
      				if(response.substr(0,2)=="no" || response.substr(response.length-2)=="no") {
		        			alert("You have entered an invalid security code.\nPlease try again.");
		        			return;
		        		}
		        		else {
			        		alert("Error:<"+response.substr(0, 500)+">");//limit in case of whole page
			       			location.reload();  // start over
		        		}
		        	}
    			} else {
      				alert(ajax_connection.status + ": Error occured\nPlease report to support@loc8code.com");
		       		//document.formdata.reset();
		       		location.reload();
    			}
  			}          
          }
          ajax_connection.send(null);      				
    }

	// Confirming a new code , check for duplicates
	function confirmcodeexists(code, rb, f, sc) {
		if(isProtectedcode(code)) return;
		var params="pc="+code+"&e="+rb+"&c=2";
//alert(params);
		var ajax_connection = createRequest();
		ajax_connection.open('get', "/exmap/fetchlocation.php?"+params+killcache());//min confirm level
        	ajax_connection.onreadystatechange = function() {
			if (ajax_connection.readyState == 4) {
    			if (ajax_connection.status == 200) {
      				var xmlDoc = ajax_connection.responseXML;
      				var lls=xmlDoc.documentElement.getElementsByTagName("location");
      				if(lls.length==0) {
					confirmcode(rb, f);
				}
				else {
					confirmhasDuplicateCode(code,rb,lls,f,sc);
				}
      				return;
			}
			}
		} 
        	ajax_connection.send(null);      				
	}
        function confirmhasDuplicateCode(code, rb, locations,f,sc) {
		if(isProtectedcode(code)) return;
		var i;
		// usetype 1 or 3
		var em = document.getElementById('field_email').value.toLowerCase();//are any of the dups from the same email?
		var addressline1 = document.getElementById('field_address1').value.split("\n")[0].toLowerCase();
		while (addressline1.substr(addressline1.length-1)==" " 
			|| addressline1.substr(addressline1.length-1)=="\r"
			|| addressline1.substr(addressline1.length-1)==",") {
			addressline1=addressline1.substr(0,addressline1.length-1);
		}
  		addressline1= addressline1.replace(/,/g, "");

		for(i=0;i<locations.length;i++) {
			var codeloc = locations[i];
			var addr1=decURL(codeloc.getAttribute('addr')).toLowerCase();
  			addr1= addr1.replace(/,/g, "");
			if(em==codeloc.getAttribute('em') && addr1.indexOf(addressline1)>=0) {
				alert("You have already created this identical Loc8 Code\nfor this same Single Occupancy location\nThis duplicate entry will be deleted");
				//should delete this duplicate entry
				rmvrecord(1,f,sc,code);
				return;
			}
		}
		for(i=0;i<locations.length;i++) {
			var codeloc = locations[i];
			var addr1=decURL(codeloc.getAttribute('addr')).toLowerCase();
  			addr1= addr1.replace(/,/g, "");
			if(addr1.indexOf(addressline1)>=0) {
				var ok = confirm("You indicated this address is single occupancy.\nHowever we already have this Loc8 Code for\n"
					+decURL(codeloc.getAttribute('prop'))+"\n"+decURL(codeloc.getAttribute('addr'))+"\n\nCan we assume that this is a Multiple Occupancy location\nat the same address as yours?");
				if(ok) {
					occupancy = 1;
					confirmcode(rb, f);
					return;
				} else {
            				alert("Please click on 'Adjust Marker position'\nand click on another spot on your property");
					return;
				}
			}
		}		
		alert("A Loc8 Code has already been assigned for this exact location.\nIf this is a multiple occupancy, select that option\nor click on 'Adjust Marker position'\nto choose another spot on your property");
        }


	function confirmcode(e2,f2) {
//alert("confirmcode "+e2);
		if(e2 == 2) { moveConfRecord(1, f2); return; }
		confirmcodeOK(e2,f2);
	}

	function moveConfRecord(e2 ,f2){

          	var ajax_connection = createRequest();
	  	var params="f=" + f2
          		+ "&e=" + e2
		;
          ajax_connection.open('get', "/exmap/moverecord.php?"+params+killcache());
          ajax_connection.onreadystatechange = function() {
          	  if (ajax_connection.readyState == 4) {
    			if (ajax_connection.status == 200) {
      				var response = ajax_connection.responseText;
      				if(response.substr(0,2)=="ok") {
					f = response.substr(2);
					e = 2;
					confirmcodeOK(e,f);
		        	} 
				else {
		        		if(response=="nok") {
		        			alert("We cannot confirm your Loc8 Code.");
		        			return;
		        		}
		        		else {
			        		alert("Error:<"+response.substr(0, 500)+">");//limit in case of whole page
			       			location.reload();  // start over
		        		}
		        	}
    			} else {
      				alert(ajax_connection.status + ": Error occured\nPlease report to support@loc8code.com");
		       		//document.formdata.reset();
		       		location.reload();
    			}
  		    }          
          }
          ajax_connection.send(null);      				
    }
	function confirmcodeOK(e,f) {
		numother = occupancy;
          	var ajax_connection = createRequest();
	  var params="f=" + f
          + "&e=" + e
          + "&b4=" + encURL(document.getElementById('field_propname').value)
          + "&b5=" + encURL(document.getElementById('field_address1').value)
          + "&sc=" + document.getElementById('b').value
          + "&bh=" + numother
          + "&pc=" + position.poncode
          + "&bl=" + position.latitude
          + "&bm=" + position.longitude
	  ;
	  if(entrance.mygraphic) {
		params = params
          	+ "&ec=" + entrance.poncode
          	+ "&ed=" + entrance.latitude
          	+ "&ee=" + entrance.longitude
	  	;
	  }
	  else {
		params = params
          	+ "&ec=" + ''
          	+ "&ed=" + 0
          	+ "&ee=" + 0
	  	;
	  }
//alert("confirmcodeOK:"+params);
          ajax_connection.open('get', "/exmap/confirm.php?"+params+killcache());
          ajax_connection.onreadystatechange = function() {
          	  if (ajax_connection.readyState == 4) {
    			if (ajax_connection.status == 200) {
      				var response = ajax_connection.responseText;
      				if(response != "nok") {
      					var dmsg='<div id="dialogh1">Loc8 Code CONFIRMED</div><p><br />Thank you for confirming your Loc8 Code.</p>';
					
					dmsg+="<p>You may now enter additional information and<br />email your Loc8 Code to friends and associates.</p>";
      					sm('dialogbox',500,300);
					$mdbox('dialogtxt').innerHTML = dmsg;
      					return;
		        	}
		        	else {
		        		if(response=="nok") {
		        			alert("We cannot confirm your Loc8 Code.");
		        			return;
		        		}
		        		else {
			        		alert("Error:<"+response.substr(0, 500)+">");//limit in case of whole page
			       			location.reload();  // start over
		        		}
		        	}
    			} else {
      				alert(ajax_connection.status + ": Error occured\nPlease report to support@loc8code.com");
		       		location.reload();
    			}
  		    }          
          }
          ajax_connection.send(null);      				
    }

	function moverecord(e2 ,f2, b){
          	var ajax_connection = createRequest();
	  	var params="f=" + f2
          		+ "&e=" + e2
		;
          	ajax_connection.open('get', "/exmap/moverecord.php?"+params+killcache());
          	ajax_connection.onreadystatechange = function() {
          	  if (ajax_connection.readyState == 4) {
    			if (ajax_connection.status == 200) {
      				var response = ajax_connection.responseText;
      				if(response.substr(0,2)=="ok") {
					f = response.substr(2);
					e = (e2==1)? 2 : 1;
					openpropertypage(e,f,b);
		        	} 
				else {
		        		if(response=="nok") {
		        			alert("We could not change the property type.");
		        			return;
		        		}
		        		else {
			        		alert("Error:<"+response.substr(0, 500)+">");//limit in case of whole page
			       			location.reload();  // start over
		        		}
		        	}
    			} else {
      				alert(ajax_connection.status + ": Error occured\nPlease report to support@loc8code.com");
		       		location.reload();
    			}
  		    }          
          	}
          	ajax_connection.send(null);      				
	}
	function openpropertypage(e,f,b){
		var loc=srvurl+"maps/my_loc8_accounta/?f="+f+"&b="+b+"&e="+e;
		window.location=loc;
	}

	function rmvrecord(e2,f2,b2,code) {
          	var ajax_connectiond = createRequest();
	  	var params="f=" + f2
          		+ "&b=" + b2
          		+ "&e=" + e2
		;
          ajax_connectiond.open('get', "/exmap/rmvrecord.php?"+params+killcache());
          ajax_connectiond.onreadystatechange = function() {
          	  if (ajax_connectiond.readyState == 4) {
    			if (ajax_connectiond.status == 200) {
      				var response = ajax_connectiond.responseText;
      				if(response.substr(0,2)=="ok") {
					alert("This location has been deleted.\nHere is the original.");
					location=srvurl+code;
		        	} 
				else {
		        		if(response=="nok") {
		        			alert("error: could not delete your Loc8 Code.");
		        			return;
		        		}
		        		else {
			        		alert("Error:<"+response.substr(0, 500)+">");//limit in case of whole page
			       			location.reload();  // start over
		        		}
		        	}
    			} else {
      				alert(ajax_connectiond.status + ": Error occured\nPlease report to support@loc8code.com");
		       		//document.formdata.reset();
		       		location.reload();
    			}
  		    }          
          }
          ajax_connectiond.send(null);      				
	}

	function transfercode(e2,f2,b2,emailfrom,code,email,emailc) {
		email=trim(email);
		if(email.length==0 || email!=trim(emailc)) {
			alert("Please make sure that the email addresses match");
			return;
		}
          	var ajax_connection = createRequest();
	  	var params="f=" + f2
          		+ "&bj=" + email
          		+ "&e=" + e2
			+ "&b=" + b2
          		+ "&emfrom=" + emailfrom
          		+ "&pc=" + code
		;
          	ajax_connection.open('get', "/exmap/transferrecord.php?"+params+killcache());
          	ajax_connection.onreadystatechange = function() {
          	  if (ajax_connection.readyState == 4) {
    			if (ajax_connection.status == 200) {
      				var response = ajax_connection.responseText;
      				if(response.substr(0,2)=="ok") {
					alert(code+" has been transferred to "+email);
					location=srvurl+"loc8mgmt";
		        	} 
				else {
		        		if(response=="nok") {
		        			alert("error: could not transfer this Loc8 Code.");
		        			return;
		        		}
		        		else {
			        		alert("Error:<"+response.substr(0, 500)+">");//limit in case of whole page
			       			location.reload();  // start over
		        		}
		        	}
    			} else {
      				alert(ajax_connection.status + ": Error occured\nPlease report to support@loc8code.com");
		       		//document.formdata.reset();
		       		location.reload();
    			}
  		    }          
          }
          ajax_connection.send(null);      				
	}




	function relinquishcode(e2,f2,b2,code) {
		var ok=confirm("Are you sure you want to relinquish control of "+code+" ?");
		if(!ok) return;
          	var ajax_connection = createRequest();
	  	var params="f=" + f2
          		+ "&b=" + b2
          		+ "&e=" + e2
		;
          	ajax_connection.open('get', "/exmap/relinquishrecord.php?"+params+killcache());
          	ajax_connection.onreadystatechange = function() {
          	  if (ajax_connection.readyState == 4) {
    			if (ajax_connection.status == 200) {
      				var response = ajax_connection.responseText;
      				if(response.substr(0,2)=="ok") {
					alert(code+" has been relinquished.");
					location=srvurl+"loc8mgmt";
		        	} 
				else {
		        		if(response=="nok") {
		        			alert("error: could not delete this Loc8 Code.");
		        			return;
		        		}
		        		else {
			        		alert("Error:<"+response.substr(0, 500)+">");//limit in case of whole page
			       			location.reload();  // start over
		        		}
		        	}
    			} else {
      				alert(ajax_connection.status + ": Error occured\nPlease report to support@loc8code.com");
		       		//document.formdata.reset();
		       		location.reload();
    			}
  		    }          
          }
          ajax_connection.send(null);      				
	}

	function deletecode(e2,f2,b2,code) {
		var ok=confirm("Are you sure you want to delete "+code+" ?");
		if(!ok) return;
          	var ajax_connectiond = createRequest();
	  	var params="f=" + f2
          		+ "&b=" + b2
          		+ "&e=" + e2
		;
          ajax_connectiond.open('get', "/exmap/requestdelete.php?"+params+killcache());
          ajax_connectiond.onreadystatechange = function() {
          	  if (ajax_connectiond.readyState == 4) {
    			if (ajax_connectiond.status == 200) {
      				var response = ajax_connectiond.responseText;
      				if(response.substr(0,2)=="ok") {
					alert(code+" has been deleted.");
					location=srvurl+"loc8mgmt";
		        	} 
				else {
		        		if(response=="nok") {
		        			alert("error: could not delete this Loc8 Code.");
		        			return;
		        		}
		        		else {
			        		alert("Error:<"+response.substr(0, 500)+">");//limit in case of whole page
			       			location.reload();  // start over
		        		}
		        	}
    			} else {
      				alert(ajax_connectiond.status + ": Error occured\nPlease report to support@loc8code.com");
		       		//document.formdata.reset();
		       		location.reload();
    			}
  		    }          
          }
          ajax_connectiond.send(null);      				
    }


	function managepage(){
		var loc=srvurl+"maps/getmyloc8home/?f="+document.getElementById("f").value+"&b="+document.getElementById("b").value+"&e="+document.getElementById("e").value;
		window.location=loc;
	}

	function updresbuscode(e,f){
		if(e==1) updrescode(f);
		if(e==2) updbuscode(f);
	}
	function updrescode(a1d) {
          	var ajax_connection = createRequest();
		var params="f=" + a1d
          + "&b4=" + encURL(document.getElementById('field_propname').value)
          + "&b5=" + encURL(document.getElementById('field_address1').value)
          + "&pc=" + position.poncode
          + "&sc=" + document.getElementById('sc').value
	 ;
	  if(parking.added) {
		params = params
                + "&bi=" + 0
          	+ "&pp=" + parking.poncode
          	+ "&bn=" + parking.latitude
          	+ "&bo=" + parking.longitude
	  	;
	  }
	  else {
		params = params
                + "&bi=" + 1
          	+ "&pp=" + ''
          	+ "&bn=" + 0
          	+ "&bo=" + 0
	  	;
	  }
	  if(entrance.added) {
		params = params
          	+ "&ec=" + entrance.poncode
          	+ "&ed=" + entrance.latitude
          	+ "&ee=" + entrance.longitude
	  	;
	  }
	  else {
		params = params
          	+ "&ec=" + ''
          	+ "&ed=" + 0
          	+ "&ee=" + 0
	  	;
	  }
	  if(transport.added) {
		params = params
                + "&ta=" + ttype
          	+ "&tc=" + transport.poncode
          	+ "&td=" + transport.latitude
          	+ "&te=" + transport.longitude
	  	;
	  }
	  else {
		params = params
                + "&ta=" + 0
          	+ "&tc=" + ''
          	+ "&td=" + 0
          	+ "&te=" + 0
	  	;
	  }
//alert(params);
          ajax_connection.open('get', "/exmap/updres.php?"+params+killcache());
          ajax_connection.onreadystatechange = function() {
          	  if (ajax_connection.readyState == 4) {
    			if (ajax_connection.status == 200) {
      				var response = ajax_connection.responseText;
      				if(response != "nok") {
      					var dmsg='<div id="dialogh1">CHANGES CONFIRMED</div><p><br / >You will soon receive an e-mail showing the changes.</p>';
					
      					sm('dialogbox',500,250);
					$mdbox('dialogtxt').innerHTML = dmsg;

				        if(document.forms["gotolist"]) setTimeout('document.forms["gotolist"].submit()', 2000);
					//changerating(code,1,1);
      					return;
		        	}
		        	else {
		        		if(response=="nok") {
		        			alert("You have entered an invalid security code.\nPlease try again or\nclick 'Get a new code' to get another code.");
		        			return;
		        		}
		        		else {
			        		alert("Error:<"+response.substr(0, 500)+">");//limit in case of whole page
			       			location.reload();  // start over
		        		}
		        	}
    			} else {
      				alert(ajax_connection.status + ": Error occured\nPlease report to support@loc8code.com");
		       		//document.formdata.reset();
		       		location.reload();
    			}
  			 }          
          }
          ajax_connection.send(null);      				
    }

	function formatphone(number, country) {
		number = trim(number);
                if(number.length<8)return number;
		if(number.substr(0,4) == "1800" || number.substr(0,5) == "1 800") return number;
		//if(number.substr(0,1) == "+") return number;
		var prefix="+353 (0)"; //ROI default
		if(country == "Ireland") {
			if(number.substr(0,8)==prefix) {number = trim(number.substr(8));}
			else if(number.substr(0,3)=="353") {number = trim(number.substr(3));}
			else if(number.substr(0,4)=="+353") {number = trim(number.substr(4));}
			else if(number.substr(0,5)=="00353") {number = trim(number.substr(5));}
			if(number.substr(0,3)=="(0)") {number = trim(number.substr(3));}
			else if(number.substr(0,3)=="-1-") {number = "1 "+trim(number.substr(3));}
			else if(number.substr(0,2)=="-1") {number = "1 "+trim(number.substr(2));}
			else if(number.substr(0,4)=="(01)") {number = "1 "+trim(number.substr(4));}
			else if(number.substr(0,1)=="0") {number = trim(number.substr(1));}
			return new String("" + prefix + number);	
		}
		else if(country == "Northern Ireland") {
			prefix = "+44";
			if(number.substr(0,3)==prefix) return number;
			if(number.substr(0,prefix.length-1) == prefix.substr(1)) return new String("" + "+" + number);
			return new String("" + prefix + " " + number);
		}
		else return number;
	}

	function updbuscode(a1d) {
		//fix up phone numbers
		var tel=new String(formatphone(document.getElementById('field_tel').value,document.getElementById('country').value));
		var fax=new String(formatphone(document.getElementById('field_fax').value,document.getElementById('country').value));
		
          var ajax_connection = createRequest();
	var params="f=" + a1d
          + "&b3=" + encURL(document.getElementById('field_busname').value)
          + "&b4=" + encURL(document.getElementById('field_propname').value)
          + "&b5=" + encURL(document.getElementById('field_address1').value)
          + "&b9=" + encURL(document.getElementById('field_floor').value)
          + "&bd=" + encURL(new String(tel))
          + "&be=" + encURL(new String(fax)) 
          + "&bf=" + encURL(document.getElementById('field_typebus').value)
          + "&bg=" + encURL(document.getElementById('field_typebusoth').value)
          + "&bj=" + document.getElementById('field_email').value.toLowerCase()
          + "&bk=" + document.getElementById('field_emailalt').value.toLowerCase()
          + "&bw=" + document.getElementById('field_website').value.toLowerCase()
          + "&bp=" + encURL(document.getElementById('field_proptype').value)
          + "&bq=" + encURL(document.getElementById('field_services').value)
          + "&cn=" + encURL(document.getElementById('field_contact').value)
          + "&pc=" + position.poncode
          + "&sc=" + document.getElementById('sc').value
	 ;
	  if(parking.added) {
		params = params
                + "&bi=" + 0
          	+ "&pp=" + parking.poncode
          	+ "&bn=" + parking.latitude
          	+ "&bo=" + parking.longitude
	  	;
	  }
	  else {
		params = params
                + "&bi=" + 1
          	+ "&pp=" + ''
          	+ "&bn=" + 0
          	+ "&bo=" + 0
	  	;
	  }
	  if(entrance.added) {
		params = params
          	+ "&ec=" + entrance.poncode
          	+ "&ed=" + entrance.latitude
          	+ "&ee=" + entrance.longitude
	  	;
	  }
	  else {
		params = params
          	+ "&ec=" + ''
          	+ "&ed=" + 0
          	+ "&ee=" + 0
	  	;
	  }
	  if(transport.added) {
		params = params
                + "&ta=" + ttype
          	+ "&tc=" + transport.poncode
          	+ "&td=" + transport.latitude
          	+ "&te=" + transport.longitude
	  	;
	  }
	  else {
		params = params
                + "&ta=" + 0
          	+ "&tc=" + ''
          	+ "&td=" + 0
          	+ "&te=" + 0
	  	;
	  }
//alert(params);
          ajax_connection.open('get', "/exmap/updbus.php?"+params+killcache());
          
          ajax_connection.onreadystatechange = function() {
          	  if (ajax_connection.readyState == 4) {
    			if (ajax_connection.status == 200) {
      				var response = ajax_connection.responseText;
      				if(response=="ok") {
      					var dmsg='<div id="dialogh1">CHANGES CONFIRMED</div><p><br />You will soon receive an e-mail showing the changes.</p>';
					
      					sm('dialogbox',500,250);
					$mdbox('dialogtxt').innerHTML = dmsg;
					//changerating(code,2,1);

				        if(document.forms["gotolist"]) setTimeout('document.forms["gotolist"].submit()', 2000);

      					return;
		        	}
		        	else {
		        		if(response=="nok") {
		        			alert("You have entered an invalid security code.\nPlease try again or\nclick 'Get a new code' to get another code.");
		        			return;
		        		}
		        		else {
			        		alert("Error from Database: "+response);
			       			location.reload();  // start over
		        		}
		        	}
    			} else {
      				alert(ajax_connection.status + ": Error occured\nPlease report to irishpostcodes.ie");
		       		location.reload();
    			}
  			}          
          }
          ajax_connection.send(null);      				
    }


	function claimcode(f, code, emailaddr,e) {
          	var ajax_connection = createRequest();
	 	var params="f=" + f
          		+ "&e=" + (e ? e : 1)
          		+ "&bj=" + emailaddr.toLowerCase()
          		+ "&pc=" + code.toUpperCase();
          	ajax_connection.open("GET", "/exmap/claimcode.php?"+params+killcache());
          	ajax_connection.onreadystatechange = function() {
          	 if (ajax_connection.readyState == 4) {
    			if (ajax_connection.status == 200) {
      				var response = ajax_connection.responseText;
      				if(response.substr(0,2)=="ok") {
      					var dmsg='<div id="dialogh1">Your Loc8 Code: '+code+'</div>';
						if(emailaddr.length>0) {
      					           dmsg+="<p>You will soon receive an e-mail to validate your Loc8 Code.</p>"
						   +"<p>The email will provide a link for you to validate the Code.<br />"
						   +"<br /></p>";
						}
      					sm('dialogbox',500,350);
					$mdbox('dialogtxt').innerHTML = dmsg;
      					return;
		        	}
		        	else {
      				if(response.substr(0,2)=="no" || response.substr(response.length-2)=="no") {
		        			alert("You have entered an invalid security code.\nPlease try again.");
		        			return;
		        		}
		        		else {
			        		alert("Error:<"+response.substr(0, 500)+">");//limit in case of whole page
			       			location.reload();  // start over
		        		}
		        	}
    			} else {
      				alert(ajax_connection.status + ": Error occured\nPlease report to support@loc8code.com");
		       		//document.formdata.reset();
		       		location.reload();
    			}
  		}          
          }
          ajax_connection.send(null);      				
    }
	// called form Map page
	function requestValidation(f, e, code) {
          	var ajax_connection = createRequest();
	 	var params="f=" + f
          		+ "&e=" + e
          		+ "&pc=" + code.toUpperCase();
          ajax_connection.open("GET", "/exmap/requestvalidation.php?"+params+killcache());
          ajax_connection.onreadystatechange = function() {
          	 if (ajax_connection.readyState == 4) {
    			if (ajax_connection.status == 200) {
      				var response = ajax_connection.responseText;
      				if(response.substr(0,2)=="ok") {
      					var dmsg='<div id="dialogh1">For Loc8 Code: '+code+'</div>';
      					           dmsg+="<p>Validation Request Acknowledged.</p>"
						   +"<p>An email will be sent to the owner.<br />"
						   +"<br /></p>";
      					sm('dialogbox',500,350);
					$mdbox('dialogtxt').innerHTML = dmsg;
      					return;
		        	}
		        	else {
      				if(response.substr(0,2)=="no" || response.substr(response.length-2)=="no") {
		        			alert("This is an invalid request.");
		        			return;
		        		}
		        		else {
			        		alert("Error:<"+response.substr(0, 500)+">");//limit in case of whole page
			       			location.reload();  // start over
		        		}
		        	}
    			} else {
      				alert(ajax_connection.status + ": Error occured\nPlease report to support@loc8code.com");
		       		//document.formdata.reset();
		       		location.reload();
    			}
  		}          
          }
          ajax_connection.send(null);      				
	}




	//100108
	function changerating(code,rb,changeval) {
		var ajax_connection = createRequest();
		ajax_connection.open('get', "/exmap/changerating.php?pc="+code+"&e="+rb+"&val="+changeval+killcache());
        	ajax_connection.onreadystatechange = function() {
			if (ajax_connection.readyState == 4) {
    				if (ajax_connection.status == 200) {
      					return;
				}
			}
		} 
        	ajax_connection.send(null);      				
	}
	function verifylic(purpose) {
		var ajax_connection = createRequest();
		ajax_connection.open('get', "/exmap/verifylicence.php?f="+document.getElementById("f").value
				+"&p="+document.getElementById("password").value
				+"&u="+purpose+killcache()
				);
        	ajax_connection.onreadystatechange = function() {
			if (ajax_connection.readyState == 4) {
    				if (ajax_connection.status == 200) {
      				var response = ajax_connection.responseText;
      					if(response=="ok") {
						passlic();
					} else {
						alert("Login failed.  Your licence may have expired");
					}
				}
			}
		} 
        	ajax_connection.send(null);      				
	}
	function verifyacc() {
		var ajax_connection = createRequest();
		ajax_connection.open('get', "/exmap/verifyacc.php?f="+document.getElementById("f").value
				+"&p="+document.getElementById("pwd").value
				+"&e="+document.getElementById("t").value+killcache()
				);
        	ajax_connection.onreadystatechange = function() {
			if (ajax_connection.readyState == 4) {
    				if (ajax_connection.status == 200) {
      				var response = ajax_connection.responseText;
      					if(response=="ok") {
						pass();
					} else {
						fail();
					}
				}
			}
		} 
        	ajax_connection.send(null);      				
	}
	function addacc() {
		var ajax_connection = createRequest();
		ajax_connection.open('get', "/exmap/addacc.php?f="+document.getElementById("f").value
				+"&p="+trim(document.getElementById("pwd").value)
				+"&e="+document.getElementById("t").value
				+"&n="+document.getElementById("scrname").value+killcache()
				);
        	ajax_connection.onreadystatechange = function() {
			if (ajax_connection.readyState == 4) {
    				if (ajax_connection.status == 200) {
      				var response = ajax_connection.responseText;
      					if(response=="ok") {
						passtoconfirm();
					} else {
						fail();
					}
				}
			}
		} 
        	ajax_connection.send(null);      				
	}
	function pwdreminder() {
		var ajax_connection = createRequest();
		ajax_connection.open('get', "/exmap/pwdreminder.php?f="+document.getElementById("f").value
				+"&e="+document.getElementById("t").value+killcache()
				);
        	ajax_connection.onreadystatechange = function() {
			if (ajax_connection.readyState == 4) {
    				if (ajax_connection.status == 200) {
      				var response = ajax_connection.responseText;
      					if(response=="ok") {
						alert("An email has been sent to your registered email address\nwith your Loc8 Code password");
					} else {
						fail();
					}
				}
			}
		} 
        	ajax_connection.send(null);      				
	}
	function pwdforgot(em) {
                em = trim(em); //common.js
                if(em.length==0){alert("Enter your email address first and then ask again");return;}
		var ajax_connection = createRequest();
		ajax_connection.open('get', "/exmap/pwdforgot.php?f="+em+killcache());
        	ajax_connection.onreadystatechange = function() {
			if (ajax_connection.readyState == 4) {
    				if (ajax_connection.status == 200) {
      				var response = ajax_connection.responseText;
      					if(response=="ok") {
						alert("An email has been sent to "+em+"\nwith your Loc8 Code password");
						window.location=srvurl+"loc8mgmt/";
					} else {
						alert("Your email address is not registered with Loc8\nPlease try again");
					}
				}
			}
		} 
        	ajax_connection.send(null);      				
	}
	function pass(){
		var loc=srvurl+"maps/getmyloc8home/?f="+document.getElementById("f").value+"&b="+document.getElementById("b").value+"&e="+document.getElementById("t").value;
		window.location=loc;
	}
	function passtoconfirm(){
		var loc=srvurl+"maps/getconfirm/?f="+document.getElementById("f").value+"&b="+document.getElementById("b").value+"&e="+document.getElementById("t").value;
		window.location=loc;
	}


//******************************************************************//
	function showPONCdialog(msg) {
		sm('dialogbox',500,400);
		$mdbox('dialogtxt').innerHTML = msg;
	}
	function okSelected() {
		nextpage();
	}

