﻿//-------------------------------------------------------------------------------
// Calculate...
//-------------------------------------------------------------------------------
function calculatelop()
{
    try
    {
        if( isValidDetails2() )
        {
            // Loan details.
            var loanAmount      = parseFloat( getElement( AmountTextboxId2 ).value );
            var tenor           = parseInt( getElement( TenorDropdownId2 ).value );
            var interestRate    = getInterest2( tenor );
            var bookingDate     = getDate( getElement( BookindDateYearDropdownId2 ).value, getElement( BookindDateMonthDropdownId2 ).value, getElement( BookindDateDayDropdownId2 ).value );
            var loanCycle       = parseInt( getElement( LoanCycleDropdownId2 ).value );
            
            // Create loan object.
            var oLoan = new Loan( loanAmount, interestRate, tenor, bookingDate, loanCycle, DaysInYearConstant );
            
            // Display the result.
            display2( oLoan );
        }
        else
            resetDisplay2();
    }
    catch(e)
    {
        alert( 'An unexpected error while calculating. \nError Description: ' + e.Message );
    }
}

function resetDisplay2()
{
    getElement( 'uxResultDiv2' ).innerHTML = '';
    showElement( 'uxGraphDiv2', false );
}


//--------------------------------------------------------------------------------
// Validation helpers.
//--------------------------------------------------------------------------------
function isValidDetails2()
{
    var amountTextbox2               = getElement( AmountTextboxId2 );
    var tenorDropdown2               = getElement( TenorDropdownId2 );
    var bookindDateDayDropdown2     = getElement( BookindDateDayDropdownId2 );
    var bookindDateMonthDropdown2    = getElement( BookindDateMonthDropdownId2 );
    var bookindDateYearDropdown2     = getElement( BookindDateYearDropdownId2 );
    var loanCycleDropdown2           = getElement( LoanCycleDropdownId2 );
    
    if( amountTextbox2.value.length == 0 || !isNumeric( amountTextbox2.value ) )
    {
        alert( 'Please enter a valid loan amount.' );
        amountTextbox2.select();
        amountTextbox2.focus();
        return false;
    }
    else if( parseFloat( amountTextbox2.value ) < MinLoanAmountConstant )
    {
        alert( 'Loan amount should not be less than ' + MinLoanAmountConstant + '.' );
        amountTextbox2.select();
        amountTextbox2.focus();
        return false;
    }
    else if( tenorDropdown2.selectedIndex == 0 )
    {
        alert( 'Please select tenor.' );
        tenorDropdown2.focus();
        return false;
    }
    else if( bookindDateDayDropdown2.selectedIndex == 0 || bookindDateMonthDropdown2.selectedIndex == 0 || bookindDateYearDropdown2.selectedIndex == 0 )
    {
        if( bookindDateDayDropdown2.selectedIndex == 0 )
        {
            alert( 'Please select booking day.' );
            bookindDateDayDropdown2.focus();
        }
        else if( bookindDateMonthDropdown2.selectedIndex == 0 )
        {
            alert( 'Please select booking month.' );
            bookindDateMonthDropdown2.focus();
        }
        else if( bookindDateYearDropdown2.selectedIndex == 0 )
        {
            alert( 'Please select booking year.' );
            bookindDateYearDropdown2.focus();
        }
        return false;
    }
    else if( !isValidDate( bookindDateDayDropdown2.value, bookindDateMonthDropdown2.value, bookindDateYearDropdown2.value, true ) )
    {   
        bookindDateDayDropdown2.focus();
        return false;
    }
    else if( loanCycleDropdown2.selectedIndex == 0 )
    {
        alert( 'Please select your credit card statement date.' );
        loanCycleDropdown2.focus();
        return false;
    }
    
    // Success.
    return true;
}

// This method Gets the interest for the given tenor.
function getInterest2( pTenor )
{
    // Loop through tenor list, find the tenor and 
    // return relevent interest rate.
    for( var index in TenorsConstantList )
    {
        if( TenorsConstantList[index] == pTenor )
            return AnualInterestsConstantList2[index];
    }
}


//-------------------------------------------------------------------------------
// Form controls implementation helpers.
//-------------------------------------------------------------------------------
// This method create the tenor dropdown control.
function tenorDropdown2( 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 loanCycleDropdown2( 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 dateDropdown2( 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>' );
}
