/*
This and all associated code is the sole property of Professional
Credit Services. PCS reserves all rights to this code. 
*/

//
// checkDate
// dateObj - input field JS Object
// returns - none
//

function checkDate(dateObj)
{
    // function setup
    var today      = new Date();
    var todayYear  = '' + today.getFullYear();
    var century    = todayYear.substr(0, 2);
    var validChars = '0123456789';
    var reformat   = true;
    
    // variables set throughout processing
    var dateString = '';
    var error = new Array();
    var temp1;
    var temp2;
    var i;
    
    // remove any non-numeric characters (including slashes (/))
    for (i = 0; i < dateObj.value.length; i++){
        if (validChars.indexOf(dateObj.value.substr(i, 1)) >= 0){
            dateString = dateString + dateObj.value.substr(i, 1);
        }
   	}
    
    // check the length of the date
    if(dateString.length == 6 || dateString.length == 8){
        if(dateString.length == 6){
            // we have matched a short year format, so we change it to the full format
            dateString = dateString.substr(0, 4) + century + dateString.substr(4, 2);
        }
        
        // check month
        temp1 = dateString.substr(0, 2);
        // basically, make sure the month is between 1 and 12
        // if not update the dateString to be the closest valid month
        if(0 >= temp1 || temp1 > 12){
            error.push('The month (' + temp1 + ') is not correct. Must be between 01 and 12.');
            if(temp1 > 12){
                dateString = '12' + dateString.substr(2, 6);
            } else {
                dateString = '01' + dateString.substr(2, 6);
            }
        }

        // check the year
        temp1 = century - 1;
        temp1 = temp1 + '00'; // beginning of the previous century
        temp1 = (temp1 / 1); // make sure our data type for this variable is a number
        temp2 = (dateString.substr(4, 4) / 1); // the year they submitted
        if((temp1 > temp2 || temp2 > (todayYear / 1))){
            if(temp1 > temp2){
                error.push('The year must be within the last century (more recent than ' + temp1 + ').');
            } else {
                temp1 = (todayYear / 1);
                error.push('The year can only be one year into the future (cannot be greater than ' + temp1 + ').');
            }
            // update the dateString with the closest valid year for what they inputted
            dateString = dateString.substr(0, 4) + temp1;
        }
        
        // check day
        temp1 = dateString.substr(2, 2);
        temp2 = getDaysInMonth(dateString.substr(0, 2), dateString.substr(4, 4));
        // make sure the day is between 1 and 28-31 (depending on the month they entered)
        if((0 >= temp1 || temp1 > temp2)){
            error.push('The day (' + temp1 + ') is not correct. Must be between 01 and ' + temp2 +
                       ' for the month you have chosen.');
            // update the dateString with the closest valid day
            if(temp1 > temp2){
                dateString = dateString.substr(0,2) + temp2 + dateString.substr(4, 4);
            } else {
                dateString = dateString.substr(0,2) + '01' + dateString.substr(4, 4);
            }
        }
        
    } else {
        if(dateString.length == 0){
            reformat = false;
        } else {
            // we either have too many or too few characters in the data
            // in these error messages we are going to append correct date formats
            reformat = false;
            if(dateString.length > 8){
                error.push('There are too many digits in your date.' + getPossibleDateFormats());
            } else {
                error.push('There are too few digits in your date.' + getPossibleDateFormats());
            }
        }
    }
    
    // check if there are errors, if so, tell the user
    if(error.length > 0)
    {
        temp1 = 'There are the following errors in the date entered:\n\n';
        for(i = 0; i < error.length; i++){
            temp1 += '  ' + error[i] + "\n";
        }
        alert(temp1);
    }
    
    // we've been updating a dateString variable with the best formatted date,
    // so let's replace the user's input, unless there was an error due to the number
    // of digits entered in the date
    if(reformat){
        dateObj.value = dateString.substr(0, 2) + '/' + dateString.substr(2, 2) + '/' + dateString.substr(4, 4);
    }
}

//
// getDaysInMonth
// month - the month to get the days for
// year  - the year to get the days for
// returns - the number of days in the month
//

function getDaysInMonth(month, year)
{
    // default days per month
    // index 0 is january and index 11 is december
    var regularDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    
    // figure out if it is a leap year
    if((year % 4) == 0){
        if((year % 100) > 0){
            // it is a leap year, so we update february
            regularDays[1] = 29;
        } else {
            if((year % 400) == 0){
                // it is a leap year, so we update february
                regularDays[1] = 29;
            }
        }
    }
    
    return regularDays[(month / 1) - 1];
}

//
// getPossibleDateFormats
// returns - a string of date examples using today's date as an example
//

function getPossibleDateFormats()
{
    var today      = new Date();
    var todayDay   = today.getDate();
    var todayMonth = today.getMonth() + 1;
    var todayYear  = '' + today.getFullYear();
    
    var returnString = '\n\nPossible formats are ';
    
    // possible date input formats are MM/DD/YYYY, MM/DD/YY, MMDDYYYY, and MMDDYY
    
    returnString += leadingZero(todayMonth) + '/' + leadingZero(todayDay) + '/' + todayYear + ', ';
    returnString += leadingZero(todayMonth) + '/' + leadingZero(todayDay) + '/' + todayYear.substr(2, 2) + ', ';
    returnString += leadingZero(todayMonth) + leadingZero(todayDay) + todayYear + ', and ';
    returnString += leadingZero(todayMonth) + leadingZero(todayDay) + todayYear.substr(2, 2) + '.';
    
    return returnString;
}

//
// leadingZero
// number - a day of the month or month
// returns - a 2-digit day of month or month (with leading zeros if necisary)
//

function leadingZero(number)
{
    // turn it into a string so we can use the length() string function
    var returnString = '' + number;
    
    if(returnString.length == 1){
        returnString = '0' + returnString;
    }
    
    return returnString;
}
