/// <reference path="jQuery\js\jquery_VS.js">
//Funciones JS para el formulario
function control_formulario() {

  var cad = "";

  //Comprueba que se elija un destino si procede
  if ($("#destino").val() != isNaN) {
    if ($("#destino").val() == '') {
      switch ($("#idioma").val()) {
        case "ES":
          cad = cad + '- Introduzca un destino.\n';
          break;
        case "DE":
          cad = cad + '- Geben Sie Ihr Reiseziel an.\n';
          break;
        case "FR":
          cad = cad + '- Introduire une destination.\n';          
          break;
        case "IT":
          cad = cad + '- Please choose a destination.\n';
          break;
        case "NL":
          cad = cad + '- Please choose a destination.\n';
          break;
        default:
          cad = cad + '- Please choose a destination.\n';
          break;
   	     
      }
      
    }
  }

  //Comprueba que la fecha de entrada no sea menor que la fecha actual
  var f_entrada = new Date($("#fechaEntrada").val().substr(6, 4), $("#fechaEntrada").val().substr(3, 2) - 1, $("#fechaEntrada").val().substr(0, 2));
  var f_salida = new Date($("#fechaSalida").val().substr(6, 4), $("#fechaSalida").val().substr(3, 2) - 1, $("#fechaSalida").val().substr(0, 2));
  var f_actual = new Date();
  f_actual.setHours(0,0,0,0);
  if (f_entrada < f_actual) {
    switch ($("#idioma").val()) {
      case "ES":
        cad = cad + '- La fecha de entrada debe de ser posterior a la fecha actual.\n';
        break;
      case "DE":
        cad = cad + '- Das Eincheck Datum muss nach dem aktuellem Datum sein.\n';
        break;
      case "FR":
        cad = cad + '- La date d\'entree doit etre posterieure a la date du jour.\n';        
        break;
      case "IT":
        cad = cad + '- The arrival date should be later than today\'s date.\n';
        break;
      case "NL":
        cad = cad + '- The arrival date should be later than today\'s date.\n';
        break;
      default:
        cad = cad + '- The arrival date should be later than today\'s date.\n';
        break;
    }          
    
  }
  //Comprueba que la fecha de entrada no sea mayor que la fecha salida
  if (f_entrada >= f_salida) {
    switch ($("#idioma").val()) {
      case "ES":
        cad = cad + '- La fecha salida debe de ser posterior a la fecha de entrada.\n';
        break;
      case "DE":
        cad = cad + '- Das Auscheck Datum muss nach dem Eincheck Datum sein.\n';
        break;
      case "FR":
        cad = cad + '- La date de depart doit etre posterieure a la date d\'entree.\n';
        break;
      case "IT":
        cad = cad + '- The departure date should be later than the arrival date.\n';
        break;
      case "NL":
        cad = cad + '- The departure date should be later than the arrival date.\n';
        break;
      default:
        cad = cad + '- The departure date should be later than the arrival date.\n';
        break;
    }         
        
    
  }

  if (cad != '') {
    alert(cad);
    return false;
  }
  else
    return true;

  alert(cad);
} //Fin control_formulario()

function fNumNinos(opcion) {
  switch (opcion.toString()) {
    case '0':
      $('#spanNinos1').hide();
      $('#spanNinos2').hide();
      break;
    case '1':
      $('#spanNinos1').show();
      $('#spanNinos2').hide();
      break;
    case '2':
      $('#spanNinos1').show();
      $('#spanNinos2').show();
  }
} //Fin fNumNinos(opcion)

//Funciones JS de fechas
function fCambioNoches() {
  var fechaFinal;
  fechaFinal = fAumentarDias($("#noches").val(), $("#fechaEntrada").val());
  
  $("#fechaSalida").val(fechaFinal);
} //Fin fCambioNoches()


/*
function fAumentarDias(dias, fechaOrigen) {



  var date2;
  var aux_fecha_recogida, aux_dia, aux_mes, aux_year;
  aux_fecha_recogida = fechaOrigen;
  aux_dia = aux_fecha_recogida.substr(0, 2);
  aux_mes = aux_fecha_recogida.substr(3, 2);
  aux_year = aux_fecha_recogida.substr(6, 4);

  date2 = new Date();  
  date2.setMonth(aux_mes - 1);
  date2.setFullYear(aux_year);
  date2.setDate(aux_dia);

  var date3;
  date3 = new Date(date2.getTime() + (dias * 24 * 60 * 60 * 1000));  
  aux = eval(date3.getMonth() + 1);
  return (fFormatoFecha(date3.getDate().toString()) + '/' + fFormatoFecha(aux.toString()) + '/' + date3.getUTCFullYear());
} //Fin fAumentarDias(dias, fechaOrigen)

*/

function fFormatoFecha(fecha) {

	if (fecha.length == 1)
	{
		return '0' + fecha; 
	}
	else
	{
		return fecha; 
	}
} //Fin fFormatoFecha(fecha)

