/* Abre ventana nueva. Los par?metros target y features son opcionales.
Si no se especifican cogen los establecidos por defecto.
En caso de querer otros diferentes a los de _POPUP_FEATURES, se espcificar?an al llamar a la funci?n */

function isUndefined(v) {
    var undef;
    return v===undef;
}

var _POPUP_FEATURES = 'location=0,statusbar=no,scrollbars=yes,fullscreen=yes,menubar=no,resizable =yes,width=760,height=450';

function popup(url, target, features) {
    if (isUndefined(features)) features = _POPUP_FEATURES;
    if (isUndefined(target  )) target = '_blank';
    var theWindow = window.open(url, target, features);
    theWindow.focus();
    return theWindow;
}

function romperFrames() {
	if (self!=top) 
    	top.location.href=self.location.href; 
} 

function abrirVentana(url){
window.open(url,'_blank','location=0,toolbar = yes,resizable =yes,statusbar=no,scrollbars=no,menubar=yes,fullscreen=yes');
}


function toggleGIF(obj, imagen, txt) {
 mHTML = "<div class='floatright'>";
 mHTML += "Espere por favor";
 mHTML += "<img src='"+ imagen + "' alt='lapoliza.com' /></div>";
 mHTML +="<div class='spacer'>&nbsp;</div>";
 document.getElementById(obj).innerHTML = mHTML;
}


/* Funciones para mostrar y ocultar capas de los formularios */
function hideDivs() {
	for(i=0; i<arguments.length; i++)
		ocultar(arguments[i]);
}

function mostrar(nombreCapa)	{ 

	document.getElementById(nombreCapa).style.display='block'; 
} 

function ocultar(nombreCapa)	{ 

	document.getElementById(nombreCapa).style.display='none'; 

} 

function mostrar2(nombreCapa)	{ 
	parent.pagina.document.getElementById(nombreCapa).style.display='block'; 
} 


function ocultar2(nombreCapa)	{ 
	parent.pagina.document.getElementById(nombreCapa).style.display='none'; 
} 

function getId (id) {
	return document.getElementById(id)
}
function Redondear (numero, decimales) {
  numero = numero - 0;
  
  if (decimales == 0) {
  	p = numero.substring(0, numero.indexOf('.'));
  	dec1 = numero.substring(  numero.indexOf('.') , numero.indexOf('.')+1 );
  	ndec1 = number (dec1);
  	ndec = number ( p )
  	if (ndec1>4) ndec = ndec +1
  	return ndec;
  }
  
  decimales = decimales || 2;
  var f = Math.pow(10, decimales);
  numero = Math.round(numero * f) / f;
  numero += Math.pow(10, - (decimales + 1));
  numero += '';
  return decimales== 0 ? n.substring(0, numero.indexOf('.')) :
      numero.substring(0, numero.indexOf('.') + decimales + 1);
}


function radioSiNo (x,elemento){
if(x.value=='si')
	mostrar(elemento)
	else	
    ocultar(elemento)
}

function radioSiNohogar (x,elemento) {
	if(x.value=='si' ){
	
		mostrar(elemento)
	}else{
		ocultar(elemento)
	}
	if(elemento=='divalarma'){
		document.getElementById('alarma1no').checked=false;
		document.getElementById('alarma2no').checked=false;
		document.getElementById('alarma3no').checked=false;
		document.getElementById('alarma1si').checked=false;
		document.getElementById('alarma2si').checked=false;
		document.getElementById('alarma3si').checked=false;
		document.getElementById('alarma1no').disabled=false;
		document.getElementById('alarma2no').disabled=false;
		document.getElementById('alarma3no').disabled=false;
		document.getElementById('alarma1si').disabled=false;
		document.getElementById('alarma2si').disabled=false;
		document.getElementById('alarma3si').disabled=false;
		}
	if(elemento=='divrejas'){
		document.getElementById('rejas1si').checked=false;
		document.getElementById('rejas1no').checked=false;
		document.getElementById('rejas2si').checked=false;
		document.getElementById('rejas2no').checked=false;
		document.getElementById('rejas1si').disabled=false;
		document.getElementById('rejas1no').disabled=false;
		document.getElementById('rejas2si').disabled=false;
		document.getElementById('rejas2no').disabled=false;
	}
	if(elemento=='divcaja'){
		document.getElementById('caja1si').checked=false;
		document.getElementById('caja1no').checked=false;
		document.getElementById('caja1si').disabled=false;
		document.getElementById('caja1no').disabled=false;
		if(x.value=='si')
			document.datoshogarform.joyascaja.disabled=false;
		else
			document.datoshogarform.joyascaja.disabled=true;
	}		


}

