//CONSTANTES para type
FIELD = new Object();
FIELD.LABEL = 0;
FIELD.EDIT = 1;
FIELD.COMBO = 2;
FIELD.LIST = 3;
FIELD.CHECK = 4;
FIELD.RADIO = 5;
FIELD.PASSWORD = 6;
FIELD.FILE = 7;
FIELD.RADIOSELECT = 8;
FIELD.DATE = 9;

function Field(id, label, type, data,  readOnly) {
	this.id = id;
	this.label = label;
	this.type = type;
	this.data = data;
	this.readOnly = readOnly || readOnly;
	this.editor = this.createEditor(data);
	this.editor.readOnly = this.readOnly;
	this.editor.field = this;
}

Field.prototype.createEditor = function(data) {
	var editor;
	if(this.type == FIELD.LABEL)	editor = this.createLabel(data);
	else if(this.type == FIELD.EDIT)	editor = this.createEdit(data);
	else if(this.type == FIELD.COMBO)	editor = this.createCombo(data);
	else if(this.type == FIELD.LIST)	editor = this.createList(data);
	else if(this.type == FIELD.CHECK)	editor = this.createCheck(data);
	else if(this.type == FIELD.RADIO)	editor = this.createRadio(data);
	else if(this.type == FIELD.PASSWORD)	editor = this.createPassword(data);
	else if(this.type == FIELD.FILE)	editor = this.createFile(data);
	else if(this.type==FIELD.RADIOSELECT)editor= this.createRadioSelect(data);
	else if(this.type==FIELD.DATE)	editor = this.createDate(data);
	else {
		editor = document.createElement("DIV");
		editor.innertHTML = data;
	}
	return editor;
}

Field.prototype.createDOM = function(type) {
	var editor = document.createElement(type);

	//LISTNERS
	editor.keyListeners = new Array();
	editor.mouseListeners = new Array();
	editor.actionListeners = new Array();

	editor.addKeyListener = function( keyListener ) {
		keyListener.editor = this;
		this.keyListeners[ this.keyListeners.length ] = keyListener;
	}

	editor.addActionListener = function( actionListener ) {
		actionListener.editor = this;
		this.actionListeners[ this.actionListeners.length ] = actionListener;
	}
	
	return editor;
}

Field.prototype.createLabel = function() {
	var editor = this.createDOM("DIV");
	editor.id = this.id;
	editor.innerHTML = this.data;
	return editor;
}

Field.prototype.addKeyListener = function( keyListener ) {
	this.editor.addKeyListener( keyListener );
}

Field.prototype.addActionListener = function( actionListener ) {
	this.editor.addActionListeners( actionListener );
}

Field.prototype.createInput = function(type) {
	var editor = this.createDOM("INPUT");
	editor.id = this.id;
	editor.type = type;
	editor.value = this.data;
	editor.size = 30;
	
	editor.onkeyup = function( e ) {
		e = e || window.event;
		e.editor = this;
		for(var i=0; i<this.keyListeners.length; i++)
			this.keyListeners[ i ].onkeyup( e );
	}
	
	editor.getValue = function() { return this.value; }
	editor.setValue = function(value) {
		this.setValue = value;
	}

	return editor;
}

Field.prototype.createEdit = function() {
	return this.createInput("text");
}

/**
	Genera un serie entre <b>from</b> y <b>to</b> incluy&eacute;ndolos.
	en el intervalo [from, to]. order
	
	@param from Obligatorio. N&uacute;mero. Inicio de la serie.
	@param to Obligatorio. N&uacute;mero. Hasta donde sera la serie
	@param desc Opcional. Bool. Descendente, por defecto es <b>false</b>
	@return un array con la serie.
*/
Field.prototype.genSerie = function(from, to, desc) {
	desc = !(desc || desc);
	var serie = new Array(to - from + 1);
	for( var i = from; i < to + 1; i++ )
		serie[ i-from ] = i;
	serie = desc ? serie : serie.sort(function(a,b){return b-a;});
	return serie;
}

