
function createSelector(el, nrOptions, idx) {
	var oOption, oText;
	
	if (el) {
		for (i = idx; i < nrOptions+1; i++) {
			oOption = document.createElement("OPTION");
			oText = document.createTextNode("" + i);
			oOption.value = i;
			el.appendChild(oOption);
			oOption.appendChild(oText);
		}
	}
}


function fixDateSelectors() {
	if (!document.forms['searchform']) return;
	
	var date = new Date();
	var m = date.getMonth();
	var d = date.getDate();
	var y = date.getYear();
	var i;

	// limit the day selector's length according to the number of days in the current month
	// after that, increment month by one 'coz it goes [0..11]
	for (i = 0; i < document.forms.length; i++) {
		if (document.forms[i].name == "searchform" || document.forms[i].name == "calculateform")
			initDaySelector(document.forms[i], y, m);
	}
	m++;
	
	// convert to strings
	m += "";
	d += "";
	y += "";
	
	// init "today" as the default value in the selectors
	if (d.length == 1) d = "0" + d;
	if (m.length == 1) m = "0" + m;
	for (i = 0; i < document.forms.length; i++) {
		if (document.forms[i].name == "searchform" || document.forms[i].name == "calculateform") {
			document.forms[i].day.value = d;
			document.forms[i].month.value = m;
			document.forms[i].year.value = y;
		}
	}
}

function initDaySelector(aForm, year, month) {
	var days = 32 - new Date(year, month, 32).getDate();	// the number of days in the current month
	var formEl = aForm.day;
	var oldLength = formEl.length;

	formEl.length = days;
	
	// if the new selected month has more days than the previous one, 
	// we need to fill in the missing days in the selector
	if (oldLength < days) {
		var dx = days - oldLength;
		var value = formEl.options[oldLength-1].value;
		for (var i = 0; i < dx; i++) {
			formEl.options[oldLength].value = formEl.options[oldLength].text = ++value;
			oldLength++;
		}
	}
}

// Controlamos si la fecha que nos están introduciendo es menor que el día actual
function comprobarDia(yearSel, mesSel, diaSel, yearHoy, monthHoy, dayHoy, msg) {	
	var fechaHoy= yearHoy+monthHoy+dayHoy;
	var fechaSel= yearSel+mesSel+diaSel;
	
	if (fechaHoy>fechaSel) {
		alert (msg);
		return false;
	}
	return true;
}

// Controlamos si la fecha que nos están introduciendo es menor que el día actual
function comprobarDiaNew(yearSel, mesSel, diaSel, yearSelFin, mesSelFin, diaSelFin, yearHoy, monthHoy, dayHoy, msg) {	
	var fechaHoy= yearHoy+monthHoy+dayHoy;
	var fechaSel= yearSel+mesSel+diaSel;
	var fechaSelFin= yearSelFin+mesSelFin+diaSelFin;
	
	if (fechaHoy>fechaSel) {
		alert (msg);
		return false;
	}
	if (fechaSelFin <= fechaSel) {
		alert ('Departure date must be greater than the initial.');
		return false;
	}
	// Asignamos el número de noches
	
	
	
   //Obtiene dia, mes y año
   var fecha1 = new fecha( diaSel+'/'+mesSel+'/'+yearSel )  
   var fecha2 = new fecha( diaSelFin+'/'+mesSelFin+'/'+yearSelFin)  
   
   //Obtiene objetos Date
   var miFecha1 = new Date( fecha1.anio, fecha1.mes-1, fecha1.dia )	//Month is 0-11 in JavaScript
   var miFecha2 = new Date( fecha2.anio, fecha2.mes-1, fecha2.dia )	//Month is 0-11 in JavaScript

   //Resta fechas y redondea
   var diferencia = miFecha2.getTime() - miFecha1.getTime()
   var dias = Math.floor(diferencia / (1000 * 60 * 60 * 24))
   var segundos = Math.floor(diferencia / 1000)
	
	document.searchform.elements['nr_nights'].value = (dias);

	return true;
}


// Controlamos si la fecha que nos están introduciendo es menor que el día actual
function comprobarDiaNew2(yearSel, mesSel, diaSel, yearSelFin, mesSelFin, diaSelFin, yearHoy, monthHoy, dayHoy, msg) {	
	var fechaHoy= yearHoy+monthHoy+dayHoy;
	var fechaSel= yearSel+mesSel+diaSel;
	var fechaSelFin= yearSelFin+mesSelFin+diaSelFin;
	
	if (fechaHoy>fechaSel) {
		alert (msg);
		return false;
	}
	if (fechaSelFin <= fechaSel) {
		alert ('Departure date must be greater than the arrival.');
		return false;
	}
	// Asignamos el número de noches
	
	
	
   //Obtiene dia, mes y año
   var fecha1 = new fecha( diaSel+'/'+mesSel+'/'+yearSel )   
   var fecha2 = new fecha( diaSelFin+'/'+mesSelFin+'/'+yearSelFin)
   
   //Obtiene objetos Date
   var miFecha1 = new Date( fecha1.anio, fecha1.mes-1, fecha1.dia )	//Month is 0-11 in JavaScript
   var miFecha2 = new Date( fecha2.anio, fecha2.mes-1, fecha2.dia )	//Month is 0-11 in JavaScript

   //Resta fechas y redondea
   var diferencia = miFecha2.getTime() - miFecha1.getTime()
   var dias = Math.floor(diferencia / (1000 * 60 * 60 * 24))
   var segundos = Math.floor(diferencia / 1000)
	
	
	document.calculateform.elements['nr_nights_modif'].value = (dias);
	return true;
}



