function getXMLHTTP() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
    xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }

  try { return xmlhttp; }
  finally { xmlhttp = null; }
}


var _xmlHttp=null; // This is the XMLHttp Object...
function chcontent(url, idto) {
  chcontent_loadstart();

  if (_xmlHttp && _xmlHttp.readyState != 0) { _xmlHttp.abort(); }
  
  _xmlHttp = getXMLHTTP();
  if (_xmlHttp) {
    _xmlHttp.open("GET", url+"&print=1&"+(new Date()).getTime(), true);
    _xmlHttp.onreadystatechange = function() { //Firefox does not fire onreadystatechange for the synchronous call
      if (_xmlHttp.readyState == 4) {
        if (!_xmlHttp.status || _xmlHttp.status >= 200 && _xmlHttp.status < 300 || _xmlHttp.status == 304) {
          if (_xmlHttp.responseText.length>100) {
	    document.getElementById(idto).innerHTML = _xmlHttp.responseText;
	  }
          chcontent_loadend();
        }
      }
    }
    _xmlHttp.send(null);
  }
}
function chcontent_loadstart() {
  chdspl("rounded-box-loading","block");
}
function chcontent_loadend() {
  chdspl("rounded-box-loading","none");
}

function dtsend(url, data, hreload) {
  try{dtsend_loadstart();} catch(e){}

  //var length=data.length;
  //if (length<20){return false;}

  if (_xmlHttp && _xmlHttp.readyState != 0) { _xmlHttp.abort(); }
  
  _xmlHttp = getXMLHTTP();
  if (_xmlHttp) {
    _xmlHttp.open("POST", url, true);
    
    _xmlHttp.setRequestHeader("Content-Length",data.length);
    _xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
    _xmlHttp.setRequestHeader("Accept-Charset", "UTF-8");
    //_xmlHttp.setRequestHeader("Connection","close");
    
    _xmlHttp.onreadystatechange = function() { //Firefox does not fire onreadystatechange for the synchronous call
      if (_xmlHttp.readyState == 4) {
        if (!_xmlHttp.status || _xmlHttp.status >= 200 && _xmlHttp.status < 300 || _xmlHttp.status == 304) {
	  try{dtsend_end(_xmlHttp.responseText);} catch(e){}
	  if (_xmlHttp.responseText == "OK") {
	    try{dtsend_loadend(hreload);} catch(e){}
	  } else {
	    try{dtsend_loadend(false);} catch(e){}
	  }
        }
      }
    }
    _xmlHttp.send(data);
  }
}
function dtsend_loadstart() {
  chdspl("rounded-box-loading","block");
}
function dtsend_loadend(hrld) {
  chdspl("rounded-box-loading","none"); 

  if (hrld) { hreload(); }
  else { reload(); }
}


function chdspl(id, mode) {
  document.getElementById(id).style.display = (mode == 'none' ? 'none' : 'block');
}

function chdspl_auto(id) {
  var d=document.getElementById(id);
  d.style.display = d.style.display == "block" ? "none" : "block";
}

function chcontentlocal(content, idto) {
  document.getElementById(idto).innerHTML=content;
}


function hreload() { //hard reload
  window.location.reload(true);
}


function ctrlEnter(event) {
  if (event.ctrlKey && (event.keyCode == 0xA || event.keyCode == 0xD)) { return true; }
  return false;
}
function ctrlLeft(event) {
  if (!event) event = window.event;
  if (event.ctrlKey && event.keyCode == 37) { return true; }
  return false;
}
function ctrlRight(event) {
  if (!event) event = window.event;
  if (event.ctrlKey && event.keyCode == 39) { return true; }
  return false;
}

function textadd(id, text) {
  document.getElementById(id).value += text;
}

////////////////////////////////////
////////////////////////////////////
var ns6=document.getElementById&&!document.all;

function restrictinput(maxlength, e, placeholder) {
  if (window.event && event.srcElement.value.length >= maxlength) { return false; }
  else if (e.target && e.target == eval(placeholder) && e.target.value.length >= maxlength) {
    var pressedkey = /[a-zA-Z0-9\.\,\/]/;
    if (pressedkey.test(String.fromCharCode(e.which))) { e.stopPropagation(); }
  }
}
										    
function countlimit(maxlength, e, placeholder) {
  var theform = eval(placeholder);
  var lengthleft = maxlength-theform.value.length;
  var placeholderobj = document.all ? document.all[placeholder] : document.getElementById(placeholder);
  if (window.event || e.target&&e.target == eval(placeholder)) {
    if (lengthleft < 0) { theform.value = theform.value.substring(0, maxlength); }
    placeholderobj.innerHTML = lengthleft;
  }
}

function displaylimit(thename, theid, thelimit) {
  var theform = theid != "" ? document.getElementById(theid) : thename;
  var limit_text = '<span id="' + theform.toString() + '">' + thelimit + '</span>';  
  if (document.all || ns6) { document.write(limit_text); }
  if (document.all) {
    eval(theform).onkeypress = function() { 
    if (ctrlEnter(event)) { comment_submit(); }
      return restrictinput(thelimit, event, theform);
    }
    eval(theform).onkeyup = function() { countlimit(thelimit, event, theform); }
  } else if (ns6) {
    document.body.addEventListener('keypress', function(event) { restrictinput(thelimit, event, theform) }, true);
    document.body.addEventListener('keyup', function(event) { countlimit(thelimit, event, theform) }, true);
  }
}