Field.prototype.createDate = function() {
	this.date = this.data;
	this.editor = document.createElement("DIV");
	this.editor.id = this.id;
	this.editor.days = this.genSerie(1, 31); 
	this.data = this.editor.days;
	this.editor.dayEditor = this.createSelect();
	this.editor.dayEditor.id = this.editor.id + '.day';
	this.editor.months = new Array('Enero', 'Febrero', 'Marzo',
			'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto',
			'Setiembre', 'Octubre', 'Noviembre', 'Diciembre'
		); 
	this.data = this.editor.months;
	this.editor.monthEditor = this.createSelect();
	this.editor.monthEditor.id = this.editor.id + '.month';
	this.editor.years = this.genSerie(1901, 2000, true);
	this.data = this.editor.years;
	this.editor.yearEditor = this.createSelect();	
	this.editor.yearEditor.id = this.editor.id + '.year';

	this.editor.appendChild(this.editor.dayEditor);
	this.editor.appendChild(this.editor.monthEditor);
	this.editor.appendChild(this.editor.yearEditor);


	var al = new Object();
	
	al.yearEditor = this.editor.yearEditor;
	al.monthEditor = this.editor.monthEditor;
	al.dayEditor = this.editor.dayEditor;

	al.onblur = function(e) {
			switch(this.monthEditor.value) {
			case '0': case '2': case '4': case '6': 
			case '7': case '9':	case '10': case '12':
				this.dayEditor.options[28] = new Option('29', 28);
				this.dayEditor.options[29] = new Option('30', 29);
				this.dayEditor.options[30] = new Option('31', 30);
				break;
			case '1': 
				this.dayEditor.remove(30);
				this.dayEditor.remove(29);
				
				var year = 2000 - parseInt( this.yearEditor.value );
				
				if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
					this.dayEditor.options[28] = new Option('29', 28);
				else
					this.dayEditor.remove(28);
				break;
			case '3': case '5': case '8': case '11':
				this.dayEditor.remove(30);
				this.dayEditor.options[28] = new Option('29', 28);
				this.dayEditor.options[29] = new Option('30', 29);
				break;
			}
		}

	//Creacion de ActionListeners
	this.editor.monthEditor.addActionListener( al );
	this.editor.yearEditor.addActionListener( al );

	
	
	/**
		@param sep es el serparado con q se retornar&aacute;
			por defecto es /.
		@return Una fecha en el formato 'YYYY/MM/DD';
	*/
	this.editor.getValue = function(sep) {
		sep = sep || '/';
		return this.years[ this.yearEditor.getValue() ] + sep
			+ ( parseInt( this.monthEditor.getValue() ) + 1 ) + sep 
			+ ( parseInt( this.dayEditor.getValue() ) + 1 );
	}

	/**
		@return Una fecha del tipo Date;
	*/
	this.editor.getDateValue = function() {
		var date = new Date();
		date.setDay( this.dayEditor.getValue() );
		date.setMonth( this.monthEditor.getValue() );
		date.setYear( this.yearEditor.getValue() );
		return date;
	}

	/**
		Establece la fecha, 
		@param date debe tener el formato 'YYYY/MM/DD',
					o bien date debe ser de tipo Date.
		@param sep es el separador q se usar&aacute; por defecto es /.
	*/
	this.editor.setValue = function(date, sep) {
		sep = sep || '/';
		if( typeof(date) == 'string' ) {
			var nums = date.split(sep);
		}
		else { //FIXME data.getYear  y date.getDay no es lo esperado
			var nums = [date.getYear(), date.getMonth(), date.getDay()];
		}
		this.yearEditor.setValue( 2000 - parseInt( nums[ 0 ], 10 ) );
		this.monthEditor.setValue( parseInt( nums[ 1 ], 10 ) - 1 );
		this.dayEditor.setValue( parseInt( nums[ 2 ], 10 ) - 1 );
	}
	
	this.editor.setValue( this.date, '-' );

	return this.editor;
}																																	

Field.prototype.createRadioSelect = function() {
	var editor = this.createDOM("DIV");
	editor.id = this.id;
	var radio, label, name = this.label.replace(" ", "_");
	for(var i=0; i<this.data.length; i++) {
		radio = this.createRadio();
		radio.name = name;
		radio.value = this.data[ i ];
		editor.appendChild( radio );
		label = document.createElement("STRING");
		label.innerHTML = this.data[ i ] + "&nbsp;";
		editor.appendChild( label );
	}
	return editor;
}

Field.prototype.createSelect = function(data, multiple) {
	var editor = this.createDOM("SELECT");
	editor.id = this.id;
	editor.multiple = multiple;
	var option;
	for(var i = 0; i < this.data.length; i++) {
		option = document.createElement("OPTION");
		option.text = this.data[i];
		option.value = i;
		option.id = editor.id + '.' + i;
		
		if(navigator.appName != "Netscape")
			editor.add(option);
		else
			editor.appendChild(option);
	}

	editor.getValue = function() {
		return this.value;
	}

	editor.setValue = function(value) {
		this.value = value;
	}

	editor.onblur = function( e ) {
		for(var i=0; i<this.actionListeners.length; i++) {
//	alert(	this.actionListeners[ i ]);
			this.actionListeners[ i ].onblur( e );
		}
	}
	
	return editor;
}

Field.prototype.createCombo = function(data) {
	return this.createSelect(data, false);
}

Field.prototype.createList = function(data) {
	return this.createCombo(data, true);
}

Field.prototype.createCheck = function(value) {
	var editor = this.createInput("checkbox");
	editor.setValue = function(value) {	this.checked = value;	}
	editor.getValue = function() {	return this.checked; }

	editor.setValue(value);

	return editor;
}

Field.prototype.createRadio = function() {
		return this.createInput("radio");
}

Field.prototype.createFileUploader = function() {
		return this.createInput("file");
}
Field.prototype.createPassword = function() {
		return this.createInput("password");
}

Field.prototype.createCustomEditor = function() {
}

Field.prototype.getValue = function() {
	return this.editor.getValue();
}

Field.prototype.setValue = function(value) {
	this.editor.setValue( value );
}

function Form(id) {
	this.container = document.getElementById(id);
	this.fields = new Array();
	this.header = document.createElement("DIV");
}

Form.prototype.setHeaderContent = function(data) {
	this.header.innerHTML = data;
}

Form.prototype.addField = function( field ) {
	this.fields[ this.fields.length ] = field;
}

Form.prototype.add = function(id, label, type, data, readOnly) {
	this.fields[ this.fields.length ] = new Field(id, label, type, data, readOnly);
}

Form.prototype.show = function() {
	var table = document.createElement("TABLE");
	table.border = 1;
	this.container.fields = new Array();
	for(var i=0; i< this.fields.length; i++) {
		table.insertRow( i );
		table.rows[ i ].insertCell( 0 );
		table.rows[ i ].cells[ 0 ].innerHTML = this.fields[ i ].label; 
		table.rows[ i ].insertCell( 1 );
		table.rows[ i ].cells[ 1 ].appendChild( this.fields[ i ].editor );
		this.container.fields[ this.fields[ i ].id ] = this.fields[ i ];
	}
	this.container.appendChild( this.header );
	this.container.appendChild( table );
}


