/*

This is a library of functions that can validate:

Dates

Email

Zipcode

Integers

SSN's

TaxID's

PhoneNumber



Also formating of fields onChange:

TrimWhitespace

Zipcode

SSN

TaxID

Phonenumber



*/





/**

 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)

 */

// Declaring valid date character, minimum year and maximum year

var dtCh= "/";

var minYear=1900;

var maxYear=2100;



function isInteger(s){

	var i;

    for (i = 0; i < s.length; i++){   

        // Check that current character is number.

        var c = s.charAt(i);

        if (((c < "0") || (c > "9"))) return false;

    }

    // All characters are numbers.

    return true;

}



function stripCharsInBag(s, bag){

	var i;

    var returnString = "";

    // Search through string's characters one by one.

    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++){   

        var c = s.charAt(i);

        if (bag.indexOf(c) == -1) returnString += c;

    }

    return returnString;

}



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 );

}

function DaysArray(n) {

	for (var i = 1; i <= n; i++) {

		this[i] = 31

		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}

		if (i==2) {this[i] = 29}

   } 

   return this

}



function isDate(dtStr){

	var daysInMonth = DaysArray(12)

	var pos1=dtStr.indexOf(dtCh)

	var pos2=dtStr.indexOf(dtCh,pos1+1)

	var strMonth=dtStr.substring(0,pos1)

	var strDay=dtStr.substring(pos1+1,pos2)

	var strYear=dtStr.substring(pos2+1)

	strYr=strYear

	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)

	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)

	for (var i = 1; i <= 3; i++) {

		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)

	}

	month=parseInt(strMonth)

	day=parseInt(strDay)

	year=parseInt(strYr)

	if (pos1==-1 || pos2==-1){

		alert("The date format should be : mm/dd/yyyy")

		return false

	}

	if (strMonth.length<1 || month<1 || month>12){

		alert("Please enter a valid month")

		return false

	}

	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){

		alert("Please enter a valid day")

		return false

	}

	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){

		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)

		return false

	}

	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){

		alert("Please enter a valid date")

		return false

	}

return true

}



function emailCheck (emailStr) {

/* The following pattern is used to check if the entered e-mail address

   fits the user@domain format.  It also is used to separate the username

   from the domain. */

var emailPat=/^(.+)@(.+)$/

/* The following string represents the pattern for matching all special

   characters.  We don't want to allow special characters in the address. 

   These characters include ( ) < > @ , ; : \ " . [ ]    */

var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"

/* The following string represents the range of characters allowed in a 

   username or domainname.  It really states which chars aren't allowed. */

var validChars="\[^\\s" + specialChars + "\]"

/* The following pattern applies if the "user" is a quoted string (in

   which case, there are no rules about which characters are allowed

   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com

   is a legal e-mail address. */

var quotedUser="(\"[^\"]*\")"

/* The following pattern applies for domains that are IP addresses,

   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal

   e-mail address. NOTE: The square brackets are required. */

var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/

/* The following string represents an atom (basically a series of

   non-special characters.) */

var atom=validChars + '+'

/* The following string represents one word in the typical username.

   For example, in john.doe@somewhere.com, john and doe are words.

   Basically, a word is either an atom or quoted string. */

var word="(" + atom + "|" + quotedUser + ")"

// The following pattern describes the structure of the user

var userPat=new RegExp("^" + word + "(\\." + word + ")*$")

/* The following pattern describes the structure of a normal symbolic

   domain, as opposed to ipDomainPat, shown above. */

var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")





/* Finally, let's start trying to figure out if the supplied address is

   valid. */



/* Begin with the coarse pattern to simply break up user@domain into

   different pieces that are easy to analyze. */

var matchArray=emailStr.match(emailPat)

if (matchArray==null) {

  /* Too many/few @'s or something; basically, this address doesn't

     even fit the general mould of a valid e-mail address. */

	alert("Email address seems incorrect (check @ and .'s)")

	return false

}

var user=matchArray[1]

var domain=matchArray[2]



// See if "user" is valid 

if (user.match(userPat)==null) {

    // user is not valid

    alert("The username doesn't seem to be valid.")

    return false

}



/* if the e-mail address is at an IP address (as opposed to a symbolic

   host name) make sure the IP address is valid. */

var IPArray=domain.match(ipDomainPat)

if (IPArray!=null) {

    // this is an IP address

	  for (var i=1;i<=4;i++) {

	    if (IPArray[i]>255) {

	        alert("Destination IP address is invalid!")

		return false

	    }

    }

    return true

}



// Domain is symbolic name

var domainArray=domain.match(domainPat)

if (domainArray==null) {

	alert("The domain name doesn't seem to be valid.")

    return false

}



/* domain name seems valid, but now make sure that it ends in a

   three-letter word (like com, edu, gov) or a two-letter word,

   representing country (uk, nl), and that there's a hostname preceding 

   the domain or country. */



/* Now we need to break up the domain to get a count of how many atoms

   it consists of. */

var atomPat=new RegExp(atom,"g")

var domArr=domain.match(atomPat)

var len=domArr.length

if (domArr[domArr.length-1].length<2 || 

    domArr[domArr.length-1].length>4) {

   // the address must end in a two letter or three letter word.

   alert("The address must end in a three-letter domain, or two letter country.")

   return false

}



// Make sure there's a host name preceding the domain.

if (len<2) {

   var errStr="This address is missing a hostname!"

   alert(errStr)

   return false

}



// If we've gotten this far, everything's valid!

return true;

}





