String.prototype.trim = function(){ return this.replace(/^\s*|\s*$/g,''); }
String.prototype.ltrim = function(){ return this.replace(/^\s*/g,''); }
String.prototype.rtrim = function(){ return this.replace(/\s*$/g,''); }

function isAlpha(p_strValue) {
    //Define Matching expression
    var l_regExp = /^[a-zA-Z\ \'\-\.\,]*$/;
    var l_strValue = p_strValue.trim()
    if (l_strValue.match(l_regExp)) 
        return true; 
    else 
        return false;
}

function isNumeric(p_strNum) {   
    if (p_strNum.match(/^\d+$/)) 
        return true; 
    else 
        return false;
}

function isValidZipCode(p_strZip) {
    //Define Matching expression
    var l_regExp = /(^\d{5}$)|(^\d{5}-\d{4}$)/
    
    if (p_strZip.match(l_regExp)) 
        return true; 
    else 
        return false;
}

function boxchk(obj,max) {
	var box = obj.name.substr(0,obj.name.lastIndexOf('_')+1);
	var cnt=0,i=1;
	while(obj.form[box+i]) {
		cnt += obj.form[box+i].checked;
		i++;
	}
	if (cnt > max) {
		obj.checked = false;
		alert('Only choose ' + max + ' '+box.substr(0,box.length-1) +' checkboxes.\nTo pick this option unselect one of the others.');
	}
}

function validateAge(p_frmAge)
{
	var l_blnInvalid = false;
	var l_numMonth  = p_frmAge.ddlMonth.value;
	var l_numDay    = p_frmAge.ddlDay.value;
	var l_numYear   = p_frmAge.ddlYears.value;
    var l_strWarning = "";

	//Check value of month
	if (eval(l_numMonth) == 0) {
		l_blnInvalid = true;
	}
	//Check value of day
	if (eval(l_numDay) == 0 ) {
        l_blnInvalid = true;
	}
	//Check short months
	if ((l_numMonth == "4" || l_numMonth == "6" || l_numMonth == "9" || l_numMonth == "11") && eval(l_numDay) > 30) {
        l_blnInvalid = true;
	}
	//Check February
	if (l_numMonth == "2") {
		//Leap Year calculations
		if (eval(l_numDay) > 28) {
			if (eval(l_numDay) > 29) {
                l_blnInvalid = true;
			}
			else { //l_numDay == 29
				if (eval(l_numYear) % 4 != 0) {
                    l_blnInvalid = true;
				}
				else { //Year is divisible by 4
					if (eval(l_numYear) % 100 == 0 && eval(l_numYear) % 400 != 0) {
                            l_blnInvalid = true;
					}
				}
			}
	 	}
	}

	//Check value of year
	if (l_numYear == "0") {
        l_blnInvalid = true;
	}

	//Out put error message
	if (l_blnInvalid) {
        return ("Date of birth is invalid");
	}
	else {
		return ('');
	}
}

function validate(pForm) {
    l_strWarning = ""
	
    if(pForm.firstName.value.trim() == '') l_strWarning += "<li><strong>First Name:</strong> Cannot be empty.</li>";
    if(!isAlpha(pForm.firstName.value)) l_strWarning += "<li><strong>First Name:</strong> Must contain alphabetic characters.</li>";

    if(pForm.lastName.value.trim() == '') l_strWarning += "<li><strong>Last Name:</strong> Cannot be empty.</li>";
    if(!isAlpha(pForm.lastName.value)) l_strWarning += "<li><strong>Last Name:</strong> Must contain alphabetic characters.</li>";

    if(pForm.state.value.trim() == '') l_strWarning += "<li><strong>State:</strong> Cannot be empty.</li>";
	
    if (!isValidZipCode(pForm.zipCode.value.trim())) l_strWarning += "<li><strong>Zip code:</strong> Invalid zip code.</li>";
	
	if ((pForm.seasonal_checkbox.checked == false) && (pForm.recipes_checkbox.checked == false) && (pForm.reese_checkbox.checked == false)) {
		l_strWarning += "<li><strong>Newsletter:</strong> Please select a newsletter.</li>";
	}
	
    var l_strEmailWarning = '';
    l_strEmailWarning = emailCheck(pForm.emailAddress.value, l_strEmailWarning);
    if (l_strEmailWarning != "") {
		l_strWarning += "<li><strong>Email:</strong>" + l_strEmailWarning + '.';
	}
	else {
		if (pForm.emailAddress.value != pForm.confirmEmailAddress.value){
			l_strWarning += "<li><strong>Email addresses do not match.</strong>";
		}
	}
    if (l_strWarning != "") {
        document.getElementById("formError").style.display = "block";
        document.getElementById("formError2").style.display = "block";
        document.getElementById("formError").innerHTML= "<strong>The form cannot be submitted because\nthe following fields are invalid/incomplete:</strong><ul>" + l_strWarning + "</ul>";
		document.getElementById("formError2").innerHTML= "The form cannot be submitted, scroll to the top for details.";
        return false;
    } else {
        pForm.submit();
    }	
}
