var universe = $("#ez_informations").children("#universe").html();
var updateMenuExtension = $("#ez_informations").children("#updateMenuExtension").html();
var defaultTranslationValues = new Array();
defaultTranslationValues['destination'] = $("#ez_informations").children("#defaultTranslationValuesDestination").html();
defaultTranslationValues['residence'] = $("#ez_informations").children("#defaultTranslationValuesResidence").html();
var defaultAltTranslationValues = new Array();
defaultAltTranslationValues['residence'] = $("#ez_informations").children("#defaultAltTranslationValuesResidence").html();
var selectedValues = new Array();
if($("#ez_informations").children("#SelectedValuesDestination").length > 0) selectedValues['destination'] = $("#ez_informations").children("#SelectedValuesDestination").html();
if($("#ez_informations").children("#SelectedValuesResidence").length > 0) selectedValues['residence'] = $("#ez_informations").children("#SelectedValuesResidence").html();

$(document).ready
(
	function()
	{
		updateMenu( new Array( 'destination', 'residence' ), universe, 'initMenu' );
		stayLength();
	}
);

function updateMenu( type, universe, id )
{
	$.each( type, function( indexType )
	{
		var currentType = type[indexType];
		var currentTypeId = "#" +currentType;
		if( id != '' )
		{
			$.getJSON( updateMenuExtension + currentType + '/' +universe + '/' + id,function( data )
			{
				if( selectedValues[currentType] || ( data.length == 1 ) )
				{
					if( currentType == 'residence' )
						$( currentTypeId ).empty().append( $( "<option/>" ).attr( "value", "allMenu" ).html( defaultAltTranslationValues[currentType] ) );
					else
						$( currentTypeId ).empty().append( $( "<option/>" ).attr( "value", "chooseMenu" ).html( defaultTranslationValues[currentType] ) );
				}
				else
					$( currentTypeId ).empty().append( $( "<option selected=\"selected\"/>" ).attr( "value", "chooseMenu" ).html( defaultTranslationValues[currentType] ) );

				$.each( data, function( indexData )
				{
					if( ( selectedValues[currentType] && ( data[indexData].id.indexOf( selectedValues[currentType] ) == 0 ) ) || ( data.length == 1 ) )
					{
						$( currentTypeId ).append( $( "<option/>" ).attr( "value", data[indexData].id ).attr( "selected", "selected" ).html( data[indexData].label ) ).removeAttr( "disabled" );
						selectedValues[currentType] = false;

						if( ( data.length > 1 ) && ( id != 'initMenu' ) )
						{
							if( currentType == 'destination' )
								updateMenu( new Array( 'residence' ), universe, data[indexData].id );
							else if( currentType == 'residence' )
								updateDestination( universe, data[indexData].id );
						}
					}
					else
						$( currentTypeId ).append( $( "<option/>" ).attr( "value", data[indexData].id ).html( data[indexData].label ) ).removeAttr( "disabled" );
				});
			});
		}
		else
			$( currentTypeId ).empty().append( $( "<option selected='selected'/>" ).attr( "value", "" ).html( defaultTranslationValues[currentType] ) ).attr( "disabled", "disabled" );
	});
}

function updateDestination( universe, id )
{
	var idArray = id.split( '|' );
	var destOID = 0
	if( idArray[2] ) destOID = idArray[2];
	$("#destination option").each
	(
		function(i)
		{
			var valArray = $(this).val().split( '|' );
			if ( destOID == valArray[1] ) {
				$("#destination").val( $(this).val() );
			}
		}
	);
	if ( idArray[0] == 'allMenu' )
		updateMenu( new Array( 'residence' ), universe, 'initMenu' );
}

function updateStayType( universe )
{
	var idArray = $('#residence').val().split( '|' );
	var duration = getDuration();
	if( ( idArray.length > 0 ) && (!isNaN( parseInt( idArray[0] ) ) ) && duration )
	{
		$.getJSON(  updateMenuExtension + 'staytype/' + universe + '/' + idArray[0] + '/' + duration, function( data )
		{
				$.each( data, function( indexData )
				{
					if( ( data.length > 0 ) && data[indexData] && data[indexData].label )
		    			$('#staylength').html( $("#ez_informations").children("#staylengthlabel").html() + " <strong>" + data[indexData].label + "</strong>" );
					else
						$('#staylength').html( '' );
				});
		});
	}
	else
		$('#staylength').html( '' );
}

function getDuration()
{
	// Date processing
	var formatDateFrom = $('#dateFrom').val();
	var timestampDateFrom = 0;
	var dateFrom = formatDateFrom.split( '/' );
	if ((dateFrom.length == 3) && (dateFrom[2].length == 4))
		formatDateFrom = dateFrom[2] + '/' + dateFrom[1] + '/' + dateFrom[0];
	if (Date.parse( formatDateFrom ) > 0)
		timestampDateFrom = Date.parse( formatDateFrom )/1000.0;
	var formatDateTo = $('#dateTo').val();
	var timestampDateTo = 0;
	var dateTo = formatDateTo.split( '/' );
	if ((dateTo.length == 3) && (dateTo[2].length == 4))
		formatDateTo = dateTo[2] + '/' + dateTo[1] + '/' + dateTo[0];
	if (Date.parse( formatDateTo ) > 0)
		timestampDateTo = Date.parse(formatDateTo)/1000.0;
	if ((timestampDateFrom > 0) && (timestampDateTo > 0) && (timestampDateFrom < timestampDateTo))
		return Math.floor( (timestampDateTo - timestampDateFrom) / 86400 );
	else
		return 0;
}

