/*******************************************************************************
*  K2Share JavaScript Form Field Validation Functions                          *
*  File:   validate.js                                                         *
*  Author: Keith L. Miller                                                     *
*          Bret McGowen - IS PHONE function                                    *
*  Date:   9/20/2001                                                           *
*  COPYRIGHT 2001 K2SHARE LLC., ALL RIGHTS RESERVED                            *
*                                                                              *
*	Revisions:                                                                  *				
*		B. Roderick	7/14/2003                                                    *
*			Fixed bugs in checkSSN & validateZIP.  Added isRadioChecked.          *
********************************************************************************
*  Requirements:                                                               *
*  - User must be running a browser that supports JavaScript 1.2 (IE4+,NS4+)   *
********************************************************************************
*  Function List:                                                              *
*     isEmpty                                                                  *
*     isSelected                                                               *
*     isPhone                                                                  *
*     isAlpha                                                                  *
*     isNum                                                                    *
*     isAlphaNum                                                               *
*     isValidTime                                                              *
*     dateCheck                                                                *
*     validateZIP                                                              *
*     pwdCheck                                                                 *
*     emailCheck                                                               *
*     ssnCheck                                                                 *
*     htmlStrip                                                                *
*     stripWord                                                                *
*     limitAttach                                                              *
*     isRadioChecked                                                           *
*                                                                              *
********************************************************************************
* VARIABLE DECLARATIONS                                                        *
*******************************************************************************/
// Define variables to hold messages to be returned to user.
var msg="";

// PUT Browser info into global variables
var browserName = navigator.appName
var browserVersion = navigator.appVersion
var browserVersionNum = parseFloat(browserVersion)
var codeName=navigator.appCodeName
var userAgent=navigator.userAgent

// Determine browser type and set linebreak character appropriately
if (browserName == "Netscape" && browserVersion >= 6){
   var lb="\n";
} else {
	var lb="\r";
}

// Set variables for phone number validation
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;

// Set variables for dateCheck function and subsequent routines
var tokPat=new RegExp("^month_strict|month|Month|MONTH|yyyy|YYYY|mins|MINS|mon_strict|ampm|AMPM|mon|Mon|MON|min|MIN|dd|DD|mm|MM|yy|YY|hh|HH|ss|SS|m|M|d|D|y|Y|h|H|s|S");
var lowerMonArr={jan:1, feb:2, mar:3, apr:4, may:5, jun:6, jul:7, aug:8, sep:9, oct:10, nov:11, dec:12}
var monPatArr=new Array();
monPatArr['mon_strict']=new RegExp(/jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec/);
monPatArr['Mon']=new RegExp(/Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec/);
monPatArr['MON']=new RegExp(/JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC/);
monPatArr['mon']=new RegExp("jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec",'i');
var monthPatArr=new Array();
monthPatArr['month']=new RegExp(/^january|february|march|april|may|june|july|august|september|october|november|december/i);
monthPatArr['Month']=new RegExp(/^January|February|March|April|May|June|July|August|September|October|November|December/);
monthPatArr['MONTH']=new RegExp(/^JANUARY|FEBRUARY|MARCH|APRIL|MAY|JUNE|JULY|AUGUST|SEPTEMBER|OCTOBER|NOVEMBER|DECEMBER/);
monthPatArr['month_strict']=new RegExp(/^january|february|march|april|may|june|july|august|september|october|november|december/);
var cutoffYear=50;

/*******************************************************************************
* BEGIN FUNCTIONS                                                              *
*******************************************************************************/
/* isEmpty
	Determines if a field has data in it or not.
*/
function isEmpty(field,name,required){
	if(!required && field.length==0){
		return true
	} else if(required && field.length==0){
		msg += name+" cannot be blank."+lb;
		return false
	} else {
		if(!field){
			msg += name+" cannot be blank."+lb
			return false
		} else {
			return true
		}
	}
}

/* isCustEmpty
	Determines if a field has data in it or not.
*/
function isCustEmpty(field,name,required,defMsg){
	if(!required && field.length==0){
		return true
	} else if(required && field.length==0){
		msg += defMsg+name+" "+lb;
		return false
	} else {
		if(!field){
			msg += defMsg+name+" "+lb
			return false
		} else {
			return true
		}
	}
}

