﻿String.prototype.trim = function(){ return this.replace(/^s+|s+$/g,'') }

// Esta funcion quita los espacios de los campos tanto de detras como de delante
function Sin_espacios(v){
  for (j=0; j<v.length; j++){
  	if (v.charAt(j)!=" "){v=v.substring(j,v.length);break }
  	if(j==v.length-1){v="";return v
  }    
                               }
  for (j=v.length-1;j>=0; j--){
  	if (v.charAt(j)!=" "){v=v.substring(0,j+1);break }
  }
  return v    
}


function validarCadena(cadena,formulario,msj,name){
	valor =eval('document.'+formulario+'.'+cadena+'.value');
	nombre = eval('document.'+formulario+'.'+cadena);
	valor = Sin_espacios(valor);
	eval('document.'+formulario+'.'+cadena+'.value="'+valor+'"');
	if(valor==''){
		msjVacio(msj+name);
		nombre.focus();
		return false;	
	}
	return true;
}

function validarClave(clave1, clave2, formulario,msj){
	claveA=eval('document.'+formulario+'.'+clave1);
	claveB=eval('document.'+formulario+'.'+clave2);
	
	if(claveA.value!=claveB.value){
		alert(msj);
		claveA.value="";
		claveB.value="";
		claveA.focus();
		return false;
	}
	return true;	
}		

function validoCombo(cadena,formulario){
	nombre = eval('document.'+formulario+'.'+cadena);
    if(nombre.selectedIndex==-1){
    	msjVacio(cadena);
    	nombre.focus();
		return false;		
    }	
    return true;		
}

function validoCombo2(cadena,formulario){
	nombre = eval('document.'+formulario+'.'+cadena);
    if(nombre.selectedIndex==0 ){
    	msjVacio(cadena);
    	nombre.focus();
		return false;		
    }	
    return true;		
}

function validarMailSorteo(cadena,formulario){
	 valor= eval('document.'+formulario+'.'+cadena+'.value');
	 nombre = eval('document.'+formulario+'.'+cadena);
	 pos_A= valor.indexOf("@")
	 pos_P= valor.indexOf(".")
	 if (cadena.length>0){
		if(pos_A==-1 || pos_A==0|| pos_A==valor.length-1){ 
			alert("E-mail no válido");
			nombre.focus();
			return false;
		} 
	}	
}		

function validarMail(cadena,formulario,msj){
	 valor= eval('document.'+formulario+'.'+cadena+'.value');
	 nombre = eval('document.'+formulario+'.'+cadena);
	 pos_A= valor.indexOf("@")
	 pos_P= valor.indexOf(".")
	if(pos_A==-1 || pos_A==0 || pos_A==valor.length-1){ 
		alert(msj);
		nombre.focus();
		return false;
	} 
	
	if(pos_P==-1 || pos_P==0 || pos_P==valor.length-1){
		alert(msj);
		return false;
		nombre.focus();
	}
	return true;
}

function validoFecha(dia,mes,anio,formulario,msj,name){
	cadDia = eval('document.'+formulario+'.'+dia);
	cadMes = eval('document.'+formulario+'.'+mes);
	cadAnio = eval('document.'+formulario+'.'+anio);
    if(cadDia.selectedIndex==0 ||cadMes.selectedIndex==0 || cadAnio.selectedIndex==0){
    	msjVacio(msj+name);
    	if(cadDia.selectedIndex==0) cadDia.focus();
    	else if(cadMes.selectedIndex==0) cadMes.focus();
    	else cadAnio.focus();
		return false;		
    }	
    return true;		
}