/**

 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)

 */



// Declaring required variables

var digits = "0123456789";

// non-digit characters which are allowed in phone numbers

var phoneNumberDelimiters = "()- ";

// characters which are allowed in international phone numbers

// (a leading + is OK)

var validWorldPhoneChars = phoneNumberDelimiters;

// Minimum no of digits in an international phone no.

var minDigitsInIPhoneNumber = 10;



function isInteger(s)

{   var i;

    for (i = 0; i < s.length; i++)

    {   

        // Check that current character is number.

        var c = s.charAt(i);

        if (((c < "0") || (c > "9"))) return false;

    }

    // All characters are numbers.

    return true;

}



function stripCharsInBag(s, bag)

{   var i;

    var returnString = "";

    // Search through string's characters one by one.

    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++)

    {   

        // Check that current character isn't whitespace.

        var c = s.charAt(i);

        if (bag.indexOf(c) == -1) returnString += c;

    }

    return returnString;

}



function checkInternationalPhone(strPhone){

s=stripCharsInBag(strPhone,validWorldPhoneChars);

return (isInteger(s) && s.length == minDigitsInIPhoneNumber);

}



function SSNValidation(ssn) {

var matchArr = ssn.match(/^(\d{3})-?\d{2}-?\d{4}$/);

var numDashes = ssn.split('-').length - 1;

	if (matchArr == null || numDashes == 1) {

		alert('Invalid SSN. Must be 9 digits in the ###-##-#### format.');

		msg = "does not appear to be valid";

		return false;

	}

	else 

		if (parseInt(matchArr[1],10)==0) {

			alert("Invalid SSN: SSN's can't start with 000.");

			msg = "does not appear to be valid";

			return false;

		}

		else {

	   }

}



function validateZIP(field) {

var valid = "0123456789-";

var hyphencount = 0;



	if (field.length!=5 && field.length!=10) {

		alert("Please enter 5 digit or 5 digit+4 zip code.");

		return false;

	}

	for (var i=0; i < field.length; i++) {

	temp = "" + field.substring(i, i+1);

		if (temp == "-") hyphencount++;

		if (valid.indexOf(temp) == "-1") {

			alert("Invalid characters in zip code. Please try again.");

			return false;

		}

		if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {

			alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'. Please try again.");

			return false;

	   }

	}

}



//Used onload to set elements for onblur formating

function InitPage()

{

        ME=document.forms[0];

        ME.elements[0].focus();

} 







//New validate SSN or TaxID function

function ValSSN(ctl, num, desc, fReq )

