
var numberMaxDate = setDateMaxCalendar();

$(function()
{
	setDateFlight_1();
	setDateFlight_2();
	
	setDateComboBox();
}); 
 
 var dayInt;
 var monthInt;
 var yearInt;


function minDateComeBack()
{
	if($("input#date_picker_id_1").attr('value') != "")
	{
		var monthString = $("input#date_picker_id_1").attr('value').substring(0,2);
		var dayString 	= $("input#date_picker_id_1").attr('value').substring(3,5);
		var yearString 	= $("input#date_picker_id_1").attr('value').substring(6);
		dayInt	 = parseInt(dayString);
		monthInt = parseInt(monthString);
		yearInt	 = parseInt(yearString);
		
		return true;
	}
	else
	{
		return false;
	}
}

var monthSelected;
var daySelected  ;
var yearSelected ;
	
function setDateComboBox1(date)
{
    if(date!='' && date!="" && date != null)
    {
	    monthSelected = $("input#date_picker_id_1").attr('value').substring(0,2);
	    daySelected   = $("input#date_picker_id_1").attr('value').substring(3,5);
	    yearSelected  = $("input#date_picker_id_1").attr('value').substring(6);

        $('div#marketDate_1 div.marketDropdown select.wSml1s option').removeAttr('selected');
	    $('div#marketDate_1 div.marketDropdown select.wSml1s option[value='+daySelected+']').eq(0).attr('selected',' ');
	    $('div#marketDate_1 p select.wSml1s option').removeAttr('selected');
	    $('div#marketDate_1 p select.wSml1s option[value='+daySelected+']').eq(0).attr('selected',' ');

	    var stringValueYear = yearSelected+'-'+monthSelected;
    	
	    $('div#marketDate_1 div.marketDropdown select.wLrgs option').removeAttr('selected');
	    $('div#marketDate_1 div.marketDropdown select.wLrgs option[value='+stringValueYear+']').eq(0).attr('selected',' ');
    	
    	if(changeDateComingBach())
    	{   
    	
	        $('div#marketDate_2 div.marketDropdown select.wSml1s option').removeAttr('selected');
	        $('div#marketDate_2 div.marketDropdown select.wSml1s option[value='+daySelected+']').eq(0).attr('selected',' ');
        		
	        var stringValueYear = yearSelected+'-'+monthSelected;
	        $('div#marketDate_2 div.marketDropdown select.wLrgs option').removeAttr('selected'); 
	        $('div#marketDate_2 div.marketDropdown select.wLrgs option[value='+stringValueYear+']').eq(0).attr('selected',' ');
    	}
    	
    	$("input#date_picker_id_2").datepicker('destroy');
        $("input#date_picker_id_2").datepicker('enable');
        
	    setDateFlight_ComeBack(yearSelected, monthSelected-1, daySelected, "input#date_picker_id_2");
	}
}

function setDateComboBox2(date)
{
    //if(date!='' || date!=null)
    if(date!="")
    {
	    monthSelected = $("input#date_picker_id_2").attr('value').substring(0,2);
	    daySelected   = $("input#date_picker_id_2").attr('value').substring(3,5);
	    yearSelected  = $("input#date_picker_id_2").attr('value').substring(6);

	    $('div#marketDate_2 div.marketDropdown select.wSml1s option').removeAttr('selected');
	    $('div#marketDate_2 div.marketDropdown select.wSml1s option[value='+daySelected+']').eq(0).attr('selected',' ');

	    var stringValueYear = yearSelected+'-'+monthSelected;
    	
	    $('div#marketDate_2 div.marketDropdown select.wLrgs option').removeAttr('selected');
	    $('div#marketDate_2 div.marketDropdown select.wLrgs option[value='+stringValueYear+']').eq(0).attr('selected',' ');
	}
}

function setDateFlight_1()
{
	var date;

	$("#date_picker_id_1").datepicker(
	{
		  minDate: 0,
		  maxDate:numberMaxDate,
		  numberOfMonths: 2,
		  showOn: 'button',
		  buttonImageOnly: true,
		  buttonImage: 'js/Azul/Calendar/images/calendar.gif',
		  
		  onClose:(function(date) {setDateComboBox1(date);})
	});
}

