﻿
//Callback object
//**********************************************
function cbo()
{
  this.req = this.GetHttpObject();
}
cbo.prototype.GetHttpObject = function()
{ 
    var req = false;
    if(window.XMLHttpRequest) //Mozilla
    {
        req = new XMLHttpRequest();
        if(req.overrideMimeType) req.overrideMimeType("text/xml");
    }
    else if(window.ActiveXObject) //IE
        {
	        try{req = new ActiveXObject("Msxml2.XMLHTTP");}
	        catch(e01)
	        {
		        try{req = new ActiveXObject("Microsoft.XMLHTTP");}
		        catch(e02){}
	        }
        }
    return req;
}
cbo.prototype.DoCallBack = function(eventTarget, eventArgument)
{
  var theData = '';
  var theform = document.forms[0];
  var thePage = window.location.pathname + window.location.search;
  var eName = '';
 
  theData  = '__EVENTTARGET='  + escape(eventTarget.split("$").join(":")) + '&';
  theData += '__EVENTARGUMENT=' + eventArgument + '&';
  theData += '__VIEWSTATE=' + escape(theform.__VIEWSTATE.value).replace(new RegExp('\\+', 'g'), '%2b') + '&';
  theData += 'IsCallBack=true&';
  for( var i=0; i<theform.elements.length; i++ )
  {
    eName = theform.elements[i].name;
    if( eName && eName != '')
    {
      if( eName == '__EVENTTARGET' || eName == '__EVENTARGUMENT' || eName == '__VIEWSTATE')
      {
        // Do Nothing
      }
      else
      {
        //theData = theData + escape(eName.split("$").join(":")) + '=' + theform.elements[i].value;
        //if( i != theform.elements.length - 1 ) theData = theData + '&';
        theData = theData + this.GetFrmData(theform.name)
      }
    }
  }
  if( this.req )
  {
    if( this.req.readyState == 4 || this.req.readyState == 0 )
    {
      var oThis = this;
      this.req.open('POST', thePage, true);
      this.req.onreadystatechange = function(){ oThis.ReadyStateChange(); };
      this.req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      this.req.send(theData);
    }
  }
}
cbo.prototype.FetchUrl = function(url,frm)
{
  var data = "cache=" + new Date();
  var f = document.forms[frm];
  if(f) data=this.GetFrmData(frm)
 
  if( this.req )
  {
    if( this.req.readyState == 4 || this.req.readyState == 0 )
    {
      var oThis = this;
      this.req.open('POST', url, true);
      this.req.onreadystatechange = function(){ oThis.ReadyStateChange(); };
      this.req.setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=UTF-8');
      if(!document.all)//fix for firefox
        this.req.overrideMimeType('text/html');
      this.req.send(data);
    }
  }
}
cbo.prototype.GetFrmData = function(frmId) 
{
    var f = document.forms[frmId];
    var str = ""; 
    for(var i = 0;i < f.elements.length;i++) 
    {
       var el=f.elements[i];
       switch(el.type) 
       {
           case "password":
           case "hidden":
           case "textarea":
           case "text":
                str += escape(el.name.split("$").join(":")) + "=" + escape(el.value).replace(new RegExp('\\+', 'g'), '%2b'); 
                break; 
           case "radio":
                if(el.checked)
                    str += escape(el.name.split("$").join(":")) + "=" + escape(el.value); 
                break; 
           case "select-one": 
                if(el.selectedIndex>=0)
                    str += escape(el.name.split("$").join(":")) + "=" + escape(el.options[el.selectedIndex].value); 
                break; 
       }
       if( i < f.elements.length ) str+='&';
    }
    str += 'IsCallback=true';
    return str; 
}
cbo.prototype.AbortCallBack = function()
{
  if( this.req )this.req.abort();
}
 
cbo.prototype.ReadyStateChange = function()
{
  if(this.req)
  {
      if( this.req.readyState == 1 ){this.OnLoading();}
      else if( this.req.readyState == 2 ){this.OnLoaded();}
      else if( this.req.readyState == 3 ){this.OnInteractive();}
      else if( this.req.readyState == 4 )
      {
        if( this.req.status == 0 ) 
            this.OnAbort();
        else if( this.req.status == 200 && this.req.statusText == "OK" )
          this.OnComplete(this.req.responseText,this.req.responseXML);
        else
          this.OnError(this.req.status,this.req.statusText, this.req.responseText);   
      }
  }
}
cbo.prototype.OnLoading = function(){}
cbo.prototype.OnLoaded = function(){}
cbo.prototype.OnInteractive = function(){}
cbo.prototype.OnComplete = function(responseText, responseXml){}
cbo.prototype.OnAbort = function(){}
cbo.prototype.OnError = function(status, statusText){}
//**********************************************