var fromDate,toDate;

function calendarSetup(){
    var min_days=parseInt($("#mindays").html());
    $("#fromDate").datepicker({
        numberOfMonths:2,
        minDate:new Date(),
        changeMonth: true,
        changeYear: true,
        showAnim:"slideDown",
        showOn:'both',
        buttonText: '',
        beforeShow: function(input, inst){
            $('#ui-datepicker-div').css("z-index", 200000);
            if(fromDate==null && toDate==null){
                fromDate=$("#fromDate").datepicker('getDate');
                toDate=$("#toDate").datepicker('getDate');
            }
        },
        beforeShowDay: function (date){
            if(fromDate!=null && toDate!=null){
                if(toDate>=date && date>=fromDate){
                    return[true,"ui-state-active"];
                }else{
                    return[true,""]
                }
            }else{
                return[true,""]
            }
        },
        onSelect: function(dateText, inst) {
            fromDate=$(this).datepicker('getDate');
            if(toDate==null){
                if(isNaN(min_days)){
                    toDate=new Date(fromDate.getTime());
                }else{
                    toDate=new Date(fromDate.getTime())
                    toDate.setDate(fromDate.getDate()+min_days);
                }
                $('#toDate').datepicker("setDate",toDate);
            }
            if(fromDate>toDate){
                toDate=new Date(fromDate.getTime())
                if(isNaN(min_days)){
                    toDate.setDate(fromDate.getDate()+1);
                    $('#toDate').datepicker('option', 'minDate', fromDate.getDate()+1);
                }else{
                    toDate.setDate(fromDate.getDate()+min_days);
                    $('#toDate').datepicker('option', 'minDate', fromDate.getDate()+min_days);
                }
                $('#toDate').datepicker("setDate",toDate);
           }
        }

    });

    $('#toDate').datepicker({
        numberOfMonths: 2,
        minDate:new Date(),
        showButtonPanel: false,
        showAnim:"slideDown",
        showOn:'both',
        buttonText: '',
        beforeShow: function(){
            if($("#toDate").val()!="" && toDate==null){
                if(toDate==null){
                    toDate=new Date(fromDate.getTime())
                }
                if(fromDate>toDate){
                    if(isNaN(min_days)){
                        toDate.setDate(fromDate.getDate()+1);
                    }else{
                        toDate.setDate(fromDate.getDate()+min_days);
                    }
                    $('#toDate').datepicker("setDate",toDate);
                }
               
                
            }
            if(fromDate==null && toDate==null){
                fromDate=$("#fromDate").datepicker('getDate');
                toDate=$("#fromDate").datepicker('getDate');;
            }
        },
        beforeShowDay: function (date){
            if(fromDate!=null && toDate!=null){
                if(toDate>=date && date>=fromDate){
                    return[true,"ui-state-active"];
                }else{
                    return[true,""]
                }
            }else{
                return[true,""]
            }
        },
        onSelect: function(dateText, inst) {
            tempDate=$(this).datepicker('getDate');
            if(fromDate>tempDate){
                $('#toDate').datepicker("setDate",toDate);
            }else{
                toDate=$(this).datepicker('getDate');
            }
        }
    });
}

$(function(){
    calendarSetup();
})


