// JavaScript Document

function end() {
alert( "6 seconds later?");
} 

function validateQuote() {
 	var errors = false;
 	var emailq = document.forms['quoteForm'].elements['emailq'].value;
 	var nameq = document.forms['quoteForm'].elements['nameq'].value;
 	var message = document.forms['quoteForm'].elements['comments'].value;
	var town = document.forms['quoteForm'].elements['town'].value;
	var phoneq = document.forms['quoteForm'].elements['phoneq'].value;
	var sqft = document.forms['quoteForm'].elements['sqft'].value;
	var bedrooms = document.forms['quoteForm'].elements['bedrooms'].value;
	var bathrooms = document.forms['quoteForm'].elements['bathrooms'].value;
	
//	for (i=0;i<document.forms['quoteForm'].freq.length;i++) {
//		if (document.forms['quoteForm'].freq[i].checked) {
//			var freq = document.forms['quoteForm'].freq[i].value;}		
//	}
//
//	for (i=0;i<document.forms['quoteForm'].property.length;i++) {
//		if (document.forms['quoteForm'].property[i].checked) {
//			var property = document.forms['quoteForm'].property[i].value;}
//	}
//
//	for (i=0;i<document.forms['quoteForm'].contact.length;i++) {
//		if (document.forms['quoteForm'].contact[i].checked) {
//			var contact = document.forms['quoteForm'].contact[i].value;}
//	}
	
	// Reset All Pink Backgrounds
	document.getElementById('emailq').style.background ='#bbf6fa';
	document.getElementById('nameq').style.background ='#bbf6fa';
	document.getElementById('town').style.background ='#bbf6fa';
	document.getElementById('sqft').style.background ='#bbf6fa';
	document.getElementById('bedrooms').style.background ='#bbf6fa';
	document.getElementById('bathrooms').style.background ='#bbf6fa';
	document.getElementById('phoneq').style.background ='#bbf6fa';
	
	
	
	var thisObj = eval("document.getElementById('errormsg').style");  //Reset error message
		thisObj.visibility = "hidden";
		var errors = false;
	var thisObj1 = eval("document.getElementById('cancelmsg').style");  //Reset error message
		thisObj.visibility = "visible";
 	
	//Check email address
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(emailq) == false) {
		document.getElementById('emailq').style.background ='#ffcccc';
		errors = true ;	}
	
	//Check Name & Town are only text
	var reg = /^[A-Za-z .'-]+$/;
	if(reg.test(nameq) == false) {
		document.getElementById('nameq').style.background ='#ffcccc';
		errors = true ;	}
	if(town.length==0) {
		document.getElementById('town').style.background ='#ffcccc';
		errors = true ;	}
	
	//Check for numeric input
	if(sqft != parseInt(sqft)) {
		document.getElementById('sqft').style.background ='#ffcccc';
		errors = true;	}
	if(bedrooms != parseInt(bedrooms)) {
		document.getElementById('bedrooms').style.background ='#ffcccc';
		errors = true;	}
	if(bathrooms != parseInt(bathrooms)) {
		document.getElementById('bathrooms').style.background ='#ffcccc';
		errors = true;	}
	
	///Check for vaild Phone Number
	phonetest = phoneq
	phonetest = phonetest.replace(/[-]/g,"");	//replace all dashes
	phonetest = phonetest.replace(/ /g,"");		//replace all spaces
	phonetest = phonetest.replace(/[.]/g,"");	//replace all periods
	phonetest = phonetest.replace(/\(/g,"");	//replace all (
	phonetest = phonetest.replace(/\)/g,"");	//replace all )
    var valid = "0123456789";
		//if number of character in phone field is less than 10 - display warning and return false
	if(phonetest.length != 10) {
		document.getElementById('phoneq').style.background ='#ffcccc';
		errors = true;
    }
        //check each character entered in the phone field
	for (var i=0; i < phonetest.length; i++) {
         //put in temp variable each character, one at a time.
		temp = "" + phonetest.substring(i, i+1);
    	//check index of a phone character in the "valid" variable.
    	// if temp contains a character which is not in "valid" variable,
    	//then valid.indexOf(temp) will be -1, otherwise it may be 0.1.2.3.4.5.6.7.8 or 9
        if (valid.indexOf(temp) == "-1") {
    		document.getElementById('phoneq').style.background ='#ffcccc';
			errors = true;
        }
	}

	

	if(errors == true) {		
		thisObj.visibility = "visible";
		thisObj1.visibility = "hidden";
		return false; }
	else {
		alert("Your Quote Request has been submitted. \n \n  We value the opportunity to be of service to you, \n  and will contact you shortly. \n \n  Thank you for your consideration! \n \n ");
		setTimeout( "end();", 3000);
	}
	
	
}

function validate_email() {
 	var errors = "";
 	var email = document.forms['eMailForm'].elements['email'].value;
 	var name = document.forms['eMailForm'].elements['name'].value;
 	var message = document.forms['eMailForm'].elements['comments'].value;
 
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  	errormessage = "appears to be invalid."
	if(reg.test(email) == false) {
		 errors = "\t email address"
	}
	 
	var reg = /^[A-Za-z .'-]+$/;
  
	if(reg.test(name) == false) {
		errors = errors + "\t name" ;
	}
		
	if (message.length < 1) {
	   errors = errors + "\t message";
	}
	
	if (errors.length > 1) {
	  //errors.replace(/^\s+/, '');
	  //var thisObj = eval("document.getElementById('errormsg').style");
	  //thisObj.visibility = "visible";
	  //document.getElementById("errormsg").innerHTML = "Please Correct the Following Errors and Re-Submit:<br>"+errors ;
	  alert("Please Correct the Following Errors and Re-Submit:\n \n \n "+errors+"\n");
	  return false; }
	else {	  
	  alert( "Thank you for your message. \n \n  We will be in contact with you shortly. \n \n ");
	  setTimeout( "return true;", 3000);	
	}
}

function validate_email2() {
 	var errors = "";
 	var email = document.forms['eMailForm'].elements['email'].value;
 	var name = document.forms['eMailForm'].elements['name'].value;
 
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  	errormessage = "appears to be invalid."
	if(reg.test(email) == false) {
		 errors = "\t email address"
	}
	 
	var reg = /^[A-Za-z .'-]+$/;
  
	if(reg.test(name) == false) {
		errors = errors + "\t name" ;
	}
	
	if (errors.length > 1) {
	  alert("Please Correct the Following Errors and Re-Submit:\n \n \n "+errors+"\n");
	  return false; }
	else {	  
	  alert( "Your request is being sent.");
return true;
	}
}

function validate_email3() {
 	var errors = "";
 	var email = document.forms['eMailForm'].elements['email'].value;
 	var name = document.forms['eMailForm'].elements['name'].value;
 	var message = document.forms['eMailForm'].elements['comments'].value;
 
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  	errormessage = "appears to be invalid."
	if(reg.test(email) == false) {
		 errors = "\t email address"
	}
	 
	var reg = /^[A-Za-z .'-]+$/;
  
	if(reg.test(name) == false) {
		errors = errors + "\t name" ;
	}

	if (message.length < 1 || message=="Message:") {
	   errors = errors + "\t message";
	}
	
	if (errors.length > 1) {
	  alert("Please Correct the Following Errors and Re-Submit:\n \n \n "+errors+"\n");
	  return false; }
	else {	  
	  alert( "Your message is being sent.");
return true;
	}
}

function send_list_form(){
if (validate_email2()){
document.forms['eMailForm'].submit()
}
}

function send_list_form2(){
if (validate_email3()){
document.forms['eMailForm'].submit()
}
}
