
////////////////////////////////////////////////////
//Globally defined -- you could move these around //
//to each function to make the code more modular  //
////////////////////////////////////////////////////

var dtFormat = "MM/DD/YYYY"; //NN
var sepChar = '/'; //NN
var fullDateMask = /^[01][0-9]\/[0-3][0-9]\/[2][0-9][0-9][0-9]/; //NN
var lastKeyStrokeVal;
var currMask;
var monthVal;
var dayVal;
var yearVal;
var autoFillVal = '='; //NN
var day1Val;
var day2Val;


//////////////////////////////////////////////////////////
// returnCurrentDate() : return the current system date //
// in MM/DD/YYYY format                                 //
//////////////////////////////////////////////////////////
function returnCurrentDate() //NN
{
   d = new Date();
   m = parseInt(d.getMonth())+1;
   if (m < 10)
   {
      m = '0' + m;
   }
   return(m + sepChar + d.getDate() + sepChar + d.getYear());
}

////////////////////////////////////////////////////////////
// scrutinizeKeyVal() : apply mask to the keystroke value //
// when each keystroke is typed                           //
////////////////////////////////////////////////////////////
function scrutinizeKeyVal(obj)
{
   ////////////////////////////////////////////////////////////////////
   // If using IE, the "String.fromCharCode(window.event.keyCode)"   //
   // will return the key value pressed. For Netscape, the "which"   //
   // keyword will return the keyvalue. NOT TESTED WITH NETSCAPE YET //
   ////////////////////////////////////////////////////////////////////

   var length = parseInt(obj.value.length);

   lastKeyStrokeVal = String.fromCharCode(window.event.keyCode); // IE =Only
   /* NOT NEEDED - MOE
   if (lastKeyStrokeVal == autoFillVal)
   {
      obj.value = returnCurrentDate();
      return -1;
   }
   END NOT NEEDED - MOE*/
   
   /////////////////////////////////////////////////////////
   // The date format is mm/dd/yyyy and leading zeros are //
   // required in the Months and Days fields              //
   /////////////////////////////////////////////////////////

   /////////////////////////////////////////////
   // FIRST CHARACTER TYPED - month field     //
   // The first char typed should be a 0 or 1 //
   /////////////////////////////////////////////
   
   if (length == 0)
   {
     currMask = /^[0-1]/;
     if (!compareValue(lastKeyStrokeVal, currMask))
     {
        return -1;
     }
   }
   ///////////////////////////////////////////////////////
   // SECOND CHARACTER TYPED - month field              //
   // if first char is 1, second char may only be 0,1,2 //
   ///////////////////////////////////////////////////////
   if (length == 1)
   {
     if (obj.value.charAt(length -1) == 1)
     {
        currMask = /^[0-2]/; //months 10,11,12
     }
     else
     {
        currMask = /^[1-9]/; //months 01-09
     }
     if (!compareValue(lastKeyStrokeVal, currMask))
     {
        return -1;
     }
     //////////////////////////////////
     // capture the month value and  //
     // Autofill the first delimiter //
     //////////////////////////////////
     monthVal = obj.value + lastKeyStrokeVal; 
     return 1;                                
   }
   

   ///////////////////////////////////////////////////////
   // THE FOURTH CHARACTER TYPED - day field            //
   // The fourth char typed should be a number...0,1,2,3//
   // We can't check for leap year yet because we don't //
   // have the year value yet. This will need to be     //
   // done after the date field is populated...         //
   ///////////////////////////////////////////////////////
   if (length == 3)  
   {
     currMask = /^[0-3]/;
     if (!compareValue(lastKeyStrokeVal, currMask))
     {
        return -1;
     }
     day1Val = lastKeyStrokeVal;
   }
   //////////////////////////////////////////////////////////
   // THE FIFTH CHARACTER TYPED - day field                //
   // The fifth char typed should be a number              //
   // if first char is 0, second char may only be 1-9      //
   // if first char is 1 or 2, second char may only be 0-9 //
   // if first char is 3, second char may only be 0-1      //
   //////////////////////////////////////////////////////////
   if (length == 4)  
   {
     if (day1Val == 0)
     {
        currMask = /^[1-9]/;
     }
     else if ((day1Val == 1) || (day1Val == 2))
     {
        currMask = /^[0-9]/;
     }
     else if (day1Val == 3)
     {
        currMask = /^[0-1]/;
     }
     else
     {
        return -1;
     }
     if (!compareValue(lastKeyStrokeVal, currMask))
     {
        return -1;
     }
     day2Val = lastKeyStrokeVal;
     return 1;
   }
   //////////////////////////////////////////////////////////////
   // THE SEVENTH CHARACTER TYPED - year field                 //
   // Safe to assume this character is going to be a 1 or 2... //
   //////////////////////////////////////////////////////////////
   if (length == 6)  
   {
      currMask = /^[1-2]/;
      if (!compareValue(lastKeyStrokeVal, currMask))
      {
         return -1;
      }
   }
        
   ///////////////////////////////////////////////////////////
   // THE EIGHTH, NINTH, TENTH CHARACTER TYPED - year field //
   ///////////////////////////////////////////////////////////
   if (length == 7) 
   {
      //alert(obj.value.charAt(6));
      //alert(obj.value);
      
      if (obj.value.charAt(6) == 1)
      {		 
         currMask = /^[9]/;
         if (!compareValue(lastKeyStrokeVal, currMask))
         {			
            return -1;
         }
      }      
      return 0;
   }
   
   if ((length == 8) || (length == 9)) 
   {
      currMask = /^[0-9]/;
      if (!compareValue(lastKeyStrokeVal, currMask))
      {
         return -1;
      }
   }
   ///////////////////////////////////////////////////////////
   // DON'T LET MORE THAN 9 CHARACTERS //
   ///////////////////////////////////////////////////////////
   if (length > 9) //Max length
   {
      return -1;
   }
   
   /////////////////////////////////////////////////////////
   // Finally, do a mask check for the date val so far... //
   /////////////////////////////////////////////////////////
   if (compareValue(lastKeyStrokeVal, currMask))
   {
      return 0;      
   }
   else
   {
      return -1;      
   }
   
   
} //end scrutinizeKeyVal()


