function UpdateNights()
{
    var dayStartSel = document.getElementById( 'day_start' );
    var dayEndSel = document.getElementById( 'day_end' );
    var monthStartSel = document.getElementById( 'month_start' );
    var monthEndSel = document.getElementById( 'month_end' );
    var nightsEl = document.getElementById( 'nights' );
    
    var today = new Date();
    var aDateStart = new Date();
    aDateStart.setMonth( parseInt(today.getMonth()) + parseInt(monthStartSel.value) );
    aDateStart.setDate( parseInt(dayStartSel.value) );
    var aDateEnd = new Date();
    aDateEnd.setMonth( parseInt(today.getMonth()) + parseInt(monthEndSel.value) );
    aDateEnd.setDate( parseInt(dayEndSel.value) );
   
    if( ( aDateStart.valueOf() < 0 ) || ( aDateEnd.valueOf() < 0 ) ) return false; // not valid dates

    if( !validInterval( aDateStart, aDateEnd ) )
    {
        nightsEl.value = 0;
        var errorMessage = 'Please make sure that arrival date comes before departure date!';
        alert( errorMessage );
        return false;
    }
    
    // compute the difference between the two dates, in miliseconds
    var diff = aDateEnd - aDateStart;
    // convert to days
    var days = Math.round( diff/86400000 );
    
    nightsEl.value = days;
    
    return true;
}

function validInterval( aDateStart, aDateEnd ) 
{
    aDateStart = new Date( aDateStart );
    aDateEnd = new Date( aDateEnd );
    
    if( ( aDateStart.getTime() > aDateEnd.getTime() ) )
    {
        return false;
    }
    
    return true;
}
