﻿	
function contactUs()
{
    if ( validateContactForm() )
        loadXMLDoc("contactus.asmx/contactUs", "name=" + document.getElementById("contactName").value + "&company=" + document.getElementById("contactCompany").value + "&email=" + document.getElementById("contactEmail").value + "&phone=" + document.getElementById("contactPhone").value + "&website=" + document.getElementById("contactWebsite").value + "&comments=" + document.getElementById("contactComments").value );
	return false;
}

var req;
function loadXMLDoc(url,body) 
{
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange;
		req.open("POST", url, true);
		req.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
		req.send(body);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
    	req = new ActiveXObject("Microsoft.XMLHTTP");
    	if (req) {
        	req.onreadystatechange = processReqChange;
	    	req.open("POST", url, true);
			req.send(body);
		}
	}
}

function processReqChange() 
{
// only if req shows "complete"
    if (req.readyState == 4) {
    // only if "OK"
		if (req.status == 200) {
		
			var doc = req.responseXML;   // assign the XML file to a var
			alert("Thank you, we have received your information and will contact you within 1 business day. \n If you require faster response, please call Jonathan Minder direct: 858.736.5821");	
            pageTracker._trackPageview("/contactform" );
            document.getElementById("contactName").value = "";			
            document.getElementById("contactCompany").value = "";
            document.getElementById("contactEmail").value = "";
            document.getElementById("contactPhone").value = "";
            document.getElementById("contactWebsite").value = "";
            document.getElementById("contactComments").value = "";
		} else {
        	alert("There was a problem sending your contact information.");
        	alert(req.responseXML);
        }
    }
}
			
			
			
function validateContactForm()
{

    var formValid = true;
    var errorDiv = document.getElementById("formErrorList");
    errorDiv.innerHTML = "";
    errorBoxOff("contactEmailBox");
    errorBoxOff("contactNameBox");
    errorBoxOff("contactCompanyBox");
    errorBoxOff("contactPhoneBox");
    
/*
	if( document.getElementById("contactEmail").value == "" ||  document.getElementById("contactEmail").value == null)
    {
    errorBoxOn("contactEmailBox");
        errorDiv.innerHTML += "<br />To help us better serve you, please provide your email address.";
        formValid = false;
    }
    
*/  

	if( document.getElementById("contactName").value == "" ||  document.getElementById("contactName").value == null)
    {
        errorDiv.innerHTML += "<br />To help us better serve you, please provide your name.";
        errorBoxOn("contactNameBox");
        formValid = false;
    }
    
    
	if( document.getElementById("contactCompany").value == "" ||  document.getElementById("contactCompany").value == null)
    {
        errorDiv.innerHTML += "<br />To help us better serve you, please provide your company name.";
        errorBoxOn("contactCompanyBox");
        formValid = false;
    }
    
    
	if( document.getElementById("contactPhone").value == "" ||  document.getElementById("contactPhone").value == null)
    {
        errorDiv.innerHTML += "<br />To help us better serve you, please provide your phone number.";
        errorBoxOn("contactPhoneBox");
        formValid = false;
    }


	var str = document.getElementById("contactEmail").value;
	var Exp=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if(!Exp.test(str))
	{
	    errorDiv.innerHTML += "<br />To help us better serve you, please provide a valid email address.";
	    errorBoxOn("contactEmailBox");
	    
//	    document.getElementById("contactEmailBox").style.color = "red" ;
		formValid = false;
	}				




    return formValid;
}


function errorBoxOn( divObj )
{
    document.getElementById(divObj).style.border = "2px solid red";
}

function errorBoxOff( divObj )
{
    document.getElementById(divObj).style.border = "";
}
