<!--

// Cambia el estilo de una fila para iluminarla o apagarla
//	f:			fila a iluminar o apagar
//	color:	color a aplicar a la fila
function fila_on_off(f, st) {
	f.className = st;
}

// Abre una ventana de popup centrada en la pantalla
// con barra de estado
//	url:	url a abrir en el popup
//	win:	nombre de la ventana de popup
//	w:		ancho de la ventana
//	h:		alto de la ventana
function new_window(url, win, w, h) {
  var max_height = screen.availHeight - 30;
  h = (h > max_height) ? max_height : h;
  var posx = (screen.availWidth - w) / 2;
  var posy = (max_height - h) / 2;
  var opt = "titlebar=no,toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=" + w + ",height=" + h + ",top=" + posy + ",left=" + posx;

  var v = window.open(url, win, opt);
  v.focus();
}

// Abre una ventana de popup centrada en la pantalla
// con barra de estado y barra de menu para imprimir
//	url:	url a abrir en el popup
//	win:	nombre de la ventana de popup
//	w:		ancho de la ventana
//	h:		alto de la ventana
function new_window_prn(url, win, w, h) {
  var max_height = screen.availHeight - 30;
  h = (h > max_height) ? max_height : h;
  var posx = (screen.availWidth - w) / 2;
  var posy = (max_height - h) / 2;
  var opt = "titlebar=no,toolbar=no,location=no,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=" + w + ",height=" + h + ",top=" + posy + ",left=" + posx;

  var v = window.open(url, win, opt);
  v.focus();
}

/*********************
** GESTION DE MENUS **
*********************/
// Los menus se llamaran:
//		 <menu_name>_<menu_orden>
//		 		<menu_name>_<menu_orden>_<menu_orden>
//						...
// Las imagenes de expandir o colapsar asociadas a cada menu se llamaran:
//		 <img_name>_<menu_orden>
//		 		<img_name>_<menu_orden>_<menu_orden>
//						...
// Deberan declararse en la pagina dos variables:
//		img_expandir: icono de expandir (con mapeo)
//		img_colapsar: icono de colapsar (con mapeo)
// Si no existen esas variables o las imagenes correspondientes no se
// cambia el icono al expandir o colapsar

// Expande o colapsa un menu y cambia su imagen asociada
//	menu_name:		prefijo del nombre del menu
//	img_name:			prefijo del nombre de la imagen del menu
//	menu_orden:		ordinal del menu
function menu_on_off(menu_name, img_name, menu_orden) {
  var menu = document.getElementById(menu_name + '_' + menu_orden).style;
  var img = document.getElementById(img_name + '_' + menu_orden);
  if (menu.display == 'none') {
    menu.display='block';
    if ( img && (typeof(img_colapsar) != "undefined") )
    	img.src=img_colapsar;
  }
  else {
    menu.display='none';

    if ( img && (typeof(img_expandir) != "undefined"))
	    img.src=img_expandir;
  }
}

// Expande todos los menus
//	menu_name:		prefijo del nombre de los menus
//	img_name:			prefijo del nombre de las imagenes de los menus
function expand_all(menu_name, img_name) {
	for (var i=0; document.getElementById(menu_name + "_" + i); i++) {
		document.getElementById(menu_name + "_" + i).style.display = 'block';
		if ( (document.getElementById(img_name + "_" + i)) &&
					(typeof(img_colapsar) != "undefined"))
			document.getElementById(img_name + "_" + i).src = img_colapsar;
		expand_all(menu_name + "_" + i, img_name + "_" + i);
	}
}

// Colapsa todos los menus
//	menu_name:		prefijo del nombre de los menus
//	img_name:			prefijo del nombre de las imagenes de los menus
function collapse_all(menu_name, img_name) {
	for (var i=0; document.getElementById(menu_name + "_" + i); i++) {
		document.getElementById(menu_name + "_" + i).style.display = 'none';
		if ( (document.getElementById(img_name + "_" + i)) &&
					(typeof(img_expandir) != "undefined"))
			document.getElementById(img_name + "_" + i).src = img_expandir;
		collapse_all(menu_name + "_" + i, img_name + "_" + i);
	}
}

/**************************************
** CHEQUEOS DE CAMPOS DE FORMULARIOS **
**************************************/

