﻿
var __prevTimeoutID;

function XMLReqObj(strMethod,strUrl,
                   strObjNameToSet,oFunction,ResquestUseingIFrameOnError)
{
     strUrl=SiteUrl+strUrl
     this.objX=new Object();     
	 this.CallBackFunction=null;
	 this.objXML_onreadystatechange= function () 
		{var retCount=0;
		 var retText="",retErr=null;
		 if(objX.readyState == 4) 
           { 
            if(objX.status == 200) 
               {
                 var ajxError=getAjaxRequestError(objX.responseText);
                 if(ajxError.ErrID==-1)
                    retText=ajxError.Message;
                 else
                    retErr=ajxError.Message
               }
            else 
               {retErr=objX.statusText; }
            objX=null;
           if(typeof CallBackFunction=='function'){CallBackFunction(retText,retErr);}
          window.clearTimeout(__prevTimeoutID);   
         }      
	}//end objXML_onreadystatechange
	
  if(window.XMLHttpRequest) 
     {
      try {objX = new XMLHttpRequest();}
       catch(e){objX = null;}
      }
   else if(window.ActiveXObject) 
     {
      try {objX = new ActiveXObject("Microsoft.XMLHTTP");}
      catch(e) 
         {try {objX = new ActiveXObject("Msxml2.XMLHTTP");}
          catch(e){objX = null;}
        }
    }
    if(oFunction!=null)CallBackFunction =(typeof(oFunction)=="string")?window[oFunction]:oFunction;
    if(objX!=null)
       {
        if(strObjNameToSet!=null)
           {window[strObjNameToSet]=objX;}         
           
        
        objX.onreadystatechange=this.objXML_onreadystatechange;
        
        if(strMethod==null)strMethod='GET'; 
       
        if(strUrl!=null)
          try{objX.open(strMethod,strUrl,true);}
          catch(e){if(typeof CallBackFunction=='function'){CallBackFunction(null,(document.all?e.message:e));}}
        
        if(strMethod.toLowerCase()=='post')
           {objX.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded");}
       }
      else
      {
       if(ResquestUseingIFrameOnError!=false)
       {
        FrameRequestCallBackFunction=CallBackFunction;
        SendFrameRequest(strUrl);
       }
      }
    return objX;
}

function getAjaxRequestError(ResponseText)
{
 var ErrID=-1,Message;
 if(ResponseText.length>5)
 {
  if(ResponseText.substring(0,5)=="+ERR:")
    {
     ErrID   = ResponseText.substring(5,ResponseText.indexOf(";"))
     Message = ResponseText.substring(ResponseText.indexOf(";")+1);     
    }
    else
    {
     Message=ResponseText;
    }
  }
  else
    {
     Message=ResponseText;
    }  
  var ret=new Object();
  ret.ErrID=ErrID;
  ret.Message=Message;   
  //alert("ERR:" +ResponseText)  
    return ret;// new Array(ErrID,Message);
}








var _SEND_REQUEST_SUCCESS = 0,
    _SEND_REQUEST_REDIRECTED = 1,
    _SEND_REQUEST_FIELD = 2
    ;
function SendIndividualRequest(url, method, data, CallBackFunction) {
    var loc_objXML;
    if (window.XMLHttpRequest)
    { loc_objXML = new XMLHttpRequest(); }
    else if (window.ActiveXObject) {
        try
       { loc_objXML = new ActiveXObject("Msxml2.XMLHTTP"); }
        catch (e) {
            try
            { loc_objXML = new ActiveXObject("Microsoft.XMLHTTP"); }
            catch (e) { }
        }
    }

    if (loc_objXML == null) {
        return _SEND_REQUEST_FIELD;
    }

    loc_objXML.onreadystatechange = function () {
        if (this.readyState == 4) {
            if (this.status == 200)
                CallBackFunction(this.responseText);
        }
        else {

        }
    };

    if (!method) method = "GET";
    if (method == '') method = "GET";
    if (method == null) method = "GET";
    if (url.indexOf('?') != -1) {
        url += '&dt=' + new Date().toString();
    }
    loc_objXML.open(method, url, true);
    loc_objXML.send(data);
    return _SEND_REQUEST_SUCCESS;
}