function stayLength()
{
	var duration = getDuration();
	if( duration )
		$('#nbnights').html( '' + duration );
	else
		$('#nbnights').html( "" );
}

function checkAndSend_1()
{
	// No destination, no residence : can't validate			
	if( ( $('#destination').val().indexOf( '|' ) < 0 ) && ( $('#residence').val().indexOf( '|' ) < 0 ) )
	{
		alert($("#ez_informations").children("#alert_1").html());
		$('#destination').css({'background-color':'#741b3b'});
		$('#destination').css({'color':'#FFFFFF'});
		$('#residence').css({'background-color':'#741b3b'});
		$('#residence').css({'color':'#FFFFFF'});
		$('#destination').focus();
		return false;
	}
	else
	{
		// Date processing
		var formatDateFrom = $('#dateFrom').val();
		var timestampDateFrom = 0;
		var dateFrom = formatDateFrom.split( '/' );
		if( ( dateFrom.length == 3 ) && ( dateFrom[2].length == 4 ) )
		{
			formatDateFrom = dateFrom[2] + '/' + dateFrom[1] + '/' + dateFrom[0];
		}
		else if( ( formatDateFrom != '' ) && ( dateFrom.length != 3 ) )
		{
			alert($("#ez_informations").children("#alert_2").html());
			$('#dateFrom').css({'background-color':'#741b3b'});
			$('#dateFrom').css({'color':'#FFFFFF'});
			$('#dateFrom').focus();
			return false;
		}
		if( Date.parse( formatDateFrom ) > 0 ) timestampDateFrom = Date.parse( formatDateFrom )/1000.0;

		var formatDateTo = $('#dateTo').val();
		var timestampDateTo = 0;
		var dateTo = formatDateTo.split( '/' );
		if( ( dateTo.length == 3 ) && ( dateTo[2].length == 4 ) )
		{
			formatDateTo = dateTo[2] + '/' + dateTo[1] + '/' + dateTo[0];
		}
		else if( ( formatDateTo != '' ) && ( dateTo.length != 3 ) )
		{
			alert($("#ez_informations").children("#alert_2").html());
			$('#dateTo').css({'background-color':'#741b3b'});
			$('#dateTo').css({'color':'#FFFFFF'});
			$('#dateTo').focus();
			return false;
		}
		if( Date.parse( formatDateTo ) > 0 ) timestampDateTo = Date.parse( formatDateTo )/1000.0;

		// Date From is later than Date To
		if( ( timestampDateFrom > 0 ) && ( timestampDateTo > 0 ) && ( timestampDateFrom >= timestampDateTo ) )
		{
			alert($("#ez_informations").children("#alert_3").html());
			$('#dateFrom').css({'background-color':'#741b3b'});
			$('#dateFrom').css({'color':'#FFFFFF'});
			$('#dateTo').css({'background-color':'#741b3b'});
			$('#dateTo').css({'color':'#FFFFFF'});
			$('#dateFrom').focus();
			return false;
		}
		
		var params_1 = new Array();
		var params_2 = new Array();

		if( $('#destination').val().indexOf( '|' ) >= 0 )
		{
			var destination_id = $('#destination').val().split( '|' );
			params_1[params_1.length] = destination_id[destination_id.length-1];
		}

		if( $('#residence').val().indexOf( '|' ) >= 0 )
		{
			var residence_id = $('#residence').val().split( '|' );
			params_1[params_1.length] = residence_id[residence_id.length-2];
		}

		if( $('#nb').val() > 1 ) params_2[params_2.length] = $('#nb').val() + '-' + $("#ez_informations").children("#trad_1").html();
		else params_2[params_2.length] = $('#nb').val() + '-' + $("#ez_informations").children("#trad_2").html();
		if( timestampDateFrom > 0 ) params_2[params_2.length] = $("#ez_informations").children("#trad_3").html() + '=' + timestampDateFrom;
		if( timestampDateTo > 0 ) params_2[params_2.length] = $("#ez_informations").children("#trad_4").html() + '=' + timestampDateTo;

		switch ($("#ez_informations").children("#current_access").html())
		{
			case 'fo_ash_fr':
				window.location.href = '/recherche-dispo/' + params_1.join( '_' ) + '&' + params_2.join( '_' );
			break;
			case 'fo_ash_en':
				window.location.href = '/availability/' + params_1.join( '_' ) + '&' + params_2.join( '_' );
			break;
			case fo_ash_es:
				window.location.href = '/disponibilidad/' + params_1.join( '_' ) + '&' + params_2.join( '_' );
			break;
			default: 
				window.location.href = '/recherche-dispo/' + params_1.join( '_' ) + '&' + params_2.join( '_' );
			break;
		}		

		return false;
	}
}

function checkAndSend_2( univ )
{
	if( $("#recactivity input:radio:checked").val() )
		window.location.href = $("#recactivity input:radio:checked").val();
	else
	{
		if( univ == 'professionnel' )
			alert($("#ez_informations").children("#alert_4").html());
		else
			alert($("#ez_informations").children("#alert_5").html());
	}
	return false;
}


