function $(id) { return document.getElementById(id); }
function _(fname, el) { return (el != undefined) ? document.forms[fname].elements[el] : document.forms[fname]; }
function Hide(obj) { if (typeof(obj)=='string') obj=$(obj); if (obj) obj.style.display='none'; return false; }
function Show(obj) { if (typeof(obj)=='string') obj=$(obj); if (obj) obj.style.display=''; return false; }
function setPos(obj, x, y) { obj.style.left = x+'px'; obj.style.top = y+'px'; }
function getPos(obj) { return {x : (isNaN(parseInt(obj.style.left))) ? 0 : obj.style.left, y : (isNaN(parseInt(obj.style.top))) ? 0 : obj.style.top}; }
function goto(url) { window.location.href = url; }

String.prototype.trim = new Function("return this.replace(/^\\s+|\\s+$/g,'')");
String.prototype.URI = new Function("return encodeURIComponent(this)");

//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);
}

//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;
}

var AjaxReq = false;

function loadDoc(url, uri, processReqFunc) {
  if (!uri) uri = '';
  var ajaxreq = {'send': sendAjax, 'url': url, 'uri': uri, 'reqFunc': function(str) { EnableAll(); $('ShopBoxRes').innerHTML = str; }};
  if (processReqFunc) ajaxreq.reqFunc = processReqFunc;
  ajaxreq.send();
  return false;
}

function loadJS(url, uri, processReqFunc) {
  if (!uri) uri = '';
  var ajaxjsreq = {'send': sendAjax, 'url': url, 'reqFunc': function(str) { if (str!='') eval(str); }, 'uri': uri};
  if (processReqFunc) ajaxjsreq.reqFunc = processReqFunc;
  ajaxjsreq.send();
  return false;
}

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.r.responseText != '') {
        self.reqFunc(self.r.responseText);
        delete self.r;
      }
    };

    if (this.uri != '') {
      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;
}


//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;
}


//Get active window size
function getWindowSize() {
  if(typeof(window.innerWidth)=='number') //Non-IE
    return {x: window.innerWidth, y:window.innerHeight};
  else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) //IE 6+
    return {x: document.documentElement.clientWidth, y:document.documentElement.clientHeight};
  else if(document.body && (document.body.clientWidth || document.body.clientHeight)) //IE 4 compatible
    return {x: document.body.clientWidth, y:document.body.clientHeight};
  return {x:0,y:0};
}

function setCookie(name, value, path) {
  if (path == undefined) path=""; else path="; path="+path;
  var expDate=new Date();
  expDate.setTime(expDate.getTime()+2592000000); //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 '';
}

function ShowAnim(id, step) {
  if (step == undefined) { step = 0; Show(id); }
  step = parseFloat(step)+0.1;
  $(id).style.opacity = step;
  $(id).style.filter = 'alpha(opacity='+(step*100)+')';
  if (step < 1) setTimeout('ShowAnim("'+id+'",'+step+')', 50);
}

function HideAnim(id, step) {
  if (step == undefined) step = 1;
  step = parseFloat(step)-0.2;
  if (step <= 0) { step=1; Hide(id); }
  $(id).style.opacity = step;
  $(id).style.filter = 'alpha(opacity='+(step*100)+')';
  if (step < 1) setTimeout('HideAnim("'+id+'",'+step+')', 100);
}

function playvideo() {
  Hide('replayBtn');
  $('videoPlayer').play();
}

function endvideo() {
  ShowAnim('replayBtn');
}

function ShowLogin() {
  $('LOGIN').className = ($('LOGIN').className == '') ? 'drop' : '';
  if ($('CAPTION'))
    $('CAPTION').className = ($('CAPTION').className == '') ? 'drop' : '';
  return false;
}
