/// place holder of all generic JavaScript functions used across pages
/**
 * @file
 * @startcomment
 *  File: GLibJs.js
 *
 *  Current Owner: Raghu Anand (10-10-2007)
 *
 *  Purpose: place holder of all generic JavaScript functions used across pages
 *
 *
 *  Dependencies:   none
 *
 *
 *  Classes:    none
 *
 *
 *  Global functions:
 *  1. Key press functions   
 *      NavigateOnEnter ()
 *      IsTextFieldKeyPress ()
 *      IsNumericFieldKeyPress ()
 *      IsIntegerFieldKeyPress ()
 *      IsDateFieldKeyPress ()
 
 *  2. Data validation function 
 *      InputNotNumericReset ()
 *      IsStringBlank ()
 *      IsAlphaNum ()
 *      IsIntegerValue ()
 *      IsNumericValue ()
 *
 *  3. Other FORM functions
 *      SetCheckboxGroup ()
 *
 *  4. Date Functions 
 *      IsValidDate ()
 *      IsDate ()
 *      CompareDates ()
 *      DateAdd ()
 *      DoDateValidation ()
 *      ChangePeriod ()
 *      IsLeapYear ()
 *      GetMonthDays ()
 *
 *  5. Other functions
 *      CreateProgressBar ()
 *      StartProgressBar ()
 *      IsParentContainerAvailable ()
 *      CommifyArray ()
 *      myrtrim ()
 *      myltrim ()
 *      mytrim ()
 *
 *  Notes:
 *  1. STRING obj's trim() method is defined.
 *     Eg : var string = '   string   '; var trimmed_str = string.trim();
 *
 * @endcomment
 *
 */

// case : STRING JS obj does not have the method 'trim' defined. Hence implement own trim method
if ( typeof String.prototype.trim == 'undefined' ) { String.prototype.trim = new Function ( "return this.replace(/^\\s+|\\s+$/g,'')" ) }

/// To check if any of the parent containers are unrendered/invisible
/**
 * @startcomment
 * Purpose: To check if any of the parent containers are unrendered/invisible
 *
 * Input Params:
 *  1.  pSrcElem - obj - source element object
 *
 * Output Params:
 *
 * Return Value:
 *  1  - parent containers are rendered/visible
 *  -1 - Any of parent containers is unrendered/invisible
 *
 * Notes:
 *  1. This function is recursively called till all the parent containers are reached.
 * @endcomment
 */
function IsParentContainerAvailable ( pSrcElem )
{
    // case: parent node exist
    if( pSrcElem.parentNode != null && pSrcElem.parentNode.tagName != null )
    {
        // case: parent node is hidden/unrendered
        if( pSrcElem.parentNode.style.visibility == 'hidden' || pSrcElem.parentNode.style.display == 'none' ) return -1;

        // case: check for this container's parent
        return IsParentContainerAvailable ( pSrcElem.parentNode );
    }

    // case: no more parent node
    else return 1;
}

/// to set the focus to the next available FORM control on enter key pressed
/**
 * @startcomment
 * Purpose: to set the focus to the next available FORM control on enter key pressed
 *
 * Input Params:
 *  1.  pFormName           - str   - The FORM name
 *  2.  pNavigateOnTextArea - int   - Flag denoting if navigation is to be executed on a textarea
 *
 * Output Params:   none
 *
 * Return Value:    none
 *
 * Notes:
 *  1. No setting focus on a radio button, or on a read only type
 *  2. By default, no navigation from a textarea on enter key press.
 *  3. Will return without execution if browser is not IE
 *  4. Requires all the FORM elements to specify the 'ID' attribute
 * @endcomment
 */
function NavigateOnEnter ( pFormName, pNavigateOnTextArea )
{
    // case : if textarea navigation param is set, then set the flag
    if ( pNavigateOnTextArea ) var do_nav_on_textarea = parseInt ( pNavigateOnTextArea );

    // case : no navigation from textarea by default
    else var do_nav_on_textarea = 0;
    
    // determine if browser is IE
    var is_IE = ( document.all ) ? 1 : 0;

    // case : not IE
    if( !is_IE ) return true;

    // get key code
    var key_code = window.event.keyCode;

    // check if pressed key is enter
    if ( key_code != 13 ) return;
        
    // case : no navigation from textarea on enter keypress
    if ( do_nav_on_textarea == 0 && window.event.srcElement.type == 'textarea' )
    {
        window.event.returnValue = true;
        return;
    }

    // reset key code
    window.event.keyCode = 0;

    // loop to walk through each form element 
    for( n = 0; n < document.forms(pFormName).elements.length; n++ )
    {
        // do not consider other controls except source control
        if ( document.forms(pFormName).elements(n).id != window.event.srcElement.id ) continue;

        // find next eligible control to have focus
        for( i = n + 1; i < document.forms(pFormName).elements.length; i++ )
        {
            // if next control is <input type='hidden'> / disabled element / READONLY element / is hidden / is unrendered, then skip it
            if ( document.forms(pFormName).elements(i).type == 'hidden'
                 || document.forms(pFormName).elements(i).disabled == true
                 || document.forms(pFormName).elements(i).style.visibility == 'hidden'
                 || document.forms(pFormName).elements(i).style.display == 'none' )
                    continue;

            // case : if any of the parent containers of the next control are hidden/unrendered, then skip it
            if ( IsParentContainerAvailable ( document.forms(pFormName).elements(i) ) < 0 ) continue;
            
            // if has a tab index -1
            if ( document.forms(pFormName).elements(i).tabIndex < 0 ) continue;
            
            // if control is read only( readonly is not applicable for combobox and listbox )
            if ( document.forms(pFormName).elements(i).type != 'select-one' 
                 && document.forms(pFormName).elements(i).type != 'select-multiple' 
                 && document.forms(pFormName).elements(i).readOnly == true )
                    continue;

            // set the focus
            document.forms(pFormName).elements(i).focus();

            return;
        }
    }
}

