function getRequest() {
    var xmlhttp=false;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    // JScript gives us Conditional compilation, we can cope with old IE versions.
    // and security blocked creation of the objects.
     try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
      try {
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
       xmlhttp = false;
      }
     }
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
        try {
            xmlhttp = new XMLHttpRequest();
        } catch (e) {
            xmlhttp=false;
        }
    }
    if (!xmlhttp && window.createRequest) {
        try {
            xmlhttp = window.createRequest();
        } catch (e) {
            xmlhttp=false;
        }
    }

    return xmlhttp;
}

function processContactForm() {
	var args = PreProcessContactForm();
	
	//do request
    if (args != ''){
    	var request = getRequest();
		formButtonEnable(0);

	    request.onreadystatechange=function()
		{
		if(request.readyState==4)
		  {
		  	PostProcessContactForm(request.responseText, args);
		  }
		}

		clearFeedback('GOOD');
		clearFeedback('BAD');

		addFeedback("GOOD", "We are sending your information.")

		var address = "ajax/processcontact.php?"+args;
		request.open("get", address, true);
    	request.send(null);
	}
}

function PostProcessContactForm(res, args){
	clearFeedback('GOOD');
	clearFeedback('BAD');

	if (res == "OK"){
		addFeedback("GOOD", "Your information was successfully sent.");
		addFeedback("GOOD", "If you need to send again, click the Reset button.");
		return;
	} else {
		formButtonEnable(1);
		if (res != ""){
			addFeedback("BAD", res)
		} else {
			addFeedback("BAD", "We are sorry, but the server could not be contacted. Please try again soon.")
		}
	}
}

function PreProcessContactForm(){
	clearFeedback('GOOD');
	clearFeedback('BAD');
	var args = '';
	var errors = 0;

	//----------
	//REQUIRED
	//first name
	ele = document.getElementById("contact_form_firstName");
	highlightMissingInfo(ele, 0);
	var fnStr = ele.value;
	filter=/^[a-zA-Z\.' ]+$/
	if (!filter.test(fnStr)){
		addFeedback("BAD", "The first name field may only be letters and punctuation.")
		highlightMissingInfo(ele, 1);
		ele.onchange = PreProcessContactForm;
		errors++;
	} else {
		args += "firstName="+fnStr;
	}
	
	//last name
	ele = document.getElementById("contact_form_lastName");
	highlightMissingInfo(ele, 0);
	var lnStr = ele.value;
	filter=/^[a-zA-Z\.' ]+$/
	if (!filter.test(lnStr)){
		addFeedback("BAD", "The last name field may only be letters and punctuation.")
		highlightMissingInfo(ele, 1);
		ele.onchange = PreProcessContactForm;
		errors++;
	} else {
		args += "&lastName="+lnStr;
	}
	
	ele = document.getElementById("contact_form_emailAddress");
	highlightMissingInfo(ele, 0);
	var emailStr = ele.value;
	if (!check_email(emailStr)){
		addFeedback("BAD", "The email address is not valid.")
		highlightMissingInfo(ele, 1);
		ele.onchange = PreProcessContactForm;
		errors++;
	} else {
		args += "&emailAddress="+emailStr;
	}
	
	ele = document.getElementById("contact_form_description");
	highlightMissingInfo(ele, 0);
	if (ele.value == ""){
		addFeedback("BAD", "You must have a project description.")
		highlightMissingInfo(ele, 1);
		ele.onchange = PreProcessContactForm;
		errors++;
	}else {
		args += "&description="+ele.value;
	}
	
	
	args += "&phoneNumber="+document.getElementById("contact_form_phoneNumber").value;
	
	if (errors != 0){
		args = '';
	}
	return args;
}

function getFItem(type){
	var fItem = "";
	if (type == "GOOD")	{
		fItem = document.getElementById("positiveFeedback");
	} else if (type == "BAD"){
		fItem = document.getElementById("negativeFeedback");
	}
	return fItem;
}

function addFeedback(type, info){
	var fItem = getFItem(type);
	if (fItem){
		if (fItem.innerHTML){
			fItem.innerHTML+="<br/>"+info;
		}else{
			fItem.innerHTML = info;
		}
		
		if (info){
			fItem.style.display = "block";

		}
	}
}

function check_email(e) {
	var filter=/^.+@.+\..{2,3}$/

	if (filter.test(e)) return 1;
	return 0;
}

function clearFeedback(type){
	var fItem = getFItem(type);
	if (fItem){
		fItem.innerHTML = "";
	}
	fItem.style.display = "none";
}

function highlightMissingInfo(ele, force){
	if (ele){
		if (force == 1 || (ele.value == "" && force == "")){
			ele.style.backgroundColor = "#FF8888";
		} else {
			ele.style.backgroundColor = "#FFFFFF";
		}
	}
}

function formButtonEnable(en){
	if (en){
		getFormButton().disabled = "";
	} else {
		getFormButton().disabled = "disabled";
	}
}

function getFormButton(){
	return document.getElementById("contact_form_continue");
}

function getFormReset(){
	return document.getElementById("contact_form_reset");
}