﻿function validateCaptcha() {

    challengeFieldCheck = $("input#recaptcha_challenge_field");

    if ($(challengeFieldCheck).length > 0) {
        challengeField = $("input#recaptcha_challenge_field").val();
        responseField = $("input#recaptcha_response_field").val();



        var html = $.ajax({
            type: "POST",
            url: "controls/recaptcha.ashx",
            data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField,
            async: false
        }).responseText;
  
        if (html.replace(/^\s+|\s+$/, '') == "success") {
            $("#captchaStatus").html(" ");
            // Uncomment the following line in your application
            return true;
        }
        else {
            $("#captchaStatus").html("Your captcha is incorrect. Please try again");
            Recaptcha.reload();
            return false;
        }
    }
    else {
        //something went wrong with loading the captcha.  let them through.
        return true;
    }
}


$(document).ready(function () {

    $("form").each(function (i) {
        var required = $(this).find("input[name='required']");
        //var captcha = $(this).find("input#recaptcha_challenge_field");
        var captcha = $(this).find("#recaptcha_div");
        if (required) {

            if ($(captcha).length > 0) {
                $(this).validate({ submitHandler: function (form) {
                    if (validateCaptcha()) {
                        // Submit form 
                        form.submit();
                    }
                }
                });

                Recaptcha.create("6LdNybkSAAAAALxEEaKikzeC6OuVJCOaQXGES7aU",
                "recaptcha_div",
                {
                    theme: "white",
                    callback: Recaptcha.focus_response_field
                }
              );
            }
            else {
                $(this).validate();
            }

            var fields = $(required).val().split('|');

            for (var i = 0; i < fields.length; i++) {
                var obj = $("[name='" + fields[i] + "']");
                obj.addClass('required');
            }

        }
    });






    $.validator.addMethod("phone", function (ph, element) {
        if (ph == null) {
            return false;
        }
        var stripped = ph.replace(/[\s()+-]|ext\.?/gi, "");
        // 10 is the minimum number of numbers required
        return ((/\d{10,}/i).test(stripped));
    }, "Please enter a valid phone number");

    $.validator.addMethod("postalcode", function (postalcode, element) {
        return this.optional(element) || postalcode.match(/(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXYabceghjklmnpstvxy]{1}\d{1}[A-Za-z]{1} ?\d{1}[A-Za-z]{1}\d{1})$/);
    }, "Please specify a valid postal/zip code");


    $.validator.addMethod("truedate", function (ph, element) {
        if (ph == null) {
            return false;
        }
        var valid = false;
        var d = Date.parseExact(ph, "MM/dd/yyyy");
        var d2 = Date.parseExact(ph, "M/dd/yyyy");
        var d3 = Date.parseExact(ph, "M/d/yyyy");
        var d4 = Date.parseExact(ph, "MM/d/yyyy");

        if (d !== null || d2 !== null || d3 !== null || d4 !== null) {
            valid = true;
        }
        return valid;

    }, "Please enter a valid date");

    $.validator.addMethod("creditcard", function (value, element) {
        if (this.optional(element))
            return "dependency-mismatch";
        // accept only digits and dashes
        value = value.replace(/ /g, '');
        if (/[^0-9-]+/.test(value))
            return false;
        var nCheck = 0,
				nDigit = 0,
				bEven = false;

        value = value.replace(/\D/g, "");

        for (n = value.length - 1; n >= 0; n--) {
            var cDigit = value.charAt(n);
            var nDigit = parseInt(cDigit, 10);
            if (bEven) {
                if ((nDigit *= 2) > 9)
                    nDigit -= 9;
            }
            nCheck += nDigit;
            bEven = !bEven;
        }

        return (nCheck % 10) == 0;
    }, "Please enter a valid credit card.");



});


