﻿//--------------------------------------------------------------------------------
// General date methods.
//--------------------------------------------------------------------------------
var MonthsConstantList      = new Array( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 
                                         'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' );
var MonthsAbbrvConstantList = new Array( 'January', 'February', 'March', 'April', 'May', 'June', 
	                                     'July', 'August', 'September', 'October', 'November', 'December' );
var DateSeperator           = '-';

// Get month name from the month number.
function getMonthName( pMonthNumber )
{
    // Fix month number to index format.
    pMonthNumber = pMonthNumber - 1; // Because index starts with 0.
    
    // Loop through tenor list, find the tenor and 
    // return relevent interest rate.
    for( var index = 0; index < MonthsConstantList.length; index++ )
        if( index == pMonthNumber )
            return MonthsConstantList[index];
}

// Get month number from the month name.
function getMonth( pMonthName )
{
    // Loop through tenor list, find the tenor and 
    // return relevent interest rate.
    for( var index = 0; index < MonthsConstantList.length; index++ )
        if( MonthsConstantList[index] == pMonthName )
            return index + 1;
}

// Thid method convert the given details to date format.
function getDate( pYear, pMonth, pDate )
{
    // Convert to date format.
    return new Date( pYear, pMonth-1, pDate ); // Note: 0-11 for month in javascript.
}

// This method Gets the total number of days in the 
// given year.
function daysInYear( pYear )
{
    // Calculate difference between 1st of the given 
    // year and next year.
    return dateDiff( '01' + DateSeperator + '01' + DateSeperator + pYear, '01' + DateSeperator + '01' + DateSeperator + (pYear+1) );
}	

// This method Gets the total number of days in the given month 
// and year.
function daysInMonth( pMonth, pYear )
{
    // Calculate difference between the given month and 
    // the next month.
    return dateDiff( (pMonth+1) + DateSeperator +'01'+ DateSeperator + pYear, pMonth + DateSeperator + '01' + DateSeperator + pYear );
}

// This method Gets the difference between given dates in days.
function dateDiff( pStartDate, pEndDate )
{
    // Convert to date type.
    var theStartDate = new Date( pStartDate );
    var theEndDate = new Date( pEndDate );
    
    // Get 1 day in milliseconds.
    var singleDay = 24*60*60*1000; // (24hours, 60mins, 60secs and 1000 milliseconds).
    
    // Calcualte the difference in days.
    var days = Math.round( (theStartDate-theEndDate)/(singleDay) );
    return days;
}

function isDateGreaterThanOrEqualTo( pDate1, pDate2 )
{
    // Convert to date type.
    var theDate1 = new Date( pDate1 );
    var theDate2 = new Date( pDate2 );
    if( ( theDate1.getFullYear() > theDate2.getFullYear() ) || 
        ( theDate1.getFullYear() == theDate2.getFullYear() && theDate1.getMonth() > theDate2.getMonth() ) || 
        ( theDate1.getFullYear() == theDate2.getFullYear() && theDate1.getMonth() == theDate2.getMonth() && theDate1.getDate() >= theDate2.getDate() ) )
    {
        return true;
    }
    else
    {
        return false;
    }
}

function isDateGreaterThan( pDate1, pDate2 )
{
    // Convert to date type.
    var theDate1 = new Date( pDate1 );
    var theDate2 = new Date( pDate2 );
    if( ( theDate1.getFullYear() > theDate2.getFullYear() ) || 
        ( theDate1.getFullYear() == theDate2.getFullYear() && theDate1.getMonth() > theDate2.getMonth() ) || 
        ( theDate1.getFullYear() == theDate2.getFullYear() && theDate1.getMonth() == theDate2.getMonth() && theDate1.getDate() > theDate2.getDate() ) )
    {
        return true;
    }
    else
    {
        return false;
    }
}

function addMonth( pDate, pNumberOfMonths )
{
    // Add given number of months.
    var month   = pDate.getMonth() + parseInt( pNumberOfMonths );
    
    // Update year and month in the given data and return.
    var resultDate = new Date( pDate );
    resultDate.setMonth( month );
    
    // Return the updated date.
    return resultDate;
}

function addDay( pDate, pNumberOfDays )
{
    // Add given number of days.
    var day = pDate.getDate() + parseInt( pNumberOfDays );
    
    // Update it in the result date.
    var resultDate = new Date( pDate );
    resultDate = new Date( resultDate.setDate( day ) );
    
    // Return the updated date.
    return resultDate;
}

// Checks for the following valid date formats:
// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
function isValidDate( pDay, pMonth, pYear, pCheckToday, pErrorMessage )
{
	var checkToday = false;
	if( pCheckToday != undefined )
		checkToday = pCheckToday;
		
    // Check the error message.
	var errorMessage = '';
	if( !isNull( pErrorMessage ) )
		errorMessage = '\n' + pErrorMessage;
	
	// Parse date into variables.
	day     = parseInt( pDay );
	month   = parseInt( pMonth );
	year    = parseInt( pYear );
	
	// Check with today.
	if( checkToday )
	{
		var today = new Date();
		if( !isDateGreaterThanOrEqualTo( new Date(month + DateSeperator + day + DateSeperator + year), today ) )
		{
			alert( 'Date should be greater than today.' );
			return false;
		}
	}
    
	// Check month range.
	if( month < 1 || month > 12 )
	{
		alert( 'Month must be between 1 to 12.' + errorMessage );
		return false;
	}

	// Check date range.
	if( day < 1 || day > 31 )
	{
		alert( 'Day must be between 1 and 31.' + errorMessage );
		return false;
	}
    
	// Check month and days.
	if( ( month == 4 || month == 6 || month == 9 || month == 11 ) && day == 31 )
	{
		alert( MonthsAbbrvConstantList[month-1] + ' month ' + 'doesn\'t have 31 days!' + errorMessage )
		return false
	}

	// Check for february 29th according to the leap year.
	if(month == 2)
	{
		var isleap = ( year % 4 == 0 && ( year % 100 != 0 || year % 400 == 0 ) );
		if( day > 29 || ( day == 29 && !isleap ) )
		{
			alert( 'February ' + year + ' doesn\'t have ' + day + ' days!' + errorMessage );
			return false;
	   	}
	}

	// The given date is a valid date.
	return true;
}