function comparar_intervalo_fechas(dia1,mes1,dia2,mes2,msj){
	
	var dia1 =dia1;
	var mes1 =mes1;
	
	var dia2 =dia2;
	var mes2 =mes2;
			
	dia1 = parseInt(dia1,10);
	mes1 = parseInt(mes1,10);
	
	dia2 = parseInt(dia2,10);
	mes2 = parseInt(mes2,10);
	
	if (dia1>31 || dia2>31){
		alert("El día no puede ser superior a 31");
		return false;
	}	
	
	if (mes1>12 || mes2>12){
		alert("El mes no puede ser superior a 12");
		return false;
	}
	
	if ((mes1==mes2)){
		if ((dia1>dia2)){
			msjVacio(msj);
			return false;
		}
	}
			
	if ((mes1>mes2)){
		msjVacio(msj);
		return false;
	}
	
	//Valido los meses de 30 dias
	if((mes1==4 || mes1==6 || mes1==9 || mes1==11 )){
		
		if((dia1==31)){
			alert("En Abril, Junio, Septiembre y Noviembre el máximo son 30 días");
			return false;			
		}			
	}	
	
	if((mes2==4 || mes2==6 || mes2==9 || mes2==11 )){
		
		if((dia2==31)){
			alert("En Abril, Junio, Septiembre y Noviembre el máximo son 30 días");
			return false;			
		}			
	}
	
	//Valido el año Bisiesto
	if((mes1==2)){
		if((dia1==30 ||dia1==31)){
			alert("Febrero no debe superar el día 29");
			return false;			
		}		
	}	
	
	if((mes2==2)){
		if((dia2==30 ||dia2==31)){
			alert("Febrero no debe superar el día 29");
			return false;			
		}		
	}	
	return true;
}

function comparar_intervalo_fechas_completas(dia1,mes1,dia2,mes2,anio1,anio2,msj){
	
	var dia1 =dia1;
	var mes1 =mes1;
	
	var dia2 =dia2;
	var mes2 =mes2;
			
	dia1 = parseInt(dia1,10);
	mes1 = parseInt(mes1,10);
	
	dia2 = parseInt(dia2,10);
	mes2 = parseInt(mes2,10);
	
	if (dia1>31 || dia2>31){
		alert("El día no puede ser superior a 31");
		return false;
	}	
	
	if (mes1>12 || mes2>12){
		alert("El mes no puede ser superior a 12");
		return false;
	}
	
	var Hoy = new Date(); 
	dia=Hoy.getDate();
	mes=Hoy.getMonth()+1;
	anio=Hoy.getYear();
	/*alert(dia + '|' + dia1);
	alert(mes + '|' + mes1);
	alert(anio + '|' + anio1);*/
	if((mes1==mes) && (anio1==anio)){
		if (dia1<dia){
		alert("La fecha inicio debe ser superior a la fecha actual");
		return false;
		}
	}
	if ((mes1<mes) && (anio1==anio)){
	alert("La fecha inicio debe ser superior a la fecha actual");
	return false;
	}	
	
	if ((mes1==mes2)){
		if ((dia1>dia2)){
			msjVacio(msj);
			return false;
		}
	}
			
	if ((mes1>mes2) && (anio1==anio2)){
		msjVacio(msj);
		return false;
	}
	
	if ((anio2<anio1)){
		msjVacio(msj);
		return false;
	}
	
	//Valido los meses de 30 dias
	if((mes1==4 || mes1==6 || mes1==9 || mes1==11 )){
		
		if((dia1==31)){
			alert("En Abril, Junio, Septiembre y Noviembre el máximo son 30 días");
			return false;			
		}			
	}	
	
	if((mes2==4 || mes2==6 || mes2==9 || mes2==11 )){
		
		if((dia2==31)){
			alert("En Abril, Junio, Septiembre y Noviembre el máximo son 30 días");
			return false;			
		}			
	}
	
	//Valido el año Bisiesto
	if((mes1==2)){
		if((dia1==30 ||dia1==31)){
			alert("Febrero no debe superar el día 29");
			return false;			
		}		
	}	
	
	if((mes2==2)){
		if((dia2==30 ||dia2==31)){
			alert("Febrero no debe superar el día 29");
			return false;			
		}		
	}	
	return true;
}