function fPulsadoFechaSalida() {

  //Comprueba que la fecha de entrada no sea menor que la fecha actual	
  var cad = "";
  var f_entrada = new Date($("#fechaEntrada").val().substr(6, 4), $("#fechaEntrada").val().substr(3, 2) - 1, $("#fechaEntrada").val().substr(0, 2));
  var f_salida = new Date($("#fechaSalida").val().substr(6, 4), $("#fechaSalida").val().substr(3, 2) - 1, $("#fechaSalida").val().substr(0, 2));
  var f_actual = new Date();
  if (f_entrada < f_actual)
  {
    switch ($("#idioma").val()) {
      case "ES":
        cad = cad + '- La fecha de entrada debe de ser posterior a la fecha actual.\n';
        break;
      case "DE":
        cad = cad + '- Das Eincheck Datum muss nach dem aktuellem Datum sein.\n';
        break;
      case "FR":
        cad = cad + '- La date d\'entree doit etre posterieure a la date du jour.\n';
        break;
      case "IT":
        cad = cad + '- The arrival date should be later than today\'s date.\n';
        break;
      case "NL":
        cad = cad + '- The arrival date should be later than today\'s date.\n';
        break;
      default:
        cad = cad + '- The arrival date should be later than today\'s date.\n';
        break;
    }    
  }
  //Comprueba que la fecha de entrada no sea mayor que la fecha salida	
  if (f_entrada >= f_salida){
    switch ($("#idioma").val()) {
      case "ES":
        cad = cad + '- La fecha salida debe de ser posterior a la fecha de entrada.\n';
        break;
      case "DE":
        cad = cad + '- Das Auscheck Datum muss nach dem Eincheck Datum sein.\n';
        break;
      case "FR":
        cad = cad + '- La date de depart doit etre postérieure a la date d\'entree.\n';
        break;
      case "IT":
        cad = cad + '- The departure date should be later than the arrival date.\n';
        break;
      case "NL":
        cad = cad + '- The departure date should be later than the arrival date.\n';
        break;
      default:
        cad = cad + '- The departure date should be later than the arrival date.\n';
        break;
    }    
	}  	   
  
  /* Calcular las noches y mostrarlas */
  var MSxDia;
  var dias;
  MSxDia=24*60*60*1000; //milisegundos que tiene un dia
	dias = (f_salida.getTime() - f_entrada.getTime()) / MSxDia;
	dias = Math.floor(dias);
	
	if (dias > 30)
	  dias = 31;
	  
	document.formulario_reserva.noches.value = dias;
} //FinfPulsadoFechaSalida()