function radioNoSi (x,elemento) {
	if(x.value=='no')
		mostrar(elemento)
	else
		ocultar(elemento)
}

/* para Hogar */
function hogarNif (x) {
	if(getId(x).selectedIndex==0 ||getId(x).selectedIndex==2)
		ocultar('divrs');
	else
		mostrar('divrs');
}

function bloqueaRadio (x,elemento) {
	  if(x.value=='si') {
	    getId(elemento+'no').checked=true;
		getId(elemento+'si').disabled=true;
		getId(elemento+'no').disabled=true;
	} else {
		getId(elemento+'si').disabled=false;
		getId(elemento+'no').disabled=false;
	}
}

/* para Coche */
function cocheNuevo (x) {
	var Hoy = new Date();
	
	if(getId(x).selectedIndex==0 || getId(x).selectedIndex==1) {
		document.getElementById('fcompra').value='';
		document.getElementById('fcompra1').value='';
		document.getElementById('fcompra2').value='';
		document.getElementById('fmatric').value='';
		document.getElementById('fmatric1').value='';
		document.getElementById('fmatric2').value='';
		mostrar('divseguro');
		ocultar('divotroseg');
	
	} else {
		if(getId(x).selectedIndex==2) {
			document.getElementById('fmatric').value=Hoy.getDate();
			document.getElementById('fmatric1').value=Hoy.getMonth()+1;
			document.getElementById('fmatric2').value=Hoy.getFullYear();
		} else {
			document.getElementById('fmatric').value='';
			document.getElementById('fmatric1').value='';
			document.getElementById('fmatric2').value='';
		}
		document.getElementById('fcompra').value=Hoy.getDate();
		document.getElementById('fcompra1').value=Hoy.getMonth()+1;
		document.getElementById('fcompra2').value=Hoy.getFullYear();
		ocultar('divseguro');
		mostrar('divotroseg');
	}
	
	ocultar('divSegAnt');
	ocultar('divSinies');
	
}

function vacio(txt){
	var v = false;
	if ( txt == null ) v =  true;
	else if ( trim(txt).length == 0 ) v = true;
	return v;
}

function cocheAsegurado(x) {
	if(x.value=='si') {
		mostrar('divSegAnt')
		ocultar('divotroseg')
	} else {
		ocultar('divSegAnt')
		mostrar('divotroseg')
	}
	ocultar('divSinies')
}

function otroSeguro(x) {
	if(x.value=='si')
		mostrar('divSegAnt')
	else
		ocultar('divSegAnt')
	ocultar('divSinies')
}

function cocheNifCif (x) {
	if(getId(x).selectedIndex==0) {
		mostrar('divTomNif');
		ocultar('divTomCif');
		mostrar('divCondNif');
		ocultar('divCondTit');
	} else {
		ocultar('divTomNif');
		mostrar('divTomCif');
		ocultar('divCondNif');
		mostrar('divCondTit');
	}
}


function cocheNifCif1 (x,y) {
	if(getId(x).selectedIndex==0) {
		mostrar('divTomNif');
		ocultar('divTomCif');
	} else {
		ocultar('divTomNif');
		mostrar('divTomCif');
	}
	y.value="";
}

function conductorPrincipal (x) {
	if(getId(x).selectedIndex==0 || getId(x).selectedIndex==1)
		ocultar('divCondTit');
	else
		mostrar('divCondTit');
}

function condProp(x) {
	if(x.value=='si') {
		ocultar('divProp')
		ocultar('divPropTit')
	} else
		mostrar('divProp')
}

function propietario (x,y) {
	if(getId(x).selectedIndex==0 || getId(x).selectedIndex==1)
		ocultar('divPropTit');
	else if(getId(x).selectedIndex==5){
		mostrar('divPropTit');
		ocultar('divPropNif');
		mostrar('divPropCif');
		ocultar('divPropNie');
		y[1].selected=true;
	}else{
		mostrar('divPropTit');
		mostrar('divPropNif');
		ocultar('divPropCif');
		ocultar('divPropNie');
		y[0].selected=true;
	}
}
/* Para Moto */

function motoNifCif (x,y) {
	if(getId(x).selectedIndex==0) {
		mostrar('divTomNif');
		ocultar('divTomNie');
		ocultar('divTomCif');
		ocultar('divCondTit');
	} else if(getId(x).selectedIndex==1){
		ocultar('divTomNif');
		ocultar('divTomNie');
		mostrar('divTomCif');
		mostrar('divCondTit');
		ocultar('divCondRel');
	}else{
		mostrar('divTomNif');
		mostrar('divTomNie');
		ocultar('divTomCif');
		ocultar('divCondTit');
	
	}
	y.value="";
}

