﻿function netRequest(urls,onloads,onerror,method)
{
	this.net =new Object();
	this.net.READY_STATE_UNINITIALIZED=0;
    this.net.READY_STATE_LOADING=1;
    this.net.READY_STATE_LOADED=2;
    this.net.READ_STATE_INTERACTIVE=3;
    this.net.READY_STATE_COMPLETE=4;
    this.net.url =urls;
	this.net.req=null;
	this.net.onload=onloads;
	this.net.method=method;
//GET方式直接获取XML数据
    this.net.loadXMLDoc=function()
	{
	    if(window.ActiveXObject )
		{
			 try
			{

				this.req =new ActiveXObject("Msxml2.XMLHTTP") ;
			}
			catch(er)
			{
				this.req =new ActiveXObject("Microsoft.XMLHTTP") 
			}
		}
		else if(window.XMLHttpRequest)
		{
			this.req=new XMLHttpRequest();
		}
		
		if(this.req)
		{
			
			try
			{
				var loader =this;
				this.req.onreadystatechange=function(){loader.onReadyState.call(loader);}
				this.req.open('GET',this.url,true);
				if   (window.XMLHttpRequest) 
				this.req.send(null);
				else   if   (window.ActiveXObject) 
				this.req.send();
				
			}
			catch(err)
			{
				this.onerror.call(this);
			}
		}
	};
	//POST方式与服务器进行交互获取XML数据
    this.net.requestXMLDoc=function(paramters)
	{
		
	    if(window.ActiveXObject )
		{
			 try
			{

				this.req =new ActiveXObject("Msxml2.XMLHTTP") ;
			}
			catch(er)
			{
				this.req =new ActiveXObject("Microsoft.XMLHTTP") 
			}
		}
		else if(window.XMLHttpRequest)
		{
			this.req=new XMLHttpRequest();
			  
		}
		
		if(this.req)
		{
			 
			try
			{
				var loader =this;
				this.req.open('POST',this.url,true);
				
				this.req.setRequestHeader("Host","localhost");
				this.req.setRequestHeader("Content-Length","1024");
				//alert('a');
				this.req.setRequestHeader ("Content-Type","application/soap+xml; charset=utf-8");
				
				this.req.send(buildparam(paramters,this.method));
				this.req.onreadystatechange=function(){loader.onReadyState.call(loader);}
				
			}
			catch(err)
			{
				this.onerror.call(this);
			}
		}
	};
    this.net.onReadyState=function()
	{
		var req=this.req;
		var ready =req.readyState;
		if(ready==this.READY_STATE_COMPLETE)
		{
			var httpStatus =req.status;
			if(httpStatus==200 || httpStatus==0)
			{
				
				this.onload.call(this);
			}else
			{
				//alert('a');
				this.onerror.call(this);
			}
		}
		
	};
    this.net.defaultError=function()
	{
		//alert("error fetching data!"+"\n\nreadyState:"+this.req.readyState+"\nstatus:"+this.req.status+"\nheaders:"+this.req.getAllResponseHeaders());
	};
	this.net.onerror=(onerror)?onerror:this.net.defaultError;

};
netRequest.prototype.loadXMLDoc=function()
{
    
	this.net.loadXMLDoc(this.net.url);
		
}
netRequest.prototype.requestXMLDoc=function(params)
{
	this.net.requestXMLDoc(params);
}
//从str构造中构造soap的xml对象.作用转换http post 到soap
function buildparam(str,method)
{
	
	var header ="<?xml version=\"1.0\" encoding=\"utf-8\"?>";
	header =header+"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\"><soap12:Body>";
	header=header+"<"+method+" xmlns=\"http://huanke.rungoo.cn/\">";
	var param =str.split("&");
	
	for(i=0;i<param.length;i++)
	{
		var innerparam =param[i].split("=");
		header=header+"<"+innerparam[0]+">"+innerparam[1]+"</"+innerparam[0]+">";
	}
	header=header+"</"+method+">";
	header=header+"</soap12:Body>";
  header=header+"</soap12:Envelope>";

  return header;
}

