var sTimeFormat = "h:mm AM/PM";

var months_short = new Array('J', 'F', 'M', 'A', 'M', 'J', 'J', 'A', 'S', 'O', 'N', 'D');
var months_three = new Array('JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC');
var months_complete = new Array('JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER');


var month_january = new Array('J', 'Ja', 'Jan', 'Janu', 'Janua', 'Januar', 'Janury', 'JANUARY');
var month_february = new Array('F', 'FEB', 'FEBRUARY', 'Fe', 'Fb', 'Fbrury', 'febu', 'febr', 'febru', 'februa', 'februar');
var month_march = new Array('M', 'MA', 'MAR', 'MARCH', 'marc');
var month_april = new Array('A', 'APR', 'APRIL', 'Ap', 'Aprl', 'apri');
var month_may = new Array('M', 'MAY', 'MA');
var month_june = new Array('J', 'JUN', 'JUNE', 'JN', 'ju');
var month_july = new Array('J', 'JUL', 'JULY', 'JL');
var month_august = new Array('A', 'AUG', 'AUGUST', 'Au', 'Ag', 'Agst', 'augu', 'augus');
var month_september = new Array('S', 'SEP', 'SEPTEMBER', 'Sp', 'Se', 'Sptmbr', 'sept', 'septe', 'septem', 'septemb', 'septembe');
var month_october = new Array('O', 'OCT', 'OCTOBER', 'Oc', 'Octbr', 'octo', 'octob', 'octobe');
var month_novermber = new Array('N', 'NOV', 'NOVEMBER', 'No', 'Novmbr', 'nove', 'novem', 'novemb', 'novembe');
var month_december = new Array('D', 'DEC', 'DECEMBER', 'De', 'Decmbr', 'dece', 'decem', 'decemb', 'decembe');

var months = new Array(12);
months[0] = month_january;
months[1] = month_february;
months[2] = month_march;
months[3] = month_april;
months[4] = month_may;
months[5] = month_june;
months[6] = month_july;
months[7] = month_august;
months[8] = month_september;
months[9] = month_october;
months[10] = month_novermber;
months[11] = month_december;


var day_monday = new Array('M', 'MN', 'Mon', 'Mnday', 'Mondy', 'Mndy', 'Mon', 'Monday', 'Mond', 'Monda');
var day_tuesday = new Array('Tue', 'Tu', 'Tus', 'Tusday', 'Tusdy', 'T', 'Tuesday', 'Tues', 'Tuesd', 'Tuesda');
var day_wednesday = new Array('Wed', 'We', 'Wnsdy', 'Wednsday', 'Wednsdy', 'W', 'Wednesday', 'Wedn', 'Wedne', 'Wednes', 'Wednesd', 'Wednesda');
var day_thursday = new Array('T', 'Thu', 'Th', 'Thrsdy', 'Thursday', 'Thur', 'Thurs', 'Thursd', 'Thursda');
var day_friday = new Array('F', 'Fr', 'Fri', 'Fridy', 'Friday', 'Frid', 'Frida');
var day_saturday = new Array('S', 'SAT', 'SATURDAY', 'STRDAY', 'SATRDAY', 'sa', 'satu', 'satur', 'saturd', 'saturda');
var day_sunday = new Array('S', 'SUN', 'SUNDAY', 'SNDAY', 'su', 'sund', 'sunda');

var tomorrow_patterns = new Array('TOM', 'TOMO', 'TOMOR', 'TOMORR', 'TOMORRO', 'TOMORROW');
var today_patterns = new Array('TO', 'TOD', 'TODA', 'TODAY', 'TODAT', 'TODATE');
var next_patterns = new Array('NE', 'NEX', 'NEXT');
var this_patterns = new Array('THI', 'THIS');

var days = new Array(7);
days[0] = day_monday;
days[1] = day_tuesday;
days[2] = day_wednesday;
days[3] = day_thursday;
days[4] = day_friday;
days[5] = day_saturday;
days[6] = day_sunday;

var daysofmonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var daysofmonthLY = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);


function leapYear(year) {
    if ((year / 4) != Math.floor(year / 4))   return false;
    if ((year / 100) != Math.floor(year / 100)) return true;
    if ((year / 400) != Math.floor(year / 400)) return false;
    return true;
}


var dayFound = false;
var day = -1;

var monthFound = false;
var month = -1;

var yearFound = false;
var year = -1;

var timeFound = false;
var time = -1;

var today = false;
var tomorrow = false;
var next = false;
var _this = false;
var comparisonDate;

function getFirstCharacters(str, length) {
    var result = "";
    for (var i = 0; i < str.length && i < length; i++) {
        result = result + str.charAt(i);
    }
    return result;
}

function getRemovedPunctuationString(str) {
    str = replaceChar(str, ',', '', true);
    return str;
}


function removeSuffix(str) {
    if (str.indexOf('st') > -1 || str.indexOf('nd') > -1
            || str.indexOf('rd') > -1 || str.indexOf('th') > -1 || str.indexOf('s') > -1) {
        str = str.replace('th', '').replace('st', '').replace('nd', '').replace('rd', '').replace('s', '');
    }
    return str;
}


