// JavaScript Document

/************************************
*																		*
*					validate_form.js					*
*																		*
************************************/

/*
	Contains functions for validating forms. This code works for browsers 'Firefos Mozilla
	(Netscape) and 'Microsoft Internet Explorer '.
	Note: You need the file js 'sim_functions.js' and 'messages.js'.
*/

function set_obligatory_data(id)
{
	/*
		Set a form field as obligatory.
		Param:
			- 'id': id field.
	*/
	
	var attributeName;
	switch (navigator.appName) {
		case 'Microsoft Internet Explorer':	attributeName = 'className';
	                                    	break;
 		case 'Netscape':	attributeName = 'class';
											break;
	}
	
	el = document.getElementById(id);
	el.setAttribute(attributeName, 'dato_obligatorio');
	
	var func = function () {
		document.getElementById(id).setAttribute(attributeName, '');
	};
	
	switch (el.tagName) {
		case 'INPUT':	if (el.readOnly)
										register_function_in_event(id, 'onblur', func);
									else
										register_function_in_event(id, 'onkeydown', func);
									break;
		case 'TEXTAREA':	register_function_in_event(id, 'onkeydown', func);
											break;
		case 'SELECT':	register_function_in_event(id, 'onchange', func);
										break;
		default:	alert('Error: Seteo de dato obligatorio para etiqueta desconocida no implementada.');
							break;
	}
}

function validate_data_completed(a)
{
	/*
		Validate if the form fields was completed.
		Param:
	  	- 'a': arrangement that contains the id of the elements of the form to validate.
	*/
	
	var completed = true, data_completed = true, el, i, l;

	l = a.length;
	for (i = 0; i < l; i++) {
		el = document.getElementById(a[i]);	
		switch (el.tagName) {
			case 'INPUT':			data_completed = validate_data_completed_input(a[i]);
												break;
			case 'TEXTAREA':	data_completed = validate_data_completed_textarea(a[i]);
												break;
			case 'SELECT':		data_completed = validate_data_completed_select(a[i]);
												break;
			default:					alert('Error: Etiqueta desconocida para la validación.');
												data_completed = false;
												break;
		}
		
		if (!data_completed) {
			set_obligatory_data(a[i]);
			if (completed)
				completed = data_completed;
		}
	}

	if (!completed) {
		set_message('Algunos datos no fueron completados.', 'mensaje_erroneo');
	}
	
	return completed;
}

function validate_data_completed_input(id)
{
	/*
		Validate if an INPUT field was completed.
		Param:
			- 'id': id field.
	*/
	
	var el = document.getElementById(id);
	
	switch (el.type) {
		case 'text':			if (el.value == '')
												return false;
											else
												return true;
		case 'file':			if (el.value == '')
												return false;
											else
												return true;
		case 'password':	if (el.value == '')
												return false;
											else
												return true;
		case 'checkbox':	alert('Error: Validación de etiqueta INPUT de tipo CHECKBOX no implementada.');
											return false;
		case 'radio':			alert('Error: Validación de etiqueta INPUT de tipo RADIO no implementada.');
											return false;
	}
}

function validate_data_completed_select(id)
{
	/*
		Validate if an SELECT field was completed.
		Param:
			- 'id': id field.
	*/
	
	var el = document.getElementById(id);
	if (el.options[el.selectedIndex].text != '')
		return true;
	else
		return false;
}

function validate_data_completed_textarea(id)
{
	/*
		Validate if an TEXTAREA field was completed.
		Param:
			- 'id': id field.
	*/
	
	var el = document.getElementById(id);
	if (el.value == '')
		return false;
	else
		return true;
}