function fecha( cadena ) {

   //Separador para la introduccion de las fechas
   var separador = "/"

   //Separa por dia, mes y año
   if ( cadena.indexOf( separador ) != -1 ) {
        var posi1 = 0
        var posi2 = cadena.indexOf( separador, posi1 + 1 )
        var posi3 = cadena.indexOf( separador, posi2 + 1 )
        this.dia = cadena.substring( posi1, posi2 )
        this.mes = cadena.substring( posi2 + 1, posi3 )
        this.anio = cadena.substring( posi3 + 1, cadena.length )
   } else {
        this.dia = 0
        this.mes = 0
        this.anio = 0   
   }
}





function validate(f) {

	ERROR = "";
	if (f.name.value == '' || f.name.value == ' ')
		ERROR += "Please insert first name\n"; 
	if(f.country.value == '')
		ERROR += "Please select a country\n";
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(f.email.value)))
		ERROR += "Please enter a valid email\n";
	if(f.email.value != f.reemail.value) {
		ERROR += "Please confirm your email\n";
		f.reemail.value = "";
	}
	if (f.daytime_phone.value == '')
		ERROR += "Please insert your daytime phone\n"; 
	if(ERROR == "")
			return true;
	else{
		alert(ERROR);
		return false;
	}
}

function checkFormEN(f)
{
	ERROR = "";
	if(f.nombre.value == '' ||f.nombre.value == ' ' )
		ERROR += "Please enter your first name<br/>";
	if(f.apellidos.value == '' ||f.lastname.value == ' ' )
		ERROR += "Please enter your last name<br/>";
	if(f.ciudad.value == '')
		ERROR += "Please select a country<br/>";
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(f.email.value)))
		ERROR += "Please enter a valid email<br/>";
/*
	if(f.email.value != f.reemail.value) {
		ERROR += "The two mails have to be the same<br/>";
		f.reemail.value = "";
	}
    if(f.daytime_phone.value == '' ||f.daytime_phone.value == ' ' )
		ERROR += "Please enter your day time phone<br/>";
	if(f.i_agree_to_the_terms_and_conditions.checked != true)
		ERROR += "You have to agree to the terms and conditions<br/>";

	
	if(f.address.value == '' || f.address.value == ' ' )
		ERROR += "Please enter your address<br/>";
	if(f.codpos.value == '' || f.codpos.value == ' ' )
		ERROR += "Please enter your zip<br/>";
	if(f.ciudad.value == '' || f.ciudad.value == ' ' )
		ERROR += "Please enter your city<br/>";
*/	
	if(ERROR == "")
	{
		return true;
	}
	else{
		document.getElementById("error").innerHTML=ERROR;
		return false;
	}
}

function checkFormES(f) {
	ERROR = "";
	if (f.nombre.value == '' || f.nombre.value == ' ' ) {
		ERROR += "Por favor, es necesario el nombre<br/>";
	}
	if (f.apellidos.value == '' || f.apellidos.value == ' ' )
		ERROR += "Por favor, son necesarios los apellidos<br/>";
	if (f.ciudad.value == '')
		ERROR += "Por favor, rellene el campo ciudad<br/>";
	if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(f.email.value)))
		ERROR += "Por favor, escriba un email v&aacute;lido<br/>";
/*
	if(f.email.value != f.reemail.value) {
		ERROR += "The two mails have to be the same<br/>";
		f.reemail.value = "";
	}
    if(f.daytime_phone.value == '' ||f.daytime_phone.value == ' ' )
		ERROR += "Please enter your day time phone<br/>";
	if(f.i_agree_to_the_terms_and_conditions.checked != true)
		ERROR += "You have to agree to the terms and conditions<br/>";

	
	if(f.address.value == '' || f.address.value == ' ' )
		ERROR += "Please enter your address<br/>";
	if(f.codpos.value == '' || f.codpos.value == ' ' )
		ERROR += "Please enter your zip<br/>";
	if(f.ciudad.value == '' || f.ciudad.value == ' ' )
		ERROR += "Please enter your city<br/>";
*/	
	if (ERROR == "") {
		return true;
	} else{
		document.getElementById("error").innerHTML=ERROR;
		return false;
	}
}


