// This is DDSI code that had previously been included inline
// on just about every HTML form page


// Function to test for a valid number
function isNumber(num)
{ 
	if (num.length < 1) return false;	
	for (i = 0; i < num.length; i++)
	{
		var c = num.charAt(i);
		if (c < "0" || c > "9")
		{
			return false;
		}
	}
	return true;
}

// Function to test for a valid letter
function isLetter(letter)
{
	letter = letter.toLowerCase();
	
	for (i = 0; i < letter.length; i++)
	{
		var c = letter.charAt(i);
		if (c < "a" || c > "z")
		{
			return false;
		}
	}
	return true;
}


// Function to check that date is valid.
// e.g Will reject February 31!
function checkDate(d, m, y)
{

	if (m == 2) {
		var isleap = y % 4
		if (isleap == 0)
		{
			if (d > 29) {
				return false;
			}
		}
		else {
			if (d > 28) {
				return false;
			}
		}
	}
	else {
		if ((m == 4) ||
			(m == 6) ||
			(m == 9) ||
			(m == 11)) {
			if (d > 30) {
				return false;
			}
		}
		else {
			if (d > 31) {
				return false;
			}
		}
	}
	
	return true;
}

// Check phone number
function validPhoneNumber(phoneNum)
{
	return ((phoneNum.length == 10) & isNumber(phoneNum));
}

// Handles email checking
function handleEmail(email)
{
	var check;
	check = true;
	
	if (email.length > 0)
	{
		if (isEmail(email) == false)
		{
			check = false;
		}
	}
	
	return check;
}

// Check that Email Address in the basic format.
// The address is not been validated.
function isEmail(email)
{
	var check;
	
	atIndex = email.indexOf("@");
	emailL = email.length-1;
	lastdot = email.lastIndexOf(".");
	comma = email.indexOf(",");
	
	check = (atIndex > 0);
	check &= ((emailL - lastdot == 3) || (emailL - lastdot == 2));
	check &= (lastdot - atIndex > 1);
	check &= (comma == -1);
	
	return check;
}

// Check that Social Security No in the basic format.
function isSocSecNo(socnum)
{
	if (socnum.length < 9) return false;	
	if (isNumber(socnum) == false) return false;	
	return true;
}

// Check that Zip Code is valid.
function isZipCode(zip)
{
	return ((zip.length == 9 || zip.length == 5) && isNumber(zip));
}

// Function to test if value only contains white spaces
function isSpaces(fieldText)
{
	var strText = new String("");
	
	strText = fieldText;
		
	for (i = 0; i < strText.length; i++)
	{
		var c = strText.charAt(i);
		if (c != " ")
		{
			return false;
		}
	}
	return true;
}

function ValidateDate(datecontrol, datename) {

var err=0;
var firstSepIndex;
var secondSepIndex;
var countSep;
var newDateStr;

if (datecontrol.value.length < 1) {
    alert("Please Supply a valid " + datename + "!!")
    datecontrol.focus();
    return (false);
}
else
   {
   if (datecontrol.value.length > 0){
      for (var i=0; i < datecontrol.value.length; i++){
         var ch = datecontrol.value.substring(i, i+1);
         if ( (ch < "0" || ch > "9") && (ch != "/")) {
            alert("Invalid Date.\nCharacters Allowed are: 1..9, and '/'");
            datecontrol.focus();
            return (false);
         }
      }
   }

   dateStr=datecontrol.value
   //if only one digit for month or day - prefix with 0
   countSep = 0;
   sepIndex = dateStr.indexOf("/");
   firstSepIndex = sepIndex;
   while (sepIndex != -1){
     countSep++;
     sepIndex = dateStr.indexOf("/",sepIndex+1);
     if(sepIndex!=-1)
       secondSepIndex = sepIndex; 
     }
   

   if (firstSepIndex == -1 || countSep != 2){
      err = 1;
      newDateStr = "";
     }
   else{
      var tempMonth = dateStr.substring(0,firstSepIndex);
      var tempDay = dateStr.substring(firstSepIndex+1,secondSepIndex);
      var restOfString = dateStr.substring(secondSepIndex+1)
      if (firstSepIndex < 2)
         tempMonth = "0" + tempMonth;
      if ((secondSepIndex -(firstSepIndex+1)) < 2)
         tempDay = "0" + tempDay;
      newDateStr = tempMonth + "/" + tempDay + "/" + restOfString;
   }

   if (newDateStr.length !=10) err=1
   month = newDateStr.substring(0, 2)
   sep1 = newDateStr.substring(2, 3)
   day = newDateStr.substring(3, 5)
   sep2 = newDateStr.substring(5, 6)
   year = dateStr.substring(6, 10)
   if (month<1 || month>12) err = 1
   if (sep1 != '/') err = 1
   if (day<1 || day>31) err = 1
   if (sep2 != '/') err = 1
   if (year<0 || year>3000) err = 1
   if (month==4 || month==6 || month==9 || month==11){
      if (day==31)
         err=1
   }

   if (month==2){
      var leapyear=parseInt(year/4)
      if (isNaN(leapyear)) {
         err=1
      }
      if (day>29) err=1
      if (day==29 && ((year/4)!=parseInt(year/4))) err=1
   }

   if (err==1) {
      alert('Incorrect Date Format!!');
      datecontrol.focus();
      return (false);
   }
}
return true;
}

