﻿function TabStripShowTab(e) {
    if (e.className == 'active') return;

    var id = e.id.substring(0, e.id.lastIndexOf('_tab'));
    var active_tab = document.getElementById(id);

    document.getElementById(id + '_tab' + active_tab.value).className = '';
    document.getElementById(id + '_tab' + active_tab.value + '_body').style.display = 'none';
    
    e.className = 'active';
    document.getElementById(e.id + '_body').style.display = '';
    
    active_tab.value = e.id.substring(e.id.lastIndexOf('_tab') + 4);
}

function WizardShowPage(e) {
    if (e.parentNode.className == 'active' || e.parentNode.className == 'disabled') return;
    
    var id = e.id.substring(0, e.id.lastIndexOf('_tab'));
    var active_tab = document.getElementById(id);

    document.getElementById(id + '_tab' + active_tab.value).parentNode.className = '';
    document.getElementById(id + '_tab' + active_tab.value + '_body').style.display = 'none';

    e.parentNode.className = 'active';
    document.getElementById(e.id + '_body').style.display = '';
    
    active_tab.value = e.id.substring(e.id.lastIndexOf('_tab') + 4);
    
    var ul = e.parentNode.parentNode;
    var i_active_tab, i_first_normal_tab= undefined, i_last_normal_tab;

    for (i = 0; i < ul.childNodes.length; i++) {
        var li = ul.childNodes[i];

        if(li.className == 'active') i_active_tab = i;
        else if(li.className == '')
        {
            i_last_normal_tab = i;
            if(i_first_normal_tab == undefined) i_first_normal_tab = i;
        }
    }
    
    document.getElementById(id + '$prev_button').disabled = i_active_tab < i_first_normal_tab;
    document.getElementById(id + '$next_button').disabled = i_active_tab > i_last_normal_tab;
}

function WizardGotoPrevPage(e) {
    var ul = document.getElementById(e.id.substring(0, e.id.lastIndexOf('$prev_button')) + '_tab0').parentNode.parentNode;

    for (i = 0; i < ul.childNodes.length; i++) {
        var li = ul.childNodes[i];

        if(li.className == 'active')
        {
            for(j=i-1; ul.childNodes[j].className != ''; j--);
            
            WizardShowPage(ul.childNodes[j].firstChild);
            break;
        }
    }
}

function WizardGotoNextPage(e) {
    var ul = document.getElementById(e.id.substring(0, e.id.lastIndexOf('$next_button')) + '_tab0').parentNode.parentNode;

    for (i = 0; i < ul.childNodes.length; i++) {
        var li = ul.childNodes[i];

        if(li.className == 'active')
        {
            for(j=i+1; ul.childNodes[j].className != ''; j++);
            
            WizardShowPage(ul.childNodes[j].firstChild);
            break;
        }
    }
}

function OnWizardOKButtonClick(e, EventTarget, EventArgument) {
    var theForm = document.forms['aspnetForm'];

    if (!theForm) theForm = document.aspnetForm;

    theForm.__EVENTTARGET.value = EventTarget;
    theForm.__EVENTARGUMENT.value = EventArgument;
    theForm.target = "_blank";
    theForm.submit();
    theForm.target = "";

    __doPostBack(e.id, '');
}

function InputOnblur(e) {
    if(e.value=='') e.value=e.attributes['default'].value;
}

function InputOnfocus(e) {
    if(e.value == e.attributes['default'].value) e.value='';
}

function FormOnsubmit(form, args) {
    if(args != null && args != undefined)
    {
        for(i=0; i < args.length; i++)
        {
            var e = document.getElementById(args[i]);
            if(e.value == e.attributes['default'].value) return;
        }
    }
    form.submit();    
}

function PromptQuestions(e)
{
    var result = prompt('Введите список вопросов для самопроверки', '');
    
    if(result!=null)
        e.href+='$'+result;
    
    return result!=null;
}

function ToggleQuestion(e)
{
    while(e.nextSibling != null && e.className != 'faq-answer') e = e.nextSibling;
    
    if(e != null) e.style.display = e.style.display == 'none' ? '' : 'none';
    
    return false;
}

/* =================================================================== */
function slideConfig(Id, dir) {
    if (!Id) return;

    var opposite = (dir == 'Down' ? 'Up' : 'Down');

    $telerik.$("#" + Id + " .cfgContent")["slide" + dir](300);
    $telerik.$("#" + Id + " .cfgHead span.cfgButton").removeClass("cfgUp").removeClass("cfgDown").addClass("cfg" + opposite);
    $telerik.$("#" + Id + " .cfgHead")[0].href = "javascript:slideConfig('" + Id + "','" + opposite + "');";
    $telerik.$("input[name='" + Id + "']").val(dir == "Down" ? "true" : "false");
}

