﻿

function sendAjax(ajaxObj,type,url,value){

    ajaxObj.appendParam("type",type);
    ajaxObj.appendParam("url",url);
    ajaxObj.appendParam("value",value);
    ajaxObj.send();      
}


//给string增加去首尾空白的方法
String.prototype.trim = function(){return this.replace(/(^\s*)|(\s*$)/g, "");}

var urlDefault = window.location.pathname + window.location.search;
var paramArray = {url:urlDefault,type:"get",value:null};

function AjaxHelper()
{
  this.XmlHttp = this.getHttpObject();
}
 

AjaxHelper.prototype.getHttpObject = function()
{ 
  var xmlhttp = false;
        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            xmlhttp = new XMLHttpRequest();
        } 
        else if (window.ActiveXObject) { // IE
            try {
               
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!xmlhttp) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
              
  return xmlhttp;
}

AjaxHelper.prototype.appendParam =function(name,valueOut)
{   
    paramArray[name] = valueOut;

}

AjaxHelper.prototype.doCallBack = function(eventTarget, eventArgument)
{
  var theData = '';
  var theform = document.forms[0];
  var thePage = window.location.pathname + window.location.search;
  var eName = '';
 
  theData  = '__EVENTTARGET='  + escape(eventTarget.split("$").join(":")) + '&';
  theData += '__EVENTARGUMENT=' + eventArgument + '&';
  theData += '__VIEWSTATE='    + escape(theform.__VIEWSTATE.value).replace(new RegExp('\\+', 'g'), '%2b') + '&';
  theData += 'IsCallBack=true&';
  for( var i=0; i<theform.elements.length; i++ )
  {
    eName = theform.elements[i].name;
    if( eName && eName != '')
    {
      if( eName == '__EVENTTARGET' || eName == '__EVENTARGUMENT' || eName == '__VIEWSTATE' )
      {
        // Do Nothing
      }
      else
      {
        theData = theData + escape(eName.split("$").join(":")) + '=' + theform.elements[i].value;
        if( i != theform.elements.length - 1 )
          theData = theData + '&';
      }
    }
  }
 
  if( this.XmlHttp )
  {
    if( this.XmlHttp.readyState == 4 || this.XmlHttp.readyState == 0 )
    {
      var oThis = this;
      oThis.XmlHttp.open('POST', thePage, true);
      oThis.XmlHttp.onreadystatechange = function(){ oThis.readyStateChange(); };
      oThis.XmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      oThis.XmlHttp.send(theData);
    }
  }
}
 
AjaxHelper.prototype.isAvailable = function(){
	if(this.XmlHttp.readyState == 4 || this.XmlHttp.readyState == 0)
	  return true;
	return false;
}

AjaxHelper.prototype.send = function()
{      
   var url =  paramArray["url"];
   var type=  paramArray["type"];     
   var value= paramArray["value"];  
   
    /*
   if (url.indexOf("?") > 0)
        {
           url += "&randnum=" + Math.random();
        }
    else
        {
           url += "?randnum=" + Math.random();
        }
   
      */
    if(this.XmlHttp)
      {
			if( this.XmlHttp.readyState == 4 || this.XmlHttp.readyState == 0 )
			{
				var oThis = this;
				this.XmlHttp.open(type,url,true);
				this.XmlHttp.onreadystatechange = function(){ oThis.readyStateChange(); };
				if(type=="post")
				  this.XmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				//this.XmlHttp.overrideMimeType('text/xml');
				this.XmlHttp.send(value);
			    //$8("debug").innerHTML += "<a href='" +  url + "' target='_blank'>" +url+"</a><Br/>";
			    //alert(url + "\n" + type +"\n value" + value + "\n");
			}
     }
}


AjaxHelper.prototype.abort = function()
{
  if( this.XmlHttp )
    this.XmlHttp.abort();
}
 
AjaxHelper.prototype.onLoading = function()
{
  // Loading
}
 
AjaxHelper.prototype.onLoaded = function()
{
  // Loaded
}
 
AjaxHelper.prototype.onInteractive = function()
{
  // Interactive
}
 
AjaxHelper.prototype.onComplete = function(responseText, responseXml)
{
  // Complete
}
 
AjaxHelper.prototype.onAbort = function()
{
  // Abort
}
 
AjaxHelper.prototype.onError = function(status, statusText)
{
  // Error
}
 
AjaxHelper.prototype.readyStateChange = function()
{

  if( this.XmlHttp.readyState == 1 )
  {
    this.onLoading();
  }
  else if( this.XmlHttp.readyState == 2 )
  {
    this.onLoaded();
  }
  else if( this.XmlHttp.readyState == 3 )
  {
    this.onInteractive();
  }
  else if( this.XmlHttp.readyState == 4 )
  {
    //var s = typeof(this.XmlHttp.status);
    //alert(this.XmlHttp.readyState );
    if( this.XmlHttp.status == 0 )
      this.onAbort();
    else if( this.XmlHttp.status == 200 && this.XmlHttp.statusText == "OK" )
     //alert("response text" + this.XmlHttp.responseText);
    //if(this.XmlHttp.responseText)
      this.onComplete(this.XmlHttp.responseText, this.XmlHttp.responseXML);
    else
      this.onError(this.XmlHttp.status, this.XmlHttp.statusText, this.XmlHttp.responseText);   
      //this.onError(100, "error", this.XmlHttp.responseText);
  }
}


