/*
	-----------------------------------------------------
  Autor:

    Joaquín Villarroya Durá

  Fecha:

    viernes, 14 de mayo de 2004

  Descripción:

    Scripts para la página inicial de búsqueda de alojamientos.
	-----------------------------------------------------
*/

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
   //if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];
   //alert("x.src:" + x.src + ", " + a[i] + ", " + a[i+2]);}
}

/*
-------------------------------------------------------------------------------
  Descripción:

    Recibe un Date() y devuelve "01/07/0004"
-------------------------------------------------------------------------------
*/
function FormatDate(dDate)
{
	var sDate
	var iValue

	sDate = ""

	//día
	iValue = dDate.getDate();
	if (iValue < 10) {
		sDate = sDate + "0" + iValue;
	}
	else {
		sDate = sDate + String(iValue);
	}

	//mes
	sDate = sDate + "/"
	iValue = dDate.getMonth()+1;
	if (iValue < 10) {
		sDate = sDate + "0" + iValue;
	}
	else {
		sDate = sDate + iValue;
	}

	//año
	sDate = sDate + "/"
	sDate = sDate + dDate.getFullYear();

	return sDate;
}

/*

  Autor:

    Juan Diego Gutiérrez Gallardo

  Fecha:

    lunes 22 de marzo de 2004

  Descripción:

    Scripts para la página de búsqueda de alojamientos.

*/

/*

  Descripción:

    Comprobamos que todos los valores de la búsqueda son correctos.

*/

//-----------------------------------------------------------------------------
/*
function check_parameters(oForm) {

  var dateCheckin  = new Date(oForm.checkin.value);
  var iNights      = new Number(oForm.nights.value);
  //var iIdCountry = new Number(oForm.idcountry.value);
  var iIdState     = new String(oForm.idstate.value);
  //var sCity      = new String(oForm.city.value);
  var iAdults      = new Number(oForm.adults.value);
  var iChildren    = new Number(oForm.children.value);
  var iIdLanguage  = new Number(oForm.idlanguage.value);

  // [todo] Mensajes de error dependientes del idioma.
  if(isNaN(dateCheckin)){
    alert("Fecha incorrecta.");
    oForm.checkin.select();
    return false;
  }

  if(iNights <= 0){
    alert("Número de noches incorrecto.");
    oForm.nights.select();
    return false;
  }

  //if(iIdCountry <= 0){
  //  alert("Es necesario seleccionar un país.");
  //  return false;
  //}

  //if(iIdState <= 0){
  //  alert("Es necesario seleccionar una provincia.");
  //  return false;
  //}

  //if(sCity==""){
  //  alert("Es necesario indicar una localidad.");
  //  oForm.city.select();
  //  return false;
  //}

  if(iAdults <= 0){
    alert("Número de adultos incorrecto.");
    oForm.adults.select();
    return false;
  }
  if((iChildren <= 0) || (iChildren > 12)){
    alert("Número de niños incorrecto.");
    //oForm.children.select();
    return false;
  }

  if(iIdLanguage <= 0){
    alert("El idioma no es correcto.");
    oForm.idlanguage.select();
    return false;
  }

  return(true);
}
*/

