﻿//-------------------------------------------------------------------------------
// Calculate...
//-------------------------------------------------------------------------------
function calculateins()
{
    try
    {
        if( isValidDetails3() )
        {
            // Loan details.
            var loanAmount      = parseFloat( getElement( AmountTextboxId3 ).value );
            var tenor           = parseInt( getElement( TenorDropdownId3 ).value );
            var interestRate    = getInterest3( tenor );
            var bookingDate     = getDate( getElement( BookindDateYearDropdownId3 ).value, getElement( BookindDateMonthDropdownId3 ).value, getElement( BookindDateDayDropdownId3 ).value );
            var loanCycle       = parseInt( getElement( LoanCycleDropdownId3 ).value );
            
            // Create loan object.
            var oLoan = new Loan( loanAmount, interestRate, tenor, bookingDate, loanCycle, DaysInYearConstant );
            
            // Display the result.
            display3( oLoan );
        }
        else
            resetDisplay3();
    }
    catch(e)
    {
        alert( 'An unexpected error while calculating. \nError Description: ' + e.Message );
    }
}

function resetDisplay3()
{
    getElement( 'uxResultDiv3' ).innerHTML = '';
    showElement( 'uxGraphDiv3', false );
}


//--------------------------------------------------------------------------------
// Validation helpers.
//--------------------------------------------------------------------------------
function isValidDetails3()
{
    var amountTextbox3               = getElement( AmountTextboxId3 );
    var tenorDropdown3               = getElement( TenorDropdownId3 );
    var bookindDateDayDropdown3     = getElement( BookindDateDayDropdownId3 );
    var bookindDateMonthDropdown3    = getElement( BookindDateMonthDropdownId3 );
    var bookindDateYearDropdown3     = getElement( BookindDateYearDropdownId3 );
    var loanCycleDropdown3           = getElement( LoanCycleDropdownId3 );
    
    if( amountTextbox3.value.length == 0 || !isNumeric( amountTextbox3.value ) )
    {
        alert( 'Please enter a valid loan amount.' );
        amountTextbox3.select();
        amountTextbox3.focus();
        return false;
    }
    else if( parseFloat( amountTextbox3.value ) < MinLoanAmountConstant )
    {
        alert( 'Loan amount should not be less than ' + MinLoanAmountConstant + '.' );
        amountTextbox3.select();
        amountTextbox3.focus();
        return false;
    }
    else if( tenorDropdown3.selectedIndex == 0 )
    {
        alert( 'Please select tenor.' );
        tenorDropdown3.focus();
        return false;
    }
    else if( bookindDateDayDropdown3.selectedIndex == 0 || bookindDateMonthDropdown3.selectedIndex == 0 || bookindDateYearDropdown3.selectedIndex == 0 )
    {
        if( bookindDateDayDropdown3.selectedIndex == 0 )
        {
            alert( 'Please select booking day.' );
            bookindDateDayDropdown3.focus();
        }
        else if( bookindDateMonthDropdown3.selectedIndex == 0 )
        {
            alert( 'Please select booking month.' );
            bookindDateMonthDropdown3.focus();
        }
        else if( bookindDateYearDropdown3.selectedIndex == 0 )
        {
            alert( 'Please select booking year.' );
            bookindDateYearDropdown3.focus();
        }
        return false;
    }
    else if( !isValidDate( bookindDateDayDropdown3.value, bookindDateMonthDropdown3.value, bookindDateYearDropdown3.value, true ) )
    {   
        bookindDateDayDropdown3.focus();
        return false;
    }
    else if( loanCycleDropdown3.selectedIndex == 0 )
    {
        alert( 'Please select your credit card statement date.' );
        loanCycleDropdown3.focus();
        return false;
    }
    
    // Success.
    return true;
}

// This method Gets the interest for the given tenor.
function getInterest3( pTenor )
{
    // Loop through tenor list, find the tenor and 
    // return relevent interest rate.
    for( var index in TenorsConstantList )
    {
        if( TenorsConstantList[index] == pTenor )
            return AnualInterestsConstantList3[index];
    }
}


//-------------------------------------------------------------------------------
// Form controls implementation helpers.
//-------------------------------------------------------------------------------
// This method create the tenor dropdown control.
function tenorDropdown3( pId )
{
    // Create dropdown with the given id.
    document.write( '<select id="' + pId + '" class="ddmm" style="width:60px;">' );
    document.write( '<option value="-1" selected>Select</option>' );
    for( index = 0; index < TenorsConstantList.length; index++ )
	    document.write( '<option value="' + TenorsConstantList[index] + '">' + fixDigit( TenorsConstantList[index], 2 ) + '</option>' );
    document.write( '</select>' );
}

// This method create the tenor dropdown control.
function loanCycleDropdown3( pId )
{
    // Create dropdown with the given id.
    document.write( '<select id="' + pId + '" class="ddmm" style="width:60px;">' );
    document.write( '<option value="-1" selected>Select</option>' );
    for( index = 0; index < LoanCycleConstantList.length; index++ )
	    document.write( '<option value="' + LoanCycleConstantList[index] + '">' + fixDigit( LoanCycleConstantList[index], 2 ) + '</option>' );
    document.write( '</select>' );
}

function dateDropdown3( pDayId, pMonthId, pYearId, pExtraYears, pPopulateToday, pIgnoreLastYears )
{
	// Get this year.
	var today = new Date();
	var thisDate    = today.getDate();
	var thisMonth   = today.getMonth() + 1;
	var thisYear    = today.getFullYear();
	var uptoYear    = today.getFullYear() + pExtraYears;
	
	// Check populate today date.
	var populateToday = false;
	if( pPopulateToday != undefined )
		populateToday = pPopulateToday;
	
	// Validate total number of years.
	var endYear = thisYear; // 1901;
	if( !isNull( pIgnoreLastYears ) )
		uptoYear = uptoYear - parseInt( pIgnoreLastYears );
	
	// Load days.
	document.write( '<select class="ddmm" id="' + pDayId + '" style="width:36pt;">' );
	document.write( '<option value="-1" selected>Day</option>' );
	for( day = 1; day <= 31; day++ )
		document.write( '<option value="' + day + '"' + (( populateToday && day == thisDate ) ? 'selected' : '') + '>' + fixDigit( day, 2 ) + '</option>' );
	document.write( '</select>' );
	
	// Load months.
	document.write( '<select class="ddmm" id="' + pMonthId + '" style="width:44pt;">' );
	document.write( '<option value="-1" selected>Month</option>' );
	for( month = 1; month <= 12; month++ )
		document.write( '<option value="' + month + '"' + (( populateToday && month == thisMonth ) ? 'selected' : '') + '>' + MonthsConstantList[month-1] + '</option>' );
	document.write( '</select>' );
	
	// Load years.
	document.write( '<select class="ddmm" id="' + pYearId + '" style="width:40pt;">' );
	document.write( '<option value="-1" selected>Year</option>' );
	for( year = endYear; year <= uptoYear; year++ )
		document.write( '<option value="' + year + '"' + (( populateToday && year == thisYear ) ? 'selected' : '') + '>' + year + '</option>' );
	document.write( '</select>' );
}