/// To avoid inputting double quotes in html text input
/**
 * @startcomment
 * Purpose: To avoid inputting double quotes in html text input
 *
 * Input Params:
 *  1.  pEventObj - obj - DOM event object handle
 *
 * Output Params:
 *
 * Return Value:
 *  true - if string is blank
 *  false - if string is not blank
 *
 * Notes:
 *  1. In database, we store single quote, but not double quote. Hence, in the whole FORM, we might 
 *     want to supress doubel quotes.
 *  2. Sending of event handler is must for this function to work for Netscape and mozilla
 *     For example, onKeyPress='IsTextFieldKeyPress( event )'
 *  3. In case IE also, please send the event handler like previous example
 *  4. In IE, with out passing event handler will also work. This is to keep compatability
 *     with existing call/references. 
 * @endcomment
 */
function IsTextFieldKeyPress ( pEventObj )
{
    // determine the browser is IE
    var is_IE = ( document.all ) ? 1 : 0;

    if( is_IE && pEventObj == undefined ) pEventObj = window.event;
    
    // get the keycode
    var key_code = ( is_IE ) ? pEventObj.keyCode : pEventObj.which;

    // case : if key code is 34( means " ), then stop the key press event
    if ( key_code == 34 )
    {
        if( is_IE ) pEventObj.returnValue = false;

        else pEventObj.preventDefault();
    }
}

/// To allow entry of numbers with a single "." for a html text input
/**
 * @startcomment
 * Purpose: To allow entry of numbers with a single "." for a html text input
 *
 * Input Params:    none
 *
 * Output Params:   none
 *
 * Return Value:    none
 *
 * Notes:
 *  1.  event.returnValue = TRUE  - if any numeric key or '.' is pressed
 *  2.  event.returnValue = FALSE - any other key is pressed
 * @endcomment
 */
function IsNumericFieldKeyPress ()
{
    var element;

    element = event.srcElement;

    if ( event.keyCode >= 48 && event.keyCode <= 57 )
    {
        event.returnValue = true;
        return;
    }
    else if (event.keyCode == 46)
    {
        if ( element.value.indexOf('.', 0) == -1 )
        {
            event.returnValue = true;
            return;
        }
    }

    event.returnValue = false;
}

/// To allow entry of numbers for a html text input
/**
 * @startcomment
 * Purpose: To allow entry of numbers for a html text input
 *
 * Input Params:    none
 *
 * Output Params:   none
 *
 * Return Value:    none
 *
 * Notes:
 *   1.  event.returnValue = TRUE  - if any numeric key is pressed
 *   2.  event.returnValue = FALSE - any other key is pressed
 * @endcomment
 */
function IsIntegerFieldKeyPress ()
{
    var key_code = window.event.keyCode;

    // case : numeric key, or enter key
    if ( ( key_code >= 48 && key_code <= 57 ) || key_code == 13 )
    {
        window.event.returnValue = true;
        return;
    }

    window.event.returnValue = false;
}

/// To allow entry of valid date
/**
 * @startcomment
 * Purpose: To allow entry of valid date
 *
 * Input Params:    none
 *
 * Output Params:   none
 *
 * Return Value:    none
 *
 * Notes:
 *  1. Will allow '-' as separating char
 * @endcomment
 */
function IsDateFieldKeyPress ()
{
    var key_code = window.event.keyCode;

    // if valid key (number, - )
    if ( ( key_code >= 48 && key_code <= 57 ) || key_code == 13 || key_code == 45 ) return;

    // else if invalid key
    else   window.event.keyCode = 0;

    return;      
}

/// To check the source input control contains a valid number, else reset it to 0
/**
 * @startcomment
 * Purpose: To check the source input control contains a valid number, else reset it to 0
 *
 * Input Params:    none
 *
 * Output Params:   none
 *
 * Return Value:    none
 *
 * Notes:
 * @endcomment
 */
function InputNotNumericReset ()
{
    var src_element = window.event.srcElement;

    // case : not a number, or blank value
    if ( isNaN( src_element.value ) == true || IsStringBlank( src_element.value ) == true ) src_element.value = 0;
}

/// To check if the supplied string is empty/a string with white spaces
/**
 * @startcomment
 * Purpose: To check if the supplied string is empty/a string with white spaces
 *
 * Input Params:
 *  1.  pStr - str - string to be checked
 *
 * Output Params:
 *
 * Return Value:
 *  true - if string is blank
 *  false - if string is not blank
 *
 * Notes:
 *  1. Will check for blank/tab/new-line/carriage-return
 * @endcomment
 */
function IsStringBlank ( pStr )
{
	if( pStr == null ) return true;

    // loop through each char to check if it is a blank/tab/new-line/carriage-return
	for( var i=0; i<pStr.length; i++ )
    {
		if ( (pStr.charAt(i)!=' ') && (pStr.charAt(i)!="\t") && (pStr.charAt(i)!="\n") && (pStr.charAt(i)!="\r") ) return false;
	}

    return true;
}

