/* 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){

		p_email = trimString( p_email );

		if (mustValidate) {
			
			if (p_email.length > 0) {
				if (p_email.length < 6) return false;
				if (p_email.indexOf(",") != -1) return false;
	
				if (p_email.indexOf(".") == -1) {
					return false;
				}
		
				if (p_email.indexOf("@") == -1 || p_email.indexOf("@@") != -1) {
					return false;
				}
			
				// there must be a Ô.Õ after the Ô@Õ.
				if (p_email.lastIndexOf(".") < p_email.indexOf("@")) return false;
		
				// the last instance of Ô.Õ in the string must be followed by at least two characters. Note that because a string's index begins at zero, a string with one character would have that character indexed at the zero position. To allow for this inconsistency we add 3, not 2.
				if ( (p_email.lastIndexOf(".")+3) > p_email.length ) return false;
				
				// <mteguh add>
				if ( p_email.indexOf("@") != p_email.lastIndexOf("@") ) return false;
				
			}
			return true;
		} else {
			
			if (p_email == null) return false;
			
			if (p_email == "") return false;
		
			//the shortest p_email address I can think of is a@a.aa which is six characters long, anything less must be invalid.
			if (p_email.length < 6) return false;
		
			//Commas are invalid
			if (p_email.indexOf(",") != -1) return false;
	
			if (p_email.indexOf(".") == -1) {
				return false;
			}
	
			if (p_email.indexOf("@") == -1 || p_email.indexOf("@@") != -1) {
				return false;
			}
	
			// the last instance of Ô.Õ in the string must be followed by at least two characters. Note that because a string's index begins at zero, a string with one character would have that character indexed at the zero position. To allow for this inconsistency we add 3, not 2.
			if ( (p_email.lastIndexOf(".")+3) > p_email.length ) return false;
			
			// <mteguh add>
			if ( p_email.indexOf("@") != p_email.lastIndexOf("@") ) 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();
		
	/* validate required form fields */

	this.find(".required select, .required input, .required textarea").each(function() {
		
		if ((jQuery(this).val() == "") 
		||  (jQuery(this).hasClass("toggleDefault") && (jQuery(this).val() == this.defaultValue))) 
		{
			jQuery(this).addClass("failed");
			failed = true;
		}
		
	});
	
	this.find(".required select, .required input, .required textarea, .not-required select, .not-required input, .not-required textarea").each(function() {
		
		if (jQuery(this).hasClass("validate-phone") && jQuery(this).val() != "") {
			if(!isValidPhone(jQuery(this).val())) {
				jQuery(this).addClass("failed");
				failed = true;
			}
		}
		
		if (jQuery(this).hasClass("validate-email") && jQuery(this).val() != "") {
			if(!isValidEmail(jQuery(this).val(), true)) {
				jQuery(this).addClass("failed");
				failed = true;
			}
		}
		
		if (jQuery(this).hasClass("validate-zip") && jQuery(this).val() != "") {
			if(!isValidZipCode(jQuery(this).val(), true)) {
				jQuery(this).addClass("failed");
				failed = true;
			}
		}
	});
	
	/* check verifications
	s = p.verify.split(',');
	
	for (i=0; i<s.length; i++) {
		if(s[i+1]) {
			if(jQuery(s[i]).val() != jQuery(s[i+1]).val()) {
				jQuery(s[i]).addClass("failed");
				jQuery(s[i+1]).addClass("failed");
				failed = true;
			}
		}
	}
	*/
	
	/* trigger failure states */	
	this.find(".required select, .required input, .required textarea").each(function() {
		if(jQuery(this).hasClass("failed") && !jQuery(this).hasClass("previously-failed")) {
			jQuery(this).animate({
				backgroundColor: failureColor,
				color: "#fff"
			}, 300);
			jQuery(this).focus(function() {
				jQuery(this).animate({
					backgroundColor: "#fff",
					color: "#000"
				}, 300).removeClass("failed").removeClass("previously-failed").unbind("focus");
			});
		}
		
		if(jQuery(this).hasClass("previously-failed") && !jQuery(this).hasClass("failed")) {
			jQuery(this).animate({
				backgroundColor: "#fff",
				color: "#000"
			}, 300);
		}
	});
	
	this.find(".not-required select, .not-required input, .not-required textarea").each(function() {
		if(!jQuery(this).hasClass("failed")) {
			jQuery(this).animate({
				backgroundColor: "#fff",
				color: "#000"
			}, 300);
		} else {
			jQuery(this).animate({
				backgroundColor: failureColor,
				color: "#fff"
			}, 300);
		}
	});
	
	this.find(".previously-failed").removeClass("previously-failed");

	/* return validation status */

	if(failed) {
	    this.find(".checkout-status-error").removeClass("checkout-status-error");
		//this.append('<p id="page_error">Please correct the highlighted fields</p>');
		jQuery("#checkout-submit").append('<p id="page_error">Please correct the highlighted fields</p>');
		jQuery("#page_error").fadeIn("fast");
		return false;
	} else
		return true;
};
