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' );
		updateStayLength();
	}
);



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] ) );
                        // updateMenu( new Array( 'residence' ), universe, data[0].id );
                    }
				}
				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 updateStayLength()
{
    // handle Date From
    var formatDateFrom = $('#dateFrom').val();
    var timestampDateFrom = 0;
    var dateFrom = formatDateFrom.split( '/' );
    // handle french / english date format
    if( ( dateFrom.length == 3 ) && ( dateFrom[2].length == 4 ) ) formatDateFrom = dateFrom[2] + '/' + dateFrom[1] + '/' + dateFrom[0];
    else if( ( dateFrom.length == 3 ) && ( dateFrom[0].length == 4 ) ) formatDateFrom = dateFrom[0] + '/' + dateFrom[1] + '/' + dateFrom[2];
    // get timestamp
    if( Date.parse( formatDateFrom ) > 0 )
        timestampDateFrom = Date.parse( formatDateFrom )/1000.0 + 43200;

    // handle Date To
    var formatDateTo = $('#dateTo').val();
    var timestampDateTo = 0;
    var dateTo = formatDateTo.split( '/' );
    // handle french / english date format
    if( ( dateTo.length == 3 ) && ( dateTo[2].length == 4 ) )
        formatDateTo = dateTo[2] + '/' + dateTo[1] + '/' + dateTo[0];
    else if( ( dateTo.length == 3 ) && ( dateTo[0].length == 4 ) )
        formatDateTo = dateTo[0] + '/' + dateTo[1] + '/' + dateTo[2];
    // get timestamp
    if( Date.parse( formatDateTo ) > 0 )
        timestampDateTo = Date.parse( formatDateTo )/1000.0 + 43200;

    // get diff
    if( ( timestampDateFrom > 0 ) && ( timestampDateTo > 0 ) && ( timestampDateFrom < timestampDateTo ) )
        $('#nbnights').val( '' + Math.floor( (timestampDateTo - timestampDateFrom) / 86400 ) );
    else
        $('#nbnights').val( "" );
    
}

function updateDateTo()
{
    // handle Date From
    var formatDateFrom = $('#dateFrom').val();
    var timestampDateFrom = 0;
    var dateFrom = formatDateFrom.split( '/' );
    // handle french / english date format
    if( ( dateFrom.length == 3 ) && ( dateFrom[2].length == 4 ) ) formatDateFrom = dateFrom[2] + '/' + dateFrom[1] + '/' + dateFrom[0];
    else if( ( dateFrom.length == 3 ) && ( dateFrom[0].length == 4 ) ) formatDateFrom = dateFrom[0] + '/' + dateFrom[1] + '/' + dateFrom[2];
    // get timestamp
    if( Date.parse( formatDateFrom ) > 0 )
        timestampDateFrom = Date.parse( formatDateFrom )/1000.0 + 43200;

    // handle Nb nights
    var nbnights = 0;
    if( parseInt( $('#nbnights').val() ) > 0 )
        nbnights = $('#nbnights').val();

    // get new Date To
    if( ( timestampDateFrom > 0 ) && ( nbnights > 0 ) )
    {
        var timestampDateTo = timestampDateFrom + nbnights * 86400;
        var dateTo = new Date( ( timestampDateTo - 43200 ) * 1000 );
        var month = ( ( dateTo.getMonth() + 1 ) < 10 ) ? '0' + ( dateTo.getMonth() + 1 ) : ( dateTo.getMonth() + 1 );
        var day = ( dateTo.getDate() < 10 ) ? '0' + dateTo.getDate() : dateTo.getDate();
        if( ( dateFrom.length == 3 ) && ( dateFrom[2].length == 4 ) ) 
            $('#dateTo').val( day + '/' + month + '/' + dateTo.getFullYear() );
        else if ( ( dateFrom.length == 3 ) && ( dateFrom[0].length == 4 ) ) 
            $('#dateTo').val( dateTo.getFullYear() + '/' + month + '/' + day );
    }
}

function checkAndSend()
{
	// 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'});
		$('#residence').css({'background-color':'#741b3b'});
		$('#destination').focus();
		return false;
	}
	else
	{
		// Date processing
		var stamp = new Date();
		var timestampDateToday = stamp.getFullYear()+"/"+(stamp.getMonth()+1)+"/"+stamp.getDate();
		timestampDateToday = Date.parse(timestampDateToday )/1000;
		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').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').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 )) || ( ( timestampDateFrom > 0 ) && (timestampDateFrom < timestampDateToday) ) )
		{
			alert($("#ez_informations").children("#alert_3").html());
			$('#dateFrom').css({'background-color':'#741b3b'});
			$('#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 checkAndBook()
{
    // No residence : can't validate            
    if( $('#residence').val().indexOf( '|' ) < 0 )
    {
        alert($("#ez_informations").children("#alert_1").html());
        $('#residence').css({'background-color':'#741b3b'});
        $('#residence').focus();
        return false;
    }
    else
    {
        // Date processing
        var stamp = new Date();
        var timestampDateToday = stamp.getFullYear()+"/"+(stamp.getMonth()+1)+"/"+stamp.getDate();
        timestampDateToday = Date.parse(timestampDateToday )/1000;
        
        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').focus();
            return false;
        }
        
        if( isNaN( Date.parse( formatDateFrom ) ) )
        {
	        alert($("#ez_informations").children("#alert_4").html());
	        $('#dateFrom').css({'background-color':'#741b3b'});
	        $('#dateFrom').focus();
            return false;
        }
        else
        {
            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').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 )) || ( ( timestampDateFrom > 0 ) && (timestampDateFrom < timestampDateToday) ) )
	        {
	            alert($("#ez_informations").children("#alert_3").html());
	            $('#dateFrom').css({'background-color':'#741b3b'});
	            $('#dateFrom').focus();
	            return false;
	        }
	        
            // Now construct RIT request
            var params = new Array();
            var residence_id = $('#residence').val().split( '|' );
            
            // hotelid
            params[params.length] = 'hotelid='+residence_id[residence_id.length-1];
            // nbpax
            params[params.length] = 'nbpax='+$('#nb').val();
            // day, month, year
            if( ( dateFrom.length == 3 ) && ( dateFrom[2].length == 4 ) )
            {
                params[params.length] = 'fday='+dateFrom[0];
                params[params.length] = 'fmonth='+dateFrom[1];
                params[params.length] = 'fyear='+dateFrom[2];
            }
            else if( ( dateFrom.length == 3 ) && ( dateFrom[0].length == 4 ) )
            {
                params[params.length] = 'fday='+dateFrom[2];
                params[params.length] = 'fmonth='+dateFrom[1];
                params[params.length] = 'fyear='+dateFrom[0];
            }
            // nbnights
            if( parseInt( $('#nbnights').val() ) > 0 )
                params[params.length] = 'numnight='+$('#nbnights').val();
    
            window.location.href = $("#ez_informations").children("#rit_base_url").text() + '&' + params.join( '&' );

	        return false;
        }
    }
}