{

        rTxt = "";

        if( eval(ctl) )

        {

          ssn = GetNumbers(ctl.value);

          if( ssn == "" )

          {

            if (fReq)

              rTxt = desc + " is blank";

          }

          else

          {

            if( !IsNumStr(ssn) )

                rTxt = desc + " has invalid characters.";

            else if(ssn.length != num)

                rTxt = desc + " must be " + num + " digits long.";

          }

        }



        if (rTxt!="")

          rTxt = A1+(ctl.name)+Foc + rTxt + A2;

}



//Need this for new functiona

function IsNumStr(s)

{

        return !s.match(/\D/);

}



//Check to see if it is a white space for the trimwhitespace

function IsWhiteSpace( c )

{

        return( (' ' == c) || ('\t' == c) || ('\n' == c) );

}



// This function trims whitespace from the beginning and end of a string.

function trimwhitespace( Ctrl )

{

        var index;

        var end;

        var result = Ctrl.value;



        if( result.length == 0 )

                return;



        // Trim the rightside

        index = result.length-1;

        while( index >= 0 )

        {

                // If it's not whitespace we're done

                if( IsWhiteSpace(result.charAt(index)) )

                        index -= 1;

                else

                        break;

        }



        end = index+1;

        index = 0;



        if( end > 0 )

        {

                // Trim the left side

                while( index < result.length )

                {

                        // If it's not whitespace we're done

                        if( IsWhiteSpace(result.charAt(index)) )

                                index += 1;

                        else

                        break;

                }

        }



        Ctrl.value = result.substring( index, end );

}



//Gets all the numbers out of a field to do formatting

function GetNumbers(dt)

{

        numbers = dt.replace( /[^0-9]/g, "" );

        return numbers;

}



//Formats SSN onblur

function FormatSSN(Ctrl)

{

        trimwhitespace( Ctrl );

        var SSN = GetNumbers( Ctrl.value );

        Ctrl.value = "";



        // Keep us from going too far...

        if( SSN.length > 9 )

                SSN = SSN.substring(0, 9);



        if( SSN.length >= 3 )

        {

                Ctrl.value = SSN.substring(0,3) + "-";

                SSN = SSN.substring(3, SSN.length);



                if( SSN.length >= 2 )

                        Ctrl.value += SSN.substring(0,2) + "-";

                SSN = SSN.substring(2, SSN.length);

        }



        Ctrl.value += SSN;

}





function CheckEmail(Ctrl){

	var email = Ctrl.value

	if (email.indexOf("@")<2)

		{

		alert("Please make sure you have typed your email address correctly. Please check the prefix and the '@' sign.");

		return false;

		}

	//var At 

	//var SubAt

	//var LenEmail

	//At = (email.indexOf("@")+2);

	//LenEmail = email.length;

	//SubAt = email.substring(At,LenEmail);

	//if (!(SubAt.indexOf(".")>=0))

		//{

		//alert("Please make sure you have typed your email address correctly. Please check the suffix and the '.' sign.");

		//return false;

		//}

	var temp

	//Check for blank charaters

	for (var i=0; i < email.length; i++) {

	temp = "" + email.substring(i, i+1);

	 if( IsWhiteSpace(temp) )

              {

		   alert("Please make sure you have typed your email address correctly. Remove all blank characters.");

			return false;

		   }       

	}

}



function FormatPhoneNumber(Ctrl)

{

        trimwhitespace( Ctrl );

        var PhoneNumber = GetNumbers( Ctrl.value );

        Ctrl.value = "";



        // Keep us from going too far...

        if( PhoneNumber.length > 10 )

                PhoneNumber = PhoneNumber.substring(0, 10);



        if( PhoneNumber.length >= 3 )

        {

                Ctrl.value = PhoneNumber.substring(0,3) + "-";

                PhoneNumber = PhoneNumber.substring(3, PhoneNumber.length);



                if( PhoneNumber.length >= 3 )

                        Ctrl.value += PhoneNumber.substring(0,3) + "-";

                PhoneNumber = PhoneNumber.substring(3, PhoneNumber.length);

        }



        Ctrl.value += PhoneNumber;

}



function FormatZip(Ctrl)