/// To check if input is a +ve integer value
/**
 * @startcomment
 * Purpose: To check if input is a +ve integer value
 *
 * Input Params:
 *  1.  pVal - str - input to be checked for digits
 *
 * Output Params:
 *
 * Return Value:
 *  true - if input is a +ve integer
 *  false - if input is not an integer
 *
 * Notes:
 *  1. Will check each char in input for digits 0 to 9
 * @endcomment
 */
function IsIntegerValue ( pVal )
{
    // case : if blank input, then return false
	if ( IsStringBlank ( pVal ) ) return false;

    var digits="1234567890";

    // case : if any of the char is not a digit, then return false
	for( var i=0; i<pVal.length; i++ )
    {
        if ( digits.indexOf( pVal.charAt( i ) ) == -1 ) return false;
	}

	return true;
}

/// To check if input is a numeric value (+ or -)
/**
 * @startcomment
 * Purpose: To check if input is numeric value (+ or -)
 *
 * Input Params:
 *  1.  pVal - str - input to be checked
 *
 * Output Params:
 *
 * Return Value:
 *  true - if input is numeric
 *  false - if input is not numeric
 *
 * Notes:
 * 1. Example inputs : 2.5687, -3.58, 0.12, .12
 * @endcomment
 */
function IsNumericValue ( pVal )
{
    // case : if blank input, then return false
	if ( IsStringBlank ( pVal ) ) return false;

    // 1. consider only 1 decimal in the input - parseFloat( pVal, 10 )
    // 2. try and convert the input to a numeric value - pVal * 1
    // If the input is not a valid number, then (2) will return NaN
    // match (1) and (2) and return the result

    return( parseFloat( pVal, 10 ) == ( pVal * 1 ) );
}

/// To check if string is alphanumeric
/**
 * @startcomment
 * Purpose: To check if string is alphanumeric
 *
 * Input Params:
 *  1. pStr - string - string to be tested
 *
 * Output Params:   none
 *
 * Return Value:
 *  true - if all characters in the string are a character from 0-9 or a-z or A-Z
 *  false - if otherwise
 *
 * Notes:
 * @endcomment
 */
function IsAlphaNum ( pStr )
{
    // Return immediately if an invalid value was passed in
    if ( pStr+"" == "undefined" || pStr+"" == "null" || pStr+"" == "" ) return false;

    var is_valid = true;

    // convert to a string for performing string comparisons.
       pStr += "";

    // Loop through length of string and test for any alpha numeric characters
    for ( var i=0; i<pStr.length; i++ )
    {
        // Alphanumeric must be between "0"-"9", "A"-"Z", or "a"-"z"
        if (!(((pStr.charAt(i) >= "0") && (pStr.charAt(i) <= "9")) ||
              ((pStr.charAt(i) >= "a") && (pStr.charAt(i) <= "z")) ||
              ((pStr.charAt(i) >= "A") && (pStr.charAt(i) <= "Z"))))
        {
            is_valid = false;
            break;
        }
    }

    return is_valid;
}

/// To remove trailing spaces
/**
 * @startcomment
 * Purpose: To remove trailing spaces
 *
 * Input Params:
 *  1.  pStr - string - The string to be right trimmed
 *
 * Output Params:   none
 *
 * Return Value:
 *  string with trailing spaces removed
 *
 * Notes:
 * @endcomment
 */
function myrtrim ( pStr )
{
    for( var i=pStr.length-1; i>=0; i-- )
    {
        if( pStr.charCodeAt(i) != 32 )
        {
            pStr = pStr.substr( 0, i+1 );
            return pStr;
        }                  
    }

    return pStr;
}

/// To remove leading spaces
/**
 * @startcomment
 * Purpose: To remove leading spaces
 *
 * Input Params:
 *  1.  pStr - string - The string to be left trimmed
 *
 * Output Params:   none
 *
 * Return Value:
 *  string with leading spaces removed
 *
 * Notes:
 * @endcomment
 */
function myltrim ( pStr )
{
    for( var i=0; i<pStr.length; i++ )
    {
        if( pStr.charCodeAt(i) != 32 )
        {
            pStr = pStr.substr( i, pStr.length-i );
            return pStr;
        }
    }

    return pStr;
}

/// To remove leading and trailing spaces
/**
 * @startcomment
 * Purpose: To remove leading and trailing spaces
 *
 * Input Params:
 *  1.  pStr - string - The string to be trimmed
 *
 * Output Params:   none
 *
 * Return Value:
 *  string with leading and trailing spaces removed
 *
 * Notes:
 * @endcomment
 */
function mytrim ( pStr )
{
    return myltrim( myrtrim( pStr ) );
}

/// To set a checkbox group to on/off
/**
 * @startcomment
 * Purpose: To set a checkbox group to on/off
 *
 * Input Params:
 *  1. pCheckItem - obj - check box group id/name attribute
 *  2. pFlag - int - group to be checked on/off (1/0)
 *
 * Output Params:   none
 *
 * Return Value:    none
 *
 * Notes:
 *  1. pFlag = 1 if group to be checked
 *  2. pFlag = 0 if group to be unchecked
 * @endcomment
 */
function SetCheckboxGroup ( pChkItem, pFlag )
{
    if ( pChkItem ) for (var i=0; i<pChkItem.length; i++ ) pChkItem[i].checked = ( pFlag == 1 ) ? true : false;
}

