// isYear (STRING s [, BOOLEAN emptyOK])
// 
// isYear returns true if string s is a valid 
// Year number.  Must be 2 or 4 digits only.
// 
// For Year 2000 compliance, you are advised
// to use 4-digit year numbers everywhere.
//
// And yes, this function is not Year 2000 compliant, but 
// because I am giving you 8003 years of advance notice,
// I don't feel very guilty about this ...
//
// For B.C. compliance, write your own function. ;->
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isYear (s)
{   //if (IsEmpty(s)) return false;
    return ((s.length == 2) || (s.length == 4));
	//return (s.length == 4);
}

// isIntegerInRange (STRING s, INTEGER a, INTEGER b [, BOOLEAN emptyOK])
// 
// isIntegerInRange returns true if string s is an integer 
// within the range of integer arguments a and b, inclusive.
// 
// For explanation of optional argument emptyOK,
// see comments of function isInteger.


function isIntegerInRange (s, a, b)
{   //if (IsEmpty(s)) return false;
    // Catch non-integer strings to avoid creating a NaN below,
    // which isn't available on JavaScript 1.0 for Windows.
    if (isNaN(s, false)) return false;
    // Now, explicitly change the type to integer via parseInt
    // so that the comparison code below will work both on 
    // JavaScript 1.2 (which typechecks in equality comparisons)
    // and JavaScript 1.1 and before (which doesn't).
    
	//var num = parseInt (s); //Not working for 08 and 09
	var num = Number(s); // *** changed from parseInt to Number ***
	
    return ((num >= a) && (num <= b));
}



// isMonth (STRING s [, BOOLEAN emptyOK])
// 
// isMonth returns true if string s is a valid 
// month number between 1 and 12.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isMonth (s)
{   //if (IsEmpty(s)) return false;
    return isIntegerInRange (s, 1, 12);
}



// isDay (STRING s [, BOOLEAN emptyOK])
// 
// isDay returns true if string s is a valid 
// day number between 1 and 31.
// 
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isDay (s)
{   //if (IsEmpty(s)) return false;
    return isIntegerInRange (s, 1, 31);
}


function daysInMonth (month) {
	
	month = Number(month);
	if (month == 4 || month == 6 || month == 9 || month == 11) return 30;
	return 31;	
	
}


// daysInFebruary (INTEGER year)
// 
// Given integer argument year,
// returns number of days in February of that year.

