/* 	
	------------------------------------------------------------------------------
	/framework/classes/js/checkbox.js
 	------------------------------------------------------------------------------
	
*/

function CheckBox(_id, _value, _checked) {

	
	
	// standard properties
	this.id = _id;	
	this.value = _value;
	this.checked = _checked;
	
	this.formElementId = "F_" + this.id;
	this.formElement = document.getElementById(this.formElementId);
	this.labelElementid = "L_" + this.id;
	this.labelElement = document.getElementById(this.labelElementId);
	
	this.parentElement = null;
	
	// appearance
	this.width = 0;
	this.visible = true;
	this.type = "text";
	
	// events
	this.onclick = "";
	this.onkeyup = "";
	this.onblur = "";
	
	// extra properties
	this.error = "";
	this.tag = "";
	
	// methods
	this.show = CheckBox_show;
	this.hide = CheckBox_hide;
	
	this.registerEvents = CheckBox_registerEvents;
	this.addEvent = CheckBox_addEvent;
	this.getEventName = CheckBox_getEventName;
	
	this.getError = CheckBox_getError;
	this.setError = CheckBox_setError;
	
	this.showBusy = CheckBox_showBusy;
	this.hideBusy = CheckBox_hideBusy;
	
	this.getValue = CheckBox_getValue;
	this.setValue = CheckBox_setValue;
	
	this.getChecked = CheckBox_getChecked;
	this.setChecked = CheckBox_setChecked;
	
	this.setEnabled = CheckBox_setEnabled;
	this.setDisabled = CheckBox_setDisabled;
	
	this.focus = CheckBox_focus;
	this.blur = CheckBox_blur;
	
	this.render = CheckBox_render;
	this.destroy = CheckBox_destroy;
	
	this.getType = CheckBox_getType;
}

function CheckBox_getType() {
	return "CheckBox";
}

function CheckBox_destroy() {
	debug.write("Destroyin' myself: " + this.id);
	/*
	if (this.containerElement) {
		if (this.containerElement.parentNode) {
			debug.write("really destroying");
			this.containerElement.parentNode.removeChild(this.containerElement);
		}
	}
	this.containerElement.removeChild(this.formElement);
	this.containerElement.removeChild(this.busyElement);
	this.containerElement = null;
	this.formElement = null;
	this.busyElement = null;
	*/
	if (this.formElement) {
		if (this.formElement.parentNode) {
			debug.write("really destroying");
			this.formElement.parentNode.removeChild(this.formElement);
		}
	}
	this.formElement = null;
}

function CheckBox_render() {
	debug.write("rendering " + this.id + " in " + this.parentElement.id + " with value " + this.value + " and tag " + this.tag);
	
	//	"<input type=\"checkbox\" id=\"F_$this->id\" style=\"$c_style\" $c_enabled class=\"fw_checkbox\" name=\"F_$this->id\" $c_checked/>\n";
	// "<label id=\"L_$this->id\" for=\"F_$this->id\">" . $this->value . "</label>\n";
		
	
	
	debug.write("Creating normal element");
	this.formElement = document.createElement("input");
	this.formElement.type = "checkbox";
	this.formElement.id = "F_" + this.id;
	if (this.width != "") 
		this.formElement.style.width = this.width;
	this.formElement.style.display = "none";
	this.formElement.className = "fw_checkbox";
	
	this.parentElement.appendChild(this.formElement);
	
	// label
	this.labelElement = document.createElement("label");
	this.labelElement.id = "L_" + this.id;
	this.labelElement.setAttribute("for", "F_" + this.id);
	this.labelElement.innerHTML = this.value;
	this.parentElement.appendChild(this.labelElement);
	
	this.registerEvents();
	page.addControl(this);

}


function CheckBox_hide() {
	if (this.formElement) {
		this.visible = false;
		this.formElement.style.display = "none";
	}
}

function CheckBox_show() {
	if (this.formElement) {
		this.visible = true;
		this.formElement.style.display = "inline";
	}
}

function CheckBox_blur() {
	if (this.formElement) {
		this.formElement.blur();
	}
}

function CheckBox_focus() {
	if (this.formElement) {
		this.formElement.focus();
	}
}

function CheckBox_getChecked() {
	//debug.write("Is it checked? " + this.formElement.checked);
	return this.formElement.checked;
}

function CheckBox_setChecked(newCheckValue) {
	debug.write("Setting " + this.formElement.id + " to checked=" + newCheckValue + " (value now = " + this.formElement.checked + ")");
	this.formElement.checked = newCheckValue;
}


function CheckBox_setEnabled() {
	debug.write("Setting " + this.id + " to enabled");
	this.formElement.disabled = false;
}

function CheckBox_setDisabled() {
	debug.write("Setting " + this.id + " to disabled");
	this.formElement.disabled = true;
}

function CheckBox_getValue() {
	if (this.formElement) {
		return this.formElement.value;
	}
}
function CheckBox_setValue(newValue) {
	if (this.formElement) {
		this.formElement.value = newValue;
	}
}
function CheckBox_showBusy() {
	/*if (this.busyElement) {
		this.busyElement.style.display = "block";
	}
	*/
}
function CheckBox_hideBusy() {
	/*if (this.busyElement) {
		this.busyElement.style.display = "none";
	}
	*/
}

function CheckBox_getError() {
	return this.error;	
}

function CheckBox_setError(msg) {
	/*this.error = msg;
	
	if (this.errorElement) {
		this.errorElement.alt = this.error;
		this.errorElement.title = this.error;
		
		if (msg.length > 0) {
			this.errorElement.style.display = "block";
		}
		else {
			this.errorElement.style.display = "none";
		}
	}
	*/
}


function CheckBox_getEventName(whichevent) {
	//alert("eval dit " + eval("this." + whichevent));
	return (eval("this.on" + whichevent));
	
}

function CheckBox_registerEvents() {
	
	if (this.formElement) {
		if (this.onclick.length > 0) {
			this.addEvent('click');
		}
		
		if (this.onkeyup.length > 0) {
			this.addEvent('keyup');
		}
		
		if (this.onblur.length > 0) {
			this.addEvent('blur');
		}
	}
	
}
	
function CheckBox_addEvent(eventtype) {
	
	//W3C
	if(this.formElement.addEventListener) {
		//alert('W3C event added');
		this.formElement.addEventListener(eventtype, handle_event, false);
	}

	//Microsoft
	else if(this.formElement.attachEvent){
		//alert("MS event added");
		this.formElement.attachEvent("on" + eventtype, handle_event);
	}
	
	
}