function scrutinizeKeyShortVal(obj)
{
   ////////////////////////////////////////////////////////////////////
   // If using IE, the "String.fromCharCode(window.event.keyCode)"   //
   // will return the key value pressed. For Netscape, the "which"   //
   // keyword will return the keyvalue. NOT TESTED WITH NETSCAPE YET //
   ////////////////////////////////////////////////////////////////////

   var length = parseInt(obj.value.length);

   lastKeyStrokeVal = String.fromCharCode(window.event.keyCode); // IE =Only
   /* NOT NEEDED - MOE
   if (lastKeyStrokeVal == autoFillVal)
   {
      obj.value = returnCurrentDate();
      return -1;
   }
   END NOT NEEDED - MOE*/
   
   /////////////////////////////////////////////////////////
   // The date format is mm/dd/yyyy and leading zeros are //
   // required in the Months and Days fields              //
   /////////////////////////////////////////////////////////

   /////////////////////////////////////////////
   // FIRST CHARACTER TYPED - month field     //
   // The first char typed should be a 0 or 1 //
   /////////////////////////////////////////////
   if (length == 0)
   {
     currMask = /^[0-1]/;
     if (!compareValue(lastKeyStrokeVal, currMask))
     {
        return -1;
     }
   }
   ///////////////////////////////////////////////////////
   // SECOND CHARACTER TYPED - month field              //
   // if first char is 1, second char may only be 0,1,2 //
   ///////////////////////////////////////////////////////
   if (length == 1)
   {
     if (obj.value.charAt(length -1) == 1)
     {
        currMask = /^[0-2]/; //months 10,11,12
     }
     else
     {
        currMask = /^[1-9]/; //months 01-09
     }
     if (!compareValue(lastKeyStrokeVal, currMask))
     {
        return -1;
     }
     //////////////////////////////////
     // capture the month value and  //
     // Autofill the first delimiter //
     //////////////////////////////////
     monthVal = obj.value + lastKeyStrokeVal; 
     return 1;                                
   }
   

   //////////////////////////////////////////////////////////////
   // THE SEVENTH CHARACTER TYPED - year field                 //
   // Safe to assume this character is going to be a 1 or 2... //
   //////////////////////////////////////////////////////////////
   if (length == 3)  
   {
      currMask = /^[1-2]/;
      if (!compareValue(lastKeyStrokeVal, currMask))
      {
         return -1;
      }
   }
        
   ///////////////////////////////////////////////////////////
   // THE EIGHTH, NINTH, TENTH CHARACTER TYPED - year field //
   ///////////////////////////////////////////////////////////
   if (length == 4) 
   {
      //alert(obj.value.charAt(3));
      //alert(obj.value);
      
      if (obj.value.charAt(3) == 1)
      {		 
         currMask = /^[9]/;
         if (!compareValue(lastKeyStrokeVal, currMask))
         {			
            return -1;
         }
      }
      else if (obj.value.charAt(3) == 2)
      {		 
         currMask = /^[0]/;
         if (!compareValue(lastKeyStrokeVal, currMask))
         {			
            return -1;
         }
      }      
      return 0;
   }
   
   if ((length == 5) || (length == 6)) 
   {
      currMask = /^[0-9]/;
      if (!compareValue(lastKeyStrokeVal, currMask))
      {
         return -1;
      }
   }
   ///////////////////////////////////////////////////////////
   // DON'T LET MORE THAN 9 CHARACTERS //
   ///////////////////////////////////////////////////////////
   if (length > 7) //Max length
   {
      return -1;
   }
   
   /////////////////////////////////////////////////////////
   // Finally, do a mask check for the date val so far... //
   /////////////////////////////////////////////////////////
   if (compareValue(lastKeyStrokeVal, currMask))
   {
      return 0;      
   }
   else
   {
      return -1;      
   }
   
   
} //end scrutinizeKeyVal()