/// To check if date is valid
/**
 * @startcomment
 * Purpose: To check if date is valid
 *
 * Input Params:
 *  1. pDate - string - date
 *
 * Output Params:   none
 *
 * Return Value:
 *  true - if valid date
 *  false - if invalid date
 *
 * Notes:
 * @endcomment
 */
function IsValidDate ( pDate )
{
    var dt_part;

    // not in dd-mm-yyyy fmt
    if ( ( dt_part = pDate.split('-') ).length != 3 ) return false;

    return IsDate( dt_part[2], dt_part[1], dt_part[0] );
}

/// To build date from the supplied params and check it is valid or not
/**
 * @startcomment
 * Purpose: To build date from the supplied params and check it is valid or not
 *
 * Input Params:
 *  1. pYear  - String - to get the string which represents year
 *  2. pMonth - String - to get the string which represents month
 *  3. pDay   - String - to get the string which represents day
 *
 * Output Params:   none
 *
 * Return Value:
 *  true  - if supplied date is a valid
 *  false - if supplied date is invalid
 *
 * Notes:
 * @endcomment
 */
function IsDate ( pYear, pMonth, pDay )
{
    var int_year = parseInt(pYear);

    if ( pMonth.substr(0,1) == '0' ) pMonth = pMonth.substr(1,1);

    var int_month = parseInt(pMonth) - 1;

    if ( pDay.substr(0,1) == '0' ) pDay = pDay.substr(1,1);

    var int_day = parseInt(pDay);

    // create a new date object from the input, which looks like "Wed Jan 1 00:00:00 EST 1975".
    var dt = new Date( int_year, int_month, int_day );

    // case : if inputs match the date object
    if( int_year  == dt.getFullYear() && int_month == dt.getMonth() && int_day   == dt.getDate() ) return true;

    // case : if inputs do not match the date object
    else return false;
}

/// To compare two dates
/**
 * @startcomment
 * Purpose: To compare two dates
 *
 * Input Params:
 *  pMon1  - String - to get the string which represents Mon1  
 *  pDay1  - String - to get the string which represents Day1  
 *  pYear1 - String - to get the string which represents Year1 
 *  pMon2  - String - to get the string which represents Mon2  
 *  pDay2  - String - to get the string which represents Day2  
 *  pYear2 - String - to get the string which represents Year2 
 *
 * Output Params:   none
 *
 * Return Value:
 *  -1 - Integer - if the first date is greater than second date 
 *   1 - Integer - if the second date is greater than first date
 *   0 - Integer - if both dates are equal
 *
 * Notes:
 * @endcomment
 */
function CompareDates ( pMon1, pDay1, pYear1, pMon2, pDay2, pYear2 )
{
    var year1 = parseFloat(pYear1); var mon1 = parseFloat(pMon1); var day1 = parseFloat(pDay1);
    var year2 = parseFloat(pYear2); var mon2 = parseFloat(pMon2); var day2 = parseFloat(pDay2);

    // check for years
    if( year1 > year2 ) return -1;
    else if( year2 > year1 ) return 1;

    // if years are same, check for month
    else 
    {
        if( mon1 > mon2 ) return -1;
        else if( mon2 > mon1 ) return 1;

        // if months are same, check for day
        else 
        {
            if( day1 > day2 ) return -1;
            else if( day2 > day1 ) return 1;
            else return 0;
        }
    }
}

/// To add the supplied interval with the supplied date and return the new date
/**
 * @startcomment
 * Purpose: To add the supplied interval with the supplied date and return the new date
 *
 * Input Params:
 *  dateval   - String - date
 *  interval  - String - interval
 *  addtype   - String - type ('D' / 'M' / 'Y' )
 *
 * Output Params:   none
 *
 * Return Value:
 *  date - the new date after adding the supplied interval with supplied date
 *
 * Notes:
 *  1. pAddType specifies that, interval is added to days, months or with year
 * @endcomment
 */
function DateAdd ( pDate, pInterval, pAddType )
{
    var day = pDate.getDate();
    var month = pDate.getMonth();
    var year = pDate.getYear();

    switch ( pAddType )
    {
        // add day
        case 'D':
            day = parseInt(day) + parseInt(pInterval);
            return new Date(year, month, day);

        // add month
        case 'M':
            month = parseInt(month) + parseInt(pInterval);
            return new Date(year, month, day);

        // add year
        case 'Y':
            year = parseInt(year) + parseInt(pInterval);
            return new Date(year, month, day);
    }
}

/// To validate user entered dates
/**
 * @startcomment
 * Purpose: To validate user entered dates
 *
 * Input Params:
 *  1. pFrmName    - String  - FORM name
 *  2. pDateCtrl1  - String  - date control1 name
 *  3. pDateCtrl2  - String  - date control2 name
 *  4. pEnableFlag - integer - flag to identify whether to disable/ enable the date controls 
 *
 * Output Params:   none
 *
 * Return Value:
 *  1 - Integer - if user entered dates are valid
 *  0 - Integer - if user entered dates are invalid
 *
 * Notes:
 * @endcomment
 */
