// JavaScript Document
//Form Validation Functions
function validate_field(field, fieldValue){
	if((fieldValue==null)||(fieldValue == "")){
		document.getElementById(field).setAttribute('class','rejected');
		document.getElementById(field).setAttribute('className', 'rejected');
		return false;
	} else {
		document.getElementById(field).setAttribute('class','accepted');
		document.getElementById(field).setAttribute('className', 'accepted');
	}
}

function validate() {

	if((document.contactForm.first_name.value=='')||(document.contactForm.first_name.value==null)){
    	alert('Please fill in your first name');
		return false;
	}
	if((document.contactForm.last_name.value=='')||(document.contactForm.last_name.value==null)){
    	alert('Please fill in your last name');
		return false;
	}
	if((document.contactForm.title.value=='')||(document.contactForm.title.value==null)){
    	alert('Please fill in your title');
		return false;
	}
	if((document.contactForm.account_name.value=='')||(document.contactForm.account_name.value==null)){
    	alert('Please fill in your company name');
		return false;
	}
	if((document.contactForm.primary_address_street.value=='')||(document.contactForm.primary_address_street.value==null)){
    	alert('Please fill in your street address');
		return false;
	}
	if((document.contactForm.primary_address_city.value=='')||(document.contactForm.primary_address_city.value==null)){
    	alert('Please fill in your City');
		return false;
	}
	if((document.contactForm.primary_address_state.value=='')||(document.contactForm.primary_address_state.value==null)){
    	alert('Please fill in your State');
		return false;
	}
	if((document.contactForm.primary_address_postalcode.value=='')||(document.contactForm.primary_address_postalcode.value==null)){
    	alert('Please fill in your zip/postal code');
		return false;
	}
	if((document.contactForm.country.value=='')||(document.contactForm.country.value==null)){
    	alert('Please fill in your country');
		return false;
	}
	if((document.contactForm.phone_work.value=='')||(document.contactForm.phone_work.value==null)){
    	alert('Please fill in your phone number');
		return false;
	}
	if((document.contactForm.phone_work.value=='')||(document.contactForm.phone_work.value==null)){
    	alert('Please fill in your phone number');
		return false;
	}
	if((document.contactForm.fax_work.value=='')||(document.contactForm.fax_work.value==null)){
    	alert('Please fill in your fax number');
		return false;
	}
	var emailTest = echeck('webtolead_email1', document.contactForm.webtolead_email1.value);
	
	if((emailTest==false)||(emailTest==null)){
    	alert('Please fill in a valid email address');
    	return false;
	}
	
	process();
	
		return true;
}


function echeck(field, fieldValue) {

		var at="@"
		var dot="."
		var lat=fieldValue.indexOf(at)
		var lstr=fieldValue.length
		var ldot=fieldValue.indexOf(dot)
		if (fieldValue.indexOf(at)==-1){
			document.getElementById(field).setAttribute('class','rejected');
			document.getElementById(field).setAttribute('className', 'rejected');
			return false;

		} else if (fieldValue.indexOf(at)==-1 || fieldValue.indexOf(at)==0 || fieldValue.indexOf(at)==lstr){
			document.getElementById(field).setAttribute('class','rejected');
			document.getElementById(field).setAttribute('className', 'rejected');
			return false;
		
		} else if (fieldValue.indexOf(dot)==-1 || fieldValue.indexOf(dot)==0 || fieldValue.indexOf(dot)==lstr){
			document.getElementById(field).setAttribute('class','rejected');
			document.getElementById(field).setAttribute('className', 'rejected');
			return false;

		} else if (fieldValue.indexOf(at,(lat+1))!=-1){
			document.getElementById(field).setAttribute('class','rejected');
			document.getElementById(field).setAttribute('className', 'rejected');
			return false;

		} else if (fieldValue.substring(lat-1,lat)==dot || fieldValue.substring(lat+1,lat+2)==dot){
			document.getElementById(field).setAttribute('class','rejected');
			document.getElementById(field).setAttribute('className', 'rejected');
			return false;

		} else if (fieldValue.indexOf(dot,(lat+2))==-1){
			document.getElementById(field).setAttribute('class','rejected');
			document.getElementById(field).setAttribute('className', 'rejected');
			return false;

		} else if (fieldValue.indexOf(" ")!=-1){
			document.getElementById(field).setAttribute('class','rejected');
			document.getElementById(field).setAttribute('className', 'rejected');
			return false;

		} else {
			document.getElementById(field).setAttribute('class','accepted');
			document.getElementById(field).setAttribute('className', 'accepted');
			return true;
		}
}

function createXMLHttpRequestObject()
{

	// store the reference to the XMLHttpReuquest object
	var xmlhttp;

	// IE7+Modern
	try {
		xmlhttp = new XMLHttpRequest();
	} catch(e) {
		// IE6-
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch(e) {
				try {
					xmlhttp = new ActiveXObject("Msxml2.XMLHTTP")
				} catch(e) {}
		}
	}
	if (!xmlhttp)
	{
	   	alert("Error creating the XMLHttpRequest Object");
	} else {
		return xmlhttp;

	}
}
	
function process()
{

	if ( xmlhttp )
	{
		try {
			// get the value from the <select> element
			var captcha_value = document.getElementById("captcha_code").value;

			// create the params string
			var params = "cvalue=" + captcha_value;

			// create url string
			var url = "http://www.briess.com/food/About/ajaxcaptcha.php?" + params;

			// initiate the async HTTP request
			xmlhttp.open("GET", url, true);
			xmlhttp.onreadystatechange = handleRequestStateChange;
			xmlhttp.send(null);

		} catch(e) {
			alert("Can't connect to server:\n" + e.toString());
		}
	}
}

function handleRequestStateChange()
{
	// obtain reference to the <div> element on the page
	var captcha_error = document.getElementById('error');

	if ( xmlhttp.readyState == 4 )
	{
		if ( xmlhttp.status == 200 )
		{
			try {
				//handleServerResponse()

				// read message from the server
				response = xmlhttp.responseText;

				if ( response == '0' )
				{
				document.getElementById('captcha').src = '../securimage/securimage_show.php?' + Math.random();
				document.getElementById('captcha_code').focus();
				captcha_error.innerHTML = "Please enter a valid code.";
				} else {

					document.getElementById('cForm').submit();
				}
			} catch(e) {
				alert( "Error reading the response: " + e.toString() );
			}
		} else {
				alert("There was a problem retrieving the data: \n" + xmlhttp.statusText);
		}

	}
}