/* isSelected
	Determines if a select box has something selected.
	"NULL", "", or "0" will return false
*/
function isSelected(field,name){
	if(!field || field=="0" || field=="NULL"){
		msg += "Please specify a "+name+"."+lb
		return false
	} else {
		return true
	}
}

/* isPhone
	Validates form field to verify that data is in proper phone number format
	ex: 111-111-1111
*/
/*
function isPhone(str,name,required) {
	if((str.length == 0 && !required) || (/^\d{3}-\d{3}-\d{4}$/i.test(str))){
		return true
	} else {
		msg += "Please enter a valid "+name+lb;
		return false
	}
}
*/

/* isInteger, stripCharsInBag, checkInternational and isPhone are all part of
	the phone number validation routine */
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 isPhone(str,name,required){
	if ((str.length == 0 && !required)) {
		return true;
	} else if ((str==null)||(str=="")||(checkInternationalPhone(str)==false)){
		msg += "Please enter a valid "+name+lb;
		return false;
	} else {
		return true;
	}
 }
/* validateZIP
	Determines wheather aproperly formated 5 or 5+4 digit zip code is entered
*/
function validateZIP(field,name,required){
	var valid = "0123456789-";
	var hyphencount = 0;
	if(!required && field.length==0){
		return true
	} else {
		if (field.length!=5 && field.length!=10) {
			msg += "Please enter your 5 digit or 5 digit+4 "+name+"."+lb;
			return false;
		}
		for (var i=0; i < field.length; i++) {
			temp = "" + field.substring(i, i+1);
			if (temp == "-") hyphencount++;
			if (valid.indexOf(temp) == "-1") {
				msg += "Invalid characters in your "+name+lb;
				return false;
			}
			if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
				msg += "The hyphen character should be used with a properly formatted 5 digit+4 "+name+", like '12345-6789'."+lb;
				return false;
			}
		}
		return true;
	}
}

/* pwdCheck
	Validates pasword fields for invalid characters and length.  Also checks that
	a confirm password matches the entered password field.
	REQUIRES 4 parameters
*/
function pwdCheck(pw1,pw2,name1,name2,minLength){
	var invalid = " "; 							// Invalid character is a space
//	var minLength = 3; 							// Minimum length

	if (!minLength) minLength=3;

	// check for a value in both fields.
	if (pw1 == '' || pw2 == '') {
		msg += "Please specify both "+name1+" and "+name2+" fields."+lb;
		return false;
	}
	// check for minimum length
	if (pw1.length < minLength) {
		msg += "Your "+name1+" must be at least "+minLength+" characters long"+lb;
		return false;
	}
	// check for spaces
	if (pw1.indexOf(invalid) > -1) {
		msg += "Spaces are not allowed in the "+name1+" field."+lb;
		return false;
	} else if (pw1 != pw2) {
		msg += "The "+name1+" and "+name2+" fields do not match."+lb
		return false;
	} else {
		return true;
	}
}

/* emailCheck
	Validates email addresses including those that use an IP.
*/
function emailCheck (emailStr,name){
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
		// msg += "Email address seems incorrect (check @ and .'s)"+lb;
		msg += "Please enter a valid "+name+"."+lb;
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	if (user.match(userPat)==null) {
		// msg += "The username doesn't seem to be valid."+lb;
		msg += "Please enter a valid "+name+"."+lb;
		 return false
	}
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		  for (var i=1;i<=4;i++) {
			 if (IPArray[i]>255) {
				 // msg += "Destination IP address is invalid!"+lb;
		msg += "Please enter a valid "+name+"."+lb;
			return false
			 }
		 }
		 return true
	}
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		// msg += "The domain name doesn't seem to be valid."+lb;
		msg += "Please enter a valid "+name+"."+lb;
		 return false
	}
	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) {
		// msg += "The address must end in a three-letter domain, a four-letter domain, or two letter country."+lb;
		msg += "Please enter a valid "+name+"."+lb;
		return false
	}
	if (len<2) {
		msg += "This address is missing a hostname!"
		return false
	}
	return true;
}