function DoDateValidation ( pFrmName, pDateCtrl1, pDateCtrl2, pEnableFlag )
{
    var form_obj = document.forms( pFrmName );

    // check atleast one of the dates have been mentioned
    if( IsStringBlank ( form_obj.elements( pDateCtrl1 ).value ) == true
                                    &&
        IsStringBlank ( form_obj.elements( pDateCtrl2 ).value ) == true )
    {
        alert("Both the date input cannot be left empty");
        form_obj.elements( pDateCtrl1 ).focus();
        window.event.returnValue = false;
        return 0;
    }

    // check for invalid date in date control1 (should be in dd-mm-yyyy fmt)
    if ( IsValidDate ( form_obj.elements( pDateCtrl1 ).value ) == false )
    {
        alert("Invalid date");
        form_obj.elements( pDateCtrl1 ).focus();
        window.event.returnValue = false;
        return 0;
    }

    // check for invalid date in date control2 (should be in dd-mm-yyyy fmt)
    if ( IsValidDate( form_obj.elements( pDateCtrl2 ).value ) == false )
    {
        alert("Invalid date");
        form_obj.elements( pDateCtrl2 ).focus();
        window.event.returnValue = false;
        return 0;
    }

    // enable the date controls
    if( pEnableFlag == 1 )
    {
        form_obj.elements( pDateCtrl1 ).disabled = false;
        form_obj.elements( pDateCtrl2 ).disabled = false;
    }

    // success
    return 1;
}

/// To display the startdate & end date based on the selected period
/**
 * @startcomment
 * Purpose: To display the startdate & end date based on the selected period
 *
 * Input Params:
 *  pFrmName          - String  - form
 *  pPeriodConstName  - String  - Period constant
 *  pStartDateName    - String  - start date control
 *  pEndDateName      - integer - end date control
 *  pDisableDtFldFlag - integer - flag to identify whether to disable/enable the date controls
 *
 * Output Params:
 *  1.  Param1 - DataType - Desc
 *  2.  Param1 - DataType - Desc
 *
 * Return Value:
 *  ReturnVal - DataType - Desc
 *
 * Notes:
 * @endcomment
 */
