/* 	
	------------------------------------------------------------------------------
	/framework/classes/js/textbox.js
	
	Een textbox. Maar dan niet zomaar een textbox.
	
  ------------------------------------------------------------------------------
*/

function TextBox(_id, _value) {

	// standard properties
	this.id = _id;	
	this.value = _value;
	
	//this.containerElementId = "C_" + this.id;
	//this.containerElement = document.getElementById(this.containerElementId);
	this.formElementId = "F_" + this.id;
	this.formElement = document.getElementById(this.formElementId);
	//this.errorElementid = "E_" + this.id;
	//this.errorElement = document.getElementById(this.errorElementId);
	this.busyElement = null; // will be dynamically generated
	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 = "";
	this.enabled = true;
	
	// editor
	this.editorType = "none";
	this.editorResourceBasePath = "";
	
	// methods
	this.show = TextBox_show;
	this.hide = TextBox_hide;
	
	this.registerEvents = TextBox_registerEvents;
	this.addEvent = TextBox_addEvent;
	this.getEventName = TextBox_getEventName;
	
	this.getError = TextBox_getError;
	this.clearError = TextBox_clearError;
	this.setError = TextBox_setError;
	
	this.showBusy = TextBox_showBusy;
	this.hideBusy = TextBox_hideBusy;
	
	this.getValue = TextBox_getValue;
	this.setValue = TextBox_setValue;
	
	this.focus = TextBox_focus;
	this.blur = TextBox_blur;
	
	this.render = TextBox_render;
	this.destroy = TextBox_destroy;
	
	this.getType = TextBox_getType;
	this.setEnabled = TextBox_setEnabled;
	this.setDisabled = TextBox_setDisabled;
	
	// extra events
	this.beforeEvent = TextBox_beforeEvent;
	this.afterEvent = TextBox_afterEvent;
	
	// effects
	this.highlight = TextBox_highlight;
	this.setEditorResourceBasePath = TextBox_setEditorResourceBasePath;
}

function TextBox_highlight() {
	new Effect.Highlight(this.formElementId);	
}

function TextBox_setEditorResourceBasePath(newBasePath) {
	tinyMCE.settings['file_browser_basepath'] = newBasePath;
}

function TextBox_beforeEvent(ev) {
	
}

function TextBox_afterEvent(ev) {
}

function TextBox_setEnabled() {
	debug.write("Setting " + this.id + " to enabled");
	this.formElement.disabled = false;
	this.enabled = true;
}

function TextBox_setDisabled() {
	debug.write("Setting " + this.id + " to disabled");
	this.formElement.disabled = true;
	this.enabled = false;
}

function TextBox_getType() {
	return "TextBox";
}

function TextBox_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;
	*/
	this.hide(); // is nuttig om evt een tinyEditor van ons af te gooien
	page.removeControl(this);
	
	if (this.formElement) {
		if (this.formElement.parentNode) {
			debug.write("really destroying");
			this.formElement.parentNode.removeChild(this.formElement);
		}
	}
	this.formElement = null;
}

function TextBox_render() {
	debug.write("rendering " + this.id + " in " + this.parentElement.id + " with value " + this.value + " and tag " + this.tag);
	
	if ((this.type == "text") || (this.type == "password")) {
		debug.write("Creating normal element");
		this.formElement = document.createElement("input");
		this.formElementId = "F_" + this.id;
		this.formElement.id = this.formElementId ;
		try { //if ((document.all) && (!window.opera))
			this.formElement.style.styleFloat = "left";
		}
		catch(err) { //else
			this.formElement.style.cssFloat = "left";
		}
		
		if (this.width != "") 
			this.formElement.style.width = this.width;
		
		this.formElement.style.display = "none";
		
		this.formElement.className = "fw_textbox";
		this.formElement.setAttribute("autocomplete","off");
		
		this.formElement.type = this.type;
		this.formElement.value = this.value;
		
		page.addControl(this);
		
	}
	else if(this.type == "multiline") {
		debug.write("Creating multiline element");
		
		this.formElement = document.createElement("textarea");
		this.formElementId = "F_" + this.id;
		this.formElement.id = this.formElementId ;
		try { //if ((document.all) && (!window.opera))
			this.formElement.style.styleFloat = "left";
		}
		catch(err) { //else
			this.formElement.style.cssFloat = "left";
		}
		
		if (this.width != "") 
			this.formElement.style.width = this.width;
		
		this.formElement.style.display = "none";
		
		//this.formElement.className = "fw_textbox";
		this.formElement.setAttribute("autocomplete","off");
		
		this.formElement.value= this.value;
		page.addControl(this);
	}
	this.parentElement.appendChild(this.formElement);
	
	this.registerEvents();
}


function TextBox_hide() {
	if (this.formElement) {
		this.visible = false;
		if (this.editorType != "none") {
			tinyMCE.theme = "simple";
			tinyMCE.execCommand("mceRemoveControl", false, this.formElementId);
			
		}
		this.formElement.style.display = "none";
		
	}
}

function TextBox_show() {
	if (this.formElement) {
		this.visible = true;
		this.formElement.style.display = "block";
		if (this.editorType != "none") {
			
			if (this.editorType == "simple")
				tinyMCE.settings = tinyMCE.configs[0];
			else
				tinyMCE.settings = tinyMCE.configs[1];
				
			tinyMCE.execCommand("mceAddControl", false, this.formElementId);
		}
	}
}

function TextBox_blur() {
	if (this.formElement) {
		this.formElement.blur();
	}
}

function TextBox_focus() {
	if (this.formElement) {
		this.formElement.focus();
	}
}

function TextBox_getValue() {
	if (this.formElement) {
		if (this.editorType == "none") {
			return this.formElement.value;
		}
		else {
			return tinyMCE.getContent(this.formElementId);
		}
	}
}
function TextBox_setValue(newValue) {
	if (this.formElement) {
		this.formElement.value = newValue;
		if (this.editorType != "none") {
			tinyMCE.updateContent(this.formElementId);
		}
		
	}
}
function TextBox_showBusy() {
	if (this.busyElement) {
		// nothing to be done
	}
	else {
		// create busyElement
		this.busyElement = document.createElement("img");
		this.busyElement.className = "fw_busyelement";
		this.busyElement.style.position = "absolute";
		this.busyElement.src = layoutroot + "/img/fw_icon_busy.gif";
		// plaats bepalen
		
		x = findPosX(this.formElement) + this.formElement.offsetWidth + 3;
		y = findPosY(this.formElement) + (this.formElement.offsetHeight/2) - 1; 
		try {
			this.busyElement.style.top = y + "px";
			this.busyElement.style.left = x + "px";
		}
		catch(err) {
			this.busyElement.style.top = y;
			this.busyElement.style.left = x;
		}
		this.busyElement.style.zIndex = "105";
		document.body.appendChild(this.busyElement);
	}
	
	this.busyElement.style.display = "block";
}
function TextBox_hideBusy() {
	if (this.busyElement) {
		this.busyElement.style.display = "none";
	}
	
}

function TextBox_getError() {
	return this.error;	
}

function TextBox_clearError() {
	this.setError("");	
}

function TextBox_setError(msg) {
	this.error = msg;
	
	if (msg && msg.length > 0) {
		this.formElement.className = "fw_textbox_error";
		page.hasErrors = true;
	}
	else
		this.formElement.className = "fw_textbox";
}


function TextBox_getEventName(whichevent) {
	//alert("eval dit " + eval("this." + whichevent));
	return (eval("this.on" + whichevent));
	
}

function TextBox_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 TextBox_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);
	}
}



