
/*
Function: ApplyTextAreaMaxLength
Description: Makes sure that given TextArea control does not exceed its MaxLength.
			 Uses timeout to check the characters in the control every 10 milliseconds.
Usage: BODY.onLoad="ApplyTextAreaMaxLength( 'txtTextAreaControl', 100 );"
*/
function ApplyTextAreaMaxLength( strTextAreaControlID, intMaxLength )
{
	var txtText = "" + document.getElementById( strTextAreaControlID ).value;
	
	if ( txtText.length > parseInt( intMaxLength, 10 ) )
	{
		document.getElementById( strTextAreaControlID ).value = txtText.substring( 0, parseInt( intMaxLength, 10 ) );
	}
	
	window.setTimeout( "ApplyTextAreaMaxLength( '" + strTextAreaControlID + "', " + intMaxLength + " );", 10 );
}

/*
Function: MandatoryCheckYesNoOptions
Description: Makes sure that at least one of two given Radio Options representing Yes/No options is selected.
*/
function MandatoryCheckYesNoOptions( rdoYesRadioOption, rdoNoRadioOption )
{
	if ( rdoYesRadioOption.checked == false && rdoNoRadioOption.checked == false )
	{
		return false;
	}
	else
	{
		return true;
	}
}

/*
Function: CheckMonetaryValueRanges
Description: Checks that a given Monetary value (e.g. something like £123,456.78 with pound sign and commas, etc.) are
             within given minimum and maximum monetary ranges.
             Returns true if within range, otherwise returns false.
*/
function CheckMonetaryValueRanges( strMonetaryValue, strMinMonetaryValue, strMaxMonetaryValue )
{
    // Remove commas from values
    //strMonetaryValue = strMonetaryValue.replace( /,/g, "" );
    //strMinMonetaryValue = strMinMonetaryValue.replace( /,/g, "" );
    //strMaxMonetaryValue = strMaxMonetaryValue.replace( /,/g, "" );
    strMonetaryValue = RemoveCharFromString( strMonetaryValue, "," );
    strMinMonetaryValue = RemoveCharFromString( strMinMonetaryValue, "," );
    strMaxMonetaryValue = RemoveCharFromString( strMaxMonetaryValue, "," );
    
    // Remove pound sign from values
    if (isNaN(parseFloat( strMonetaryValue )) == true)
        strMonetaryValue = strMonetaryValue.substring( 1 );
        
    if (isNaN(parseFloat( strMinMonetaryValue )) == true)
        strMinMonetaryValue = strMinMonetaryValue.substring( 1 );
        
    if (isNaN(parseFloat( strMaxMonetaryValue )) == true)
        strMaxMonetaryValue = strMaxMonetaryValue.substring( 1 );
    
    // Convert string values into floating point values
    strMonetaryValue = parseFloat( strMonetaryValue );
    strMinMonetaryValue = parseFloat( strMinMonetaryValue );
    strMaxMonetaryValue = parseFloat( strMaxMonetaryValue );
    
    // Check whether strMonetaryValue is within minimum and maximum range
    if ( strMonetaryValue >= strMinMonetaryValue && strMonetaryValue <= strMaxMonetaryValue )
    {
        return true;
    }
    else
    {
        return false;
    }
}

