/*--------------------------------------------------------------------------*  
 *	Funciones utilizadas en el index										*
 *--------------------------------------------------------------------------*/

var cal1;
var over_cal = false;
var cal2;
var over_cal2 = false;
function init() {
    cal1 = new YAHOO.widget.Calendar("cal1","cal1Container");
    cal1.selectEvent.subscribe(getDate, cal1, true);
    cal1.renderEvent.subscribe(setupListeners, cal1, true);
    YAHOO.util.Dom.setStyle('cal1Container', 'display', 'none');
    YAHOO.util.Event.addListener('cal1Date', 'blur', hideCal);
    YAHOO.util.Event.addListener('cal1Date', 'focus', showCal);
    cal1.render();

    cal2 = new YAHOO.widget.Calendar("cal2","cal2Container");
    cal2.selectEvent.subscribe(getDate2, cal2, true);
    cal2.renderEvent.subscribe(setupListeners2, cal2, true);
    YAHOO.util.Dom.setStyle('cal2Container', 'display', 'none');
    YAHOO.util.Event.addListener('cal2Date', 'blur', hideCal2);
    YAHOO.util.Event.addListener('cal2Date', 'focus', showCal2);
    cal2.render();
}
function setupListeners() {
    YAHOO.util.Event.addListener('cal1Container', 'mouseover', overCal);
    YAHOO.util.Event.addListener('cal1Container', 'mouseout', outCal);
}
function getDate() {
        var calDate = this.getSelectedDates()[0];
		if (calDate.getDate()<10){dia= "0"+calDate.getDate();} else {dia=calDate.getDate();}
		if ((calDate.getMonth() + 1)<10){mes= "0"+(calDate.getMonth() + 1);} else {mes=calDate.getMonth() + 1;}
        calDate = dia + '/' + mes + '/' + calDate.getFullYear();
        YAHOO.util.Dom.get('cal1Date').value = calDate;
        over_cal = false;
        hideCal();
}
function showCal() {
    var xy = YAHOO.util.Dom.getXY('cal1Date');
	var date = $('cal1Date').value.substring(3,5)+"/"+$('cal1Date').value.substring(0,2)+"/"+$('cal1Date').value.substring(6,10);
    //var date = YAHOO.util.Dom.get('cal1Date').value;
    if (date) {
        cal1.cfg.setProperty('selected', date);
        cal1.cfg.setProperty('pagedate', new Date(date), true);
        cal1.render();
    }
    YAHOO.util.Dom.setStyle('cal1Container', 'display', 'block');
    xy[1] = xy[1] + 20;
    YAHOO.util.Dom.setXY('cal1Container', xy);
}
function hideCal() {
    if (!over_cal) {
        YAHOO.util.Dom.setStyle('cal1Container', 'display', 'none');
    }
}
function overCal() {
    over_cal = true;
}
function outCal() {
    over_cal = false;
}
 
function setupListeners2() {
    YAHOO.util.Event.addListener('cal2Container', 'mouseover', overCal2);
    YAHOO.util.Event.addListener('cal2Container', 'mouseout', outCal2);
}
function getDate2() {
        var calDate = this.getSelectedDates()[0];
		if (calDate.getDate()<10){dia= "0"+calDate.getDate();} else {dia=calDate.getDate();}
		if ((calDate.getMonth() + 1)<10){mes= "0"+(calDate.getMonth() + 1);} else {mes=calDate.getMonth() + 1;}
        calDate = dia + '/' + mes + '/' + calDate.getFullYear();
        YAHOO.util.Dom.get('cal2Date').value = calDate;
        over_cal2 = false;
        hideCal2();
}
function showCal2() {
    var xy = YAHOO.util.Dom.getXY('cal2Date');
	var date = $('cal2Date').value.substring(3,5)+"/"+$('cal2Date').value.substring(0,2)+"/"+$('cal2Date').value.substring(6,10);
    //var date = YAHOO.util.Dom.get('cal1Date').value;
    if (date) {
        cal2.cfg.setProperty('selected', date);
        cal2.cfg.setProperty('pagedate', new Date(date), true);
        cal2.render();
    }
    YAHOO.util.Dom.setStyle('cal2Container', 'display', 'block');
    xy[1] = xy[1] + 20;
    YAHOO.util.Dom.setXY('cal2Container', xy);
}
function hideCal2() {
    if (!over_cal2) {
        YAHOO.util.Dom.setStyle('cal2Container', 'display', 'none');
    }
}
function overCal2() {
    over_cal2 = true;
}
function outCal2() {
    over_cal2 = false;
}

YAHOO.util.Event.addListener(window, 'load', init);

/* -------------------------------------------------- formulario ----------------------------------------------------*/ 

function inicializaMensaje(){
	//$("msg-").hide();
	//$("msg-").hide();
	$("msg-cal1Date").hide();
	$("msg-cal2Date").hide();
}

function checkCampos () {
	inicializaMensaje();
	var fin=1;
	if (! esFechaValida("cal1Date","La fecha no es válida","msg-cal1Date")){
		fin=fin*0;
	}
	if (! esFechaValida("cal2Date","La fecha no es válida","msg-cal2Date")){
		fin=fin*0;
	}
	if (esMenor("cal1Date","cal1Date",1))
	{
		muestraMensaje("msg-cal1Date","La fecha no puede ser anterior al día actual");
		fin=fin*0;
	}
	if (esMenor("cal1Date","cal2Date",0))
	{
		muestraMensaje("msg-cal2Date","La fecha de fín no puede ser anterior a la fecha de inicio");
		fin=fin*0;
	}

	return fin;
}

function obtenerCoordenadas () {
	if (checkCampos ()) {
		var loc= $F("loc");
		var cp= $F("cp");
		if ((loc) || (cp))
		{	
			loc=cp + " " + loc + ", España";
			showAddress(loc);
		} else {
			$("searchform").submit();
		}
	}else{
		alert ("Revisa los valores de la configuración de la búsqueda");
	}
}

var geocoder = new GClientGeocoder();

function showAddress(address) {
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " no encontrado");
		return 0;
      } else {
			// Hacer aquí el submit
			var cor = "" + point + "";
			var cor = cor.replace(/\./g,"z");

			$("cor").value=cor;
			$("searchform").submit();

			return 1;
      }
    }
  );
}