function validateDate(theDate) {
	var tokens = theDate.split('/');
	
	if(tokens.length != 3)
		return false;
	
	// Validate each token
	for(var i=0; i<tokens.length; i++) {
		// Validate the length of the tokenized date strings
		if(tokens[i].length > 2 || tokens[i].length < 1) 
			return false;
		
		// Validate that the tokens are numbers
		if(isNaN(tokens[i]))
			return false;
	}
	
	// Validate the month value
	if(parseInt(tokens[0]) > 12 || parseInt(tokens[0]) < 1)
		return false;
		
	// Validate the day value
	if(parseInt(tokens[1]) > 31 || parseInt(tokens[1]) < 1)
		return false;
	
	return true;
}

function validateEmail(theAddress) {
	var retVal	= true;
	var AtSym   = theAddress.indexOf('@');
	var Period  = theAddress.lastIndexOf('.');
	var Space   = theAddress.indexOf(' ');
	var Length  = theAddress.length - 1;  // Array is from 0 to length-1

	// '@' cannot be in first position, Must be at least one valid char btwn '@' and '.'
	// Must be at least one valid char after '.', No empty spaces permitted
	if((AtSym < 1) || (Period <= AtSym+1) || (Period == Length ) || (Space  != -1))
		retVal = false;

	return retVal;
}

var remote;
function launchWin(helpURL, size) {
	// size string should have the format of 'width=#,height=#'
	// this avoids having to change all the function calls to launchHelp()
	var firstEqual = size.indexOf("=")+1;
	var comma = size.indexOf(",")+1;
	var secondEqual =  size.lastIndexOf("=")+1;
	var sizeLen = size.length;
	
	var w = parseInt(size.substring(firstEqual, comma));
	var h = parseInt(size.substring(secondEqual, sizeLen));
	
	var xPos = (screen.height-h)/2;
	var yPos = (screen.width-w)/2;
	remote = window.open(helpURL, "Clearvue", size+",scrollbars=1,resizable=1,left="+yPos+",top="+xPos);
	remote.focus();
}

function launchWinPopup(helpURL, size) {
	// size string should have the format of 'width=#,height=#'
	// this avoids having to change all the function calls to launchHelp()
	var firstEqual = size.indexOf("=")+1;
	var comma = size.indexOf(",")+1;
	var secondEqual =  size.lastIndexOf("=")+1;
	var sizeLen = size.length;
	
	var w = parseInt(size.substring(firstEqual, comma));
	var h = parseInt(size.substring(secondEqual, sizeLen));
	
	var xPos = (screen.height-h)/2;
	var yPos = (screen.width-w)/2;
	remote = window.open(helpURL, "Clearvue", size+",scrollbars=1,resizable=1,left="+yPos+",top="+xPos);
	remote.focus();
}

var printable;
function launchPrintable(helpURL, size) {
	// size string should have the format of 'width=#,height=#'
	// this avoids having to change all the function calls to launchHelp()
	var firstEqual = size.indexOf("=")+1;
	var comma = size.indexOf(",")+1;
	var secondEqual =  size.lastIndexOf("=")+1;
	var sizeLen = size.length;
	
	var w = parseInt(size.substring(firstEqual, comma));
	var h = parseInt(size.substring(secondEqual, sizeLen));
	
	var xPos = (screen.height-h)/2;
	var yPos = (screen.width-w)/2;
	printable = window.open(helpURL, "Clearvue", size+",toolbar=1,scrollbars=1,resizable=1,left="+yPos+",top="+xPos);
	printable.focus();
}

function launchPrintWin(objectID) {
	var w = 600;
	var h = 425;
	
	var xPos = (screen.height-h)/2;
	var yPos = (screen.width-w)/2;
	remote = window.open("/printPage.asp?objectID=" + objectID, "printWin", "width=" + w + ",height=" + h + ",toolbar=1,scrollbars=1,resizable=1,left="+yPos+",top="+xPos);
	remote.focus();
}
	
function validatePhone(fld) {
	var retVal = true;
	var temp_value = fld.value;
	do {
		temp_value = temp_value.replace(" ", "");
	}
	while(temp_value.indexOf(" ") != -1);
	var Chars = "0123456789.-()";
	
	if (temp_value == "")
		return retVal;
	
	for (var i = 0; i < temp_value.length; i++) {
		if (Chars.indexOf(temp_value.charAt(i)) == -1)
			retVal = false;
	}
	return retVal;
} 


