function init(pErrorMsg) {
    if (pErrorMsg != "") {
        document.getElementById("formError").style.display = "block";
        document.getElementById("formError").innerHTML = pErrorMsg;
    } else {
        document.getElementById("formError").style.display = "none";
        document.getElementById("formError2").style.display = "none";
    }
}

function updateAddress(obj) {
    val = obj.options[obj.selectedIndex].value;
    if (val == "") return;
    // switch values must match the ddlCountry element in /contactus/contact-form.asp!
    switch(val) {
        case "United States of America" : swapRows('USAddress','intAddress');hideRows('CanAddress'); break;
        case "Canada"                   : swapRows('CanAddress','USAddress');hideRows('intAddress'); break;
        default                         : swapRows('intAddress','USAddress');hideRows('CanAddress'); break;
    }
}

function validate(pForm) {
    l_strWarning = ""

    if (pForm.ddlSubject.options.selectedIndex < 1) l_strWarning += "<li><strong>Subject:</strong> Must be selected.</li>";

    var l_strEmailWarning = '';
    l_strEmailWarning = emailCheck(pForm.txtEmail.value, l_strEmailWarning);
    if (l_strEmailWarning != "") l_strWarning += "<li><strong>Email:</strong>" + l_strEmailWarning + '.';

    var l_strComment = pForm.txtComments.value.trim()
    if(l_strComment == '') l_strWarning += "<li><strong>Comments:</strong> Cannot be empty.</li>";
    if(l_strComment.length > 4000) l_strWarning += "<li><strong>Comments:</strong> Cannot be more than 4000 characters.</li>";

    if(pForm.txtFirstName.value.trim() == '') l_strWarning += "<li><strong>First Name:</strong> Cannot be empty.</li>";
    if(!isAlpha(pForm.txtFirstName.value)) l_strWarning += "<li><strong>First Name:</strong> Must contain alphabetic characters.</li>";

    if(pForm.txtLastName.value.trim() == '') l_strWarning += "<li><strong>Last Name:</strong> Cannot be empty.</li>";
    if(!isAlpha(pForm.txtLastName.value)) l_strWarning += "<li><strong>Last Name:</strong> Must contain alphabetic characters.</li>";

    if(pForm.txtAddress1.value.trim() == '') l_strWarning += "<li><strong>Street Address:</strong> Cannot be empty.</li>";

    switch(pForm.ddlCountry.options[pForm.ddlCountry.options.selectedIndex].value) {
        case "United States of America" :
            if (pForm.txtCity.value.trim() == "") l_strWarning += "<li><strong>City:</strong> Cannot be empty.</li>";
            if (pForm.ddlState.options.selectedIndex < 1) l_strWarning += "<li><strong>State:</strong> Must Be selected.</li>";
            if (!isValidZipCode(pForm.txtZip.value.trim())) l_strWarning += "<li><strong>Zip code:</strong> Invalid zip code.</li>";
        break;
        case "Canada" :
            if (pForm.txtCanCity.value.trim() == "") l_strWarning += "<li><strong>City:</strong> Cannot be empty.</li>";
            if (pForm.ddlProvince.options.selectedIndex < 1) l_strWarning += "<li><strong>Province:</strong> Must Be selected.</li>";
            if (pForm.txtCanZip.value.trim() == "") l_strWarning += "<li><strong>Postal Code:</strong> Cannot be empty.</li>";
        break;
        default :
            if (pForm.txtIntCity.value.trim() == "") l_strWarning += "<li><strong>City:</strong> Cannot be empty.</li>";
            if (pForm.ddlCountry.options.selectedIndex < 1) l_strWarning += "<li><strong>Country:</strong> Must Be selected.</li>";
            if (pForm.txtIntZip.value.trim() == "") l_strWarning += "<li><strong>Postal Code:</strong> Cannot be empty.</li>";
        break;
    }

	var l_strCheckDate = isDate(pForm.txtPurchaseDate.value)
    if (l_strCheckDate != "") l_strWarning += "<li><strong>Start Date:</strong>" + l_strCheckDate;

    if (l_strWarning != "") {
        document.getElementById("formError2").style.display = "block";
        document.getElementById("formError").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>";
        return false;
    } else {
        pForm.submit();
    }
}

//======================== Show/Hide/Swap Table Rows ========================
//--- Hide Table Rows ---
function hideRows(pRowsToHide) {
    var l_elmRow = document.getElementById(pRowsToHide);
    //alert('hideRows:'+pRowsToHide+">"+l_elmRow.style.display);
    l_elmRow.style.display = "none";
}
//------

//--- Swap Show/Hide Table Rows ---
function swapRows(pRowsToShow,pRowsToHide) {
    //Display rows
    var l_elmRow = document.getElementById(pRowsToShow);
    //alert('swapRows-Display:'+pRowsToShow+">"+l_elmRow.style.display);
    if (document.all)
        l_elmRow.style.display = "inline";
    else
        l_elmRow.style.display = "table-row";
    //Hide Rows
    l_elmRow = document.getElementById(pRowsToHide);
    //alert('hideRows-Display:'+pRowsToHide+">"+l_elmRow.style.display);
    l_elmRow.style.display = "none";
}
//------

//===========================================================================

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year

var dtCh= "/";
var minYear=0;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
}

function isDate(dtStr){
	if (dtStr == "") return ""
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		return "The date format should be : mm/dd/yyyy"
	}
	if (strMonth.length<1 || month<1 || month>12){
		return "Please enter a valid month"
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return "Please enter a valid day"
	}
	if (strYear.length < 2 || year==0 || year<minYear || year>maxYear){
		return "Please enter a valid 4 digit year between "+minYear+" and "+maxYear
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return "Please enter a valid date"
	}
return ""
}

function ValidateForm(){
	var dt=document.frmSample.txtDate
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
    return true
 }