﻿function show_print(){
    if($('.phr:visible').length > 0){
        $('table.main_table').css('width', '560');
        $('.phr').css('display', 'none');
        $('.phd').css('display', 'none');
        $('#print_icon')
            .unbind()
            .attr('src', '/musor/_i/icons/screen.gif');

    } else {
        $('table.main_table').css('width', '942');
        $('.phr').css('display', '');
        $('.phd').css('display', '');
		
        $('#print_icon')
            .attr('src', '/musor/_i/icons/print.gif')
            .attr('orig', '/musor/_i/icons/print.gif')
            .attr('hover', '/musor/_i/icons/print_hover.gif');
        init_hovers();
    }
}


/* COMMON FUNCTIONS */

function set_overlay(){
    $('<div id="my_overlay"></div>').css('height', body_height()).css('width', body_width()).hide().appendTo('body');
    opacity_inc('0');
}

function opacity_inc(op){
    if(op < 5){
        op = op+1;
        $('#my_overlay').css('opacity', '0.' + op).css('filter', 'alpha(opacity = '+op+'0)').show();
        setTimeout('opacity_inc('+op+')',25);
    }
}

function opacity_dec(op){
    if(op > 0){
        op = op - 1;
        $('#my_overlay').css('opacity', '0.' + op).css('filter', 'alpha(opacity = '+op+'0)').show();
        setTimeout('opacity_dec('+op+')',25);
    } else {
        $('#my_overlay').remove();
    }
}

function body_height(){
    if ($.browser.msie && $.browser.version < 7) {
        var scrollHeight = Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);
        var offsetHeight = Math.max(document.documentElement.offsetHeight, document.body.offsetHeight);
        if (scrollHeight < offsetHeight) {
            return $(window).height() + 'px';
        } else {
            return scrollHeight + 'px';
        }
    } else if ($.browser.opera) {
        return Math.max(window.innerHeight, $(document).height()) + 'px';
    } else {
        return $(document).height() + 'px';
    }
}

function body_height_int(){
    if ($.browser.msie && $.browser.version < 7) {
        var scrollHeight = Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);
        var offsetHeight = Math.max(document.documentElement.offsetHeight, document.body.offsetHeight);
        if (scrollHeight < offsetHeight) {
            return $(window).height();
        } else {
            return scrollHeight;
        }
    } else if ($.browser.opera) {
        return Math.max(window.innerHeight, $(document).height());
    } else {
        return $(document).height();
    }
}

function body_width(){
    if ($.browser.msie && $.browser.version < 7) {
        return $(window).width() + 'px';
    } else if ($.browser.opera) {
        return Math.max(window.innerWidth, $(document).width()) + 'px';
    } else {
        return $(window).width() + 'px';
    }
}

function body_width_int(){
    if ($.browser.msie && $.browser.version < 7) {
        return $(window).width();
    } else if ($.browser.opera) {
        return Math.max(window.innerWidth, $(document).width());
    } else {
        return $(window).width();
    }
}

/* ACTIVATE */

function activate_close(){
    $('#activate_container').remove();
    opacity_dec('5');
}

function activate_set_welcome(){
    $('#activate_desc').html('<p class="head"><b>Добро пожаловать!</b></p><p>Вы находитесь в разделе Активации Карты БЕРЁЗА.<br />Введите 16-ти значный номер Карты БЕРЁЗА.<br />Затем введите 8-и значный Код активации и контрольные цифры.</p>');
}