var pdfWin;
function launchPDF(pdf) {
	var w = 675;
	var h = 550;
	
	var xPos = (screen.height-h)/2;
	var yPos = (screen.width-w)/2;
	pdfWin = window.open(pdf, "PDF", "width=675,height=550,scrollbars=1,resizable=1,left="+yPos+",top="+xPos);
	pdfWin.focus();
}

/* Swap positions of two list items
   Note: used in conjunction with moveUp() & moveDown()
   list: ID of listbox */
function moveSwap(obj,i,j) {
	var o = obj.options;
	var iSelected = o[i].selected;
	var jSelected = o[j].selected;
	var temp1 = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	var temp2 = new Option(o[j].text, o[j].value, o[j].defualtSelected, o[j].selected);
	o[i] = temp2;
	o[j] = temp1;
	o[i].selected = jSelected;
	o[j].selected = iSelected;
}

/* Move list item(s) up one position
   Note: used in conjunction with moveSwap()
   list: ID of listbox */
function moveUp(list) {
	objList = document.getElementById(list);
	
	for (i = 0; i < objList.options.length; i++) {
		if (objList.options[i].selected) {
			if (i != 0 && !objList.options[i - 1].selected) {
				moveSwap(objList,i,i - 1);
				objList.options[i - 1].selected = true;
			}
		}
	}
}

/* Move list item(s) down one position
   Note: used in conjunction with moveSwap()
   list: ID of listbox */
function moveDown(list) {
	objList = document.getElementById(list);
	
	for (i = objList.options.length-1; i >= 0; i--) {
		if (objList.options[i].selected) {
			if (i != (objList.options.length - 1) && !objList.options[i + 1].selected) {
				moveSwap(objList,i,i+1);
				objList.options[i + 1].selected = true;
			}
		}
	}
}

/* Move list item(s) to top of list
   Note: used in conjunction with moveSwap() & moveUp()
   list: ID of listbox */
function moveTop(list) {
	objList = document.getElementById(list);
	var i = objList.options.length;
	
	do {
		moveUp(list);
		i--
	} while (i > 0)
}

/* Move list item(s) to bottom of list
   Note: used in conjunction with moveSwap() & moveDown()
   list: ID of listbox */
function moveBottom(list) {
	objList = document.getElementById(list);
	var i = 0;
	
	do {
		moveDown(list);
		i++
	} while (i < objList.options.length)
}

//changes the number of days (28,29,30,31) depending on the month selected
//The parameters are the ids of the month and day select boxes
//The value of th months must be numerical
function changeDays(dayFieldName, monthFieldName, yearFieldName, theForm)
{
	//alert("TEST")
	var dayField = theForm.elements[dayFieldName];
	var monthField = theForm.elements[monthFieldName];
	var yearField = theForm.elements[yearFieldName];
	
	var month = monthField[monthField.selectedIndex].value;
	var year = yearField[yearField.selectedIndex].value;

	if (month == "2"){
		if (year%4 == 0 && ((year%100 == 0 && year%400 == 0) || year%100 != 0)){ //leap year
			maxDays=29;
		}
		else {
			maxDays=28;
		}
	}
	else if (month == "4" || month == "6" || month == "9" || month == "11"){
		maxDays=30;
	}
	else {
		maxDays=31;
	}
	for (i=dayField.options.length-1;i>=0;i--) {
           dayField.options[i] = null;
       }
	for (i=0;i<maxDays;i++) {
		temp = i + 1;
           dayField.options[i] = new Option(temp, temp, false, false);
    }
}

// Validate string value
function isString(strValue) {
	//make sure numbers pass as strings as well
	return ((typeof strValue == "string" && strValue != "") || (!isNaN(strValue) && strValue != ""));
}

// Validate numeric value
function isNumber(strValue) {
	return (!isNaN(strValue) && strValue != "");
}