{

        trimwhitespace( Ctrl );

        var Zip = GetNumbers( Ctrl.value );

        Ctrl.value = "";



        // Keep us from going too far...

        if( Zip.length > 9 )

                Zip = Zip.substring(0, 9);



        if( Zip.length >= 6 )

        {

                Ctrl.value = Zip.substring(0,5) + "-";

                Zip = Zip.substring(5, Zip.length);

        }



        Ctrl.value += Zip;

}





//Formats Federal Tax ID onblur

function FormatTaxID(Ctrl)

{

        trimwhitespace( Ctrl );

        var SSN = GetNumbers( Ctrl.value );

        Ctrl.value = "";



        // Keep us from going too far...

        if( SSN.length > 9 )

                SSN = SSN.substring(0, 9);



        if( SSN.length >= 2 )

        {

                Ctrl.value = SSN.substring(0,2) + "-";

                SSN = SSN.substring(2, SSN.length);

        }



        Ctrl.value += SSN;

}







//Need this global so it can be closed by closeDep()

var win



var isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;

var Emac = ((document.all)&&(isMac)) ? true : false;





//centered popup

function NewWindow(mypage, myname, w, h, scroll) {

	if (!isMac && !Emac){

		closeDep()

	}

	var winl = (screen.width - w) / 2;

	var wint = (screen.height - h) / 2;

	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable=no'

	win = window.open(mypage, myname, winprops)

	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }

}



//top left popup

function popUp(mypage, myname, w, h, scroll){

	if (!isMac && !Emac){

		closeDep()

	}

	winprops = 'height='+h+',width='+w+',top=50,left=50,scrollbars='+scroll+',resizable'

    win = window.open(mypage, myname, winprops);

} 



//Close popup

function closeDep(){

	if (!isMac && !Emac){

		if (win && win.open && !win.closed) win.close();

	}

	

}





function validRequired(formField,fieldLabel)

{

	var result = true;

	

	if (formField.value == "")

	{

		alert('Please enter a value for the "' + fieldLabel +'" field.');

		formField.focus();

		result = false;

	}

	

	return result;

}





function allDigits(str)

{

	return inValidCharSet(str,"0123456789");

}



function inValidCharSet(str,charset)

{

	var result = true;

	

	for (var i=0;i<str.length;i++)

		if (charset.indexOf(str.substr(i,1))<0)

		{

			result = false;

			break;

		}

	

	return result;

}



function isValidExpDate(formField,fieldLabel,required)

{

	var result = true;

	var formValue = formField;



	if (required && !validRequired(formField,fieldLabel))

		result = false;

  

 	if (result && (formField.length>0))

 	{

 		var elems = formValue.split("/");

 		

 		result = (elems.length == 2); // should be two components

 		var expired = false;

 		

 		if (result)

 		{

 			var month = parseInt(elems[0],10);

 			var year = parseInt(elems[1],10);

 			

 			if (elems[1].length == 2)

 				year += 2000;

 			

 			var now = new Date();

 			

 			var nowMonth = now.getMonth() + 1;

 			var nowYear = now.getFullYear();

 			

 			expired = (nowYear > year) || ((nowYear == year ) && (nowMonth > month));

 			

			result = allDigits(elems[0]) && (month > 0) && (month < 13) &&

					 allDigits(elems[1]) && ((elems[1].length == 2) || (elems[1].length == 4));

 		}

 		

  		if (!result)

 		{

 			alert('Please enter a date in the format MM/YY for the "' + fieldLabel +'" field.');

			

		}

		else if (expired)

		{

 			result = false;

 			alert('The date for "' + fieldLabel +'" has expired.');

			

		}

	} 

	

	return result;

}



function isValidCreditCardNumber(formField,ccType,fieldLabel,required)

{

	var result = true;

 	var ccNum = formField.value;



	if (required && !validRequired(formField,fieldLabel))

		result = false;

 

  	if (result && (formField.value.length>0))

 	{ 

 		if (!allDigits(ccNum))

 		{

 			alert('Please enter only numbers (no dashes or spaces) for the "' + fieldLabel +'" field.');

			formField.focus();

			result = false;

		}	



		if (result)

 		{ 

 			

 			if (!LuhnCheck(ccNum) || !validateCCNum(ccType,ccNum))

 			{

 				alert('Please enter a valid card number for the "' + fieldLabel +'" field.');

				formField.focus();

				result = false;

			}	

		} 



	} 

	

	return result;

}



