var xmlHttp;
// create request object
function createXMLHttpRequest() {
	if(window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if(window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
}

// response handler. calls menuChanger when server response is recieved
function handler() {
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			contactMade();	
		} else {
			contactFailed();
		}
	}
}

//create XMLhttp request
function makeContact(target) { //This is the function that should be called from the page needing to be modified.
	title = target.title.value;
	fname = target.fname.value;
	lname = target.lname.value;
	email = target.email.value;
	phone = target.phone.value;
	comments = target.comments.value;
	jobid = target.jobid.value;

	reqstring = "title=" + escape(title) + "&fname=" + escape(fname) + "&lname=" + escape(lname) + "&email=" + escape(email) + "&phone=" + escape(phone) + "&comments=" + escape(comments)+ "&jobid=" + escape(jobid);
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handler;
	xmlHttp.open("POST", "/forms/contactform", true);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttp.send(reqstring);
	return false; //prevent normal submit (since we are doing this with ajax)
}

function contactMade() { //this function takes the response from the PHP call and puts info back into the page.
	document.getElementById('contactFormMatte').style.display = "none";
	var results = xmlHttp.responseText;
	if(results.indexOf("Success", 0))
	{
		alert('Your Message has been delivered');
	}
}

function contactFailed() {
	document.getElementById('contactFormMatte').style.display = "none";
	alert('There was a communication error.\n\nPlease try again later or, send an email to jodi@spotonrecruiting.com');
}
