﻿  /////////////////////////////////////////////////////////////////////////
 //////////Copyright © William J. Familia (wjfamilia@gmail.com)///////////
/////////////////////////////////////////////////////////////////////////
function formatPhoneNumber(phoneNumberField, allowExtension, useTollFreeFormatting, countryID, e) 
{
    //8  = Backspace
    //37 = Left Arrow
    //39 = Right Arrow
    
    if(getKeycode(e)!= 8 && countryID == "184")
    {
        var unformattedPhoneNumber = makeNumeric(phoneNumberField.value, false);
        var isValidTollFreePrefix = false;
        var formattedPhoneNumber   = "";
        
        //valid current & future toll free number prefixes : http://en.wikipedia.org/wiki/Toll_free_number
        var validTollFreePrefixies = new Array("800","888","877","866","855","844","833","822");
        var validTollFreePrefixiesFirst2Only = new Array("80","88","87","86","85","84","83","82");
        var first3Digits = unformattedPhoneNumber.charAt(0) + unformattedPhoneNumber.charAt(1) + unformattedPhoneNumber.charAt(2);
        var first4Digits = first3Digits + unformattedPhoneNumber.charAt(3);
        
        for(var k = 0; k < validTollFreePrefixies.length; k++)
        {
            if(validTollFreePrefixies[k] == first3Digits)
            {
                isValidTollFreePrefix = true;
                unformattedPhoneNumber = "1" + unformattedPhoneNumber;
            }
            else if("1" + validTollFreePrefixies[k] == first4Digits)
            {
                isValidTollFreePrefix = true;
            }
            else if("1" + validTollFreePrefixiesFirst2Only[k] == first3Digits && unformattedPhoneNumber.length == 3)
            {
                isValidTollFreePrefix = true;
            }
       }
       
       if(!useTollFreeFormatting)
       {
            isValidTollFreePrefix = false;
       }

       if(isValidTollFreePrefix)
       {
            //1-###-###-#### ext. ####
            formattedPhoneNumber += "1" + "-" + unformattedPhoneNumber.charAt(1) + unformattedPhoneNumber.charAt(2) + unformattedPhoneNumber.charAt(3);
            if(unformattedPhoneNumber.length > 3)
            {
                formattedPhoneNumber += "-"; 
                formattedPhoneNumber += unformattedPhoneNumber.charAt(4) + unformattedPhoneNumber.charAt(5) + unformattedPhoneNumber.charAt(6);
                if(unformattedPhoneNumber.length > 6)
                {
                    formattedPhoneNumber += "-";
                    formattedPhoneNumber += unformattedPhoneNumber.charAt(7) + unformattedPhoneNumber.charAt(8) + unformattedPhoneNumber.charAt(9) + unformattedPhoneNumber.charAt(10);
                    if(allowExtension)
                    {
                        if(unformattedPhoneNumber.length > 11)                                                                                                                                              
                        {                                                                                                                                                                                        
                            formattedPhoneNumber += " ext. ";
                            for(var i = 11; i < unformattedPhoneNumber.length; i++)                                                                                                                             
                            {                                                                                                                                                                                       
                                formattedPhoneNumber += unformattedPhoneNumber.charAt(i);                                                                                                                           
                            }
                        }
                    }
                }
            }
        }
        else
        {
            //If less then 4 in length, this still could be a valid toll-free number
            if(unformattedPhoneNumber.length > 2)
            {
                if(unformattedPhoneNumber.charAt(0) == "1")
                {
                    var tempUnformattedPhoneNumber = "";
                    for(var j = 1; j < unformattedPhoneNumber.length; j++)
                    {
                        tempUnformattedPhoneNumber += unformattedPhoneNumber.charAt(j);
                    }
                    unformattedPhoneNumber = tempUnformattedPhoneNumber;
                }
                //(###) ###-#### ext. ####
                if(unformattedPhoneNumber.length > 2 || phoneNumberField.value.charAt(0) == "(")
                {
                   formattedPhoneNumber += "("; 
                }
                formattedPhoneNumber += unformattedPhoneNumber.charAt(0) + unformattedPhoneNumber.charAt(1) + unformattedPhoneNumber.charAt(2);
                if(unformattedPhoneNumber.length > 2)
                {
                    formattedPhoneNumber += ") ";
                    formattedPhoneNumber += unformattedPhoneNumber.charAt(3) + unformattedPhoneNumber.charAt(4) + unformattedPhoneNumber.charAt(5);
                    if(unformattedPhoneNumber.length > 5)
                    {
                        formattedPhoneNumber += "-";
                        formattedPhoneNumber += unformattedPhoneNumber.charAt(6) + unformattedPhoneNumber.charAt(7) + unformattedPhoneNumber.charAt(8) + unformattedPhoneNumber.charAt(9);                                                                                                                                                                                   
                        if(allowExtension)
                        {
                            if(unformattedPhoneNumber.length > 10)
                            {
                                formattedPhoneNumber += " ext. ";
                                for(var i = 10; i < unformattedPhoneNumber.length; i++)
                                {
                                    formattedPhoneNumber += unformattedPhoneNumber.charAt(i);
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                if(phoneNumberField.value == "1" && useTollFreeFormatting)
                {
                    formattedPhoneNumber = "1-";
                }
                else if(phoneNumberField.value == "1--" && useTollFreeFormatting)
                {
                    formattedPhoneNumber = "1-";
                }
                else if(phoneNumberField.value == "1-8" && useTollFreeFormatting)
                {
                    formattedPhoneNumber = "1-8";
                }
                else if(phoneNumberField.value.charAt(0) == "(")
                {
                    formattedPhoneNumber = "(" + unformattedPhoneNumber;
                }
                else
                {
                    formattedPhoneNumber = unformattedPhoneNumber;
                }
            }
        }       
                
       return formattedPhoneNumber;    
    }
    else
    {
        return phoneNumberField.value;
    }
}
    
    
function formatSocialSecurityNumber(ssnField, e) 
{
    //8  = Backspace
    //37 = Left Arrow
    //39 = Right Arrow
    
    if(getKeycode(e)!= 8)
    {
        var unformattedSsn = makeNumeric(ssnField.value, false);
        var formattedSsn   = "";
        
        formattedSsn += unformattedSsn.charAt(0) + unformattedSsn.charAt(1) + unformattedSsn.charAt(2);
        if(unformattedSsn.length >= 3)
        {
            formattedSsn += "-";
            if(unformattedSsn.length > 3)
            {
                formattedSsn += unformattedSsn.charAt(3) + unformattedSsn.charAt(4);
                if(unformattedSsn.length > 4)
                {
                    formattedSsn += "-" + unformattedSsn.charAt(5) + unformattedSsn.charAt(6) + unformattedSsn.charAt(7) + unformattedSsn.charAt(8);
                } 
            }
        }
                    
        return formattedSsn;  
    }
    else
    {
        return ssnField.value;
    }                                    
}


function formatPostalCode(postalCodeField, allowExtended ,countryID, e) 
{
    //8  = Backspace
    //9  = Tab (so will stay highlighted)
    //16 = Reverse Tab - Shift+Tab (so will stay highlighted)
    //37 = Left Arrow
    //39 = Right Arrow
    
    if(getKeycode(e)!= 8)
    {
        if(countryID != null && countryID == "31") //Canada
        {
            //http://www.infinitegravity.ca/postalcodeformat.htm (ANA NAN)
            var unformattedPostalCode = postalCodeField.value.replace(" ", "");
            var formattedPostalCode   = "";
            
            if(unformattedPostalCode.length > 0)
            {
                var validFirstCharacterOfFSA = new Array("A","B","C","E","G","H","J","K","L","M","N","P","R","S","T","V","X","Y");
                
                for(var i = 0; i < validFirstCharacterOfFSA.length; i++)
                {
                    if(validFirstCharacterOfFSA[i] == unformattedPostalCode.charAt(0).toUpperCase())
                    {
                        formattedPostalCode += validFirstCharacterOfFSA[i];
                    }
                }
                
                if(formattedPostalCode.length == 1 && unformattedPostalCode.length > 1)
                {
                    if(!isNaN(unformattedPostalCode.charAt(1)))
                    {
                        formattedPostalCode += unformattedPostalCode.charAt(1);
                    }
                }
                
                if(formattedPostalCode.length == 2 && unformattedPostalCode.length > 2)
                {
                    //http://en.wikipedia.org/wiki/Canadian_postal_code
                    //No postal code includes the letters D, F, I, O, Q, or U, as the OCR equipment used in automated sorting could easily confuse them with other letters and digits, especially when they are rendered as cursive handwriting. The letters W and Z are used, but are not currently used as the first letter.
                    
                    //Added W and Z to First list
                    var validThirdCharacterOfFSA = new Array("A","B","C","E","G","H","J","K","L","M","N","P","R","S","T","V","W","X","Y","Z");
                
                    for(var j = 0; j < validThirdCharacterOfFSA.length; j++)
                    {
                        if(validThirdCharacterOfFSA[j] == unformattedPostalCode.charAt(2).toUpperCase())
                        {
                            formattedPostalCode += validThirdCharacterOfFSA[j];
                        }
                    }
                }
                
                if(formattedPostalCode.length == 3)
                {
                    formattedPostalCode += " ";
                }
                
                if(formattedPostalCode.length == 4 && unformattedPostalCode.length > 3)
				{
					if(!isNaN(unformattedPostalCode.charAt(3)))
					{
						formattedPostalCode += unformattedPostalCode.charAt(3);
					}
				}
				
				if(formattedPostalCode.length == 5 && unformattedPostalCode.length > 4)
                {
                    //http://en.wikipedia.org/wiki/Canadian_postal_code
                    //No postal code includes the letters D, F, I, O, Q, or U, as the OCR equipment used in automated sorting could easily confuse them with other letters and digits, especially when they are rendered as cursive handwriting. The letters W and Z are used, but are not currently used as the first letter.
                    
                    //Added W and Z to First list
                    var validSecondCharacterOfLDU = new Array("A","B","C","E","G","H","J","K","L","M","N","P","R","S","T","V","W","X","Y","Z");
                
                    for(var j = 0; j < validSecondCharacterOfLDU.length; j++)
                    {
                        if(validSecondCharacterOfLDU[j] == unformattedPostalCode.charAt(4).toUpperCase())
                        {
                            formattedPostalCode += validSecondCharacterOfLDU[j];
                        }
                    }
                }
                
                if(formattedPostalCode.length == 6 && unformattedPostalCode.length > 5)
				{
					if(!isNaN(unformattedPostalCode.charAt(5)))
					{
						formattedPostalCode += unformattedPostalCode.charAt(5);
					}
				}
            }
                        
            return formattedPostalCode; 
        }
        else //USA countryID == "184"
        {
            var unformattedPostalCode = makeNumeric(postalCodeField.value, false);
            var formattedPostalCode   = "";
            
            formattedPostalCode += unformattedPostalCode.charAt(0) + unformattedPostalCode.charAt(1) + unformattedPostalCode.charAt(2) + unformattedPostalCode.charAt(3) + unformattedPostalCode.charAt(4);
            if(unformattedPostalCode.length > 5 && allowExtended)
            {
                formattedPostalCode += "-" + unformattedPostalCode.charAt(5) + unformattedPostalCode.charAt(6) + unformattedPostalCode.charAt(7) + unformattedPostalCode.charAt(8);
            }
                        
            return formattedPostalCode; 
        }
    }
    else
    {
        return postalCodeField.value;
    }                                      
}


function formatNumberWithCheckDigit(formField, e) 
{
    //8  = Backspace
    //37 = Left Arrow
    //39 = Right Arrow
    
    if(getKeycode(e)!= 8)
    {
        var unformattedNumberWithCheckDigit = makeNumeric(formField.value, false);
        var formattedNumberWithCheckDigit   = "";
        var originalValueLength        = formField.value.length;
        var dashIndex                  = formField.value.indexOf("-");
        
        if(dashIndex == 0)
        {
            formattedNumberWithCheckDigit = unformattedNumberWithCheckDigit;
        }
        else if(dashIndex > 0)
        {
            formattedNumberWithCheckDigit = unformattedNumberWithCheckDigit.substr(0, dashIndex) + "-" + unformattedNumberWithCheckDigit.substr(dashIndex, 1);
        }
        else
        {
            formattedNumberWithCheckDigit = unformattedNumberWithCheckDigit;
        }
        
        return formattedNumberWithCheckDigit;
    }
    else
    {
        return formField.value;
    }
}


function makeNumeric(value, setEmptyToZero) 
{
    var numericValue = "";
    for(var i = 0; i <= value.length; i++) 
    {
        if(value.charAt(i) == "1" || value.charAt(i) == "2" || value.charAt(i) == "3" || value.charAt(i) == "4" || value.charAt(i) == "5" || value.charAt(i) == "6" || value.charAt(i) == "7" || value.charAt(i) == "8" || value.charAt(i) == "9" || value.charAt(i) == "0")
        {
            numericValue += value.charAt(i);
        }
    }
    
    if(setEmptyToZero)
    {
        if(numericValue == "")
        {
            numericValue = 0;
        }
    }
    
    return numericValue;
}

function getKeycode(e)
{
    var blnDOM = false, blnIE4 = false, blnNN4 = false; 
    
    if(document.layers) blnNN4 = true;
    else if(document.all) blnIE4 = true;
    else if(document.getElementById) blnDOM = true;

    if(blnNN4)
    {
        var NN4key = e.which
        return NN4key;
    }
    if(blnDOM)
    {
        var blnkey = e.which
        return blnkey;
    }
    if(blnIE4)
    {
        var IE4key = event.keyCode
        return IE4key;
    }
}

function formatCurrency(formField, setEmptyToZero, e) 
{
    //8  = Backspace
    //37 = Left Arrow
    //39 = Right Arrow
    
    if(getKeycode(e)!= 8)
    {
        var unformattedCurrency = makeNumeric(formField.value, setEmptyToZero);
        var formattedCurrency   = "";
        var originalValueLength = formField.value.length;
        var decimalIndex        = formField.value.indexOf(".");
        
        if(decimalIndex == 0)
        {
            formattedCurrency = unformattedCurrency;
        }
        else if(decimalIndex > 0)
        {
            formattedCurrency = unformattedCurrency.substr(0, decimalIndex) + "." + unformattedCurrency.substr(decimalIndex, 2);
        }
        else
        {
            formattedCurrency = unformattedCurrency;
        }
        
        return formattedCurrency;
    }
    else
    {
        return formField.value;
    }
}

function formatDateTime(formField, mode, minDateTime, maxDateTime, e) 
{
    //8  = Backspace
    //9  = Tab (so will stay highlighted)
    //16 = Reverse Tab - Shift+Tab (so will stay highlighted)
    //37 = Left Arrow
    //39 = Right Arrow
    
    if(getKeycode(e)!= 8)
    {
        var unformattedDateTime = makeNumeric(formField.value, false);
        var formattedDateTime = "";
		
		//Ensure correct MM/DD/YYYY combinations		
        if (unformattedDateTime.length >= 1)
        {
			if(unformattedDateTime.charAt(0) > 1)
			{
				unformattedDateTime = "0" + unformattedDateTime;
			}
			
			if(formField.value.length >= 2)
			{
                if(formField.value.substr(0,2) == "1/")
                {
                    unformattedDateTime = "0" + unformattedDateTime;
                }
            }
        }
        
        if (unformattedDateTime.length >= 2)
        {            
			tempUnformattedDateTime = unformattedDateTime;
			if(unformattedDateTime.charAt(0) == 1 && unformattedDateTime.charAt(1) > 2)
			{
				unformattedDateTime = "12";
				if(tempUnformattedDateTime.length > 2)
				{
					unformattedDateTime += tempUnformattedDateTime.substr(2);
				}
			}
			
			tempUnformattedDateTime = unformattedDateTime;
            if(unformattedDateTime.charAt(0) == 0 && unformattedDateTime.charAt(1) == 0)
		    {
			    unformattedDateTime = "01";
			    
			    if(tempUnformattedDateTime.length > 2)
                {
                    unformattedDateTime += tempUnformattedDateTime.substr(2);
                }
		    }
        }
        
        if (unformattedDateTime.length >= 3)
        {
			if((unformattedDateTime.charAt(0) == 0 && unformattedDateTime.charAt(1) == 2 && unformattedDateTime.charAt(2) > 2) || (unformattedDateTime.charAt(2) > 3))
			{
				unformattedDateTime = unformattedDateTime.charAt(0) + unformattedDateTime.charAt(1) + "0" + unformattedDateTime.substr(2);
			}
			
			if(formField.value.length >= 5)
			{
                if(formField.value.substr(3,2) == "1/" || formField.value.substr(3,2) == "2/" || formField.value.substr(3,2) == "3/")
                {
                    unformattedDateTime = unformattedDateTime.charAt(0) + unformattedDateTime.charAt(1) + "0" + unformattedDateTime.substr(2);
                }
            }
        }
        
        if (unformattedDateTime.length >= 4)
        {
			tempUnformattedDateTime = unformattedDateTime;
			month = unformattedDateTime.charAt(0) + unformattedDateTime.charAt(1);
			day = unformattedDateTime.charAt(2) + unformattedDateTime.charAt(3);
			
			if(unformattedDateTime.charAt(2) == 3 && unformattedDateTime.charAt(3) > 1)
			{
				unformattedDateTime = month + "31";
				if(tempUnformattedDateTime.length > 4)
				{
					unformattedDateTime += tempUnformattedDateTime.substr(4);
				}
			}
			
			
			tempUnformattedDateTime = unformattedDateTime;
			month = unformattedDateTime.charAt(0) + unformattedDateTime.charAt(1);
			day = unformattedDateTime.charAt(2) + unformattedDateTime.charAt(3);
			
			if((month == "04" || month == "06" || month == "09" || month == "11") && day == "31")
			{
				unformattedDateTime = month + "30";
				if(tempUnformattedDateTime.length > 4)
				{
					unformattedDateTime += tempUnformattedDateTime.substr(4);
				}
			}
		
			tempUnformattedDateTime = unformattedDateTime;
            if(unformattedDateTime.charAt(2) == 0 && unformattedDateTime.charAt(3) == 0)
		    {
			    unformattedDateTime = unformattedDateTime.substr(0,2) + "01";
			    
			    if(tempUnformattedDateTime.length > 4)
                {
                    unformattedDateTime += tempUnformattedDateTime.substr(4);
                }
		    }
        }
        
        month = unformattedDateTime.charAt(0) + unformattedDateTime.charAt(1);
		day = unformattedDateTime.charAt(2) + unformattedDateTime.charAt(3);
        if (unformattedDateTime.length >= 8 && month == "02" && day == "29")
        {
			//check if leap year
			if(!isLeapYear(unformattedDateTime.substr(4,4)))
			{
				unformattedDateTime = month + "28" + unformattedDateTime.substr(4);
			}
        }
        
        if(mode == "DateAndTimeWithoutSeconds" || mode == "DateAndTimeWithSeconds")
        {
            if (unformattedDateTime.length > 8)
            {
                if(unformattedDateTime.charAt(8) > 1)
			    {
				    unformattedDateTime = unformattedDateTime.substr(0,8) + "0" + unformattedDateTime.substr(8);
			    }
    			
			    if(formField.value.length >= 12)
			    {
                    if(formField.value.substr(11,2) == "1:")
                    {
                        unformattedDateTime = unformattedDateTime.substr(0,8) + "0" + unformattedDateTime.substr(8);
                    }
                }
                
                if(unformattedDateTime.length == 9 && unformattedDateTime.charAt(8) == 0)
                {
                    //do nothing
                }
                else
                {
                    tempUnformattedDateTime = unformattedDateTime;
                    if(unformattedDateTime.charAt(8) == 0 && unformattedDateTime.charAt(9) == 0)
			        {
				        unformattedDateTime = unformattedDateTime.substr(0,8) + "01";
    				    
				        if(tempUnformattedDateTime.length > 10)
                        {
                            unformattedDateTime += tempUnformattedDateTime.substr(10);
                        }
			        }
                }
                
                if(unformattedDateTime.length >= 10)
                {
                    tempUnformattedDateTime = unformattedDateTime;
			        if(unformattedDateTime.charAt(8) == 1 && unformattedDateTime.charAt(9) > 2)
			        {
				        unformattedDateTime = unformattedDateTime.substr(0,8) + "12";
				        if(tempUnformattedDateTime.length > 9)
				        {
					        unformattedDateTime += tempUnformattedDateTime.substr(10);
				        }
			        }
			    }
                
                if(unformattedDateTime.length >= 11)
                {
			        if(unformattedDateTime.charAt(10) > 5)
			        {
				        unformattedDateTime = unformattedDateTime.substr(0,10) + "0" + unformattedDateTime.substr(10);
			        }
			    }
            }
            
            if(mode == "DateAndTimeWithoutSeconds")
            {
                unformattedDateTime = unformattedDateTime.substr(0,12);
            }
        }
        
        if(mode == "DateAndTimeWithSeconds")
        {
            if(unformattedDateTime.length >= 13)
            {
                //alert("1");
		        if(unformattedDateTime.charAt(12) > 5)
		        {
		            //alert("2");
			        unformattedDateTime = unformattedDateTime.substr(0,12) + "0" + unformattedDateTime.substr(12);
		        }
		    }
		    
		    unformattedDateTime = unformattedDateTime.substr(0,14);
        }
        
        //Check if in valid date range
        var useMinMeridiemOnFormat = false;
        var useMaxMeridiemOnFormat = false;
        
        if(unformattedDateTime.length >= 8)
        {
            var userYear = unformattedDateTime.substr(4,4);
            var userMonth = unformattedDateTime.substr(0,2);
            var userDay = unformattedDateTime.substr(2,2);
            
            var minYear = minDateTime.substr(6,4);
            var minMonth = minDateTime.substr(0,2);
            var minDay = minDateTime.substr(3,2);
            var minHour = minDateTime.substr(11,2);
            var minMinute = minDateTime.substr(14,2);
            var minSecond = minDateTime.substr(17,2);
            var minMeridiem = minDateTime.substr(20,2);

            tempUnformattedDateTime = unformattedDateTime;
            if((userYear < minYear) || (userYear == minYear && userMonth < minMonth) || (userYear == minYear && userMonth == minMonth && userDay < minDay))
            {
                unformattedDateTime = minMonth + minDay + minYear;
                if(tempUnformattedDateTime.length > 8)
				{
					unformattedDateTime += tempUnformattedDateTime.substr(8);
				}
            }

            var maxYear = maxDateTime.substr(6,4);
            var maxMonth = maxDateTime.substr(0,2);
            var maxDay = maxDateTime.substr(3,2);
            var maxHour = maxDateTime.substr(11,2);
            var maxMinute = maxDateTime.substr(14,2);
            var maxSecond = maxDateTime.substr(17,2);
            var maxMeridiem = maxDateTime.substr(20,2);
            
            tempUnformattedDateTime = unformattedDateTime;
            if((userYear > maxYear) || (userYear == maxYear && userMonth > maxMonth) || (userYear == maxYear && userMonth == maxMonth && userDay > maxDay))
            {
                unformattedDateTime = maxMonth + maxDay + maxYear;
                if(tempUnformattedDateTime.length > 8)
				{
					unformattedDateTime += tempUnformattedDateTime.substr(8);
				}
            }
            
            if(mode == "DateAndTimeWithoutSeconds" && unformattedDateTime.length >= 12 && (formField.value.toUpperCase().indexOf("P") >= 0 || formField.value.toUpperCase().indexOf("A") >= 0))
            {
                var userHour = unformattedDateTime.substr(8,2);
                var userMinute = unformattedDateTime.substr(10,2);
                var userMeridiem = "PM";
                if(formField.value.toUpperCase().indexOf("A") >= 0)
                {
                    userMeridiem = "AM";
                }
                
                if((userYear == minYear && userMonth == minMonth && userDay == minDay && minMeridiem == "PM" && userMeridiem == "AM") || (userYear == minYear && userMonth == minMonth && userDay == minDay && minMeridiem == userMeridiem && userHour < minHour) || (userYear == minYear && userMonth == minMonth && userDay == minDay && minMeridiem == userMeridiem && userHour == minHour && userMinute < minMinute))
                {
                    unformattedDateTime = minMonth + minDay + minYear + minHour + minMinute;
                    useMinMeridiemOnFormat = true;
                }
                
                if((userYear == maxYear && userMonth == maxMonth && userDay == maxDay && maxMeridiem == "AM" && userMeridiem == "PM") || (userYear == maxYear && userMonth == maxMonth && userDay == maxDay && maxMeridiem == userMeridiem && userHour > maxHour) || (userYear == maxYear && userMonth == maxMonth && userDay == maxDay && maxMeridiem == userMeridiem && userHour == maxHour && userMinute > maxMinute))
                {
                    unformattedDateTime = maxMonth + maxDay + maxYear + maxHour + maxMinute;
                    useMaxMeridiemOnFormat = true;
                }
            }
            
            if(mode == "DateAndTimeWithSeconds" && unformattedDateTime.length >= 14 && (formField.value.toUpperCase().indexOf("P") >= 0 || formField.value.toUpperCase().indexOf("A") >= 0))
            {
                var userHour = unformattedDateTime.substr(8,2);
                var userMinute = unformattedDateTime.substr(10,2);
                var userSecond = unformattedDateTime.substr(12,2);
                var userMeridiem = "PM";
                if(formField.value.toUpperCase().indexOf("A") >= 0)
                {
                    userMeridiem = "AM";
                }
                
                if((userYear == minYear && userMonth == minMonth && userDay == minDay && minMeridiem == "PM" && userMeridiem == "AM") || (userYear == minYear && userMonth == minMonth && userDay == minDay && minMeridiem == userMeridiem && userHour < minHour) || (userYear == minYear && userMonth == minMonth && userDay == minDay && minMeridiem == userMeridiem && userHour == minHour && userMinute < minMinute) || (userYear == minYear && userMonth == minMonth && userDay == minDay && minMeridiem == userMeridiem && userHour == minHour && userMinute == minMinute && userSecond < minSecond))
                {
                    unformattedDateTime = minMonth + minDay + minYear + minHour + minMinute + minSecond;
                    useMinMeridiemOnFormat = true;
                }
                
                if((userYear == maxYear && userMonth == maxMonth && userDay == maxDay && maxMeridiem == "AM" && userMeridiem == "PM") || (userYear == maxYear && userMonth == maxMonth && userDay == maxDay && maxMeridiem == userMeridiem && userHour > maxHour) || (userYear == maxYear && userMonth == maxMonth && userDay == maxDay && maxMeridiem == userMeridiem && userHour == maxHour && userMinute > maxMinute) || (userYear == maxYear && userMonth == maxMonth && userDay == maxDay && maxMeridiem == userMeridiem && userHour == maxHour && userMinute == maxMinute && userSecond > maxSecond))
                {
                    unformattedDateTime = maxMonth + maxDay + maxYear + maxHour + maxMinute + maxSecond;
                    useMaxMeridiemOnFormat = true;
                }
            }
        }
       
        
        //Format with correct "/", " ", and ":" placement
        if(unformattedDateTime.length <= 1)
        {
			formattedDateTime = unformattedDateTime;
        }
        
        if (unformattedDateTime.length >= 2)
        {
			formattedDateTime = unformattedDateTime.charAt(0) + unformattedDateTime.charAt(1) + "/"
			
			if(unformattedDateTime.length == 3)
			{
				formattedDateTime += unformattedDateTime.charAt(2);
			}
        }
        
        if(unformattedDateTime.length >= 4)
        {
			formattedDateTime += unformattedDateTime.charAt(2) + unformattedDateTime.charAt(3) + "/"
			
			if(unformattedDateTime.length > 4)
			{
				formattedDateTime += unformattedDateTime.substr(4, 4);
			}
        }
        
        if(mode == "DateAndTimeWithoutSeconds" || mode == "DateAndTimeWithSeconds")
        {
            if (unformattedDateTime.length >= 8)
            {
                formattedDateTime += " ";
            }
            
            if (unformattedDateTime.length > 8)
            {
                formattedDateTime += unformattedDateTime.charAt(8);
            }
            
            if (unformattedDateTime.length > 9)
            {
                formattedDateTime += unformattedDateTime.charAt(9);
            }
            
            if (unformattedDateTime.length >= 10)
            {
                formattedDateTime += ":";
            }
            
            if (unformattedDateTime.length > 10)
            {
                formattedDateTime += unformattedDateTime.charAt(10);
            }
            
            if (unformattedDateTime.length > 11)
            {
                formattedDateTime += unformattedDateTime.charAt(11);
            }
        }
        
        if(mode == "DateAndTimeWithSeconds")
        {
            if (unformattedDateTime.length >= 13)
            {
                formattedDateTime += ":";
            }
            
            if (unformattedDateTime.length > 12)
            {
                formattedDateTime += unformattedDateTime.charAt(12);
            }
            
            if (unformattedDateTime.length > 13)
            {
                formattedDateTime += unformattedDateTime.charAt(13);
            }
        }
        
        if((mode == "DateAndTimeWithoutSeconds" && unformattedDateTime.length == 12) || (mode == "DateAndTimeWithSeconds" && unformattedDateTime.length == 14))
        {
            if (useMinMeridiemOnFormat)
            {
                formattedDateTime += " " + minDateTime.substr(20,2);;
            }
            else if (useMaxMeridiemOnFormat)
            {
                formattedDateTime += " " + maxDateTime.substr(20,2);;
            }
            else
            {
                if(formField.value.toUpperCase().indexOf("P") >= 0)
                {
                    formattedDateTime += " PM";
                }
                else if (formField.value.toUpperCase().indexOf("A") >= 0)
                {
                    formattedDateTime += " AM";
                }
                else
                {
                    formattedDateTime += " PM";
                }
            }
        }
        
        
        return formattedDateTime;
    }
    else
    {
        return formField.value;
    }
}

function isLeapYear(yyyy)
{
	yyyy = parseInt(yyyy);

	if(yyyy%4 == 0)
	{
		if(yyyy%100 != 0)
		{
			return true;
		}
		else
		{
			if(yyyy%400 == 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}
	
	return false;
}


function isEmpty(fieldValue) 
{
    if(trim(fieldValue) == "")
    {
        return true;
    }
    else
    {
        return false;
    }
}

function trim(fieldValue)
{
	return fieldValue.replace(/^\s+|\s+$/g,"");
}
