// Functions
function isInteger(val) {
    if(val==null) return false;
    if (val.length==0) return false;
    for (var i = 0; i < val.length; i++) {
        var ch = val.charAt(i);
        if (ch < "0" || ch > "9") return false;
    }
    return true;
}

// Parse Date

function strip_chars_in_bag(s, bag){
	var i;
    var returnString = "";
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function days_in_february (year){
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function days_array(n) {
	for (var i=1; i<=n; i++) {
		this[i] = 31;
		if (i==4 || i==6 || i==9 || i==11) this[i] = 30;
		if (i==2) this[i] = 29;
   }
   return this;
}

function is_date(dtStr){
	
	dtStr = dtStr.replace(/^\s*|\s*$/g,"");
	
	var dtCh= "/";
	var minYear=1900;
	var maxYear=2100;
	
	var daysInMonth = days_array(12);
	var pos1 = dtStr.indexOf(dtCh);
	var pos2 = dtStr.indexOf(dtCh,pos1+1);
	var strDay = dtStr.substring(0,pos1);
	var strMonth = dtStr.substring(pos1+1,pos2);
	var strYear = dtStr.substring(pos2+1);
	strYr = strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay = strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth = strMonth.substring(1);
	for (var i=1; i<=3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr = strYr.substring(1);
	}
	day = parseInt(strDay);
	month = parseInt(strMonth);
	year = parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd/mm/yyyy");
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month");
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>days_in_february(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day");
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear);
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(strip_chars_in_bag(dtStr, dtCh))==false){
		alert("Please enter a valid date");
		return false;
	}
	return true
}

// Idioma
cambiar_idioma = function(lang) {
	new Ajax.Request('idioma.php',{method:'post',
		parameters:{lang:lang},
		onComplete: function(transport) {
			document.location = document.location;
		}
	});
	return false;
}

//Modo travelsenior
cambiar_a_modo_travelsenior = function() {
	var pass = prompt("Introduce el password asociado a Travelsenior", "");
	new Ajax.Request('travelsenior.php',{method:'post',
		parameters:{pass:pass},
		onComplete: function(transport) {
			document.location = document.location;
		}
	});
	return false;
}

salir_modo_travelsenior = function() {
	new Ajax.Request('travelsenior.php',{method:'post',
		onComplete: function(transport) {
			document.location = document.location;
		}
	});
	return false;
}

// Reservar
obtener_productos = function(id) {
	if($('tipo_producto').selectedIndex >= 0) {
		new Ajax.Updater('producto','productos.php',{method:'get',
			parameters:{
				id:$('tipo_producto').options[$('tipo_producto').selectedIndex].value,
				selected:id
			}
		});
	}
}

obtener_numero_habitaciones = function() {
	var plazas = $('plazas').value;

	$('triples').value = 0;
	$('dobles').value = Math.floor(plazas / 2);
	$('individuales').value = plazas % 2;
}

obtener_hoteles = function() {
	
	if ($('tipo_producto').options[$('tipo_producto').selectedIndex].value == 0) {
		alert('Seleccionar un destino');
	}
	else if ($('productos').options[$('productos').selectedIndex].value == 0) {
		alert('Seleccionar un producto');
	}
	else if ($('plazas').value == '' || $('individuales').value == ''
			|| $('dobles').value == '' || $('triples').value == '') {
		alert('Campos de plazas vacios');
	}
	else if (!isInteger($('plazas').value) || !isInteger($('individuales').value)
		|| !isInteger($('dobles').value) || !isInteger($('triples').value)) {
		alert('Los campos de plazas solo admiten números enteros');
	}
	else if ( (parseInt($('triples').value)*3
				+ parseInt($('dobles').value)*2
				+ parseInt($('individuales').value)) != $('plazas').value ) {
		alert('El número de plazas no coincide con las habitaciones reservadas');
	}
	else if ($('fecha').value == '') {
		alert('Campo fecha vacio');
	}
	else {
		$('reservar').submit();
	}
}

// Pasajeros/Vuelos

submit_passengers = function(max) {
	var valido = 1;
	
	for(var i=0; i<=max; i++) {
		if ($('p_f_nacimiento_'+i)) {
			if ($('p_f_nacimiento_'+i).value != '' && !is_date($('p_f_nacimiento_'+i).value)){
				valido = 0;
				break;
			}
		}
	}
	
	if(valido == 1) $('form_pasajeros').submit();
}

submit_passengers_flyies = function(max) {
	
	var valido = 1;
	
	for(var i=0; i<=max; i++) {
		if ($('p_check['+i+']')) {
			if ($('p_vuelo_llegada['+i+']').value.length > 7){
				alert('Longitud del numero de vuelo de llegada superior al maximo (7 caracteres)');
				valido = 0;
				break;
			}
			if ($('p_vuelo_salida['+i+']').value.length > 7){
				alert('Longitud del numero de vuelo de llegada superior al maximo (7 caracteres)');
				valido = 0;
				break;
			}
			if ($('p_fecha_llegada_'+i).value != '' && !is_date($('p_fecha_llegada_'+i).value)){
				valido = 0;
				break;
			}
			if ($('p_fecha_salida_'+i).value != '' && !is_date($('p_fecha_salida_'+i).value)){
				valido = 0;
				break;
			}
		}
	}
	
	if(valido == 1) $('form_pasajeros').submit();
}

copy_to_all_passengers = function(max) {
	for(var i=0; i<=max; i++) {
		if ($('p_check['+i+']')) {
			$('p_vuelo_llegada['+i+']').value = $('p_vuelo_llegada').value;
			$('p_fecha_llegada_'+i).value = $('p_fecha_llegada').value;
			$('p_hora_llegada['+i+']').selectedIndex = $('p_hora_llegada').selectedIndex;
			$('p_minuto_llegada['+i+']').selectedIndex = $('p_minuto_llegada').selectedIndex;
			$('p_aeropuerto_llegada['+i+']').selectedIndex = $('p_aeropuerto_llegada').selectedIndex;
			$('p_vuelo_salida['+i+']').value = $('p_vuelo_salida').value;
			$('p_fecha_salida_'+i).value = $('p_fecha_salida').value;
			$('p_hora_salida['+i+']').selectedIndex = $('p_hora_salida').selectedIndex;
			$('p_minuto_salida['+i+']').selectedIndex = $('p_minuto_salida').selectedIndex;
			$('p_aeropuerto_salida['+i+']').selectedIndex = $('p_aeropuerto_salida').selectedIndex;
		}
	}
}

copy_to_selected_passengers = function(max) {
	for(var i=0; i<=max; i++) {
		if ($('p_check['+i+']')) {
			if ($('p_check['+i+']').checked) {
				$('p_vuelo_llegada['+i+']').value = $('p_vuelo_llegada').value;
				$('p_fecha_llegada_'+i).value = $('p_fecha_llegada').value;
				$('p_hora_llegada['+i+']').selectedIndex = $('p_hora_llegada').selectedIndex;
				$('p_minuto_llegada['+i+']').selectedIndex = $('p_minuto_llegada').selectedIndex;
				$('p_aeropuerto_llegada['+i+']').selectedIndex = $('p_aeropuerto_llegada').selectedIndex;
				$('p_vuelo_salida['+i+']').value = $('p_vuelo_salida').value;
				$('p_fecha_salida_'+i).value = $('p_fecha_salida').value;
				$('p_hora_salida['+i+']').selectedIndex = $('p_hora_salida').selectedIndex;
				$('p_minuto_salida['+i+']').selectedIndex = $('p_minuto_salida').selectedIndex;
				$('p_aeropuerto_salida['+i+']').selectedIndex = $('p_aeropuerto_salida').selectedIndex;
			}
		}
	}
}

cambiar_checks = function(max) {
	for(var i=0; i<=max; i++) {
		if ($('p_check['+i+']'))
			$('p_check['+i+']').checked = $('p_check').checked;
	}
}

// Reservas
show_hide = function(id) {
	if ($(id).style.display == 'none')  $(id).style.display = 'table-cell';
	else if ($(id).style.display == 'table-cell')  $(id).style.display = 'none';
}

// Calendario Info
obtener_reservas = function(hotel, fecha) {
	$('detalle_'+hotel).innerHTML = '<img src="img/loading.gif" />';
	new Ajax.Updater('detalle_'+hotel, 'calendario-detalle.php',{method:'get',
			parameters:{hotel:hotel, fecha:fecha}
	});
}
