// JavaScript Document

/************************************
*																		*
*					gema_functions.js					*
*																		*
************************************/

/*
	Contains useful features for different applications. This code works for browsers
	'Firefos Mozilla (Netscape) and 'Microsoft Internet Explorer '.
	Note: You need the file js 'messages.js'.
*/

function trigger_action_button_pressing_enter(ev, button)
{
  /*
    Triggers the action button when you press the enter key.
		Param:
	  	- 'ev': object event.
	  	- 'button': id of the button to trigger action.
  */

  var key;
  switch (navigator.appName) {
    case 'Microsoft Internet Explorer': key = ev.keyCode;
	                                    	if (key == 13) {
										  										document.getElementById(button).focus();
																				}
	                                    	break;
		case 'Netscape':	key = ev.which;
	                 		if (key == 13) {
												document.getElementById(button).focus();
					   						document.getElementById(button).click();
					 						}
	                 		break;
  }
}

function get_date(f)
{
	/*
		Get today's date in a format specified.
		Param:
			- 'f' = date format.
	*/
	
	if (arguments.length > 1) {
		alert("Función 'get_date': demasiados argumentos.");
		return '1900-01-01 00:00:00';
	}
	
	var formats = new Array('dd-mm-yyyy', 'yyyy-mm-dd', 'hh:mm', 'dd-mm-yyyy hh:mm:ss', 'yyyy-mm-dd hh:mm:ss');
	
	f = new String(arguments[0]);
	
	var format_available = false;
	for (var i = 0; i < formats.length; i++) {
		if (formats[i] == f) {
			format_available = true;
			break;
		}
	}
	if (!format_available) {
		alert("Función 'get_date': formato de fecha no disponible.");
		return '1900-01-01 00:00:00';
	}
	
	if (arguments.length == 1) {
		var today = new Date();
		var year = today.getFullYear();
		var month = today.getMonth()+1;
		month = (month < 10) ? ('0' + month) : (month);
		var date = today.getDate();
		date = (date < 10) ? ('0' + date) : (date);
		var hour = today.getHours();
		hour = (hour < 10) ? ('0' + hour) : (hour);
		var minute = today.getMinutes();
		minute = (minute < 10) ? ('0' + minute) : (minute);
		var second = today.getSeconds();
		second = (second < 10) ? ('0' + second) : (second);
	}
	
	if ( (f.indexOf('dd-mm-yyyy') != -1) || (f.indexOf('yyyy-mm-dd') != -1) ) {
		f = f.replace('yyyy', year);
		f = f.replace('mm', month);
		f = f.replace('dd', date);
	}
	if (f.indexOf('hh:mm') != -1) {
		f = f.replace('hh', hour);
		f = f.replace('mm', minute);
	}
	if (f.indexOf('ss') != -1) {
		f = f.replace('ss', second);
	}
	
	return f;
}

function register_function_in_event(id, ev, func)
{
	/*
		Register a function to an event.
		Param
			- 'id': id field.
			- 'ev': event in which the recording function.
			- 'func': function to register.
	*/
	
	var old_function;
	var el = document.getElementById(id);
	switch (ev) {
		case 'onblur':	old_function = (el.onblur) ? el.onblur : function () {} ;
										el.onblur = function () {
											old_function();
											func();
										};
										break;
		case 'onchange':	old_function = (el.onchange) ? el.onchange : function () {} ;
											el.onchange = function () {
												old_function();
												func();
											};
											break;
		case 'onclick':	old_function = (el.onclick) ? el.onclick : function () {} ;
										el.onclick = function () {
											old_function();
											func();
										};
										break;
		case 'onfocus':	old_function = (el.onfocus) ? el.onfocus : function () {} ;
										el.onfocus = function () {
											old_function();
											func();
										};
										break;
		case 'onkeydown':	old_function = (el.onkeydown) ? el.onkeydown : function () {} ;
											el.onkeydown = function () {
												old_function();
												func();
											};
											break;
		case 'onload':	old_function = (el.onload) ? el.onload : function () {} ;
										el.onload = function () {
											old_function();
											func();
										};
										break;
	}
}