/* ssnCheck
	Validates a Social Security Number
*/
function ssnCheck(ssn,name,required){
	var matchArr = ssn.match(/^(\d{3})-?\d{2}-?\d{4}$/);
	var numDashes = ssn.split('-').length - 1;
	if(!required && ssn.length==0){
		return true
	} else if(required && ssn.length==0){
		msg += "Please specify a "+name+"."+lb;
		return false
	} else {
		if (matchArr == null || numDashes == 1) {
			msg += "Invalid "+name+". Must be 9 digits or in the form NNN-NN-NNNN."+lb;
			return false
		} else if (parseInt(matchArr[1],10)==0) {
			msg += "Invalid "+name+": SSN's can't start with 000."+lb;
			return false
		} else {
			return true
		}
	}
}

/* isAlpha
	Verifies that only alpha characters are entered into the form field
*/
function isAlpha(field,name,required){
	var valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz. "
	var ok = "yes";
	var temp;
	if(!required && field.length==0){
		return true
	} else if(required && field.length==0){
		msg += "Please specify a "+name+"."+lb;
		return false
	} else {
		for (var i=0; i<field.length; i++) {
			temp = "" + field.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") ok = "no";
		}
		if (ok == "no") {
			msg += "Invalid character(s) found in "+name+" field. (Only alpha characters can be used.)"+lb;
			return false
		} else {
			return true
		}
	}
}

/* isName
	Verifies that only alpha characters - or ' are entered into the form field
*/
//Added the dash because of possible dashes in people who have two last names
function isName(field,name,required){
	var valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.- \'"
	var ok = "yes";
	var temp;
	if(!required && field.length==0){
		return true
	} else if(required && field.length==0){
		msg += "Please specify a "+name+"."+lb;
		return false
	} else {
		for (var i=0; i<field.length; i++) {
			temp = "" + field.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") ok = "no";
		}
		if (ok == "no") {
			msg += "Invalid character(s) found in "+name+" field. (Only alpha characters can be used.)"+lb;
			return false
		} else {
			return true
		}
	}
}



/* isNum
	Verifies that only numeric characters are entered into the form field
*/
function isNum(field,name,required){
	var valid = "0123456789.-"
	var ok = "yes";
	var temp;
	if(!required && field.length==0){
		return true
	} else if(required && field.length==0){
		msg += "Please specify a "+name+"."+lb;
		return false
	} else {
		for (var i=0; i<field.length; i++) {
			temp = "" + field.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") ok = "no";
		}
		if (ok == "no") {
			msg += "Invalid character(s) found in "+name+" field. (Only numeric characters can be used.)"+lb;
			return false
		} else {
			return true
		}
	}
}

/* isAlphaNum
	Verifies that only alpha numeric characters are entered into the form field
*/
function isAlphaNum(field,name,required){
	var valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.- "
	var ok = "yes";
	var temp;
	if(!required && field.length==0){
		return true
	} else if(required && field.length==0){
		msg += "Please specify a "+name+"."+lb;
		return false
	} else {
		for (var i=0; i<field.length; i++) {
			temp = "" + field.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") ok = "no";
		}
		if (ok == "no") {
			msg += "Invalid character(s) found in "+name+" field. (Only alpha numeric characters can be used.)"+lb;
			return false
		} else {
			return true
		}
	}
}

