/*

Form validation for: /contact/default.aspx

---

01. FORM VALIDATION
02. HELPER FUNCTIONS

*/

/*----------------------------------------------------------
01. FORM VALIDATION
----------------------------------------------------------*/
function checkContactForm(theform) {
	//alert('checking contact form');
	
	var isValid = true;
	if (checkContactFormInput(theform, theform.comments, 'comments', 'Please enter your project description') == false) { isValid = false;  }
	if (checkContactFormInput(theform, theform.email, 'email', 'Please enter an email address') == false) { isValid = false; }	
	if (checkContactFormInput(theform, theform.phone, 'phone', 'Please enter a phone number') == false) { isValid = false;  }
	if (checkContactFormInput(theform, theform.company, 'company', 'Please enter a company name') == false) { isValid = false;  }
	if (checkContactFormInput(theform, theform.lastname, 'lastname', 'Please enter last name') == false) { isValid = false; }
	if (checkContactFormInput(theform, theform.firstname, 'firstname', 'Please enter first name') == false) { isValid = false; }
	if (checkContactFormInput(theform, theform.captcha, 'captcha', 'Please enter the correct captcha text') == false) { isValid = false; }
	
	//alert('checks complete');
	
	if (isValid) {
		document.getElementById('general-inquiry').submit();
		return true;
	} else {
		return false;
	}
}

function checkNewsletterForm(theform) {
	var isValid = true;

	if (checkContactFormInput(theform, theform.email, 'email', 'Please enter an email address') == false) { isValid = false; }	
	if (checkContactFormInput(theform, theform.captcha, 'captcha', 'Please enter the correct captcha text') == false) { isValid = false; }
	if (checkContactFormInput(theform, theform.company, 'company', 'Please enter a company name') == false) { isValid = false;  }
	if (checkContactFormInput(theform, theform.lastname, 'lastname', 'Please enter last name') == false) { isValid = false; }
	if (checkContactFormInput(theform, theform.firstname, 'firstname', 'Please enter first name') == false) { isValid = false; }

	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) || ( theinputid=='captcha' && !checkCaptcha(theform.captcha.value) ) || ( theinputid=='email' && checkEmail(theform.email.value)==false ) ) {
		//alert('error found: ' + theinputid);
		theinput.value = errormsg;

		
		if ( $("#"+theinputid) ) {
			//alert('input id found');
			$("#"+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';
	}
}

function checkCaptcha(captchaString) {
	var captchaResponse = $.ajax({
		type: "POST",
		url: "/assets/inc/ajax_captcha.aspx",
		data: "function=checkcaptcha&captcha=" + captchaString,
		async: false
	}).responseText;

	//alert('test - captchaResponse: ' + captchaResponse);
	if (captchaResponse == "success") { return true; } else { return false; };
}

function checkEmail(emailstring) {
	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=emailstring;
	if(!f.test(e)) { 
		return false;
	} else {
		return true;
	}
}


/*----------------------------------------------------------
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);
}