//------------------------------------------------------------------------------
// AJAX functions (may depends of position.js)
//------------------------------------------------------------------------------
document.write("<div id='js_ajaxdiv' style='z-index:5;position:absolute;visibility:hidden;border:1px solid #000;background-image:url(/_res/alpha.png);padding:10px;'><img src='/_res/spinner.gif' style='border:0;'></div>");

// result functions (called after ajaxLoad)
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 AjaxInsertEndHTML(htmlcode,elm)
{ elm.innerHTML+=htmlcode;
}
function AjaxInsertBeginHTML(htmlcode,elm)
{ elm.innerHTML=htmlcode+elm.innerHTML;
}
function AjaxSetLayer(htmlcode,arg)
{ arg[0].innerHTML=htmlcode;
  putLayer(arg[0],arg[1]);
  arg[0].style.visibility='visible';
}

// start functions (call ajaxLoad)
function ajaxLoadScript(url,param,loading,timeout,tOutFunction)
{ ajaxLoad(url,param,loading,timeout,tOutFunction,AjaxJsScript);
}
function ajaxInnerHtml(url,param,elm,loading,timeout,tOutFunction)
{ ajaxLoad(url,param,loading,timeout,tOutFunction,AjaxSetInnerHTML,elm);
}
function ajaxInsertHtml(url,param,elm,atbegin,loading,timeout,tOutFunction)
{ if(atbegin)
    ajaxLoad(url,param,loading,timeout,tOutFunction,AjaxInsertBeginHTML,elm);
  else
    ajaxLoad(url,param,loading,timeout,tOutFunction,AjaxInsertEndHTML,elm);
}
function ajaxLayer(url,parameter,elm,from,loading,timeout,tOutFunction)
{ if(elm.style.visibility=='visible'){elm.style.visibility='hidden';}
  else ajaxLoad(url,parameter,loading,timeout,tOutFunction,AjaxSetLayer,new Array(elm,from));
}

// stop event propagation (onclick)
function stopPropagation(e)
{ if(!e) var e=window.event;
  e.cancelBubble=true;
  if(e.stopPropagation) e.stopPropagation();
}

// to hide specific layer when click on body or layers
function ajaxClick(elements,from,e)
{ if(e)
    stopPropagation(e);
  
  if(elements==null)
    return false;
  else
  { for(var i=0;i<elements.length;i++)
    { var fdiv=document.getElementById(elements[i]);
      if(fdiv!=null && fdiv.style.visibility=='visible')
        fdiv.style.visibility='hidden';
    }
  }
}

//main ajaxload function (used in another funcions)
function ajaxLoad(url,parameter,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('POST',url,true);
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  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.setRequestHeader("Content-length", parameter.length);
  http_request.setRequestHeader("Connection", "close");
  http_request.send(parameter);

  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('js_ajaxdiv');
      elm.style.visibility='hidden';
      //clear timeout
      if(timeoutId)
        window.clearTimeout(timeoutId);
      
      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;
      }
    }
    else if(showStatus)
    { //get the center of the page and show Loading ...
      elm=document.getElementById('js_ajaxdiv');
      if(elm.style.visibility=='hidden')
      { if(document.body && (document.body.scrollTop || document.body.scrollLeft))
        { x=document.body.scrollLeft;
          y=document.body.scrollTop;
        }
        else
        { x=document.documentElement.scrollLeft;
          y=document.documentElement.scrollTop;
        }
        y=(parseInt(document.body.clientHeight)-parseInt(elm.offsetHeight))/2+parseInt(y);
        x=(parseInt(document.body.clientWidth)-parseInt(elm.offsetWidth))/2+parseInt(x);
        elm.style.top=y+'px';
        elm.style.left=x+'px';
        elm.style.visibility='visible';
      }
    }
  }
}