//Llamadas JQUERY para calendario
$(document).ready(
  function() {

    $('#fechaEntrada').datepicker('option', $.datepicker.regional[$('#idioma').val().toLowerCase()]);
    $('#fechaSalida').datepicker('option', $.datepicker.regional[$('#idioma').val().toLowerCase()]);



    // Datepicker Fecha Entrada
    $('#fechaEntrada').datepicker(
				  {
				    showOn: 'both',
				    buttonImageOnly: true,
				    buttonImage: 'http://www.doyouspain.com/imagenes/calendario_d.jpg',
				    showButtonPanel: true,
				    numberOfMonths: 1,
				    dateFormat: 'dd/mm/yy',
				    firstDay: 1,
				    minDate: '+0d',
				    showAnim: '',
				    closeText: 'X',
				    duration: 'fast',
				    showOtherMonths: true,
				    selectOtherMonths: true,
				    currentText: '',
				    onSelect: function(dateText, inst) {
				      //$('#fechaEntrada').val(dateText);
				      var fechaAumentada;
				      fechaAumentada = fAumentarDias($('#noches').val(), dateText);
				      $('#fechaSalida').val(fechaAumentada);
				      var fechaMassUnDia;
				      fechaMassUnDia = fAumentarDias(1, dateText)
				      $('#fechaSalida').datepicker('option', 'minDate', new Date(fechaMassUnDia.substr(6, 4), fechaMassUnDia.substr(3, 2) - 1, fechaMassUnDia.substr(0, 2)));
				    }
				  });

    // Datepicker Fecha Salida
    $('#fechaSalida').datepicker(
				  {
				    showOn: 'both',
				    buttonImageOnly: true,
				    buttonImage: 'http://www.doyouspain.com/imagenes/calendario_d.jpg',
				    showButtonPanel: true,
				    numberOfMonths: 1,
				    dateFormat: 'dd/mm/yy',
				    firstDay: 1,
				    minDate: '+1d',
				    showAnim: '',
				    closeText: 'X',
				    showOtherMonths: true,
				    selectOtherMonths: true,
				    onSelect: function(dateText, inst) {
				      //$('#fechaSalida').val(dateText);
				      fPulsadoFechaSalida();
				    }
				  });


    $('#fechaEntrada').datepicker('setDate', new Date());
    $('#fechaSalida').datepicker('setDate', +1);

    if ($('#idioma').val().toLowerCase() == 'nl') {
      $('#fechaEntrada').datepicker('option', 'nextText', '>>');
      $('#fechaSalida').datepicker('option', 'nextText', '>>');
      $('#fechaEntrada').datepicker('option', 'prevText', '\<\<');
      $('#fechaSalida').datepicker('option', 'prevText', '\<\<');
    }
    

    if ((window.location.host.toLowerCase() == "www.doyouspain.com") || (window.location.host.toLowerCase() == "alberghi.doyouspain.com") || (window.location.host.toLowerCase() == "chambres.doyouspain.com") || (window.location.host.toLowerCase() == "kamers.doyouspain.com") || (window.location.host.indexOf("desarrollo") != -1) ||
       ((window.location.host.toLowerCase() == "hoteles.doyouspain.com") && ((window.location.pathname.toLowerCase() == "/") || (window.location.pathname.toLowerCase() == "/index.htm") || (window.location.pathname.toLowerCase() == "/quote.htm"))) ||
       ((window.location.host.toLowerCase() == "hotels.doyouspain.com") && ((window.location.pathname.toLowerCase() == "/") || (window.location.pathname.toLowerCase() == "/index.htm") || (window.location.pathname.toLowerCase() == "/quote.htm"))) ||
       ((window.location.host.toLowerCase() == "betten.doyouspain.com") && ((window.location.pathname.toLowerCase() == "/") || (window.location.pathname.toLowerCase() == "/index.htm") || (window.location.pathname.toLowerCase() == "/quote.htm")))
       ) {
      //AUTOCOMPLETAR DEL FORMULARIO DESTINO
      //var lPrimeraVezMostradoAutocomplete;
      $("#destino").autocomplete({
        source: "/hotels/ajax_autocomplete.asp?idioma=" + idioma + "&pais=" + $('#pais').val(),
        minLength: 2,
        close: function(event, ui) {
          document.getElementById('noches').style.visibility = 'visible';
          document.getElementById('numHabitaciones').style.visibility = 'visible';
          document.getElementById('habitacion1numAdultos_AUX').style.visibility = 'visible';
        },
        open: function(event, ui) {
          if ($.browser.msie && $.browser.version <= 7) {
            document.getElementById('noches').style.visibility = 'hidden';
            document.getElementById('numHabitaciones').style.visibility = 'hidden';
            document.getElementById('habitacion1numAdultos_AUX').style.visibility = 'hidden';
          }
        },
        select: function(event, ui) {
          /*
          alert(ui.item.value);
          alert(ui.item.id);
          alert(ui.item.label);
          alert(ui.item.koko);
            
          */
        }
      });
    }
  }
);
	//Fin llamadas JQUERY para calendario

	//Si se pulsa Enter que haga el click
  function fTeclaEnter(e) {   
	  var tecla;
	  tecla = (document.all) ? e.keyCode : e.which;

	  if (tecla == 13) {	    
	    //$("#btnBuscar").click();
	    return false;
	  }
	  return true;
	} //Fin fTeclaEnter(e)
	



//SUMAR DIAS A FECHA
	var aFinMes = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

	function finMes(nMes, nAno) {
	  return aFinMes[nMes - 1] + (((nMes == 2) && (nAno % 4) == 0) ? 1 : 0);
	}

	function padNmb(nStr, nLen, sChr) {
	  var sRes = String(nStr);
	  for (var i = 0; i < nLen - String(nStr).length; i++)
	    sRes = sChr + sRes;
	  return sRes;
	}

	function makeDateFormat(nDay, nMonth, nYear) {
	  var sRes;
	  sRes = padNmb(nDay, 2, "0") + "/" + padNmb(nMonth, 2, "0") + "/" + padNmb(nYear, 4, "0");
	  return sRes;
	}

	function incDate(sFec0) {
	  var nDia = parseInt(sFec0.substr(0, 2), 10);
	  var nMes = parseInt(sFec0.substr(3, 2), 10);
	  var nAno = parseInt(sFec0.substr(6, 4), 10);
	  nDia += 1;
	  if (nDia > finMes(nMes, nAno)) {
	    nDia = 1;
	    nMes += 1;
	    if (nMes == 13) {
	      nMes = 1;
	      nAno += 1;
	    }
	  }
	  return makeDateFormat(nDia, nMes, nAno);
	}

	function decDate(sFec0) {
	  var nDia = Number(sFec0.substr(0, 2));
	  var nMes = Number(sFec0.substr(3, 2));
	  var nAno = Number(sFec0.substr(6, 4));
	  nDia -= 1;
	  if (nDia == 0) {
	    nMes -= 1;
	    if (nMes == 0) {
	      nMes = 12;
	      nAno -= 1;
	    }
	    nDia = finMes(nMes, nAno);
	  }
	  return makeDateFormat(nDia, nMes, nAno);
	}

	function fAumentarDias(sInc, sFec0) {
	  var nInc = Math.abs(parseInt(sInc));
	  var sRes = sFec0;
	  if (parseInt(sInc) >= 0)
	    for (var i = 0; i < nInc; i++) sRes = incDate(sRes);
	  else
	    for (var i = 0; i < nInc; i++) sRes = decDate(sRes);
	  return sRes;
	}
	//FIN SUMAR DIAS A FECHA

