var IE = false;

function callMe(url, callback){
  ajaxObject = function() {
  try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch(e){
  try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch(e){
  try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e){
  try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e){
  try { return new XMLHttpRequest() } catch(e){
  throw new Error( "This browser does not support XMLHttpRequest." );
  }}}}}}
  ajax = new ajaxObject();
  ajax.onreadystatechange = function(){
    if(ajax.readyState == 4){
      if(ajax.status == 200){
        callHandle(url,ajax.responseText,callback);
      }
    }
  };
  if(url.indexOf('time') == -1){
    url += '?time='+Number(new Date());
  }
  ajax.open('GET', 'ajax/'+url, true);
  ajax.send(null);
}
function callMePost(url, params, callback, urllock){
  ajaxObject = function() {
  try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch(e){
  try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch(e){
  try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e){
  try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e){
  try { return new XMLHttpRequest() } catch(e){
  throw new Error( "This browser does not support XMLHttpRequest." );
  }}}}}}
  ajax = new ajaxObject();
  ajax.onreadystatechange = function(){
    if(ajax.readyState == 4){
      if(ajax.status == 200){
        callHandle(url.replace('ajax/',''),ajax.responseText,callback,urllock);
      }
    }
  };
  ajax.open('POST', url, true);
  ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  ajax.setRequestHeader("Content-length", params.length);
  ajax.setRequestHeader("Connection", "close");
  ajax.send(params);
}
function callHandle(url,json, callback, urllock){
  if(!urllock) location.href = "#"+url.replace(/,small_1/,'').split('?')[0];

  try{
    if(typeof(JSON) === "object" && typeof(JSON.parse) === "function")
      var obj = JSON.parse(json);
    else
      eval("var obj = "+json+";");
    if(obj.title) document.title = obj.title;
    if(obj.description){
      document.getElementById("sitedescription").content = obj.description.replace(/\<[^\>]*\>/g,'').substring(0,250);
    }
  }catch(e){
   var obj = json;
  }
  if(typeof(callback) === "function"){
    callback(obj);
  }
}

function trim(str){
  s = str.replace(/^(\s)*/, '');
  s = s.replace(/(\s)*$/, '');
  return s;
}

function AddLoad(func){
  var last = window.onload;
  if(typeof(last) !== 'function')
    window.onload = func;
  else
    window.onload = function(){last(); func();}
}

function AsearchOT(el){
  for(var i=1;i<el.options.length;i++){
    if(el.options[i].selected){
      document.getElementById('asearch_'+el.options[i].value).className = '';
    }else{
      document.getElementById('asearch_'+el.options[i].value).className = 'hide';
    }
  }
}

function SearchOT(el){
  for(var i=1;i<el.options.length;i++){
    if(el.options[i].selected){
      document.getElementById('search_'+el.options[i].value).className = '';
    }else{
      document.getElementById('search_'+el.options[i].value).className = 'hide';
    }
  }
}

function OfferingOT(el){
  for(var i=1;i<el.options.length;i++){
    if(el.options[i].selected){
      document.getElementById('offertype_'+el.options[i].value).className = 'fullbox';
    }else{
      document.getElementById('offertype_'+el.options[i].value).className = 'fullbox hide';
    }
  }
}
function OfferingPhoto(el){
  var o = document.createElement('input');
  o.type = 'file';
  o.name = 'photo[]';
  el.parentNode.insertBefore(o,el);
  o = document.createElement('br');
  el.parentNode.insertBefore(o,el);
}

var Albumer = null;
function OfferAlbum(el, side){
  var table = document.getElementById("phototable");
  if(table.offsetWidth <= table.parentNode.offsetWidth) return;
  Albumer = setInterval(function(){
    var left = table.offsetLeft + side*3;
    left = Math.min(0,left);
    left = Math.max(left, -(table.offsetWidth-table.parentNode.offsetWidth));
    table.style.left = left+'px';
  },10);
  el.onmouseout = function(){clearInterval(Albumer)};
}

function OfferView(el){
  var src = el.src.split('/');
  src = src[src.length-1];
  src = new RegExp(src);
  var toel = document.getElementById('photobox');
  for(var i=0;i<toel.childNodes.length;i++){
    if(toel.childNodes[i].nodeType !== 3){
      if(src.test(toel.childNodes[i].src)) toel.childNodes[i].className = "";
      else toel.childNodes[i].className = "hide";
    }
  }
}

function Validate(form){
  var el, ret=true;
  for(var i=0;i<form.elements.length; i++){
    el = form.elements[i];
    if((el.type == 'text' || el.tagName.toLowerCase() == 'select' || el.type == 'password') && el.name != 'data[nip]' && el.name != 'data[captcha]'){
      if(trim(el.value).length < 1){
	ret = false;
	el.previousSibling.previousSibling.className = 'err';
      }else{
	el.previousSibling.previousSibling.className = '';
      }
    }
  }
  return ret;
}

var qx, qy;
function MoveTooltip(e){
  if(!e) var e = window.event;
  qx = e.clientX;
  qy = e.clientY;
  document.getElementById("tooltip").style.left=qx+4+'px';
  document.getElementById("tooltip").style.top=qy+8+'px';
}

function Tooltip(el, text){
  if(document.getElementById("tooltip")){
    document.body.removeChild(document.getElementById("tooltip"));
  }
  var tool = document.createElement('div');
  tool.id = "tooltip";
  tool.className = "tooltip";
  tool.innerHTML = text;
  document.body.appendChild(tool);
  el.onmousemove = MoveTooltip;
  el.onmouseout = function(){HideTooltip(el);};
}

function HideTooltip(el){
  if(document.getElementById("tooltip")){
    document.body.removeChild(document.getElementById("tooltip"));
  }
  el.onmousemove = '';
  el.onmouseout = '';
}
