// AJAX CONTACT FORM SCRIPT

function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();

function sendemail() {
	var comments = document.contactform.comments.value;
	var first_name = document.contactform.first_name.value;
	var last_name = document.contactform.last_name.value;
	var email = document.contactform.email.value;
	var phone = document.contactform.phone.value;
	
	var address = document.contactform.address.value;
	var municipality = document.contactform.municipality.value;
	var mlsnumber = document.contactform.mlsnumber.value;
	var contacttype = document.contactform.contacttype.value;
	var property_type = document.contactform.property_type.value;
	var listing_agent = document.contactform.listing_agent.value;
	var showing_agent = document.contactform.showing_agent.value;
	var forminfo = document.contactform.forminfo.value;

	document.contactform.send.disabled=true; 
	document.contactform.send.value='Sending....';

		http.open('POST', 'form_processor.php','true');
		http.onreadystatechange = handleResponse;
		// force no-cache so browser always makes request and doesn't use cache'd results
		http.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
		// tell server this is a form submission
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		//http.send('comments='+comments+'&first_name='+first_name+'&last_name='+last_name+'&phone='+phone+'&email='+email+'&action=send');
		http.send('comments='+comments+'&first_name='+first_name+'&last_name='+last_name+'&phone='+phone+'&email='+email+'&address='+address+'&municipality='+municipality+'&mlsnumber='+mlsnumber+'&contacttype='+contacttype+'&property_type='+property_type+'&listing_agent='+listing_agent+'&showing_agent='+showing_agent+'&forminfo='+forminfo+'&action=send');
}

function handleResponse() {
    if(http.readyState == 4){
        var response = http.responseText;
        var update = new Array();

        if(response.indexOf('|' != -1)) {
            update = response.split('|');
						// unhide the error div or show the main div as block
						//var styler = document.getElementById(update[0]).style;
						//styler.display = styler.height? "":"auto";
						//styler.display = styler.display? "":"block";
						//styler.display = styler.margin-top? "":"0px";
						//update the html in the div
            document.getElementById(update[0]).innerHTML = update[1];
         		document.contactform.send.disabled=false; 
						document.contactform.send.value='Submit';
        }
    }
}
