var IExp=false;
if(navigator.appName=="Microsoft Internet Explorer") IExp=true;

function testpostcode(){
	if(document.ThisForm.Postcode.value==""){
		alert("Please enter a valid postcode in the box");
	}
	else{
		//smURL="http://www.streetmap.co.uk/streetmap.dll?postcode2map?code="+escape(document.ThisForm.Postcode.value);
		smURL="http://maps.google.co.uk/maps?q="+escape(document.ThisForm.Postcode.value)+"&spn=0.008,0.02"
		if (typeof streetmapwindow == "object"){
			if (streetmapwindow.closed){
				streetmapwindow=window.open(smURL,'streetmap');
			}
		}
		else{ 
			streetmapwindow=window.open(smURL,'streetmap');
		}
		streetmapwindow.focus();
	}
}

function CountyChoice(){
	if(document.ThisForm.County.selectedIndex==89){
		document.ThisForm.ContactAddress.focus();
		document.ThisForm.County.focus();
		alert("Please enter the correct county name\nin the 'other information' box below.");
	}
}

function OLDCountyChoice(){
	//Handle County Not Listed
	if (document.all){
		//alert(document.ThisForm.County.value+"\n"+document.ThisForm.OtherCounty.value)
		if (document.ThisForm.County.value=="NotListed" || document.ThisForm.OtherCounty.value!="County not listed?")
			{document.all.newcounty.style.display=""}
		else{
			document.all.newcounty.style.display="none"
		}
	}
	//Handle London
	if(document.ThisForm.County[document.ThisForm.County.selectedIndex].value=="London" && !document.londonhelpshown){
		londonhelp();
		document.londonhelpshown = true;
	}
}

function HideExtraCounty(){
	if(document.all){
		if (document.all.OtherCounty.value=="County not listed?" && document.ThisForm.County.value!="NotListed")
			{document.all.newcounty.style.display="none"}
	}
}

function londonhelp(){
		if (typeof londonwindow == "object"){
			if (londonwindow.closed){londonwindow=window.open('londonhelp.htm','londonhelp', 'width=350,height=185,resizable=no,location=no,scrollbars=no,toolbar=no,status=no,screenX=25,screenY=300,left=100,top=300');}
		}
		else{ 
			londonwindow=window.open('londonhelp.htm','londonhelp', 'width=350,height=185,resizable=no,location=no,scrollbars=no,toolbar=no,status=no,screenX=25,screenY=300,left=25,top=300');
		}
		londonwindow.focus();
}

function ValidateForm(){
   //if(!zzsubmit) return false; //Your email

   var msg = "", errfocus="";
   with (document.ThisForm) {

	if (RestaurantName.value==""){
		msg += "Restaurant name is missing\n"
		if (errfocus=="") errfocus = "RestaurantName";
	}
	if (!isRestaurant.checked && !isTakeaway.checked){
		msg += "Is this a restaurant or take-away?\n"
		if (errfocus=="") errfocus = "RestaurantName";
	}
	if (Address.value==""){
		msg += "Address is missing\n"
		if (errfocus=="") errfocus = "Address";
	}
	if (Town.value==""){
		msg += "Town is missing\n"
		if (errfocus=="") errfocus = "Town";
	}
	if (County[County.selectedIndex].value==""){
		msg += "County not selected\n"
		if (errfocus=="") errfocus = "County";
	}
	else {
		if (County.value=="NotListed" && OtherCounty.value=="County not listed?"){
		msg += "Please specify the name of the unlisted county\n"
		if (errfocus=="") errfocus = "County";
		}
	}
	if (Postcode.value==""){
		msg += "Postcode is missing\n"
		if (errfocus=="") errfocus = "Postcode";
	}
	if (RestaurantPhone1.value==""){
		msg += "Main phone number is missing\n"
		if (errfocus=="") errfocus = "RestaurantPhone1";
	}
	if (check_email(RegistrantEmail.value,false)==false){
		msg += "\nYour email address is invalid\n(Email addresses are used ONLY for checking details)"
		if (errfocus=="") errfocus = "RegistrantEmail";
	}
	if (msg != ""){
		msg = "This registration has the following errors:\n\n" + msg;
		alert(msg);
		eval(errfocus+".focus()");
		return false;
	}
   }
}

function zzsubmit(){
	if(document.ThisForm.ContactPhone.value==""){
		alert("Sorry we cannot register this restaurant\nunless you give us your email address.\nWe ONLY use this information when we need\nto validate information.");
		document.ThisForm.ContactPhone.focus();
		return false;
	}
	else
		return true;
}


function toNearestPenny(value) {
	ret = Math.round(value * 100).toString();
	len = ret.length;
	if (len == 1) {ret = "00" + ret; len = 3}
	if (len == 2) {ret =  "0" + ret; len = 3}
	return ret.substring(0,len-2) + "." + ret.substring(len-2,len);
}


function check_email(address,allowblank) {

  if (address=='' && allowblank==true)  {return true;}
  if (address=='' && allowblank==false) {document.EmaiError="Email cannot be blank";return false;}

  if(emailCheck(address)==true) return true; else return false;
}

function emailCheck (emailStr) {
	document.EmaiError=""
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
		document.EmaiError="Email address seems incorrect (check @ and .'s)"
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]

	if (user.match(userPat)==null) {
	    // user is not valid
	    document.EmaiError="The email username doesn't seem to be valid."
	    return false
	}

	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
	    // this is an IP address
		  for (var i=1;i<=4;i++) {
		    if (IPArray[i]>255) {
		        document.EmaiError="Email destination IP address is invalid!"
			return false
		    }
	    }
	    return true
	}

	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		document.EmaiError="The email domain name doesn't seem to be valid."
	    return false
	}

	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
	    domArr[domArr.length-1].length>3) {
	   document.EmaiError="The email address must end in a three-letter domain, or two letter country."
	   return false
	}

	if (len<2) {
	   var errStr="This address is missing a hostname!"
	   document.EmaiError=errStr
	   return false
	}

	// Email valid!
	return true;
}