function setDateFlight_2()
{
    if(minDateComeBack())
    { 
		$("input#date_picker_id_2").datepicker(
		{
			minDate: (new Date(yearInt, monthInt, dayInt)),
			maxDate:numberMaxDate,
			numberOfMonths: 2,
			showOn: 'button',
			buttonImageOnly: true,
			buttonImage: 'js/Azul/Calendar/images/calendar.gif', 
			
			onClose:(function(date) {setDateComboBox2(date);})
	    });
	}
	else
	{
		$("input#date_picker_id_2").datepicker(
		{
			minDate: (new Date()),
			maxDate:numberMaxDate,
			numberOfMonths: 2,
			showOn: 'button',
			buttonImageOnly: true,
			buttonImage: 'js/Azul/Calendar/images/calendar.gif', 
			onClose:(function(date) {setDateComboBox2(date);})
        });
	}
}


function setDateFlight_ComeBack(setYear, setMonth, setDay, input)
{
		$(""+input+"").datepicker(
		{
			minDate: (new Date(setYear, setMonth, setDay)),
			numberOfMonths: 2,
			maxDate:numberMaxDate,
			showOn: 'button',
			buttonImageOnly: true,
			buttonImage: 'js/Azul/Calendar/images/calendar.gif', 

			onClose:(function(date) {setDateComboBox2(date);})
	    });
}

function changeDateComingBach()
{
    year_1  =  $('div#marketDate_1 div.marketDropdown select.wLrgs option:selected').attr('value').substring(0,4);
    month_1 =  $('div#marketDate_1 div.marketDropdown select.wLrgs option:selected').attr('value').substring(5);
    day_1   =  $('div#marketDate_1 div.marketDropdown select.wSml1s option:selected').attr('value');
    
    year_2  =  $('div#marketDate_2 div.marketDropdown select.wLrgs option:selected').attr('value').substring(0,4);
    month_2 =  $('div#marketDate_2 div.marketDropdown select.wLrgs option:selected').attr('value').substring(5);
    day_2   =  $('div#marketDate_2 div.marketDropdown select.wSml1s option:selected').attr('value');
    
    var dateComingOut = year_1+month_1+day_1;
    var dateComingBack = year_2+month_2+day_2;
    
    if(dateComingOut >= dateComingBack)
    {
        return true;
    }
    else
    {
        return false;
    }
}

function setDateComboBox()
{
    $('div#marketDate_1 p select').change(function(){
    changeByComboBox_GoingOut();
    });
}

function changeByComboBox_GoingOut()
{
        var year  =  $('div#marketDate_1 div.marketDropdown select.wLrgs option:selected').attr('value').substring(0,4);
        var month =  $('div#marketDate_1 div.marketDropdown select.wLrgs option:selected').attr('value').substring(5);
        var day   =  $('div#marketDate_1 div.marketDropdown select.wSml1s option:selected').attr('value');
	
        $("input#date_picker_id_2").datepicker('destroy'); 
        $("input#date_picker_id_2").datepicker('enable'); 
        
        setDateFlight_ComeBack(year, month-1, day, "input#date_picker_id_2");
        
        if(changeDateComingBach())
        {
            $('div#marketDate_2 div.marketDropdown select.wSml1s option').removeAttr('selected');
	        $('div#marketDate_2 div.marketDropdown select.wSml1s option[value='+day+']').eq(0).attr('selected',' ');
        		
	        var stringValueYear = yearSelected+'-'+monthSelected;
	        $('div#marketDate_2 div.marketDropdown select.wLrgs option').removeAttr('selected');
	        $('div#marketDate_2 div.marketDropdown select.wLrgs option[value='+stringValueYear+']').eq(0).attr('selected',' ');
        }
}

function setDateMaxCalendar()
{
    var dtNow = new Date();
    var dtMax = new Date(2011,01,28);
    var diff = ((dtMax.getTime()-dtNow.getTime())/86400000);
  
    return Math.ceil(diff);
}
