﻿// 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, '');
    var text;
    if (arguments[3]) {
      text = open + arguments[3] + "]" + curSelection + end;
    } else {
      text = open + curSelection + end;
    }
    var pos = textBox.selectionStart + text.length;
    textBox.value = textBefore + text + textAfter;
    textBox.setSelectionRange(pos, pos);
    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;
    var pos = selStart + text.length;
    textBox.setSelectionRange(pos, pos);
    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, innCtlId, ownershipCtlId, firmNameCtlId) {
  window.retText = document.getElementById(nameCtlId);
  window.retFirmID = document.getElementById(idCtlId);
  window.retInn = document.getElementById(innCtlId);
  window.retOwnershipId = document.getElementById(ownershipCtlId);
  window.retFirmName = document.getElementById(firmNameCtlId);
  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;
  }
}

Math.lg = function(value) {
  return Math.log(value) / Math.log(2);
}

var setBitByNumber = function(value, flag) {
  var powerOf2 = Math.round(Math.lg(flag));
  if (isNaN(powerOf2) == false) {
    return setBitByIndex(value, powerOf2);
  }
  return value;
}

var setBitByIndex = function(value, index) {
  if (isNaN(value) == false && isNaN(index) == false) {
    var binValueAsString = value.toString(2);
    var result = "";
    var length = Math.max(binValueAsString.length - 1, index);
    for (var i = 0; i <= length; i++) {
      var currentBit = binValueAsString[i];
      if (isNaN(currentBit) == true) {
        result = "0" + result;
      } else {
        result += currentBit;
      }
    }
    //set bit
    var indexInString = result.length - index;
    result = result.substr(0, indexInString - 1) + "1" + result.substr(indexInString, result.length);
    return parseInt(result, 2);
  }
  return value;
}

function imposeMaxLength(elem, e, maxlength) {
  if (maxlength > 0) {
    //Get the event object (for IE)  
    var ob = e || event;
    //Get the code of key pressed  
    var keyCode = ob.keyCode;
    //Check if it has a selected text
    var hasSelection = document.selection ? document.selection.createRange().text.length > 0 : elem.selectionStart != elem.selectionEnd;
    //return false if can't write more
    return !(elem.value.length >= maxlength && (keyCode > 50 || keyCode == 32 || keyCode == 0 || keyCode == 13) && !ob.ctrlKey && !ob.altKey && !hasSelection);
  }
  return true;
}

function ExpandCollapseBlock(img, blockId) {
  var div = document.getElementById(blockId);
  if (div.style.display == "none") {
    div.style.display = "block";
    img.src = "/Tpl/images/collapse.gif";
  } else {
    div.style.display = "none";
    img.src = "/Tpl/images/expand.gif";
  }
}
function ExpandCollapseTreeGridRow(sender, colSpan, elementId, bgColor) {
  var c = sender.parentNode;
  var r = c.parentNode;
  var t = r.parentNode;
  var e = document.getElementById(elementId);
  var p = e.parentNode;
  if (e.style.display == "none") {
    var newRow = t.insertRow(r.rowIndex + 1);
    newRow.style.backgroundColor = bgColor;
    var indentCell = newRow.insertCell(0);
    var dataCell = newRow.insertCell(1);
    dataCell.colSpan = colSpan;
    p.removeChild(e);
    dataCell.appendChild(e);
    e.style.display = "block";
    sender.src = "/Tpl/images/collapse.gif";
    c.style.borderBottom = "solid 1px " + bgColor;
    indentCell.style.borderTop = "solid 1px " + bgColor;
  } else {
    e.style.display = "none";
    p.removeChild(e);
    c.appendChild(e);
    t.deleteRow(r.rowIndex + 1);
    sender.src = "/Tpl/Images/expand.gif";
    c.style.borderBottom = c.style.borderTop;
  }
  return false;
}
function getClientWidth() {
  return window.innerWidth || document.documentElement.clientWidth || document.getElementsByTagName('body')[0].clientWidth;
}
function getClientHeight() {
  return window.innerHeight || document.documentElement.clientHeight || document.getElementsByTagName('body')[0].clientHeight;
}

if (!window.SI) { var SI = {}; };
SI.Files =
{
  htmlClass: 'StylizedFileInput',
  fileClass: 'file',
  wrapClass: 'cabinet',
  boundWidth: 116,
  boundHeight: 22,

  fini: false,
  able: false,
  init: function() {
    this.fini = true;
    this.able = true;

    var html = document.getElementsByTagName('html')[0];
    html.className += (html.className != '' ? ' ' : '') + this.htmlClass;
  },

  stylize: function(elem) {
    if (!this.fini) { this.init(); };
    if (!this.able) { return; };

    elem.parentNode.file = elem;
    elem.parentNode.onmousemove = function(e) {
      if (typeof e == 'undefined') e = window.event;
      if (typeof e.pageY == 'undefined' && typeof e.clientX == 'number' && document.documentElement) {
        e.pageX = e.clientX + document.documentElement.scrollLeft;
        e.pageY = e.clientY + document.documentElement.scrollTop;
      };

      var ox = oy = 0;
      var elem = this;
      if (elem.offsetParent) {
        ox = elem.offsetLeft;
        oy = elem.offsetTop;
        while (elem = elem.offsetParent) {
          ox += elem.offsetLeft;
          oy += elem.offsetTop;
        };
      };

      var x = e.pageX - ox;
      var y = e.pageY - oy;
      var w = this.file.offsetWidth;
      var h = this.file.offsetHeight;

      if (x < 0 || x > SI.Files.boundWidth) return;
      if (y < 0 || y > SI.Files.boundHeight) return;

      this.file.style.top = y - (h / 2) + 'px';
      this.file.style.left = x - (w - 30) + 'px';
    };
  },

  stylizeById: function(id) {
    this.stylize(document.getElementById(id));
  },

  stylizeAll: function() {
    if (!this.fini) { this.init(); };
    if (!this.able) { return; };

    var inputs = document.getElementsByTagName('input');
    for (var i = 0; i < inputs.length; i++) {
      var input = inputs[i];
      if (input.type == 'file' && input.className.indexOf(this.fileClass) != -1 && input.parentNode.className.indexOf(this.wrapClass) != -1) {
        this.stylize(input);
      };
    };
  }
};
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();