function activate(){
    $('#my_overlay').remove();
    $('#activate_container').remove();
    set_overlay();
    $(document).keypress( function(e){
        if(e.keyCode == 27){
            activate_close();
        } else {
            if(e.keyCode == 13){
                $('#activate_submit_button').click();
            }
        }
    });
    $('<div id="activate_container"></div>').css('left', eval($(window).width()/2-260)).css('top', eval($(window).height()/2-185)).appendTo('body');
    $('<table id="activate_table"><tr><td id="top_span" colspan="5"></td></tr><tr><td id="left_span"></td><td id="activate_desc"></td><td id="center_span"></td><td id="activate_form"></td><td id="right_span"></td><tr><td id="bottom_span" colspan="5"></td></tr></table>').appendTo('#activate_container');
    activate_set_welcome();
    $('<img id="activate_close" src="/musor/_i/buttons/close_green.gif" />').click(function(){activate_close()}).appendTo('#activate_container');
    $('#activate_form').html('<form id="a_process" action="/activation/" method="post" style="display: inline"><input type="hidden" name="uid_f" id="uid_f"><p id="card_id_desc">Номер Карты</p><p id="card_id"><input maxlength="4" name="card1_f" id="card1_f" type="text"><input maxlength="4" name="card2_f" id="card2_f" type="text"><input maxlength="4" name="card3_f" id="card3_f" type="text"><input maxlength="4" name="card4_f" id="card4_f" type="text"></p><p id="card_code_desc">Код Активации</p><p id="card_code"><input maxlength="8" name="code_f" id="code_f" type="text"></p><!-- p id="email_desc">E-mail</p><p id="email"><input name="email_f" id="email_f" type="text"></p --><p id="captcha_img_desc">Введите цифры</p><p id="captcha_img"><img id="captcha_image" src="/captcha.html" /><img style="margin-left: 10px; cursor: pointer" alt="Если Вы не можете прочитать код" title="Если Вы не можете прочитать код" src="/musor/_i/icons/reload.gif" onclick="set_captcha()" /></p><p id="captcha_code"><input name="c_f" id="c_f" type="text"></p><p id="activate_submit"><img style="cursor: pointer" id="activate_submit_button" src="/musor/_i/buttons/activate_green.gif" /></p></form>');
    $('#activate_submit_button').click( function(){
        $(this).attr('src', '/musor/_i/buttons/verify_green.gif');
        var activate_data = 'card1_f=' + $('#card1_f').val() + '&card2_f=' + $('#card2_f').val() + '&card3_f=' + $('#card3_f').val() + '&card4_f=' + $('#card4_f').val() + '&code_f=' + $('#code_f').val() + '&email_f=' + $('#email_f').val() + '&c_f=' + $('#c_f').val() + '&uid_f=' + $('#uid_f').val();
        $.ajax({
            type: "POST",
            url: "/rpc/xmlRpc4/",
            dataType: "xml",
            data: activate_data,
            cache: false,
            success: function(xml){
                var response = $(xml).find('check').attr('result');
                if(response == '0'){
                    $('#a_process').submit();
                } else {
                    set_captcha();
                    $('#activate_submit_button').attr('src', '/musor/_i/buttons/activate_green.gif');
                    $('#activate_desc').html('<p class="head"><b>Ошибка!</b></p><p>Вы ввели неверные данные.</p>');
                    if(response & 1){ $('<p id="a_error">&bull; Неправильный Номер карты</p>').appendTo('#activate_desc'); }
                    if(response & 2){ $('<p id="a_error">&bull; Неправильный Код активации</p>').appendTo('#activate_desc'); }
                    //if(response & 4){ $('<p id="a_error">&bull; Неправильный Электронный адрес</p>').appendTo('#activate_desc'); }
                    if(response & 8){ $('<p id="a_error">&bull; Неправильный Код подтверждения</p>').appendTo('#activate_desc'); }
                    $('#activate_desc').html($('#activate_desc').html() + '<p>Пожалуйста, попробуйте еще раз.</p>');

                }
            }
        });
    });
    set_captcha();
}

function set_captcha(){
    $.ajax({
        url: '/rpc/xmlRpc3/',
        dataType: 'xml',
        cache: false,
        success: function(xml){
            var uid = $(xml).find('uid').attr('result');
            $('#captcha_image').attr('src', '/captcha.html?uid=' + uid);
            $('#uid_f').val(uid);
        }
    });
}


/* LOGIN1 */

function login1_close(){
    $('#login1_container').remove();
    opacity_dec('5');
}

function login1_set_welcome(){
    $('#login1_desc').html('<p class="head"><b>Добро пожаловать!</b></p><p>Введите номер Карты БЕРЁЗА и Код доступа для входа в ON-Line офис Участника Единой партнерской системы БЕРЁЗА.</p>');
}

$('#card_id input').live('keydown', function(e){
    if(((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105)) && $(this).val().length >= 4){
        if($('#card_id input').index(this) < 3){
            $('#card_id input').get($('#card_id input').index(this) + 1).focus();
        }
    }
});