function ChangePeriod ( pFrmName, pPeriodConstName, pStartDateName, pEndDateName, pDisableDtFldFlag )
{
    var _TODAY                  = 1;
    var _YESTERDAY              = 2; 
    var _YESTERDAY_AND_TODAY    = 3; 
    var _TOMORROW               = 4;
    var _LAST_SEVEN_DAYS        = 5;
    var _NEXT_SEVEN_DAYS        = 6;
    var _LAST_FIFTEEN_DAYS      = 7;
    var _NEXT_FIFTEEN_DAYS      = 8;
    var _LAST_MONTH             = 9;
    var _THIS_MONTH             = 10;
    var _NEXT_MONTH             = 11;
    var _LAST_YEAR              = 12;
    var _THIS_YEAR              = 13;
    var _LAST_FINANCIAL_YEAR    = 14;
    var _THIS_FINANCIAL_YEAR    = 15;
    var _TILL_TODAY             = 16;
    var _MENTION_PERIOD         = 101;

    var form_obj = document.forms( pFrmName );
    var period_const_obj = form_obj.elements( pPeriodConstName );
    var start_dt_obj = form_obj.elements( pStartDateName );
    var end_date_obj = form_obj.elements( pEndDateName );
    var dt_obj = new Date();

    if ( pDisableDtFldFlag == 1 ) 
    {
        // set the dafault display status as disabled
        start_dt_obj.disabled = true;
        end_date_obj.disabled = true;
    }

    // set dates on the period const value
    switch ( parseInt( period_const_obj.value ) )
    {
        case _TODAY :
            day_interval        = 0;
            dt_obj              = DateAdd( dt_obj, day_interval, 'D' );
            end_date_obj.value  = dt_obj.getDate() + '-' + (dt_obj.getMonth() + 1) + '-' + dt_obj.getYear();
            start_dt_obj.value  = dt_obj.getDate() + '-' + (dt_obj.getMonth() + 1) + '-' + dt_obj.getYear();
            break;

        case _YESTERDAY :
            day_interval        = -1;
            dt_obj              = DateAdd( dt_obj, day_interval, 'D' );
            end_date_obj.value  = dt_obj.getDate() + '-' + (dt_obj.getMonth() + 1) + '-' + dt_obj.getYear();
            start_dt_obj.value  = dt_obj.getDate() + '-' + (dt_obj.getMonth() + 1) + '-' + dt_obj.getYear();
            break;

        case _YESTERDAY_AND_TODAY :
            day_interval        = -1;
            end_date_obj.value  = dt_obj.getDate() + '-' + (dt_obj.getMonth() + 1) + '-' + dt_obj.getYear();
            dt_obj              = DateAdd( dt_obj, day_interval, 'D' );
            start_dt_obj.value  = dt_obj.getDate() + '-' + (dt_obj.getMonth() + 1) + '-' + dt_obj.getYear();
            break;

        case _TOMORROW :
            day_interval        = 1;
            dt_obj              = DateAdd( dt_obj, day_interval, 'D' );
            end_date_obj.value  = dt_obj.getDate() + '-' + (dt_obj.getMonth() + 1) + '-' + dt_obj.getYear();
            start_dt_obj.value  = dt_obj.getDate() + '-' + (dt_obj.getMonth() + 1) + '-' + dt_obj.getYear();
            break;

        case _LAST_SEVEN_DAYS :
            day_interval        = -7;
            end_date_obj.value  = dt_obj.getDate() + '-' + (dt_obj.getMonth() + 1) + '-' + dt_obj.getYear();
            dt_obj              = DateAdd( dt_obj, day_interval, 'D' );
            start_dt_obj.value  = dt_obj.getDate() + '-' + (dt_obj.getMonth() + 1) + '-' + dt_obj.getYear();
            break;

        case _NEXT_SEVEN_DAYS :
            day_interval        = 7;
            start_dt_obj.value  = dt_obj.getDate() + '-' + (dt_obj.getMonth() + 1) + '-' + dt_obj.getYear();
            dt_obj              = DateAdd( dt_obj, day_interval, 'D' );
            end_date_obj.value  = dt_obj.getDate() + '-' + (dt_obj.getMonth() + 1) + '-' + dt_obj.getYear();
            break;                                                                                      

        case _LAST_FIFTEEN_DAYS :
            day_interval        = -15;
            end_date_obj.value  = dt_obj.getDate() + '-' + (dt_obj.getMonth() + 1) + '-' + dt_obj.getYear();
            dt_obj              = DateAdd( dt_obj, day_interval, 'D' );
            start_dt_obj.value  = dt_obj.getDate() + '-' + (dt_obj.getMonth() + 1) + '-' + dt_obj.getYear();
            break;

        case _NEXT_FIFTEEN_DAYS :
            day_interval        = 15;
            start_dt_obj.value  = dt_obj.getDate() + '-' + (dt_obj.getMonth() + 1) + '-' + dt_obj.getYear();
            dt_obj              = DateAdd( dt_obj, day_interval, 'D' );
            end_date_obj.value  = dt_obj.getDate() + '-' + (dt_obj.getMonth() + 1) + '-' + dt_obj.getYear();
            break;

        case _THIS_MONTH :
            start_dt_obj.value  = '01' + '-' + (dt_obj.getMonth() + 1) + '-' + dt_obj.getYear();
            end_date_obj.value  = GetMonthDays( dt_obj.getMonth() + 1, dt_obj.getYear() ) + '-' + (dt_obj.getMonth() + 1) + '-' + dt_obj.getYear();
            break;

        case _LAST_MONTH :
            dt_obj              = DateAdd( dt_obj,  -( dt_obj.getDate()), 'D');
            start_dt_obj.value  = '01' + '-' + (dt_obj.getMonth()+1) + '-' + dt_obj.getYear();
            end_date_obj.value  =  dt_obj.getDate() + '-' + (dt_obj.getMonth()+1) + '-' + dt_obj.getYear();
            break;

        case _NEXT_MONTH :
            var dayinterval     = GetMonthDays( dt_obj.getMonth() + 1, dt_obj.getYear() ) - dt_obj.getDate() + 1;
            dt_obj              = DateAdd( dt_obj,  dayinterval, 'D');
            start_dt_obj.value  = dt_obj.getDate () + '-' + (dt_obj.getMonth() + 1 ) + '-' + dt_obj.getYear() ;
            end_date_obj.value  = GetMonthDays(dt_obj.getMonth()+1, dt_obj.getYear()) + '-' + (dt_obj.getMonth()+1) + '-' + dt_obj.getYear() ;
            break;

        case _THIS_YEAR :
            start_dt_obj.value  = '01' + '-' + '01' + '-' + dt_obj.getYear();
            end_date_obj.value  = '31' + '-' + '12' + '-' + dt_obj.getYear();
            break;

        case _LAST_YEAR :
            start_dt_obj.value  = '01' + '-' + '01' + '-' + ( dt_obj.getYear() + (-1) );
            end_date_obj.value  = '31' + '-' + '12' + '-' + ( dt_obj.getYear() + (-1) );
            break;

        case _THIS_FINANCIAL_YEAR :
            var year            = ( ( dt_obj.getMonth() + 1 ) > 3 ? dt_obj.getYear() : ( dt_obj.getYear() + (- 1) ));
            var end_year        = ( ( dt_obj.getMonth() + 1 ) > 3 ? ( dt_obj.getYear() + 1 ) : ( dt_obj.getYear() ));
            start_dt_obj.value  = '01' + '-' + '04' + '-' + year;
            end_date_obj.value  = '31' + '-' + '03' + '-' + end_year;
            break;
    
        case _LAST_FINANCIAL_YEAR :
            year                = ( ( dt_obj.getMonth() + 1 ) > 3 ? ( dt_obj.getYear() + (- 1) ) : ( dt_obj.getYear() + (- 2)));
            end_year            = ( ( dt_obj.getMonth() + 1 ) > 3 ? ( dt_obj.getYear() ) : ( dt_obj.getYear() + (- 1)));
            start_dt_obj.value  = '01' + '-' + '04' + '-' + year;
            end_date_obj.value  = '31' + '-' + '03' + '-' + end_year;
            break;

        case _TILL_TODAY :
            day_interval        = 0;
            dt_obj              = DateAdd( dt_obj, day_interval, 'D' );
            start_dt_obj.value  = '';
            end_date_obj.value  = dt_obj.getDate() + '-' + (dt_obj.getMonth() + 1) + '-' + dt_obj.getYear();
            break;

        case _MENTION_PERIOD :
            // remove the disabled status of the elt
            start_dt_obj.disabled = false;
            end_date_obj.disabled = false;
            break;
    
        default: 
            start_dt_obj.value = "";
            end_date_obj.value = "";
            // start_dt_obj.disabled = true;    // ????
            // end_date_obj.disabled = true;    // ????
            break;
    }

    return;
}

