﻿var _SARISSA_IS_IE = document.all && window.ActiveXObject && navigator.userAgent.toLowerCase().indexOf("msie") > -1  && navigator.userAgent.toLowerCase().indexOf("opera") == -1;
var Ajax = new Object();
if( window.XMLSerializer )
{	
	__XMLSerializer = new XMLSerializer();
}
Ajax.XmlSerializer = function(){}
Ajax.XmlSerializer.Serialize = function(anyObject, objectName, indentSpace){
    indentSpace = indentSpace?indentSpace:'';
    var s = indentSpace  + '<' + objectName + '>';
    var isLeaf = false;
    if(!(anyObject instanceof Object) || anyObject instanceof Number || anyObject instanceof String 
        || anyObject instanceof Boolean || anyObject instanceof Date){
        s += Ajax.XmlSerializer.Escape(""+anyObject);
        isLeaf = true;
    }else{
        s += "\n";
        var itemKey = '';
        var isArrayItem = anyObject instanceof Array;
        for(var name in anyObject){
            s += Ajax.XmlSerializer.Serialize(anyObject[name], (isArrayItem?"array-item key=\""+name+"\"":name), indentSpace + "   ");
        };
        s += indentSpace;
    };
    return s += (objectName.indexOf(' ')!=-1?"</array-item>\n":"</" + objectName + ">\n");
};

Ajax.XmlSerializer.Escape = function(sXml){
    return sXml.replace(/&/g, "&amp;")
        .replace(/</g, "&lt;")
        .replace(/>/g, "&gt;")
        .replace(/"/g, "&quot;")
        .replace(/'/g, "&apos;");
};
Ajax.XmlSerializer.getXml = function( node )
{
	if(!window.XMLSerializer)
	{	
		return node.xml;
	}
	else
	{
		return __XMLSerializer.serializeToString( node );
	}
}

Ajax.Dom = function(){}
Ajax.Dom.getChildNodes = function ( node )
{
	if( !node.childNodes ) return null;
	var nodeQueue = [];
	for( var i = 0; i < node.childNodes.length; i++ )
	{
		if( node.childNodes[ i ].nodeType == 1 && node.childNodes[ i ].parentNode == node )
		{
			nodeQueue.push( node.childNodes[ i ] );
		}
	}
	if( nodeQueue.length == 0 ) return null;
	return nodeQueue;
}

Ajax.READY_STATE_UNINITIALIZED=0;
Ajax.READY_STATE_LOADING=1;
Ajax.READY_STATE_LOADED=2;
Ajax.READY_STATE_INTERACTIVE=3;
Ajax.READY_STATE_COMPLETE=4;


/*--- content loader object for cross-browser requests ---*/
Ajax.ContentLoader=function(url,onload,onerror,method,params,contentType){
  this.req=null;
  Ajax.currentLoader=this;
  this.onload=onload;
  this.onerror=(onerror) ? onerror : this.defaultError;
  this.loadXMLDoc(url,method,params,contentType);
}

Ajax.ContentLoader.prototype.loadXMLDoc=function(url,method,params,contentType){
  if (!method){
    method="GET";
  }
  //window.prompt("问题:",url);
  if( method=="POST" && !contentType && contentType != null ) 
  {
	if( contentType.toUpperCase() = "FORM" && method=="POST")
	{
      contentType='application/x-www-form-urlencoded;charset=utf-8';
	}
  }
  
  if (!_SARISSA_IS_IE){
    this.req=new XMLHttpRequest();
  } else if (_SARISSA_IS_IE){
    this.req=new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (this.req){
    try{
      var loader=this;
      this.req.onreadystatechange=function(){
        Ajax.ContentLoader.onReadyState.call(loader);
      }
      this.req.open(method,url,true);
      if (contentType){
        this.req.setRequestHeader('Content-Type', contentType);
      }
      this.req.send(params);
    }catch (err){
      this.onerror.call(this);
    }
  }
}


Ajax.ContentLoader.onReadyState=function(){
  var req=this.req;
  var ready=req.readyState;
  if (ready==Ajax.READY_STATE_COMPLETE){
     var httpStatus=req.status;
    //if (status==200 || httpStatus==0){
    if(httpStatus==200 || httpStatus==0){
      this.onload.call(this);
    }else{
      this.onerror.call(this);
    }
  }
}

Ajax.ContentLoader.prototype.defaultError=function(){
  alert("error fetching data!"
    +"\n\nreadyState:"+this.req.readyState
    +"\nstatus: "+this.req.status
    +"\nheaders: "+this.req.getAllResponseHeaders());
}


if( document.implementation.hasFeature("XPath", "3.0") )
{  
	// prototying the XMLDocument  
	XMLDocument.prototype.selectNodes = function(cXPathString, xNode)  
	{     
		if( !xNode ) 
		{ xNode = this; }      
		var oNSResolver = this.createNSResolver(this.documentElement)
		var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
		var aResult = [];
		for( var i = 0; i < aItems.snapshotLength; i++)
		{
			aResult[i] =  aItems.snapshotItem(i);
		}
		return aResult;
	}  
	// prototying the Element  
	Element.prototype.selectNodes = function(cXPathString)  
	{     
		if(this.ownerDocument.selectNodes)     
		{        
			return this.ownerDocument.selectNodes(cXPathString, this);     
		}     
		else
		{
			throw "For XML Elements Only";
		}  
	}
	// prototying the XMLDocument
	XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)  
	{     
		if( !xNode ) 
		{ 
			xNode = this; 
		}
		var xItems = this.selectNodes(cXPathString, xNode);
		if( xItems.length > 0 )
		{ 
			return xItems[0]; 
		}     
		else     
		{   return null;     
		}  
	}    
	// prototying the Element  
	Element.prototype.selectSingleNode = function(cXPathString)  
	{         
		if(this.ownerDocument.selectSingleNode)     
		{        
			return this.ownerDocument.selectSingleNode(cXPathString, this);     
		}     
		else
		{
			throw "For XML Elements Only";
		}
	}
}


