/** Ajax   edit by tyt@sohu.com
example: Ajax.Request("POST","mypage.aspx","foo=bar&baz=qux", fnWhenDone);
example: Ajax.Update("POST","mypage.aspx","foo=bar&baz=qux", "div_id");
**/

function AjaxConn(){
var xmlhttp, bComplete = false;
try{ xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
catch(e){ try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
catch(e){ try { xmlhttp = new XMLHttpRequest(); }
catch(e){ xmlhttp = false; }}}
if(!xmlhttp)return null;
this.Request = function(sMethod,sURL,sVars, fnDone){
if(!xmlhttp)return false;

bComplete = false;
sMethod = sMethod.toUpperCase();

try{
if (sMethod == "GET"){
xmlhttp.open(sMethod, sURL+"?"+sVars, true);
sVars = "";
}else{
xmlhttp.open(sMethod, sURL, true);
xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
}
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4 && !bComplete){
bComplete = true;
if( fnDone != null )fnDone(xmlhttp);
}};
        
xmlhttp.send(sVars);
}
catch(z){ return false; }
return true;
}; 

this.ShowError = function(res){
alert("data transfer error ,the code is " +res.status );
}
return this;
}

function postToInnerHtml(sURL,sVars,elmentid){	
var element = document.getElementById(elmentid);
element.innerHTML="请稍后...";
var CallBackFunc = function CallBack(res){
if(res.status == 200 ){
var response=res.responseText;//alert(response);
element.innerHTML = response;
}else{
element.innerHTML = "出现错误";
}
}
var ajax = new AjaxConn();
ajax.Request("POST",sURL,sVars,CallBackFunc);
}

function postToValue(sURL,sVars,elmentid){	
var element = document.getElementById(elmentid);
var CallBackFunc = function CallBack(res){
if(res.status == 200 ){
var response=res.responseText;//alert(response);
element.value = response;
}else{
element.innerHTML = "出现错误";
}
}
var ajax = new AjaxConn();
ajax.Request("POST",sURL,sVars,CallBackFunc);
}

function postToOuterHtml(sURL,sVars,elmentid,iseval){
if(!arguments[3]) iseval = false;
var element = document.getElementById(elmentid);
var CallBackFunc = function CallBack(res){
if(res.status==200 ){
var response=res.responseText;//alert(response);
element.outerHTML = response;
if(iseval) executeJs(response);
}else{element.innerHTML = "出现错误";	}
}
var ajax = new AjaxConn();
ajax.Request("POST",sURL,sVars,CallBackFunc);
}

function postToAlert(sURL,sVars){	
var CallBackFunc = function CallBack(res){
if(res.status == 200 ){var response=res.responseText;alert(response);
}else{alert("出现错误");}
}
var ajax = new AjaxConn();
ajax.Request("POST",sURL,sVars,CallBackFunc);
}

function delete_tablerow_action(sURL,sVars,trid,tableid){	
var element = document.getElementById(trid);
var CallBackFunc = function CallBack(res){
if(res.status == 200 ){
var response=res.responseText;//alert(response);
if( response == '' ||  response.substr(0,4)=='<!--' || response.substr(0,2) == 'ok' ){
var idx = element.rowIndex;
var obj = document.getElementById(tableid);
if( idx >= 0 && obj != null )
	obj.deleteRow(idx);
else
	alert(response);
}
}
}
var ajax = new AjaxConn();
ajax.Request("POST",sURL,sVars,CallBackFunc);
}

function postToTableCell(sURL,sVars,tableid,trid,col_idx){	
var element = document.getElementById(trid);
var CallBackFunc = function CallBack(res){
if(res.status == 200 ){
var response=res.responseText;
var idx = element.rowIndex;
var obj = document.getElementById(tableid);
if( idx >= 0 && obj != null )
	obj.rows[idx].cells[col_idx].innerHTML=response;
else
	alert(response);
}
}
var ajax = new AjaxConn();
ajax.Request("POST",sURL,sVars,CallBackFunc);
}

function executeJs(data){
    var hd = document.getElementsByTagName("head")[0];     
    var re = /(?:<script([^>]*)?>)((\n|\r|.)*?)(?:<\/script>)/ig;     
    var srcRe = /\ssrc=([\'\"])(.*?)\1/i;     
    var typeRe = /\stype=([\'\"])(.*?)\1/i;     
    var match;     
    while(match = re.exec(data)){     
         var attrs = match[1];     
         var srcMatch = attrs ? attrs.match(srcRe) : false;     
         if(srcMatch && srcMatch[2]){     
              var s = document.createElement("script");     
              s.src = srcMatch[2];     
              var typeMatch = attrs.match(typeRe);     
              if(typeMatch && typeMatch[2]){     
                   s.type = typeMatch[2];     
              }     
              hd.appendChild(s);     
          }else if(match[2] && match[2].length > 0){     
              if(window.execScript) {     
                      window.execScript(match[2]);     
              } else {     
                      window.eval(match[2]);     
              }     
         }     
    }
}
