Type.registerNamespace("ComfyTel");

ComfyTel.CHECKBOX_ON = "1";
ComfyTel.CHECKBOX_OFF = "0";

ComfyTel.Checkbox = function(imageID, innerCheckboxID, value, postback, readonly, disabled) {
	this.imageID = imageID;
	this.innerCheckboxID = innerCheckboxID;
	
	this.getInnerCheckbox().value = value;

	this.postback = postback || false;
	this.readonly = readonly || false;
	this.disabled = disabled || false;
	
	this.onclick = function() {};
	this.onchange = function(oldvalue, newvalue) {};
};

ComfyTel.Checkbox.prototype = {
	getInnerCheckbox : function() {
		return document.getElementById(this.innerCheckboxID);
	},
	
	getImage : function() {
		return document.getElementById(this.imageID);
	},

	isTickedOn : function() {
		return this.getInnerCheckbox().value != ComfyTel.CHECKBOX_OFF;
	},
	
	changeStatus : function (value, checked, image, newimage) {
		this.getInnerCheckbox().checked = checked;
		this.getInnerCheckbox().value = value;
		this.getImage().src = this.getImage().src.replace(image, newimage);
	},
	
	tickOn : function() {
		this.changeStatus(ComfyTel.CHECKBOX_ON, true, 'tick-off.png', 'tick-on.png');
	},
	
	tickOff : function() {
		this.changeStatus(ComfyTel.CHECKBOX_OFF, false, 'tick-on.png', 'tick-off.png');
	},

	toggleClick : function() {
		if (this.readonly || this.disabled) {
			return;
		}

        var oldvalue = this.getInnerCheckbox().value;

		if (this.isTickedOn()) {
			this.tickOff();
		}
		else {
			this.tickOn();
		}
		
		var newvalue = this.getInnerCheckbox().value;

		if (typeof(this.onclick) == 'function') {
			this.onclick();
		}
		
		if (typeof(this.onchange) == 'function') {
			this.onchange(oldvalue, newvalue);
		}
		
		if (this.postback) {
			__doPostBack(this.innerCheckboxID.replace('_', '$'), '');
		}
	}
};

ComfyTel.Checkbox.registerClass('ComfyTel.Checkbox', Sys.UI.Control);
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
