/*

Form validation for: /contact/default.aspx

---

01. FORM VALIDATION
02. HELPER FUNCTIONS

*/

/*----------------------------------------------------------
01. FORM VALIDATION
----------------------------------------------------------*/
function checkContactForm(theform) {
	var isValid = true;
	if (checkContactFormInput(theform, theform.comments, 'comments', 'Please enter your project description') == false) { isValid = false; theform.comments.focus(); }
	if (checkContactFormInput(theform, theform.email, 'email', 'Please enter an email address') == false) { isValid = false; theform.email.focus();}
	var f=/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i;
	e=theform.email.value;
	if(!f.test(e)){ 
		isValid = false; 
		theform.email.focus(); 		
		if ( $("#email") ) {
			$("#email").animate({ backgroundColor: "#FF0000" }, 'slow');
			$("#email").animate({ backgroundColor: "#FFAFAF" }, 'slow');
		}
	} 	
	if (checkContactFormInput(theform, theform.phone, 'phone', 'Please enter a phone number') == false) { isValid = false; theform.phone.focus(); }
	if (checkContactFormInput(theform, theform.company, 'company', 'Please enter a company name') == false) { isValid = false; theform.company.focus(); }
	if (checkContactFormInput(theform, theform.lastname, 'lastname', 'Please enter last name') == false) { isValid = false; theform.lastname.focus();}
	if (checkContactFormInput(theform, theform.firstname, 'firstname', 'Please enter first name') == false) { isValid = false; theform.firstname.focus();}
	if (isValid) {
		document.getElementById('general-inquiry').submit();
		return true;
	} else {
		return false;
	}
}

function checkContactFormInput(theform, theinput, theinputid, errormsg) {
	if ( theinput.value.length == 0 || theinput.value == errormsg ) {
		theinput.value = errormsg;

		if ( $("#"+theinputid) ) {
			$("#"+theinputid).animate({ backgroundColor: "#FF0000" }, 'slow');
			$("#"+theinputid).animate({ backgroundColor: "#FFAFAF" }, 'slow');
		}
		
		return false;
	} else {
		return true;
	}
}

function contactFormOnClickInput(theinput) {
	if ( Left(theinput.value, 13) == 'Please enter ' ) {
		theinput.value = '';
		theinput.style.backgroundColor = '#E7E7E7';
	}
}


/*----------------------------------------------------------
02. HELPER FUNCTIONS
----------------------------------------------------------*/
function Left(str, n) {
   if (n <= 0)
         return "";
   else if (n > String(str).length)
         return str;
   else
         return String(str).substring(0,n);
}