/* htmlStripper
	Removes all less than(<) and greater than (>) characters from form field
*/
function htmlStripper (field){
	filteredValues = "<>";	  				// Characters to strip out
	var i,c;
	var returnString = "";
	for (i = 0; i < field.length; i++) {  	// Search through string and append to unfiltered values to returnString.
		var c = field.charAt(i);
		if (filteredValues.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}

/* stripWord
	Replaces all words in cmp variable list with characters such as @&*%!
*/
function stripWord(field) {
	smut="#@&*%!#@&*%!#@&*%!";
	cmp="list of words you want to filter go here sepereated by spaces";
	txt=field;
	tstx="";
	for (var i=0;i<16;i++){
		pos=cmp.indexOf(" ");
		wrd=cmp.substring(0,pos);
		wrdl=wrd.length
		cmp=cmp.substring(pos+1,cmp.length);
		while (txt.indexOf(wrd)>-1){
			pos=txt.indexOf(wrd);
			txt=txt.substring(0,pos)+smut.substring(0,wrdl)
			+txt.substring((pos+wrdl),txt.length);
		}
	}
	return txt;
}

/* isValidTime
	Validates a time field with military or regular time entered
*/
function isValidTime(field,name,required) {
	// 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 = field.match(timePat);
	if(!required && field.length==0){
		return true
	} else if(required && field.length==0){
		msg += "Please specify a "+name+"."+lb;
		return false
	} else {
		if (matchArray == null) {
			msg += name+" is not in a valid format."+lb;
			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) {
			msg += "Hour must be between 1 and 12 (or 0 and 23 for military time) in "+name+lb;
			return false;
		}
		if (hour <= 12 && ampm == null) {
			if (confirm("Please indicate which time format you are using.  OK = Standard Time, CANCEL = Military Time")) {
				msg += "You must specify AM or PM in "+name+lb;
				return false
		   }
		}
		if  (hour > 12 && ampm != null) {
			msg += "You can't specify AM or PM for military time in "+name+lb;
			return false
		}
		if (minute<0 || minute > 59) {
			msg += "Minute must be between 0 and 59 in "+name+lb;
			return false
		}
		if (second != null && (second < 0 || second > 59)) {
			msg += "Second must be between 0 and 59 in "+name+lb;
			return false
		}
		return true
	}
}

/* limitAttach
	Verifies that a file is being uploaded with the proper extension
*/
function limitAttach(file,name,required) {
	extArray = new Array(".gif", ".jpg", ".png");
	allowSubmit = false;
	if(!required && file.length==0){
		return true
	} else if(required && file.length==0){
		msg += "Please specify a "+name+"."+lb;
		return false
	} else {
		while (file.indexOf("\\") != -1)
		file = file.slice(file.indexOf("\\") + 1);
		ext = file.slice(file.indexOf(".")).toLowerCase();
		for (var i = 0; i < extArray.length; i++) {
			if (extArray[i] == ext){
				allowSubmit = true;
				break;
			}
		}
		if (allowSubmit){
			return true
		} else {
			msg += name+" requires a file that end in types: "	+ (extArray.join("  ")) + lb;
		}
	}
}

/* FormatToken
	USED BY dateCheck - DO NOT MODIFY
*/
function FormatToken (token, type) {
	this.token=token;
	this.type=type;
}

/* parseFormatString
	USED BY dateCheck - DO NOT MODIFY
*/
function parseFormatString (formatStr) {
	var tokArr=new Array;
	var tokInd=0;
	var strInd=0;
	var foundTok=0;
	    
	while (strInd < formatStr.length) {
		if (formatStr.charAt(strInd)=="%" && (matchArray=formatStr.substr(strInd+1).match(tokPat)) != null) {
			strInd+=matchArray[0].length+1;
			tokArr[tokInd++]=new FormatToken(matchArray[0],"symbolic");
		} else {
			if (tokInd>0 && tokArr[tokInd-1].type=="literal") {
				tokArr[tokInd-1].token+=formatStr.charAt(strInd++);
			} else {
				tokArr[tokInd++]=new FormatToken(formatStr.charAt(strInd++), "literal");
		   }
	   }
	}
	return tokArr;
}

/* buildDate
	USED BY dateCheck - DO NOT MODIFY
*/
function buildDate(dateStr,formatStr) {
	var tokArr=parseFormatString(formatStr);
	var strInd=0;
	var tokInd=0;
	var intMonth;
	var intDay;
	var intYear;
	var intHour;
	var intMin;
	var intSec;
	var ampm="";
	var strOffset;
	var curdate=new Date();
	intMonth=curdate.getMonth()+1;
	intDay=curdate.getDate();
	intYear=curdate.getFullYear();
	intHour=0;
	intMin=0;
	intSec=0;
	
	while (strInd < dateStr.length && tokInd < tokArr.length) {
		if (tokArr[tokInd].type=="literal") {
			if (dateStr.indexOf(tokArr[tokInd].token,strInd)==strInd) {
				strInd+=tokArr[tokInd++].token.length;
				continue;
			} else {
				return "\"" + dateStr + "\" does not conform to the expected format: " + formatStr;
		   }
		}
		switch (tokArr[tokInd].token) {
			case 'm':
			case 'M':
			case 'd':
			case 'D':
			case 'h':
			case 'H':
			case 'min':
			case 'MIN':
			case 's':
			case 'S':
			curChar=dateStr.charAt(strInd);
			nextChar=dateStr.charAt(strInd+1);
			matchArr=dateStr.substr(strInd).match(/^\d{1,2}/);
			if (matchArr==null) {
				switch (tokArr[tokInd].token.toLowerCase()) {
					case 'd': var unit="day"; break;
					case 'm': var unit="month"; break;
					case 'h': var unit="hour"; break;
					case 'min': var unit="minute"; break;
					case 's': var unit="second"; break;
				}
				return "Bad " + unit + " \"" + curChar + "\" or \"" + curChar +
				nextChar + "\".";
			}
			strOffset=matchArr[0].length;
			switch (tokArr[tokInd].token.toLowerCase()) {
				case 'd': intDay=parseInt(matchArr[0],10); break;
				case 'm': intMonth=parseInt(matchArr[0],10); break;
				case 'h': intHour=parseInt(matchArr[0],10); break;
				case 'min': intMin=parseInt(matchArr[0],10); break;
				case 's': intSec=parseInt(matchArr[0],10); break;
			}
			break;
			case 'mm':
			case 'MM':
			case 'dd':
			case 'DD':
			case 'hh':
			case 'HH':
			case 'mins':
			case 'MINS':
			case 'ss':
			case 'SS':
			strOffset=2;
			matchArr=dateStr.substr(strInd).match(/^\d{2}/);
			if (matchArr==null) {
				switch (tokArr[tokInd].token.toLowerCase()) {
					case 'dd': var unit="day"; break;
					case 'mm': var unit="month"; break;
					case 'hh': var unit="hour"; break;
					case 'mins': var unit="minute"; break;
					case 'ss': var unit="second"; break;
				}
				return "Bad " + unit + " \"" + dateStr.substr(strInd,2) + "\".";
			}
			switch (tokArr[tokInd].token.toLowerCase()) {
				case 'dd': intDay=parseInt(matchArr[0],10); break;
				case 'mm': intMonth=parseInt(matchArr[0],10); break;
				case 'hh': intHour=parseInt(matchArr[0],10); break;
				case 'mins': intMin=parseInt(matchArr[0],10); break;
				case 'ss': intSec=parseInt(matchArr[0],10); break;
			}
			break;
			case 'y':
			case 'Y':

			if (dateStr.substr(strInd,4).search(/\d{4}/) != -1) {
				intYear=parseInt(dateStr.substr(strInd,4),10);
				strOffset=4;
			} else {
				if (dateStr.substr(strInd,2).search(/\d{2}/) != -1) {
					intYear=parseInt(dateStr.substr(strInd,2),10);
					if (intYear>=cutoffYear) {
						intYear+=1900;
					} else {
						intYear+=2000;
					}
					strOffset=2;
				} else {
					return "Bad year \"" + dateStr.substr(strInd,2) + 	"\". Must be two or four digits.";
	   		}
			}
			break;
			case 'yy':
			case 'YY':
			if (dateStr.substr(strInd,2).search(/\d{2}/) != -1) {
				intYear=parseInt(dateStr.substr(strInd,2),10);
				if (intYear>=cutoffYear) {
					intYear+=1900;
				} else {
					intYear+=2000;
				}
				strOffset=2;
			} else {
				return "Bad year \"" + dateStr.substr(strInd,2) + "\". Must be two digits.";
			}
			break;
			case 'yyyy':
			case 'YYYY':
			if (dateStr.substr(strInd,4).search(/\d{4}/) != -1) {
				intYear=parseInt(dateStr.substr(strInd,4),10);
				strOffset=4;
			} else {
				return "Bad year \"" + dateStr.substr(strInd,4) + "\". Must be four digits.";
			}
			break;
			case 'mon':
			case 'Mon':
			case 'MON':
			case 'mon_strict':

			monPat=monPatArr[tokArr[tokInd].token];
			if (dateStr.substr(strInd,3).search(monPat) != -1) {
				intMonth=lowerMonArr[dateStr.substr(strInd,3).toLowerCase()];
			} else {
				switch (tokArr[tokInd].token) {
					case 'mon_strict': caseStat="lower-case"; break;
					case 'Mon': caseStat="mixed-case"; break;
					case 'MON': caseStat="upper-case"; break;
					case 'mon': caseStat="between Jan and Dec"; break;
				}
				return "Bad month \"" + dateStr.substr(strInd,3) + "\". Must be " + caseStat + ".";
			}
			strOffset=3;
			break;
			case 'month':
			case 'Month':
			case 'MONTH':
			case 'month_strict':
			monPat=monthPatArr[tokArr[tokInd].token];
			matchArray=dateStr.substr(strInd).match(monPat);
			if (matchArray==null) {
				return "Can't find a month beginning at \"" +
				dateStr.substr(strInd) + "\".";
			}
			intMonth=lowerMonArr[matchArray[0].substr(0,3).toLowerCase()];
			strOffset=matchArray[0].length;
			break;
			case 'ampm':
			case 'AMPM':
			matchArr=dateStr.substr(strInd).match(/^(am|pm|AM|PM|a\.m\.|p\.m\.|A\.M\.|P\.M\.)/);
			if (matchArr==null) {
				return "Missing am/pm designation.";
			}
			if (matchArr[0].substr(0,1).toLowerCase() == "a") {
				ampm = "am";
			} else {
				ampm = "pm";
			}
			strOffset = matchArr[0].length;
			break;
		}
		strInd += strOffset;
		tokInd++;
	}
	if (tokInd != tokArr.length || strInd != dateStr.length) {
		return "\"" + dateStr + "\" is either missing desired information or has more information than the expected format: " + formatStr;
	}
	if (intMonth < 1 || intMonth > 12) {
		return "Month must be between 1 and 12.";
	}
	if (intDay < 1 || intDay > 31) {
		return "Day must be between 1 and 31.";
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && intDay == 31) {
		return "Month "+intMonth+" doesn't have 31 days!";
	}
	if (intMonth == 2) {
		var isleap=(intYear%4==0 && (intYear%100!=0 || intYear%400==0));
		if (intDay > 29 || (intDay == 29 && !isleap)) {
			return "February " + intYear + " doesn't have " + intDay + " days!";
		}
	}
	if (ampm == "") {
		if (intHour < 0 || intHour > 23) {
			return "Hour must be between 0 and 23 for military time.";
	   }
	} else {
		if (intHour < 1|| intHour > 12) {
			return "Hour must be between 1 and 12 for standard time.";
	   }
	}
	if (ampm=="am" && intHour==12) {
		intHour=0;
	}
	if (ampm=="pm" && intHour < 12) {
		intHour += 12;
	}
	if (intMin < 0 || intMin > 59) {
		return "Minute must be between 0 and 59.";
	}
	if (intSec < 0 || intSec > 59) {
		return "Second must be between 0 and 59.";
	}
		return new Date(intYear,intMonth-1,intDay,intHour,intMin,intSec);
	}

/* dateCheck
	Validates a date based on the input filter sent in variable formatStr
*/
function dateCheck(dateStr,formatStr,name,required) {
	var myObj = buildDate(dateStr,formatStr);
	if(!required && dateStr.length==0){
		return true
	} else if(required && dateStr.length==0){
		msg += "Please specify a "+name+"."+lb;
		return false
	} else {
		if (typeof myObj == "object") {
			return true;
		} else {
			msg += name+": "+myObj+lb;
			return false;
	   }
	}
}

/* isRadioChecked
	Verifies that a redio button is selected
*/
function isRadioChecked(thisbutton,defMsg) {
	myOption = -1;
	for (i=0; i<thisbutton.length; i++) {
		if (thisbutton[i].checked) {
			myOption = i;
		}
	}
	if (myOption == -1) {
		msg += defMsg+lb;
		return false;
	} else {
		return true
	}
}