function XmlDocument()
{
	var moz = (typeof document.implementation != 'undefined')   && (typeof document.implementation.createDocument != 'undefined'); 
	var ie = (typeof window.ActiveXObject != 'undefined');
	var xmlDoc=null;
	if (moz)  
	{   
		xmlDoc = document.implementation.createDocument("", "doc", null);//创建FIREFOX下XML文档对象
		xmlDoc.loadXML = function(xmlString)
    		{
			
    			var childNodes = this.childNodes;
    			for (var i = childNodes.length - 1; i >= 0; i--)
        			this.removeChild(childNodes[i]);
			var dp = new DOMParser();
    			var newDOM = dp.parseFromString(xmlString, "text/xml");
    			var newElt = this.importNode(newDOM.documentElement, true);
    			this.appendChild(newElt);
    		};

	}  
	else if (ie)  
	{   
		xmlDoc =new ActiveXObject("Microsoft.XMLDOM"); 
		
	}
	return xmlDoc;
}
//判断iE
    var userAgent = navigator.userAgent.toLowerCase();
    var is_opera = (userAgent.indexOf('opera') != -1);
    var is_saf = ((userAgent.indexOf('applewebkit') != -1) || (navigator.vendor == 'Apple Computer, Inc.'));
    var is_webtv = (userAgent.indexOf('webtv') != -1);
    var is_ie = ((userAgent.indexOf('msie') != -1) && (!is_opera) && (!is_saf) && (!is_webtv));
    var is_ie4 = ((is_ie) && (userAgent.indexOf('msie 4.') != -1));
    var is_ie7 = ((is_ie) && (userAgent.indexOf('msie 7.') != -1));
    var is_moz = ((navigator.product == 'Gecko') && (!is_saf));
    var is_kon = (userAgent.indexOf('konqueror') != -1);
    var is_ns = ((userAgent.indexOf('compatible') == -1) && (userAgent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_saf));
    var is_ns4 = ((is_ns) && (parseInt(navigator.appVersion) == 4));
    var is_mac = (userAgent.indexOf('mac') != -1);
//取得字符串的字节长度
function strlen(str)
{
	var i=0,len=0;
	for (i=0;i<str.length;i++){
		if (str.charCodeAt(i)>255 || str.charCodeAt(i)<0)
		{len+=2;}
		else
		{len++;}
	}
	return len;
}
//写字版复制粘贴
function copyToClipboard(txt) {  
    if(window.clipboardData)  
    {  
        window.clipboardData.clearData();  
        window.clipboardData.setData("Text", txt);  
    }  
    else if(navigator.userAgent.indexOf("Opera") != -1)  
    {  
        window.location = txt;  
    }  
    else if (window.netscape)  
    {  
        try {  
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  
        }  
        catch (e)  
        {  
            alert("!!被浏览器拒绝！\n请在浏览器地址栏输入'about:config'并回车\n然后将'signed.applets.codebase_principal_support'设置为'true'");  
        }  
        var clip = Components.classes["@mozilla.org/widget/clipboard;1"].createInstance(Components.interfaces.nsIClipboard);  
        if (!clip)  
            return;  
        var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);  
        if (!trans)  
            return;  
        trans.addDataFlavor("text/unicode");  
        var str = new Object();  
        var len = new Object();  
        var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);  
        var copytext = txt;  
        str.data = copytext;  
        trans.setTransferData("text/unicode",str,copytext.length*2);  
        var clipid = Components.interfaces.nsIClipboard;  
        if (!clip)  
            return false;  
        clip.setData(trans,null,clipid.kGlobalClipboard);  
        }  
    return true;  
}

//
function Confirm(n) {return window.confirm (n)}
function afirm(n) {return window.confirm (n)}
function Trim(s){
	return s.replace(/(^\s*)|(\s*$)/g, "");
}
function checknumber(data)
{
  var datastr = data;
  if (datastr.search(/\D/gi) != -1)
	  {return false;}
  if (datastr.search(/\S/gi) == -1)
	  {return false;}
  return true;
}
//function $(s){ return document.getElementById(s);}
function gname(name){return document.getElementsByTagName?document.getElementsByTagName(name):new Array()}
function hide(s){$("#"+s).style.display=$("#"+s).style.display=="none"?"":"none";}
//判断输入的内容不能为空
function checkcoAndsn(form)
{
    var inputs =form.getElementsByTagName("INPUT");
    var textarea =form.getElementsByTagName("TEXTAREA");
    var objary =new Array();   
    for(i=0;i<textarea.length;i++)
    {   
        if(textarea[i].title=="必填"&&textarea[i].value=="")
        {
            textarea[i].focus();
            textarea[i].value="内容不能为空";
            textarea[i].select();
           objary.push(textarea[i]);
        } 
    }
    for(i=0;i<inputs.length;i++)
    {   
        if(inputs[i].title=="必填"&&inputs[i].value=="")
        {
            inputs[i].focus();
            inputs[i].value="内容不能为空";
            inputs[i].select();
            objary.push(inputs[i]);
        }
       if(inputs[i].title=="必填整数"&&(inputs[i].value==""||checknumber(inputs[i].value)==false))
        {
            inputs[i].focus();
            inputs[i].value="必填整数";
            inputs[i].select();
            objary.push(inputs[i]);
        }
    }
      if(objary.length==0)
            return true;
      else 
        return false;
}
function oAjax( url ,callback)
{
    try{
        this.HttpRequest = null;
        this.Debug  = false;
        this.Url = url;
        this.ContentType = "text/xml;charset=utf-8";
        this.HttpRequest = this.createXMLHttpRequest();
        this.yibu=true;
        if(callback==null)
            yibu=false;
        if ( this.HttpRequest == null )
        {
            this._debug("XMLHttpRequest create failure!");
            return;
        }

        var xhReq = this.HttpRequest;
        if(callback!=null){
        xhReq.onreadystatechange = function (){
                    oAjax._OnReadyStateChange( xhReq,callback );
            }
         }

    } catch(e){
       this._debug( "unknow err: " + e.message );
    }
}