/// To check if the year is a leap year or not
/**
 * @startcomment
 * Purpose: To check if the year is a leap year or not
 *
 * Input Params:
 * pYear  - String - to get the year to check it is a leap year or not 
 *
 * Output Params:   none
 *
 * Return Value:
 *  true - if leap year
 *  false - if not a leap year
 *
 * Notes:
 * @endcomment
 */
function IsLeapYear ( pYear )
{
    var quotient;
    var is_leap_year = false;

    // If the year is evenly divisible by 4 and not by 100, then this is a leap year
    if( !( pYear%4 ) && ( pYear%100 ) ) is_leap_year = true;

    else
    {
        // If the year is evenly divisible by 4 and 100, then check to
        // see if the quotient of year divided by 100 is also evenly 
        // divisible by 4. If it is, then this is a leap year.
        if( !( pYear%4 ) && !( pYear%100 ) )
        {
            quotient = pYear/100;
            if(!( quotient%4 )) is_leap_year = true;
        }
    }

    return is_leap_year;
}

/// To get number of days in a month
/**
 * @startcomment
 * Purpose: To get number of days in a month
 *
 * Input Params:
 *  pMonth  - Integer - month 
 *  pYear   - Integer - year 
 *
 * Output Params:   none
 *
 * Return Value:
 *  Integer - Number of days in the month
 *
 * Notes:
 * @endcomment
 */
function GetMonthDays ( pMonth, pYear )
{
    switch( parseInt(pMonth) )
    {
        case 1: // Jan
            return 31;

        case 2: // Feb
            if ( IsLeapYear(pYear) == true ) return 29;
            else return 28;

        case 3: // Mar
            return 31;

        case 4: // Apr
            return 30;

        case 5: // May
            return 31;

        case 6: // Jun
            return 30;

        case 7: // Jul
            return 31;

        case 8: // Aug
            return 31;

        case 9: // Sept
            return 30;

        case 10: // Oct
            return 31;

        case 11: // Nov
            return 30;

        case 12: // Dec
            return 31;
    }
}

/// To convert an array to a comma separated string
/**
 * @startcomment
 * Purpose: To convert an array to a comma separated string
 *
 * Input Params:
 *  1.  pArray - obj - array to be commified
 *  2.  pDelimiter - str - the delimiting char (optional)
 *
 * Output Params:   none
 *
 * Return Value:
 *  commified_string - str - string of comma separated values
 *
 * Notes:
 * @endcomment
 */
function CommifyArray ( pArray, pDelimiter )
{
    // default delimiter is ","
	if ( typeof ( pDelimiter ) == "undefined" || pDelimiter == null ) pDelimiter = ",";

    if ( IsStringBlank(pDelimiter) ) pDelimiter = ",";

	var commified_string = "";

    // case : blank array
	if ( pArray == null || pArray.length <= 0 ) return commified_string;

    // loop through the array elements to prepare delimited string
	for ( var i=0; i<pArray.length; i++ ) commified_string = commified_string + ( ( commified_string == "" ) ? "" : pDelimiter ) + pArray[i].toString();

	return commified_string;
}

/// To create the progress bar
/**
 * @startcomment
 * Purpose: To create the progress bar
 *
 * Input Params:
 *  1. pWidth - int - the width of progress bar
 *  2. pHeight - int - the height of the progress bar
 *
 * Output Params:   none
 *
 * Return Value:
 *  intervalobj of the progress bar
 *
 * Notes:
 *  1. The progress bar can be stopped by calling clearInterval(intervalobj)
 *  2. Implemented in GLibProject.php
 * @endcomment
 */
function CreateProgressBar( pWidth, pHeight )
{
    // is IE
    var is_IE = (document.all) ? true : false;

    // is browser w3c compliant
    var is_w3c = (document.getElementById) ? true : false;

    // progress speed
    var speed = 85;

    // number of progress blocks
    num_blocks = 7;

    // max counter ???? unsure what this is required for
    count = 3;

    // case : progress bar will work only in IE or w3c compliant browsers
    if( is_IE || is_w3c )
    {
        var html = '<div id="ProgressBarContainer" style="visibility:visible; position:relative; overflow:hidden; width:'+pWidth+'px; height:'+pHeight+'px; background-color:white; border-color:black; border-width:1px; border-style:solid; font-size:1px;">';
        html += '<span id="ProgressBarBlocks" style="left:-' + (pHeight*2+1) + 'px; position:absolute; font-size:1px">';

        for( i=0; i<num_blocks; i++ )
        {
            html += '<span style="background-color:blue; left:-' + ((pHeight*i)+i) + 'px; font-size:1px; position:absolute; width:' + pHeight + 'px; height:' + pHeight + 'px; ';
            html += (is_IE) ? 'filter:alpha(opacity=' + (100-i*(100/num_blocks))+ ')' : '-Moz-opacity:'+((100-i*(100/num_blocks))/100);
            html += '"></span>';
        }
        html += '</span></div>';

        document.write(html);

        var progress_bar = (is_IE)?document.all['ProgressBarBlocks']:document.getElementById('ProgressBarBlocks');
        progress_bar.bar = (is_IE)?document.all['ProgressBarContainer']:document.getElementById('ProgressBarContainer');
        progress_bar.Blocks = num_blocks;
        progress_bar.BarWidth = pWidth;
        progress_bar.BarHeight = pHeight;
        progress_bar.Speed = speed;
        progress_bar.Counter = 0;
        progress_bar.Count = count;
        progress_bar.IntervalId = setInterval( 'StartProgressBar()', speed );

        return progress_bar.IntervalId;
    }
}

