//
//Validates the form before submission
//
function ValidateForm() {

    var formValid = true;

    jQuery(":input[rel='ValidateField']").each(function () {
        if (jQuery(this).val() == "") {
            jQuery(this).css("background-color", "#5EB6FF");
            formValid = false;
        }
        else
            jQuery(this).css("background-color", "white");
    });

    return formValid;
}

//
//Posts the form submission to the handler
//
function SendContactRequest() {

    if (ValidateForm()) {
        var emailaddress = jQuery("#email").val();

        if (!validateEmail(emailaddress)) {
            alert("Please enter a valid email address");
            return;
        }

        jQuery.post("/formhandler/handlers/contact.ashx",
            {
                name: jQuery("#name").val(),
                email: jQuery("#email").val(),
                company: jQuery("#company").val(),
                telephone: jQuery("#telephone").val(),
                message: jQuery("#message").val(),
                product: window.location.href,
                captchaid: jQuery("#captchaid").val(),
                captchavalue: jQuery("#captchavalue").val()
            },
            function (retval) {
                if (retval == "OK") {
                    //alert("Thank you. Your contact request has been sent.");
                    window.location.href = "/en/15219.aspx";
                }
                else {
                    alert("Sorry, your contact request could not be sent.\n\n" + retval);
                }
            },
            "text");
    }
}

//
//Validates a date in UK format
//
function validateUKDate(datestring) {
    if (datestring.length == 0) {
        return true;
    }

    var matchPat = /^(?:(?:0?[1-9]|1\d|2[0-8])(\/)(?:0?[1-9]|1[0-2]))(\/)(?:[1-9]\d\d\d|\d[1-9]\d\d|\d\d[1-9]\d|\d\d\d[1-9])$|^(?:(?:31(\/)(?:0?[13578]|1[02]))|(?:(?:29|30)(\/|-)(?:0?[1,3-9]|1[0-2])))(\/|-)(?:[1-9]\d\d\d|\d[1-9]\d\d|\d\d[1-9]\d|\d\d\d[1-9])$|^(29(\/|-)0?2)(\/|-)(?:(?:0[48]00|[13579][26]00|[2468][048]00)|(?:\d\d)?(?:0[48]|[2468][048]|[13579][26]))$/;
    var matchArr = datestring.match(matchPat);
    if (matchArr == null) {
        return false;
    }
    else {
        return true;
    }
}

//
//Validates an email address
//
function validateEmail(email) {
    if (email.length == 0) {
        return true;
    }

    var matchPat = /(\w[-._\w]*\w@\w[-._\w]*\w\.\w{2,3})/;
    var matchArr = email.match(matchPat);
    if (matchArr == null) {
        return false;
    }
    else {
        return true;
    }
}

//
//Validates an integer
//
function validateInteger(textstring) {
    if (textstring.length == 0) {
        return true;
    }

    var matchPat = /^\d*$/;
    var matchArr = textstring.match(matchPat);
    if (matchArr == null) {
        return false;
    }
    else {
        return true;
    }
}

//
//Validates a decimal
//
function validateDecimal(textstring) {
    if (textstring.length == 0) {
        return true;
    }

    var matchPat = /^-?\d*(\.\d+)?$/;
    var matchArr = textstring.match(matchPat);
    if (matchArr == null) {
        return false;
    }
    else {
        return true;
    }
}

//
//Validates a postcode
//
function validatePostcode(postcode) {
    if (postcode.length == 0) {
        return true;
    }

    var matchPat = /^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) {0,1}[0-9][A-Za-z]{2})$/;
    var matchArr = postcode.match(matchPat);
    if (matchArr == null) {
        return false;
    }
    else {
        return true;
    }
}

//
//Validates a password (7-20 characters mixed numbers and letters)
//
function validatePassword(passwordstring) {
    if (passwordstring.length == 0) {
        return true;
    }

    var matchPat = /(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{7,20})$/;
    var matchArr = passwordstring.match(matchPat);
    if (matchArr == null) {
        return false;
    }
    else {
        return true;
    }
}

//
//Validates a credit card no
//
function validateCreditCardNo(number) {
    if (number.length == 0) {
        return true;
    }
    var matchPat = /^([0-9]{12,19})$/;
    var matchArr = number.match(matchPat);
    if (matchArr == null) {
        return false;
    }
    else {
        return true;
    }
}

//
//Validates a credit card CVV
//
function validateCreditCardCVV(number) {
    if (number.length == 0) {
        return true;
    }
    var matchPat = /^([0-9]{3,4})$/;
    var matchArr = number.match(matchPat);
    if (matchArr == null) {
        return false;
    }
    else {
        return true;
    }
}

//
//Validates a credit card expiry 
//
function validateCreditCardExpiry(number) {
    if (number.length == 0) {
        return true;
    }

    var matchPat = /^((0[1-9])|(1[0-2]))*((0[8-9])|(1[0-9]))$/;
    var matchArr = number.match(matchPat);
    if (matchArr == null) {
        return false;
    }
    else {
        return true;
    }
} 


