﻿function GetRootPath() {
    var path = document.getElementById('MasterScripts').src;
    return path.split('/').slice(0, -1).join('/') + '/';
}
function OnHelpClick(element, url) {
    IframePopupControl.SetContentUrl(url);
    IframePopupControl.SetSize(730, 710);
    IframePopupControl.ShowAtElement(element);
}
function OnAddressMapClick(element, partnumber, class_man, location) {
    var url = GetRootPath() + 'addressesonmap.aspx?partnumber=' + partnumber;
    if (class_man != '') url += '&class_man=' + class_man;
    if (location != '') url += '&location=' + location;
    url += '&rnd=' + Math.random();

    IframePopupControl.SetContentUrl(url);
    IframePopupControl.SetHeaderText('Адерса продавцов на карте...');
    IframePopupControl.SetSize(730, 710);
    IframePopupControl.ShowAtElement(element);
}
function OnUserInfoClick(element, code_user, code_doc_b, code_addr, code_cat, mini, tab) {
    var url = GetRootPath() + 'userinfo.aspx?code_user=' + code_user + '&code_doc_b=' + code_doc_b + '&code_addr=' + code_addr + '&code_cat=' + code_cat + '&tab=' + tab;

    if (mini == 1) {
        url += '&mini=1';
        MiniIframePopupControl.SetHeaderText('Контакты продавца...');
        MiniIframePopupControl.SetContentUrl(url);
        MiniIframePopupControl.ShowAtElement(element);
    }
    else {
        IframePopupControl.SetHeaderText('Контакты продавца...');
        IframePopupControl.SetSize(700, 700);
        IframePopupControl.SetContentUrl(url);
        IframePopupControl.ShowAtElement(element);
    }
}
function OnImageClick(element, imagepath) {
    IframePopupControl.SetContentUrl(GetRootPath() + 'catalogimage.aspx?imagepath=' + escape(trim(imagepath)));
    IframePopupControl.ShowAtElement(element);
}
function OnCatalogClick(element, code) {
    if (code == null || code == -1) return;
    window.open(GetRootPath() + 'redirect.aspx?code_man=' + code + '&target=link1');
}
function OnSparesClick(element, code) {
    if (code == null || code == -1) return;
    window.location = GetRootPath() + 'spares.aspx?code_man=' + code;
}
function OnRedirClick(element, code) {
    if (code == null || code == -1) return;
    window.open(GetRootPath() + 'redirect.aspx?code_doc_b=' + code + '&target=redir');
}
function OnAddressesClick(element) {
    CustomIframePopupControl.SetContentUrl(GetRootPath() + 'user/addresses.aspx?rnd=' + Math.random());
    CustomIframePopupControl.ShowAtElement(element);
}
function OnIpsClick(element, code_stat_cust) {
    IframePopupControl.SetContentUrl(GetRootPath() + 'user/ips.aspx?code_stat_cust=' + code_stat_cust);
    IframePopupControl.ShowAtElement(element);
}

function GetUrlParam(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}

function trim(string) {
    return string.replace(/(^\s+)|(\s+$)/g, '');
}

var nullComboText = '(выбрать)';

function OnComboLostFocus(s, e) {
    if (s.GetValue() != null)
        return;

    var input = s.GetInputElement();
    input.style.color = 'gray';
    input.value = nullComboText;
}
function OnComboGotFocus(s, e) {
    var input = s.GetInputElement();
    if (input.value == nullComboText) {
        input.style.color = 'black';
    }
}
function OnComboInit(s, e) {
    OnComboLostFocus(s, e);
}

function OnFloatValidate(s, e) {
    var number = ParseFloat(s.GetValue());
    if (number == null) {
        e.value = null;
        e.isValid = false;
        e.errorText = 'Ошибка в формате поля.';
    }
}
function ParseFloat(strValue) {
    var number = parseFloat(strValue);
    return !isNaN(number) ? number : null;
}
function OnIntValidate(s, e) {
    var number = ParseInt(s.GetValue());
    if (number == null) {
        e.value = null;
        e.isValid = false;
        e.errorText = 'Ошибка в формате поля.';
    }
}
function ParseInt(strValue) {
    var number = parseInt(strValue);
    return !isNaN(number) ? number : null;
}

function getXmlHttp() {
    var xmlhttp;
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        xmlhttp = new XMLHttpRequest();
    }
    return xmlhttp;
}

function getCookie(name) {
    var cookie = " " + document.cookie;
    var search = " " + name + "=";
    var setStr = null;
    var offset = 0;
    var end = 0;
    if (cookie.length > 0) {
        offset = cookie.indexOf(search);
        if (offset != -1) {
            offset += search.length;
            end = cookie.indexOf(";", offset)
            if (end == -1) {
                end = cookie.length;
            }
            setStr = unescape(cookie.substring(offset, end));
        }
    }
    return (setStr);
}

function setCookie(name, value, days, path, domain, secure) {
    var expires = '';

    var date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    expires = date.toGMTString();

    document.cookie = name + '=' + escape(value) +
        ((expires) ? '; expires=' + expires : '') +
        ((path) ? '; path=' + path : '') +
        ((domain) ? '; domain=' + domain : '') +
        ((secure) ? '; secure' : '');
}

function isNumber(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
}