/// To start the progress bar
/**
 * @startcomment
 * Purpose: To start the progress bar
 *
 * Input Params:
 *  1.  pBarNumber - str - the bar that is to start progressing
 *
 * Output Params:   none
 *
 * Return Value:    none
 *
 * Notes:
 *  1. Implemented in GLibProject.php
 * @endcomment
 */
function StartProgressBar ()
{
    var is_IE = ( document.all ) ? true : false;

    var span_obj = (is_IE) ? document.all['ProgressBarBlocks'] : document.getElementById('ProgressBarBlocks');

    if( parseInt(span_obj.style.left) + span_obj.BarHeight + 1 - ( span_obj.Blocks * span_obj.BarHeight + span_obj.Blocks ) > span_obj.BarWidth )
    {
        span_obj.style.left=-(span_obj.BarHeight*2+1)+'px';

        span_obj.Counter++;

        if(span_obj.Counter>=span_obj.Count)
        {
            span_obj.Counter=0;
        }
    }

    else span_obj.style.left=(parseInt(span_obj.style.left)+span_obj.BarHeight+1)+'px';
}

/// to make a client-side request to the server, and obtain the response
/**
 * Purpose: to make a client-side request to the server, and obtain the response
 *
 * Input Params:
 *  1. pURL - string - request URL
 *  2. pPostVars - string - POST variables
 *  3. pCallbackFunc - function reference - function that is to be called on successful response
 *  4. pAsync - int - if request to be asynchronous - 1/0
 *
 * Output Params:   none
 *
 * Return Value:
 *  on success - string - the response from the server
 *  on failure - bool - false
 *
 * Notes:
 *  1. By default the request will *NOT* be asynchronous. This behavour can be changed by
 *     the param pAsync. 1 is for asynchronous, and 0 is for synchronous.
 *  2. By default the request method is GET. If however pPostVars is passed to the func, then
 *     the request method will be POST.
 *     pPostVars will a set of post variables as "var1=val1&var2=val2&var3=val3&varn=valn"
 *  3. pCallbackFunc is a reference to a JS function. If specified, this function must mandatorily 
 *     take a parameter to which the server response string will be passed.
 *  4. In most cases, if the request is going to be asynchronous, then it is always better
 *     to pass a callback function to do the needful scripting after the server response.
 */
function GetServerResponse ( pURL, pPostVars, pCallbackFunc, pAsync )
{
    var http_request = false,
        http_method = "GET",
        request_type = false,
        server_response;

    if ( pPostVars && pPostVars != "") http_method = "POST";
    if ( pAsync && pAsync == 1 ) request_type = true;

    // Evil IE, use ActiveXObject as much as possible
    // IE 7 & above have native XMLHttpRequest
    // But ie7 allows switching it off!!!
    if ( window.ActiveXObject ) {
        /*
        src: http://snook.ca/archives/javascript/xmlhttprequest_activex_ie/
        http://blogs.msdn.com/xmlteam/archive/2006/10/23/using-the-right-version-of-msxml-in-internet-explorer.aspx
        */
        try { http_request = new ActiveXObject ( "MSXML2.XMLHTTP.6.0" ); }
        catch (e) {
            try { http_request = new ActiveXObject ( "MSXML2.XMLHTTP" ); }
            catch (e) { http_request = false;}
        }
    }
    // Good browsers...
    else if ( window.XMLHttpRequest ) {
        http_request = new XMLHttpRequest ();

        // some versions of Mozilla browsers won't work properly if 
        // response from server doesn't have xml mime-type header
        if ( http_request.overrideMimeType ) http_request.overrideMimeType ( "text/xml" );
    }

    if (! http_request) {
        alert('Error : Cannot create an XMLHTTP instance');
        return false;
    }

    //Asynchronous req, on State changes, AJAX obj will fire events
    if (request_type === true) {
    // return server output on successful retrieval
    http_request.onreadystatechange = function() {
            //completed
            if ( http_request.readyState == 4 ) {
            // successfully got response
                if ( http_request.status == 200 ) {

                server_response = http_request.responseText;
                    // case : if callback function specified, pass on the server response string to it as a parameter
                    if ( pCallbackFunc && typeof(pCallbackFunc) === "function" ) { 
                        pCallbackFunc(http_request.responseText);
            }
                } //status
                else {
                alert ( 'Error : Server returned a status code : ' + http_request.status );
                server_response = false;
            }
            } //ready state
        } //func
        }

    // GET method
    if ( http_method === "GET" ) {
        http_request.open ( "GET", pURL, request_type );
        http_request.send ( null );
    }

    // POST method
    else if ( http_method === "POST" ) {
        http_request.open ( "POST", pURL, request_type );
        http_request.setRequestHeader ( "Content-Type", "application/x-www-form-urlencoded" );
        //Some versions of Mozilla also require, the usage of these two headers
        //var numBytes = (""+pPostVars).length;
        //http_request.setRequestHeader ( "Content-Length: "+numBytes);
        //http_request.setRequestHeader ( "Connection: Close");
        http_request.send ( pPostVars );
    }

    //sync requests
    if ( ! request_type) { 
        server_response = http_request.responseText;

        if (pCallbackFunc && typeof(pCallbackFunc) === "function" ) {
            pCallbackFunc(http_request.responseText);
        }
    }

    return server_response;
}