// Chequeo de campos vacios (numero de parametros variable)
//	parametro 1: 					nombre del formulario
//	parametros 2, 4, ...:	nombre del campo en el formulario
//	parametros 3, 5, ...:	nombre del campo para mostrar en el alert
function chk_empty() {
	args = chk_empty.arguments;
	errors = false;
	txt_error = "ERROR. Los siguientes campos son obligatorios:\n";
	fld_blink= "";

	frm = args[0];
	for (var i=1; i<args.length - 1; i+=2) {
		fld_name = args[i];
		fld_alrt = args[i+1];
		fld = document.forms[frm].elements[fld_name];

		// Texto: chequeo si esta vacio o solo hay espacios
		if ((fld.type == "text") || (fld.type == "textarea") || (fld.type == "password") || (fld.type == "file")) {
			
			if (fld.value.search(/\S/) == -1) {
				txt_error += "\t- " + fld_alrt + "\n";
				if (!errors)
					fld_blink=fld_name;

				errors = true;
			}
		}
		else
		// Select simple: chequeo si no hay nada seleccionado o el valor
		//								de lo seleccionado es cero (0).
		if (fld.type == "select-one") {
			if ( (fld.selectedIndex < 0) || (fld.value == '0') ) {
				txt_error += "\t- " + fld_alrt + "\n";
				if (!errors)
					fld_blink=fld_name;
				errors = true;
			}
		}
		else
		// Select multiple: chequeo si no hay nada seleccionado.
		if (fld.type == "select-multiple") {
			if (fld.selectedIndex < 0) {
				txt_error += "\t- " + fld_alrt + "\n";
				if (!errors)
					fld_blink=fld_name;
				errors = true;
			}
		}
		else
		// Checkbox: chequeo que este marcado
		if (fld.type == "checkbox") {
			if (!fld.checked) {
				txt_error += "\t- " + fld_alrt + "\n";
				if (!errors)
					fld_blink=fld_name;
				errors = true;
			}
		}
	}
	if (errors)	MarcaErrorCampo(fld_blink, txt_error, false);
		
	return !errors;
}

// Chequeo de campos numericos de tipo entero (numero de parametros variable)
//	parametro 1: 					nombre del formulario
//	parametros 2, 4, ...:	nombre del campo en el formulario
//	parametros 3, 5, ...:	nombre del campo para mostrar en el alert
function chk_integer() {
	args = chk_integer.arguments;
	errors = false;
	txt_error = "ERROR. Los siguientes campos deben ser numericos:\n";
	fld_blink="";

	frm = args[0];
	for (var i=1; i<args.length - 1; i+=2) {
		fld_name = args[i];
		fld_alrt = args[i+1];
		fld = document.forms[frm].elements[fld_name];

		// Chequeo si no esta vacio y contiene no-digitos
		if ( fld.value != "" && (fld.value.search(/\D/) != -1) ) {
			txt_error += "\t- " + fld_alrt + "\n";
			if (!errors)
				fld_blink=fld_name;
				
			errors = true;
		}
	}
	
	if (errors)	MarcaErrorCampo(fld_blink, txt_error, true);
		
	return !errors;
}

// Chequeo de campos numericos de tipo float (numero de parametros variable)
//	parametro 1: 					nombre del formulario
//	parametros 2, 4, ...:	nombre del campo en el formulario
//	parametros 3, 5, ...:	nombre del campo para mostrar en el alert
function chk_float() {
	args = chk_float.arguments;
	errors = false;
	txt_error = "ERROR. Los siguientes campos deben ser numericos con o sin una coma decimal:\n";
	fld_blink="";

	frm = args[0];
	for (var i=1; i<args.length - 1; i+=2) {
		fld_name = args[i];
		fld_alrt = args[i+1];
		
		exp_reg = /^(\+|-)?\d+(\,\d+){0,1}$/;
		
		fld = document.forms[frm].elements[fld_name];
		
		// Chequeo si no esta vacio y contiene solo digitos separados por un punto
		if ( fld.value != "" && !exp_reg.test(fld.value) ) {
			txt_error += "\t- " + fld_alrt + "\n";
			if (!errors)
				fld_blink=fld_name;
				
			errors = true;
		}
	}
	if (errors)	MarcaErrorCampo(fld_blink, txt_error, true);
	
	return !errors;
}

// Chequeo de fechas (numero de parametros variable)
//	parametro 1: 					nombre del formulario
//	parametros 2, 4, ...:	nombre del campo en el formulario
//	parametros 3, 5, ...:	nombre del campo para mostrar en el alert
function chk_date() {
	args = chk_date.arguments;
	errors = false;
	error_fld= false;
	txt_error = "ERROR. Los siguientes campos no contienen fechas correctas (dd-mm-aaaa):\n";
	var dia=mes=ano=0;
	fld_blink="";

	frm = args[0];
	for (var i=1; i<args.length - 1; i+=2, error_fld=false) {
		fld_name = args[i];
		fld_alrt = args[i+1];
		fld = document.forms[frm].elements[fld_name];

		// Chequeo si no esta vacio y no contiene un formato de fecha valido
		if (fld.value != "") {
				fecha=fld.value.split("-"); 
			 
				if(fecha.length==3 && parseInt(fecha[2],10) > 1000) { 
						dia=parseInt(fecha[0],10);
						mes=parseInt(fecha[1],10);
						ano=parseInt(fecha[2],10);
						
				    var mifecha = new Date(ano,--mes,dia);
				    
				    if(dia != parseInt(mifecha.getDate()) || mes != parseInt(mifecha.getMonth()) ||
				    		(ano != parseInt(mifecha.getYear()) && ano != parseInt(mifecha.getYear() + 1900)))	error_fld=true;
				}
				else
					error_fld=true;
				
				if (error_fld) {
			    txt_error += "\t- " + fld_alrt + "\n";
					if (!errors) {
						fld_blink=fld_name;
						errors = true;
					}
				}
		} // IF fecha no vacia
	} // FOR
	
	if (errors)	MarcaErrorCampo(fld_blink, txt_error, true);
	
	return !errors;
}

