//DateMask.js

//Field must contain a default value of mm/dd/yyyy for this to work
//replaces the mm/dd/yyyy with numbers as the user types
 
String.prototype.trim = function () {    
  return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

String.prototype.padL = function (nLength, sChar) {  
  var sreturn = this;  
  while (sreturn.length < nLength) {   
    sreturn = String(sChar) + sreturn;  
  }  
  return sreturn;
}             

function date_onkeydown(theObj) {  
  if (window.event.srcElement.readOnly) return;  
  var key_code = window.event.keyCode;  
  var oElement = window.event.srcElement; 

  if (theObj.value.charAt(9) != 'y' && ((key_code > 47 && key_code < 58) || (key_code > 95 && key_code < 106))) {
    if (key_code > 95) key_code -= (95-47);
    oElement.value = "mm/dd/yyyy";
	}
	   
  //holding shift T initializes to today's date
  if (window.event.shiftKey && String.fromCharCode(key_code) == "T") {        
    var d = new Date();       
    oElement.value = String(d.getMonth() + 1).padL(2, "0") + "/" + String(d.getDate()).padL(2, "0") + "/" + d.getFullYear();       
    window.event.returnValue = 0;    
  } 
  
  //shift, ctrl, alt are not pressed
  if (!window.event.shiftKey && !window.event.ctrlKey && !window.event.altKey) {        
  
    //number is either from keyboard or numpad
    if ((key_code > 47 && key_code < 58) || (key_code > 95 && key_code < 106)) {
      
      //if number from numpad normalize it to regular numbers
      if (key_code > 95) key_code -= (95-47);
        
      //replace the first 'm','d','y' character we see with the number that was pressed
      oElement.value = oElement.value.replace(/[mdy]/, String.fromCharCode(key_code));        


    }        
    
    //backspace is pressed
    if (key_code == 8) {            
      if (!oElement.value.match(/^[mdy0-9]{2}\/[mdy0-9]{2}\/[mdy0-9]{4}$/)) oElement.value = "mm/dd/yyyy";
      oElement.value = oElement.value.replace(/([mdy\/]*)[0-9]([mdy\/]*)$/,                
        function ($0, $1, $2) {                    
          var idx = oElement.value.search(/([mdy\/]*)[0-9]([mdy\/]*)$/);                    
          if (idx >= 5) {                        
            return $1 + "y" + $2;                    
          } else if (idx >= 2) {                        
            return $1 + "d" + $2;                    
          } else {                       
            return $1 + "m" + $2;                    
          }                
        } 
      );           
      window.event.returnValue = 0;        
    }    
  }    
  
  //tab is pressed
  if (key_code != 9) {       
    event.returnValue = false;    
  }
}
