var http = getHTTPObject();

function handleCodeVerification() {

	if (http.readyState == 4) { //finished
	
		result = http.responseText;
		
		if (result == "success") {
			//The form was sent with no problems
			document.getElementById('formcontainer').innerHTML="Your message was successfully submitted. Thank you for your comments."
		} else {
			//There were problems
			document.getElementById('formcontainer').innerHTML="Whoops! Something seems to have gone wrong while trying to submit your message. Please try again later."
		}
		
	}

}

function sendform() {
	
	if (checkfields()) {
		
		//Fields are good, send the data
		
		//Change the text on the button and disable all fields and the button
		document.getElementById('submitbutton').innerHTML="Sending...";
		document.getElementById('name').disabled=1;
		document.getElementById('email').disabled=1;
		document.getElementById('phone').disabled=1;
		document.getElementById('comments').disabled=1;
		document.getElementById('submitbutton').disabled=1;
		
		//Show the sending animation
		document.getElementById('sendanimation').src = 'images/sendanimation.gif';
		
		//Send the data
		transmitdata();
		
	}

}

function checkfields() {

	//Check to make sure the required fields aren't blank and the email address is formatted correctly
	fielderror = false; //if this variable is true at the end of the function, there was an error
	
	//Send To
	if (document.getElementById('sendto').value == '0') {
	
		document.getElementById('labelSendto').style.color = '#FF0000';
		document.getElementById('messageSendto').innerHTML = '<br /><span class="small_bold">Please select a person to contact.</span><br /><br />';
		fielderror = true;
		
	} else {
		
		document.getElementById('labelSendto').style.color = '#555555';
		document.getElementById('messageSendto').innerHTML = '';
	
	}	
	
	//Name
	if (document.getElementById('name').value == '') {
	
		document.getElementById('labelName').style.color = '#FF0000';
		document.getElementById('messageName').innerHTML = '<br /><span class="small_bold">Please enter a name.</span><br /><br />';
		fielderror = true;
		
	} else {
		
		document.getElementById('labelName').style.color = '#555555';
		document.getElementById('messageName').innerHTML = '';
	
	}
	
	//Email
	if (document.getElementById('email').value == '') {
	
		document.getElementById('labelEmail').style.color = '#FF0000';
		document.getElementById('messageEmail').innerHTML = '<br /><span class="small_bold">Please enter an email address.</span><br /><br />';
		fielderror = true;
		
	} else if (!verifyemail(document.getElementById('email').value)) {
	
		document.getElementById('labelEmail').style.color = '#FF0000';
		document.getElementById('messageEmail').innerHTML = '<br /><span class="small_bold">This email address is not valid.</span><br /><br />';
		fielderror = true;
		
	} else {
		
		document.getElementById('labelEmail').style.color = '#555555';
		document.getElementById('messageEmail').innerHTML = '';
	
	}
	
	//Comments
	if (document.getElementById('comments').value == '') {
		
		document.getElementById('labelComments').style.color = '#FF0000';
		document.getElementById('messageComments').innerHTML = '<br /><span class="small_bold">Please enter a comment.</span><br /><br />';
		fielderror = true;
		
	} else {
		
		document.getElementById('labelComments').style.color = '#555555';
		document.getElementById('messageComments').innerHTML = '';
	
	}
	
	return !fielderror;
		
}

function verifyemail(email) {
	
	return (email.lastIndexOf(".") > 2) && (email.indexOf("@") > 0) && (email.lastIndexOf(".") > (email.indexOf("@")+1)) && (email.indexOf("@") == email.lastIndexOf("@"));

}

function transmitdata() {
	
	sendto = escape(document.getElementById('sendto').value);
	name = escape(document.getElementById('name').value);
	email = escape(document.getElementById('email').value);
	phone = escape(document.getElementById('phone').value);
	comments = escape(document.getElementById('comments').value);
	
	http.open("POST", 'contact_send.php', 'true');
	http.onreadystatechange = handleCodeVerification;
	http.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"); //force no-cache
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); //let the server know this is a form
	http.send('sendto='+sendto+'&name='+name+'&email='+email+'&phone='+phone+'&comments='+comments+'&action=send');
	http.send(null);
	
}

function getHTTPObject() {
	
	var xmlhttp;
	
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	
	return xmlhttp;

}