/*
Function: FormatUKCurrency
*/
function FormatUKCurrency( strValue, fShowPence )
{
    var intIndex;
    var intInitIndex;

    // Convert value into a floating point value, to ensure that the value can be properly converted to a UK Currency
    // string with £ sign and thousand grouping.

    // Convert value into a string
    strValue = "" + strValue;
    
    // Remove pound sign from values
    //strValue = strValue.replace( /£/g, "" );
    strValue = RemoveCharFromString( strValue, "£" );
    
    // Remove commas from values
    //strValue = strValue.replace( /,/g, "" );
    strValue = RemoveCharFromString( strValue, "," );
    
    // Convert string values into floating point values
    strValue = parseFloat( strValue );
    
    // Convert value into a string
    strValue = "" + strValue;
    
    if ( fShowPence == false && strValue.indexOf( "." ) > 0 )
    {
        // Remove fractional part of pence
        strValue = strValue.substring( 0, strValue.indexOf( "." ) );
    }
    
    if ( fShowPence == true && strValue.indexOf( "." ) == -1 )
    {
        // Add .00
        strValue = strValue + ".00";
    }
    
    // Put in commas for thousand grouping
    intInitIndex = strValue.indexOf( "." );
    
    if ( intInitIndex < 0 )
    {
        intInitIndex = strValue.length;
    }
    
    for ( intIndex = intInitIndex - 3; intIndex > 0; intIndex = intIndex - 3 )
    {
        strValue = strValue.substring( 0, intIndex ) + "," + strValue.substring( intIndex, strValue.length );
    }
    
    // Add pound sign to value
    strValue = "&pound;" + strValue;
    
    return strValue;
}

/*
Function: RemoveStringFromString
*/
function RemoveCharFromString( strStringToSearchWithin, strCharToRemove )
{
    var intIndex;
    var strNewString = "";

    if ( strCharToRemove.length > 1 )
        strCharToRemove = strCharToRemove.substring( 0, 1 );
   
    for ( intIndex = 0; intIndex < strStringToSearchWithin.length; intIndex++ )
    {
        if ( strStringToSearchWithin.substring( intIndex, intIndex + 1 ) != strCharToRemove )
            strNewString = strNewString + strStringToSearchWithin.substring( intIndex, intIndex + 1 );
    }
    
    return strNewString;
}

/*
Function: IsLeapYear
*/
function IsLeapYear( intYear )
{
    var fLeapYear = false;
    
    if ( ( intYear % 400 ) == 0 )
        fLeapYear = true;
    else
    {
        if ( ( intYear % 100 ) == 0 )
            fLeapYear = false;
        else
        {
            if ( ( intYear % 4 ) == 0 )
                fLeapYear = true;
            else
                fLeapYear = false;
        }
    }
    
    return fLeapYear;
}

/*
Function: ValidateDate
*/
function ValidateDate( strYear, strMonth, strDay )
{
    var fLeapYear;
    var fValid = IsInt( strDay );
    
    if ( fValid == true )
        fValid = IsInt( strMonth );
        
    if ( fValid == true )
        fValid = IsInt( strYear );
        
    if ( fValid == true )
    {
        strDay = parseInt( strDay, 10 );
        strMonth = parseInt( strMonth, 10 );
        strYear = parseInt( strYear, 10 );
        
        fValid = ( strMonth >= 1 && strMonth <= 12 );
    }
    
    if ( fValid == true && strMonth == 2 )
    {
        if ( ( strYear % 400 ) == 0 )
            fLeapYear = true;
        else
        {
            if ( ( strYear % 100 ) == 0 )
                fLeapYear = false;
            else
            {
                if ( ( strYear % 4 ) == 0 )
                    fLeapYear = true;
                else
                    fLeapYear = false;
            }
        }
        
        fValid = ( strDay >= 1 && ( ( strDay <= 29 && fLeapYear == true ) || ( strDay <= 28 && fLeapYear == false ) ) );
    }
    
    if ( fValid == true && strMonth != 2 )
    {
        if ( strMonth == 4 || strMonth == 6 || strMonth == 9 || strMonth == 11 )
            fValid = ( strDay >= 1 && strDay <= 30 );
        else
            fValid = ( strDay >= 1 && strDay <= 31 );
    }
    
    return fValid;
}

/*
Function: IsInt
*/
function IsInt( strNumber )
{
    var INTEGER_DIGITS = "0123456789";
    
    var fValid;
    var intIndex = 0;
    
    strNumber = "" + strNumber;
    
    if ( strNumber == "" )
        fValid = false;
    else
    {
        fValid = true;
        
        while ( intIndex < strNumber.length && fValid == true )
        {
            fValid = ( INTEGER_DIGITS.charAt( strNumber.substring( intIndex, intIndex + 1 ) ) > -1 );
            
            intIndex++;
        }
    }
    
    return fValid;
}

