/****************************************************/
function trim(cadena){
	if (cadena==undefined)
		return cadena;
	var ini=0;
	while (esBlanco(cadena.charAt(ini)) && ini<cadena.length)
		ini++;

	var fin = cadena.length-1;
	while (esBlanco(cadena.charAt(fin)) && fin>=ini)
		fin--;

	return cadena.substring(ini,fin+1);
}//trim


function Confirmarurl(accion,ruta,ventana){
	if (!confirm('¿'+accion+'?'))
		return false;
	
	self.document.location.href=ruta;
}//mensaje confirmacion


/****************************************************/
function obtenerFecha(fech /*, hora*/){
	var f = fech.split("/");
	var hora = "";
	if (arguments.length>1)
		 hora = " "+arguments[1];
	
	return new Date(f[1]+"/"+f[0]+"/"+f[2]+hora);
}//obtenerFecha
/****************************************************/
function buscarElementoArray(elemento, vector){
	for (var i=0; i<vector.length; i++){
		if (elemento==vector[i]){
			return i;
		}
	}//for
	return -1;
}//buscarElementoArray
/*********************************************************************/
function getElement(elemento){
	if (typeof(elemento)=="string"){

		return document.getElementById(elemento);
	}else{
		return elemento;
	}
}//getElement
/*********************************************************************/
//borra todos los campos del formulario excepto los que se indiquen 
function limpiarFormulario(silencio, formu/*, excepto1, excepto2*/){
	if (!silencio)
	if (!confirm("Se van a limpiar los campos del formulario. ¿Está seguro?"))
		return false;
	
	formu = getElement(formu);
	
	var elemento;
	for (var i=0;i<formu.elements.length;i++){
		elemento = formu.elements[i];
		if (buscarElementoArray(elemento.name, arguments)<0){ //si no es una excepcion
			switch(elemento.type){
			case "button":
			case "reset":
			case "submit":
				break;
			case "select-one":
				elemento.selectedIndex = 0;
				break;
			default:
				elemento.value = "";
			}//switch
		}//if
	}//for
	return true;
}//limpiarFormulario
/****************************************************/
function hayCambiosFormulario(formu){
	var elemento;
	for (var i=0; i<formu.elements.length; i++){
		elemento = formu.elements[i];
		if (elemento.value != elemento.defaultValue && elemento.type!="select-one")
			return true;
	}//for
	return false;
}
/****************************************************/
function esCampoVacio(campo/*puntero al campo del formulario*/){
	
	if (campo==null || campo==undefined || !campo.form)
		return true; //es vacio porque no existe o no es un campo de formulario
		
	var valor="";
	
	if (campo.type=="select-one"){
		
		valor = campo.options[campo.selectedIndex].value;
		
		/*¡OJO!*/	if (valor=="0") return true;
	} else {
		valor = campo.value;
	}//else

	return esCadenaVacia(valor);
}//esCampoVacio
/*********************************************************************/
function parseReal(numero){
	return parseFloat(new String(numero).replace(",","."));
}//parseReal
/*********************************************************************/
function formatReal(numero, separador){
	var n = parseReal(numero);
	if (n==NaN)
		return n;
	
	numero = new String(n).replace(".",separador)

	return numero;
}//formatReal
/*********************************************************************/
//Dado un grupo de radiobutton devuelve el valor del radiobutton seleccionado
function getRadioButtonValue(radioButton){
	var i;
	for (i=0; i<radioButton.length; i++){
		if (radioButton[i].checked == true){
			return radioButton[i].value;
		}//if
	}//for
	return "";
}//getRadioButtonValue
/*********************************************************************/
//devuelve el valor actual del campo referenciado por idcampo
function getValorCampo(idcampo){
	var campo = document.getElementById(idcampo);
	
	if (!campo)
		return null;
		
	switch(campo.type){
	case "select-one":
		return campo.options[campo.selectedIndex].value;
	case "radio":
		return getRadioButtonValue(campo.form.elements[idcampo]);
	default:
		return campo.value;
	}//switch
	
	return null;
}//getValorCampo
/*********************************************************************/
//devuelve el texto de la opcion seleccionada en un campo select
function getTextoSeleccionado(idselect){
	var campo = document.getElementById(idselect);
	
	if (!campo || campo.type!="select-one")
		return "";
	
	return campo.options[campo.selectedIndex].text;
}//getTextoSeleccionado
/*********************************************************************/
//envia todos los formularios de la pagina
function submitFormularios(){
	var i;
	for (i=0;i<document.forms.length;i++)
		document.forms[i].submit();
}//submitFormularios
/********************************************************************/
//cambia el color del elemento "nombre"
function changeColor(nombre, color){
	if (document.getElementById(nombre) && document.getElementById(nombre).style)
		document.getElementById(nombre).style.color=color;
}//changeColor
/********************************************************************/
function modificarFrameSetRows(rows){
	top.frameset.rows = rows;
}//modificarFrameSetRows
/*******************************************************************/
function obtenerExtension(sArchivo){
	var i = sArchivo.lastIndexOf('.');
	return sArchivo.substring(i+1,sArchivo.length);
}//obtenerExtension
/*******************************************************************/
CCampo.prototype.esCampoVacio = esCampoVacio;
CCampo.prototype.trim = trim;
function CCampo(id, nombre, formu, requerido, funcion){
	this.id=id; //id del campo
	this.nombre=nombre; //nombre del campo (para mostrar al usuario)
	this.requerido=requerido;
	if (typeof(formu)=="string")
		this.formu = document.getElementById(formu);
	else{
		this.formu = formu;
	}
	this.campo = document.getElementById(this.id);
	
	if (funcion==undefined) //tipo del campo (para comprobacion de tipos (por hacer...))
		this.funcion = null; //no hacer comprobacion de tipos
	else
		this.funcion = funcion;
	
	this.setFoco = function(){
		return this.campo.focus(); 
	}
	
	this.validar = function(){
	
		var aux;

		this.campo.value = this.trim(this.campo.value);
		
		//compracion de si se ha rellenado
		if (this.esCampoVacio(this.campo)){
			if (this.requerido){
				alert('No se ha rellenado el campo "'+this.nombre+'".');
				return false;
			} 
		}//if
		else
		//comprobacion de tipos
		if (this.funcion!=null && !this.funcion(this.campo.value)){
			alert('El valor del campo "'+this.nombre+'" no es correcto.');
			return false;
		}//if this.funcion...
		
		return true;
	}//Validar
}//CCampo
//valida un vector de elementos CCampo
function validarCampos(campos){
	for (i=0; i<campos.length; i++){
		if (!campos[i].validar()){
			campos[i].setFoco();
			return false;
		}//if
	}//for
	return true;
}//validarCampos
/*********************************************************************/
function hayCamposRellenos(/*id campo1, id campo2 ... id campo n*/){
	for (var i=0; i<arguments.length; i++)
		if (!esCadenaVacia(document.getElementById(arguments[i]).value))
			return true;
	return false;
}//hayCamposRellenos
/*********************************************************************/
function hayCamposVacios(/*id campo1, id campo2 ... id campo n*/){
	for (var i=0; i<arguments.length; i++)
		if (esCadenaVacia(document.getElementbyId(arguments[i]).value))
			return true;
	return false;
}//hayCamposVacios
/*********************************************************************/
function printSelectValores(idselect, seleccionado, onchange ){
	var ini = 3;
	var fin = arguments.length;
	
	document.writeln('<select name="'+idselect+'" id="'+idselect+'" onchange="'+onchange+'">');
	for (var i=ini; i<fin; i++){
		document.write('<option value="'+arguments[i]+'" ');
		if (arguments[i]==seleccionado)
			document.write(' selected="selected" ');
		document.writeln('> '+arguments[i]+'</option>');
	}//for
	document.writeln('</select>');
}//printSelectValores
/*********************************************************************/
function printSelectRango(idselect, seleccionado, onchange, desde, hasta, incremento){
	document.writeln('<select name="'+idselect+'" id="'+idselect+'" onchange="'+onchange+'">');
	//document.write('<option value="0" ');
	//document.writeln('>0</option>');
	for (var i=desde; i<=hasta; i+=incremento){
		document.write('<option value="'+i+'" ');
		if (i==seleccionado)
			document.write(' selected="selected" ');
		document.writeln('> '+i+'</option>');
	}//for
	if (idselect=="cuantos") {
		document.write('<option value="'+hasta+'" ');
		document.writeln('> '+hasta+'</option>');
		document.writeln('</select>');
	}
}//printSelectRango
/**********************************************************************/
function eliminarDelListado(formu, ident){
	if (!confirm('¿Eliminar registro?'))
		return false;
	formu = getElement("formu");
	formu.elements.ident.value = ident;
	formu.elements.accion.value = "eliminar";
	formu.submit();
}//eliminarAsegurado
/**********************************************************************/
//para el paginador. debe existir un objeto select con numeros (que indiquen desde donde empezar a mostrar resultados)
function verSiguiente(formu){
	sel = formu.elements.desde;
	if (sel.selectedIndex<sel.options.length-1){
		sel.selectedIndex++;
		formu.submit();
	}//if
}//verSiguiente