// Validate email address
function isEmail(strValue) {
	var objRE = /^[\w-\.\']{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,}$/;
	return (strValue != "" && objRE.test(strValue));
}

// Validate select box selection
function isSelected(objField) {
	if ((objField.multiple && objField.selectedIndex == -1) || (!objField.multiple && objField.selectedIndex == 0)) {
		return false;
	} else {
		return true;
	}
}

// Validate zip code
function isZipcode(strZip){
	var s = new String(strZip);

	if (s.length != 5 && s.length != 10)
	return false;

	for (var i=0; i < s.length; i++)
	if ((s.charAt(i) < '0' || s.charAt(s) > '9') && s.charAt(i) != '-')
	return false;

	return true;
}

// Validate whether a checkbox or radio is selected
function isChecked(objField) {
	for (i=0;i<objField.length;i++) {
		if(objField[i].checked == true){
			return true;
		}
	}
	return false;
}

// checks to see if string is all spaces
function isBlank(val){
	if(val ==null){return true;}
	for(var i=0;i<val.length;i++) {
		if ((val.charAt(i)!=' ')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r")){return false;}
	}
	return true;
}

// Validate various types
function validate(arrFields) {
	var isValid = true;
	
	for (var i = 0; i < arrFields.length; i++) {
		// arrTheField: [label, id, type, required]
		var arrTheField = arrFields[i].split("|");
		
		if (document.getElementById(arrTheField[1])) {
			var strLabel = arrTheField[0];
			var objField = document.getElementById(arrTheField[1]);
			var strType = arrTheField[2];
			var req = arrTheField[3];
			
			// Check if field is required or not required and not empty
			if (req == "true" || (req == "false" && objField.value != "")) {
				switch (strType) {
					case "string":
						isValid = isString(objField.value.replace(/^\s*|\s*$/g, ''));
						break;
					case "number":
						isValid = isNumber(objField.value);
						break;
					case "email":
						isValid = isEmail(objField.value);
						break;
					case "zip":
						isValid = isZipcode(objField.value);
						break;
					case "select":
						isValid = isSelected(objField);
						break;
					default:
						isValid = true;
				}
			}
			
			if (!isValid) {
				// If field is invalid, alert user, stop validating
				var errMessage = "";
				errMessage = "The information you provided for \"" + strLabel + "\" is invalid.\n";
				errMessage += "Please correct this and submit again.";
				alert(errMessage);
				
				if (objField.nodeName.toLowerCase() != "select") {
					objField.select();
				}
				
				objField.focus();
				return false;
			}
		}
		
		// could get here because validating checkbox/radio (getElementsByName is used instead)
		else {
			if (document.getElementsByName(arrTheField[1])) {
				var strLabel = arrTheField[0];
				var objField = document.getElementsByName(arrTheField[1]);
				var strType = arrTheField[2];
				var req = arrTheField[3];
				
				if (req == "true") {
					switch (strType) {
						case "check":
							isValid = isChecked(objField);
							break;
						default:
							isValid = true;
					}
				}
				
				if (!isValid) {
					// If field is invalid, alert user, stop validating
					var errMessage = "";
					errMessage = "The information you provided for \"" + strLabel + "\" is invalid.\n";
					errMessage += "Please correct this and submit again.";
					alert(errMessage);
					return false;
				}
			}
		}
	}
	
	return true;
}

function trimString (str) {
	str = this != window? this : str;
	return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}
String.prototype.trim = trimString;

function clearCookies() {
	// Create an array of cookie name-value pairs
	var c = document.cookie.split("; ");
	
	// Set an expiration date as yesterday
	var d = new Date();
	d.setTime(d.getTime() + (-1*24*60*60*1000));
	var e = "; expires=" + d.toGMTString();
	
	for (var i = 0; i < c.length; i++) {
		var name = c[i].split("=")[0];
		
		// Clear cookie
		document.cookie = name + "=" + e + "; path=/";
	}
	
	return;
}

// Used in reports
function compareDates(month1, date1, year1, month2, date2, year2) {
	if (parseInt(year1) > parseInt(year2))
		return 1;
	else if (parseInt(year1) < parseInt(year2))
		return -1;
	else if (parseInt(month1) > parseInt(month2))
		return 1;
	else if (parseInt(month1) < parseInt(month2))
		return -1;
	else if (parseInt(date1) > parseInt(date2))
		return 1;
	else if (parseInt(date1) < parseInt(date2))
		return -1;
	else
		return 0;
}

