var ns4 = (document.layers) ? true : false;
var w3c = (document.getElementById) ? true : false;
var ie4 = (document.all) ? true : false;
var isIE = document.all ? true : false;
var oCanvas = document.getElementsByTagName((document.compatMode && document.compatMode == "CSS1Compat") ? "HTML" : "BODY")[0];

//alert(document.getElementsByTagName("HTML").scrollLeft);

var bird = false;

var marginLeft = 8;
var marginRight = 8;
var marginTop = 19;
var qna_question_temp = 1;
var mouseX = 0;
var mouseY = 0;
var scrollX = 0;
var scrollY = 0;
var clientWidth = 0;
var clientHeight = 0;
var currentForm = false;
var alarmed = false;
var wfocused = true;
var otitle = document.title;
var newmessage = false;
var alarmed = false;
var mtm = false;
var thistory = false;

function $(name) {
  if (ns4) return eval('document.' + name);
  if (w3c) return document.getElementById(name);
  if (ie4) return eval('document.all.' + name);
  return false;
}


var ii = {
  req: null,
  ready: function(func,state) {
    if (ii.req.readyState == 4 && ii.req.status==200) {func(ii.req.responseText);}
  },
  open: function (url, func, wait) {
    ii.req = (window.XMLHttpRequest)
               ? new XMLHttpRequest()
               : ((window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : null);
    if (ii.req) {
      ii.req.open('GET', 'http://' + location.host + url, wait!=false);
      ii.req.send(null);
      if (wait==false) func(ii.req.responseText); else ii.req.onreadystatechange=function(){ii.ready(func)};
    }
  }
};

function mousem(e) {
  mouseX = isIE ? event.clientX + oCanvas.scrollLeft : e.pageX - window.pageXOffset;
  mouseY = isIE ? event.clientY + oCanvas.scrollTop : e.pageY - window.pageYOffset;
  if (hint && hint.g) hint.show();
}

function valueof(el) {
  if (el.type.toLowerCase() == 'radio' && !el.checked) return;
  if (el.type.toLowerCase() == 'checkbox' && !el.checked) return;
  if ((el.tagName.toLowerCase() == 'select')) return el.name + "=" + el.options[el.selectedIndex].value + "&";
  return el.name + "=" + encodeURIComponent(el.value) + "&";
}

function destroyForm(form) {destroyElement($(form));}
function destroyElement(element) {if (element && element.parentNode) element.parentNode.removeChild(element);}

function loadpage(element, html){
   var p = $(element);
   if (!p) {
     p = document.createElement("DIV");
     document.body.appendChild(p);
     p.id = element;
     currentForm = element;
     p.style.visibility="hidden";
     p.innerHTML= html;
     p.style.position="absolute";
     p.style.margin="0px";
     p.style.padding="0px";
     p.style.zIndex=99;
     var p_height = isIE ? p.clientHeight : p.offsetHeight;
     var p_width = isIE ? p.clientWidth : p.offsetWidth;
     p.style.top = (clientHeight-p_height)/2+scrollY + "px";
     p.style.left = (clientWidth-p_width)/2+scrollX + "px";
     p.style.visibility="visible";
   } else {
     p.innerHTML = html;
   }
   var f = $('focus');
   if (f) f.focus(); //bugged in IE? :/
}

function refpage(res){
  var id;
  var y = document.createElement('DIV');y.innerHTML=res;
//alert(res);
  while (child = y.firstChild) {
    if (child.innerHTML!="") {
      if (child.tagName=='DIV') 
        loadpage(child.id,child.innerHTML);
      else if (child.tagName=='SCRIPT') 
        eval(child.innerHTML);
    } else destroyForm(child.id);
    if (child) y.removeChild(child);
  }
  destroyElement(y);
//    ar=document.getElementById('header').style;

//ar.visibility="visible";
}

function updateelement(id,html){el=$(id);el.innerHTML=html;}
function _focus(obj) {for (var i=0;i<obj.childNodes.length;i++) if (obj.childNodes[i].id='focus') {obj.childNodes[i].focus(); return;}}

var post_a = {
  req: null,
  ready: function(func,elemid) {
    if (post_a.req.readyState == 4 && post_a.req.status == 200) {document.getElementById(elemid).innerHTML='';document.getElementById(elemid).innerHTML=post_a.req.responseText;

	}
  },
  open: function (url, parameters, elemid, func) {
    post_a.req = (window.XMLHttpRequest)
               ? new XMLHttpRequest()
               : ((window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : null);
    if (post_a.req) {
      post_a.next = false
      post_a.req.open('POST', url, true);

      if (document.getElementById('load_ing'))
	  document.getElementById('load_ing').style.display='block';

     // if (document.getElementById('global_paga'))
	 // document.getElementById('global_paga').innerHTML="";

      post_a.req.onreadystatechange=function(){post_a.ready(func,elemid)};
      post_a.req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      post_a.req.setRequestHeader("Content-length", parameters.length);
      post_a.req.setRequestHeader("Connection", "close");
      post_a.req.send(parameters);
    }
    return false;
  }
};




function post(form,elemid) {
  var res = "";
  var next = "";
  for(i=0; i<document[form].elements.length; i++) {
    next = valueof(document[form].elements[i]);
    if (next) res += next;
  }
  post_a.open(document[form].action,res,elemid,function(html){refpage(html)},false);
  	get('/forms/basket_online.php','basket');
}


var get_a = {
  req: null,
  ready: function(func,elemid) {
    if (get_a.req.readyState == 4 && get_a.req.status == 200) {document.getElementById(elemid).innerHTML='';document.getElementById(elemid).innerHTML=get_a.req.responseText;
	
	}
  },
  open: function (url, elemid,dop_elemid, func) {
    get_a.req = (window.XMLHttpRequest)
               ? new XMLHttpRequest()
               : ((window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : null);
    if (get_a.req) {
      get_a.next = false

      if (document.getElementById('load_ing'))
	  document.getElementById('load_ing').style.display='block';

      if (document.getElementById(dop_elemid))
	  document.getElementById(dop_elemid).style.display='block';

     // if (document.getElementById('global_paga'))
	 // document.getElementById('global_paga').innerHTML="";
//alert(url+" - "+ elemid);
      get_a.req.open('GET', 'http://' + location.host + url,true);
      get_a.req.send(null);
      get_a.req.onreadystatechange=function(){get_a.ready(func,elemid)};
    }
    return false;
  }
};


function reload_pages(){

}

function get(url,elemid,dop_elemid) {
  get_a.open(url,elemid,dop_elemid,function(html){refpage(html)},false);
 // reload_pages();
}

var hint = {
    t: null,
    g: false,
    o: false,
    mouseX: 0,
    mouseY: 0,
    timer: null,
    r: function(){if (hint.g) hint.t.parentNode.removeChild(hint.t);hint.g = false;},
    show: function() {
        var x_scroll = isIE ? oCanvas.scrollLeft : window.pageXOffset;
        var y_scroll = isIE ? oCanvas.scrollTop : window.pageYOffset;
        var t_width = isIE ? hint.t.clientWidth : hint.t.offsetWidth;
        var t_height = isIE ? hint.t.clientHeight : hint.t.offsetHeight;
        if (hint.t.style.visibility=="hidden") {
          var w_width = isIE ? oCanvas.clientWidth : window.innerWidth;
          var w_height = isIE ? oCanvas.clientHeight : window.innerHeight;
          var x = mouseX;
          var y = mouseY;
          if (x+t_width>w_width) x = x - t_width;
          else x = x + 8;
          if (y+t_height>w_height) y = w_height - t_height - 10;
          else y = y - 10;
          x = isIE ? x : x + x_scroll;
          y = isIE ? y : y + y_scroll;
          hint.t.style.left = x + "px";
          hint.t.style.top = y + "px";
          hint.t.style.visibility="visible";
        } else {
          var t_top = isIE ? hint.t.offsetTop : hint.t.offsetTop - y_scroll;
          var t_left = isIE ? hint.t.offsetLeft : hint.t.offsetLeft - x_scroll;
          if ((mouseX<t_left-marginLeft*2 || mouseX>t_left+t_width+marginLeft*2
            || mouseY<t_top-marginLeft*2 || mouseY>t_top+t_height+marginLeft*2) && !hint.o)
            {hint.r();return;}
        }
    },
    l: function(url) {
      hint.o = true;
      hint.timer = setTimeout(function(){if (hint.o) ii.open(url,function(html){hint.s(html)});},50);
    },
    s: function(html){
      if (hint.t) hint.r();
      hint.t = document.createElement("DIV");
      hint.t.style.visibility="hidden";
      hint.t.style.position="absolute";
      hint.t.style.margin=marginTop+"px 0px 0px "+marginLeft+"px";
      hint.t.style.padding="0px 0px 0px 0px";
      hint.t.style.zIndex=101;
      hint.t.innerHTML= html;
      document.body.appendChild(hint.t);
      hint.g = true;
      hint.show();
    },
    h: function(e){hint.o = false;clearTimeout(hint.timer);}
};


function resizeClient() {
//  if (popup==true) document.body.style.width = "100%";
//  else document.body.style.width = ((clientWidth > 980)?clientWidth-40:940)+"px";
}

window.onload=function(ev){
  if (isIE) {
    ev = event;
    var clientS = clientSize(ev); clientWidth = clientS.w; clientHeight = clientS.h-32;
    resizeClient();
  }
}

//window.onfocus=function(){wfocused=true;alarmed=false;};
//window.onblur=function(){wfocused=false;};
document.onmousemove=mousem;

window.onfocus=function(){wfocused=true;alarmed=false;};
window.onblur=function(){wfocused=false;};
window.onresize=function(ev){
  if (isIE) ev = event;
  var clientS = clientSize(ev); clientWidth = clientS.w; clientHeight = clientS.h-32;
  if (isIE) resizeClient();
  if(currentForm){
    var p=$(currentForm);
    var p_height = isIE ? p.clientHeight : p.offsetHeight;
    var p_width = isIE ? p.clientWidth : p.offsetWidth;
    p.style.top = (clientHeight-p_height)/2+scrollY + "px";
    p.style.left = (clientWidth-p_width)/2+scrollX + "px";
  }
}
function mouseCoords(ev){return isIE ? {x:ev.clientX, y:ev.clientY} : {x:ev.pageX, y:ev.pageY};}
function clientSize(ev){return isIE ? {w:oCanvas.clientWidth,h:oCanvas.clientHeight} : {w:window.innerWidth,h:window.innerHeight};}
function scrollCoords(ev) {return isIE ? {x:oCanvas.scrollLeft,y:oCanvas.scrollTop} : {x:window.pageXOffset,y:window.pageYOffset};}
