﻿$(document).ready(function() {
    //validation rules
    $.validator.addMethod('areaRequired', function(value) {
        return $("#ulArea input:checked").length > 0;
    }, 'Please select at least 1 checkbox');
    $.validator.addMethod('heardRequired', function(value) {
        return $("#ulHeard input:checked").length > 0;
    }, 'Please select at least 1 checkbox');
    $.validator.addMethod('phoneAUS',
        function(value) {
            if (value.toString().length == 0) return true;
            var regE = new RegExp('(^1300\d{6}$)|(^1800|1900|1902\d{6}$)|(^0[2|3|4|7|8]{1}[0-9]{8}$)|(^13\d{4}$)|(^04\d{2,3}\d{6}$)');
            return regE.test(value);
        },
   'Enter valid telephone number including area code');
   $.validator.addMethod('phoneNZ',
        function(value) {
            if (value.toString().length == 0) return true;
            var regE = new RegExp('^[0-9]{9,11}$');
            return regE.test(value);
        },
   'Enter valid telephone number');

    $("#ulArea input").click(function() {
        $("#iArea").valid();
    });
    $("#ulHeard input").click(function() {
        $("#iHeard").valid();
    });
    $('input:name(rblConsultant)').click(function() {
        SetupConsultant();
    });
    SetupConsultant();
    $('input:name(rblCustomerType)').click(function() {
        SetupCustomer();
    });
    SetupCustomer();
    $('select:id(ddlCountry)').change(function() {
        SetupOtherCountry();
    });
    SetupOtherCountry();
    $('input:id(cbArea)').click(function() {
        SetupComments();
    });
    SetupComments();

    $("input:id(tbPhone)").keyfilter(/[\d\-+]/);

    //form validation
    $("#aspnetForm").validate({
        ignore: '.no-validation',
        rules: {
            ctl00$cphMain$ucContact$rblCustomerType: "required",
            ctl00$cphMain$ucContact$rblConsultant: "required"
        }
    });

});

function SetupCustomer() {
    if ($("input:name(rblCustomerType)").filter(":checked").val() == 'Existing') {
        $('#dConsultant').show().find(":input").removeClass("no-validation");
    } else {
        $('#dConsultant').hide().find(":input").addClass("no-validation");
    }
}
function SetupConsultant() {
    if ($("input:name(rblConsultant)").filter(":checked").val() == 'Yes') {
        $('input:id(tbConsultant)').show().focus().removeClass("no-validation");
    } else {
        $('input:id(tbConsultant)').hide().addClass("no-validation");
    }
}
function SetupOtherCountry() {
    if ($("select:id(ddlCountry) option:selected").val() == 'Other') {
        $('#pOtherCountry').show().find(":input").removeClass("no-validation");
        $('input:id(tbCountry)').focus();
    } else {
        $('#pOtherCountry').hide().find(":input").addClass("no-validation");
    }

    $('input:id(tbPhone)').removeClass("phoneAUS");
    $('input:id(tbPhone)').removeClass("phoneNZ");
    if ($("select:id(ddlCountry) option:selected").val() == 'NZ') {
        $('input:id(tbPostcode)').addClass("no-validation");
        $("#emPostcode").text("");
        $('input:id(tbPhone)').addClass("phoneNZ");
    } else {
        if ($("select:id(ddlCountry) option:selected").val() == 'AU') {
            $('input:id(tbPhone)').addClass("phoneAUS");
        }
        $('input:id(tbPostcode)').removeClass("no-validation");
        $("#emPostcode").text("Required");
    }
}
function SetupComments() {
    if ($("input:id(cbArea):last:checked").length > 0) {
        $('textarea:id(tbEnquiry)').addClass("required").parent().find("label em").text("Required");
    } else {
        $('textarea:id(tbEnquiry)').removeClass("required").parent().find("label em").text("");
    }
}

