﻿function GoToURL(sender) {
    if (sender.value)
        document.location = sender.value;
}
function GetFavorite() {
    window.open('/favorite.ashx?path=' + location.href + '&title=' + document.title, '', '', '');
}
function SetCookie(name, value, expires) {
    //alert(value);
    var dt = new Date();
    var dtt = "";
    dt.setTime(dt.getTime() + expires * 24 * 60 * 60 * 1000);
    dtt = dt.toGMTString();
    document.cookie = name + "=" + value + ";expires=" + dtt + ";path=/";
}
function getCookie(name) {
    var prefix = name + "=";
    var cookieStartIndex = document.cookie.indexOf(prefix);
    if (cookieStartIndex == -1)
        return null;
    var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
    if (cookieEndIndex == -1)
        cookieEndIndex = document.cookie.length;
    return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}
function deleteCookie(name) {
    if (getCookie(name)) {
        document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT;path=/";
    }
}

function Expand_Or_CollapseDiv(sender, divForExpandID, lnkForExpandTextID, hideText, openText, change_sender) {
    var divForExpand = document.getElementById(divForExpandID);
    var lnkForExpandText = document.getElementById(lnkForExpandTextID);

    if (divForExpand.style.display == 'none') {
        divForExpand.style.display = "block";
        divForExpand.style.visibility = "visible";
        if (lnkForExpandTextID) {
            lnkForExpandText.innerHTML = hideText;
            lnkForExpandText.style.backgroundImage = "url(/i/minus_blue.gif)";
        }
        else if (change_sender) {
            sender.style.backgroundImage = "url(/i/minus_blue.gif)";
        }
    }
    else {
        divForExpand.style.display = "none";
        divForExpand.style.visibility = "hidden";
        if (lnkForExpandTextID) {
            lnkForExpandText.innerHTML = openText;
            lnkForExpandText.style.backgroundImage = "url(/i/plus_blue.gif)";
        }
        else if (change_sender) {
            sender.style.backgroundImage = "url(/i/plus_blue.gif)";
        }
    }
}

function Show_News_TabSheet(sender, NameForShow, NameForHide, tagNameForClassChange) {
    var divForShow = document.getElementById("div" + NameForShow);
    var tagForShow = document.getElementById(tagNameForClassChange + NameForShow);
    var class_name = sender.className.replace("_active", "");
    if (class_name == tagNameForClassChange)
        class_name = "";
    if (divForShow) {
        if (NameForHide) {
            var divForHide = document.getElementById("div" + NameForHide);
            var tagForHide = document.getElementById(tagNameForClassChange + NameForHide);
            divForHide.style.display = "none";
            divForShow.style.visibility = "hidden";
            tagForHide.className = class_name;
        }
        if (divForShow.style.display == "none") {
            divForShow.style.display = "block";
            divForShow.style.visibility = "visible";
            if (class_name)
                tagForShow.className = class_name + "_active";
            else
                tagForShow.className = tagNameForClassChange + "_active";
        }
        else {
            divForShow.style.display = "none";
            divForShow.style.visibility = "hidden";
            tagForShow.className = class_name;
        }
    }
}

function Expand_Or_CollapseTabSheet(sender, otherSenderIDForHide, IDForShow, IDForHide, sndr_css_active, sndr_css_inactive, trgt_css_active, trgt_css_inactive,
    divAdditID, addit_css_inactive, addit_css_active) {
    var divForExpand = document.getElementById(IDForShow);
    var divForClose = document.getElementById(IDForHide);
    var divAddit = document.getElementById(divAdditID);
    if (divForExpand.style.display == 'none') {
        divForExpand.style.display = "block";
        divForExpand.className = trgt_css_active;
        if (sender) {
            sender.className = sndr_css_active;
        }
        var divForClose = document.getElementById(IDForHide);
        var sndrForClose = document.getElementById(otherSenderIDForHide);
        if (divForClose) {
            divForClose.className = trgt_css_inactive;
            divForClose.style.display = "none";
        }
        if (sndrForClose) {
            sndrForClose.className = sndr_css_inactive;
        }
        if (divAddit) {
            divAddit.className = addit_css_active;
        }
    }
    else {
        divForExpand.style.display = "none";
        divForExpand.className = trgt_css_inactive;
        if (sender) {
            sender.className = sndr_css_inactive;
        }
        if (divAddit) {
            divAddit.className = addit_css_inactive;
        }
    }
}
function Switch_TabSheet(NameForShow, NameForHide, _btnForHide, _btnForShow, btnForHideCss, btnForShowCss) {
    var divForShow = document.getElementById(NameForShow);
    var divForHide = document.getElementById(NameForHide);
    var btnForShow = document.getElementById(_btnForShow);
    var btnForHide = document.getElementById(_btnForHide);
    divForHide.style.display = "none";
    divForHide.style.visibility = "hidden";
    btnForHide.className = btnForHideCss;
    divForShow.style.display = "block";
    divForShow.style.visibility = "visible";
    btnForShow.className = btnForShowCss;
}
//Показ всплывающего div-а
function ShowDiv(divID) {
    var div = document.getElementById(divID);
    if (div) {
        var x1 = (document.all) ? window.event.x + document.body.scrollLeft : undefined;
        var y1 = (document.all) ? window.event.y + document.body.scrollTop : undefined;

        div.style.left = (x1 + 20) + "px";
        div.style.top = (y1) + "px";
        div.style.display = "block";
        div.style.visibility = "visible";
    }
}
function ShowDivWithFocus(divID, inputID) {
    var div = document.getElementById(divID);
    if (div) {
        div.style.display = "block";
        div.style.visibility = "visible";
        var inpt = document.getElementById(inputID);
        if (inpt)
            inpt.focus();

    }
}
function HideDiv(divID) {
    var div = document.getElementById(divID);
    if (div) {

        div.style.display = "none";
        div.style.visibility = "hidden";
    }
}
function ExpandOrCollapseTag(id) {
    var tag = document.getElementById(id);
    if (tag) {
        if (tag.style.display == 'none')
            tag.style.display = "block";
        else
            tag.style.display = "none";
    }
}


