function isDateCorrect( strDate )
{
    var separated = strDate.split( "." );
    if( separated.length != 3 )
        return false;
    return true;
}

function getDate( strDate )
{
    var separated = strDate.split( "." );
    if( separated.length != 3 )
        return new Date();
    for( i=0; i<3; i++ )
        if( separated[i].charAt( 0 ) == "0" )
            separated[i] = separated[i].substr(1);
    intDay = parseInt( separated[ 0 ] );
    intMonth = parseInt( separated[ 1 ] );
    intYear = parseInt( separated[ 2 ] );
    var selDate = new Date();
    selDate.setFullYear( intYear );
    selDate.setMonth( intMonth-1 );
    selDate.setDate( intDay );
    return selDate;
}

function subDate( date1, date2 )
{
    return (date1.getFullYear()-date2.getFullYear())*365 +
           (date1.getMonth()-date2.getMonth())*30 +
           (date1.getDate()-date2.getDate());
}

function calculateSum()
{
    var frmObj = findObject( "calcForm" );
    if( !frmObj )
        return;
    if( !isDateCorrect( frmObj.dtInDate.value ) || !isDateCorrect( frmObj.dtOutDate.value ) )
        {
        frmObj.amount.value = "Incorrect date";
        return;
        }
    var startDate = getDate( frmObj.dtInDate.value );
    var endDate = getDate( frmObj.dtOutDate.value );

    if( endDate < startDate  )
        {
        frmObj.amount.value = "Incorrect date";
        return;
        }

    var zzz = 0;
    var roomIdx = -1;
    for( i=0; i<7; i++ )
        if( frmObj.room[i].checked )
        {
            roomIdx = i;
            ro = findObject( "room"+(i+1) );
            if ( ro )
                cnt = ro.value;
            else
                cnt = 1;
            zzz = zzz+subDate( endDate, startDate )*(rooms[roomIdx]*cnt);
        }

    if( roomIdx == -1  )
        {
        frmObj.amount.value = "Choice room";
        return;
        }
            
    frmObj.amount.value = zzz + " uah";
}
