// JavaScript Document

var alertValue, emailExists
var firstName, lastName, postCode, address, line2, town, county, email, email2, phone
function validate(){


	
	
firstName 	= document.getElementById("firstName").value;
lastName 	= document.getElementById("lastName").value;
postCode 	= document.getElementById("post_code").innerHTML;
address 	= document.getElementById("address").innerHTML;
line2 		= document.getElementById("line2").innerHTML;
town 		= document.getElementById("town").innerHTML;
county 		= document.getElementById("county").innerHTML;
email 		= document.getElementById("email1").value;
email2 		= document.getElementById("email2").value;
phone 		= document.getElementById("phone").value;
comments 	= document.getElementById("comments").value;
confidential 	= document.getElementById("confidential");



if(confidential.checked==true){
confidential=1
}else{
	confidential=0
}


	alertValue=""

if(email == email2){}
else {alertValue="The e-mail fields aren't the same"}

isEmpty(email2, "Repeat Email Address")
isEmpty(email, "Email Address")
isEmpty(address, "Postal Address")
isEmpty(lastName, "Last Name")
isEmpty(firstName, "First Name")


//doesEmailExist(email)
	
	
if (alertValue!==""){
	alert(alertValue)
	return false
	}else{


submitForm()
return false

	}
}

function isEmpty(field, fieldName){
	if(field==""){
	alertValue=fieldName + " cannot be empty"
	}

}
function submitForm() {
    /* Cancel the submit event, and find out which form was submitted */

    /* Set up the request */
    var xmlhttp =  new XMLHttpRequest();
    xmlhttp.open('POST', 'registerExec.asp', true);
    
    /* The callback function */
    xmlhttp.onreadystatechange = function() {
		        if (xmlhttp.readyState == 1) {
document.getElementById("submitform").disabled="disabled"
document.getElementById("submitform").value="Loading... Please wait"
				}
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200)
id = xmlhttp.responseText


document.getElementById("submitform").disabled=""
document.getElementById("submitform").value="Registration Complete"

document.location = "signatories.asp?message=Your Registration has been successful, Please find your signature below"


}
    }
    
    /* Send the POST request */
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	params =	"firstName="	+ encodeURI(firstName) +
				"&lastName="		+ encodeURI(lastName) +
				"&postCode="		+ encodeURI(postCode) +
				"&address="		+ encodeURI(address) +
				"&line2="		+ encodeURI(line2) +
				"&town="			+ encodeURI(town) +
				"&county="		+ encodeURI(county) +	
				"&phone="		+ encodeURI(phone) +	
				"&comments="	+ encodeURI(comments) +	
				"&email="		+ encodeURI(email) +
				"&email2="		+ encodeURI(email2) +
				"&confidential="		+ encodeURI(confidential);
//alert(params)
xmlhttp.send(params);
    
}

function doesEmailExist(email){
var xmlhttp =  new XMLHttpRequest();
    xmlhttp.open('POST', 'emailExists.asp', true);
    
    /* The callback function */
    xmlhttp.onreadystatechange = function() {
		        if (xmlhttp.readyState == 1) {
document.getElementById("submitform").value="Validating Your Email"
				}
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200)
exists = xmlhttp.responseText
if (exists!==0){
	emailExists="That email already exists, Please use another"
	
	document.getElementById("submitform").value="Complete asfsfs"

}

}}
    
    
    /* Send the POST request */
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	params =	"email="	+ encodeURI(email);
xmlhttp.send(params);
}