var ajaxTargetCtrlID;
function Return_AjaxResult() {

    if (req.readyState == 4) {

        document.getElementById(ajaxTargetCtrlID).innerHTML = req.responseText;
        //alert(req.responseText);
        delete req;
    }

}
function Blank_AjaxResult() {

    if (req.readyState == 4) {
        delete req;
    }

}
var req;
function connect(url, u_function) {
    if (req != null && req.readyState != 4) {
        alert("Подождите. Обрабатывается предыдущий запрос.");
        return false;
    }
    if (!u_function)
        u_function = 'Return_AjaxResult';
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = eval(u_function);
        req.open("GET", url, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = eval(u_function);
            req.open("GET", url, true);
            req.send();
        } else {
            alert('Не удалось создать экземпляр объекта: ActiveXObject.');
        }
    }
}
function GetAjaxDDL(div, url) {
    if (req != null && req.readyState != 4) {
        alert("Подождите. Обрабатывается предыдущий запрос.");
        return false;
    }
    ajaxTargetCtrlID = div;
    connect(url);
}


function SetImgToControl(sender, imgID, path, width, height) {
    var img = document.getElementById(imgID);
    if (img) {
        img.src = '/img.ashx?Img=' + path + sender.value + '.jpg' + '&width=' + width + '&height=' + height;
    }
}
function CheckDDLSelection(divID, ddlID) {
    var div = document.getElementById(divID);
    var ddl = document.getElementById(ddlID);
    if (div && ddl) {
        if (parseInt(ddl.value) == -1) {
            div.style.display = "block";
        }
        else
            div.style.display = "none";
    }
}
function SelectAllCheckBoxes(sender, name) {
    var list = document.getElementsByName(name);
    for (var i = 0; i < list.length; i++) {
        list[i].checked = sender.checked;
    }
}


function GoToSearch(id, path, paramName) {
    var tb = document.getElementById(id);
    //alert(path + '?search=' + tb.value);
    if (!paramName)
        window.location = path + '?search=' + tb.value;
    else
        window.location = path + '?' + paramName + '=' + escape(tb.value);
    return false;
}

function SetValToAjaxHidd(val, hiddID, str, spanID) {
    var hidd = document.getElementById(hiddID);
    if (hidd)
        hidd.value = val;
    var span = document.getElementById(spanID);
    if (span)
        span.innerHTML = str;
}

function getOffsetTop(win) {
    if (!win)
        win = window;
    if (win.innerWidth)  // All browsers but IE
        return win.pageYOffset;
    else if (win.document.documentElement && win.document.documentElement.clientWidth)
        return win.document.documentElement.scrollTop;
    else if (win.document.body.clientWidth) // These are for IE4, IE5, and IE6 without a DOCTYPE
        return win.document.body.scrollTop;
}

function GetSize(cntrl) {
    var width = 0, height = 0;
    if (cntrl.clientWidth) {
        width = cntrl.clientWidth;
        height = cntrl.clientHeight;
    }
    else
        if (cntrl.offsetWidth) {
        width = cntrl.offsetWidth;
        height = cntrl.offsetHeight;
    }
    else
        if (cntrl.style && parseInt(cntrl.style.width)) {
        width = parseInt(cntrl.style.width);
        if (parseInt(cntrl.style.height))
            height = parseInt(cntrl.style.height);
    }
    if (!width)
        width = 200;
    return { w: width, h: height };
}
function AddHtmlElem(cntrlParent, tag, id, type) {
    var result = document.createElement(tag);
    if (id)
        result.id = id;

    if (type)
        result.type = type;
    cntrlParent.appendChild(result);
    return result;
}

function Round(num) {
    var result = Math.floor(num);
    result += Math.round((num - result) * 100) / 100.0;
    return result;

}
function trim(t) {
    return t.replace(/^\s+|\s+$/g, "");
}