

var isDebug=true;
isDebug=false;
var __isReading=false; 
var __getFromUrl="";
//////////////////////
var request;
var queryString; //will hold the POSTed data
function getData(url){
     queryString="";
     __isReading=true;
     httpRequest("GET",url,true);
    //when call this just send();
}

function sendData(url,str){
	queryString=str;
	//alert(str);
	//alert(url);
                __isReading=false;
	httpRequest("POST",url,true);
}

/* Initialize a Request object that is already constructed
 para1 -- reqType: The HTTP request type such as "GET" or "POST." 
 para2 -- url: The URL of the server program.
 para3 -- isAsynch: Whether to send the request asynchronously or not. 
*/
function initReq(reqType,url,isAsynch){
	 /* Specify the function that will handle the HTTP response 
	 */
	 request.onreadystatechange=handleResponse;
	 request.open(reqType,url,isAsynch);
	 /* set the Content-Type header for a POST request 
	 */
	 request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
                 //url=encodeURI(url); //写一个不行。如果写一个就是？？？？号。
                 //url=encodeURI(url); 
	 request.send(queryString);
}

/* Wrapper function for constructing a Request object.
 Parameters:
 reqType: The HTTP request type such as GET or POST.
 url: The URL of the server program.
 asynch: Whether to send the request asynchronously or not. 
*/

function httpRequest(reqType,url,asynch){
    try{
	 //Mozilla-based browsers
	 //alert("come to httpRequest:"+reqType+url+asynch);
	 if(window.XMLHttpRequest){
		 request = new XMLHttpRequest();
		 initReq(reqType,url,asynch);
	 }else if (window.ActiveXObject){
		 request=new ActiveXObject("Msxml2.XMLHTTP");
		 if (!request){
			 request=new ActiveXObject("Microsoft.XMLHTTP");
		 }
		if(request){
			//alert(request);
			initReq(reqType,url,asynch);
			/* Unlikely to branch here, as IE uses will be able to use either 
			   one of the constructors
			*/
		} else {
			 alert("Your browser does not permit the use of all "+
			 "of this application's features!");
		}
	 } else {
		   alert("Your browser does not permit the use of all "+
		   "of this application's features!");
	 }
	 }catch(err){
	    alert("ERROR:"+err.message);
	 }
}


//event handler for XMLHttpRequest
var resultStr="";
//isDebug=true;
function handleResponse(){
 if(request.readyState == 4){
     if(request.status == 200){
         if(isDebug)
             alert("get responseText:"+request.responseText);
             resultStr=request.responseText;
             if(!__isReading){
                    exeRes();
             }else{
                    exeRes_Reading();
             }
            //alert(resultStr);
    } else {
         alert("A problem occurred with communicating between "+
               "the XMLHttpRequest object and the server program."+request.status);
    }
  }//end outer if
  else{
      //alert("readyState has not ok,its:"+request.readyState);
  }
}

function exeRes(){
    var js=document.createElement("script");
    js.setAttribute("type","text/javascript");
    //alert(js);
    if(isDebug)
        alert("get values's javascript text:"+resultStr);
    js.text=resultStr;//"alert(\"OK DYN SCRIPT\");";
    document.body.appendChild(js);
}
function exeRes_Reading(){
    var js=document.createElement("script");
    js.setAttribute("type","text/javascript");
    //alert(js);
    if(isDebug)
        alert("get values's javascript text:"+resultStr);
    //replace all " to \" to make can be set to value.
   resultStr=resultStr.replace(/["]/g,"\\\"");
   resultStr=resultStr.replace(/[\r\n]/g,"\\r\\n");
   if(isDebug){
        alert("after filter \":");
        alert("text:"+resultStr);      
    }
    js.text="__getFromUrl=\""+resultStr+"\";";
    if(isDebug){
        alert("after set getting str to var:__getFromUrl");   
        js.text+="alert(__getFromUrl);";
     }
    document.body.appendChild(js);
}