//////////////////////////////////////////////////////////
// processKeyPressDate(): Check the value of each keystroke //
// as they are typed                                    //
//////////////////////////////////////////////////////////
function processKeyPressDate(obj)
{
    // July 21, 2008 - Fernan
    // The textbox's value now defaults to the format string "MM/DD/YYYY".  Because of this,
    // scrutinizeKeyVal(obj) will always return -1 onkeypress.  The solution is to clear
    // the value IF the previous value is the format string.
    
    if(obj.value.indexOf("MM/DD/YYYY") != -1) {
        obj.value = "";
        changeActiveClass(obj);     // this is defined in AceMiniWidget.ascx
    } else {
        obj.className="DateFieldActive";
    }

   var bReturn = false;
   var retVal = scrutinizeKeyVal(obj);
   	
   if (retVal == -1) // scrutinizeKeyVal returned false: Key value =
	//does not match mask
   {

   }
   else if (retVal == 0) // scrutinizeKeyVal returned true: Key =
	//value does match mask 
   {
      bReturn = true;
   }
   else if (retVal == 1) // scrutinizeKeyVal encountered delimiter =
	//character
   {
      //////////////////////////////////////////////////////
      // This will cancel the current keypress event and  //
      // force the separator char to be appended in field //
      //////////////////////////////////////////////////////
      obj.value = obj.value + lastKeyStrokeVal + sepChar;
   }
   
   //alert("retVal: " + retVal + "\nbReturn: " + bReturn);
   return bReturn;
   
} //end processKeyPressDate()


function processKeyPressShortDate(obj)
{
   var retVal = scrutinizeKeyShortVal(obj);
   	
   if (retVal == -1) // scrutinizeKeyVal returned false: Key value =
	//does not match mask
   {
      return false;
   }
   else if (retVal == 0) // scrutinizeKeyVal returned true: Key =
	//value does match mask 
   {
      return true;
   }
   else if (retVal == 1) // scrutinizeKeyVal encountered delimiter =
	//character
   {
      //////////////////////////////////////////////////////
      // This will cancel the current keypress event and  //
      // force the separator char to be appended in field //
      //////////////////////////////////////////////////////
      obj.value = obj.value + lastKeyStrokeVal + sepChar;
      return false;
   }
} //end processKeyPressDate()

////////////////////////////////////////////////////////
// isValidDate(): Determines if a date value is valid //
// Uses the date format "MM/DD/YYYY"                  //
////////////////////////////////////////////////////////
function isValidDate(obj)     
{
	var s = new String;
	s = obj.value;
	monthVal = s.charAt(0) + s.charAt(1);
	dayVal = s.charAt(3) + s.charAt(4);
	yearVal = s.charAt(6) + s.charAt(7) + s.charAt(8) + s.charAt(9);

	if (s !== null || s !== "")
	{
		if (parseInt(dayVal) > parseInt(daysInMonth(monthVal)))
		{
			obj.value = "";
			alert("Invalid Date.");
			if (obj.enabled == true || obj.visible == true)
			{
				obj.focus();
				obj.select();
			}
		return false;
		}
	}
   return true;
}  //** end isValidDate()


/////////////////////////////////////////////
// daysInMonth(): Determines the number of //
// allowable days in a month.              //                            

/////////////////////////////////////////////
function daysInMonth(charMonth)
{
   if ((charMonth == "01") || (charMonth == "03") || (charMonth == "05")
       || (charMonth == "07") || (charMonth == "08") || (charMonth == "10")
       || (charMonth == "12"))
      return 31;

   if (charMonth == "02")
   {
      if (isLeapYear(yearVal))
         return 29;
      return 28;
   }

   if ((charMonth == "04") || (charMonth == "06") || (charMonth == "09")
       || (charMonth == "11"))
      return 30;
}


//////////////////////////////////////////////
// isLeapYear(): Determines if year is leap //
//////////////////////////////////////////////
function isLeapYear(intYear)
{
   if ((intYear % 100 == 0) && (intYear % 400 == 0))
   {
      return true;
   }
   else
   {
      if ((intYear % 4) == 0)
         return true;
      return false;
   }
}

////////////////////////////////////////////////////
// clearFields(): Clears/resets all fields on the //
// form to whatever you want                      //
////////////////////////////////////////////////////
function clearFields()
{
   DateField.value = "";
   DateField.focus();
}


//////////////////////////////////////////////////
// compareValue(): Compares a value to its mask //
// (both args are passed in)                    //
//////////////////////////////////////////////////
function compareValue(cmpVal, mask)
{
  if(!cmpVal.match(mask))
  {
     return false;
  }
  else
  {
     return true;
  }
}


/////////////////////////////////////////////////////////
// validateForm() : Validates the form when the Submit //
// button is pressed.                                  //
/////////////////////////////////////////////////////////
function validateForm(obj)
{
   if ((compareValue(DateField.value, fullDateMask)) && isValidDate(obj))
   {
      alert("Good date");
   }
   else
   {
      alert("Bad date");
      DateField.focus();
   }
}


//************************************************************************
// END scrutinizeKeyVal Routines - 
// 
//************************************************************************