/*
-------------------------------------------------------------------------------
  Descripción:

    Preparar los parámetros necesarios para realizar la búsqueda.
    Rellenar el form oculto (oFormTop) con los datos del form visible (oForm),
    y realizar el submit desde el formulario oFormTop.
-------------------------------------------------------------------------------
*/
function SearchHotels(oForm, oFormTop)
{
	/*
  if(!check_parameters(oForm)){
    return false;
  }
	*/

	if (oForm.idstate.value == "") {
  	alert(sError_Destination);
    oForm.idstate.focus();
  	return false;
	}

  var dateCheckIn = new Date(
    oForm.checkin_year.value,
    oForm.checkin_month.value - 1,
    oForm.checkin_day.value
  );

	//impedir fecha de entrada < Hoy
  var dateToday = new Date();
  if (dateCheckIn < dateToday) {
  	alert(sError_Today);
    oForm.checkin_day.focus();
  	return false;
  }

  //Impedir fechas incorrectas (31/JUN); para javascript el 31/JUN = 01/JUL
  if (dateCheckIn.getDate() != oForm.checkin_day.value) {
  	alert(sError_Checkin);
    oForm.checkin_day.focus();
  	return false;
  }
  //---------------------------------------
  //HACKING
	//impedir fecha de entrada < 01/NOV/2004
	//para evitar reservas onerosas
	//sólo para depuración.
  //---------------------------------------
  //
  //var dateHack = new Date("11/01/2004");
  //if (dateCheckIn < dateHack) {
  //	alert("Sorry, Check-in date must be > 01/NOV/2004.");
  //  oForm.checkin_day.focus();
  //	return false;
  //}
  //
  //---------------------------------------
  var sCheckIn;
  sCheckIn = FormatDate(dateCheckIn);

  var dateCheckOut  = new Date(
    oForm.checkout_year.value,
    oForm.checkout_month.value - 1,
    oForm.checkout_day.value
  );

  //Impedir fechas incorrectas (31/JUN); para javascript el 31/JUN = 01/JUL
  if (dateCheckOut.getDate() != oForm.checkout_day.value) {
  	alert(sError_Checkout);
    oForm.checkout_day.focus();
  	return false;
  }

	var diff;
	diff = dateCheckOut.getTime() - dateCheckIn.getTime();
	var iNights;
	iNights = Math.floor(diff / (1000 * 60 * 60 * 24));

	if (iNights <= 0) {
    alert(sError_Later);
    oForm.checkout_day.focus();
		return false;
	}
	if (iNights > 21) {
    alert(sError_Week);
    oForm.checkout_day.focus();
		return false;
	}

  var iRooms = new String(oForm.num_rooms.value);
  //var sCity = new String(oForm.city.value);
  var iAdults = new String(oForm.num_adults.value);
  var iChildren = new String(oForm.num_children.value);
  var iIdLanguage = new String(oForm.idlanguage.value);

  //Obtener el valor del <select id="idstate">.
  //  Un valor 14;81 significa:
  //    14 = España (País/Country)
  //    81 = Pirineo Catalán (Provincia/State)
  var sAux = new String(oForm.idstate.value);
  var iPosition = sAux.indexOf(";");
  var iIdCountry = sAux.substring(0, iPosition);
  var iIdState = sAux.substring(iPosition + 1);

  //Obtener el nombre del pais a partir del valor iIdCountry obtenido antes,
  //y de los nombres de los paises almacenados en <select id="idcountries">,
  //que está oculto en search.asp.
  var oCountries = oForm.idcountries;
  var sCountry = "";
  var iAux;
  for (iAux = 0; iAux < oCountries.options.length; iAux++) {
    if (oCountries.options[iAux].value == iIdCountry) {
      sCountry = new String(oCountries.options[iAux].text);
      break;
    }
  }

  var oState = oForm.idstate;
  var sState = new String(oState.options[oState.selectedIndex].text);

  /*
  var sRequest = new String();
  sRequest = sRequest
    + "iIdCountry: "  + iIdCountry  + "\n"
    + "sCountry: "    + sCountry    + "\n"
    + "iIdState: "    + iIdState    + "\n"
    + "sState: "      + sState      + "\n"
    //+ "sCity: "       + sCity       + "\n"
    + "dateCheckin: " + sCheckIn    + "\n"
    + "iNights: "     + iNights     + "\n"
    + "iRooms: "      + iRooms      + "\n"
    + "iAdults: "     + iAdults     + "\n"
    + "iChildren: "   + iChildren   + "\n"
    + "iIdLanguage: " + iIdLanguage + "\n"
    + "cmd: "         + "search"    + "\n"
  ;

  alert(sRequest);
  return false;
  */

  oFormTop.idcountry.value  = iIdCountry;
  //oFormTop.country.value    = sCountry;
  oFormTop.idstate.value    = iIdState;
  //oFormTop.state.value      = sState;
  //oFormTop.city.value       = sCity;
  oFormTop.checkin.value    = sCheckIn;
  oFormTop.nights.value     = iNights;
  oFormTop.rooms.value      = iRooms;
  oFormTop.adults.value     = iAdults;
  oFormTop.children.value   = iChildren;
  oFormTop.idlanguage.value = iIdLanguage;
  oFormTop.cmd.value        = "search";

  oFormTop.submit();
  return false;
}

//-----------------------------------------------------------------------------
function syncSearchForms(formName, controlName, controlValue)
{
  //-------------------------------------------------
  //[ini] bug #1607: fecha_salida = fecha_entrada + 1
  //-------------------------------------------------
  var sAux = controlName.substring(0, 7);
  //alert("formName = " + formName + "\ncontrolName = " + controlName
  //  + "\ncontrolValue = " + controlValue + "\nsAux = " + sAux);

  if (sAux == "checkin") {
    var sCheckinDay   = eval("document." + formName + ".checkin_day").value;
    var sCheckinMonth = eval("document." + formName + ".checkin_month").value;
    var sCheckinYear  = eval("document." + formName + ".checkin_year").value;

    var dDate = new Date(sCheckinYear, sCheckinMonth - 1, sCheckinDay);
    dDate.setDate(dDate.getDate() + 1);
    //alert("dDate2 = " + dDate.getDate() + "/" + (dDate.getMonth() + 1) + "/" + dDate.getFullYear());

    eval("document." + formName + ".checkout_day").value = dDate.getDate();
    eval("document." + formName + ".checkout_month").value = dDate.getMonth() + 1;
    eval("document." + formName + ".checkout_year").value = dDate.getFullYear();
  }
  //-------------------------------------------------
  //[fin] bug #1607: fecha_salida = fecha_entrada + 1
  //-------------------------------------------------
}
