﻿$(document).ready(function() {
    var isIESix = false;
    if ($.browser.msie && $.browser.version == "6.0")
        isIESix = true;

    $('div.placeholder').css('display', 'none');
    $('div.multiselect').css('display', 'block');       
        

    $('div.multiselect p').removeClass('hide');    
    $('div.multiselect ul li:first-child').each(function(e) {
        $(this).before(GetSelectAll(e));
    });

    $('div.multiselect').each(function() {
        var dvTag = $(this);
        var pTag = $(this).children()[0];
        var ulTag = $(this).children()[1];

        $(pTag).removeClass('hide');
        $(ulTag).addClass('hide');

        $(pTag).click(function() {
            if ($(ulTag).css('display') == "none") {
                $(ulTag).slideDown(150);
                $(dvTag).removeClass('borderbottom');
                if(isIESix) ShowHideSelects(false);
            }
            else {
                $(ulTag).slideUp(150);
                $(dvTag).addClass('borderbottom');

                DisableRentPerUnit(dvTag);
                
                if (isIESix) ShowHideSelects(true);
            }
        });

        $(dvTag).mouseleave(function() {
            if ($(ulTag).css('display') == "block") {
                $(ulTag).slideUp(200);

                DisableRentPerUnit(dvTag);
                
                $(dvTag).addClass('borderbottom');
                if (isIESix) ShowHideSelects(true);
            }
        })
    });

    $('div.innerselect').removeClass('relative');
    $('div.innerselect').addClass('absolute');

    $('div.innerselect ul li').click(function() {
        if (IsChecked($('input:checkbox', this)))
            SetCheckBox($('input:checkbox', this), false);
        else SetCheckBox($('input:checkbox', this), true);

        if ($(this).attr('class') == "selectall") {
            var isChecked = IsChecked($('input:checkbox', this));
            $('input:checkbox', $(this).parent()).each(function() {
                SetCheckBox(this, isChecked);
            });
        }
        SetCount($(this).parent().parent());
        SetSelectAll($(this).parent().parent())
    });

    $('div.innerselect ul li input:checkbox').click(function() {
        if (IsChecked(this))
            SetCheckBox(this, false);
        else SetCheckBox(this, true);
    });

    $('div.innerselect ul li label').click(function() {
        if (IsChecked($('input:checkbox', $(this).parent())))
            SetCheckBox($('input:checkbox', $(this).parent()), false);
        else SetCheckBox($('input:checkbox', $(this).parent()), true);

        if ($(this).parent().attr('class') == "selectall") {
            $('input:checkbox', $(this).parent().parent()).each(function() {
                SetCheckBox(this, IsChecked($('input:checkbox', $(this).parent())));
            });
        }
    });
});

function ShowHideSelects(isHidden) {
    $('select').each(function() {
        if (isHidden) $(this).removeClass('hide');
        else $(this).addClass('hide');
    });
}

function SetSelectAll(el) {
    SetCheckBox($('li.selectall input:checkbox', el), (GetCount(el) + 1) == $('li', el).length);
}

function IsChecked(el) {
    return $(el).attr('checked');
}

function SetCheckBox(el, isChecked) {
    $(el).attr('checked', isChecked);
}

function GetCount(el) {
    var itemCount = 0;
    $('input:checkbox', el).each(function() {
        if ((IsChecked($(this))) && ($(this).parent().attr('class') != 'selectall'))
            itemCount++;
    });
    return itemCount;
}

function SetCount(el) {
    var itemCount = GetCount(el);
    var str = "Please Select...";
    if (itemCount > 0)
        str = itemCount + " Item(s) Selected";
    $('p', $(el.parent())).text(str)
}

function GetSelectAll(index) {
    var selectAll = '<li class=\"selectall\"><input id="SelectAll_' + index + '" type="checkbox" value="1" name="SelectAll"/>' +
        '<label for="SelectAll_' + index + '">Select All</label></li>';
    return selectAll;
}

function ClearText(el, value) {
    if ($(el).val() == value)
        $(el).val('');
}

function PopulateText(el, value) {
    if($(el).val() == '')
        $(el).val(value);
}

function DisableRentPerUnit(dvTag) {
    
    if ($(dvTag).parent().attr("PropertyTypes") != null) {
        var ids = new Array()
        $("input:checkbox:checked", dvTag).each(function(element) {
            ids.push(this.value)
        });

        if (ids == "43") {
            EnableTextBox(true);            
            //PropertyRentPerUnitDestroy();
            //PropertyRentPerUnitDisabled();
        }
        else {
            EnableTextBox(false);
            //PropertyRentPerUnitDestroy();
            //ShowHideTextBoxPropertyRentPerUnitEnabled();
        }
    }
}

function EnableTextBox(enable) {
    $('input.tbrent').each(function(e, ui) {
        $(ui).attr("disabled", enable);
    });
}