function daysInFebruary (year)
{   // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}



// isDate (STRING year, STRING month, STRING day)
//
// isDate returns true if string arguments year, month, and day 
// form a valid date.
// 

function isDate (month, day, year)
{   // catch invalid years (not 2- or 4-digit) and invalid months and days.

	if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false;
    // Explicitly change type to integer to make code work in both
    // JavaScript 1.1 and JavaScript 1.2.
    var intYear = parseInt(year);
    var intMonth = parseInt(month);
    var intDay = parseInt(day);

    // catch invalid days, except for February
    if (intDay > daysInMonth(intMonth)) return false; 

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}

function IsValidDate(val) {
//Input should be in the format m/d/yy or mm/dd/yyyy
	var d = val.split("/");
	if ( d.length == 3)	return isDate(d[0], d[1], d[2]);
	
	return false;
}

function formatdate(val) {
//Input must be a 2000 century date that has passed IsValidDate function.

	var d = val.split("/");
	var yr = FourDigitYear(d[2]);
	var mth = TwoCharacterMonth(d[0]);
	var day = TwoCharacterDay(d[1]);
	var fd = mth + "/" + day + "/" + yr; 
	
 	//alert("format date: " + fd);
 	//var nd = new Date(yr, d[0]-1, d[1]);
 	//alert("new date: " + nd);
 	
 	return fd;
}

function formatbirthdate(dval, curyr) {
//Format birthdate as mm/dd/yyyy deriving century from fact that birthdate
//cannot be in a future year.
//Input must be a birth date that has passed IsValidDate function
//and therefore have valid values for month, day, and year.
	
	var d = dval.split("/");
	var yr = (d[2]);
	
	switch(yr.length)
	//Set yr to its right most two characters if its length is greater than 2.
	{
	   case 3: case 4:
	      //Length starts with 1, but charAt starts at displacement 0.
	      //Take right most 2 characters.
		  yr = yr.charAt(yr.length - 2) + yr.charAt(yr.length - 1);
		  break;
	}  //end switch
		
	if (parseInt(yr) > parseInt(curyr)) {
	   yr = parseInt(yr) + 1900;
	}
	else  {
	   yr = parseInt(yr) + 2000;
	}
	
	var mth = TwoCharacterMonth(d[0]);
	var day = TwoCharacterDay(d[1]);
	var fd = mth + "/" + day + "/" + yr; 
	 		 	
 	return fd;
}

function IsEmpty (val) {
//Description:	Checks whether a value passed in is empty or not
	var re = /[\S]{1,}/; //At least one non space character
	return !re.test(val);
}

function IsNumeric (val) {
//Description:	Checks whether a value has all numeric chars or  not
	var re = /^[0-9]{1,}$/;  
	return re.test(val);
}

function IsValidEmail(val) {
//Validate Email
	var reEmail = /^.+\@.+\..+$/
	return reEmail.test(val);
}

function GetTodaysDate() {
//returns Todays Date (short format, full year)
	var today = new Date();
	var year = today.getYear();
	
	if(year<1000) year+=1900;
	
    return ((today.getMonth()+1) + "/" + today.getDate() + "/" + year);
}

function LaterThanToday(val) {
	var yr;
	var dToday = new Date();
	
	var d = val.split("/");
	yr = FourDigitYear(d[2]);
	var dCheck = new Date(yr, d[0]-1, d[1]);

 	return (dCheck > dToday);
}


// wem functions below:

function IsValid_mm_dd_yyyy(val) 
{
//Input should be in the format mm/dd/yyyy
// Code below enforces use of 4 digit year:
	if (IsEmpty(val) || val.length != 10) 
	{
	    return false;
	}
	else 
	{
		if (val.charAt(2) != "/") 
		{
		   return false;     
		}
	}
	
	if (val.charAt(5) != "/") 
	{
		return false;     
	}
	else
	{
		if (!IsValidDate(val)) 
		{
		     return false;     
		}
	}
	
	//date value is a valid mm/dd/yyyy formated date
	return true;
}

function LaterThanEmpTermDate(val, etd)
//may need 4 digit year
{
	var d = val.split("/");
	var dCheck = new Date(d[2], d[0]-1, d[1]);
	
	var ed = etd.split("/");
	var edCheck = new Date(ed[2], ed[0]-1, ed[1]);

 	return (dCheck > edCheck);
}

function Date1LessThanDate2(val1, val2)
//both dates must have 2 digit year or both dates must have 4 digit year for valid comparison.
{
	var d1 = val1.split("/");
	d1 = new Date(d1[2], d1[0]-1, d1[1]);
	
	var d2 = val2.split("/");
	d2 = new Date(d2[2], d2[0]-1, d2[1]);

 	return (d1 < d2);
}

function FromDateLaterThanToDate(from_date, to_date)
//may need 4 digit year
{
	var d1 = from_date.split("/");
	var d1Check = new Date(d1[2], d1[0]-1, d1[1]);
	
	var d2 = to_date.split("/");
	var d2Check = new Date(d2[2], d2[0]-1, d2[1]);

 	return (d1Check > d2Check);
}

function FourDigitYear(ValidYear)
{   
    //Input must be a valid 2 or 4 digit year
    var yr = parseInt(ValidYear);
    if (yr < 1000)  {
       yr = yr + 2000;
       return yr;
    }
    else {
       return yr;
    }
}

function TwoCharacterMonth(ValidMonth)
{   
    //Input must be a valid 1 or 2 digit month
    var mth = ValidMonth;
    if (mth.length < 2)  {
       mth = "0" + mth;
       return mth;
    }
    else {
       return mth;
    }
}

function TwoCharacterDay(ValidDay)
{   
    //Input must be a valid 1 or 2 digit day
    var day = ValidDay;
    if (day.length < 2)  {
       day = "0" + day;
       return day;
    }
    else {
       return day;
    }
}

//function InAllowedDateRangeOld(date_mm_dd_yyyy, startCCYYMM, endCCYYMM)
function InAllowedDateRange(val, startdate, enddate)
{
  //Prerequisite is that date has passed isDate function.
  //Supports mm/dd/yy and also mm/dd/yyyy
  //Date function parms are yr_num, mo_num, day_num.
  //For Date function month integer is represented as 0 through 11.
    var yr;
      
    var d = val.split("/");
    yr = FourDigitYear(d[2]);
	var dck = new Date(yr, d[0]-1, d[1]);
	
	var sd = startdate.split("/");
	yr = FourDigitYear(sd[2]);
	var sdck = new Date(yr, sd[0]-1, sd[1]);
	
	var ed = enddate.split("/");
	yr = FourDigitYear(ed[2]);
	var edck = new Date(yr, ed[0]-1, ed[1]);
  
    //alert("val: " + val + "  startdate: " + startdate + "  enddate: " + enddate);
    //alert("dck: " + dck + "  sdck: " + sdck + " edck: " + edck);
  
    return (dck >= sdck && dck <= edck);   
  
}

function InAllowedDateRange1(val, startdate, enddate, funding)
{
  //Prerequisite is that date has passed IsValidDate function.
  //Supports mm/dd/yy and also mm/dd/yyyy
  //Date function parms are yr_num, mo_num, day_num.
  //For Date function month integer is represented as 0 through 11.
    var yr;
     
    var d = val.split("/");
    yr = FourDigitYear(d[2]);
	var dck = new Date(yr, d[0]-1, d[1]);
	
	var sd = startdate.split("/");
	yr = FourDigitYear(sd[2]);
	var sdck = new Date(yr, sd[0]-1, sd[1]);
	
	var ed = enddate.split("/");
	yr = FourDigitYear(ed[2]);
	var edck = new Date(yr, ed[0]-1, ed[1]);
  
    //alert("val: " + val + "  startdate: " + startdate + "  enddate: " + enddate);
    //alert("dck: " + dck + "  sdck: " + sdck + " edck: " + edck);
  
    var f = funding.toLowerCase();
  
    if (f == "asc")
    {
       return (dck <= edck);
    }
    else
    {
       return (dck >= sdck && dck <= edck); 
    }
}

function ValidEffDayOfMonth(dval, sEffDayOfMonthRule)
{
  // Determine if the effective date conforms to the effective date day of month rule.
  // sEffDayOfMonthRule must contain the group's sEffDayOfMonthRule.value
  // dval must contain a valid date value in format m/d/yy or m/d/yyyy.
  // mm/dd/yyyy will be split into array elements:
  // [0][1][2]
  
  var date1 = dval.split("/"); 
  var dateDD = date1[1];
  
  var s = sEffDayOfMonthRule.toLowerCase();
  
  //alert("dateDD: " + dateDD + "  s: " + s);
   
   switch(s)
		 {
			case "1st":
			    return (dateDD == "01" || dateDD == "1");
			    break;
			case "any":
			    return true;
				break;
		 } //end switch
		 
   //Unrecognized rule cannot be enforced
   return true;
}

function ValidTermDayOfMonth(dval, sTermDayOfMonthRule)
{
  // Determine if the termination date conforms to the term date day of month rule.
  // sTermDayOfMonthRule must contain the group's sTermDayOfMonthRule.value
  // dval must contain a valid date value in format m/d/yy or m/d/yyyy.
  // mm/dd/yyyy will be split into array elements and then converted into numbers.
   
  var iLastDayOfMonth;
  var dt = dval.split("/"); 
  var mth = Number(dt[0]);
  var dy = Number(dt[1]);
  var yr = Number(dt[2]);
    
  if (mth == 2) 
  {
     iLastDayOfMonth = daysInFebruary(yr);
  }
  else
  {
     iLastDayOfMonth = daysInMonth(mth);
  }

  var s = sTermDayOfMonthRule.toLowerCase();
  
  //alert("dy: " + dy + "  s: " + s);
    
   switch(s)
		 {
			case "last":
			    return (dy == iLastDayOfMonth);
				break;
			case "any":
			    return true;
				break;
		 } //end switch
		 
   //Unrecognized rule cannot be enforced
   return true;
}

 /*
function MoreThanCurrYr(chk_date)
{
	alert("chk_date: " + chk_date);
    
    var d1 = new Date(chk_date);
    var d1ccyy = d1(getFullYear);
    var d2ccyy = new Date(getFullYear);
    
	return (d1ccyy > d2ccyy);
}
*/

/* Old string date code for reference only:
  // Determine if ccyymm portion of date_mm_dd_yyyy is in range supplied.
  // Prerequisite: Function IsValid_mm_dd_yyyy should be run 1st 
  // to ensure that date_mm_dd_yyyy is in valid format of mm/dd/ccyy.
  // mm/dd/yyyy will be split into array elements:
  // [0][1][2]

   var dateCCYYMM = "";
   var date1 = date_mm_dd_yyyy.split("/"); 
   dateCCYYMM = date1[2]+date1[0];
   
   //alert("date1[2]: " + date1[2] + "  date1[0]: " + date1[0] + "  dateCCYYMM: " + dateCCYYMM);
   //alert("dateCCYYMM: " + dateCCYYMM + "  startCCYYMM: " + startCCYYMM + "  endCCYYMM: " + endCCYYMM);

   return (dateCCYYMM >= startCCYYMM && dateCCYYMM <= endCCYYMM); 
*/