function comparar_fechas(dia1,mes1,dia2,mes2,fecha_dbi,fecha_dbf){		
	
	var dia1 =dia1;
	var mes1 =mes1;
	
	var dia2 =dia2;
	var mes2 =mes2;
	
	var diadbi =fecha_dbi.substring(0,2);
	var mesdbi =fecha_dbi.substring(3,5);
	
	var diadbf =fecha_dbf.substring(0,2);
	var mesdbf =fecha_dbf.substring(3,5);
	
	dia1 = parseInt(dia1,10);
	mes1 = parseInt(mes1,10);
	
	dia2 = parseInt(dia2,10);
	mes2 = parseInt(mes2,10);
	
	diadbi = parseInt(diadbi,10);
	mesdbi = parseInt(mesdbi,10);
	
	diadbf = parseInt(diadbf,10);
	mesdbf = parseInt(mesdbf,10);
	
	/*Los años no los considero*/
	
	 
	/*Ahora se comparan los meses inicio*/
	if ((mesdbi<=mes1)){
		if (diadbi<=dia1){
			if ((mesdbf>=mes1)){
				if (diadbf>=dia1) {
					alert("La Tarifa que intenta introducir coincide con uno de los\n periodos de cierre del hotel.\n" + fecha_dbi + " al " + fecha_dbf);
					return false;
				}	
			}
		}	
	}
	
	if ((mes1<mesdbi)){
		if ((mesdbi<=mes2)){
			if (diadbi<=dia2){
				if ((mesdbf>=mes2)){
					if (diadbf>=dia2) {
						alert("La Tarifa que intenta introducir coincide con uno de los\n periodos de cierre del hotel.\n" + fecha_dbi + " al " + fecha_dbf);
						return false;
					}	
				}
			}	
		}
	
	}
}


function msjVacio(mensaje){
	alert(mensaje);	
}	

function saltoCasilla(pCasilla,NCasilla){

 switch(NCasilla){
   case "1": //Cuando estoy en el primer input
   if (pCasilla.value.length == 4)
    {
     document.forms[0].n2.focus();
    }
   break;
  case "2": //Cuando estoy en el segundo input
   if (pCasilla.value.length == 4)
    {
     document.forms[0].n3.focus();
    } 	
   break;	
   case "3":  //Fin
   if (pCasilla.value.length == 4)
    {
     document.forms[0].n4.focus();
    }
    break;
    case "4":  //Fin
   if (pCasilla.value.length == 4)
    {
     document.forms[0].mes_cad.focus();
    }
    break;
    case "5":  //Fin
   if (pCasilla.value.length == 2)
    {
     document.forms[0].anio_cad.focus();
    }
  break;
  }
}

function saltoCasillas(pCasilla,NCasilla){

 switch(NCasilla){
   case "1": //Cuando estoy en el primer input
   if (pCasilla.value.length == 4)
    {
     document.forms[0].n2.focus();
    }
   break;
  case "2": //Cuando estoy en el segundo input
   if (pCasilla.value.length == 4)
    {
     document.forms[0].n3.focus();
    } 	
   break;	
   case "3":  //Fin
   if (pCasilla.value.length == 4)
    {
     document.forms[0].n4.focus();
    }
    break;
    case "4":  //Fin
   if (pCasilla.value.length == 4)
    {
     document.forms[0].mes_cad.focus();
    }
    break;
    case "5":  //Fin
   if (pCasilla.value.length == 2)
    {	
     document.forms[0].anio_cad.focus();
    }
    break;
    case "6":  //Fin
   if (pCasilla.value.length == 2)
    {
     document.forms[0].cvc.focus();
    }
  	break;
  }
}

function saltoCasillas2(pCasilla,NCasilla,Tarjeta){

 if(Tarjeta==""){
 	alert("Debe Seleccionar antes el Tipo de Tarjeta.\n");
 	pCasilla.value="";
	return false;	
 }
 if(Tarjeta== "AMERICAN" || Tarjeta== "DINERS"){
 	 document.forms[0].n4.value="";	
	 switch(NCasilla){
	   case "1": //Cuando estoy en el primer input
	   if (pCasilla.value.length == 4)
	    {
	     document.forms[0].n2.focus();
	    }
	   break;
	  case "2": //Cuando estoy en el segundo input
	   if (pCasilla.value.length == 6)
	    {
	     document.forms[0].n3.focus();
	    } 	
	   break;	
	   case "3":  //Fin
	   if (pCasilla.value.length == 5)
	    {
	     document.forms[0].mes_cad.focus();
	    }
	    break;
	    case "5":  //Fin
	   if (pCasilla.value.length == 2)
	    {	
	     document.forms[0].anio_cad.focus();
	    }
	    break;
	    case "6":  //Fin
	   if (pCasilla.value.length == 2)
	    {
	     document.forms[0].cvc.focus();
	    }
	  	break;
	  }
  }else{
  	switch(NCasilla){
	   case "1": //Cuando estoy en el primer input
	   if (pCasilla.value.length == 4)
	    {
	     document.forms[0].n2.focus();
	    }
	   break;
	  case "2": //Cuando estoy en el segundo input
	   if (pCasilla.value.length == 4)
	    {
	     document.forms[0].n3.focus();
	    } 	
	   break;	
	   case "3":  //Fin
	   if (pCasilla.value.length == 4)
	    {
	     document.forms[0].n4.focus();
	    }
	    break;
	    case "4":  //Fin
	   if (pCasilla.value.length == 4)
	    {
	     document.forms[0].mes_cad.focus();
	    }
	    break;
	    case "5":  //Fin
	   if (pCasilla.value.length == 2)
	    {	
	     document.forms[0].anio_cad.focus();
	    }
	    break;
	    case "6":  //Fin
	   if (pCasilla.value.length == 2)
	    {
	     document.forms[0].cvc.focus();
	    }
	  	break;
	  }  
  }	  
}