/*
Function: UKCurrencyToFloat
*/
function UKCurrencyToFloat( strUKCurrencyValue, fReturnInvalidAsZero )
{
    var fltValue;

    // Remove commas from currency value
    strUKCurrencyValue = RemoveCharFromString( strUKCurrencyValue, "," );
    
    // Remove pound sign from value
    if ( isNaN( parseFloat( strUKCurrencyValue ) ) == true )
        strUKCurrencyValue = strUKCurrencyValue.substring( 1 );
        
    if ( isNaN( parseFloat( strUKCurrencyValue ) ) == false )
        fltValue = parseFloat( strUKCurrencyValue );
    else
    {
        if ( fReturnInvalidAsZero == true )
            fltValue = 0;
        else
            fltValue = null;
    }

    return fltValue;
}

/*
Function: TimeTicks
*/
function TimeTicks( strYear, strMonth, strDay )
{
    var intTicks = 0;

    strYear = "" + strYear;
    strMonth = "" + strMonth;
    strDay = "" + strDay;
    
    if ( strYear.length > 0 && strMonth.length > 0 && strDay.length > 0 )
        intTicks = ( parseInt( strYear, 10 ) * 10000 ) + ( parseInt( strMonth, 10 ) * 100 ) + parseInt( strDay, 10 );

    return intTicks;
}

/*
Function: DateDiffInDays
*/
function DateDiffInDays( strDate1Year, strDate1Month, strDate1DayOfMonth, strDate2Year, strDate2Month, strDate2DayOfMonth,
                         fMinus1MonthValue )
{
    var intMillisecondsIn1Day = 1000 * 60 * 60 * 24;
     
    intDiffInDays = parseInt( DateDiffInMilliseconds( strDate1Year, strDate1Month, strDate1DayOfMonth, strDate2Year,
                              strDate2Month, strDate2DayOfMonth, fMinus1MonthValue ) / intMillisecondsIn1Day, 10 );
    
    return intDiffInDays;
}

/*
Function: DateDiffInMilliseconds
*/
var datDate1 = new Date();
var datDate2 = new Date();
function DateDiffInMilliseconds( strDate1Year, strDate1Month, strDate1DayOfMonth, strDate2Year, strDate2Month,
                                 strDate2DayOfMonth, fMinus1MonthValue )
{
    
    var intDiffInMilliseconds = 0;
    var intMonthMinusValue = 0;
    
    strDate1Year = "" + strDate1Year;
    strDate1Month = "" + strDate1Month;
    strDate1DayOfMonth = "" + strDate1DayOfMonth;
    strDate2Year = "" + strDate2Year;
    strDate2Month = "" + strDate2Month;
    strDate2DayOfMonth = "" + strDate2DayOfMonth;
    
    if ( strDate1Year.length > 0 && strDate1Month.length > 0 && strDate1DayOfMonth.length > 0 &&
         strDate2Year.length > 0 && strDate2Month.length > 0 && strDate2DayOfMonth.length > 0 )
    {
        if ( fMinus1MonthValue == true )
            intMonthMinusValue = 1;
        
        datDate1.setFullYear( parseInt( strDate1Year, 10 ), parseInt( strDate1Month, 10 ) - intMonthMinusValue,
                              parseInt( strDate1DayOfMonth, 10 ) );
        datDate2.setFullYear( parseInt( strDate2Year, 10 ), parseInt( strDate2Month, 10 ) - intMonthMinusValue,
                              parseInt( strDate2DayOfMonth, 10 ) );

        intDiffInMilliseconds = Math.ceil( datDate1.getTime() - datDate2.getTime() );
    }
    
    return intDiffInMilliseconds;
}

