//global functions and vars
var SITE_URL='http://www.keep.com.ua/';

var IE = document.all?true:false;
if(!IE) document.captureEvents(Event.KEYPRESS);
if(!IE) document.captureEvents(Event.MOUSEMOVE);

var reciveRecArr=Array('receiveReq','receiveReqCart');
for(i=0; i<reciveRecArr.length; i++) eval(reciveRecArr[i]+' = new getXmlHttpRequestObject();');
function getXmlHttpRequestObject(){
  if(window.XMLHttpRequest){
          return new XMLHttpRequest();
  }else if(window.ActiveXObject){
          return new ActiveXObject("Microsoft.XMLHTTP");
  }
}
function doAjax(url,func){
  receiveReq.open("GET", url, true);
  receiveReq.onreadystatechange=eval(func);
  receiveReq.send(null);
}

document.onmousedown=getMouseXY;
var mouseX=0;
var mouseY=0;
function getMouseXY(e) {
  if(IE){
    mouseX=event.clientX+document.body.scrollLeft;
    mouseY=event.clientY+document.body.scrollTop;
  }else{
    mouseX=e.pageX;
    mouseY=e.pageY;
  }
  if(mouseX<0) mouseX=0;
  if(mouseY<0) mouseY=0;
  return true;
}

function getBounds(element){
  var left=element.offsetLeft;
  var top=element.offsetTop;
  for(var parent=element.offsetParent; parent; parent=parent.offsetParent){
    left+=parent.offsetLeft-parent.scrollLeft;
    top+=parent.offsetTop-parent.scrollTop
  }
  return {left: left, top: top, width: element.offsetWidth, height: element.offsetHeight};
}

function getCharCode(e){
  if(!e) if(window.event) e=window.event;
  if(typeof(e.keyCode)=='number') char_code=e.keyCode;
  else if(typeof(e.which)=='number') char_code=e.which;
  else if(typeof(e.charCode)=='number') char_code=e.charCode;
  return char_code;
}

String.prototype.trim=function() { return this.replace(/^\s+|\s+$/g, ''); }
String.prototype.explode=function(sep){
  if(sep.length!=''){
    var res_arr=Array();
    var str=this;
    var i=0;
    while((pos=str.indexOf(sep))!=-1){
      res_arr[i++]=str.substr(0,pos);
      str=str.substr(pos+sep.length);
    }
    res_arr[i++]=str.substr(0,str.length);
    if(res_arr.length>0) return res_arr;
    else Array(this);
  }return false;
}
Array.prototype.in_array=function(obj){
  var len=this.length;
  for(var x=0; x<=len; x++){
    if(this[x]==obj) return true;
  }
  return false;
}

function input_check_numeric(elemID){
  elem=document.getElementById(elemID);
  elem.onkeypress=check;
  function check(e){
    var evt = (e) ? e : window.event;
    var code = (document.all) ? evt.keyCode:evt.charCode;
    if(code>=48 && code<=57 || code==0 || code==46 || code==13) return true;
    else return false;
  }
}

function open_window(link,w,h){
  var top = (screen.height - h) / 2;
  var left = (screen.width - w) / 2;
  var win = "width="+(w+20)+",height="+(h+20)+",top="+top+",left="+left+",menubar=no,location=no,resizable=yes,scrollbars=yes";
  newWin = window.open(link,'newWin',win);
}
//end global

//shopping cart
var in_shopping_cart=false;
function updateCartInfo(notuptable){  if(receiveReq.readyState>0 && receiveReq.readyState<4) receiveReq = new getXmlHttpRequestObject();
  receiveReqCart.open("GET", SITE_URL+'ajax.php?act=get_cart_info', true);
  receiveReqCart.onreadystatechange=function(){    if(receiveReqCart.readyState==4){      eval(receiveReqCart.responseText);
      document.getElementById('cart_cnt').innerHTML=info[1];
      document.getElementById('cart_summ').innerHTML=info[0];
      if(in_shopping_cart){        if(info[1]==0){          location.href=SITE_URL;
          return false;        }
        if(!notuptable){
          var cartbody=document.getElementById('ajax_cart');
          row_index=0;
          while(cartbody.rows.length!=0) cartbody.deleteRow(0);
          for(i=0; i<items.length; i++){
            new_tr=cartbody.insertRow(row_index);
            new_tr.id='prod_tr_'+items[i][0];
            new_tr.style.background="#ffffff";
            for(j=1; j<items[i].length; j++){
              new_td=new_tr.insertCell(j-1);
              if(j==2) new_td.innerHTML='<input class="inp" name="quantity_'+items[i][0]+'" type="text" value="'+items[i][j]+'" style="width:30px; text-align:right;" />';
              else new_td.innerHTML=items[i][j];
              new_td.style.padding='0px 5px;'
              new_td.align='left';
            }
            new_td=new_tr.insertCell(items[i].length-1);
            new_td.innerHTML='<a href="javascript://" onclick="changeCart(\'del_from_cart\','+items[i][0]+')"><img src="'+SITE_URL+'images/b_drop.png" title="удалить" alt="удалить" border="0" /></a>';
            new_td.width='20';
            row_index++;
          }
        }        document.getElementById('cart_cnt2').innerHTML=info[1];
        document.getElementById('cart_summ2').innerHTML=info[0];      }    }  }
  receiveReqCart.send(null);}
function changeCart(act,product_id){
  if(receiveReq.readyState>0 && receiveReq.readyState<4) receiveReq = new getXmlHttpRequestObject();
  receiveReqCart.open("GET", SITE_URL+'ajax.php?act='+act+'&product_id='+product_id, true);
  receiveReqCart.onreadystatechange=function(){
    if(receiveReqCart.readyState==4){      updateCartInfo(act=='del_from_cart' ? true : false);
      if(act=='del_from_cart'){        var prod_tr=document.getElementById('prod_tr_'+product_id);
        prod_tr.style.visibility='hidden';
        prod_tr.style.display='none';      }
    }
  }
  receiveReqCart.send(null);
}

//end shopping cart

// For template.php
function menu_effect(act,e1,e2){
  if(act){
    document.getElementById(e1).style.background="#f00";
    document.getElementById(e2).style.background="#5a6985";
  }else{
    document.getElementById(e1).style.background="#6e8e74";
    document.getElementById(e2).style.background="#7a7f88";
  }
}

//Old script
function change_checkbox(checkid){
  checkbox=document.getElementById(checkid);
  if(checkbox.checked) checkbox.checked=false;
  else checkbox.checked=true;
}

function show_cat_edit(act,cat_id,cat_name){
  elem1=document.getElementById('cat_edit');
  elem2=document.getElementById('act');
  elem3=document.getElementById('cat_id');
  elem4=document.getElementById('cat_name');
  elem1.style.visibility="visible";
  elem2.value=act;
  elem3.value=cat_id;
  elem4.value=cat_name;
}
