﻿
function pageLoad(sender, args) {
    if (args.get_isPartialLoad()) {

        AddCarSellDatePanel();
        ProfileChangePasswordPanel();
        ProfilePhones();
        ProfileSaveClick();
        ProfileMessageHideClick();
        ProfileCityCompleteFix();
    }
}

$(function() {
    AddCarSellDatePanel();
    ProfileChangePasswordPanel();
    ProfilePhones();
    RestorePhones();
    ProfileSaveClick();
    ProfileMessageHideClick();
    ProfileCityCompleteFix();
});

function AddCarSellDatePanel() {
    var hiddenControl = $("*[id$=hIsShowSell]")[0];
    if (hiddenControl != null) {
        if (hiddenControl.value == "0") {
            $("#aSellMyCar").addClass("link-ajax");
            $("#ulSellMyCar").hide();
        }
        else {
            $("#aSellMyCar").removeClass("link-ajax");
            $("#ulSellMyCar").show();
        }
    }

    $("#aSellMyCar").click(function() {
        $("#ulSellMyCar").toggle(300);
        $("#aSellMyCar").toggleClass("link-ajax");

        var hiddenControl = $("*[id$=hIsShowSell]")[0];
        if (hiddenControl != null) {
            if (hiddenControl.value == "0") {
                hiddenControl.value = "1";
            }
            else {
                hiddenControl.value = "0";
            }
        }
        return false;
    });
}

function ProfileChangePasswordPanel() {    
    var hiddenControl = $("*[id$=hIsShowChangePassword]")[0];
    if (hiddenControl != null) {
        if (hiddenControl.value == "0") {
            $("#aChangePassword").addClass("link-ajax");
            $("#tablePassword").fadeOut(200);
        }
        else {
            $("#aChangePassword").removeClass("link-ajax");
            $("#tablePassword").fadeIn(500);
        }
    }

    $("#aChangePassword").click(function() {        
        $("#aChangePassword").toggleClass("link-ajax");

        var hiddenControl = $("*[id$=hIsShowChangePassword]")[0];
        if (hiddenControl != null) {
            if (hiddenControl.value == "0") {

                $("#tablePassword").fadeIn(500);                
            
                hiddenControl.value = "1";
            }
            else {
                hiddenControl.value = "0";

                $("#tablePassword").fadeOut(200);
            }
        }
        return false;
    });
}

function ProfilePhones() {   
    $("*[id=aPhoneDelete1]").click(function() {
        $("*[id=trPhone1]").remove();
        return false;
    });    
    
    $("*[id=aPhoneComment1]").click(function() {        
        $("*[id=txtPhoneComment1]").show();
        $("*[id=aPhoneComment1]").hide();
        return false;
    });


    $("#aAddPhone").click(function() {
        var index = Math.random();
        var x = '<tr id="trPhone' + index + '" class="tr-phone"><th>Телефон</th><td><input type="text" id="txtNewPhone' + index + '" class="text text-big phone-num"/><input style="display:none;" id="txtPhoneComment' + index + '" type="text" class="text text-big phone-comm" /><p id="aPhoneComment' + index + '" class="h-link-ajax"><span class="link-ajax">Коментарий</span></p><span id="aPhoneDelete' + index + '"><img alt="удалить" src="/img/1.7/profile/bt-del.gif"/></span></td></tr>';
        $("#tablePhones tbody").append(x);

        $("*[id=aPhoneComment" + index + "]").click(function() {
            $("*[id=txtPhoneComment" + index + "]").show();
            $("*[id=aPhoneComment" + index + "]").hide();
            return false;
        });

        $("*[id=aPhoneDelete" + index + "]").click(function() {            
            $("*[id=trPhone" + index + "]").remove();
            return false;
        });

        return false;
    });

}

function RestorePhones() {
    var hiddenPhones = $("*[id$=hfPhones]")[0];
    var hiddenPhoneComents = $("*[id$=hfPhoneComments]")[0];
    if (hiddenPhones != null && hiddenPhoneComents != null) {
        var phones = hiddenPhones.value.toString().split("☺");
        var comments = hiddenPhoneComents.value.toString().split("☺");
        for (i = 0; i < phones.length - 1; i++) {
            if (i == 0) {
                $("*[id=txtNewPhone1]")[0].value = phones[0];
                $("*[id=txtPhoneComment1]")[0].value = comments[0];
                if (comments[0] == "") {
                    $("*[id=txtPhoneComment" + 1 + "]").hide();
                    $("*[id=aPhoneComment" + 1 + "]").show();
                }
                else {
                    $("*[id=txtPhoneComment" + 1 + "]").show();
                    $("*[id=aPhoneComment" + 1 + "]").hide();
                }
            }
            else {
                if (phones[i] != "") {
                    var index = Math.random();
                    var x = '<tr id="trPhone' + index + '" class="tr-phone"><th>Телефон</th><td><input type="text" id="txtNewPhone' + index + '" value="' + phones[i] + '" class="text text-big phone-num"/><input style="display:none;" id="txtPhoneComment' + index + '" type="text" value="' + comments[i] + '" class="text text-big phone-comm" /><p id="aPhoneComment' + index + '" class="h-link-ajax"><span class="link-ajax">Коментарий</span></p><span id="aPhoneDelete' + index + '"><img alt="удалить" src="/img/1.7/profile/bt-del.gif"/></span></td></tr>';
                    $("#tablePhones tbody").append(x);

                    $("*[id=aPhoneComment" + index + "]").click(function() {
                        var ind = this.id.replace("aPhoneComment", "");  
                        $("*[id=txtPhoneComment" + ind + "]").show();
                        $("*[id=aPhoneComment" + ind + "]").hide();
                        return false;
                    });

                    $("*[id=aPhoneDelete" + index + "]").click(function() {                        
                        var ind = this.id.replace("aPhoneDelete", "");                        
                        $("*[id=trPhone" + ind + "]").remove();
                        return false;
                    });

                    if (comments[i] == "") {
                        $("*[id=txtPhoneComment" + index + "]").hide();
                        $("*[id=aPhoneComment" + index + "]").show();
                    }
                    else {
                        $("*[id=txtPhoneComment" + index + "]").show();
                        $("*[id=aPhoneComment" + index + "]").hide();
                    }
                }
            }
        }
    }
}


function ProfileSaveClick() {
    var allPhones = "";
    var allComments = "";

    $("input[id$=btnSave]").click(function() {        
        window.scroll(0, 0);

        $("*[id^=txtNewPhone]").each(function() {
        allPhones += this.value + "☺";
        })

        $("*[id^=txtPhoneComment]").each(function() {
        allComments += this.value + "☺";
        })

        $("*[id$=hfPhones]")[0].value = allPhones;
        $("*[id$=hfPhoneComments]")[0].value = allComments;
    });
}

function ProfileMessageHideClick() {
    $("*[id$=spanCloseMessage]").click(function() {
        $("#divProfileMesage").remove();
    });
}
    
function ProfileCityCompleteFix() {
    $("ul[id*=aceCityComplete]").attr("style", "border: 1px solid buttonshadow; padding: 0px; text-align: left; visibility: hidden; cursor: default; list-style-type: none; list-style-image: none; list-style-position: outside; background-color: window; color: windowtext; position: relative; width: 306px; left: 172px; top: 273px; z-index:99;");
}