// Chequeo de campos con horas (numero de parametros variable)
//	parametro 1: 					nombre del formulario
//	parametros 2, 4, ...:	nombre del campo en el formulario
//	parametros 3, 5, ...:	nombre del campo para mostrar en el alert
function chk_time() {
	args = chk_time.arguments;
	errors = false;
	txt_error = "ERROR. Los siguientes campos deben contener una hora correcta en formato 24 horas:\n";
	fld_blink="";

	frm = args[0];
	for (var i=1; i<args.length - 1; i+=2) {
		fld_name = args[i];
		fld_alrt = args[i+1];
		
		exp_reg = /^([1-9]|[0-1]\d|2[0-3]):([0-5]\d)$/;

		fld = document.forms[frm].elements[fld_name];
		
		// Chequeo si no esta vacio y contiene un formato de 24 horas correcto
		if ( fld.value != "" && !exp_reg.test(fld.value) ) {
			txt_error += "\t- " + fld_alrt + "\n";
			if (!errors)
				fld_blink=fld_name;
				
			errors = true;
		}
	}
	
	if (errors)	MarcaErrorCampo(fld_blink, txt_error, true);
	
	return !errors;
}

// Chequeo de e-mails (numero de parametros variable)
//	parametro 1: 					nombre del formulario
//	parametros 2, 4, ...:	nombre del campo en el formulario
//	parametros 3, 5, ...:	nombre del campo para mostrar en el alert
function chk_email() {
	args = chk_email.arguments;
	errors = false;
	txt_error = "ERROR. Los siguientes campos no contienen formatos de e-mail correctos:\n";
	fld_blink="";

	frm = args[0];
	for (var i=1; i<args.length - 1; i+=2) {
		fld_name = args[i];
		fld_alrt = args[i+1];
		fld = document.forms[frm].elements[fld_name];

		// Chequeo si no esta vacio y contiene un formato de e-mail valido
		if ( fld.value != "" && (fld.value.search(/^[a-zA-Z](-\w+|\.\w+|\w*)*@[a-zA-Z](-\w+|\.\w+|\w*)*\.[a-zA-Z]{2,3}$/) == -1) ) {
			txt_error += "\t- " + fld_alrt + "\n";
			if (!errors)
				fld_blink=fld_name;
			
			errors = true;
		}
	}
	
	if (errors)	MarcaErrorCampo(fld_blink, txt_error, true);
	
	return !errors;
}

// Chequeo de nombres de fichero (numero de parametros variable)
//	parametro 1: 					nombre del formulario
//	parametros 2, 4, ...:	nombre del campo en el formulario
//	parametros 3, 5, ...:	nombre del campo para mostrar en el alert
function chk_file() {
	args = chk_file.arguments;
	errors = false;
	txt_error = "ERROR. Los siguientes campos no contienen nombres de fichero correctos:\n";
	fld_blink="";

	frm = args[0];
	for (var i=1; i<args.length - 1; i+=2) {
		fld_name = args[i];
		fld_alrt = args[i+1];
		fld = document.forms[frm].elements[fld_name];
//alert(fld.value);
		indice=fld.value.lastIndexOf("\\");
		fichero=fld.value.substr(indice+1);
//alert(fichero + ":" + fichero.search(/\S/) + " : " + fichero.search(/(\\|\/)\w+(\.\w+)?$/));
		// Chequeo si no esta vacio y contiene un nombre de fichero valido
		if ( (fichero.search(/\S/) != -1) && (fichero.search(/(\\|\/)\w+(\.\w+)?$/) != -1) ) {
			txt_error += "\t- " + fld_alrt + "\n";
			if (!errors)
				fld_blink=fld_name;
			
			errors = true;
		}
	}
	
	if (errors)	MarcaErrorCampo(fld_blink, txt_error, true);
	
	return !errors;
}

// Chequeo de urls (numero de parametros variable)
//	parametro 1: 					nombre del formulario
//	parametros 2, 4, ...:	nombre del campo en el formulario
//	parametros 3, 5, ...:	nombre del campo para mostrar en el alert
function chk_url() {
	args = chk_url.arguments;
	errors = false;
	txt_error = "ERROR. Los siguientes campos no contienen urls correctas:\n";
	fld_blink="";

	frm = args[0];
	for (var i=1; i<args.length - 1; i+=2) {
		fld_name = args[i];
		fld_alrt = args[i+1];
		fld = document.forms[frm].elements[fld_name];

		// Chequeo si no esta vacio y contiene una url valida. Solo se admiten
		// los protocolos http, https y ftp.
		if ( fld.value != "" && (fld.value.search(/^[http:\/\/|https:\/\/|ftp:\/\/]\w+(\.\w+)*(\/1\/\w+.\w+)?(@\w+:\w+)?$/) == -1) ) {
			txt_error += "\t- " + fld_alrt + "\n";
			if (!errors)
				fld_blink=fld_name;

			errors = true;
		}
	}
	
	if (errors)	MarcaErrorCampo(fld_blink, txt_error, true);
	
	return !errors;
}

