// JavaScript Document

var xmlHttp;

function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
}

function sendCourseEmail() {
    
	var Contact_Name = document.getElementById("Contact_Name").value;
	var Email = document.getElementById("Email").value;
	var Telephone = document.getElementById("Telephone").value;
	var Course = document.getElementById("Course").value;
	var Course_Date = document.getElementById("Date").value;
	var Delegates = document.getElementById("Delegates").value;
	var Message = document.getElementById("Message").value;
	var strMailingList = document.getElementById("mailing_list").checked;
	var AddressNumber = document.getElementById("AddressNumber").value ;
	var AddressRoad = document.getElementById("AddressRoad").value;
	var AddressCity = document.getElementById("AddressCity").value;
	var Postcode = document.getElementById("Postcode").value;
    //var strMailingList = document.getElementById("Mailing_List").checked;
	
	if (Contact_Name == ""){
		alert("Form Error: Please enter a full contact name");
		document.getElementById("Contact_Name").focus();
		return false;
	}
	else if (checkemail(Email) == false) {
		alert("Form Error: Please submit valid email address");
		document.getElementById("Email").focus();
		return false;
	}
	else if (Course == "") {
		alert("Form Error: Please select a course");
		document.getElementById("Course").focus();
		return false;
	}
	else {
	
	createXMLHttpRequest();
	
	document.getElementById("form-status").style.display = "none";
    
	var queryString = "course-enquiry-send.asp?Contact_Name=" + Contact_Name + "&Email=" + Email + "&Telephone=" + Telephone + "&Course=" + Course + "&Date=" + Course_Date + "&Delegates=" + Delegates + "&Message=" + Message + "&Mailing_List=" + strMailingList + "&AddressNumber=" + AddressNumber + "&AddressRoad=" + AddressRoad + "&AddressCity=" + AddressCity + "&Postcode=" + Postcode;
	
	//alert(queryString);
	
    xmlHttp.open("POST", queryString, true);
    xmlHttp.onreadystatechange = handleStateChange;
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");   
    xmlHttp.send(queryString);
	
	document.getElementById("form-sending").style.display = "block";
	
	}
}
    
function handleStateChange() {
    if(xmlHttp.readyState == 4) {
        if(xmlHttp.status == 200) {
            parseResults();
        }
    }
}

function parseResults() {
    var responseDiv = document.getElementById("server-response");
	document.getElementById("form-error").style.display = "block";
    if(responseDiv.hasChildNodes()) {
        responseDiv.removeChild(responseDiv.childNodes[0]);
		responseDiv.style.color = "#900";
    }
    
    var responseText = document.createTextNode(xmlHttp.responseText);
    responseDiv.appendChild(responseText);
	
	if (responseText.length == 6) {
		document.getElementById("form-error").style.display = "none";
		responseDiv.style.color = "#333";
		
		window.setTimeout('displayFinish()',2000);	
	}
}

function checkemail(str){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(str))
		return true;
	else {
		return false;
	}
}

function ResetForm(xFormName){
    var frm = document.getElementById(xFormName);
    frm.reset();
}

showAddress = function() {
	if (document.getElementById) {
		var state = document.getElementById("mailing_list").checked;
		//alert(state);
		var div = document.getElementById("form_address")
		if (state == true) {
			div.style.display = "block";
			createCookie("form_state","block");	
		}
		else {
			div.style.display = "none";
			createCookie("form_state","none");
		}
	}
}

function createCookie(name,value) {
	document.cookie = name + " = " + value;
}

// now you are taking the P

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function displayFinish(){
	document.getElementById("form-sending").style.display = "none";
	document.getElementById("form-status").style.display = "block";
	ResetForm("styled_form");
}