/**
 * Prototype-compatible javascript voor het component FormGenerator
 *
 * Getest op prototype 1.6.0.2
 *
 * @author Maurice Bonemeijer, Martin Borsboom
 */

/**
 * InputFocus
 * Geeft het huidige veld waarin mensen aan het typen zijn de classname 'inputfocus'
 */
FormGenerator_initInputFocus = function FormGenerator_inputFocusInitialisation() {

	var areas = document.getElementsByTagName('textarea');
	for(var i = 0; i < areas.length; i++) {
		areas[i].onfocus = function() {
			FormGenerator_inputFocus(this);			
		}
		areas[i].onblur = function() {
			FormGenerator_inputBlur(this);
		}
	}
	
	var inputs = document.getElementsByTagName('input');
	for(var i = 0; i < inputs.length; i++) {
		inputs[i].onfocus = function() {
			FormGenerator_inputFocus(this);
		}
		inputs[i].onblur = function() {
			FormGenerator_inputBlur(this);
		}
	}
	
	var selects = document.getElementsByTagName('select');
	for(var i = 0; i < selects.length; i++) {
		selects[i].onfocus = function() {
			FormGenerator_inputFocus(this);
		}
		selects[i].onblur = function() {
			FormGenerator_inputBlur(this);
		}
	}

}

/**
 * De classnaam 'FormGenerator_inputfocus' aan het actieve element hangen
 */
function FormGenerator_inputFocus(element) { 
	element.className += (element.className ? ' ' : '') + 'FormGenerator_inputfocus';
}

/**
 * De classnaam 'FormGenerator_inputfocus' verwijderen van het (voorheen) actieve element
 */
function FormGenerator_inputBlur(element) {
  element.className = element.className.replace( new RegExp("(^|\\s+)FormGenerator_inputfocus(\\s+|$)"), ' ').strip();
}

/**
 * FormGenerator_removeUpload
 * Wijzigt een plaatje naar het geselecteerde
 */
function FormGenerator_removeUpload(container, id, classes) {
	
	var nodes = new Array();
	for(var i = 0; i < container.childNodes.length; i++) {
		nodes[i] = container.childNodes[i];
	}
	
	for(var y = 0; y < nodes.length; y++) {
		container.removeChild(nodes[y]);
	}

	container.appendChild( FormGenerator_createInput('file', id, id, classes, '') );
}

/**
 * FormGenerator_createInput
 * maakt een text input element aan met de opgegeven opties
 */
function FormGenerator_createInput(kind, id, name, classes, style) {
	try {
		return document.createElement('<input id="'+ id +'" type="'+ kind +'" name="'+ name +'" class="'+ classes +'" style="'+ style +'" />');
	} catch (e) {
		var element = document.createElement("input");
		if(id) { element.setAttribute("id", id); }
		if(kind) { element.setAttribute("type", kind); }
		if(name) { element.setAttribute("name", name); }
		if(classes) { element.setAttribute("class", classes); }
		if(style) { element.setAttribute("style", style); }
		
		return element;
	}	
}

/**
 * Initialisatie toevoegen aan window.onLoad
 */
addLoadEvent(FormGenerator_initInputFocus);	// inputFocus

