function ToggleButton (varName, onId, offId, processingId, stateId, onFunction, offFunction, ajaxFunction)
{
	this._variableName = varName;
	this._onElement = document.getElementById(onId);
	this._offElement = document.getElementById(offId);
	this._processingElement = document.getElementById(processingId);
	this._state = document.getElementById(stateId);
	this.OnFunction = onFunction;
	this.OffFunction = offFunction;
	this._ajaxFunction = ajaxFunction;
	this._isInitialized = false;
	this._isProcessing = false;

	this.IsOn = function()
	{
        return this._state.value == '1';	    
	}
	
	this.SetOn = function()
	{
	    if (this.IsOn() || this._isProcessing)
	        return;
	
	    this._isProcessing = true;
		if (this._processingElement)
		{
		    this._processingElement.style.position = 'static';
		    this._processingElement.style.visibility = 'visible';
		}
		   
	    if (this._onElement)
	    {
	        this._onElement.style.position = 'absolute';
	        this._onElement.style.visibility = 'hidden';
	    }
	        
        if (this._offElement)
        {
	        this._offElement.style.position = 'absolute';
	        this._offElement.style.visibility = 'hidden';
	    }
	        
	     if (this._ajaxFunction)
	        this._ajaxFunction('1',new Function('result', this._variableName + '._completeOnOff(result);'));
	     else
	        this._completeOnOff(true);
	}
	
	this._completeOnOff = function(result)
	{
	    if (this._processingElement)
	    {
		    this._processingElement.style.position = 'absolute';
		    this._processingElement.style.visibility = 'hidden';
		}
		    
        if (result == '1')
        {
            this._state.value = '1';

            if (this._onElement)
            {
                this._onElement.style.position = 'static';
                this._onElement.style.visibility = 'visible';
            }
                
            if (this._offElement)
            {
                this._offElement.style.position = 'absolute';
                this._offElement.style.visibility = 'hidden';
            }
                
            this._isProcessing = false;
                
            if (this.OnFunction)
	            this.OnFunction(); 
        }
        else
        {
            this._state.value = '0';
	            
            if (this._onElement)
            {
                this._onElement.style.position = 'absolute';
                this._onElement.style.visibility = 'hidden';
            }
                
            if (this._offElement)
            {
                this._offElement.style.position = 'static';
                this._offElement.style.visibility = 'visible';
            }
                
            this._isProcessing = false;

            if (this.OffFunction)
	            this.OffFunction();
	    } 
	}
	
	this.SetOff = function()
	{
	    if (!this.IsOn() || this._isProcessing)
	        return;
	        
	    this._isProcessing = true;
		if (this._processingElement)
		{
		    this._processingElement.style.position = 'static';
		    this._processingElement.style.visibility = 'visible';
		}
		   
	    if (this._onElement)
	    {
	        this._onElement.style.position = 'absolute';
	        this._onElement.style.visibility = 'hidden';
	    }
	        
        if (this._offElement)
        {
	        this._offElement.style.position = 'absolute';
	        this._offElement.style.visibility = 'hidden';
	    }
	     
	     if (this._ajaxFunction)
	        this._ajaxFunction('0',new Function('result', this._variableName + '._completeOnOff(result);'));
	     else
	        this._completeOnOff(false);
	}
	
	this.Toggle = function()
	{
		if (this._isProcessing || !this._isInitialized)
		    return;
		 
		if (this.IsOn())
		    this.SetOff();
		else
		    this.SetOn();
	}
	
	this._initialize = function()
	{
	    if (this._onElement)
	    {
	        this._onElement.onclick = new Function(this._variableName + '.Toggle(); return false;');
	        if (this._state.value == '1')
	        {
	            this._onElement.style.position = 'static';
	            this._onElement.style.visibility = 'visible';
	        }
	        else
	        {
	            this._onElement.style.position = 'absolute';
	            this._onElement.style.visibility = 'hidden';
	        }
	    }
	        
	    if (this._offElement)
	    {
	        this._offElement.onclick = new Function(this._variableName + '.Toggle(); return false;');
	        if (this._state.value != '1')
	        {
	            this._offElement.style.position = 'static';
	            this._offElement.style.visibility = 'visible';
	        }
	        else
	        {
	            this._offElement.style.position = 'absolute';
	            this._offElement.style.visibility = 'hidden';
	        }
	    }
	        
	    if (this._processingElement)
	    {
	        this._processingElement.onclick = new Function('return false;');
	        this._processingElement.style.position = 'absolute';
	        this._processingElement.style.visibility = 'hidden';
	    }
	        
	    this._isInitialized = true;
	}
	
	this._initialize();
}
