function $(id) {return document.getElementById(id); }

function Check(){
	CheckPostal();
}
function Close(){
	$('greyout').style.visibility='hidden';
	$('envelope').style.display='none';
	$('thanks').style.visibility='hidden';
	$('body').style.overflow='auto';
}

function Send(){
	SendEmail();
}


function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}

function ValidateEmail(emailID){
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email ID")
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		return false
	}
	return true
 }
function ValidateName(name){
	
	if ((name.value==null)||(name.value=="")){
		alert("Please Enter your name")
		return false
	}

	var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
	for (var i = 0; i < name.value.length; i++) {
	  	if (iChars.indexOf(name.value.charAt(i)) != -1) {
		  	alert ("Your username has special characters. \nThese are not allowed.\n Please remove them and try again.");
		  	return false;
	  	}
	}
	return true
 }


function createXMLHttpRequest() {
   try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
   try { return new XMLHttpRequest(); } catch(e) {}
   alert("XMLHttpRequest not supported");
   return null;
 }


function isPostCode(pc){ // CANADIAN CODES ONLY
	var	entry=pc.value;
	entry = entry.replace(/ /g,'');
	strlen=entry.length; 
	if(strlen!=6) {
		alert('Postal code has six characters.');
		return false;
	}
	entry=entry.toUpperCase();    // in case of lowercase characters
	
	// Check for legal characters in string - note index starts at zero
	if('ABCEGHJKLMNPRSTVXY'.indexOf(entry.charAt(0))<0) {
		alert('Postal code has an incorrect starting character.');
		return false;}
	if('0123456789'.indexOf(entry.charAt(1))<0) {
		alert('Postal code should have a number in the second place.');
		return false;}
	if('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(entry.charAt(2))<0) {
		alert('Postal code should have a letter in the third place.');
		return false;}
	if('0123456789'.indexOf(entry.charAt(3))<0) {
		alert('Postal code should have a number in the fourth place.');
		return false;}
	if('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(entry.charAt(4))<0) {
		alert('Postal code should have a letter in the fifth place.');
		return false;}
	if('0123456789'.indexOf(entry.charAt(5))<0) {
		alert('Postal code should have a number in the last place.');
		return false;}
	return true;
}

function CheckPostal() {
	 var postalcode = $("postal");
	 var ok=isPostCode(postalcode);
	 if (ok)
	 {
		$('greyout').style.visibility='visible';
		$('envelope').style.display='inline';
		$('all').style.overflow='hidden';

//		var email = $("notify_email");
//		var xhReq = createXMLHttpRequest();
//		var call="registeruser.php?email=" + email.value + "&carNo=" + car;
//		xhReq.open("GET", call, false);
//		xhReq.onreadystatechange = onRegisterResponse;
//		xhReq.send(null);
//		var serverResponse = xhReq.responseText;
//		alert(serverResponse);
	 } 
}
function SendEmail() {
	var postalcode = $("postal");
	var emailID = $("email");
	var fname = $("fname");
	var lname = $("lname");
	var checked = $("proceed");
	var city = $("city");

	var ok1=ValidateEmail(emailID);
	var ok2=ValidateName(fname);
	var ok3=ValidateName(lname);
	var ok4=ValidateName(city);
	 if ((ok1) && (ok2) && (ok3) && (ok4))
	 {
		alert('Your email has been sent, thanks for your support!');
		Close();
		var xhReq = createXMLHttpRequest();
	if (!checked.checked)
	{
		var call="../pmailer.php?email=" + emailID.value + "&fname=" + fname.value + "&lname=" + lname.value + "&postal=" + postalcode.value + "&city=" + city.value;
	} else {
		var call="../pmailer.php?email=" + emailID.value + "&fname=" + fname.value + "&lname=" + lname.value + "&postal=" + postalcode.value + "&city=" + city.value + "&add=yes";
	}
		xhReq.open("GET", call, false);
		xhReq.send(null);
		var serverResponse = xhReq.responseText;
//		alert(serverResponse);
	 } 
}