var FixedCallBackTarget = null;
var CallBackParam = null;

function CallBackObject()
{
  this.XmlHttp = this.GetHttpObject();
}
 
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;
}
 
CallBackObject.prototype.DoCallBack = function(eventTarget, eventArgument)
{
  var thePage = window.location.pathname.toString();
	if(thePage.length>0&&thePage.substring(thePage.length-1)=="/")
		thePage += "default.aspx";
	thePage += window.location.search.toString();
	if(FixedCallBackTarget!=null)
		thePage = FixedCallBackTarget;
	if(CallBackParam!=null){
		if(thePage.indexOf("?")<0)
			thePage += "?";
		else
			thePage += "&";
		thePage += CallBackParam;
	}
  var eName = '';
	var theForm = null;
	for(var i = 0; i < document.forms.length; i++){
		if(document.forms[i].__VIEWSTATE){
			theForm = document.forms[i];
			break;
		}
	}
	theData = 'IsCustomCallBack=true&';
  theData += '__EVENTTARGET='  + escape(eventTarget.split("$").join(":")) + '&';
  theData += '__EVENTARGUMENT=' + eventArgument + '&';
  if(theForm){
		theData += '__VIEWSTATE=' + escape(theForm.__VIEWSTATE.value).replace(new RegExp('\\+', 'g'), '%2b') + '&';
		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
				{
					if(i<theForm.elements.length-1 && eName == theForm.elements[i+1].name){
						if(theForm.elements[i].checked){
							theData += escape(eName.split("$").join(":")) + '=' + theForm.elements[i].value;
							while(eName == theForm.elements[i+1].name){
								i++;
							}
						}
					}
					else if(theForm.elements[i].type == 'checkbox'){
						if(theForm.elements[i].checked)
							theData += escape(eName.split("$").join(":")) + '=on';
						else
							theData += escape(eName.split("$").join(":")) + '=';
					}
					else
						theData += escape(eName.split("$").join(":")) + '=' + theForm.elements[i].value;
					if(i+1 < theForm.elements.length && theData.substring(theData.length-1,theData.length)!='&')
						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.send(theData);
    }
  }
}
 
CallBackObject.prototype.AbortCallBack = function()
{
  if( this.XmlHttp )
    this.XmlHttp.abort();
}
 
CallBackObject.prototype.OnLoading = function()
{
  // Loading
}
 
CallBackObject.prototype.OnLoaded = function()
{
  // Loaded
}
 
CallBackObject.prototype.OnInteractive = function()
{
  // Interactive
}
 
CallBackObject.prototype.OnComplete = function(responseText, responseXml)
{
  // Complete
}
 
CallBackObject.prototype.OnAbort = function()
{
  // Abort
}
 
CallBackObject.prototype.OnError = function(status, statusText)
{
  // Error
}
 
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( 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);   
  }
}

var Cbo = new CallBackObject();
Cbo.OnComplete = Cbo_Complete;
Cbo.OnError    = Cbo_Error;
var oContext = new Object();
var oContent = new Object();

/*function GetContent(content,target,pref)
{
	oContext.CommandName = 'GetSmaakContent';
	oContext.target = target;
	oContext.pref = pref;
  Cbo.DoCallBack(content,pref);
	try{
		document.getElementById(target+'_content').innerHTML="<p class=\"loading\">loading...</p>";
	}catch(e){}
  set_smaak(target,pref);
}*/

function GetContent(content,target,pref)
{
	oContext.CommandName = 'GetSmaakContent';
	oContext.target = target;
	oContext.pref = pref;
	oContext.content = content;
	try{
		oContent = document.getElementById(oContext.target+'_content');
		oContext.backContent = oContent.innerHTML.toString();
		oContent.innerHTML = "<span class=\"refreshing\">refreshing...</span>";
	}catch(e){}
	try{
		set_smaak(target,pref);
	}catch(e){}
	setTimeout('DelayedGet()',500);
}

function DelayedGet()
{
  Cbo.DoCallBack(oContext.content,oContext.pref);
}

function Cbo_Complete(responseText, responseXML)
{
	if(responseText.indexOf("<!DOCTYPE")>=0)
		Cbo_Error();
  else if (oContext.CommandName == 'GetSmaakContent')
    oContent.innerHTML = responseText;
	else 
		Cbo_Error();
}

function Cbo_Error(status, statusText, responseText)
{
	try{
		oContent.innerHTML = oContext.backContent;
		window.clearTimeout(rContext.timeoutID);
	}catch(e){}
}