function login1(){
   
    $('#my_overlay').remove();
    $('#login1_container').remove();
    set_overlay();
    $(document).keypress( function(e){
        if(e.keyCode == 27){
            login1_close();
        } else {
            if(e.keyCode == 13){
                $('#login1_submit_button').click();
            }
        }
    });
    $('<div id="login1_container"></div>').css('left', eval($(window).width()/2-260)).css('top', eval($(window).height()/2-136)).appendTo('body');
    $('<table id="login1_table"><tr><td id="top_span" colspan="5"></td></tr><tr><td id="left_span"></td><td id="login1_desc"></td><td id="center_span"></td><td id="login1_form"></td><td id="right_span"></td><tr><td id="bottom_span" colspan="5"></td></tr></table>').appendTo('#login1_container');
    login1_set_welcome();
    $('<img id="login1_close" src="/musor/_i/buttons/close_green.gif" />').click(function(){login1_close()}).appendTo('#login1_container');
    $('#login1_form').html('<form id="l1_process" action="/login1/" method="post" style="display: inline"><p id="card_id_desc">Номер карты</p><p id="card_id"><input name="card1_f" id="card1_f" maxlength="4" type="text"><input name="card2_f" maxlength="4" id="card2_f" type="text"><input name="card3_f" maxlength="4" id="card3_f" type="text"><input name="card4_f" maxlength="4" id="card4_f" type="text"></p><p id="pwd_desc">Код доступа</p><p id="pwd"><input name="pwd_f" id="pwd_f" type="password" maxlength="8" ></p><p id="login1_submit"><img style="cursor: pointer" id="login1_submit_button" src="/musor/_i/buttons/login_green.gif" /></p></form>');
    $('#login1_submit_button').click( function(){
        $(this).attr('src', '/musor/_i/buttons/verify_green.gif');
        var login1_data = 'card1_f=' + $('#card1_f').val() + '&card2_f=' + $('#card2_f').val() + '&card3_f=' + $('#card3_f').val() + '&card4_f=' + $('#card4_f').val() + '&pwd_f=' + $('#pwd_f').val();
        $.ajax({
            type: "GET",
            url: "/rpc/xmlRpc5/",
            dataType: "xml",
            data: login1_data,
            cache: false,
            success: function(xml){
                var response = $(xml).find('check').attr('result');
                if(response == '0'){
                    $('#l1_process').submit();
                } else {
                    $('#login1_submit_button').attr('src', '/musor/_i/buttons/login_green.gif');

                    $('#login1_desc').html('<p class="head"><b>Ошибка!</b></p><p>Неправильные Номер карты или Код доступа.</p>');
                }
            }
        });
    });
}

/* SEND LINK */

function send_link_close(){
    $('#send_link_container').remove();
    opacity_dec('5');
}

function reset_link_buttons(){
    $('#send_link_submit').remove();
    $('#send_link_close').remove();
    $('<img id="send_link_close" src="/musor/_i/buttons/cancel.gif" hover="/musor/_i/buttons/cancel_hover.gif" />').click(function(){send_link_close()}).appendTo('#send_link_container');
    $('<img id="send_link_submit" src="/musor/_i/buttons/send.gif" hover="/musor/_i/buttons/send_hover.gif" />').click(function(){send_link_submit()}).appendTo('#send_link_container');
    init_hovers();
}

function send_link_submit(){
    $('#send_link_submit').unbind().attr('src', '/musor/_i/buttons/verify.gif');
    $('#email_to').css('color', 'black'); 
    $('#email_from').css('color', 'black');
    var send_link_data = 'email_to_f=' + $('#email_to_f').val() + '&name_f=' + $('#name_f').val() + '&email_from_f=' + $('#email_from_f').val() + '&subject_f=' + $('#subject_f').val();
    $.ajax({
        type: "POST",
        url: "/rpc/xmlRpc6/",
        dataType: "xml",
        data: send_link_data,
        cache: false,
        success: function(xml){
            var response = $(xml).find('send').attr('result');
            if(response == '0'){
                $('#send_link_submit').remove();
                $('#send_link_close').remove();
                $('#link1_html').html('<img src="/musor/i/send_link/send_link_logo.png" /><br /><br /><br /><br /><br /><p style="text-align: center" class="head"><br />Ссылка успешно отправлена!</p>');
                $('<img id="send_link_close2" src="/musor/_i/buttons/close.gif" hover="/musor/_i/buttons/close_hover.gif" />').click(function(){send_link_close()}).appendTo('#send_link_container');
                init_hovers();
            } else {
                if(response & 1){ $('#email_to').css('color', 'red'); }
                if(response & 2){ $('#email_from').css('color', 'red'); }
                reset_link_buttons();
            }
        }
    });
}

