$(document).ready(function() {

    $("#customform .submit").click(function() {
        validateForm();
        return false;
    });

});

function validateForm() {

    var isvalid = true;

    $('#customform .checkboxrequired').each(function(index) {
        var n = $(this).find("input:checked").length;
        if (n == 0) {
            $(this).find("input").each(function(index) {
                $(this).addClass("bordererror");
            });
            $(this).parent().find('.error3').html('Required Field');
            $(this).parent().find('.error3').fadeIn(50);
            isvalid = false;
        } else {
            $(this).find("input").each(function(index) {
                $(this).removeClass("bordererror");
            });
            $(this).parent().find('.error3').html('');
            $(this).parent().find('.error3').hide();
        }
    });

    $('#customform .required').each(function(index) {
    if ($(this).val() == '' || $(this).val() == null) {
            $(this).addClass("bordererror");
            $(this).parent().find('.error3').html('Required Field');
            $(this).parent().find('.error3').fadeIn(50);
            isvalid = false;
        } else {
            $(this).removeClass("bordererror");
            $(this).parent().find('.error3').html('');
            $(this).parent().find('.error3').hide();
        }
    });

    $('#customform .validation_email').each(function(index) {
        if ($(this).val() != '') {
            if (isValidEmailAddress($(this).val()) == false) {
                $(this).addClass("bordererror");
                $(this).parent().find('.error3').html('Invalid E-mail Address');
                $(this).parent().find('.error3').fadeIn(50);
                isvalid = false;
            } else {
                $(this).removeClass("bordererror");
                $(this).parent().find('.error3').html('');
                $(this).parent().find('.error3').hide();
            }
        }

    });

    $('#customform .validation_numeric').each(function(index) {
        if (isValidNumeric($(this).val()) == false) {
            $(this).addClass("bordererror");
            $(this).parent().find('.error3').html('This field must be numeric');
            $(this).parent().find('.error3').fadeIn(50);
            isvalid = false;
        } else {
            $(this).removeClass("bordererror");
            $(this).parent().find('.error3').html('');
            $(this).parent().find('.error3').hide();
        }
    });

    $('#customform .validation_ddmmyyyy').each(function(index) {
        if (isValidDate($(this).val()) == false) {
            $(this).addClass("bordererror");
            $(this).parent().find('.error3').html('This field must be of the format dd/mm/yyyy');
            $(this).parent().find('.error3').fadeIn(50);
            isvalid = false;
        } else {
            $(this).removeClass("bordererror");
            $(this).parent().find('.error3').html('');
            $(this).parent().find('.error3').hide();
        }
    });

    if (isvalid == true) {
        validateRecaptcha();
    }

}

function sendEmail() {

    var str = "";

    $('#customform [id^=cmsform_]').each(function(index) {
    if ($(this).attr('type') == 'checkbox' || $(this).attr('type') == 'radio') {
            if ($(this).attr("checked") == true) {
                str += $(this).attr('name') + '=' + $(this).val() + '&'
            };
        } else {
            str += $(this).attr('name') + '=' + $(this).val() + '&'
        }
    });
    
    str = 'formid=' + $('#formid').val() + '&' + str

    $("#customform").hide();
    $(".formloading").fadeIn(200);

    //alert(str);

    $.post("/ajax/customform.aspx", str, function(data) {
        $(".formloading").hide();
        $("#customform").html('<p class="success">Thankyou, your form has been sent successfully</p>');
        $("#customform").show();
    });

}

function isValidEmailAddress(value) {
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(value);
}

function isValidNumeric(value) {
    var pattern = new RegExp('^[0-9]+$');
    return pattern.test(value);
}

function isValidDate(value) {
    //var pattern = new RegExp('(((0[1-9]|[12][0-9]|3[01])([-./])(0[13578]|10|12)([-./])(\d{4}))|(([0][1-9]|[12][0-9]|30)([-./])(0[469]|11)([-./])(\d{4}))|((0[1-9]|1[0-9]|2[0-8])([-./])(02)([-./])(\d{4}))|((29)(\.|-|\/)(02)([-./])([02468][048]00))|((29)([-./])(02)([-./])([13579][26]00))|((29)([-./])(02)([-./])([0-9][0-9][0][48]))|((29)([-./])(02)([-./])([0-9][0-9][2468][048]))|((29)([-./])(02)([-./])([0-9][0-9][13579][26])))');
    //return pattern.test(value);
    return true;
}

function validateRecaptcha() {

    var str = ""

    str = $("#recaptcha_challenge_field").attr('id') + '=' + $("#recaptcha_challenge_field").val()
    str += '&' + $("#recaptcha_response_field").attr('id') + '=' + $("#recaptcha_response_field").val()

    $.post("/ajax/recaptchavalidate.aspx", str, function(data) {

        if (data == 'correct code') {
            $("#recaptcha_response_field").removeClass("bordererror");
            $("#recaptchaquestion").find('.captchaerror').html('');
            $("#recaptchaquestion").find('.captchaerror').hide();
            sendEmail();
        } else {
            $("#recaptcha_response_field").addClass("bordererror");
            $("#recaptchaquestion").find('.captchaerror').html('Incorrect code');
            $("#recaptchaquestion").find('.captchaerror').show();
            Recaptcha.reload();
        }

    });
    
}