var ns7 = false;
//var ns7 = window.Event ? true : false;	//Navegadores
var nav = "";
if (navigator.userAgent.indexOf("Opera")!=-1) nav = "Opera";
else {
  if (navigator.appName == "Netscape") 
    {nav = "NS"; ns7 = true;}
  else 
    {nav = "IE"; ns7 = false;}
	 }


function numeros(evt){
//Espacio = 8, Enter = 13, '0' = 48, '9' = 57
 var key = ns7 ? evt.which : evt.keyCode;
 return (key <= 13 || (key >= 48 && key <= 57));
}

function spaceoff(s) {
   while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
   while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
    return s;
 }

function caja_ninos(parametro){
	var infantil=parametro.split("-");
	var ninos=infantil[1];
	
	//muestro y oculto el espacio de pantalla para los datos del niño
	if(ninos==0){
		document.getElementById('capaNinos').style.display='none';
	}else{
		document.getElementById('capaNinos').style.display='block';
	}						
	if(ninos==0){							   								   
		for (var i=1; i < 13;i++){
			eval("document.getElementById('enino' + i).style.visibility='hidden'");
	}	
								
	}else if(ninos==1){
		var longitud=(parseInt(document.demoform.habitaciones.value)*parseInt(ninos))+1;
		for (var i=1; i < longitud;i++){
			eval("document.getElementById('enino' + i).style.visibility='visible'");
		}
		for (var i=longitud; i < 13;i++){
			eval("document.getElementById('enino' + i).style.visibility='hidden'");
		}
	}else if(ninos==2){
		var longitud=(parseInt(document.demoform.habitaciones.value)*parseInt(ninos))+1;
		for (var i=1; i < longitud;i++){
			eval("document.getElementById('enino' + i).style.visibility='visible'");
		}
		for (var i=longitud; i < 13;i++){
			eval("document.getElementById('enino' + i).style.visibility='hidden'");
		}
	}else{
		var longitud=(parseInt(document.demoform.habitaciones.value)*parseInt(ninos))+1;
		for (var i=1; i < longitud;i++){
			eval("document.getElementById('enino' + i).style.visibility='visible'");
		}
		for (var i=longitud; i < 13;i++){
			eval("document.getElementById('enino' + i).style.visibility='hidden'");
		}
	}
	
	
} 

function comprobarDatos(){
	formulario=document.forms[0];
	//comprobamos que los datos se encuentren rellenos
	if(formulario.CountFP.value=='2'){
		if(formulario.n1.value.length == 4 && formulario.n2.value.length == 4 && formulario.n3.value.length == 4 && formulario.n4.value.length == 4 && formulario.mes_cad.value.length == 2 && formulario.anio_cad.value.length == 2 && formulario.cvc.value.length == 3){
			subSegunda=formulario.n2.value.substring(0,2);
			fpago=formulario.CountFP.value;
			if(formulario.n1.value=='5540' && subSegunda=='13'){
				document.getElementById('d-modalidadA').style.display = 'block';
				document.getElementById('d-modalidadB').style.display = 'none';
			}else{
				document.getElementById('d-modalidadA').style.display = 'none';
				document.getElementById('d-modalidadB').style.display = 'block';
			}
		}
	}	
}


