﻿// Do not save this script in any encoding except UTF-8
function CloseWindow(p_sUrl) {
    if (p_sUrl != null &&
        p_sUrl != '') {
        try {
            if (window.opener && window.opener.location)
                window.opener.location.href = p_sUrl;
        } catch (e) { }
    }
    window.close();
    return false;
}

function ReloadOpenerAndClose() {
    var sUrl = '';
    try {
        sUrl = window.opener.location.href;
    }
    catch (e) { }
    CloseWindow(sUrl);
}

function ReloadOpenerAndCloseEx(anchor) {
    try {
      if (window.opener && window.opener.location) {
        url = window.opener.location.href;
        idx = url.indexOf("#");
        if (idx > -1) url = url.substring(0, idx);
        window.opener.location.href = url + anchor;
        window.opener.location.reload(true);
        window.opener.location.href = url + anchor;
      }
    }
    catch (e) { }
    window.close();
}

function newGuid() {
    var g = "{";
    for (var i = 0; i < 32; i++)
        g += Math.floor(Math.random() * 0xF).toString(0xF) + (i == 8 || i == 12 || i == 16 || i == 20 ? "-" : "");
    return g + "}";
}

function HideContact(p_iID) {
    if (!isNaN(p_iID) &&
        p_iID > 0) {
        var element = $get('Contact_' + p_iID.toString());
        if (element != null)
            element.style.display = 'none';
    }
}

// MAX_SELECTED <= 0 means no restriction
function AddToCommaSeparatedString(strCSV, ID, delimiter, MAX_SELECTED) {
    var retVal = new String();
    retVal = strCSV;

    if ((delimiter + retVal + delimiter).indexOf(delimiter + ID + delimiter) != -1)
        return retVal;
    else {
        if (MAX_SELECTED > 0) {
            // no more than MAX_SELECTED
            var productsChecked = 0;
            if (retVal.length > 0)
                productsChecked = retVal.split(delimiter).length;
            if (productsChecked >= MAX_SELECTED)
                return (retVal);
        }

        // add to selected
        if (retVal.length > 0)
            retVal += delimiter;
        retVal += ID;
    }
    return (retVal);
}

///
function RemoveFromSeparatedString(strCSV, ID, delimiter) {
    var retVal = new String();
    retVal = strCSV;

    if ((delimiter + retVal + delimiter).indexOf(delimiter + ID + delimiter) != -1) {
        retVal = delimiter + strCSV + delimiter;
        retVal = retVal.replace((delimiter + ID), "");

        retVal = retVal.replace(delimiter + delimiter, delimiter);
        if (retVal.charAt(retVal.length - 1) == delimiter)
            retVal = retVal.substring(0, retVal.length - 1);
        if (retVal.indexOf(delimiter) == 0)
            retVal = retVal.substring(1, retVal.length);
    }
    return (retVal);
}

///
function trim(s) {
    while (s.substring(0, 1) == ' ')
        s = s.substring(1, s.length);
    while (s.substring(s.length - 1, s.length) == ' ')
        s = s.substring(0, s.length - 1);
    return s;
}

function ClearInputControls(p_RootControl) {
    if (document.getElementById(p_RootControl.id)) {
        var InputControls = p_RootControl.getElementsByTagName('input');
        for (var i = 0; i < InputControls.length; i++) {
            try {
                switch (InputControls[i].type) {
                    case "text":
                    case "hidden":
                        DomManager.setValue(InputControls[i], "");
                        break;
                    case "checkbox":
                        InputControls[i].checked = false;
                        break;
                }
            }
            catch (e) {
            }
        }
    }
}

function ClearSelectContols(p_RootControl) {
    if (document.getElementById(p_RootControl.id)) {
        var SelectControls = p_RootControl.getElementsByTagName('select');
        for (var i = 0; i < SelectControls.length; i++) {
            try {
                SelectControls[i].selectedIndex = 0;
            }
            catch (e) {
            }
        }
    }
}

function ClearAllControls(p_RootControl) {
    ClearSelectContols(p_RootControl);
    ClearInputControls(p_RootControl);
}

function BBCode(id, open, end) {
  var textBox = $get(id);
  var isIE = (document.all) ? true : false;
  var open = (open) ? open : "";
  var end = (end) ? end : "";
  if (isIE) {
    textBox.focus();
    var curSelect = document.selection.createRange();
    if (arguments[3]) {
      curSelect.text = open + arguments[3] + "]" + curSelect.text + end;
    } else {
      curSelect.text = open + curSelect.text + end;
    }
  } else if (typeof textBox.selectionStart != "undefined") {
    var selStart = textBox.selectionStart;
    var scrollTop = textBox.scrollTop;
    var textBefore = textBox.value.substr(0, selStart);
    var textAfter = textBox.value.substr(textBox.selectionEnd, textBox.value.length);
    var curSelection = textBox.value.replace(textBefore, '').replace(textAfter, '');
    if (arguments[3]) {
      textBox.value = textBefore + open + arguments[3] + "]" + curSelection + end + textAfter;
    } else {
      textBox.value = textBefore + open + curSelection + end + textAfter;
    }
    textBox.setSelectionRange(selStart, selStart);
    textBox.scrollTop = scrollTop;
  } else {
    textBox.value += (arguments[3]) ? open + arguments[3] + "]" + end : open + end;
  }
  textBox.focus();
  return false;
}

