function $(id) { return document.getElementById(id); }
function _(fname, el) { return (el != undefined) ? document.forms[fname].elements[el] : document.forms[fname]; }
function Hide(id) { if ($(id)) $(id).style.display='none'; return false; }
function Show(id) { if ($(id)) $(id).style.display=''; return false; }

if(typeof String.prototype.trim !== 'function') String.prototype.trim = new Function("return this.replace(/^\\s+|\\s+$/g,'')");
String.prototype.URI = new Function("return encodeURIComponent(this)");
function setPos(obj, x, y) { obj.style.left = x+'px'; obj.style.top = y+'px'; }
function getPos() {
  if (isNaN(parseInt(this.style.left))) this.style.left = '0px';
  if (isNaN(parseInt(this.style.top))) this.style.top = '0px';
  return {x : parseInt(this.style.left), y : parseInt(this.style.top)};
}


function setCookie (name, value, path) {
  if (path == undefined) path=""; else path="; path="+path;
  var expDate=new Date();
  expDate.setTime(expDate.getTime()+7776000000); //3 month
  document.cookie=name+"="+escape(value)+"; expires="+expDate.toGMTString()+path;
}

function getCookie(name) {
  var cList = document.cookie.split("; ");
  for (var i=0; i<cList.length; i++) {
    var val = cList[i].split("=");
    if (val[0] == name)
      return unescape(val[1]);
  }
  return '';
}


//is string empty?
function testS(obj, err) {
  obj.value.trim();
  if (obj.value.length == 0) {
    alert(err);
    obj.focus();
    return false;
  } else
    return true;
}

//is string email?
function is_email(str) {
  return str.match(/^[a-z-_0-9\.]+@[a-z-_=>0-9\.]+\.[a-z]{2,4}$/i);
}

//is keypressed integer?
function testN(event) {
  if (((event.keyCode<48) || (event.keyCode>57)) && event.keyCode!=13 && event.keyCode!=8 && event.keyCode!=37 && event.keyCode!=39 && event.keyCode!=46 && event.keyCode!=36 && event.keyCode!=35)
    event.returnValue=false;
}


//AJAX multithread objects
function AjaxReqObj(url, reqFunc, uri) {
  this.send=sendAjax;
  this.url=url;
  this.reqFunc = reqFunc;
  this.uri=uri;
}

function sendAjax() {
  try {
    this.r=new ActiveXObject('Msxml2.XMLHTTP');
  } catch (e) {
    try {
      this.r=new ActiveXObject('Microsoft.XMLHTTP'); //Internet Explorer
    } catch (e) {
      if (window.XMLHttpRequest) //Mozilla & Safari
        this.r=new XMLHttpRequest();
    }
  }

  if (this.r) {
    var self = this;
    this.r.onreadystatechange = function() {
      if (self.r.readyState == 4 && self.r.status == 200) {
        self.reqFunc(self.r.responseText);
        delete self.r;
      }
    };

    if (this.uri.length > 0) {
      this.r.open('POST', this.url, true);
      this.r.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      this.r.send(this.uri);
    } else {
      this.r.open('GET', this.url, true);
      this.r.send(null);
    }
    return true;
  }
  return false;
}

function loadDoc(url, processReqFunc, uri) {
  if (!processReqFunc) processReqFunc = processReq;
  if (!uri) uri = '';
  var ajaxreq = new AjaxReqObj(url, processReqFunc, uri);
  ajaxreq.send();
  return false;
}

//AJAX JavaScript Load
function loadJS(url, uri) {
  loadDoc(url, processJSReq, uri);
  return false;
}

function processJSReq(str) {
  if (str!='') eval(str);
}


//get global element position
function getGlobalPos(el) {
  var p = {x:0,y:0};
  while (el) {
    p.x += el.offsetLeft;
    p.y += el.offsetTop;
    el = el.offsetParent;
  }
  return p;
}

//Add/Remove event crossbrowser
function addEvent(obj,evt,fn) {
	if (obj.addEventListener) obj.addEventListener(evt,fn,false);
	else if (obj.attachEvent) obj.attachEvent('on'+evt,fn);
}

function removeEvent(obj,evt,fn) {
	if (obj.removeEventListener) obj.removeEventListener(evt,fn,false);
	else if (obj.detachEvent)	obj.detachEvent('on'+evt,fn);
}

//Упрощенный кейхук для всех страниц
function SimpleInitPages() {
  document.onkeydown = SimpleKeyHook;
}

function SimpleKeyHook(e) {
  var code;
  if (!e) var e = window.event;
  if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;

  if ((code == 37) && (e.ctrlKey == true)) {
    var dest = $('prev');
    if (dest) location.href = dest.href;
  } else if ((code == 39) && (e.ctrlKey == true)) {
    var dest = $('next');
    if (dest) location.href = dest.href;
  }
}

function addBookmark() {
  var title = 'ChipFind - поисковая система';
  var url = 'http://www.chipfind.ru';

  if (window.sidebar) // Firefox
    window.sidebar.addPanel(title, url,'');
  else if (window.opera){ //Opera
    var a = document.createElement("A");
    a.rel = "sidebar";
    a.target = "_search";
    a.title = title;
    a.href = url;
    a.click();
  } else if (document.all) //IE
    window.external.AddFavorite(url, title);
  else
    alert('Извините, Ваш браузер не поддерживает функцию добавления закладок.');
}

function ssubm(obj, n, e) {
  var e = e || window.event;
  var x = getGlobalPos(obj);
  $('topline'+n).style.left = (x.x-11)+'px';
  $('topline'+n).style.top = (x.y-5)+'px';
  Show('topline'+n);
  addEvent(document.body, 'click', ssubm_click);
  if (e.stopPropagation) e.stopPropagation(); else e.cancelBubble = true; //Stop this click event
  return false;
}

function ssubm_click() {
  Hide('topline1');
  Hide('topline2');
  removeEvent(document.body, 'click', ssubm_click);
}

