function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function emailValidate(fld) {
    var tfld = trim(fld.value);  // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.border="2px solid red";
    } else if (!emailFilter.test(tfld)) {
        fld.style.border="2px solid red";
    } else if (fld.value.match(illegalChars)) {
        fld.style.border="2px solid red";
    } else {
        fld.style.border='';
    }
}


function phoneValidate(fld) { 
	var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, ''); 
	if (isNaN(parseInt(stripped))) { 
		fld.style.border="2px solid red"; 
	} else if (stripped.length < 10) {  
		fld.style.border="2px solid red"; 
	} else {
		fld.style.border=''; 
	}
} 

function nameValidate(fld){
	var alphaExp = /^[a-zA-Z\- ]+$/ ;
	if(!fld.value.match(alphaExp)){ 
		document.contactus.name.style.border="2px solid red";
	}else{
		document.contactus.name.style.border='';
	}
}

function companyValidate(fld){
	if (fld.value==null || fld.value.length == 0){
		document.contactus.company.style.border="2px solid red";
	}else{
		document.contactus.company.style.border='';
	}
} 

function validateOnSubmit(fld) {
	
	var tfld = trim(document.contactus.email.value);  // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;	
	
	var stripped = document.contactus.phone.value.replace(/[\(\)\.\-\ ]/g, ''); 
	
	var alphaExp = /^[a-zA-Z\- ]+$/ ;

	if(document.contactus.name){
		
		if(document.contactus.name.value.length < 2){
			document.contactus.name.style.border="2px solid red";
			return false;	
		} else if(!document.contactus.name.value.match(alphaExp)){
			document.contactus.name.style.border="2px solid red";
			alert("Only letters, dashes, and spaces allowed in your name");
			return false;
			
			
		} else if(document.contactus.email.value == "") {
			document.contactus.email.style.border="2px solid red";
			return false;
		} else if(!emailFilter.test(tfld)) {
			document.contactus.email.style.border="2px solid red";
			alert("Please enter a valid email address");
			return false;
		} else if(document.contactus.email.value.match(illegalChars)) {
			document.contactus.email.style.border="2px solid red";
			alert("The email address contains illegal characters");
			return false;
						

		} else if (isNaN(parseInt(stripped))) {
			document.contactus.phone.style.border="2px solid red";
			return false;
		} else if (stripped.length < 10){
			alert('Your phone number must be atleast 10 digits');
			document.contactus.phone.style.border="2px solid red";
			return false;
			

		} else {
			insitePost();
			document.contactus.email.style.border='';
			document.contactus.phone.style.border='';
			document.contactus.name.style.border='';

			var name = encodeURIComponent(document.contactus.name.value);	
			var title = encodeURIComponent(document.contactus.title.value);
			var company = encodeURIComponent(document.contactus.company.value);
			var city = encodeURIComponent(document.contactus.city.value);
			var state = encodeURIComponent(document.contactus.state.value);
			var country = encodeURIComponent(document.contactus.country.value);
			var email = document.contactus.email.value;
			var zip = encodeURIComponent(document.contactus.zip.value);
			var phone = document.contactus.phone.value;		
			var indus_served = document.getElementById('indus_served').options[document.getElementById('indus_served').selectedIndex].value;
			var inquiry_type = document.getElementById('inquiry_type').options[document.getElementById('inquiry_type').selectedIndex].value;
			var hear_about_us = document.getElementById('hear_about_us').options[document.getElementById('hear_about_us').selectedIndex].value;
			var comment = encodeURIComponent(document.contactus.comment.value);

			
			sendEmail(comment, hear_about_us, inquiry_type, indus_served, phone, zip, email, country, city, state, company, title, name);
		} 
	}
}

function insitePost(fld) {
	d = document.forms[0];
	//save the action to insite metrics for tracking
	url = "http://www.topfloortech.com/insitemetrics/uRMJ/uniformv2.php";
	url += "?actk=cweavw-3eiaft32cl"; //unique action key
	url += "&imReferrerField=" + escape(document.referrer);
	url += "&name=" + escape(d.name.value);
	url += "&title=" + escape(d.title.value);
	url += "&company=" + escape(d.company.value);
	url += "&city=" + escape(d.city.value);
	url += "&state=" + escape(d.state.value);
	url += "&country=" + escape(d.country.value);
	url += "&imEmailField=" + escape(d.email.value);
	url += "&zip=" + escape(d.zip.value);
	url += "&phone=" + escape(d.phone.value);
	url += "&indus_served=" + escape(document.getElementById('indus_served').options[document.getElementById('indus_served').selectedIndex].value);
	url += "&inquiry_type=" + escape(document.getElementById('inquiry_type').options[document.getElementById('inquiry_type').selectedIndex].value);
	url += "&hear_about_us=" + escape(document.getElementById('hear_about_us').options[document.getElementById('hear_about_us').selectedIndex].value);
	url += "&comment=" + escape(d.comment.value);
	img = new Image(0,0);
 	setTimeout(function(){img.src = url;},500);
}

function resetStyles(){
	document.contactus.email.style.border='';
	document.contactus.phone.style.border='';
	document.contactus.name.style.border='';		
}

function GetXmlHttpObject(){
	var xmlHttp=null;
	try{xmlHttp=new XMLHttpRequest();// Firefox, Opera 8.0+, Safari
	}catch (e){
		try{// Internet Explorer
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e){
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function sendEmail(comment, hear_about_us, inquiry_type, indus_served, phone, zip, email, country, city, state, company, title, name){
	document.getElementById('inquiry_form').innerHTML = '<span class="loader"><img src="images/loader.gif" height="15" width="128" alt=""></span>'; 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
		//alert("Your browser does not support AJAX!");
		document.getElementById('inquiry_form').innerHTML = '<p class="mailFailed" style="font-size:10pt">Your browser does not support AJAX</p>'; 
		return;
	}
	var url="forms/send_inquiry.php";
	url=url+"?name="+name+"&title="+title+"&company="+company+"&city="+city+"&state="+state+"&country="+country+"&email="+email+"&zip="+zip+"&phone="+phone+"&indus_served="+indus_served+"&inquiry_type="+inquiry_type+"&hear_about_us="+hear_about_us+"&comment="+comment;
	url=url+"&rand="+Math.random();
	xmlHttp.onreadystatechange=function(){
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
			if (xmlHttp.status == 200) {
				mailSent=xmlHttp.responseText;
				if(mailSent==true){
					window.location = 'thankyou_form.php';
				}
			} else {
				mailSent='<p class="mailFailed" style="font-size:10pt">* Submission error. Please try again.</p>';
				document.getElementById('inquiry_form').innerHTML = mailSent;
			}
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}