///////////////////////////////////////////////////////////////////////////////
//
// Common JavaScript Utilities v1.0
//
// v1.0: created by Nikolay Basov 17.01.2007
//
///////////////////////////////////////////////////////////////////////////////

// ??????????? ???????? ???????
isDOM = document.getElementById 				       //DOM1 browser (MSIE 5+, Netscape 6, Opera 5+)
isOpera = isOpera5 = window.opera && isDOM 		       //Opera 5+
isOpera6 = isOpera && window.print 			           //Opera 6+
isOpera7 = isOpera && document.readyState 	           //Opera 7+
isMSIE = document.all && document.all.item && !isOpera //Microsoft Internet Explorer 4+
isMSIE5 = isDOM && isMSIE 				               //MSIE 5+
isNetscape4 = document.layers 				           //Netscape 4.*
isMozilla = isDOM && navigator.appName == "Netscape"   //Mozilla ??? Netscape 6.*

// ????????? ??????????? ??????? Escape ?? ??????????
var trans = [];
for (var i = 0x410; i <= 0x44F; i++)
  trans[i] = i - 0x350; // ?-??-?
trans[0x401] = 0xA8;    // ?
trans[0x451] = 0xB8;    // ?

var escapeOrig = window.escape;

window.escape = function(str)
{
  var ret = [];
  for (var i = 0; i < str.length; i++)
  {
    var n = str.charCodeAt(i);
    if (typeof trans[n] != 'undefined')
      n = trans[n];
    if (n <= 0xFF)
      ret.push(n);
  }
  return escapeOrig(String.fromCharCode.apply(null, ret));
}

// ?????????? ?????. ????????? ????? ????????? ????????. 
function FindControl(controlId, parentId)
{
  var ctl;
  if (controlId)
  {
    ctl = document.getElementById(controlId);  
    if (!ctl) throw '??????? '+ controlId +' ?? ??????';
  } 
  else if (parentId) 
  {
    ctl = document.getElementById(parentId);  
    if (!ctl) throw '??????? '+ parentId +' ?? ??????';
  } 
  else throw '??????? ?? ?????';

  return ctl;
}

// ????? ?????????????? ?????? ??? ?????????? ??????? ? ????????? ???????????? 
// ?? ???????? ??????. Uri ????? ???: '/search/_t_/search='.
function ProcessSearch(uri, controlId, parentId)
{
  try
  {
    var ctl = FindControl(controlId, parentId);   
    if (ctl.value != '')
    {
      var value = ctl.value;
      value=value.replace(/&|and|\+/g,' ');
      value=value.replace(/^\s+|\s+$/g,'');
      value=value.replace(/\s+/g,'+');
      location.href = uri+escape(value);
      return false;
    }
  
    return false; 
  }
  catch (ex)
  { 
    alert('?????? ProcessSearch: ' + ex.message);
  }
}

// ????? ?? ??????? ??????? Enter ????????? ????????, ?????????? ?? ???????
function ProcessSearchEnter(e, controlId, parentId)
{
  try
  {
    var ctl = FindControl(controlId, parentId);   

    var key = navigator.appName == 'Netscape' ? e.which : e.keyCode;
    if (key == 13)
      ctl.click();
  }
  catch (ex)
  {
    alert('?????? ProcessSearchEnter: ' + ex.message);
  }
}

// ????? ????????? ????????? ???????? ??????? ? ??????? OnLoad
function SetGlobalOnload(f) 
{
  var root = window.addEventListener || window.attachEvent ? window : document.addEventListener ? document : null
  if (root)
  {
    if(root.addEventListener) root.addEventListener("load", f, false)
    else if(root.attachEvent) root.attachEvent("onload", f)
  } 
  else 
  {
    if(typeof window.onload == 'function') 
    {
      var existing = window.onload
      window.onload = function() 
      {
        existing()
        f()
      }
    } 
    else 
    {
      window.onload = f
    }
  }
}

// ????? ?????? ?? ????? ?????????? ????????? ???????
function AddEventHandler(object, event, handler)
{
  if (typeof object.addEventListener != 'undefined')
    object.addEventListener(event, handler, false);
  else if (typeof object.attachEvent != 'undefined')
    object.attachEvent('on' + event, handler);
  else
    alert('?????? AddEventHandler: ????????????? ???????');
}

// ????? ????????????? ????? ?? ????????? ????????
function FocusControl(controlId, parentId)
{
  try
  {
    var ctl = FindControl(controlId, parentId);   
    ctl.focus();
  }
  catch (ex)
  { 
    alert('?????? FocusControl: ' + ex.message);
  }
}
    


