﻿
var _oSpanID="loading";
var _loadingFlag=false;
function oAjax( url ,eventTarget,callback,isCache)
{
    try{
        this.Debug  = false;
        this.IsCache = isCache==null?true:isCache  //是否缓存
        this.Url = url + (url.indexOf("?")>0?"&":"?")+"__SCRIPTCALLBACKID=" + eventTarget; //提交的URL
        this.ContentType = "text/xml;charset=gb2312";
        this.HttpRequest = this.createXMLHttpRequest();
        //this.oSpanID="loading";
        //this.loadingFlag=false;
        if ( this.HttpRequest == null )
        {
            this._debug("XMLHttpRequest create failure!");
            return;
        }
        var xhReq = this.HttpRequest;
        xhReq.onreadystatechange = function (){
            oAjax._OnReadyStateChange( xhReq,callback );
        }

    } catch(e){
       this._debug( "unknow err: " + e.message );
    }
}

/*
 * Get URL resource
 */
oAjax.prototype.Get = function(urldata) {
    this.Url += "&" + urldata;
    this._get();
}

/*
 * Post data to the server
 */
oAjax.prototype.Post = function( arrKey, arrValue ) {

    var data = '';

    for( i = 0; i < arrKey.length; i ++)
    {
        //alert(arrValue[i]);
        data += "&" + escape(arrKey[i]) + "=" + escape(arrValue[i]).replace(/\+/g,"%u002b").replace(/%B7/g,"%u00B7");
    }
    data = data.replace(/^&/g, "");
    this._post(data);
}

/*
 * Post formdata to the server
 */
oAjax.prototype.PostForm = function( formdata ) {
    this._post(this.getForm(formdata));
}

oAjax.prototype.createXMLHttpRequest = function() {

    try { return new ActiveXObject("Msxml2.XMLHTTP");    } catch(e) {}
    try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
    try { return new XMLHttpRequest();                   } catch(e) {}
    return null;
}

/*
 * Debug information for testing
 */
oAjax.prototype._debug = function(message) {

    if ( this.Debug ){alert(message);}
}

/*
 * Process message and data from server
 */
oAjax._OnReadyStateChange = function( xreq, callback ){

    if ( xreq == null ){ return;}
    if ( xreq.readyState == 4){ 
        oAjax.prototype.loadDelete();      
        if ( xreq.status == 200 ){  
           if(xreq.responseXML) {
            if(getTagValue(xreq.responseXML,"RowCount")=="-10000"){
                    alert("您还没有登陆呢！");}
                else if(getTagValue(xreq.responseXML,"RowCount")=="-10001"){
                    alert("对不起，您没有操作权限！");}
                    else{
           callback (xreq.responseXML );
           }
           
           }
           else {alert("数据取得失败！");}                 
        }
        else{ alert('服务器端错误！'+ xreq.status);}
        //if(this.loadingFlag){this.loadDelete();}
        
    } else {
        // Others
    }
  }

oAjax.prototype._SendRequest = function(HttpMethod, data){

    this._debug( 'Send Request ' + HttpMethod + data );
    
    if ( this.HttpRequest != null )
    {
        if(!this.IsCache){this.Url +="&time="+(new Date()).getTime();}
        this.HttpRequest.open(HttpMethod, this.Url, true);

        if ( this.ContentType != null )
        {
            //this.HttpRequest.setRequestHeader("Content-Type", this.ContentType);
            this.HttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gb2312")
        }
        if(_loadingFlag){this.loadDisplay();}
        this.HttpRequest.send(data);
        
        return true;
    }
    return false;
}

/* Send GET request to server */
oAjax.prototype._get = function () {

    this._debug( 'GET' );
    //this.ContentType = "text/html";
    return this._SendRequest("GET", null);
}

/* Send POST request and data to server */
oAjax.prototype._post = function (data) {
    this._debug( 'POST' );
    this.ContentType = "text/xml;charset=gb2312" ;
    return this._SendRequest("POST", data);
}