function checkBookingFormEN(f)
{
	ERROR = "";
	if(f.firstname.value == '' ||f.firstname.value == ' ' )
		ERROR += "Please enter your first name<br/>";
	if(f.lastname.value == '' ||f.lastname.value == ' ' )
		ERROR += "Please enter your last name<br/>";
	if(f.country.value == '')
		ERROR += "Please select a country<br/>";
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(f.email.value)))
		ERROR += "Please enter a valid email<br/>";
	if(f.email.value != f.reemail.value) {
		ERROR += "The two mails have to be the same<br/>";
		f.reemail.value = "";
	}
    if(f.daytime_phone.value == '' ||f.daytime_phone.value == ' ' )
		ERROR += "Please enter your day time phone<br/>";
	if(f.i_agree_to_the_terms_and_conditions.checked != true)
		ERROR += "You have to agree to the terms and conditions<br/>";
	
	if(f.address.value == '' || f.address.value == ' ' )
		ERROR += "Please enter your address<br/>";
	if(f.codpos.value == '' || f.codpos.value == ' ' )
		ERROR += "Please enter your zip<br/>";
	if(f.ciudad.value == '' || f.ciudad.value == ' ' )
		ERROR += "Please enter your city<br/>";
	
	if(ERROR == "")
	{
		return true;
	}
	else{
		document.getElementById("error").innerHTML=ERROR;
		return false;
	}
}

function checkBookingFormEs(f) {
	ERROR = "";
	
	if(f.checkboxOK.checked != true)
		ERROR += "PARA CONTINUAR CON LA CONTRATACI&Oacute;N DEBE ACEPTAR LAS CONDICIONES GENERALES DE VENTA !!!<br/>";
		
	if (ERROR != "") {
		document.getElementById("panelError").style.visibility= "visible";
		document.getElementById("error").innerHTML=ERROR;
		return false;
	}
	

	if(f.nombre_com.value == '' || f.nombre_com.value == ' ' )
		ERROR += "Debe rellenar el nombre del comprador<br/>";
	if(f.apellido_com.value == '' || f.lapellido_com.value == ' ' )
		ERROR += "Debe rellenar los apellidos del comprador<br/>";
	if(f.ciudad_com.value == '' || f.ciudad_com.value == ' ' )
		ERROR += "Debe rellenar el nombre de la ciudad del comprador<br/>";
	if(f.pais_com.value == '')
		ERROR += "Escriba el pa&igrave;s del comprador<br/>";
		
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(f.email_com.value)))
		ERROR += "Debe indicarnos una direcci&oacute;n de email v&aacute;lida del comprador<br/>";
		
	if(f.email_com.value != f.reemail_com.value) {
		ERROR += "Los emails del comprador no son iguales<br/>";
		f.reemail_com.value = "";
	}
		
	if(f.licencia_dri.value == '' || f.licencia_dri.value == ' ' )
		ERROR += "Debe indicar el número de licencia de conducci&oacute;n del piloto<br/>";


	// Si esta chequeado el checkbox de datos del piloto iguales al comprador
	if (f.checkbox_piloto.checked==1) {
		f.nombre_dri.value= f.nombre_com.value;
		f.apellido_dri.value= f.apellido_com.value;
		f.telefono_dri.value= f.telefono_com.value;
		f.direccion_dri.value= f.direccion_com.value;
		f.ciudad_dri.value= f.ciudad_com.value;
		f.codpos_dri.value= f.codpos_com.value;
		f.pais_dri.value= f.pais_com.value;
		f.email_dri.value= f.email_com.value;
		f.reemail_dri.value= f.reemail_com.value;
	} else {
		if(f.nombre_dri.value == '' || f.nombre_dri.value == ' ' )
			ERROR += "Debe rellenar el nombre del piloto<br/>";
		if(f.apellido_dri.value == '' ||f.lapellido_dri.value == ' ' )
			ERROR += "Debe rellenar los apellidos del piloto<br/>";
		if(f.ciudad_dri.value == '' ||f.ciudad_dri.value == ' ' )
			ERROR += "Debe rellenar el nombre de la ciudad del piloto<br/>";
		if(f.pais_dri.value == '')
			ERROR += "Escriba el pa&igrave;s del piloto<br/>";
		
		if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(f.email_dri.value)))
			ERROR += "Debe indicarnos una direcci&oacute;n de email v&aacute;lida del piloto<br/>";
			
		if(f.email_dri.value != f.reemail_dri.value) {
			ERROR += "Los emails del piloto no son iguales<br/>";
			f.reemail_com.value = "";
		}
	}

	
	
	if (ERROR == "") {
		return true;
	} else {
		document.getElementById("panelError").style.visibility= "visible";
		document.getElementById("error").innerHTML= ERROR;
		return false;
	}
}
