/*
 * Coder: Mario Hartmann [mario@hartmann.net // http://mario.hartmann.net/]
 * Copyright: 2006, Photocolor Kreuzlingen AG
 * Original version: Javascipt
 * Original product name: Photocolor Kreuzlingen AG  -AsyncFramework-
 * Official site: http://www.Ifolor.ch
 * Last updated Date: 24-JUL-2006
 * Derivate works, translation in other languages
 * of this code must retain this copyright notice.
*/
// -----------------------------------------------------af
//
function CallBackObject()
{
   this.XmlHttp = this.GetHttpObject();
  	this.ParamList = new Array();
  	this.Version ="1.0.1";
  	this.ShowLoading=false;
}
// -----------------------------------------------------af
//
CallBackObject.prototype.GetHttpObject = function()
{ 
  var _xmlHttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {_xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");}
    catch (e) {
		try {_xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");}
      catch (E) {_xmlHttp = false;}
    }
  @else
	_xmlHttp = false;
  @end @*/
  if (!_xmlHttp && typeof XMLHttpRequest != 'undefined')
  {
    try {_xmlHttp = new XMLHttpRequest();} 
    catch (e) {_xmlHttp = false;}
  }
  return _xmlHttp;
}
// -----------------------------------------------------af
//
CallBackObject.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 + '&';}
      }
    }
  } 
  if( this.XmlHttp )
  {
    if( this.XmlHttp.readyState == 4 || this.XmlHttp.readyState == 0 )
    {
      var oThis = this;
      this.XmlHttp.open('POST', thePage, true);
      this.XmlHttp.onreadystatechange = function() { oThis.ReadyStateChange(); };
      this.XmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      if (HideLoading && this.ShowLoading) {HideLoading();}
      this.XmlHttp.send(theData);
    }
  }
}
// -----------------------------------------------------af
//
CallBackObject.prototype.DoPostBack = function()
{
  var theData = '';
  var theform = document.forms[0];
  var thePage = window.location.pathname + window.location.search;
  var eName = '';

  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 + '&';}
      }
    }
  }
  if( this.XmlHttp )
  {
    if( this.XmlHttp.readyState == 4 || this.XmlHttp.readyState == 0 )
    {
      var oThis = this;
      this.XmlHttp.open('POST', thePage, true);
      this.XmlHttp.onreadystatechange = function() { oThis.ReadyStateChange(); };
      this.XmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      this.XmlHttp.setRequestHeader('X-AsyncVersion',this.Version);      
      if (HideLoading && this.ShowLoading) {HideLoading();}
      this.XmlHttp.send(theData);
    }
  }
}
// -----------------------------------------------------af
//
CallBackObject.prototype.AddParam = function(name,value)
{
	this.ParamList[name]= value;
}
// -----------------------------------------------------af
//
CallBackObject.prototype.DoPostCallback = function(url)
{
  var _thePage = url ;
  var _theData = '';
  
    for(var _name in this.ParamList)
   {
		_value = this.ParamList[_name];
		if (_theData.length>0){ _theData = _theData + '&';}
       _theData = _theData + escape(_name.split("$").join(":")) + '=' + _value;
   }
  if( this.XmlHttp )
  {
    if( this.XmlHttp.readyState == 4 || this.XmlHttp.readyState == 0 )
    {
      var oThis = this;
      this.XmlHttp.open('POST', _thePage, true);
      this.XmlHttp.onreadystatechange = function() { oThis.ReadyStateChange(); };
      this.XmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      this.XmlHttp.setRequestHeader('X-AsyncVersion',this.Version);     
      if (HideLoading && this.ShowLoading) {HideLoading();} 
      this.XmlHttp.send(_theData);
    }
  }
 }
// -----------------------------------------------------af
//
CallBackObject.prototype.DoGetCallback = function(url)
{
  var _thePage = url + "?" ;
  var _theData = window.location.search;
  for(var _name in this.ParamList)
   {
		if (_theData.length>0){ _theData = _theData + '&';}
		_value = paramArray[_name];
       _theData = _theData + escape(_name.split("$").join(":")) + '=' + _value;
       _theData = _theData + '&';
   }  
  if( this.XmlHttp)
  {
    if( this.XmlHttp.readyState == 4 || this.XmlHttp.readyState == 0 )
    {
      var oThis = this;
      this.XmlHttp.open('GET', _thePage + _theData , true);
      this.XmlHttp.onreadystatechange = function() { oThis.ReadyStateChange(); };
      this.XmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		this.XmlHttp.setRequestHeader('X-AsyncVersion',this.Version);      
      if (ShowLoading && this.ShowLoading) {ShowLoading();}
      this.XmlHttp.send();
    }
  }
}
// -----------------------------------------------------af
//
CallBackObject.prototype.AbortCallBack = function()
{
  if( this.XmlHttp ){ this.XmlHttp.abort();}
}  
// -----------------------------------------------------af
//
CallBackObject.prototype.OnLoading = function() {}
// -----------------------------------------------------af
//
CallBackObject.prototype.OnLoaded = function(){}
// -----------------------------------------------------af
//
CallBackObject.prototype.OnInteractive = function(){}
// -----------------------------------------------------af
//
CallBackObject.prototype.OnComplete = function(responseText, responseXml){}
// -----------------------------------------------------af
//
CallBackObject.prototype.OnAbort = function(){}
// -----------------------------------------------------af
//
CallBackObject.prototype.OnError = function(status, statusText)
{
// Error
	if (m_debug == true)
	{
		_errWindow = window.open('','Error','width=800,height=800,top=5,left=5,location=0,directories=0,status=no,menuBar=no,toolbar=no,scrollBars=no,resizable=yes');
		var _errDoc = _errWindow.document;
		_errDoc.open();
		_errDoc.writeln("<html><head><title>"+ status +"</title>")
		_errDoc.writeln(statusText) ;
		_errDoc.close();
		_errDoc.focus; 
	}
}
// -----------------------------------------------------af
//
CallBackObject.prototype.ReadyStateChange = function()
{
  if( this.XmlHttp.readyState == 1 ){this.OnLoading();}
  else if( this.XmlHttp.readyState == 2 ){this.OnLoaded();}
  else if( this.XmlHttp.readyState == 3 ){this.OnInteractive();}
  else if( this.XmlHttp.readyState == 4 )
  {
	if (HideLoading && this.ShowLoading) {HideLoading();}
		if( this.XmlHttp.status == 0 ){this.OnAbort();}
		else if( this.XmlHttp.status == 200 && this.XmlHttp.statusText == "OK" ){this.OnComplete(this.XmlHttp.responseText,this.XmlHttp.responseXML);}
		else{this.OnError(this.XmlHttp.status, this.XmlHttp.statusText, this.XmlHttp.responseText);}   
  }
}
// -----------------------------------------------------
// end
// -----------------------------------------------------