oAjax.ArrayValue = function ( xmlobj ) {
    var array = new Array();
    var i = 0;
    var response = xmlobj.getElementsByTagName('Response')[0];
	var element = response.firstChild;
	array[i] = element.firstChild.nodeValue;
	while ( element = element.nextSibling )
	{
		i ++;
		array[i] = element.firstChild.nodeValue;
		}
	return array;
}

oAjax.prototype.getForm = function(theFormID){
  var form = document.forms[theFormID];
   if(typeof form.name == "undefined" || form.name==""){ form = $(theFormID);}

   var formData = "";
   var element;
   for (var i = 0; i < form.elements.length; i++) {
      element = form.elements[i];
      var type=element.type.toLowerCase();      
      if (type == "hidden" ||type == "text" || type == "password" || type == "textarea"){      	
       formData += $U(element.name) + "=" + $U(element.value) + "&";
	  }else if (element.type.indexOf("select") != -1 && element.selectedIndex>=0) {
            formData +=   $U(element.name) + "=" + $U(element.options[element.selectedIndex].value) + "&";
      }
      else if (element.type == "checkbox") {
		 if (element.checked) formData +=  $U(element.name) + "=" + $U(element.value) + "&";
	  }
      else if (element.type == "radio") {
		 if (element.checked) formData +=  element.name + "=" + $U(element.value) + "&";
	  }
   }
   return formData.substring(0, formData.length - 1);
}
oAjax.prototype.loading = function(SpanID,simgRUL,strStText){
    //if(!simgRUL) {simgRUL = "../ajax/loader3.gif";}
    simgRUL=simgRUL?simgRUL:"../ajax/loader3.gif";
    strStText=strStText?strStText:" Loading ...";
    SpanID=SpanID?SpanID:_oSpanID;
    //if(!SpanID) {SpanID = _oSpanID;}
    if($(SpanID)){ 
    _oSpanID=SpanID;
    document.getElementById(SpanID).innerHTML="<img id=\"ajaxLoadImg\" src=\"" + simgRUL + "\" style=\" height:16px; width:16px;\"/>"+strStText;
    //this.loadingFlag=true;
    _loadingFlag=true;
    }
}
oAjax.prototype.loadDisplay = function(){
if(_loadingFlag&& $(_oSpanID)){
$(_oSpanID).style.display = "";
}
}
oAjax.prototype.loadDelete = function(){
if(_loadingFlag&& $(_oSpanID)){
$(_oSpanID).style.display = "none";
_loadingFlag=false;
}
}
function createLoading(strPath,divID)
{ if(!strPath) {strPath = "../resource/pic/load.gif";}
  if(!divID) {divID = "loading";}
  document.getElementById(divID).innerHTML="<img src=\"" + strPath + "\" style=\"position: absolute;top: 5px;right: 5px; height:25px; width:25px\"></img> Loading ...";
  document.getElementById(divID).style.display = "block";
}

function displayLoading(strPath,divID,strDisplay)
{ 
  if(!divID) {divID = "loading";}
  if(strPath && strDisplay) {document.getElementById(divID).innerHTML="<img src=\"" + strPath + "\" ></img>" + strDisplay ;}
  else if(strPath) { document.getElementById(divID).innerHTML="<img src=\"" + strPath + "\" ></img>"; }
  else if(strDisplay){document.getElementById(divID).innerHTML= strDisplay ;}
  document.getElementById(divID).style.display = "block";
}

function deleteLoading()
{ 
    if(!divID) {divID = "loading";}
    if(document.getElementById(divID)) {document.getElementById(divID).style.display = "none";}
}

function getTagValue(doc, tag){
     if(!doc.getElementsByTagName(tag)[0] || doc.getElementsByTagName(tag)[0].firstChild==null){return '';}
     return doc.getElementsByTagName(tag)[0].firstChild.nodeValue;
}

function $(obj){
  return document.getElementById(obj);	
}

function $F(obj){
  return (document.getElementById(obj)?document.getElementById(obj).value.trim():"");
}
function $U(str){
  return str?escape(str).replace(/\+/g,"%2b"):"";	
}

String.prototype.trim =function() {   
if(!this){return '';}
    return this.replace(/(^[  \s\u3000]*)|([  \s\u3000]*$)/g, "");  
}