function mensaje(url_destino,titulo,ancho,alto,derecha,izquierda){
var ventanaCalendario=false
var direccion=url_destino;
var titular=titulo+'';
ventanaCalendario = window.open(direccion,'',"width="+ancho+",height="+alto+",left="+derecha+",top="+izquierda+",scrollbars=0,menubar=0,titlebar=0,statusbar=0,status=0,resizable=0,location=0"+"'")    
}
/***********************************************************************/

function openwindow(url_destino,titulo,ancho,alto,derecha,izquierda){
var ventanaCalendario=false
//var param="'"+ancho+","+alto+","+derecha+","+izquierda+",scrollbars=no,menubars=no,statusbar=no,status=no,resizable=no,location=no'";
//alert (param) ;	

var direccion=url_destino;
var titular=titulo+'';
//alert( '"'+ direccion +'","'+titular+'","'+"width="+ancho+",height="+alto+",left="+derecha+",top="+izquierda+",scrollbars=1,menubar=1,titlebar=0,statusbar=0,status=0,resizable=0,location=0"+'"');
//ventanaCalendario = window.open( '"'+ direccion +'","'+titular+'","'+"width="+ancho+",height="+alto+",left="+derecha+",top="+izquierda+",scrollbars=1,menubar=1,titlebar=0,statusbar=0,status=0,resizable=0,location=0"+'"');
ventanaCalendario = window.open(direccion,'',"width="+ancho+",height="+alto+",left="+derecha+",top="+izquierda+",scrollbars=1,menubar=1,titlebar=0,statusbar=0,status=0,resizable=0,location=0"+"'")
//window.open(direccion,titulo,'param');
    
}
/***********************************************************************/
function verAnterior(formu){
	sel = formu.elements.desde;
	if (sel.selectedIndex>0){
		sel.selectedIndex--;
		formu.submit();
	}//if
}//verAnterior