function replaceChar(str, targetChar, withChar, ignoreCase) {
    var result = "";
    if (ignoreCase) {
        for (var i = 0; i < str.length; i++) {
            if (str.charAt(i).toLowerCase() != targetChar.toLowerCase()) {
                result = result.concat(str.charAt(i));
            }
        }
    }
    else {
        for (i = 0; i < str.length; i++) {
            if (str.charAt(i) != targetChar) {
                result = result.concat(str.charAt(i));
            }
        }
    }
    return result;
}

                    // Check that a string contains only letters and numbers
function isAlphanumeric(string, ignoreWhiteSpace) {
    if (string.search) {
        if ((ignoreWhiteSpace && string.search(/[^\w\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\W/) != -1)) return false;
    }
    return true;
}

                    // Check that a string contains only letters
function isAlphabetic(string, ignoreWhiteSpace) {
    if (string.search) {
        if ((ignoreWhiteSpace && string.search(/[^a-zA-Z\s]/) != -1) || (!ignoreWhiteSpace && string.search(/[^a-zA-Z]/) != -1)) return false;
    }
    return true;
}

                    // Check that a string contains only numbers
/*function !isNaN(string, ignoreWhiteSpace) {
    if (!isNaN(string)) {
        if (string.search) {
            if ((ignoreWhiteSpace && string.search(/[^\d\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\D/) != -1)) {
                return false;
            }
        }
    }
    else {
        return false;
    }
    return true;
}*/


function getMonthFromNumeric(monthValue) {
    if (monthValue > 0 && monthValue <= 12) {
        return monthValue - 1;
    }
    return -1;
}


function getMonth(monthName) {

    for (var i = 0; i < months.length; i++) {
        for (var j = 0; j < months[i].length; j++) {
            if (monthName.toLowerCase() == months[i][j].toLowerCase()) {
                return i;
            }
        }
    }
    return -1;

}


function getDayforParser(dayName) {
    var currentDate = new Date();
    if (comparisonDate != null && comparisonDate.toString().length > 0) {
        currentDate = comparisonDate;
    }
    for (var i = 0; i < days.length; i++) {
        for (var j = 0; j < days[i].length; j++) {
            if (dayName.toLowerCase() == days[i][j].toLowerCase()) {
                if (i >= (currentDate.getDay() - 1))
                {
                    return currentDate.getDate() + ( (i - (currentDate.getDay() - 1)));
                }
                else {
                    return currentDate.getDate() + ( (i - (currentDate.getDay() - 1)) + 7);
                }
            }
        }
    }
    return -1;

}


function getTime(time) {
    if (time.indexOf(".") > -1 && time.indexOf(":") < 0)
    {
        time = time.replace('.', ':');
    }
    return getStringTime(time);
}


function getCurrentOrActual(month) {
    var currentMonth = getCurrentMonth();
    if (( currentMonth == 0 || currentMonth == 5 || currentMonth == 6 ) && ( month == 0 || month == 5 || month == 6 )) {
        return currentMonth;
    }
    if (( currentMonth == 2 || currentMonth == 4 ) && ( month == 2 || month == 4 )) {
        return currentMonth;
    }
    if (( currentMonth == 3 || currentMonth == 7 ) && ( month == 3 || month == 7 )) {
        return currentMonth;
    }
    return month;
}

function getCurrentMonth() {
    var d = new Date();
    if (comparisonDate != null && comparisonDate.toString().length > 0) {
        d = comparisonDate;
    }
    return d.getUTCMonth();
}

function searchDate(listOfItems) {
    if (listOfItems.length > 1) {
        for (var i = 0; i < listOfItems.length; i++) {
            if (i == 0)
            {
                if ((!isNaN(listOfItems[i]) || isAlphabetic(listOfItems[i]))) {
                    month = searchMonth(listOfItems[i]);
                    monthFound = ( month != -1 );
                    if (!monthFound) {
                        day = searchDay(listOfItems[i]);
                        dayFound = (day != -1);
                    }
                    if (!monthFound && !dayFound)
                    {
                        if (existsInArray(today_patterns, listOfItems[i]) != -1/*'today'*/) {
                            today = true;
                        }
                        else if (existsInArray(tomorrow_patterns, listOfItems[i]) != -1/*'tomorrow'*/) {
                            tomorrow = true;
                        }
                        else if (existsInArray(next_patterns, listOfItems[i]) != -1/*'next'*/) {
                            next = true;
                        }
                        else if (existsInArray(this_patterns, listOfItems[i]) != -1/*'this'*/) {
                            _this = true;
                        }
                    }
                }
                else if (listOfItems[i].indexOf(".") > -1 || listOfItems[i].indexOf("/") > -1 || listOfItems[i].indexOf("-") > -1)
                {
                    var dottedString = "";
                    if (listOfItems[i].indexOf(".") > -1)
                    {
                        dottedString = listOfItems[i].split(".");
                    }
                    if (listOfItems[i].indexOf("/") > -1)
                    {
                        dottedString = listOfItems[i].split("/");
                    }
                    if (listOfItems[i].indexOf("-") > -1)
                    {
                        dottedString = listOfItems[i].split("-");
                    }

                    for (var j = 0; j < dottedString.length; j++) {
                        if (j == 0)
                        {
                            if (!isNaN(dottedString[j]))
                            {
                                month = searchMonth(dottedString[j]);
                                monthFound = month != -1;
                                if (!monthFound) {
                                    day = searchDay(dottedString[j]);
                                    dayFound = (day != -1);
                                }
                            }
                            if (isAlphabetic(dottedString[j]))
                            {
                                month = searchMonth(dottedString[j]);
                                monthFound = month != -1;
                                if (!monthFound) {
                                    day = searchDay(dottedString[j]);
                                    dayFound = (day != -1);
                                }
                            }
                        }
                        if (j == 1)
                        {
                            if (!monthFound && dayFound && (!isNaN(dottedString[j]) || isAlphanumeric(dottedString[j])))
                            {
                                month = searchMonth(dottedString[j]);
                                monthFound = month != -1;
                            }
                            else if (dayFound && (!isNaN(dottedString[j]) || isAlphanumeric(dottedString[j])))
                            {
                                time = searchTime(removeSuffix(dottedString[j]));
                                timeFound = time != -1;
                            }
                            else if (monthFound && (!isNaN(dottedString[j]) || isAlphanumeric(dottedString[j])))
                            {
                                day = searchDay(removeSuffix(dottedString[j]));
                                if (!yearFound)
                                {
                                    var currentDate = new Date();
                                    if (comparisonDate != null && comparisonDate.toString().length > 0) {
                                        currentDate = comparisonDate;
                                    }
                                    year = currentDate.getUTCFullYear();
                                }
                                if (leapYear(year) && day > daysofmonthLY[month])
                                {

                                    monthFound = false;
                                    dayFound = false;
                                }
                                else if (day > daysofmonth[month])
                                {
                                    monthFound = false;
                                    dayFound = false;
                                }

                                if (!monthFound && !dayFound)
                                {
                                    time = searchTime(listOfItems[i]);
                                    timeFound = (time != null && time != "undefined" && time != -1 );
                                }
                                else
                                {
                                    dayFound = day != -1;
                                }
                            }
                        }
                        if (j == 2)
                        {
                            if (monthFound && dayFound && (!isNaN(dottedString[j]) || isAlphanumeric(dottedString[j])))
                            {
                                year = searchYear(dottedString[j]);
                                yearFound = year != -1;
                            }
                        }
                    }
                }
                else if (isAlphanumeric(listOfItems[i]))
                {
                    var number = "";
                    var alphabets = "";
                    for (var x = 0; x < listOfItems[i].length; x++)
                    {
                        number = listOfItems[i].charAt(x);
                        var characterCode = number.charCodeAt(0);
                        if (characterCode > 47 && characterCode < 58)
                        {
                            number = listOfItems[i].substring(x);
                            alphabets = listOfItems[i].substring(0, x);
                            break;
                        }
                    }

                    month = searchMonth(alphabets);
                    monthFound = month != -1;
                    day = searchDay(number);
                    dayFound = (day != -1);
                }

            }
            if (i == 1) {
                if (!isNaN(listOfItems[i])) {
                    // 2nd Position if numeric => day
                    if (timeFound && !monthFound && !dayFound &&
                        listOfItems.length == 2) {
                        month = searchMonth(listOfItems[i]);
                        monthFound = ( month != -1 );
                    }
                        // 2nd Position if numeric => day
                    else if (monthFound && dayFound &&
                             listOfItems.length == 2) {
                        time = searchTime(listOfItems[i]);
                        timeFound = (time != null && time != "undefined" && time != -1 );
                    }
                    else if (monthFound && !dayFound &&
                             listOfItems.length == 3 &&
                             isAlphabetic(listOfItems[i - 1])
                            ) {

                        day = searchDay(listOfItems[i]);
                        dayFound = (day != -1);
                    }
                    else if (!monthFound && !dayFound && (today || tomorrow)) {
                        time = searchTime(listOfItems[i]);
                        timeFound = (time != -1 );
                    }
                    else if (!dayFound) {
                        day = searchDay(listOfItems[i]);
                        dayFound = (day != -1);
                        /*if (!dayFound) {
                            year = searchYear(listOfItems[i]);
                            yearFound = (year != -1);
                        }*/
                    }
                    else if (dayFound && !monthFound) {
                        time = searchTime(listOfItems[i]);
                        timeFound = (time != null && time != "undefined" && time != -1 );
                    }
                }
                else if (isAlphabetic(listOfItems[i])) {
                    // 2nd Position if alpha => month
                    month = searchMonth(listOfItems[i]);
                    if (_this)
                    {
                        day = searchDay(listOfItems[i]);
                        dayFound = (day != -1);
                    }
                    else if (next)
                    {
                        day = searchDay(listOfItems[i]);
                        dayFound = (day != -1);
                    }
                    else if (monthFound && month != -1) {
                        day = month + 1;
                        dayFound = true;
                    }
                    else if (!isNaN(listOfItems[i - 1])) {
                        searchDay(listOfItems[i - 1]);
                    }
                    monthFound = ( month != -1 );
                }
                else if (isAlphanumeric(listOfItems[i])) {
                    if (!monthFound && dayFound &&
                        listOfItems.length == 2) {
                        time = searchTime(listOfItems[i]);
                        timeFound = (time != null && time != "undefined" && time != -1 );
                    }
                    else if (monthFound && dayFound &&
                             listOfItems.length == 2) {
                        time = searchTime(listOfItems[i]);
                        timeFound = (time != null && time != "undefined" && time != -1 );
                    }
                    else if (monthFound && !dayFound &&
                             isAlphabetic(listOfItems[i - 1])) {
                        day = searchDay(removeSuffix(listOfItems[i]));
                        dayFound = (day != -1);
                    }
                    else if (monthFound && !dayFound &&
                             listOfItems[i - 1].indexOf(".") > -1) {
                        day = searchDay(removeSuffix(listOfItems[i]));
                        dayFound = (day != -1);
                    }
                    else if (!monthFound && !dayFound && (today || tomorrow)) {
                        time = searchTime(listOfItems[i]);
                        timeFound = (time != -1 );
                    }
                }
                else if (!today && !tomorrow && !dayFound && !monthFound && (listOfItems[i].indexOf("-") > -1 || listOfItems[i].indexOf(".") > -1))
                {
                    if (listOfItems[i].indexOf(".") > -1)
                    {
                        dottedString = listOfItems[i].split(".");
                    }
                    if (listOfItems[i].indexOf("-") > -1)
                    {
                        dottedString = listOfItems[i].split("-");
                    }

                    for (j = 0; j < dottedString.length; j++) {
                        if (j == 0)
                        {

                            if (!isNaN(dottedString[j]))
                            {
                                if (timeFound && !monthFound && !dayFound)
                                {
                                    month = searchMonth(dottedString[j]);
                                    monthFound = ( month != -1 );
                                }
                                else
                                {
                                    day = searchDay(dottedString[j]);
                                    dayFound = day != -1;
                                }
                            }
                        }
                        if (j == 1)
                        {
                            if (timeFound && monthFound && !dayFound)
                            {
                                day = searchDay(dottedString[j]);
                                dayFound = ( day != -1 );
                            }
                            else if (monthFound && dayFound && (!isNaN(dottedString[j]) || isAlphanumeric(dottedString[j])))
                            {
                                time = searchTime(dottedString[j]);
                                timeFound = (time != null && time != "undefined" && time != -1 );
                            }
                        }
                    }
                }
                else if (monthFound && dayFound && (listOfItems[i].indexOf("-") > -1 || listOfItems[i].indexOf(".") > -1))
                {
                    time = searchTime(listOfItems[i]);
                    timeFound = (time != null && time != "undefined" && time != -1 );
                }
                else if (monthFound && (listOfItems[i].indexOf("-") > -1 || listOfItems[i].indexOf(".") > -1))
                {
                    if (listOfItems[i].indexOf(".") > -1)
                    {
                        dottedString = listOfItems[i].split(".");
                    }
                    if (listOfItems[i].indexOf("-") > -1)
                    {
                        dottedString = listOfItems[i].split("-");
                    }

                    for (j = 0; j < dottedString.length; j++) {
                        if (j == 0)
                        {

                            if (!isNaN(dottedString[j]))
                            {
                                if (timeFound && !monthFound && !dayFound)
                                {
                                    month = searchMonth(dottedString[j]);
                                    monthFound = ( month != -1 );
                                }
                                else
                                {
                                    day = searchDay(dottedString[j]);
                                    dayFound = day != -1;
                                }
                            }
                        }
                        if (j == 1)
                        {
                            if (timeFound && monthFound && !dayFound)
                            {
                                day = searchDay(dottedString[j]);
                                dayFound = ( day != -1 );
                            }
                            else if (monthFound && dayFound && (!isNaN(dottedString[j]) || isAlphanumeric(dottedString[j])))
                            {
                                time = searchTime(dottedString[j]);
                                timeFound = (time != null && time != "undefined" && time != -1 );
                            }
                        }
                    }
                }
                else
                {
                    if (monthFound && dayFound &&
                        listOfItems.length == 2) {
                        time = searchTime(listOfItems[i]);
                        timeFound = (time != null && time != "undefined" && time != -1 );
                    }
                    if (!monthFound && !dayFound && (today || tomorrow)) {
                        time = searchTime(listOfItems[i]);
                        timeFound = (time != -1 );
                    }
                    else if (dayFound && !monthFound) {
                        time = searchTime(listOfItems[i]);
                        timeFound = (time != null && time != "undefined" && time != -1 );
                    }
                }

            }
            if (i == 2 && !yearFound) {


                if (!isAlphabetic(listOfItems[i]))
                {
                    if (monthFound && dayFound &&
                        isAlphanumeric(listOfItems[i - 1]) && isAlphabetic(listOfItems[i - 2])) {
                        time = searchTime(listOfItems[i]);
                        timeFound = (time != null && time != "undefined" && time != -1 );
                    }
                    if (monthFound && dayFound &&
                        isAlphanumeric(listOfItems[i - 1]) && listOfItems[i - 2].indexOf(".") > -1) {
                        time = searchTime(listOfItems[i]);
                        timeFound = (time != null && time != "undefined" && time != -1 );
                    }
                }
            }
            if (i == 3) {
                if (monthFound && dayFound &&
                    isAlphanumeric(listOfItems[i - 2]) && isAlphabetic(listOfItems[i - 3]) && isAlphabetic(listOfItems[i - 1])) {
                    time = searchTime(listOfItems[i]);
                    timeFound = (time != null && time != "undefined" && time != -1 );
                }
                if (monthFound && dayFound &&
                    isAlphanumeric(listOfItems[i - 2]) && listOfItems[i - 3].indexOf(".") > -1 && isAlphabetic(listOfItems[i - 1])) {
                    time = searchTime(listOfItems[i]);
                    timeFound = (time != null && time != "undefined" && time != -1 );
                }
            }
        }
    }
}

function searchYear(_year) {
    //  02 like case => 2002
    if (!isNaN(_year, true)) {
        if (_year.length == 2) {
            return 20 + _year;
        }
        else if (_year.length == 4) {
            return _year;
        }
    }
    return -1;
}

function searchMonth(_month) {
    if (isAlphabetic(_month, true)) {
        return getMonth(_month);
    }
    else if (!isNaN(_month, true)) {
        return getMonthFromNumeric(_month);
    }
    return -1;
}

function searchDay(_day) {
    if (isAlphabetic(_day, true)) {
        return getDayforParser(_day);
    }
    else if (!isNaN(_day)) {
        // TODO: day's according to month (i.e., 28 for February)
        if (_day > 0 && _day <= 31) {
            return _day;
        }
    }
    return -1;
}


function searchTime(_time) {
    return getTime(_time);
}


function search(controlValue) {

    if (!isNaN(controlValue))
    {
        month = searchMonth(controlValue);
        monthFound = month != -1;
        if (!monthFound) {
            day = searchDay(controlValue);
            dayFound = (day != -1);
        }
    }
    else if (isAlphabetic(controlValue, true)) {

        month = searchMonth(controlValue);
        monthFound = month != -1;

        if (!monthFound) {
            day = searchDay(controlValue);
            dayFound = (day != -1);
        }

        if (!monthFound && !dayFound)
        {
            if (existsInArray(today_patterns, controlValue) != -1/*'today'*/) {
                today = true;
            }
            else if (existsInArray(tomorrow_patterns, controlValue) != -1/*'tomorrow'*/) {
                tomorrow = true;
            }
            else if (existsInArray(next_patterns, controlValue) != -1/*'next'*/) {
                next = true;
            }
            else if (existsInArray(this_patterns, controlValue) != -1/*'this'*/) {
                _this = true;
            }
        }
    }
    else if (isAlphanumeric(controlValue, true)) {
        var number = "";
        var alphabets = "";
        for (var j = 0; j < controlValue.length; j++)
        {
            number = controlValue.charAt(j);
            var characterCode = number.charCodeAt(0);
            if (characterCode > 47 && characterCode < 58)
            {
                number = controlValue.substring(j);
                alphabets = controlValue.substring(0, j);
                break;
            }
        }

        month = searchMonth(alphabets);
        monthFound = month != -1;
        day = searchDay(number);
        dayFound = (day != -1);
    }
    else if (controlValue.indexOf(".") > -1 || controlValue.indexOf("/") > -1 || controlValue.indexOf("-") > -1)
    {
        var dottedString = "";
        if (controlValue.indexOf(".") > -1)
        {
            dottedString = controlValue.split(".");
        }
        if (controlValue.indexOf("/") > -1)
        {
            dottedString = controlValue.split("/");
        }
        if (controlValue.indexOf("-") > -1)
        {
            dottedString = controlValue.split("-");
        }

        for (var i = 0; i < dottedString.length; i++) {
            if (i == 0)
            {
                if (!isNaN(dottedString[i]))
                {
                    month = searchMonth(dottedString[i]);
                    monthFound = month != -1;
                    if (!monthFound) {
                        day = searchDay(dottedString[i]);
                        dayFound = (day != -1);
                    }
                }
                if (isAlphabetic(dottedString[i]))
                {
                    month = searchMonth(dottedString[i]);
                    monthFound = month != -1;
                    if (!monthFound) {
                        day = searchDay(dottedString[i]);
                        dayFound = (day != -1);
                    }
                }
            }
            if (i == 1)
            {
                if (!monthFound && dayFound && (!isNaN(dottedString[i]) || isAlphanumeric(dottedString[i])))
                {
                    month = searchMonth(dottedString[i]);
                    monthFound = month != -1;
                }
                else if (dayFound && (!isNaN(dottedString[i]) || isAlphanumeric(dottedString[i])))
                {
                    time = searchTime(removeSuffix(dottedString[i]));
                    timeFound = time != -1;
                }
                else if (monthFound && (!isNaN(dottedString[i]) || isAlphanumeric(dottedString[i])))
                {
                    day = searchDay(removeSuffix(dottedString[i]));
                    if (!yearFound)
                    {
                        var currentDate = new Date();
                        if (comparisonDate != null && comparisonDate.toString().length > 0) {
                            currentDate = comparisonDate;
                        }
                        year = currentDate.getUTCFullYear();
                    }

                    if (leapYear(year) && day > daysofmonthLY[month])
                    {
                        monthFound = false;
                        dayFound = false;
                    }
                    else if (day > daysofmonth[month])
                    {
                        monthFound = false;
                        dayFound = false;
                    }

                    if (!monthFound && !dayFound)
                    {
                        time = searchTime(controlValue);
                        timeFound = (time != null && time != "undefined" && time != -1 );
                    }
                    else
                    {
                        dayFound = day != -1;
                    }
                }
            }
            if (i == 2)
            {
                if (monthFound && dayFound && (!isNaN(dottedString[i]) || isAlphanumeric(dottedString[i])))
                {
                    year = searchYear(dottedString[i]);
                    yearFound = year != -1;
                }
            }
        }
    }
}


function getDate() {
    var _date = new Date();
    if (comparisonDate != null && comparisonDate.toString().length > 0) {
        _date = comparisonDate;
    }
    if (!today && !tomorrow && !next && !_this)
    {
        if (dayFound || monthFound || yearFound || timeFound) {

            if (monthFound && !dayFound) {
                if (daysofmonth[_date.getMonth()] > daysofmonth[month]) {
                    _date.setDate(1);
                }
            }

            if (dayFound) {
                _date.setDate(day);
            }
            if (monthFound) {
                _date.setMonth(month);
            }
            if (yearFound) {
                _date.setFullYear(year);
            }
            return _date;
        }
    }
    else if (tomorrow)
    {
        _date.setDate(_date.getDate() + 1);
        return _date;
    }
    else if (next)
    {
        if (dayFound) {
            _date.setDate(day);
        }
        _date.setDate(_date.getDate() + 7);
        return _date;
    }
    else if (_this)
    {
        if (dayFound) {
            _date.setDate(day);
        }
        return _date;
    }
    else
    {
        return _date;
    }

    return null;
}

function _clear() {
    month = year = day = time = -1;
    monthFound = yearFound = dayFound = today = tomorrow = timeFound = next = _this = false;
    comparisonDate = "";
}

function parse(controlName, tempOutputDisplayTextField, nextControlName, dateToCompareComponent, e) {
    _clear();
    var rsvpComparisonDate = null;
    var localComparisonDate = null;
    var controlValue = document.getElementById(controlName).value;
    if (controlValue != null && controlValue.length > 30) {
        textCounter(document.getElementById(controlName), 30);
    }
    if (dateToCompareComponent != null && dateToCompareComponent.length > 0) {
        if (document.getElementById(dateToCompareComponent)) {
            var dateToCompareComponentValue = document.getElementById(dateToCompareComponent).value;
            if (dateToCompareComponentValue != null && dateToCompareComponentValue.length > 0) {
                comparisonDate = new Date(dateToCompareComponentValue);
                localComparisonDate = new Date(dateToCompareComponentValue);
            } else {
                comparisonDate = new Date();
                localComparisonDate = new Date();
            }
        } else {
            dateToCompareComponent = dateToCompareComponent.substring(dateToCompareComponent.indexOf(" ") + 1);
            var yearString = dateToCompareComponent.substring(dateToCompareComponent.lastIndexOf(" ") + 1);
            var monthString = dateToCompareComponent.substring(0, dateToCompareComponent.indexOf(" "));
            dateToCompareComponent = dateToCompareComponent.substring(dateToCompareComponent.indexOf(" ") + 1);
            var dateString = dateToCompareComponent.substring(0, dateToCompareComponent.indexOf(" "));
            dateToCompareComponent = monthString + " " + dateString + ", " + yearString;
            if (dateToCompareComponent != null && dateToCompareComponent.length > 0) {
                rsvpComparisonDate = new Date(dateToCompareComponent);
            }            
        }
    }
    /*June 11, 2008 08:00 AM*/
    var formName = "";
    var actualControlName = controlName;
    var showBothDateAndTime = false;
    if (controlName.indexOf(":") > -1) {
        formName = controlName.substr(0, controlName.lastIndexOf(":") + 1);
        controlName = controlName.substr(controlName.lastIndexOf(":") + 1);
    }
    if (controlName.toLowerCase().indexOf("rsvp") > -1) {
        showBothDateAndTime = false;
    }
    var code;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    var character = String.fromCharCode(code);
    /*if ((code >= 65 && code <= 90) || (code >= 97 && code <= 122) || (code >= 48 && code <= 57) || code == 46 || code == 47 || code == 58) {
        controlValue = controlValue + character;
    }*/

    /*if (code == 8) {
        controlValue = controlValue.substr(0, controlValue.length - 1);
    }*/
    
    var originalcontrolValue = controlValue;
    if ((code != 13 || code != 9 || code != 40) && !(controlValue != null && controlValue.indexOf(",") > -1 && controlValue.indexOf(":") > -1 && (controlValue.indexOf("AM") > -1 || controlValue.indexOf("PM") > -1))) {
        controlValue = getRemovedPunctuationString(controlValue);
        var spacedString = controlValue.split(" ");

        if (spacedString.length > 1) {
            searchDate(spacedString);
        }
        else {
            search(controlValue);
        }

        var date = getDate();

        if (controlName.toLowerCase().indexOf("rsvp") > -1) {
            if (rsvpComparisonDate != null && rsvpComparisonDate.toString().length > 0) {
                if (date > rsvpComparisonDate) {
                    date = rsvpComparisonDate;
                }
                var todaysDate = new Date();
                if (date < todaysDate) {
                    date = todaysDate;
                }
            }
        }
        var tempDate = "";
        if (localComparisonDate != null && localComparisonDate.toString().length > 0) {
            if (date != null && date < localComparisonDate) {

                if (date != null) {
                    tempDate = date;
                    tempDate.setFullYear(date.getFullYear() + 1);
                    tempDate.setDate(1);
                    tempDate.setMonth(date.getMonth());
                }
                if (tempDate != null && tempDate != "") {
                    date = tempDate;
                } else {
                    date = localComparisonDate;
                }

                var temporary = new Date(originalcontrolValue);

                if (temporary != null && temporary.getFullYear() > 1000 && temporary < localComparisonDate) {
                    document.getElementById("invalidDateMesaageFieldSet").style.display = "block";
                }
                else {
                    document.getElementById("invalidDateMesaageFieldSet").style.display = "none";
                }
            }
            /*else if (date != null && date.getMonth() > localComparisonDate.getMonth()) {
                tempDate = "";
                if (date != null) {
                    tempDate = date;
                    if (localComparisonDate.getDate() > 28 && date.getDate() < 3) {
                        if (!next && !tomorrow) {
                            tempDate.setMonth(tempDate.getMonth() - 1);
                            tempDate.setDate(1);
                        }
                    } else {
                        tempDate.setDate(1);
                    }
                }

                if (tempDate != null && tempDate != "") {
                    date = tempDate;
                } else {
                    date = localComparisonDate;
                }
            }*/
        }
        var inputComponent = formName + tempOutputDisplayTextField;
        if (code == 13 || code == 9 || code == 40) {
            inputComponent = actualControlName;
        }

        var formatString = "F d, Y h:i A";
        var formatStringWithoutTime = "F d, Y";
        if (date != null) {
            var finalDateString = "";
            if (timeFound) {
                if (showBothDateAndTime) {
                    finalDateString = date.formatDate(formatStringWithoutTime) + " " + time;
                }
                else {
                    finalDateString = date.formatDate(formatStringWithoutTime);
                }
            }
            else {
                date.setHours(8);
                date.setMinutes(0);
                if (showBothDateAndTime) {
                    finalDateString = date.formatDate(formatString);
                }
                else {
                    finalDateString = date.formatDate(formatStringWithoutTime);
                }
            }
            if (finalDateString.indexOf("1996") > -1) {
                finalDateString = finalDateString.substring(0, finalDateString.indexOf(" 1996"));
                var tempfinalDateString = finalDateString.substring(finalDateString.lastIndexOf(" "));
                finalDateString = finalDateString.substring(0, finalDateString.lastIndexOf(" "));
                finalDateString = finalDateString + "," + tempfinalDateString + " 08:00 AM";
            }
            document.getElementById(inputComponent).value = finalDateString;
        }
        else {
            document.getElementById(formName + tempOutputDisplayTextField).value = controlValue;
        }
    }

    if (controlName.toLowerCase().indexOf("rsvp") == -1 && dateToCompareComponent == "") {
        if (document.getElementById(formName + "endDateInputDate") != null) {
            var endDateStringValue = document.getElementById(formName + "endDateInputDate").value;
            var startDateStringValue = document.getElementById(formName + "startDateInputDate").value;
            if (endDateStringValue != null && endDateStringValue.length > 0 && startDateStringValue != null && startDateStringValue.length > 0) {
                var endDateValue = new Date(endDateStringValue);
                var startDateValue = new Date(startDateStringValue);
                if (endDateValue && startDateValue && endDateValue < startDateValue) {
                    var finalEndDateValueString = date.formatDate("F d, Y");
                    document.getElementById(formName + "endDateInputDate").value = finalEndDateValueString;
                }
            }
        }
    }

    if (code == 9 && nextControlName != null) {
        document.getElementById(formName + nextControlName).focus();
    }
    
    return (code != 13 && code != 9 && code != 40);
}

function parseTime(timeControl, suggestorControl, e) {
    _clear();
    var timeValue = timeControl.value;
    var code;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    var character = String.fromCharCode(code);
    if ((code >= 65 && code <= 90) || (code >= 97 && code <= 122) || (code >= 48 && code <= 57) || code == 46 || code == 47 || code == 58) {
        timeValue = timeValue + character;
    }

    if (code == 8) {
        timeValue = timeValue.substr(0, timeValue.length - 1);
    }
    var inputComponent;
    if (code == 13 || code == 9 || code == 40) {
        inputComponent = timeControl;
    } else {
        inputComponent = document.getElementById(suggestorControl);
    }

    time = searchTime(removeSuffix(timeValue));
    timeFound = (time != -1);
    if (timeFound) {
        inputComponent.value = time;
    }
    
    return (code != 13 && code != 9 && code != 40);
}


function getStringTime(value, format) {
    // Get the date format.
    // If a format was passed, use it, otherwise the default format.
    var fmt = format ? format : sTimeFormat;
    var re = /^(h|hh)(:mm)(:ss)?( am\/pm| AM\/PM| a\/p| A\/P)?$/;
                                                // Use the default format if the provided format is not valid.
    if (!re.test(fmt))
        fmt = "h:mm AM/PM";


    var hasMeridian = false;
    var meridian = "am,pm".split(",");
    if (/\b((am\/pm|AM\/PM|a\/p|A\/P))\b/.test(fmt)) {
        var meridianLength = 0;
        hasMeridian = true;
        if (/\bam\/pm\b/.test(fmt)) {
            meridian = "am,pm".split(",");
            meridianLength = 6;
        } else if (/\bAM\/PM\b/.test(fmt)) {
            meridian = "AM,PM".split(",");
            meridianLength = 6;
        } else if (/a\/p/.test(fmt)) {
            meridian = "a,p".split(",");
            meridianLength = 4;
        } else if (/\bA\/P\b/.test(fmt)) {
            meridian = "A,P".split(",");
            meridianLength = 4;
        }

        fmt = fmt.substring(0, fmt.length - meridianLength)
    }

                                                // Initialize time variables.
    var h = "08";
    var m = "00";
    var s = "00";
    var sMeridian = "AM";
    var valueHasMeridian = /[aApP][mM]?$/.test(value);
    var sTemp = "";
    var values = value.split(":");
    var fmtItems = fmt.split(":");

                                                // Go through each item in the value field.
    for (var i = 0; i < values.length; i++) {
        if (i >= fmtItems.length)
            break;

        switch (fmtItems[i]) {
            case "h":
            case "hh":
                var iTemp = parseInt(values[i], 10);
                if (iTemp >= 24) {
                    h = 8;
                    sMeridian = meridian[0];
                }
                else if (iTemp >= 0 && iTemp < 24) {
                    //If the string contains no PM or AM
                    if (hasMeridian) {
                        if (iTemp >= 6 && iTemp < 12) {            // am
                            sMeridian = meridian[0];
                        } else if (iTemp >= 12) {        // pm
                            if (iTemp > 12) {            // pm
                                iTemp -= 12;
                            }
                            sMeridian = meridian[1];
                        }
                        else if (iTemp >= 0 && iTemp < 6) {        // pm
                            if (iTemp == 0) {            // am
                                iTemp = 12;
                                sMeridian = meridian[0];
                            } else {
                                sMeridian = meridian[1];
                            }

                        }
                    }

                //If the string contains PM or AM
                    if (valueHasMeridian) {
                        if (/[pP][mM]?$/.test(value)) {
                            sMeridian = meridian[1];
                        } else if (/[aA][mM]?$/.test(value)) {
                            sMeridian = meridian[0];
                        }
                    }

            // Format the hours.
                    if (fmtItems[i] == "h") {
                        h = iTemp;
                    } else {
                        sTemp = "0" + iTemp;
                        h = sTemp.substring(sTemp.length - 2, sTemp.length);
                    }
                }
                break;

            case "mm":
            // Format minutes.
                sTemp = "0" + parseInt(values[i], 10);
                if (sTemp > 59) {
                    m = "00";
                }
                else if (sTemp >= 0 && sTemp <= 59) {
                    m = sTemp.substring(sTemp.length - 2, sTemp.length);
                }
                break;

            case "ss":
            // Format seconds.
                sTemp = "0" + parseInt(values[i], 10);
                s = sTemp.substring(sTemp.length - 2, sTemp.length);
                break;
        }
    }
    if (value != null && value.length > 0)
    {

        if (m == "NaN" || m == "aN")
        {
            m = "00";
        }

        if (h == "NaN" || h == "aN")
        {
            h = "00";
        }
        var hString = "" + h;
        if (h < 10) {
            hString = "0" + h;
        }

        if (fmtItems.length == 2)
            return hString + ":" + m + (hasMeridian ? " " + sMeridian : "");

        return h + ":" + m + ":" + s + (hasMeridian ? " " + sMeridian : "");
    }

    return -1;
}