var IE = !+"\v1";

function callMe(url, 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,ajax.responseText,callback,urllock);
      }
    }
  };
  if(url.indexOf('time') == -1){
    if(url.indexOf('?') == -1){
      url += '?time='+Number(new Date());
    }else{
      url += '&time='+Number(new Date());
    }
  }
  ajax.open('GET', 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];
  var obj;
  try{
    if(typeof(JSON) === "object" && typeof(JSON.parse) === "function")
      obj = JSON.parse(json);
    else
      eval("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){
   obj = json;
  }
  if(typeof(callback) === "function"){
    callback(obj);
  }
}
function qqUploaded(id, filename, responseJSON){
  if(responseJSON && responseJSON.success){
    var el = document.getElementById(id);
    el.parentNode.innerHTML = (responseJSON.msg ? responseJSON.msg : filename).replace('&lt;','<').replace('&gt;','>');
  }
}

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

function EmitSignal(signal, value, values){
  var els;
  for(var i=0; i<values.length; i++){
    els = document.getElementsByName("show["+signal+"_"+values[i]+"]");
    for(j=0;j<els.length;j++){
      els[j].style.display = (value==values[i]?'':'none');
    }
  }
}
function CheckAll(from, rel){
  var els = document.getElementsByTagName("input"), el;
  for(var i=0; i<els.length; i++){
    el = els[i];
    if(el.type.toLowerCase() == "checkbox" && el.getAttribute("rel") == rel){
      el.checked = from.checked;
      if(typeof(el.onchange) == "function"){
        el.onchange();
      }
    }
  }
}

var LoaderFunctions = [];
var LoaderLoaded = false;
var LoaderLastOnLoad = window.onload;
if(typeof(LoaderLastOnLoad) !== 'function')
  window.onload = FireLoader;
else
  window.onload = function(){LoaderLastOnLoad(); FireLoader();};
var LoaderLastContentLoaded = window.DOMContentLoaded;
if(typeof(LoaderLastContentLoaded) !== 'function')
  window.DOMContentLoaded = FireLoader;
else
  window.DOMContentLoaded = function(){LoaderLastContentLoaded(); FireLoader();};
function AddLoad(func){
  LoaderFunctions.push(func);
}
function FireLoader(){
  for(var i=0; i<LoaderFunctions.length; i++){
    LoaderFunctions[i]();
  }
  LoaderFunctions = [];
}

function ShowHide(id){
  var el = document.getElementById(id);
  if(el){
    if(el.style.display !== 'block'){
      el.style.display = 'block';
    }else{
      el.style.display = 'none';
    }
  }
}

function stop(e){
  if (!e) var e = window.event;
  e.cancelBubble = true;
  if (e.stopPropagation) e.stopPropagation();
}
function RegDebug(id){
  el = document.getElementById(id);
  if(!el) return;
  el.onclick = function(){ShowHide(id)};
  el.firstChild.onclick = stop;
}
AddLoad(function(){RegDebug('debugger'); RegDebug('transger'); });

var tKeys = '';
function Trans(){
  var el = document.getElementById("transger");
  if(el) el = el.firstChild;
  else return;
  if(!el.loaded){
    tKeys = el.innerHTML;
    el.innerHTML = 'Loading...';
    callMe("trans.php?keys="+tKeys, function(o){
      el.innerHTML = o.html;
    }, true);
    el.loaded = true;
  }
  ShowHide('transger');
}
function TransSubmit(form){
  var el, send=[], keys=[];
  for(var i=0; i<form.elements.length; i++){
    el = form.elements[i];
    if(!el.name) continue;
    keys.push(el.name);
    if(el.value == el.defaultValue) continue;
    send.push("trans["+el.name+"]="+el.value);
  }
  var el = document.getElementById("transger");
  if(el) el = el.firstChild;
  callMePost("trans.php?keys="+tKeys, send.join('&'), function(o){
    el.innerHTML = o.html;
    alert("Saved");
  }, true);
  el.innerHTML = 'Sending...';
}