function HiliteButton(btn, hilite) {
  if (hilite) {
    btn.style.border = "solid 1px #000080";
    btn.style.backgroundColor = "#C0C0FF";
  } else {
    btn.style.border = "none";
    btn.style.backgroundColor = "Transparent";
  }
}

function InternalInsertText(textBox, text) {
  var isIE = (document.all) ? true : false;
  if (isIE) {
    textBox.focus();
    var curSelect = document.selection.createRange();
    curSelect.text = text;
  }
  else if (typeof textBox.selectionStart != "undefined") {
    var selStart = textBox.selectionStart;
    var scrollTop = textBox.scrollTop;
    var textBefore = textBox.value.substr(0, selStart);
    var textAfter = textBox.value.substr(textBox.selectionEnd, textBox.value.length);
    var curSelection = textBox.value.replace(textBefore, '').replace(textAfter, '');
    textBox.value = textBefore + text + textAfter;
    textBox.setSelectionRange(selStart, selStart);
    textBox.scrollTop = scrollTop;
  }
  else
    textBox.value += text;
  textBox.focus();
  return false;
}
  
function InsertText(id, text)
{
  var textBox = $get(id);
  return InternalInsertText(textBox, text);
}

function InsertTextEx(wnd, id, text)
{
  var textBox = wnd.document.getElementById(id);
  return InternalInsertText(textBox, text);
}

function InsertURL(id) {
  var url = prompt("Введите полный адрес ссылки:", "http://");
  if (url != null) {
    if (url != "" && url != "http://") {
      var name = prompt("Введите название ссылки:", "");
      if (name != null) {
        if (name == "")
          name = url;
        var link = '[a href="' + url + '"]' + name + '[/a]';
        InsertText(id, link);
      }
    }
    else
      alert("Ошибка. Введите полный адрес.");
  }
  return false;
}

function InsertImage(id) {
  var url = prompt("Введите полный адрес картинки:", "http://");
  if (url != null) {
    if (url != "" && url != "http://") {
      var link = '[img src="' + url + '"]' + '&nbsp;';
      InsertText(id, link);
    }
    else
      alert("Ошибка. Введите полный адрес.");
  }
  return false;
}

function DisableIfChecked() {
  var chk = document.getElementById(arguments[0]);
  for (i = 1; i < arguments.length; i++) {
    var e = document.getElementById(arguments[i]);
    if (e != null)
      e.disabled = chk.checked;
  }
}

function UncheckIfChecked(chk, chkToUncheck) {
  if (document.getElementById(chk).checked)
    document.getElementById(chkToUncheck).checked = false;
}

function CheckIfValueGreaterThanZero(txtId, chkId) {
  var chk = document.getElementById(chkId);
  var txt = document.getElementById(txtId);
  var val = parseInt(txt.value, 10);
  chk.checked = (!isNaN(val) && val > 0);
  return false;
}

function CheckIfHasValue(txtId, chkId) {
  var chk = document.getElementById(chkId);
  var txt = document.getElementById(txtId);
  chk.checked = txt.value.trim() != "";
  return false;
}

function InsertTextAndClose(id, text)
{
  if (window.opener != null)
    InsertTextEx(window.opener, id, text);
  window.close();
}

function GetSelText()
{
  var txt = "";
  if (window.getSelection)
    txt = window.getSelection();
  else if (document.getSelection)
    txt = document.getSelection();
  else if (document.selection)
    txt = document.selection.createRange().text;
  return txt;
}

function ChooseFirm(nameCtlId, idCtlId) {
  window.retText = document.getElementById(nameCtlId);
  window.retFirmID = document.getElementById(idCtlId);

  popup = window.open('/PickFirm.aspx?mode=select', 'pickfirm', 'width=520,height=600,left=' + ((screen.width - 520) / 2) + ',top=' + ((screen.height - 600) / 2) + ',scrollbars=yes,resizable');
  popup.focus();
  return false;
}

function ExpandForum(sender, id, container) {
  var div = document.getElementById(container + id.toString());
  if (div.style.display == "none") {
    div.style.display = "block";
    sender.src = "/Tpl/images/collapse.gif";
  } else {
   div.style.display = "none";
   sender.src = "/Tpl/images/expand.gif";
  }
  return false;
}

var Base64 = {

  _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

  decode: function(input) {
    var output = "";
    var chr1, chr2, chr3;
    var enc1, enc2, enc3, enc4;
    var i = 0;

    input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

    while (i < input.length) {
      enc1 = this._keyStr.indexOf(input.charAt(i++));
      enc2 = this._keyStr.indexOf(input.charAt(i++));
      enc3 = this._keyStr.indexOf(input.charAt(i++));
      enc4 = this._keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output = output + String.fromCharCode(chr1);

      if (enc3 != 64) {
        output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
        output = output + String.fromCharCode(chr3);
      }
    }
    output = Base64._utf8_decode(output);
    return output;
  },

  _utf8_decode: function(utftext) {
    var string = "";
    var i = 0;
    var c = c1 = c2 = 0;

    while (i < utftext.length) {
      c = utftext.charCodeAt(i);
      if (c < 128) {
        string += String.fromCharCode(c);
        i++;
      }
      else if ((c > 191) && (c < 224)) {
        c2 = utftext.charCodeAt(i + 1);
        string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
        i += 2;
      }
      else {
        c2 = utftext.charCodeAt(i + 1);
        c3 = utftext.charCodeAt(i + 2);
        string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
        i += 3;
      }
    }
    return string;
  }
}
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();