function motoNifCif2 (x,y,z) {
	if(getId(x).selectedIndex==0) {
		mostrar('divPropNif');
		ocultar('divPropCif');
		ocultar('divPropNie');
	}  else if(getId(x).selectedIndex==1){
		ocultar('divPropNif');
		mostrar('divPropCif');
		ocultar('divPropNie');
		y[5].selected=true;
	}else{
		mostrar('divPropNif');
		ocultar('divPropCif');
		mostrar('divPropNie');
	}
	z.value="";
}

function motoNifCif3 (x,y) {
	if(getId(x).selectedIndex==0) {
		ocultar('divCondNie');
	}  else{
		mostrar('divCondNie');
	}
	y.value="";

}

function motoNueva (x) {
	var Hoy = new Date();
	
	if(getId(x).selectedIndex==0 || getId(x).selectedIndex==1) {
		document.getElementById('fcompra').value='';
		document.getElementById('fcompra1').value='';
		document.getElementById('fcompra2').value='';
		document.getElementById('fmatric').value='';
		document.getElementById('fmatric1').value='';
		document.getElementById('fmatric2').value='';
	} else {
		if(getId(x).selectedIndex==2) {
			document.getElementById('fmatric').value=Hoy.getDate();
			document.getElementById('fmatric1').value=Hoy.getMonth()+1;
			document.getElementById('fmatric2').value=Hoy.getFullYear();
		} else {
			document.getElementById('fmatric').value='';
			document.getElementById('fmatric1').value='';
			document.getElementById('fmatric2').value='';
		}
		document.getElementById('fcompra').value=Hoy.getDate();
		document.getElementById('fcompra1').value=Hoy.getMonth()+1;
		document.getElementById('fcompra2').value=Hoy.getFullYear();
	}
}

function seguroAnterior(x){
	if(x.value=='si') {
		mostrar('divSegAnt');
		ocultar('otroSeguro');
		ocultar('divOtroSeg');
	} else {
		ocultar('divSegAnt');
		mostrar('otroSeguro');
	}
	document.getElementById('otroSegSi').checked = false;
	document.getElementById('otroSegNo').checked = false;
}

/* Para Veh?culo Cl?sico */

function otroMarca (x) {
	if(getId(x).selectedIndex==4)
		mostrar('divOtros');
	else
		ocultar('divOtros');
}
function cilindradaMoto (x) {
	if(getId(x).selectedIndex==0 || getId(x).selectedIndex==1)
		ocultar('divCilindrada');
	else
		mostrar('divCilindrada');
}


/* Para Caza */

function cazaAseg (x) {

	if(getId(x).selectedIndex==0 || getId(x).selectedIndex==1)
		ocultar('divAsegCaza');
	else
		mostrar('divAsegCaza');
}

/*
  Funci?n encargada de mostrar el nombre del banco en funci?n del c?digo de entidad que haya escogido el cliente
*/
function putBanco(origen, destino){
  
	existe = false
	for(j=0;j<codBancos.length;j++){
		if(codBancos[j]==parseInt(origen.value,'10')){
			destino.value=nomBancos[j];
			existe = true;
			break;
		}
		if(!existe){destino.value="";}
	}
}

/*
para multas
*/
function multasNif (x) {
	if(getId(x).selectedIndex==0 || getId(x).selectedIndex==2){
		ocultar('divrs');
		mostrar('divTomNif');
	}else{
		mostrar('divrs');
		ocultar('divTomNif');
	}
}

/*
para puntos
*/
function puntosNif (x) {
	if(getId(x).selectedIndex==0 || getId(x).selectedIndex==2){
		ocultar('divrs');
		mostrar('divTomNif');
	}else{
		mostrar('divrs');
		ocultar('divTomNif');
	}
}


/******************************************************************************
*
*   COMPRUEBA LAS COMAS Y LOS PUNTOS DE LOS NUMEROS DECIMALES
*
******************************************************************************/




function comprobarDecimal(campo){


try{
	if(isNaN(campo.value))
		campo.value="0.00";
	else{
		valor=campo.value;
	
		while(valor.indexOf(',',0)>0){
		  valor	= valor.replace(',','.'); 
		}
		var valor1 = valor.substring(0,valor.lastIndexOf('.',valor.length)); 
		while(valor1.indexOf('.',0)>0){
		  valor1 = valor1.replace('.',''); 
		}
		var valor2 = valor.substring(valor.lastIndexOf('.',valor.length),valor.length); 
		valor = valor1+valor2;
		if(valor2.length==4){
			 valor2 = valor2.replace('.',''); 
		     valor = valor1+valor2;
		}
	
		campo.value=valor;
	}
}catch(exception) {}

}

