/*------------------------------------------
Author: xujiwei
Website: http://www.xujiwei.cn
E-mail: vipxjw@163.com
Copyright (c) 2006, All Rights Reserved
------------------------------------------*/
function $(id){
   return document.getElementById(id);
}
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;
	if(arguments[0]) this.url=arguments[0]; else this.url="";
	if(arguments[1]) this.callback=arguments[1]; else this.callback=function(obj){return};
	if(arguments[2]) this.content=arguments[2]; else this.content="";
	if(arguments[3]) this.method=arguments[3]; else this.method="POST";
	if(arguments[4]) this.async=arguments[4]; else this.async=true;
	this.send=function() {
		var purl,pcbf,pc,pm,pa;
		if(arguments[0]) purl=arguments[0]; else purl=this.url;
		if(arguments[1]) pc=arguments[1]; else pc=this.content;
		if(arguments[2]) pcbf=arguments[2]; else pcbf=this.callback;
		if(arguments[3]) pm=arguments[3]; else pm=this.method;
		if(arguments[4]) pa=arguments[4]; else pa=this.async;
		if(!pm||!purl) return false;
		xmlObj.open (pm, purl, pa);
		if(pm=="POST") xmlObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		xmlObj.onreadystatechange=function() {
			if(xmlObj.readyState==4) {
				if(xmlObj.status==200) {
					pcbf(xmlObj);
				}
				else {
					pcbf(null);
				}
			}
		}
		if(pm=="POST")
			xmlObj.send(pc);
		else
			xmlObj.send("");
	}
	this.get=function() {
		var purl,pcbf;
		if(arguments[0]) purl=arguments[0]; else purl=this.url;
		if(arguments[1]) pcbf=arguments[1]; else pcbf=this.callback;
		if(!purl&&!pcbf) return false;
		this.send(purl,"",pcbf,"GET");
	}
	this.post=function() {
		var fo,pcbf,purl,pc,pm;
		if(arguments[0]) fo=arguments[0]; else return false;
		if(arguments[1]) pcbf=arguments[1]; else pcbf=this.callback;
		if(arguments[2])
			purl=arguments[2];
		else if(fo.action)
			purl=fo.action;
		else
			purl=this.url;
		if(arguments[3])
			pm=arguments[3];
		else if(fo.method)
			pm=fo.method.toLowerCase();
		else
			pm="post";
		if(!pcbf&&!purl) return false;
		pc=this.formToStr(fo);
		if(!pc) return false;
		if(pm) {
			if(pm=="post")
				this.send(purl,pc,pcbf,"POST");
			else
				if(purl.indexOf("?")>0)
					this.send(purl+"&"+pc,"",pcbf,"GET");
				else
					this.send(purl+"?"+pc,"",pcbf,"GET");
		}
		else
			this.send(purl,pc,pcbf,"POST");
      return xmlObj;
			
	}
	// formToStr
	// from SurfChen <surfchen@gmail.com>
	// @url     http://www.surfchen.org/
	// @license http://www.gnu.org/licenses/gpl.html GPL
	// modified by xujiwei
	// @url     http://www.xujiwei.cn/
	this.formToStr=function(fc) {
		var i,query_string="",and="";
		for(i=0;i<fc.length;i++) {
			e=fc[i];
			if (e.name!='' && e.disabled==false) {
				if (e.type=='select-one') {
                                    if (e.options.length >0)
					element_value=e.options[e.selectedIndex].value;
                                    else 
                                        element_value ="";
				}
            else if (e.type=='select-multiple') {
               for (j = 0; j < e.options.length; j ++){
                  if (e.options[j].selected){
					      element_value=e.options[j].value;
				         query_string+=and+e.name+'='+element_value;
                     and = "&";
                  }
               }
               continue;
				}
				else if (e.type=='checkbox' || e.type=='radio') {
					if (e.checked==false) {
						continue;	
					}
					element_value=e.value;
				}
				else {
					element_value=e.value;
				}
				element_value=encodeURIComponent(element_value);
				query_string+=and+e.name+'='+element_value;
				and="&";
			}
		}
		return query_string;
	}
}
function getXMLDocument(url){
   req = false;
   if(window.XMLHttpRequest) {
      try {
         req = new XMLHttpRequest();
      } catch(e) {
         req = false;
      }
   }
   else if(window.ActiveXObject)
   {
      try {
         req = new ActiveXObject("Msxml2.XMLHTTP");
      } catch(e) {
         try {
            req = new ActiveXObject("Microsoft.XMLHTTP");
         } catch(e) {
            req = false;
         }
      }
   }
   if(req) {
      req.open("GET", url, false);
      req.send("");
      return req.responseXML;
   }
   return null;



}



var getDOMAtt=function(pNode,pAttribute){
   try{
      return pNode.attributes.getNamedItem(pAttribute).nodeValue;
   }catch(e){
      //alert("指定节点不存在，或指定属性："+pAttribute+" 不存在!")
      return false;
   }

}




function Green_Exec_command(URL){
var result = 0;
 
   try{
   var doc = getXMLDocument(URL).documentElement.getElementsByTagName('result');

	result = getDOMAtt(doc[0], "value");
    
   } catch (e){
      result = -1;
   } 
  if ( result == 1)
  {
     
      return true;
  }
  else 
     
  return false;
}



function Exec_Command(URL){
   var result = 0;
   try{
 
      result =Green_Exec_command(URL);
   } catch (e){
      result = -1;
   } 
  if ( result == 1)
  {
      alert('操作成功!');
      return true;
  }
  else 
      alert('操作失败!');
  return false;
}
function get_value_by_xpath(doc, xpath){
	if(document.all){
		return doc.documentElement.selectNodes(xpath)[0].nodeValue;
   }
	else
	{
		var xpath_result;
		xpath_result=doc.evaluate( xpath, doc, null, XPathResult.STRING_TYPE, null );
		return  xpath_result.stringValue;
	}
}

