var whitespace = " \t\n\r";

function isEmail( string ) {
    if ( string.search(/^(\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+(\,|\, )?)*$/) != -1 ) {
        return true;
    } else {
        return false;
    }
}

function isZip( zip ) {
    // 5 digit zips
    if( zip.match(/^\d\d\d\d\d$/) )
        return true;
    // 5+4 digit zips
    if( ( zip.match(/^\d\d\d\d\d\d\d\d\d$/) ) || ( zip.match(/^\d\d\d\d\d\-\d\d\d\d$/) ) )
        return true;
    return false;
}

function isPostalCode( pc ) {
    // canadian postal codes (6 or 7 characters)
    if( ( pc.match(/^\w\d\w\d\w\d$/) ) || ( pc.match(/^\w\d\w \d\w\d$/) ) )
        return true;
    return false;
}

function isEmpty(s){
    return ((s == null) || (s.length == 0))
}

function isWhitespace (s) {
    var i;
    if (isEmpty(s)) return true;
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return false;
    }
    return true;
}

function stripWhitespace (s) {
    if (isEmpty(s)) return "";
    var i;
    var returnString = "";
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function blnHasScript (inString) {
    var tmpString = stripWhitespace(inString).toUpperCase();
    if (tmpString.indexOf("<" + "SCRIPT") != -1) return true;
    if (tmpString.indexOf("<" + "/" + "SCRIPT") != -1) return true;
    if (tmpString.indexOf("<" + "?" + "php") != -1) return true;
    if (tmpString.indexOf("?" + ">") != -1) return true;
    if (tmpString.indexOf("language=") != -1) return true;
    return false;
}


// form specific code =================================================


function jsValidateSendToFriend(theForm) {
        if ( isWhitespace(theForm.txtRecipientName.value) ) {
            alert("The recipient's name is a required field");
            theForm.txtRecipientName.focus();
            return false;
        }
        if (isWhitespace(theForm.txtRecipientEmail.value)) {
            alert("The recipient's email address is a required field");
            theForm.txtRecipientEmail.focus();
            return false;
        }
        if (isWhitespace(theForm.txtSenderName.value)) {
            alert("Your name is a required field");
            theForm.txtRecipientName.focus();
            return false;
        }
        if (isWhitespace(theForm.txtSenderEmail.value)) {
            alert("Your email address is a required field");
            theForm.txtRecipientEmail.focus();
            return false;
        }

        if ( !isEmail(theForm.txtRecipientEmail.value) ){
            alert("The recipient's email address is not a valid email address.");
            theForm.txtRecipientEmail.focus();
            return false;
        }
        if ( !isEmail(theForm.txtSenderEmail.value) ){
            alert("Your email address is not a valid email address.");
            theForm.txtSenderEmail.focus();
            return false;        
        }        

        if ( theForm.txtComments.value.length > 500  ){
            alert("Comments cannot exceed 500 characters.");
            theForm.txtComments.focus();
            return false;
        }
        
        if (blnHasScript(theForm.txtRecipientName.value) || blnHasScript(theForm.txtRecipientEmail.value) || 
            blnHasScript(theForm.txtSenderName.value) || blnHasScript(theForm.txtSenderEmail.value)) {
            alert("Your form contains invalid script tags");
            theForm.txtRecipientName.focus();
            return false;
        }

        return true;
}

function jsValidateEmailOptIn(theForm) {
        if (isWhitespace(theForm.txtEmail.value)) {
            alert("Email address is a required field");
            theForm.txtEmail.focus();
            return false;
        }

        if ( !isEmail(theForm.txtEmail.value) ){
            alert("The email address is not valid.");
            theForm.txtEmail.focus();
            return false;
        }

        if ( blnHasScript(theForm.txtEmail.value) || blnHasScript(theForm.txtFirstname.value) || blnHasScript(theForm.txtLastname.value) ) {
            alert("Your form contains invalid script tags");
            theForm.txtEmail.focus();
            return false;
        }

        return true;
}

function jsValidateContactUs(theForm) {
        if (isWhitespace(theForm.txtName.value)) {
            alert("Name is a required field");
            theForm.txtName.focus();
            return false;
        }
        if (isWhitespace(theForm.txtEmail.value)) {
            alert("Email address is a required field");
            theForm.txtEmail.focus();
            return false;
        }
        if (isWhitespace(theForm.selSubject.value)) {
            alert("Subject is a required field");
            theForm.selSubject.focus();
            return false;
        }
        
        if ( !isEmail(theForm.txtEmail.value) ){
            alert("The email address is not valid.");
            theForm.txtEmail.focus();
            return false;
        }

        if ( theForm.txtComments.value.length > 1000  ){
            alert("Comments cannot exceed 1000 characters.");
            theForm.txtComments.focus();
            return false;
        }

        if ( blnHasScript(theForm.txtName.value) || blnHasScript(theForm.txtEmail.value) || blnHasScript(theForm.txtComments.value) ) {
            alert("Your form contains invalid script tags");
            theForm.txtEmail.focus();
            return false;
        }

        return true;
}

function jsValidateSweep(theForm) {
        if (isWhitespace(theForm.txtLastName.value)) {
            alert("Name is a required field");
            theForm.txtLastName.focus();
            return false;
        }
        if (isWhitespace(theForm.txtFirstName.value)) {
            alert("Name is a required field");
            theForm.txtFirstName.focus();
            return false;
        }
        if (isWhitespace(theForm.txtEmail.value)) {
            alert("Email address is a required field");
            theForm.txtEmail.focus();
            return false;
        }
        if ( !isEmail(theForm.txtEmail.value) ){
            alert("The email address is not valid.");
            theForm.txtEmail.focus();
            return false;
        }
        if (isWhitespace(theForm.txtYear.value)
            || isWhitespace(theForm.txtMonth.value)
            || isWhitespace(theForm.txtDay.value)) {
            alert("Date of Birth is a required field");
            theForm.txtYear.focus();
            return false;
        }
        if (blnHasScript(theForm.txtLastName.value)
            || blnHasScript(theForm.txtFirstName.value)
            || blnHasScript(theForm.txtAddress.value)
            || blnHasScript(theForm.txtCity.value)
            || blnHasScript(theForm.txtState.value)
            || blnHasScript(theForm.txtZip.value)
            || blnHasScript(theForm.txtYear.value)
            || blnHasScript(theForm.txtMonth.value)
            || blnHasScript(theForm.txtDay.value)
            || blnHasScript(theForm.txtEmail.value)
            || blnHasScript(theForm.txtPhone.value)
            || blnHasScript(theForm.newsletter.value) ) {
            alert("Your form contains invalid script tags");
            theForm.txtFirstName.focus();
            return false;
        }

        return true;
}

function jsValidateSweepFr(theForm) {
        if (isWhitespace(theForm.txtLastName.value)) {
            alert("Le nom est un champ obligatoire");
            theForm.txtLastName.focus();
            return false;
        }
        if (isWhitespace(theForm.txtFirstName.value)) {
            alert("Le nom est un champ obligatoire");
            theForm.txtFirstName.focus();
            return false;
        }
        if (isWhitespace(theForm.txtEmail.value)) {
            alert("L'adresse email est un champ obligatoire");
            theForm.txtEmail.focus();
            return false;
        }
        if ( !isEmail(theForm.txtEmail.value) ){
            alert("L'adresse e-mail n'est pas valide");
            theForm.txtEmail.focus();
            return false;
        }
        if (isWhitespace(theForm.txtYear.value)
            || isWhitespace(theForm.txtMonth.value)
            || isWhitespace(theForm.txtDay.value)) {
            alert("La date de naissance est un champ obligatoire");
            theForm.txtYear.focus();
            return false;
        }
        if (blnHasScript(theForm.txtLastName.value)
            || blnHasScript(theForm.txtFirstName.value)
            || blnHasScript(theForm.txtAddress.value)
            || blnHasScript(theForm.txtCity.value)
            || blnHasScript(theForm.txtState.value)
            || blnHasScript(theForm.txtZip.value)
            || blnHasScript(theForm.txtYear.value)
            || blnHasScript(theForm.txtMonth.value)
            || blnHasScript(theForm.txtDay.value)
            || blnHasScript(theForm.txtEmail.value)
            || blnHasScript(theForm.txtPhone.value)
            || blnHasScript(theForm.newsletter.value) ) {
            alert("Le concours de cette année est maintenant terminé");
            theForm.txtFirstName.focus();
            return false;
        }

        return true;
}