function LuhnCheck(str) 

{

  var result = true;



  var sum = 0; 

  var mul = 1; 

  var strLen = str.length;

  

  for (i = 0; i < strLen; i++) 

  {

    var digit = str.substring(strLen-i-1,strLen-i);

    var tproduct = parseInt(digit ,10)*mul;

    if (tproduct >= 10)

      sum += (tproduct % 10) + 1;

    else

      sum += tproduct;

    if (mul == 1)

      mul++;

    else

      mul--;

  }

  if ((sum % 10) != 0)

    result = false;

    

  return result;

}







function GetRadioValue(rArray)

{

	for (var i=0;i<rArray.length;i++)

	{

		if (rArray[i].checked)

			return rArray[i].value;

	}

	

	return null;

}





function validateCCNum(cardType,cardNum)

{

	var result = false;

	cardType = cardType.toUpperCase();

	


	var cardLen = cardNum.length;

	var firstdig = cardNum.substring(0,1);

	var seconddig = cardNum.substring(1,2);

	var first4digs = cardNum.substring(0,4);



	switch (cardType)

	{

		case "VISA":

			result = ((cardLen == 16) || (cardLen == 13)) && (firstdig == "4");

			break;

		case "AMEX":

			var validNums = "47";

			result = (cardLen == 15) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);

			break;

		case "MC":

			var validNums = "12345";

			result = (cardLen == 16) && (firstdig == "5") && (validNums.indexOf(seconddig)>=0);

			break;

		case "DISC":

			result = (cardLen == 16) && (first4digs == "6011");

			break;

		case "DINERS":

			var validNums = "068";

			result = (cardLen == 14) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);

			break;

	}

	return result;

}



function CheckCard(Name,CardType,CardNumber,expmonth,expyear,cvv){

	if(Name.value==""){

		alert("You must enter credit card name.");

		Name.focus();

		return false;

	}

	if(CardType.value==""){

		alert("You must select a credit card type.");

		CardType.focus();

		return false;

	}

	if(CardNumber.value==""){

		alert("You must enter a credit card number.");

		CardNumber.focus();

		return false;

	}

	if (!isValidCreditCardNumber(CardNumber,CardType.value,"Credit Card Number",true)){

		CardNumber.focus()

		return false;

	}

	if(expmonth.value==""){

		alert("You must select a credit card expiration month.");

		expmonth.focus();

		return false;

	}

	if(expyear.value==""){

		alert("You must select a credit card expiration year.");

		expyear.focus();

		return false;

	}

	//create MM/YY string from 2 drop down date fields

	var ExpDate = expmonth.value+"/"+expyear.value;

	if (!isValidExpDate(ExpDate,"Card Expiration",true)){

		expmonth.focus()

		return false;

	}

	

	var SecurityLength;

	

	if(CardType.value != "AMEX"){

		SecurityLength = 3;

	}else{

		SecurityLength = 4;

	}

	

	if(cvv.value == ""){

		alert("You must enter the credit card "+SecurityLength+" digit security code other wise known as the CID. Click the (What is this?) link for description.");

		cvv.focus();

		return false;

	}

	

	if(!isInteger(cvv.value)){

		alert("You can only enter digits for your card security code or CID.");

		cvv.value = "";

		cvv.focus();

		return false;

	}

	

	if(cvv.value.length != SecurityLength){

		alert("Your card security code or CID must be "+SecurityLength+" digits long.");

		cvv.focus();

		return false;

	}

	return true;

}





function valCheckRouting(Routing,FieldName){

	var OkCharacters = '-';

	var MinCharacters = 9;

	

	s=stripCharsInBag(Routing.value,OkCharacters);

	

	if(!(isInteger(s) && Routing.value.length == MinCharacters)){

	

		alert('Please correct "'+ FieldName +'". It should contain ' + MinCharacters + ' digits. (Canada: 8 digits and a dash)');

		Routing.focus();

		return false;

	}

	return true;

}