function have_numbers(s)
{
	/*
		Detect if a string is composed of numbers.
		Param:
			- 's': string to check.
	*/
	
	var numbers = '0123456789';
	for(var i = 0; i < s.length; i++) {
		if (numbers.indexOf(s.charAt(i), 0)!= -1)
			return 1;
	}	
	return 0;
} 

function have_letters(s)
{
	/*
		Detect if a string is composed of letters.
		Param:
			- 's': string to check.
	*/
	
	var letters = 'abcdefghyjklmnñopqrstuvwxyz';
	s = s.toLowerCase();
	for(i = 0; i < s.length; i++) {
		if (letters.indexOf(s.charAt(i),0)!= -1)
			return 1;
	}
	return 0;
	
}

function enable_form_elements(a)
{
	/*
		Enables the elements of the form. If not pass any parameters, it enables all elements
		of the form.
		Param:
			- 'a': arrangement that contains the id of the elements of the form to enable.
				Example: a = ['search', 'save', 'cancel'];
	*/
	
	var i, l;
	
	if (arguments.length == 0) {
		l = document.forms[0].elements.length;
		var b = new Array();
		for (i = 0; i < l; i++) {
			b.push(document.forms[0].elements[i].id);
		}
		a = b;
	}
	
	l = a.length;
	for (i = 0; i < l; i++) {
		document.getElementById(a[i]).disabled = false;
	}
}

function disable_form_elements(a)
{
	/*
		Disables the elements of the form. If not pass any parameters, it disables all
		elements of the form.
		Param:
			- 'a': arrangement that contains the id of the elements of the form to disable.
				Example: a = ['search', 'save', 'cancel'];
	*/
	
	var i, l;
	
	if (arguments.length == 0) {
		l = document.forms[0].elements.length;
		var b = new Array();
		for (i = 0; i < l; i++) {
			b.push(document.forms[0].elements[i].id);
		}
		a = b;
	}
	
	l = a.length;
	for (i = 0; i < l; i++) {
		document.getElementById(a[i]).disabled = true;
	}
}

function reset_form_elements(a)
{
	/*
		Reset the elements of the form. If not pass any parameters, it resets all
		elements of the form.
		Param:
			- 'a': arrangement that contains the id of the elements of the form to reset.
				Example: a = ['search', 'save', 'cancel'];
	*/
	
	var i, l;
	var radio_name = '', radio_name_aux = '';
	
	if (arguments.length == 0) {
		l = document.forms[0].elements.length;
		var b = new Array();
		for (i = 0; i < l; i++) {
			b.push(document.forms[0].elements[i].id);
		}
		a = b;
	}
	
	for(var i = 0; i < a.length; i++) {
		el = document.getElementById(a[i]);
		switch (el.type) {
			case 'select-one':	el.options[0].selected = true;
													break;
			case 'text':	el.value = '';
										break;
			case 'textarea':	el.value = '';
												break;
			case 'radio':	radio_name = el.name;
										if (radio_name == radio_name_aux) {
											el.checked = false;
										}
										else {
											el.checked = true;
											radio_name_aux = radio_name;
										}
										break;
			case 'checkbox':	el.checked = false;
								break;
		}
	}
}

function press_link(el)
{
	el.style.borderLeftWidth = '2px';
	el.style.borderTopWidth = '2px';
	el.style.borderRightWidth = '0px';
	el.style.borderBottomWidth = '0px';
}

function up_link(el)
{
	el.style.borderLeftWidth = '0px';
	el.style.borderTopWidth = '0px';
	el.style.borderRightWidth = '2px';
	el.style.borderBottomWidth = '2px';
}

function add_option_to_select(id, value, text)
{
	/*
		Adds an option to an element 'select'.
		Param:
			- 'id': id field.
			- 'value': value option.
			- 'text': text option.
	*/
	
	var option = document.createElement('option');
	option.value = value;
	option.text = text;
	
	if (navigator.appName == 'Microsoft Internet Explorer')
		document.getElementById(id).add(option);
	else
		if (navigator.appName == 'Netscape')
			document.getElementById(id).appendChild(option);
	
}

function delete_option_to_select(id, index)
{
	
	document.getElementById(id).remove(index);
	
}