/* function to trim whitespace from beginning and end of string */

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

/* function to strip non-numeric characters from a string */

function stripAlphaChars(str) 
{  
    return str.replace(/[^0-9]/g, '');  
}

/* function to test if string is empty */
function isEmpty(str)
	{	
		
	if(str == null || str == "" || str.length <1)
		{
		return true;
		}
		
	return false;
	};
	
/* function to test validity of phone number field */

function isValidPhone(p_phone){

	p_phone = stripAlphaChars(p_phone);

	var valid = true;
	
	// we are allowing the format 12345 or 12345-6789
	if ( p_phone == null || p_phone == "" || p_phone.length < 10 || p_phone.length > 10 || isNaN(parseInt(p_phone)) ){
		valid = false;
	}
	
	for ( var i = 0; i < p_phone.length; i++ ){	
	
		if ( isNaN( p_phone.charAt(i)) ){
			return false;
			}
		}
	
	return valid;
}

/* function to test validity of zip code field */

function isValidZipCode(p_zip)
{

	p_zip = stripAlphaChars(p_zip);

	var valid = true;
	
	// we are allowing the format 12345 or 12345-6789
	if (p_zip == null || p_zip == "" || p_zip.length < 5 || p_zip.length > 10 || isNaN(parseInt(p_zip)) )
	{
		valid = false;
	}
	else if( p_zip.length > 5 && p_zip.charAt(5) != "-" )
	{
		valid = false;
	}
	else if( p_zip.length > 5 && p_zip.charAt(5) == "-" && p_zip.length != 10 )
	{
		valid = false;
	}
	
	for(var i=0;i<p_zip.length;i++)
		{	
		if(isNaN(p_zip.charAt(i)))
			{
			return false;
			}
		}
	
	return valid;
}

/* function to test validity of email address */

function  isValidEmail(p_email, mustValidate) {
		if(mustValidate) {
		p_email = trimString( p_email );
 		emailpat = /^([_a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+)(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,4})$/;
 		if( !emailpat.test( p_email ) ) {
  			return false;
 		}
 		return true;
 	}
}
	
/* function to compare email to verify email fields */

function verifyEmail(p_email, p_emailVerify){
	if( p_email.toLowerCase == p_emailVerify.toLowerCase ) {
		return true;
	}
	return false;
}

/* jQuery function to validate form */

jQuery.fn.validate = function(p, failureColor) {
	
	var failed = false;
	
	/* flag all previous validation failures and reset to default states */
	
	this.find(".failed").addClass("previously-failed").removeClass("failed");
	this.find("#page_error").remove();
	
	//only used on request a brochure form page
	this.find("#form_validation_error").css("display","none");
		
	/* validate required form fields */

	this.find(".required select, .required input").each(function() {
		
		if (isEmpty($(this).val())) {
			$(this).addClass("failed");
			failed = true;
		}
		
	});
	
	this.find(".required select, .required input, .not-required select, .not-required input").each(function() {
		
		if ($(this).hasClass("validate-phone") && $(this).val() != "") {
			if(!isValidPhone($(this).val())) {
				$(this).addClass("failed");
				failed = true;
			}
		}
		
		if ($(this).hasClass("validate-email") && $(this).val() != "") {
			if(!isValidEmail($(this).val(), true)) {
				$(this).addClass("failed");
				failed = true;
			}
		}
		
		if ($(this).hasClass("validate-zip") && $(this).val() != "") {
			if(!isValidZipCode($(this).val(), true)) {
				$(this).addClass("failed");
				failed = true;
			}
		}
	});
	
	
	//05.07.2009 - cmpwr : modified so that 2 email addresses are not required to validate a form (ie Request a Brochure form),
	// if only 1 email field exists, pass 'none' or any useless string as the first parameter (ie request a brochure form)
	var isRequestaBrochure = false; //toggle page error based on whether it is RAB or another form page
	var location = window.location.href.split("/");
	if(location[3] == "brochure" || location[4] == "brochure") {
		isRequestaBrochure = true;
	}
	if(!isRequestaBrochure){
		/* check verifications */
		s = p.verify.split(',');
	}
	
	for (i=0; i<s.length; i++) {
		if(s[i+1]) {
			if($(s[i]).val() != $(s[i+1]).val()) {
				$(s[i]).addClass("failed");
				$(s[i+1]).addClass("failed");
				failed = true;
			}
		}
	}
	
	/* trigger failure states */	
	this.find(".required select, .required input").each(function() {
		if($(this).hasClass("failed") && !$(this).hasClass("previously-failed")) {
			$(this).animate({
				backgroundColor: failureColor,
				color: "#fff"
			}, 300);
			$(this).focus(function() {
				$(this).animate({
					backgroundColor: "#fff",
					color: "#000"
				}, 300).removeClass("failed").removeClass("previously-failed").unbind("focus");
			});
		}
		
		if($(this).hasClass("previously-failed") && !$(this).hasClass("failed")) {
			$(this).animate({
				backgroundColor: "#fff",
				color: "#000"
			}, 300);
		}
	});
	
	this.find(".not-required select, .not-required input").each(function() {
		if(!$(this).hasClass("failed")) {
			$(this).animate({
				backgroundColor: "#fff",
				color: "#000"
			}, 300);
		} else {
			$(this).animate({
				backgroundColor: failureColor,
				color: "#fff"
			}, 300);
		}
	});
	
	this.find(".previously-failed").removeClass("previously-failed");

	/* return validation status */

	if(failed) {
		if(!isRequestaBrochure){
			this.append('<p id="page_error">Please correct the highlighted fields</p>');
			$("#page_error").fadeIn("fast");
	}else if(isRequestaBrochure){
		$("#form_validation_error").fadeIn("fast");
	}	
			
		return false;
	} else
		return true;
};
