// used in currency masks

//Event.observe(window, 'load', function() 
//{   
//	Event.observe(document.forms[0], 'submit', Mask.RemoveSpecialChars);
//}); 

var Mask = {
    prefix: '$',
    RemoveSpecialChars: function()
    {
	    if (Page_IsValid)
	    {	
		    var obj = document.getElementsByTagName('span');
		    for (i = 0; i < obj.length; i++)
		    {
			    if (obj[i].getAttribute('maskType') == 'Currency')
			    {
				    // remove the prefix from currency masks
				    var textBox = $(obj[i].getAttribute('controltovalidate'));
				    var prefixIndex = textBox.value.indexOf(Mask.prefix);
				    if (prefixIndex == 0)
				    {
					    textBox.value = textBox.value.substring(1, textBox.value.length);
				    }
				    else
				    {
					    textBox.value = textBox.value.substring(0, prefixIndex) + textBox.value.substring(prefixIndex + 1, textBox.value.length);
				    }
				    // parse the entered value for commas and remove them
				    var tempNum = '';
				    var startIndex = 0;
				    var endIndex = 0;
				    for (j = 0; j < textBox.value.length; j++)
				    {
					    if (textBox.value.charAt(j) == ',')
					    {
						    endIndex = j;
						    tempNum += textBox.value.substring(startIndex, endIndex);
						    startIndex = endIndex + 1;
					    }
    					
				    }
				    // add on the segment of the entered value to the right of the last comma
				    if (endIndex > 0 && endIndex < textBox.value.length - 1)
				    {
					    tempNum += textBox.value.substring(endIndex + 1, textBox.value.length);
				    }
				    // if there were commas then replace the entered value
				    if (tempNum.length > 0)
				    {
					    textBox.value = tempNum;
				    }
			    } // end if
		    } // end for
	    } // end if
    },

    TextMaskValidatorOnkeydown: function(event, textBox)
    {
	    var maskType = textBox.getAttribute('validationtype');

	    var systemKeysAry =
	    {
		    backspace:	Event.KEY_BACKSPACE,
		    enter:	    13,
		    tab:		Event.KEY_TAB,
		    leftKey:	Event.KEY_LEFT,
		    rightKey:	Event.KEY_RIGHT,
		    'delete':	Event.KEY_DELETE,
		    end:		35,
		    home:		36
	    }
    	
	    var keysToAllowAry = 
	    { 
		    num1:	49,
		    num2:	50,
		    num3:	51,
		    num4:	52,
		    num5:	53,
		    num6:	54,
		    num7:	55,
		    num8:	56,
		    num9:	57,
		    num0:	48,
		    numPad1: 97,
		    numPad2: 98,
		    numPad3: 99,
		    numPad4: 100,
		    numPad5: 101,
		    numPad6: 102,
		    numPad7: 103,
		    numPad8: 104,
		    numPad9: 105,
		    numPad0: 96
	    }
    	
	    switch (maskType)
	    {
		    case 'date':
			    keysToAllowAry.forwardSlash = 191;
			    keysToAllowAry.divide = 111;
			    keysToAllowAry.letter_t = 84;
			    break;

		    case 'currency':
			    keysToAllowAry.period = 190;
			    keysToAllowAry.dec = 110;
			    if (textBox.getAttribute('maskAcceptNegative') == 'true')
			    {
				    keysToAllowAry.minus = 109;
				    keysToAllowAry.dash = 189;
			    }
			    break;

		    default:
			    break;
	    }

	    for (allowKey in keysToAllowAry)
	    {
		    if (event.keyCode == keysToAllowAry[allowKey] && !event.shiftKey)
		    {
			    textBox.setAttribute('isChanged', 'true');
			    // adjust the keycode if the numpad is being used
			    if (event.keyCode >= 96 && event.keyCode <= 105) 
			    {
				    event.keyCode -= 48;
			    }
    		
			    var selectedText = Mask.getSelectedText();
			    if (selectedText != '')
			    {
				    var selectedTextIndex = textBox.value.indexOf(selectedText);
				    if (selectedTextIndex > -1)
				    {
					    textBox.value = textBox.value.substring(0, selectedTextIndex) + textBox.value.substring(selectedTextIndex + selectedText.length, textBox.value.length);
					    Mask.SetCaretPosition(textBox, selectedTextIndex);
				    }
			    }
    			
			    var caretPosition = Mask.GetCaretPosition(textBox);
    						
			    // special cases to not allow certain 'allowed' characters
			    switch (maskType)
			    {
				    case 'numeric':
					    var length = parseInt(textBox.getAttribute('maskLength'));
					    if (length > -1)
					    {
						    if (textBox.value.length >= length)
						    {
							    Event.stop(event);
						    }
					    }
					    break;
    					
				    case 'date':
				        
					    // don't allow any more characters if the value is 'today'
					    if (textBox.value == 'today')
					    {
						    Event.stop(event);
					    }
					    // only allow the first digit to be a 't'
					    if (event.keyCode == keysToAllowAry.letter_t)
					    {
						    Event.stop(event);
						    if (textBox.value.length == 0)
						    {		
							    textBox.value = 'today';
							    textBox.select();
						    }
					    }
					    // don't allow double forward slashes or more than 2 slashes total or first character as slash
					    else if (event.keyCode == keysToAllowAry.forwardSlash || event.keyCode == keysToAllowAry.divide && textBox.value.length > 0)
					    {	
						    if (caretPosition == 0 || textBox.value.length == 0 || textBox.value.charAt(caretPosition - 1) == '/')
						    {
							    Event.stop(event);
						    }
						    else
						    {
							    var slashCount = Mask.CountSlashes(textBox);
							    if (slashCount == 2)
							    {
								    Event.stop(event);
							    }
						    }
					    }
					    // if entering a digit, limit the number and type of said digits between slashes
					    else
					    {								
						    Mask.MaskDateNumericKeyDown(textBox);
					    }
					    break;

				    case 'currency':
					    // only allow the first digit to be a minus sign
					    if (event.keyCode == keysToAllowAry.minus || event.keyCode == keysToAllowAry.dash)
					    {
						    if (textBox.value.length > 0 || textBox.getAttribute('maskAcceptNegative') != 'true')
						    {
							    Event.stop(event);
						    }
					    }
					    // don't allow multiple decimal places
					    else if (event.keyCode == keysToAllowAry.period || event.keyCode == keysToAllowAry.dec)
					    {
						    var decimalIndex = textBox.value.indexOf('.');
    						
						    if (decimalIndex == caretPosition)
						    {
							    Event.stop(event);
							    Mask.SetCaretPosition(textBox, caretPosition + 1);
						    }
						    else if (decimalIndex > -1)
						    {
							    Event.stop(event);
						    }
					    }
					    else
					    {
						    var decimalIndex = textBox.value.indexOf('.');
						    if (decimalIndex > -1)
						    {
							    if (caretPosition > decimalIndex)
							    {
								    // don't allow more than 2 numbers after the decimal place
								    var tempNum = '';
								    tempNum = textBox.value.substring(textBox.value.indexOf('.') + 1, textBox.value.length);
    								
								    if (tempNum.length == 2)
								    {
									    if (caretPosition < textBox.value.length)
									    {
										    textBox.value = textBox.value.substring(0, caretPosition) + textBox.value.substring(caretPosition + 1, textBox.value.length);
										    Mask.SetCaretPosition(textBox, caretPosition);
									    }
									    else
									    {
										    Event.stop(event);
									    }
								    }
							    }
							    else if (textBox.value.charAt(caretPosition) == Mask.prefix)
							    {
								    Event.stop(event);
								    textBox.value = Mask.prefix + String.fromCharCode(event.keyCode) + textBox.value.substring(1, textBox.value.length);
								    Mask.SetCaretPosition(textBox, caretPosition + 2);
							    }
						    }
					    }
					    break;
    					
				    case 'phone':
					    Event.stop(event);
					    if (textBox.value.length < 14)
					    {
						    var oldLength = textBox.value.length;
    						
						    textBox.value = textBox.value.substring(0, caretPosition) + String.fromCharCode(event.keyCode) + textBox.value.substring(caretPosition, textBox.value.length);		
						    Mask.FormatPhone(textBox);
    						
						    var newLength = textBox.value.length;
						    caretPosition += newLength - oldLength;
						    Mask.SetCaretPosition(textBox, caretPosition);
					    }
					    break;
    				
				    default:
					    break;
			    }
			    return;
		    }
	    }
	    // always allow these keys to be pressed
	    for (allowKey in systemKeysAry)
	    {
		    if (event.keyCode == systemKeysAry[allowKey])
		    {
			    switch (maskType)
			    {
				    case 'date':
					    if (event.keyCode == Event.KEY_BACKSPACE || event.keyCode == Event.KEY_DELETE)
					    {
						    Mask.SmartDeleteDate(textBox);
					    }
					    break;
    					
				    case 'phone':
					    if (event.keyCode == Event.KEY_BACKSPACE || event.keyCode == Event.KEY_DELETE)
					    {
						    var setPosition = Mask.SmartDeletePhone(textBox);
						    Mask.FormatPhone(textBox);
						    Mask.SetCaretPosition(textBox, setPosition);
					    }
					    break;
    				
				    default:
					    break;
			    } // end switch
			    return;			
		    } // end if
	    } // end for	
	    Event.stop(event);		
    },

    getSelectedText: function()
    {
	    var txt = '';
    	
	    if (window.getSelection)
	    {
		    txt = window.getSelection();		
	    }
	    else if (document.getSelection)
	    {
		    txt = document.getSelection();		
	    }
	    else if (document.selection)
	    {
		    txt = document.selection.createRange().text;
	    }
	    else return;

      return txt;
    },

    SmartDeleteDate: function(textBox)
    {
	    var caretPosition = Mask.GetCaretPosition(textBox);
	    if (event.keyCode == Event.KEY_BACKSPACE)
	    {
		    if (caretPosition > 0)
		    {
			    // if user is deleting a slash
			    if (textBox.value.charAt(caretPosition - 1) == '/')
			    {
				    Event.stop(event);
				    // if cursor is at the end of the entered value
				    if (caretPosition == textBox.value.length)
				    {
					    // since it's the end of the string just delete the last number and the slash
					    textBox.value = textBox.value.substring(0, textBox.value.length - 2);
				    }
				    else
				    {
					    // delete the number left of the slash and remove the slash if necessary
					    var secondSubstringIndex = caretPosition;
					    if (caretPosition == 3 || textBox.value.charAt(caretPosition - 4) == '/')
					    {
						    secondSubstringIndex--;
					    }
					    textBox.value = textBox.value.substring(0, caretPosition - 2) + textBox.value.substring(secondSubstringIndex, textBox.value.length);
					    Mask.SetCaretPosition(textBox, caretPosition - 2);
				    }
			    }
			    // if user is deleting a number
			    else
			    {
				    var isValidSmartDelete = caretPosition != textBox.value.length && textBox.value.charAt(caretPosition) == '/' && (caretPosition == 1 || textBox.value.charAt(caretPosition - 2) == '/');
				    if (isValidSmartDelete)
				    {					
					    Event.stop(event);
					    textBox.value = textBox.value.substring(0, caretPosition - 1) + textBox.value.substring(caretPosition + 1, textBox.value.length);
					    Mask.SetCaretPosition(textBox, caretPosition - 1);
				    }
			    }
		    }
	    }
	    else if (event.keyCode == Event.KEY_DELETE)
	    {
		    if (caretPosition < textBox.value.length)
		    {
			    // if user is deleting a slash
			    if (textBox.value.charAt(caretPosition) == '/')
			    {
				    Event.stop(event);
				    // delete the number to the right of the slash and remove the slash if necessary
				    var firstSubstringEndIndex = caretPosition + 1;
				    if (textBox.value.charAt(caretPosition + 2) == '/')
				    {
					    firstSubstringEndIndex--;
				    }
				    textBox.value = textBox.value.substring(0, firstSubstringEndIndex) + textBox.value.substring(caretPosition + 2, textBox.value.length);
				    Mask.SetCaretPosition(textBox, caretPosition + 2);
			    }
			    // if user is deleting a number
			    else
			    {
				    var isValidSmartDelete = textBox.value.charAt(caretPosition + 1) == '/';
				    if (isValidSmartDelete)
				    {					
					    Event.stop(event);
					    textBox.value = textBox.value.substring(0, caretPosition) + textBox.value.substring(caretPosition + 2, textBox.value.length);
					    Mask.SetCaretPosition(textBox, caretPosition);
				    }
			    }
		    }
	    }
    },

    SmartDeletePhone: function(textBox)
    {
	    Event.stop(event);
	    var caretPosition = Mask.GetCaretPosition(textBox);
	    var deleteChar = '';
	    var setPosition = 0;
	    if (event.keyCode == Event.KEY_BACKSPACE)
	    {
		    if (caretPosition > 1)
		    {
			    // find the nearest number to the left of the caret for deletion
			    for (i = caretPosition - 1; i > 0; i--)
			    {
				    setPosition = Mask.DeletePhoneCharAt(textBox, i, caretPosition);
				    if (setPosition > -1)
				    {	
					    return setPosition;
				    }
			    }
		    }
	    }
	    else if (event.keyCode == Event.KEY_DELETE)
	    {
		    if (caretPosition < textBox.value.length)
		    {
			    // find the nearest number to the right of the caret for deletion
			    for (i = caretPosition; i < textBox.value.length; i++)
			    {
				    setPosition = Mask.DeletePhoneCharAt(textBox, i, caretPosition);
				    if (setPosition > -1)
				    {	
					    return setPosition;
				    }
			    }
		    }
	    }
    },

    DeletePhoneCharAt: function(textBox, i, caretPosition)
    {
	    var setPosition = 0;
	    deleteChar = textBox.value.charAt(i);
	    if (deleteChar != '(' && deleteChar != ')' && deleteChar != ' ' && deleteChar != '-')
	    {
		    if (textBox.value.length == 2)
		    {
			    textBox.value = '';
			    return;
		    }
		    else if (caretPosition == textBox.value.length)
		    {
			    textBox.value = textBox.value.substring(0, i);																	
			    setPosition = textBox.value.length;
		    }
		    else 
		    {
			    textBox.value = textBox.value.substring(0, i) + textBox.value.substring(i + 1, textBox.value.length);
			    setPosition = i;
		    }
		    return setPosition;
	    }
	    return -1;
    },

    FormatPhone: function(textBox)
    {
	    var tempNum = '';
	    var formattedNum = '';
	    for (i = 0; i < textBox.value.length; i++)
	    {
		    if (textBox.value.charAt(i) != '(' && textBox.value.charAt(i) != ')' && textBox.value.charAt(i) != ' ' && textBox.value.charAt(i) != '-')
		    {
			    tempNum += textBox.value.charAt(i);
		    }
	    }
	    for (i = 0; i < tempNum.length; i++)
	    {
		    switch (i)
		    {
			    case 0:
				    formattedNum += '(' + tempNum.charAt(i); 
				    break;
    				
			    case 2:
				    formattedNum += tempNum.charAt(i) + ') ';
				    break;
    			
			    case 5:
				    formattedNum += tempNum.charAt(i) + '-';
				    break;
    				
			    default:
				    formattedNum += tempNum.charAt(i); 
				    break;
		    }
	    }
	    textBox.value = formattedNum;
    },

    TextMaskValidatorOnChange: function(e)
    {
	    if (typeof(this.onchange) == 'function' && this.getAttribute('isChanged') == 'true')
	    {
		    this.setAttribute('isChanged', 'false');
		    this.onchange();
	    }
    },

    MaskDateNumericKeyDown: function(textBox)
    {
	    var tempNum = '';
	    var slashCount = Mask.CountSlashes(textBox);
	    var caretPosition = Mask.GetCaretPosition(textBox);
	    var keyChar = String.fromCharCode(event.keyCode);
    	
	    switch (slashCount)
	    {
		    case 0:
			    if (textBox.value.length == 1)
			    {
				    textBox.value += keyChar + '/';
				    Event.stop(event);
			    }
			    else if (textBox.value.length == 4)
			    {
				    if (caretPosition == 0)
				    {
					    textBox.value = keyChar + '/' + textBox.value;
					    Mask.SetCaretPosition(textBox, 1);
				    }
				    Event.stop(event);
			    }
			    break;
    			
		    case 1:
			    var slashIndex = textBox.value.indexOf('/');
			    if (caretPosition <= slashIndex)
			    {
				    tempNum = textBox.value.substring(0, slashIndex);
				    if (tempNum.length == 2)
				    {
					    if (caretPosition == 0)
					    {
						    textBox.value = keyChar + '/' + textBox.value;
						    Mask.SetCaretPosition(textBox, 1);
					    }
					    else if (caretPosition == 2)
					    {
						    textBox.value = textBox.value.substring(0, slashIndex + 1) + keyChar + '/' + textBox.value.substring(slashIndex + 1, textBox.value.length);
						    Mask.SetCaretPosition(textBox, 4);
					    }
					    Event.stop(event);
				    }
			    }
			    else
			    {
				    tempNum = textBox.value.substring(slashIndex + 1, textBox.value.length);
				    if (tempNum.length == 4)
				    {
					    if (caretPosition == slashIndex + 1)
					    {
						    textBox.value = textBox.value.substring(0, slashIndex + 1) + keyChar + '/' + textBox.value.substring(slashIndex + 1, textBox.value.length);
						    Mask.SetCaretPosition(textBox, 1);
					    }
					    Event.stop(event);
				    }
				    else if (tempNum.length == 1)
				    {
					    textBox.value += keyChar + '/';					
					    Event.stop(event);
				    }
			    }
			    break;
    			
		    case 2:
			    var firstSlashIndex = textBox.value.indexOf('/');
			    var secondSlashIndex = textBox.value.lastIndexOf('/');
			    if (caretPosition <= firstSlashIndex)
			    {
				    tempNum = textBox.value.substring(0, firstSlashIndex);
				    if (tempNum.length == 2)
				    {
					    Event.stop(event);
				    }
			    }
			    else if (caretPosition <= secondSlashIndex)
			    {
				    tempNum = textBox.value.substring(firstSlashIndex + 1, secondSlashIndex);
				    if (tempNum.length == 2)
				    {
					    Event.stop(event);
				    }
			    }
			    else
			    {
				    tempNum = textBox.value.substring(secondSlashIndex + 1, textBox.value.length);
				    if (tempNum.length == 4)
				    {
					    Event.stop(event);
				    }
			    }
			    break;
    			
		    default:
			    break;
	    }
    },

    CountSlashes: function(textBox)
    {
	    var slashCount = 0;
	    for (i = 0; i < textBox.value.length; i++)
	    {
		    if (textBox.value.charAt(i) == '/')
		    {
			    slashCount++;
		    }
	    }
	    return slashCount;
    },

    GetCaretPosition: function(textBox) 
    {	
	    var CaretPos = 0;	
	    // IE Support	
	    if (document.selection) 
	    {
		    textBox.focus ();		
		    var Sel = document.selection.createRange ();		
		    Sel.moveStart ('character', -textBox.value.length);		
		    CaretPos = Sel.text.length;	
	    }	
	    // Firefox support	
	    else if (textBox.selectionStart || textBox.selectionStart == '0')		
		    CaretPos = textBox.selectionStart;	
	    return (CaretPos);
    },

    SetCaretPosition: function(textBox, index)
    {	
	    if (textBox.setSelectionRange)	
	    {		
		    textBox.focus();		
		    textBox.setSelectionRange(index, index);	
	    }	
	    else if (textBox.createTextRange) 
	    {		
		    var range = textBox.createTextRange();		
		    range.collapse(true);		
		    range.moveEnd('character', index);		
		    range.moveStart('character', index);		
		    range.select();	
	    }
    },

    IsLeapYear: function(year)
    {
        return  ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0));
    },

    TextMaskValidatorOnblur: function(textBox) 
    {
       
	    var maskType = textBox.getAttribute('maskType');

	    if (textBox.value.length > 0)
	    {
		    switch (maskType)
		    {
			    case 'Date':
				    var slashCount = Mask.CountSlashes(textBox);
				    if (textBox.value == 'today')
				    {
					    // put the current date in the textbox
					    textBox.value = Mask.GetCurrentDate();
				    }
				    else
				    {
					    // make sure year is present and in 4-digit format
					    Mask.AutoCompleteYear(textBox, slashCount);
				    }
				    if (slashCount == 2)
				    {
					    // put upper and lower bounds on month/day/year entered values
					    textBox.value = Mask.AutoCorrectDateValues(textBox);
				    }
				    break;

			    case 'Currency':				
				    // remove prefix if present
				    Mask.RemoveCurrencyPrefix(textBox, Mask.prefix);

				    // remove commas if present
				    Mask.RemoveCommas(textBox);
    				
				    // add zeroes before and/or after the decimal
				    Mask.AddZeroesToDecimal(textBox);
    				
				    // insert commas if desired
				    
				    Mask.AddCommas(textBox);
                    if (textBox.getAttribute('maskHideCurrencySymbol') == 'false')
				        textBox.value = Mask.prefix + textBox.value;
				    break;

			    default:
				    break;
		    }
	    }
    },

    AddCommas: function(textBox)
    {
	    if (textBox.getAttribute('maskHideCommas') == 'false')
	    {
		    var tempNum = textBox.value.substring(0, textBox.value.indexOf('.'));
		    var firstThousands = tempNum.length % 3;
		    var commaCount = Math.floor(tempNum.length / 3);
    		
		    if (firstThousands == 0)
		    {
			    firstThousands = 3;
			    commaCount--;
		    }
    		
		    var commaIndex = 0;
		    for (i = 0; i < commaCount; i++)
		    {
			    commaIndex = i * 4 + firstThousands;
			    tempNum = tempNum.substring(0, commaIndex) + ',' + tempNum.substring(commaIndex, tempNum.length);
		    }
		    textBox.value = tempNum + textBox.value.substring(textBox.value.indexOf('.'), textBox.value.length);
	    }
    },

    AddZeroesToDecimal: function(textBox)
    {
	    var decimalFound = false;
	    // check for a decimal
	    for (i = 0; i < textBox.value.length; i++)
	    {
		    if (textBox.value.charAt(i) == '.')
		    {
			    decimalFound = true;
			    break;
		    }
	    }
    	
	    // add zeroes after the decimal
	    if (decimalFound)
	    {
		    if (textBox.value.indexOf('.') > textBox.value.length - 3)
		    {
			    textBox.value += '0';
		    }
	    }
	    else
	    {
		    textBox.value += '.00';
	    }
    	
	    // add zero before the decimal
	    if (decimalFound)
	    {
		    if (textBox.value.indexOf('.') == 0)
		    {
			    textBox.value = '0' + textBox.value;
		    }
	    }
    },

    RemoveCommas: function(textBox)
    {
	    var i = 0;
	    while (true)
	    {
		    if (i < textBox.value.length - 1)
		    {
			    if (textBox.value.charAt(i) == ',')
			    {
				    textBox.value = textBox.value.substring(0, i) + textBox.value.substring(i + 1, textBox.value.length);
			    }
			    else
			    {
				    i++;
			    }
		    }
		    else
		    {
			    if (textBox.value.charAt(i) == ',')
			    {
				    textBox.value = textBox.value.substring(0, i);
			    }
			    break;
		    }
	    }				
    },

    RemoveCurrencyPrefix: function(textBox, prefix)
    {
	    switch (textBox.value.indexOf(prefix))
	    {
		    case -1:
			    // check for minus sign
			    if (textBox.value.charAt(0) == '-')
			    {
				    prefix = '-' + prefix;
				    textBox.value = textBox.value.substring(1, textBox.value.length);
			    }
			    break;
    			
		    case 0:
			    textBox.value = textBox.value.substring(1, textBox.value.length);
			    break;
    			
		    case 1:
			    prefix = '-' + prefix;
			    textBox.value = textBox.value.substring(2, textBox.value.length);
			    break;
    		
		    default:
			    break;
	    }
    },

    GetCurrentDate: function()
    {
	    var currentTime = new Date();
	    var month = currentTime.getMonth() + 1;
	    var day = currentTime.getDate();
	    var year = currentTime.getFullYear();
	    return month + '/' + day + '/' + year;
    },

    AutoCompleteYear: function(textBox, slashCount)
    {
	    var tempNum = '';
	    var currentTime = new Date();
	    var month = currentTime.getMonth() + 1;
	    var day = currentTime.getDate();
	    var year = currentTime.getFullYear();
	    var lastSlashIndex = textBox.value.lastIndexOf('/');
	    if (slashCount == 2)
	    {
		    if (lastSlashIndex != textBox.value.length - 1)
		    {
			    tempNum = textBox.value.substring(lastSlashIndex + 1, textBox.value.length);
			    var stringYear = year + '';

			    if (tempNum.length == 1)
			    {
				    stringYear = stringYear.substring(0, 3);
				    textBox.value = textBox.value.substring(0, lastSlashIndex + 1) + stringYear + tempNum;
				    return;
			    }
    				
			    if (tempNum.length == 2 || tempNum.length == 3)
			    {
				    var centuryPrefix;
				    if (tempNum.length == 2)
				    {
					    centuryPrefix = parseInt(stringYear.substring(0, 2));
				    }
				    else
				    {
					    centuryPrefix = parseInt(stringYear.substring(0, 1));
				    }
				    var prevCenturyPrefix = centuryPrefix - 1;
				    var nextCenturyPrefix = centuryPrefix + 1;
				    var difference = Math.abs(parseInt(centuryPrefix + tempNum) - year);
				    var prevCenturyDiff = Math.abs(parseInt(prevCenturyPrefix + tempNum) - year);
				    var nextCenturyDiff = Math.abs(parseInt(nextCenturyPrefix + tempNum) - year);
				    if (difference < prevCenturyDiff && difference < nextCenturyDiff)
				    {
					    textBox.value = textBox.value.substring(0, lastSlashIndex + 1) + centuryPrefix + tempNum;
				    }
				    else if (prevCenturyDiff < difference && prevCenturyDiff < nextCenturyDiff)
				    {
					    textBox.value = textBox.value.substring(0, lastSlashIndex + 1) + prevCenturyPrefix + tempNum;
				    }
				    else
				    {
					    textBox.value = textBox.value.substring(0, lastSlashIndex + 1) + nextCenturyPrefix + tempNum;
				    }
			    }
		    }
		    else
		    {
			    textBox.value += year;
		    }
	    }
	    // if there is one slash and year has not been entered
	    else if (slashCount == 1 && textBox.value.length < 6)
	    {
		    if (lastSlashIndex != textBox.value.length - 1)
		    {
			    textBox.value += '/' + year;
		    }
	    }
    },

    AutoCorrectDateValues: function(textBox)
    {
	    var lastSlashIndex = textBox.value.lastIndexOf('/');
	    var tempMonth = textBox.value.substring(0, textBox.value.indexOf('/')) * 1;
	    var tempDay = textBox.value.substring(textBox.value.indexOf('/') + 1, lastSlashIndex) * 1;
	    var tempYear = parseInt(textBox.value.substring(lastSlashIndex + 1, textBox.value.length));
	    var isLeapYear = Mask.IsLeapYear(tempYear);
	    // month
	    if (tempMonth < 1)
	    {
		    tempMonth = 1;
	    }
	    else if (tempMonth > 12)
	    {
		    tempMonth = 12;
	    }
	    // day
	    if (tempDay < 1)
	    {
		    tempDay = 1;
	    }
	    else
	    {
		    switch (tempMonth)
		    {
			    case 2:
				    if (isLeapYear)
				    {
					    if (tempDay > 29)
					    {
						    tempDay = 29;
					    }
				    }
				    else
				    {
					    if (tempDay > 28)
					    {
						    tempDay = 28;
					    }
				    }
				    break;
    			
			    case 4:
			    case 6:
			    case 9:
			    case 11:
				    if (tempDay > 30)
				    {
					    tempDay = 30;
				    }
				    break;
    				
			    default:
				    if (tempDay > 31)
				    {
					    tempDay = 31;
				    }
				    break;
		    }
	    }
	    // year
	    if (tempYear < 1753)
	    {
		    tempYear = '1753';
	    }
	    return tempMonth + '/' + tempDay + '/' + tempYear;
    }