function PonPuntos(obj, sign, imaxlength)
{
var snumero; 
snumero = obj.value + 1; 
 
if (snumero.length > imaxlength)
	{  
	event.returnValue = false;
     	return false;  
   	}
if (isEmpty(obj.value) && (event.keyCode == 48)) 
  	{
     	Event.returnValue = false;
}
else
	if (sign == "positive")
  		{	
    		if ((event.keyCode < 48) || (event.keyCode > 57))
    			{
      			event.returnValue = false;
    			}
    		else 
    			{
      			var s_pre = obj.value;
      			var s_inter = "";
      			var s_post = "";
      			var single_character = "";
      			var poner_punto = 1;
      			for (var i = 0; i < s_pre.length; i++)
      				{
        			single_character = s_pre.substring(i,i+1);
        			if (single_character != ":")
        				{
          				s_inter = s_inter + single_character;
        				}
      				}
			s_inter = s_inter + String.fromCharCode(event.keyCode);
			for (var j = s_inter.length; j > 0; j--, poner_punto++)
      				{
        			s_post = s_inter.substring(j-1,j) + s_post;
        			if ((poner_punto == 2) && (j != 1))
        				{
          				poner_punto = 0;
          				s_post = ":" + s_post;
        				}
      				}
      			obj.value = s_post;
      			event.returnValue = false;
    			}
  		}
  	

	}
//-------------------------------------------------	
//-------------------------------------------------	
function isEmpty(s){return ((s == null) || (s.length == 0))}
//-------------------------------------------------