function send_link_set_welcome(){
    $('#link1_html').html('<img src="/musor/i/send_link/send_link_logo.png" /><br /><p class="head">Отправить ссылку на раздел ON-Line системы на:</p><div id="send_link_form"></div>');
}

function send_link(){
    $('#my_overlay').remove();
    $('#send_link_container').remove();

    set_overlay();
    $(document).keypress( function(e){
        if(e.keyCode == 27){
            send_link_close();
        } else {
            if(e.keyCode == 13){
                $('#send_link_submit').click();
            }
        }
    });
    $('<div id="send_link_container"></div>').css('left', eval($(window).width()/2-300)).css('top', eval($(window).height()/2-300)).appendTo('body');
    $('<table id="send_link_table"><tr><td id="top_span" colspan="3"></td></tr><tr><td id="left_span"></td><td id="link1_html"></td><td id="right_span"></td><tr><td id="bottom_span" colspan="3"></td></tr></table>').appendTo('#send_link_container');
    send_link_set_welcome();
    reset_link_buttons();
    $('#send_link_form').html('<p id="email_to">E-mail получателя:<br /><input type="text" name="email_to_f" id="email_to_f"></p><p id="name">Ваши имя:<br /><input type="text" name="name_f" id="name_f"></p><p id="email_from">Ваш E-mail:<br /><input type="text" name="email_from_f" id="email_from_f"></p><p id="subject">Тема письма:<br /><input type="text" name="subject_f" id="subject_f"></p><p id="link">Отправляемая ссылка:<p><p id="link_url">' + document.location.href + '</p>');
    $('#send_link_submit').click( send_link_submit );
}

/* LOGIN2 */

function login2_close(){
    $('#login2_container').remove();
    opacity_dec('5');
}

function login2_set_welcome(){
    $('#login2_desc').html('<p class="head"><b>Добро пожаловать!</b></p><p>Введите Логин и Пароль для входа в ON-Line офис Партнёра Единой партнерской системы БЕРЁЗА.</p>');
}



function login2(){
   
    $('#my_overlay').remove();
    $('#login2_container').remove();
    set_overlay();
    $(document).keypress( function(e){
        if(e.keyCode == 27){
            login2_close();
        } else {
            if(e.keyCode == 13){
                $('#login2_submit_button').click();
            }
        }
    });
    $('<div id="login2_container"></div>').css('left', eval($(window).width()/2-260)).css('top', eval($(window).height()/2-136)).appendTo('body');
    $('<table id="login2_table"><tr><td id="top_span" colspan="5"></td></tr><tr><td id="left_span"></td><td id="login2_desc"></td><td id="center_span"></td><td id="login2_form"></td><td id="right_span"></td><tr><td id="bottom_span" colspan="5"></td></tr></table>').appendTo('#login2_container');
    login2_set_welcome();
    $('<img id="login2_close" src="/musor/_i/buttons/close_green.gif" />').click(function(){login2_close()}).appendTo('#login2_container');
    $('#login2_form').html('<form id="l2_process" action="/login2/" method="post" style="display: inline"><p id="login_desc">Логин</p><p id="login"><input name="login_f" id="login_f" type="text"></p><p id="pwd_desc">Пароль</p><p id="pwd"><input name="pwd_f" id="pwd_f" type="password"></p><p id="login2_submit"><img style="cursor: pointer" id="login2_submit_button" src="/musor/_i/buttons/login_green.gif" /></p></form>');
    $('#login2_submit_button').click( function(){
        $(this).attr('src', '/musor/_i/buttons/verify_green.gif');
        var login2_data = 'login_f=' + $('#login_f').val() + '&pwd_f=' + $('#pwd_f').val();
        $.ajax({
            type: "GET",
            url: "/rpc/xmlRpc9/",
            dataType: "xml",
            data: login2_data,
            cache: false,
            success: function(xml){
                var response = $(xml).find('login').attr('result');
                if(response == '0'){
                    $('#l2_process').submit();
                } else {
                    $('#login2_submit_button').attr('src', '/musor/_i/buttons/login_green.gif');
                    $('#login2_desc').html('<p class="head"><b>Ошибка!</b></p><p>Неправильные Логин или Пароль.</p>');
                }
            }
        });
    });
}


