﻿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 ? sender.className.replace("_active", "") : null;
    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 (tagForShow)
                if (class_name)
                    tagForShow.className = class_name + "_active";
                else
                    tagForShow.className = tagNameForClassChange + "_active";
        }
        else {
            divForShow.style.display = "none";
            divForShow.style.visibility = "hidden";
            if (tagForShow)
                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) {
        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);
    if (divForHide) {
        divForHide.style.display = "none";
        divForHide.style.visibility = "hidden";
    }
    btnForHide.className = btnForHideCss;
    if (divForShow) {
        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) {
        if (div.style.display != 'block') {
            div.style.display = "block";
            div.style.visibility = "visible";
        }
        if (inputID) {
            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";
    }
}
var ajaxTargetCtrlID;
function Return_AjaxResult() {

    if (req.readyState == 4) {

        document.getElementById(ajaxTargetCtrlID).innerHTML = 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 SelectAllChildCheckBoxes(sender, id, val) {
    var elem = document.getElementById(id);
    var list = elem.getElementsByTagName('input');
    for (var i = 0; i < list.length; i++) {
        if (sender == null)
            list[i].checked = val;
        else
            list[i].checked = sender.checked;
    }
}
function GetSelectedCheckBoxes(id) {
    var cbl = document.getElementById(id);
    var list = cbl.getElementsByTagName('input');
    var result = new Array();
    for (var i = 0; i < list.length; i++) {
        if (list[i].checked) result.push(list[i]);
    }
    return result;
}
function GetSelectedCheckBoxesValues(id) {
    var cbl = document.getElementById(id);
    var list = cbl.getElementsByTagName('input');
    var result = new Array();
    for (var i = 0; i < list.length; i++) {
        if (list[i].checked) result.push(list[i].value);
    }
    return result;
}


function GoToSearch(id, path, paramName, hiddCountryID, hiddCityID, appendix) {
    var tb = document.getElementById(id);
    var tmp = '';
    if (hiddCountryID) {
        var hiddCountry = document.getElementById(hiddCountryID);
        if (hiddCountry && parseInt(hiddCountry.value) > 0) {

            tmp = '&CountryID=' + hiddCountry.value;
            if (hiddCityID) {
                var hiddCity = document.getElementById(hiddCityID);
                if (hiddCity && parseInt(hiddCity.value) > 0) {
                    tmp += '&CityID=' + hiddCity.value;
                }
            }
        }

    }
    //alert(path + '?search=' + tb.value);
    if (!paramName)
        window.location = path + '?search=' + tb.value;
    else {
        if (appendix)
            tmp += appendix;
        window.location = path + '?' + paramName + '=' + escape(tb.value) + tmp;
    }
    return false;
}

function SetValToAjaxHidd(val, hiddID, str, spanID, rel_url, rel_url_param) {
    if (rel_url && rel_url_param) {
        document.location = unescape(rel_url) + '&' + rel_url_param + '=' + val;
        return;
    }
    var hidd = document.getElementById(hiddID);
    if (hidd)
        hidd.value = val;
    var span = document.getElementById(spanID);
    if (span)
        span.innerHTML = str;
}
function SetValToHidd(val, hiddID) {
    var hidd = document.getElementById(hiddID);
    if (hidd)
        hidd.value = val;
}

function AddHtmlElem(cntrlParent, tag, id, type) {
    var result = document.createElement(tag);
    result.id = id;
    if (type)
        result.type = type;
    cntrlParent.appendChild(result);
    return result;
}

function FindByID(arr, id) {
    for (var i = 0; i < arr.length; i++)
        if (arr[i].ID == id)
            return arr[i];
    return null;
}

function IndexByValue(arr, val) {
    for (i = 0; i < arr.length; i++)
        if (arr[i] == val)
            return i;
    return -1;
}

function OpenModalWindow(url, modal_params, wnd_params) {
    window.showModalDialog ? window.showModalDialog(url, window, modal_params) : window.open(url, '', wnd_params);
    return false;
}

function GetContainer(container) {
    return document.getElementById(container);
}

var windowSize = { width: screen.width, height: screen.height };

function registerLoad(js) {
    if (window.addEventListener)
        window.addEventListener("load",
      js,
       false);
    else if (window.attachEvent) window.attachEvent("onload", js);
    else window.onload = js;

}
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 FillContainerAccordingToOther(selector1, selector2Parent, selector2Elems, spanShowID, spanHideID) {
    var width = $(selector2Parent).width();
    var height = $(selector1).height();
    var elem=$(selector2Elems).eq(0);
    var elemHeight = elem.height();
    var elemWidth = elem.width();
    if (parseInt(elem.css('marginBottom')) > 0)
        elemHeight += parseInt(elem.css('marginBottom'));
    if (parseInt(elem.css('marginRight')) > 0)
        elemWidth += parseInt(elem.css('marginRight'));
    var elemsToShow = Math.floor(width / elemWidth) * Math.floor(height / elemHeight) - 1;
    if (elemsToShow < 11)
        elemsToShow = 11;
    $(selector2Elems + ':gt(' + elemsToShow + ')').hide();
    $("#"+spanShowID).click(function () {
        $(selector2Elems + ':gt(' + elemsToShow + ')').show();
    });
    $("#"+spanHideID).click(function () {
        $(selector2Elems + ':gt(' + elemsToShow + ')').hide();
    });
}