/*
 * Get URL resource
 */
oAjax.prototype.Get = function() {

    this.SetContentType( "text/html;charset=utf-8" );
    this._get();
}

/*
 * Post data to the server
 */
oAjax.prototype.Post = function( arrKey, arrValue ) {

    var data = '';
    this.SetContentType( "application/x-www-form-urlencoded;charset=utf-8" );
    for( i = 0; i < arrKey.length; i ++)
    {
        data += "&" + escape(arrKey[i]) + "=" + escape(arrValue[i]);
		//data += "&" + arrKey[i] + "=" + arrValue[i];
    }
	//document.write(data);
    data = data.replace(/^&/g, "");
    this._post(data);
}

/*
 * Initialization for oAjax class
 */
oAjax.prototype.Init = function() {
    // initialization
}

/*
 * Change URL for request
 */
oAjax.prototype.SetUrl = function( url ) {
    this.Url = url;
}

/*
 * Set content type for HTTP header before sending request
 */
oAjax.prototype.SetContentType = function( type ) {
    this.ContentType = type;
}

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;    }
    
    /*Status is completed, then process result */
    if ( xreq.readyState == 4)
    {
        // OK        
        if ( xreq.status == 200 )
        {
			//alert(xreq.responseText);
          	callback (this.ArrayValue(xreq.responseXML) );
        }else{
//	
		document.write (xreq.responseText);
		}
    } else {
        // Others
    }
}
oAjax.prototype.GetResponseTxt=function(HttpMethod,data)
{
    this._debug( 'Send Request ' + HttpMethod + data );
    
    if ( this.HttpRequest != null )
    {
        this.HttpRequest.open(HttpMethod, this.Url, false);

        if ( this.ContentType != null )
        {
            //  <FORM> MIME type: application/x-www-form-urlencoded
            this.HttpRequest.setRequestHeader("Content-Type", this.ContentType);
        }
        this.HttpRequest.send(data);
        return this.HttpRequest.responseText;
    }
    return null;
}
oAjax.prototype.GetResponseArray=function(HttpMethod,data)
{
    this._debug( 'Send Request ' + HttpMethod + data );
    
    if ( this.HttpRequest != null )
    {
        this.HttpRequest.open(HttpMethod, this.Url, false);

        if ( this.ContentType != null )
        {
            //  <FORM> MIME type: application/x-www-form-urlencoded
            this.HttpRequest.setRequestHeader("Content-Type", this.ContentType);
        }
        this.HttpRequest.send(data);
        var array = new Array();
        var i = 0;
        var response = this.HttpRequest.responseXML.getElementsByTagName('Response')[0];
	    var element = response.firstChild;
	    array[i] = element.firstChild.nodeValue;
	    while ( element = element.nextSibling )
	    {
		    i ++;
		    array[i] = element.firstChild.nodeValue;
		}
	    return array;
    }
    return null;
}
oAjax.prototype._SendRequest = function(HttpMethod, data){

    this._debug( 'Send Request ' + HttpMethod + data );
    
    if ( this.HttpRequest != null )
    {
        this.HttpRequest.open(HttpMethod, this.Url, this.yibu);

        if ( this.ContentType != null )
        {
            //  <FORM> MIME type: application/x-www-form-urlencoded
            this.HttpRequest.setRequestHeader("Content-Type", this.ContentType);
        }
        this.HttpRequest.send(data);
        return true;
    }
    return false;
}

/* Send GET request to server */
oAjax.prototype._get = function () {

    this._debug( 'GET' );
    return this._SendRequest("GET", null);
}

/* Send POST request and data to server */
oAjax.prototype._post = function (data) {

    this._debug( 'POST' );
    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;
}
function AJAXRequest() {
	var xmlObj = false;
	var CBfunc,ObjSelf;
	ObjSelf=this;
	try { xmlObj=new XMLHttpRequest; }
	catch(e) {
		try { xmlObj=new ActiveXObject("MSXML2.XMLHTTP"); }
		catch(e2) {
			try { xmlObj=new ActiveXObject("Microsoft.XMLHTTP"); }
			catch(e3) { xmlObj=false; }
		}
	}
	if (!xmlObj) return false;
	this.method="POST";
	this.url;
	this.async=true;
	this.content="";
	this.callback=function(cbobj) {return;}
	this.send=function() {
		if(!this.method||!this.url||!this.async) return false;
		xmlObj.open (this.method, this.url, this.async);
		if(this.method=="POST") xmlObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		xmlObj.onreadystatechange=function() {
			if(xmlObj.readyState==4) {
				if(xmlObj.status==200) {
					ObjSelf.callback(xmlObj);
				}
			}
		}
		if(this.method=="POST") xmlObj.send(this.content);
		else xmlObj.send(null);
	}
}