/* BOOKMARKS */
function bookmarksite(title,url){
    if (window.sidebar)
        window.sidebar.addPanel(title, url, "");
    else if(window.opera && window.print){
        var elem = document.createElement('a');
        elem.setAttribute('href',url);
        elem.setAttribute('title',title);
        elem.click();
    } else if(document.all) {
        window.external.AddFavorite(url, title);
    }
}


/* VOTE */
function vote_set_center(){
    $('#vote_content').css('vertical-align', 'middle').css('text-align', 'center');
}

function vote_set_top(){
    $('#vote_content').css('vertical-align', 'top').css('text-align', 'left');
}

function vote_set_process(){
    vote_set_center();
    $('#vote_content').html('<img src="/musor/_i/vote/vote_process.gif" />');
}

function vote_show_buttons(){ 
    $('<img src="/musor/_i/buttons/vote_green.gif" />').unbind().bind('click', vote_ajax_vote).css('cursor', 'pointer').appendTo('#vote_submit');
    $('<img src="/musor/_i/buttons/result_green.gif" />').unbind().bind('click', vote_ajax_load_result).css('cursor', 'pointer').appendTo('#vote_result');
}

function vote_ajax_load_lines(){ 
    vote_set_process();
    $.ajax({
        type: "POST",
        url: "/rpc/loadlines/",
        data: "",
        dataType: "xml",
        cache: false,
        success: function(vote_xml){
            switch($(vote_xml).find('error').attr('type')){
                case '0':
                    var vote_title = $(vote_xml).find('title').text();
                    var lines = $(vote_xml).find('lines').find('line');
                    $('#vote_content').html('');
                    vote_set_top();
                    $('<p></p>').css('font-weight', 'bold').text(vote_title).appendTo('#vote_content');
                    $('#vote_content').append('<form style="display: inline"><table style="margin-top: 5px"></table></form>');
                    $(lines).each( function(){
                        $('#vote_content').find('form').find('table').append('<tr><td><input type="radio" name="vote" id="vote_' + $(this).attr('value') + '" value="' + $(this).attr('value') + '"></td><td><label for="vote_' + $(this).attr('value') + '">' + $(this).attr('title') + '</label></td></tr>');
                    });
                    $('#vote_submit').find('img').unbind().bind('click', vote_ajax_vote);
                    break;
                case '1':
                    $('#vote_content').html('');
                    var vote_error = $(vote_xml).find('error').attr('description');
                    $('#vote_content').html('');
                    $('<p></p>').css('font-weight', 'bold').text(vote_error).appendTo('#vote_content');
                    vote_hide_buttons();
                    break;
                case '2':
                    $('#vote_content').html('');
                    var vote_error = $(vote_xml).find('error').attr('description');
                    $('#vote_content').html('');
                    $('<p></p>').css('font-weight', 'bold').text(vote_error).appendTo('#vote_content');
                    vote_hide_buttons();
                    break;
            }
        }
    });
}

function vote_hide_buttons(){ 
    $('#vote_submit').find('img').remove();
    $('#vote_result').find('img').remove();
}

