﻿$(document).ready(function () {
    var userTypeDdl = "#contour fieldset.typeofaccount .form_usertype div select";
    var hfCommercial = ".CommercialValidator";

    var hfAbn = ".AbnValidator";
    ShowAbn();


    ShowCommercialFieldSet();

    // Click events for user type dropdown list.
    $(userTypeDdl).change(function () {
        ShowCommercialFieldSet();
    });


    // add compare validator for passwords...TODO: implement this in .net!
    $("#contour .form_password").append("<span style='color:Red; display:none' class='customCompare'>Passwords must match</span>");
    $("#contour .contourNavigation input").click(function (evt) {
        var password = $("#contour .form_password input").val();
        var confirmPassword = $("#contour .form_password_confirmation input").val();

        if (password != confirmPassword) { // inject span
            $("#contour .form_password span.customCompare").show();
            //evt.preventDefault();
            return false;
        }
    });


    // move injected validator to inside the email field div
    var email = $("#contour .form_email span:eq(0)");
    var val = $("#contour .ValidateExisting");
    email.before(val);



    // Shows and hides commercial details fieldset
    function ShowCommercialFieldSet() {
        var selectedIndex = $(userTypeDdl).attr("selectedIndex");
        if ((selectedIndex > 0 && selectedIndex < 7) || selectedIndex > 10) {
            $("fieldset.commercialdetails").fadeIn();
            $(hfCommercial).attr("value", "true");
        } else {
            $("fieldset.commercialdetails").fadeOut();
            $(hfCommercial).attr("value", "false");

            // reset fields so they don't get passed into database
            $("#contour fieldset.commercialdetails .form_company input").val("");
            $("#contour fieldset.commercialdetails .form_fax input").val("");
            $("#contour fieldset.commercialdetails .form_abn input").val("");

        }
    }


    function ShowAbn() {
        var value = $(hfAbn).attr("value");
        if (value == "False") {
            $(".form_abn").remove();
        }
    }

});