﻿$(document).ready(function() {
    LoadShowFields();
});

function LoadShowFields() {
    $("table[id*=iuc_IsRegistered]").find("input[type=radio]").unbind('click', SetShowNewUserFields).bind('click', SetShowNewUserFields);
    SetShowNewUserFields();
    $("table[id*=iuc_IsBusiness]").find("input[type=radio]").unbind('click', SetShowNewConsumerFields).bind('click', SetShowNewConsumerFields);
    SetShowNewConsumerFields();
}

function SetShowNewUserFields() {
    var isRegRadioButton = $("table[id*=iuc_IsRegistered]").find("input[type=radio][value=True]");
    var isNotRegRadioButton = $("table[id*=iuc_IsRegistered]").find("input[type=radio][value=False]");
    var isBusinessField = $("#ul_isBusiness");
    if (isRegRadioButton.attr("checked")) {
        isBusinessField.find("span.validator").disableValidators();
        ClearInputs(isBusinessField);
        isBusinessField.slideUp(500);
        $("#li_unRegConsumerQuestions").hide();  
    }
    else if (isNotRegRadioButton.attr("checked")) {
        isBusinessField.find("span.validator").enableValidators();
        isBusinessField.slideDown(500);
    }
}

function SetShowNewConsumerFields() {
    var isBizRadioButton = $("table[id*=iuc_IsBusiness]").find("input[type=radio][value=True]");
    var isNotBizRadioButton = $("table[id*=iuc_IsBusiness]").find("input[type=radio][value=False]");
    var newConsumerFields = $("#li_unRegConsumerQuestions");
    if (isBizRadioButton.attr("checked")) {
        newConsumerFields.find("span.validator").disableValidators();
        ClearInputs(newConsumerFields);
        newConsumerFields.slideUp(500);
    }
    else if (isNotBizRadioButton.attr("checked")) {
        newConsumerFields.find("span.validator").enableValidators();
        newConsumerFields.slideDown(500);
    }
}

function ClearInputs(elem) {
    elem.find('input , select').each(function() {
        if ($(this)[0].tagName.toUpperCase() == "SELECT") {
            $(this).val(-1);
        }
        else if ($(this)[0].tagName.toUpperCase() == "INPUT") {
            if ($(this)[0].type.toUpperCase() == "CHECKBOX" || $(this)[0].type.toUpperCase() == "RADIO") {
                $(this)[0].checked = false;
            }
            else if ($(this)[0].type.toUpperCase() == "TEXT") {
                $(this).val("");
            }
        }
    });
}

// Fixes the following error:
// Microsoft JScript runtime error: Sys.ScriptLoadFailedException: The script '...' failed to load. Check for: Inaccessible path.
if (typeof Sys !== "undefined") { Sys.Application.notifyScriptLoaded(); }