function formatCurrency(num) {

	num = num.toString().replace(/\$|\,/g,'');

	if(isNaN(num))

	num = "0";

	sign = (num == (num = Math.abs(num)));

	num = Math.floor(num*100+0.50000000001);

	cents = num%100;

	num = Math.floor(num/100).toString();

	if(cents<10)

	cents = "0" + cents;

	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)

	num = num.substring(0,num.length-(4*i+3))+','+

	num.substring(num.length-(4*i+3));

	return (((sign)?'':'-') + '$' + num + '.' + cents);

}



function ValidateDecimal(num,field){

	nv=/^\s*\d+\.?\d{0,3}\s*$/; //Reg Exp for money decimal format

	if (!num.match(nv)){

		alert("You can only enter "+field+" in number formats i.e. 7.0 or 7");

		return false;

	}

	return true;

}



//This is a credit card mod 10 validator that does not require card type

function checkCC(s) {



  var i, n, c, r, t;



  // First, reverse the string and remove any non-numeric characters.



  r = "";

  for (i = 0; i < s.length; i++) {

    c = parseInt(s.charAt(i), 10);

    if (c >= 0 && c <= 9)

      r = c + r;

  }



  // Check for a bad string.



  if (r.length <= 1)

    return false;



  // Now run through each single digit to create a new string. Even digits

  // are multiplied by two, odd digits are left alone.



  t = "";

  for (i = 0; i < r.length; i++) {

    c = parseInt(r.charAt(i), 10);

    if (i % 2 != 0)

      c *= 2;

    t = t + c;

  }



  // Finally, add up all the single digits in this string.



  n = 0;

  for (i = 0; i < t.length; i++) {

    c = parseInt(t.charAt(i), 10);

    n = n + c;

  }



  // If the resulting sum is an even multiple of ten (but not zero), the

  // card number is good.



  if (n != 0 && n % 10 == 0)

    return true;

  else

    return false;

}





	function validRadio(formField,fieldLabel)

			{

				var result = false;



				for (i=0;i<formField.length;i++){ 

					if(formField[i].checked == true){

						result = true;

					} 

				}

				if(result == false){

					alert ('Please select a value for the following: "' + fieldLabel +'".');



				}



		return result;

		}



function HideEmail(email,emailHost,contact,subject){

	if(contact == ""){

		contact = email + "@" + emailHost;

	}

	if(subject != ""){

		subject = "?subject=" + subject; 

	}

	document.write("<a href=" + "mail" + "to:" + email + "@" + emailHost + subject + ">" + contact + "</a>");



}	



function IsValidTime(timeStr) {

	// Checks if time is in HH:MM:SS AM/PM format.

	// The seconds and AM/PM are optional.

	

	var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

	



	var matchArray = timeStr.match(timePat);

	if (matchArray == null) {

		alert("Time is not in a valid format.");

		return false;

	}

	hour = matchArray[1];

	minute = matchArray[2];

	second = matchArray[4];

	ampm = matchArray[6];

	

	if (second=="") { second = null; }

	if (ampm=="") { ampm = null }

	

	if (hour < 0  || hour > 23) {

		alert("Hour must be between 1 and 12.");

		return false;

	}

	if (hour <= 12 && ampm == null) {

		/*

		if (confirm("Please indicate which time format you are using.  OK = Standard Time, CANCEL = Military Time")) {

			*/

			alert("You must specify AM or PM.");

			return false;

	  /*

	   }

	   */

	}

	/*

	if  (hour > 12 && ampm != null) {

		alert("You can't specify AM or PM for military time.");

		return false;

	}

	*/

	if (minute<0 || minute > 59) {

		alert ("Minute must be between 0 and 59.");

		return false;

	}

	if (second != null && (second < 0 || second > 59)) {

		alert ("Second must be between 0 and 59.");

		return false;

	}

	return true;

}


function isValidDate(dateStr) {
// Checks for the following valid date formats:
// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
// Also separates date into month, day, and year variables

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

// To require a 4 digit year entry, use this line instead:
// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert("Date is not in a valid format.")
return false;
}
month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[4];
if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!")
return false
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
   }
}
return true;  // date is valid
}