function vote_ajax_load_result(){ 
    vote_set_process();
    $.ajax({
        type: "POST",
        url: "/rpc/loadresult/",
        data: "",
        dataType: "xml",
        cache: false,
        success: function(vote_xml){
            switch($(vote_xml).find('error').attr('type')){
                case '0':
                    var vote_title = $(vote_xml).find('title').text();
                    var lines = $(vote_xml).find('lines').find('line');
                    $('#vote_content').html('');
                    vote_set_top();
                    $('<p></p>').css('font-weight', 'bold').text(vote_title).appendTo('#vote_content');
                    $(lines).each( function(){
                        $('<div></div>').addClass('vote_res_desc').html($(this).attr('title') + ' (' + $(this).attr('percent') + '%)').appendTo('#vote_content');
                        $('<div></div>').addClass('vote_res_div').width($(this).attr('width')).appendTo('#vote_content');
                    });
                    $('<p></p>').css('font-weight', 'bold').html('Проголосовало: ' + $(vote_xml).find('lines').attr('count')).appendTo('#vote_content');
                    $('#vote_submit').find('img').unbind().bind('click', vote_ajax_load_lines);
                    break;
                case '1':
                    $('#vote_content').html('');
                    var vote_error = $(vote_xml).find('error').attr('description');
                    $('#vote_content').html('');
                    $('<p></p>').css('font-weight', 'bold').text(vote_error).appendTo('#vote_content');
                    vote_hide_buttons();
                    break;
                case '2':
                    $('#vote_content').html('');
                    var vote_error = $(vote_xml).find('error').attr('description');
                    $('#vote_content').html('');
                    $('<p></p>').css('font-weight', 'bold').text(vote_error).appendTo('#vote_content');
                    vote_hide_buttons();
                    break;
            }
        }
    });
}

function vote_ajax_vote(){ 
    var vote_line = $('input[name="vote"]:checked').val();
    if(vote_line != undefined){
        vote_set_process();
        $.ajax({
            type: "POST",
            url: "/rpc/vote/",
            data: "vote=" + vote_line,
            dataType: "xml",
            cache: false,
            success: function(vote_xml){
                switch($(vote_xml).find('error').attr('type')){
                    case '0':
                        $('#vote_content').html('');
                        vote_set_top();
                        vote_ajax_load_result();
                        vote_hide_buttons();
                        break;
                    case '1':
                        $('#vote_content').html('');
                        var vote_error = $(vote_xml).find('error').attr('description');
                        $('#vote_content').html('');
                        $('<p></p>').css('font-weight', 'bold').text(vote_error).appendTo('#vote_content');
                        vote_hide_buttons();
                        break;
                    case '2':
                        $('#vote_content').html('');
                        var vote_error = $(vote_xml).find('error').attr('description');
                        $('#vote_content').html('');
                        $('<p></p>').css('font-weight', 'bold').text(vote_error).appendTo('#vote_content');
                        vote_hide_buttons();
                        break;
                    case '3':
                        $('#vote_content').html('');
                        var vote_error = $(vote_xml).find('error').attr('description');
                        $('#vote_content').html('');
                        $('<p></p>').css('font-weight', 'bold').text(vote_error).appendTo('#vote_content');
                        break;
                }
            }
        });
    }
}

/* INIT HOVERS */
  
function init_hovers(){

    $('img[hover]').each( function(){
        $(this).attr('orig', $(this).attr('src'));
    });

    $('img[hover]').mouseover( function(){
        $(this).attr('src', $(this).attr('hover'));
    });

    $('img[hover]').mouseout( function(){
        $(this).attr('src', $(this).attr('orig'));
    });
}


/* EMAIL VALIDATION */
function email_validate(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      return false;
   } else {
       return true;
   }
}

/* */
function myToggle(id){
    if($('#' + id).css('display') == 'block'){ 
        $('#' + id).slideUp('fast');
    } else { 
        $('#' + id).slideDown('fast');
    }

}

function prf(){
    $('form.form_process').each( function(){
        $(this).find('input').each( function(){
            if($(this).val().length == 0){
                $(this).addClass('fp_default').val(fp[$(this).attr('name')]);
                $(this).focus( function(){
                    $(this).removeClass('fp_default');
                    if($(this).val() == fp[$(this).attr('name')]){
                        $(this).val('');
                    }
                });
                $(this).blur( function(){
                    if($(this).val().length == 0 || $(this).val() == fp[$(this).attr('name')]){
                        $(this).addClass('fp_default');
                        $(this).val(fp[$(this).attr('name')]);
                    }
                });
            }
        });
    });
}

/* R */

$(document).ready(function() {
	

						   
						   });