/************************/
/** FORMATEO DE CAMPOS **/
/************************/
// Cambia una comilla simple por dos en los campos de texto
// y los textareas de un formulario
//	f:		Nombre del formulario donde realizar el cambio
function chg_apostrofes(f) {
	for (var i=0; i < document.forms[f].elements.length; i++) {
		if ((document.forms[f].elements[i].type == 'text') || 
				(document.forms[f].elements[i].type == 'textarea'))
			document.forms[f].elements[i].value = document.forms[f].elements[i].value.replace(/\'/g,"\'\'");
	}
}

// Cambia dos comillas simples por una en los campos de texto
// y los textareas de un formulario
//	f:		Nombre del formulario donde realizar el cambio
function chg_dobles_apostrofes(f) {
	for (var i=0; i < document.forms[f].elements.length; i++) {
		if ((document.forms[f].elements[i].type == 'text') || 
				(document.forms[f].elements[i].type == 'textarea'))
			document.forms[f].elements[i].value = document.forms[f].elements[i].value.replace(/\'\'/g,"\'");
	}
}

// Cambia los retornos de carro (\n) por <br> en los textareas
// de un formulario
//	f:		Nombre del formulario donde realizar el cambio
function chg_retornos(f) {
	for (var i=0; i < document.forms[f].elements.length; i++) {
		if (document.forms[f].elements[i].type == 'textarea')
			document.forms[f].elements[i].value = chg_brs_retornos(document.forms[f].elements[i].value, /\r\n/g, "<br>");
	}
}

// Cambia los <br> por retornos de carro (\n) en los textareas
// de un formulario
//	f:		Nombre del formulario donde realizar el cambio
function chg_brs(f) {
	for (var i=0; i < document.forms[f].elements.length; i++) {
		if (document.forms[f].elements[i].type == 'textarea')
			document.forms[f].elements[i].value = chg_brs_retornos(document.forms[f].elements[i].value, /<br>/g, "\n");
	}
}

// Cambia en un texto una cadena por otra, pero no realiza el cambio en la parte del texto
// que este encerrada entre los tags "<html>" y "</html>". Devuelve el texto modificado
//	texto:	texto con el contenido
//	origen:	cadena a buscar
//	destino:	cadena a poner
function chg_brs_retornos(texto, origen, destino) {
  var start=end=0;
  var str=texto;
  var TExtraido;
  var TFinal="";

  while (end != -1) {
    if ((end=str.indexOf("<html>",start)) == -1) {
      TExtraido=str.substring(start,str.length+1);
      TFinal=TFinal.concat(TExtraido.replace(origen,destino));
    }
    else {
      TExtraido=str.substring(start,end);
      TFinal=TFinal.concat(TExtraido.replace(origen,destino));

      start=end;
      if ((end=str.indexOf("</html>",start)) == -1)
        TFinal=TFinal.concat(str.substring(start,str.length+1));
      else {
        end=end+7;
        TFinal=TFinal.concat(str.substring(start,end));
        start=end;
      }
    }
  }

  return TFinal;
}

/******************
** LISTAS DUALES **
******************/
// Selecciona todos los valores de un desplegable
//	lista: lista a marcar
function select_all(lista) {
	var len = 0;
	for(len = 0; len < lista.options.length; len++)
		lista.options[ len ].selected = true;
}

// Deselecciona todos los valores de un desplegable
//	lista: lista a marcar
function unselect_all(lista) {
	var len = 0;
	for(len = 0; len < lista.options.length; len++)
		lista.options[ len ].selected = false;
}

// Compara dos opciones de una lista por valor
//	opt1:	primera opcion
//	opt2:	segunda opcion
function cmp_option_val(opt1, opt2) {
	// Radix 10: numeros
	// Radix 36: alfanumericos
	var s1 = parseInt(opt1.value, 36);
	var s2 = parseInt(opt2.value, 36);
	return s1 - s2;
}

// Compara dos opciones de una lista por texto
//	opt1:	primera opcion
//	opt2:	segunda opcion
function cmp_option_txt(optA, optB) {
	// Radix 10: numeros
	// Radix 36: alfanumericos
	var sA = parseInt(optA.text, 36);
	var sB = parseInt(optB.text, 36);
	return sA - sB;
}

// Mueve valores entre dos listas
//	move_from:	lista origen
//	move_to:		lista destino
//	move_all:		mover todos los valores (true) o solo los seleccionados (false)
//	move_ord:		ordenacion de la lista destino (v: valor, t: texto)
function dual_move(move_from, move_to, move_all, move_ord) {
  // Si no hay nada seleccionado y no se quiere mover todo no se hace nada
	if ( (move_from.selectedIndex == -1) && (move_all == false) )
		return;

	var len = move_to.options.length;
	var save_selected = new Array();

	// Copia de los elementos de la lista origen en la lista auxiliar
	for(var i = 0, x=0; i < move_from.options.length; i++) 
	{
		if ( (move_from.options[i] != null) &&
					((move_from.options[i].selected == true) || move_all) ) {
			save_selected[x++]=i;
			move_to.options[ len ] = new Option(move_from.options[i].text, move_from.options[i].value, move_from.options[i].defaultSelected, move_from.options[i].selected);
			len++;
		}
	}

	// Borrado de los elementos de la lista origen
	for(var i = save_selected.length - 1; i >= 0; i--) move_from.options[save_selected[i]]=null;
}

function dual_move2(move_from, move_to, move_all, move_ord) {
  // Si no hay nada seleccionado y no se quiere mover todo no se hace nada
	if ( (move_from.selectedIndex == -1) && (move_all == false) )
		return;

	// Lista auxiliar donde componer la lista destino
	new_move_to = new Array(move_to.options.length);

	var len = 0;

	// Copia de la lista destino en la lista auxiliar
	for(len = 0; len < move_to.options.length; len++)	{
		if (move_to.options[ len ] != null)
			new_move_to[ len ] = new Option(move_to.options[ len ].text, move_to.options[ len ].value, move_to.options[ len ].defaultSelected, move_to.options[ len ].selected);
	}

	// Copia de los elementos de la lista origen en la lista auxiliar
	for(var i = 0; i < move_from.options.length; i++) 
	{
		if ( (move_from.options[i] != null) &&
					((move_from.options[i].selected == true) || move_all) ) {
			new_move_to[ len ] = new Option(move_from.options[i].text, move_from.options[i].value, move_from.options[i].defaultSelected, move_from.options[i].selected);
			len++;
		}
	}

	// Ordenacion de la nueva lista
	if (move_ord == "v")
		new_move_to.sort(cmp_option_val);
	else if (move_ord == "t")
		new_move_to.sort(cmp_option_txt);

	// Copia de lalista auxiliar en la lista destino
	for (var j = 0; j < new_move_to.length; j++) {
		if (new_move_to[ j ] != null)
			move_to.options[ j ] = new_move_to[ j ];
	}

	// Borrado de los elementos de la lista origen
	for(var i = move_from.options.length - 1; i >= 0; i--) {
		if ( (move_from.options[i] != null) &&
					((move_from.options[i].selected == true) || move_all) )
			move_from.options[i] = null;
	}
}

// Inicializa una lista con algunos valores de otra lista
//	move_from:	lista origen
//	move_to:		lista destino
//	move_values:	array con la lista de valores a mover de una lista a otra.
//	move_ord:		ordenacion de la lista destino (v: valor, t: texto)
function dual_init(move_from, move_to, move_values, move_ord) {
		// Borramos primero todos los elementos que haya en la lista destino
	for (var i = move_to.options.length - 1; i >= 0; i--)
	  if (move_to.options[i] != null)	move_to.options[i] = null;
	
	// Seleccionamos en la lista origen todos los valores indicados en move_values
	for (var i = 0; i < move_values.length; i++) {
		for (var j=0; j < move_from.options.length; j++) {
			if (move_values[i] == move_from.options[j].value) {
				move_from.options[j].selected = true;
				break;
			}
		}
	}
	
	dual_move(move_from, move_to, false, move_ord);
}

// Inicializa una lista con algunos valores de otra lista, recibiendo los valores como una cadena con formato
//	move_from:			lista origen
//	move_to:				lista destino
//	move_values:		string con la lista de valores a mover de una lista a otra y con formato
//	move_separador:	caracter separador de los valores dentro de la cadena (por defecto ",")
//									El caracter separador no debe estar dentro de alguno de los valores.
//	move_cualificador:	caracter utilizado para cualificar los valores
//											El caracter utilizado no debe estar dentro de alguno de los valores.
//	move_ord:				ordenacion de la lista destino (v: valor, t: texto)
function dual_init_string(move_from, move_to, move_values, move_separador, move_cualificador, move_ord) {
	// Se suprime el cualificador
	var aux=(move_cualificador == "" ? move_values : move_values.replace(move_cualificador, ""));
	// Separamos los valores
	aux=move_values.split((move_separador == "" ? "," : move_separador));

	dual_init(move_from, move_to, aux, move_ord);
}

/*****************************
** Utilidades campos SELECT **
*****************************/
// Selecciona un elemento de un select simple
function Select_Preselecciona(select, valor)
{
  for (var i=0; i < select.length; i++)
     if (select.options[i].value == valor) {
       select.selectedIndex=i;
       return true;
     }
  
  return false;
}

// Inicializa una lista con valores recibidos en un array
//	lista:	lista origen
//	datos:	array de datos (primera posicion es el valor y segunda es la descripcion y asi sucesivamente)
//	defecto: valor por defecto
function Select_Init(lista, datos, defecto) {
	// Borramos primero todos los elementos que haya en la lista
	for (var i=lista.options.length - 1; i >= 0; i--) lista.options[i] = null;
	  
	// Inicializamos la lista con los valores del array
	for (var i=0, x=0; i < (datos.length / 2); i++, x+=2) lista.options[i]=new Option(datos[x+1], datos[x], false, false);
	
	if (defecto != null) lista.value=defecto;
}

// Crea una cadena con los valores seleccionados de una select separados por un separador
//	lista:	lista origen
//	separador:  caracter a utilizar como separador entre los valores (por defecto ",")
//	cualificador:	caracter para cualificar los valores
function Get_Selected(lista, separador, cualificador) {
	for(var len=0, aux=""; len < lista.options.length; len++)
		aux=aux + (aux != "" ? (separador == "" ? "," : separador) : "") + cualificador + lista.options[len].value + cualificador;
	
	return aux;
}

/******************************************************************************
** Funciones para la gestión de capas de visualización de cargas de ficheros **
******************************************************************************/
// Funcion para la creación de una capa no visible que sera presentada antes del submit del formulario
// Debe ser llamada después del body de la pagina y recibe como parametro el mapeo a las imagenes
function CrearCapaCarga(mapeo) {
  document.writeln("<div id='cargando' style='position:absolute; left:0px; top:0px; width:102%; height:100%; z-index:1; visibility: hidden'>");
  document.writeln("<table width='100%' height='100%'><tr>");
  document.writeln("<td align='center' valign='middle' background='" + mapeo + "fondo_repeat.gif'>");
  document.writeln("<table border='1' cellspacing='0' cellpadding='0' bordercolordark='#000000' bordercolorlight='#FFFFFF'><tr>");
  document.writeln("<td align='center' valign='middle' bgcolor='#FFFFFF'>");

  document.writeln("<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0' width='267' height='61'>");
  document.writeln("<param name=movie value='" + mapeo + "cargando.swf'>");
  document.writeln("<param name=quality value=high>");
  document.writeln("<embed src='" + mapeo + "cargando.swf' quality=high pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width='267' height='61'>");
  document.writeln("</embed></object>");

  document.writeln("<br><span class='txt12'>&nbsp;&nbsp;&nbsp;Adjuntando documento, espere por favor...&nbsp;&nbsp;&nbsp;<br><br></span>");
  document.writeln("</td></tr></table></td></tr></table></div>");
}

// Funcion que comprueba si hay campos de tipo fichero en el formulario, indicado como parametro, y si estos
// tienen algun valor se asume que hay algun fichero a cargar por lo que visualiza la capa de carga.
function ActivarCapaCarga(formulario) {
 var Campos=document.forms[formulario].elements;

 for (i=0, found=0; i<Campos.length; i++) {
    if (Campos[i].type == "file" && Campos[i].value != "") {
      found=1;
      break;
    }
 }
 
 if (found) {
   if (document.layers)
     document.cargando.visibility="show";
   else
     document.getElementById("cargando").style.visibility="visible";

  document.getElementById("cargando").style.pixelTop=document.body.scrollTop;
 }
}

/******************************************************************************
** Funciones para la gestión de capas de visualización de mensajes de espera **
******************************************************************************/
// Funcion para la creación de una capa no visible
// Debe ser llamada después del body de la pagina y recibe como parametro el mapeo a las imagenes
function CrearCapaMensaje(mensaje, mapeo) {
  document.writeln("<div id='MensajeEspera' style='position:absolute; left:0px; top:0px; width:102%; height:100%; z-index:1; visibility: hidden'>");
  document.writeln("<table width='100%' height='100%'><tr>");
  document.writeln("<td align='center' valign='middle' background='" + mapeo + "fondo_repeat.gif'>");
  document.writeln("<table border='1' cellspacing='0' cellpadding='0' bordercolordark='#000000' bordercolorlight='#FFFFFF'><tr>");
  document.writeln("<td align='center' valign='middle' bgcolor='#FFFFFF'><br>");

  document.writeln("<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0' width='162' height='20'>");
  document.writeln("<param name=movie value='" + mapeo + "barra_mensaje.swf'>");
  document.writeln("<param name=quality value=high>");
  document.writeln("<embed src='" + mapeo + "barra_mensaje.swf' quality=high pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width='162' height='20'>");
  document.writeln("</embed></object>");

  document.writeln("<br><br><span class='txt12'>&nbsp;&nbsp;&nbsp;" + mensaje + "&nbsp;&nbsp;&nbsp;<br><br></span>");
  document.writeln("</td></tr></table></td></tr></table></div>");
}

// Funcion que activa la capa del mensaje
function ActivarCapaMensaje() {
 if (document.layers)
   document.MensajeEspera.visibility="show";
 else
   document.getElementById("MensajeEspera").style.visibility="visible";

  document.getElementById("MensajeEspera").style.pixelTop=document.body.scrollTop;
}

/******************************************************************************
** Funciones para abrir popups de algunos componentes                        **
******************************************************************************/
//Apertura de PopUp para las Notas
function PopUpNotas(dir) {
  new_window(dir,"PopUpNotas",490,370);
}

/******************************************************************************
** Funciones para visualizar u ocultar un bloques                            **
******************************************************************************/

// Si el bloque indicado es visible, se oculta y sino se pone visible
//	Bloque:	Nombre del bloque a tratar
function VerOcultar(Bloque) {

  aux=document.getElementById(Bloque).style;
  if (aux.display == 'none')
    aux.display='block';
  else
    aux.display='none';
}

// Se ocultan todos los bloques de un mismo tipo y se visualiza uno solo. La nomenclatura de asociar varios bloques
// es haciendo que cada nombre sea igual y termine en un numero.
//	Parametro 1:	Nombre por el que comienzan todos los bloques del mismo tipo
//	Parametro 2:  Numero de bloque a visualizar 
//	Parametro 3:	True para indicar que siempre visualize o false para visualizar u ocultar segun su estado actual
function VerUno_OcultarTodo() {
	args = VerUno_OcultarTodo.arguments;
	aux = document.getElementById(args[0] + args[1]).style;
	EstadoAnterior=aux.display;

	// Se ocultan todos los bloques
  for (var i=1; document.getElementById(args[0] + i); i++) document.getElementById(args[0] + i).style.display='none';
  
  if (args.length == 2 || args[2] == true || EstadoAnterior == 'none')
  	aux.display='block'; // Se visualiza el bloque indicado
  else
    aux.display='none';
}

/******************************************************************************
** Funcion para botenes de formulario                                        **
******************************************************************************/

function goLite(FRM,BTN)
{
   window.document.forms[FRM].elements[BTN].style.color = "#000000";
   window.document.forms[FRM].elements[BTN].style.backgroundColor = "#E9F3FF";
}

function goDim(FRM,BTN)
{
   window.document.forms[FRM].elements[BTN].style.color = "#000000";
   window.document.forms[FRM].elements[BTN].style.backgroundColor = "#CEE2FB";
}

// ************* INICIO COMBO ************* 
// datos:  tabla con todos los datos
// filas:  numero de filas de la tabla
// valor:  valor por defecto
// campo:  campo sobre el que hay que actualizar 
// *****************************************
function combo(datos, filas, valor, campo)																											
{																																		
       var indice=valor;					       	
       var tabla = new Array(filas);															
       var orden=1, i, anterior;																					
       if (valor=='') indice=0 ;					       	
       for(i=0;i<filas;i++) tabla[i]=new Array(3);								
       for(i=0;i<filas;i++) {tabla[i][0]=datos[i][0]; tabla[i][1]=datos[i][1]; tabla[i][2]=datos[i][2];       }
 
	// inicializo la lista a blancos
			 campo.options.length = filas +1;
	 	   campo.options[0].value='0';	   			
       campo.options[0].text='Ver Todas' ;   
	     for(i=1;i<=filas;i++) 
	     {					   												
	 	  		 campo.options[i].value='';	   		    
	     		 campo.options[i].text='';      
	     }   	 

	// escribo la lista
  		 campo.selectedIndex=0;          				
	     for(i=0;i<filas;i++) 
	     {				   																	
	 	        if (tabla[i][2]==indice || indice==0)
	      		{		                                                 
							if (anterior!=tabla[i][0])
							{
								anterior=tabla[i][0];
		 	         	campo.options[orden].value=tabla[i][0];                  
		       	   	campo.options[orden].text=tabla[i][1];                     
		            orden=orden+1;							 																			
		          } 
	          }																																		
	     }		
	     campo.options.length = orden;																																									
}	
// ************* FIN COMBO ************* 



/******************************************************************************
** Compara dos fechas                                        **
******************************************************************************/
// Comprueba que la fecha 1 sea menor o igual que la fecha 2
//	parametro 1: 					nombre del formulario
//	parametro 2:					nombre del campo de la fecha 1 
//	parametro 3:					nombre del campo de la fecha 2
function chk_datefromto(frm, fecha1, fecha2) {
	v_fecha1=	document.forms[frm].elements[fecha1];
	v_fecha2=	document.forms[frm].elements[fecha2];

	if (v_fecha1.value != "" && v_fecha2.value != "" && ComparaFechas(v_fecha1, v_fecha2) == 1) {
		MarcaErrorCampo(fecha1, "ERROR. Fecha 'desde' debe ser menor o igual a la fecha 'hasta'", true);
		return false;
	}
	
	return true;
}

function ComparaFechas(obj1, obj2,traza)
{
   fecha1=obj1.value.split("-");
   fecha2=obj2.value.split("-");
   
   c_fecha1=fecha1[2] + (fecha1[1].length == 1 ? "0" : "") + fecha1[1] + (fecha1[0].length == 1 ? "0" : "") + fecha1[0];
   c_fecha2=fecha2[2] + (fecha2[1].length == 1 ? "0" : "") + fecha2[1] + (fecha2[0].length == 1 ? "0" : "") + fecha2[0];
   
   if(traza==1)
   		alert(c_fecha1+"-"+c_fecha2);
   
   if (c_fecha1 == c_fecha2) return 0;
   else if (c_fecha1 > c_fecha2) return 1;
   else return -1;
} 

/******************************************************************************
** Comprueba si se ha pulsado un enter                                       **
******************************************************************************/
function PulsadoEnter() {	return (window.event && window.event.keyCode == 13); }

/******************************************************************************
** Marca un campo para indicar un error. Se realiza un blink del background  **
******************************************************************************/
// Cambia el color de un campo 
function blinkExecute(target,color){
  document.getElementById(target).style.backgroundColor = color;
}

// Crea las funciones de cambio de color de background espaciadas en medio segundo cada una
// Realiza 5 cambios de color en el tiempo.
// target: nombre del campo donde realizar el blinking
// msg: mensaje a presentar
// select: true o false para seleccionar el valor del campo
function MarcaErrorCampo (target, msg, select){
  color1 = "#feff6f"; // blinking color
  color2=document.getElementById(target).style.backgroundColor;
  
  if (msg != null) alert(msg);

	if (document.getElementById(target).type != "hidden") {
		if (document.getElementById(target).type == "select-one") {
			setTimeout('blinkExecute("'+target+'","'+color1+'")',0);
			setTimeout('blinkExecute("'+target+'","'+color2+'")',4500);
		}
		else {
		  setTimeout('blinkExecute("'+target+'","'+color1+'")',0);
		  setTimeout('blinkExecute("'+target+'","'+color2+'")',500);
		  setTimeout('blinkExecute("'+target+'","'+color1+'")',1000);
		  setTimeout('blinkExecute("'+target+'","'+color2+'")',1500);             
		  setTimeout('blinkExecute("'+target+'","'+color1+'")',2000);
		  setTimeout('blinkExecute("'+target+'","'+color2+'")',2500);     
		  setTimeout('blinkExecute("'+target+'","'+color1+'")',3000);
		  setTimeout('blinkExecute("'+target+'","'+color2+'")',3500);     
		  setTimeout('blinkExecute("'+target+'","'+color1+'")',4000);
		  setTimeout('blinkExecute("'+target+'","'+color2+'")',4500);             
		}
	  
	  document.getElementById(target).focus();
	  
	  if (select) document.getElementById(target).select();
	}
}

/******************************************************************************
** Funciones para alterar el contenido de un elemento                        **
******************************************************************************/
// Cambia el contenido de un elemento
function ChangeText(id, NewValue) {
	document.getElementById(id).innerHTML = NewValue;
}

/******************************************************************************
** Funcion para mostrar una capa                                             **
******************************************************************************/
function mostrar(nombreCapa)
{ 
	var Capa = document.getElementById(nombreCapa).style;
	if (Capa.display == 'none')
			Capa.display='block';
} 

/******************************************************************************
** Funcion para ocultar    una capa                                          **
******************************************************************************/
function ocultar(nombreCapa)
{ 
	var Capa = document.getElementById(nombreCapa).style;
	if (Capa.display == 'block')
			Capa.display='none';

} 

/******************************************************************************
** Funcion para reemplazar todas las ocurrencias de una variable             **
******************************************************************************/
function replaceAll( str, from, to ) 
{
    var idx = str.indexOf( from );
    while ( idx > -1 ) {
        str = str.replace( from, to ); 
        idx = str.indexOf( from );
    }
    return str;
}


/******************************************************************************
** Funcion para escanear un documento                                      **
******************************************************************************/
function Escanear_DOC(cod_primaria,cod_licencia,codigo_tipo_fichero,codigo_barras,chequea_codigo_barras,pos, cod_certificado)
{ 
 	if(pos==undefined)	pos=0;
 	var dir = '../NFG_NuevaCaptura?cod_primaria='+cod_primaria+'&cod_licencia='+cod_licencia+'&codigo_tipo_fichero='+codigo_tipo_fichero+'&codigo_barras='+codigo_barras+'&validacion=1&chequea_codigo_barras='+chequea_codigo_barras+'&pos='+pos+'&cod_certificado='+cod_certificado;
  new_window(dir,'Escaner',660,600);
}  

/******************************************************************************
** Funciones de radio con prefijo R_ para evitar duplicidades de páginas ya existentes          **
******************************************************************************/


function R_GetRadio(r)
{
  if(r==null || r.length==undefined) return 0;
  
  for (var i=0; i<r.length; i++)
  {
    if (r[i].checked)
      return r[i].value;
  }
  return 0;
}

function R_BuscarEnRadio(r,valor)
{
  if(r==null || r.length==undefined) return false;
  for (var i=0; i<r.length; i++)
  {
    if (r[i].value == valor)
      return true;
  }
  return false;
}

function R_LimpiarRadio(r)
{
  if(r==null || r.length==undefined) return 0;
  for (var i=0; i<r.length; i++)
  {
    if (r[i].checked)
      r[i].checked=false;
  }
  return 0;
}

function R_CambiarRadio(r,valor)
{
  if(r==null || r.length==undefined) return 0;
  for (var i=0; i<r.length; i++)
  {
    if (r[i].value == valor)
      r[i].checked=true;
  }
  return 0;
}



//-->