/*
Funci?n que dadas dos fechas devuelve la diferencia en a?os entre una fecha y
otra
*/

function diferenciaFechas(fecha_nacim,fecha_carnet){

    //calculo la fecha que recibo 
    //La descompongo en un array 
    var array_fecha = fecha_nacim.split("/") 
    //si el array no tiene tres partes, la fecha es incorrecta 
    if (array_fecha.length!=3) 
       return false 
    //compruebo que los ano, mes, dia son correctos 
    var anoNac 
    anoNac = parseInt(array_fecha[2]); 
 
    if (isNaN(anoNac)) 
       return false 

    var mesNac 
    mesNac = parseInt(array_fecha[1]); 

    if (isNaN(mesNac)) 
       return false 

	if(array_fecha[1] == "08")
		mesNac=8;
	if(array_fecha[1] == "09")
		mesNac=9;

    var diaNac 
    diaNac = parseInt(array_fecha[0]); 
    if (isNaN(diaNac)) 
       return false 


    //si el a?o de la fecha que recibo solo tiene 2 cifras hay que cambiarlo a 4 
    if (anoNac<=99) 
       anoNac +=1900 

    array_fecha = fecha_carnet.split("/") 
    //si el array no tiene tres partes, la fecha es incorrecta 
    if (array_fecha.length!=3) 
       return false 

    //compruebo que los ano, mes, dia son correctos 
    var anoNac 
    anoCar = parseInt(array_fecha[2]); 
    if (isNaN(anoCar)) 
       return false 

    var mesCar 
    mesCar = parseInt(array_fecha[1]); 

    if (isNaN(mesCar)) 
       return false 
       
	if(array_fecha[1] == "08")
		mesCar=8;
	if(array_fecha[1] == "09")
		mesCar=9;

    var diaCar
    diaCar = parseInt(array_fecha[0]); 
    if (isNaN(diaCar)) 
       return false 


    //si el a?o de la fecha que recibo solo tiene 2 cifras hay que cambiarlo a 4 
    if (anoCar<=99) 
       anoCar +=1900 


    //resto los a?os de las dos fechas 
    edad=anoCar-anoNac - 1; //-1 porque no se si ha cumplido a?os ya este a?o 

    //si resto los meses y me da menor que 0 entonces no ha cumplido a?os. Si da mayor si ha cumplido 
    if (mesCar - mesNac < 0) //+ 1 porque los meses empiezan en 0 
       return edad 
    if (mesCar - mesNac > 0) 
       return edad+1 

    //entonces es que eran iguales. miro los dias 
    //si resto los dias y me da menor que 0 entonces no ha cumplido a?os. Si da mayor o igual si ha cumplido 
    if (diaCar - diaNac >= 0) 
       return edad + 1 

    return edad 
    
   
}      
/*
Funci?n que dada una fecha devuelve la diferencia en a?os entre la fecha
actual y la fecha dada
*/
function calcular_edad(fecha){ 

    //calculo la fecha de hoy 
    hoy=new Date() 
    //alert(hoy) 
    //calculo la fecha que recibo 
    //La descompongo en un array 
    var array_fecha = fecha.split("/") 
    //si el array no tiene tres partes, la fecha es incorrecta 
    if (array_fecha.length!=3) 
       return false 
    //compruebo que los ano, mes, dia son correctos 
    var ano 
    ano = parseInt(array_fecha[2]); 
    if (isNaN(ano)) 
       return false 
    var mes 
    mes = parseInt(array_fecha[1]); 

    if (isNaN(mes)) 
       return false 
	if(array_fecha[1] == "08")
		mes=8;
	if(array_fecha[1] == "09")
		mes=9;

    var dia 
    dia = parseInt(array_fecha[0]); 
    if (isNaN(dia)) 
       return false 
    //si el a?o de la fecha que recibo solo tiene 2 cifras hay que cambiarlo a 4 
    if (ano<=99) 
       ano +=1900 

    //resto los a?os de las dos fechas 
    edad=hoy.getYear()- ano - 1; //-1 porque no se si ha cumplido a?os ya este a?o 

    //si resto los meses y me da menor que 0 entonces no ha cumplido a?os. Si da mayor si ha cumplido 
    if (hoy.getMonth() + 1 - mes < 0) //+ 1 porque los meses empiezan en 0 
       return edad 
    if (hoy.getMonth() + 1 - mes > 0) 
       return edad+1 
    //entonces es que eran iguales. miro los dias 
    //si resto los dias y me da menor que 0 entonces no ha cumplido a?os. Si da mayor o igual si ha cumplido 
    if (hoy.getUTCDate() - dia >= 0) 
       return edad + 1 
    return edad 
} 
