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>Objet:</strong> Doit &ecirc;tre s&eacute;lectionn&eacute;.</li>";

    var l_strEmailWarning = '';
    l_strEmailWarning = emailCheck(pForm.txtEmail.value, l_strEmailWarning);
    if (l_strEmailWarning != "") l_strWarning += "<li><strong>Adresse &eacute;lectronique:</strong>" + l_strEmailWarning + '.';

    var l_strComment = pForm.txtComments.value.trim()
    if(l_strComment == '') l_strWarning += "<li><strong>Commentaires:</strong> Ce champ ne peut pas &ecirc;tre vide.</li>";
    if(l_strComment.length > 4000) l_strWarning += "<li><strong>Commentaires:</strong> Ne peut contenir plus de 4 000 caract&egrave;res.</li>";

    if(pForm.txtFirstName.value.trim() == '') l_strWarning += "<li><strong>Pr&eacute;nom:</strong> Ce champ ne peut pas &ecirc;tre vide.</li>";
    if(!isAlpha(pForm.txtFirstName.value)) l_strWarning += "<li><strong>Pr&eacute;nom:</strong> Doit contenir des caract&egrave;res alphanum&eacute;riques.</li>";

    if(pForm.txtLastName.value.trim() == '') l_strWarning += "<li><strong>Nom:</strong> Ce champ ne peut pas &ecirc;tre vide.</li>";
    if(!isAlpha(pForm.txtLastName.value)) l_strWarning += "<li><strong>Nom:</strong> Doit contenir des caract&egrave;res alphanum&eacute;riques.</li>";

    if(pForm.txtAddress1.value.trim() == '') l_strWarning += "<li><strong>Adresse de voirie:</strong> Ce champ ne peut pas &ecirc;tre vide.</li>";

    switch(pForm.ddlCountry.options[pForm.ddlCountry.options.selectedIndex].value) {
        case "United States of America" :
            if (pForm.txtCity.value.trim() == "") l_strWarning += "<li><strong>Ville:</strong> Ce champ ne peut pas &ecirc;tre vide.</li>";
            if (pForm.ddlState.options.selectedIndex < 1) l_strWarning += "<li><strong>Pays:</strong> Doit &ecirc;tre s&eacute;lectionn&eacute;.</li>";
            if (!isValidZipCode(pForm.txtZip.value.trim())) l_strWarning += "<li><strong>Code postal:</strong> Code postal invalide.</li>";
        break;
        case "Canada" :
            if (pForm.txtCanCity.value.trim() == "") l_strWarning += "<li><strong>Ville:</strong> Ce champ ne peut pas &ecirc;tre vide.</li>";
            if (pForm.ddlProvince.options.selectedIndex < 1) l_strWarning += "<li><strong>Province:</strong> Doit &ecirc;tre s&eacute;lectionn&eacute;.</li>";
            if (pForm.txtCanZip.value.trim() == "") l_strWarning += "<li><strong>Code postal:</strong> Ce champ ne peut pas &ecirc;tre vide.</li>";
        break;
        default :
            if (pForm.txtIntCity.value.trim() == "") l_strWarning += "<li><strong>Ville:</strong> Ce champ ne peut pas &ecirc;tre vide.</li>";
            if (pForm.ddlCountry.options.selectedIndex < 1) l_strWarning += "<li><strong>Pays:</strong> Doit &ecirc;tre s&eacute;lectionn&eacute;.</li>";
            if (pForm.txtIntZip.value.trim() == "") l_strWarning += "<li><strong>Code postal:</strong> Ce champ ne peut pas &ecirc;tre vide.</li>";
        break;
    }

	var l_strCheckDate = isDate(pForm.txtPurchaseDate.value)
    if (l_strCheckDate != "") l_strWarning += "<li><strong>Date de d&eacute;but:</strong>" + l_strCheckDate;

    if (l_strWarning != "") {
        document.getElementById("formError2").style.display = "block";
        document.getElementById("formError").style.display = "block";
        document.getElementById("formError").innerHTML= "<strong>Le formulaire ne peut pas &ecirc;tre soumis\n parce que les donn&eacute;es des champs suivants\n sont incompl&egrave;tes ou invalides:</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 "Le format de la date doit &ecirc;tre : mm/jj/aaaa"
	}
	if (strMonth.length<1 || month<1 || month>12){
		return "Veuillez entrer un mois valide"
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return "Veuillez entrer un jour valide"
	}
	if (strYear.length < 2 || year==0 || year<minYear || year>maxYear){
		return "Veuillez entrer une ann&eacute;e &agrave; quatre chiffres valide entre "+minYear+" et "+maxYear
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return "Veuillez entrer une date valide"
	}
return ""
}

function ValidateForm(){
	var dt=document.frmSample.txtDate
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
    return true
 }