//    TextMaskValidatorIsValid: function(validator) 
//    { 
//	    // the evaluationfunction is called before onblur, so this function must be called inside the validator
//	    Mask.TextMaskValidatorOnblur(validator);
//    	
//	    var value = ValidatorGetValue(validator.getAttribute('controltovalidate')); 
//	    var maskType = validator.getAttribute('maskType');

//	    // backup original errormessage
//	    if (validator.getAttribute('placeholderMessage') == '')
//	    {
//		    validator.setAttribute('placeholderMessage', validator.innerText);
//	    }
//    	
//	    if (value.length > 0)
//	    {		
//		    var isValid = true;
//		    switch (validator.getAttribute('maskType'))
//		    {
//			    case 'Date':
//				    isValid = regEx.isValidDate(value);
//				    break;

//			    case 'Zip':
//				    isValid = regEx.isValidZipCode(value);
//				    break;

//			    case 'Phone':
//				    isValid = regEx.isValidPhone(value);
//				    break;

//			    case 'Email':
//				    isValid = regEx.isValidEmail(value);
//				    break;

//			    case 'SSN':
//				    isValid = regEx.isValidSSN(value);
//				    break;

//			    case 'Currency':
//				    isValid = regEx.isValidCurrency(value);
//				    break;

//			    case 'Numeric':
//				    isValid = regEx.isValidNumeric(value);
//				    break;

//			    case 'Alphanumeric':
//				    isValid = true;
//				    break;

//			    default:
//				    break;
//		    }
//		    if (!isValid && validator.innerText != validator.getAttribute('placeholderMessage'))
//		    {
//			    validator.innerText = validator.getAttribute('placeholderMessage');
//		    }
//		    return isValid;
//	    }
//	    else if (validator.getAttribute('isValidEmpty') == 'false')
//	    {
//		    validator.innerText = validator.getAttribute('emptyValueMessage');
//		    return false;
//	    }
//	    else
//	    {
//		    return true;
//	    }
//    }
}

