//------------------------------------------------------------------------------
// AJAX functions (maybe depends on position.js)
//------------------------------------------------------------------------------
document.write("<div id='dhtmldiv' style='font-family:Verdana,Arial,Helvetica;font-weight:bold;font-size:11px;vertical-align:middle;z-index:5;position:absolute;visibility:hidden;border:1px solid #000;background-color:#fff;padding:2px 10px;'><img src='/_res/loading.gif' style='border:0px;'>&nbsp;&nbsp;Loading&nbsp;...</div>");

function AjaxJsScript(code)
{ var e=document.createElement("script");
  e.type="text/javascript";
  e.text=code;
  document.getElementsByTagName("head")[0].appendChild(e);
}
function AjaxSetInnerHTML(htmlcode,elm){elm.innerHTML=htmlcode;}
function AjaxSetLayer(htmlcode,arg)
{ arg[0].innerHTML=htmlcode;
  putLayer(arg[0],arg[1]);
  arg[0].style.visibility='visible';
  FixLayer(arg[0]);
}

function dhtmlLoadScript(url,loading,timeout,tOutFunction){ajaxLoad(url,loading,timeout,tOutFunction,AjaxJsScript);}
function dhtmlInnerHtml(url,elm,loading,timeout,tOutFunction){ajaxLoad(url,loading,timeout,tOutFunction,AjaxSetInnerHTML,elm);}
function dhtmlLayer(url,elm,from,loading,timeout,tOutFunction)
{ if(elm.style.visibility=='visible'){elm.style.visibility='hidden';FixLayer(elm);}
  else ajaxLoad(url,loading,timeout,tOutFunction,AjaxSetLayer,new Array(elm,from));
}

function ajaxLoad(url,showStatus,timeout,tOutFunction,rFunction,argFunction)
{ var http_request=false;
  var url=url+(url.indexOf('?')==-1?'?':'&')+Math.random();
  var showStatus=(showStatus==null)?false:showStatus;
  var timeout=(timeout==null)?false:timeout*1000;
  var rFunction=rFunction; //return function
  var argFunction=(argFunction==null)?false:argFunction;

	if(timeout)
		var timeoutId=window.setTimeout(function(){if(callInProgress()){ http_request.abort(); } tOutFunction();},timeout);
		
  //create object
  if(!getXMLHttpObj()){alert('ERROR: Cannot create XMLHttpRequest object');return;}
  http_request.onreadystatechange=StateChange;
  http_request.open('GET',url,true);
  http_request.setRequestHeader("Content-Type", "text/html; charset=utf-8");
  http_request.setRequestHeader("Cache-Control","no-store, no-cache, must-revalidate");
  http_request.setRequestHeader("Cache-Control","post-check=0, pre-check=0");
  http_request.setRequestHeader("Pragma","no-cache");
  http_request.send(null);

	function callInProgress()
	{	switch(http_request.readyState)
		{	case 1: case 2: case 3:
				return true;
				break;
			default:
				return false;
				break;
		}
	} 

  function getXMLHttpObj() //new object xmlhttprequest
  { if(window.XMLHttpRequest)
      http_request=new XMLHttpRequest();//http_request.overrideMimeType('text/xml');
    else if(window.ActiveXObject)
    { try{http_request=new ActiveXObject("Msxml2.XMLHTTP");}
      catch(e)
      { try{http_request=new ActiveXObject("Microsoft.XMLHTTP");}
        catch(e){}
      }
    }
    if(http_request) return true;
    else return false;
  }
  function StateChange()
  { if(http_request.readyState==4)
    { //hide loading ...
      elm=document.getElementById('dhtmldiv');
      elm.style.visibility='hidden';
      //clear timeout
      if(timeoutId) window.clearTimeout(timeoutId);
      
      try
      {	switch(http_request.status)
      	{ case 0: alert('ERROR: Unknow');break;
        	case 200: rFunction(http_request.responseText,argFunction);break;
        	case 400: alert('ERROR: Bad Request'); break;
        	case 403: alert('ERROR: Forbidden'); break;
        	case 404: alert('ERROR: Not Found');break;
        	case 405: alert('ERROR: Method Not Allowed');break;
        	case 408: alert('ERROR: Request Timeout');break;
        	default: alert('ERROR: '+http_request.status);break;
        }
      }
      catch(err)
      { //Handle errors here
      }
    }
    else if(showStatus)
    { //get the center of the page and show Loading ...
      elm=document.getElementById('dhtmldiv');
      if(elm.style.visibility=='hidden')
      { elm.style.top=(document.body.clientHeight-elm.offsetHeight)/2+document.body.scrollTop;
        elm.style.left=(document.body.clientWidth-elm.offsetWidth)/2+document.body.scrollLeft;
        elm.style.visibility='visible';
      }
    }
  }
}
