//ajax functions, weng nanshan, 2008-4-14

//Create a xmlhttp object according to different browser
//return: a xmlhttp object
function CreateObjXmlhttp()
{
	var obj = null; 
	    
	try 
	{ 
		obj=new ActiveXObject("Msxml2.XMLHTTP"); 
	} 
	catch(e)
	{ 
		try 
		{ 
			obj=new ActiveXObject("Microsoft.XMLHTTP"); 
		}
		catch(oc)
		{ 
			obj=null; 
		} 
	} 
	if( !obj && typeof XMLHttpRequest != "undefined" ) 
	{ 
		obj=new XMLHttpRequest();
	} 
	return obj; 
}


function SendToServer(weburl)
{   
    //初始化个xmlhttp对象   
    var xmlhttp = CreateObjXmlhttp();
    
    //提交数据，第一个参数最好为get，第三个参数最好为true   
    xmlhttp.open("get",weburl,true);//asyn mode
   
    if(xmlhttp.readyState==1)
    {
		//发送数据，请注意顺序和参数，参数一定为null或者""   
		xmlhttp.send(null);
    }
}

//use "get" to obtain data from server, syn mode
function GetFromServer(weburl, tagname)
{
	//初始化个xmlhttp对象   
    var xmlhttp = CreateObjXmlhttp();
    
    xmlhttp.open("get",weburl,false);//syn mode
    
    if(xmlhttp.readyState==1)
	{
		//发送数据，请注意顺序和参数，参数一定为null或者""   
		xmlhttp.send(null);
	}
	
	if (xmlhttp.status == 200) 
    {
        var result = xmlhttp.responseXML;//得到服务器返回的数据
        
        //var result1 = xmlhttp.responseText;//得到服务器返回的数据
			                
        var node = result.getElementsByTagName(tagname);   					
		return node[0].firstChild.nodeValue;
    }
    
    return null;

    
    /*xmlhttp.onreadystatechange = function()
    {
		alert("5");
		
		if(xmlhttp.readyState==4)
		{
			alert("6");
			var result = xmlhttp.responseText;//得到服务器返回的数据
			
			alert("result:"+ result);
			
			return result;
		}
		
    }*/
}







//http get
function RequestHttpGet(weburl)
{
    var xmlhttp = CreateObjXmlhttp();
    
    //提交数据，第一个参数最好为get，第三个参数最好为true   
    xmlhttp.open("get",weburl,true);   
   
    if(xmlhttp.readyState==1)
    {
		//发送数据，请注意顺序和参数，参数一定为null或者""   
		xmlhttp.send(null);
    }
}

//http post
function RequestHttpPost(weburl)
{
    var xmlhttp = CreateObjXmlhttp();
    
    xmlhttp.open("post",weburl,true);   
   
    if(xmlhttp.readyState==1)
    {
		xmlhttp.send(null);
    }
}

//call webservice by Get
function RequestServiceGet(weburl)
{
	RequestHttpGet(weburl);
}

//call webservice by post. soap1.2
//data: e.g. 'city=shanghai'
function RequestServicePost(weburl,data)
{
	var xmlhttp = CreateObjXmlhttp();
	
    //Webservice location.
    xmlhttp.Open("Post",weburl, false);
    xmlhttp.SetRequestHeader ("Content-Type","  application/x-www-form-urlencoded"); 
    xmlhttp.SetRequestHeader ("Content-Length",data.length);
    
    if(xmlhttp.readyState==1)
    {     
		xmlhttp.Send(data);
    }
}




//Call webservice using xmlhttp, by Get
function RequestByGet()
{
	var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
	//Webservice location.
	var URL="http://localhost:1323/WebSite6/Service.asmx/SayHelloTo?Name=Zach";
	xmlhttp.Open("GET",URL, false); 
	xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8"); 
	xmlhttp.SetRequestHeader ("SOAPAction","http://tempuri.org/SayHelloTo"); 
	xmlhttp.Send(null); 
	var result = xmlhttp.status;
	
	//OK
	if(result==200)
	{ 
		document.write(xmlhttp.responseText); 
	} 
	xmlhttp = null; 
} 


