﻿/**
 *	Fa la diferencia en dies de dues dates yyyymmdd
 */
 function diffDates(d1, d2) {
	var d1y;
	var d1m;
	var d1d;
	var d2y;
	var d2m;
	var d2d;
	d1y = parseInt(d1.substr(0, 4));
	d1m = parseInt(d1.substr(4, 2));
	d1d = parseInt(d1.substr(6, 2));
	d2y = parseInt(d2.substr(0, 4));
	d2m = parseInt(d2.substr(4, 2));
	d2d = parseInt(d2.substr(6, 2));
	
	var dt1;
	dt1 = new Date(d1y, d1m, d1d);
	var dt2;
	dt2 = new Date(d2y, d2m, d2d);
	
	diff = dt1 - dt2;
//	alert((diff / 86400000) + 1);
	return ((diff / 86400000) + 1);
 }
 
/**
 * Enviarà el formulari de consulta si la comprovació es correcte.
 */
function checkForm() {
	var i;
	var elt;
      
    // Comprobar Sortida
	elt = getElement(document.forms[0], "ddlSalida");
	var salidaText = elt.options[elt.selectedIndex].text;
	if (salidaText.substring(0,2) != "- ") {
		alert(FRgetMessage(1)); //"Debe seleccionar una poblacion de Salida");
		elt.focus();
		return false;
	}

    // Comprobar Combo dates Sortida
    try {
	    elt = getElement(document.forms[0], "ddlFechasSalida");
	    var salidaText = elt.options[elt.selectedIndex].text;
	    if (salidaText.substring(0,2) != "- ") {
		    alert(FRgetMessage(2)); //"Debe seleccionar una fecha de salida de las disponibles");
		    elt.focus();
		    return false;
	    }
	} catch (err) { }

	// Comprobar dates.
    try {
    	var data1 = getElement(document.forms[0], "selAny1").value 
	    		+ getElement(document.forms[0], "selMes1").value 
		    	+ getElement(document.forms[0], "selDia1").value;
	    var data2 = getElement(document.forms[0], "selAny2").value 
		    	+ getElement(document.forms[0], "selMes2").value 
			    + getElement(document.forms[0], "selDia2").value;
				
	    var avui = getTodayStr();
	    diffDates(data1, avui);
	    if ((parseInt(avui)) >= parseInt(data1)) {
		    alert(FRgetMessage(4)); //"La fecha inicial debe ser posterior a hoy");	// Data sortida < data avui.
		    getElement(document.forms[0], "selDia1").focus();
		    return false;
	    }	
	
	    if (parseInt(data2) <= parseInt(data1)) {
		    alert(FRgetMessage(3)); //"La fecha final debe ser posterior a la fecha inicial");	// Data sortida < data arribada.
		    getElement(document.forms[0], "selDia2").focus();
		    return false;
	    }
	} catch (err) { }

    // Comprobar nits.
    //		var nits;
    //		nits =  getElement(document.forms[0], "selNits").selectedIndex + 1;
    //		if (nits == 0) {
    //			alert(FDgetMessage(3));	//alert("Fechas incorrectas (maximo 30 dias de estancia)");
    //			return false;
    //		}
	
    // Comprobar habitacions.
//	var habitacions;
//	habitacions = 0;
//	habitacions = habitacions + parseInt(getElement(document.forms[0], "lbIndividuals").value);
//	habitacions = habitacions + parseInt(getElement(document.forms[0], "lbDobles").value);
//	habitacions = habitacions + parseInt(getElement(document.forms[0], "lbDoblesA").value);			
//	habitacions = habitacions + parseInt(getElement(document.forms[0], "lbDoblesN").value);			
//	habitacions = habitacions + parseInt(getElement(document.forms[0], "lbDoblesAA").value);
//	habitacions = habitacions + parseInt(getElement(document.forms[0], "lbDoblesAN").value);				
//	habitacions = habitacions + parseInt(getElement(document.forms[0], "lbDoblesNN").value);
//		
//	if (habitacions == 0) {
//		alert("Debe seleccionar habitaciones");		//("Falta seleccionar habitaciones");
//		return false;
//	}
//	if (habitacions > 5) {
//		alert("Demasiadas habitaciones (Máximo 5)");		//("Demasiadas habitaciones (Maximo 5)");
//		return false;
//	}
	return true;
}

/**
 * Llegeix el camp ocult amb la data del servidor
 */
function getTodayStr() {
	var hid;
	hid = getElement(document.forms[0], "hidToday");
	if (hid != undefined) {
		return hid.value;
	} else {
		return "undefined";
	}
}

/**
 * retorna un element de formulari acabat en un nom.
 */ 
function findElement(name) 
{
	var s;
	var frm;
	for (i = 0; i < document.forms.length; i++) 
	{
		frm = document.forms[i];
		for (j = 0; j < frm.length; j++) 
		{
			s = frm.elements[j].name;
			if (s.search(name) != -1) return frm.elements[j];			
		}
	}
}

/**
 * 
 */
function FRgetMessage(msgId) {
	switch (getCulture()) {
		case "fr" :
			if (msgId == 1) return "Il faut choisir un lieu de d%e9part.";
			if (msgId == 2) return "Il faut choisir une des dates de d%e9part de ce circuit.";
			if (msgId == 3) return unescape("La premiere date doit %eatre ant%e9rieure a la seconde.");
			if (msgId == 4) return unescape("Date de d%e9part est antérieure a aujourd'hui.");
			break;
		case "es" :
			if (msgId == 1) return "Debe seleccionar una poblacion de Salida.";
			if (msgId == 2) return "Debe seleccionar una fecha de salida de las disponibles";
			if (msgId == 3) return "Primera fecha tiene que ser inferior a la segunda.";
			if (msgId == 4) return "La primera fecha debe ser posterior a hoy";
			break;
		case "ca" :
			if (msgId == 1) return "Cal escollir un punt de sortida.";
			if (msgId == 2) return "Cal escollir una de les dates de sortida del circuit.";
			if (msgId == 3) return "La primera data ha de ser anterior a la segona.";
			if (msgId == 4) return "La primera data ha de ser posterior a avui.";
			break;			
	}
	if (msgId == 1) return "Please select departure point.";
	if (msgId == 2) return "Please select a departure date from the list.";
	if (msgId == 3) return "First date must be lower than the second date";
	if (msgId == 4) return "Dates must be greater than today.";
	return String(msgId);
}
