function RowHighlight(row)
{
	changeStyleClass(row, getStyleClass(row) + ' Highlight');
}
function RowUnHighlight(row)
{
	classes = getStyleClass(row).split(' ');
	newstring = '';
	for(i=0; i<classes.length; i++) {
		if(classes[i] == 'Highlight') continue;
		newstring += classes[i] + ' ';
	}
	changeStyleClass(row, newstring);
}

function getStyleClass(item)
{
	return(item.className);
}

function changeStyleClass(item, className)
{
	item.className=className;
}

function show_description(listItem) {
	infodiv = document.getElementById('info_' + listItem);
	moreinfodiv = document.getElementById('moreinfo_' + listItem);
	lessinfodiv = document.getElementById('lessinfo_' + listItem);
	if(infodiv && moreinfodiv && lessinfodiv) {
		lessinfodiv.style.display = 'inline';
		moreinfodiv.style.display = 'none';
		infodiv.style.display = 'block';
	}
}

function hide_description(listItem) {
	infodiv = document.getElementById('info_' + listItem);
	moreinfodiv = document.getElementById('moreinfo_' + listItem);
	lessinfodiv = document.getElementById('lessinfo_' + listItem);
	if(infodiv && moreinfodiv && lessinfodiv) {
		lessinfodiv.style.display = 'none';
		moreinfodiv.style.display = 'inline';
		infodiv.style.display = 'none';
	}
}

function get_nextSibling_by_className(current, className)
{
	current = current.nextSibling;
	while(current) {
		if(current.className == className) {
			return(current);
		}
		current = current.nextSibling;
	}
}

function get_previousSibling_by_className(current, className)
{
	current = current.previousSibling;
	while(current) {
		if(current.className == className) {
			return(current);
		}
		current = current.previousSibling;
	}
}

function get_nextSibling_by_tagName(current, tagName)
{
	current = current.nextSibling;
	while(current) {
		if(current.tagName == tagName) {
			return(current);
		}
		current = current.nextSibling;
	}
}

function get_previousSibling_by_tagName(current, tagName)
{
	current = current.previousSibling;
	while(current) {
		if(current.tagName == tagName) {
			return(current);
		}
		current = current.previousSibling;
	}
}

function get_nextSibling_by_name(current, name)
{
	current = current.nextSibling;
	while(current) {
		if(current.name == name) {
			return(current);
		}
		current = current.nextSibling;
	}
}

function get_previousSibling_by_name(current, name)
{
	current = current.previousSibling;
	while(current) {
		if(current.name == name) {
			return(current);
		}
		current = current.previousSibling;
	}
}

function table_set_alternating(table_id, rows_per_section)
{
	if(!rows_per_section) {
		rows_per_section = 1;
	}
	
	var table = document.getElementById(table_id);
	var tbody = table.getElementsByTagName("TBODY")[0];

	// as stupid as this looks (I could have used for and getElementsByTagName) it's because Safari doesn't work properly (apparently)
	var section_count = 0;
	var row_count = 0;
	var row = tbody.firstChild;
	do {
		if(row.nodeName == "TR") {
			row.className = (section_count%2?'even':'odd');
			if(++row_count == rows_per_section) {
				section_count++;
				row_count = 0;
			}
		}

		row = row.nextSibling;
	} while(row);

}

// THE FOLLOWING VALIDATION CODE IS
// Copyright © 2001 by Apple Computer, Inc., All Rights Reserved.
//
// You may incorporate this Apple sample code into your own code
// without restriction. This Apple sample code has been provided "AS IS"
// and the responsibility for its operation is yours. You may redistribute
// this code, but you are not permitted to redistribute it as
// "Apple sample code" after having made changes.

function checkEmail (strng, desc)
{
	var error="";
	if(desc == null) {
		desc = 'the Email address';
	}

	if (strng.length == 0) {
		error = "Please enter " + desc + ".\n";
	} else {

		var emailFilter=/^.+@.+\..{2,3}$/;

		if (!(emailFilter.test(strng))) { 
			error = desc.substring(0, 1).toUpperCase() + desc.substring(1, 255) + " is not valid.\n";
		} else {
			// test for illegal characters
			var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
			if (strng.match(illegalChars)) {
				error = desc.substring(0, 1).toUpperCase() + desc.substring(1, 255) + " contains invalid characters.\n";
			}
		}
	}
	return error;    
}


// phone number - strip out delimiters and check for 10 digits
function checkPhone(strng, desc)
{
	var error = "";
	if(desc == null) {
		desc = 'the phone number';
	}


	if (strng == "") {
		error = "Please enter " + desc + ".\n";
	} else {
		var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters

		if (isNaN(parseInt(stripped))) {
			error = desc.substring(0, 1).toUpperCase() + desc.substring(1, 255) + " contains illegal characters.";
		}
		if (!(stripped.length == 10)) {
			error = desc.substring(0, 1).toUpperCase() + desc.substring(1, 255) + " is the wrong length. Make sure you included an area code.\n";
		} 
	}
	return error;
}


// password - between 6-8 chars, uppercase, lowercase, and numeral
function checkPassword(strng, desc)
{
	var error = "";
	if(desc == null) {
		desc = 'your password';
	}

	if (strng == "") {
		error = "Please enter " + desc + ".\n";
	}

	var illegalChars = /[\W_]/; // allow only letters and numbers

	if (illegalChars.test(strng)) {
//		error = desc.substring(0, 1).toUpperCase() + desc.substring(1, 255) + " contains illegal characters.\n";
	} else if (!((strng.search(/(a-z)+/)) && (strng.search(/(A-Z)+/)) && (strng.search(/(0-9)+/)))) {
//		error = desc.substring(0, 1).toUpperCase() + desc.substring(1, 255) + " must contain at least one uppercase letter, one lowercase letter, and one numeral.\n";
	}  
	return error;    
}    


// username - 4-10 chars, uc, lc, and underscore only.
function checkUsername(strng, desc)
{
	var error = "";
	if(desc == null) {
		desc = 'your username';
	}

	if (strng == "") {
		error = "Please enter " + desc + ".\n";
	}

	var illegalChars = /\W/; // allow letters, numbers, and underscores

	if ((strng.length < 4) || (strng.length > 10)) {
		error = "The username is the wrong length.\n";
	} else if(illegalChars.test(strng)) {
		error = "The username contains illegal characters.\n";
	} 
	return error;
}       


// valid date and valid date format mm/dd/yyyy
function checkDate(strng, desc){
	
	var error = "";
	if(desc == null) {
		desc = 'the date';
	}
	
	if (strng == ""){
		return desc.substring(0, 1).toUpperCase() + desc.substring(1, 255) + " cannot be empty.\n";
	}
    
	var daysInMonth = DaysArray(12)
	var pos1=strng.indexOf(dtCh)
	var pos2=strng.indexOf(dtCh,pos1+1)
	var strMonth=strng.substring(0,pos1)
	var strDay=strng.substring(pos1+1,pos2)
	var strYear=strng.substring(pos2+1)
	var 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)
		}
	}

	var month=parseInt(strMonth)
	var day=parseInt(strDay)
	var year=parseInt(strYr)
	
	if (pos1 == -1 || pos2 == -1){
		error = desc.substring(0, 1).toUpperCase() + desc.substring(1, 255) + " format should be : mm/dd/yyyy";
	}
	
	if (strMonth.length < 1 || month < 1 || month > 12){
		error = desc.substring(0, 1).toUpperCase() + desc.substring(1, 255) + " has an invalid month value";
	}
	
	if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month]){
		error = desc.substring(0, 1).toUpperCase() + desc.substring(1, 255) + " has an invalid day value";
	}
	
	if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear){
		error = desc.substring(0, 1).toUpperCase() + desc.substring(1, 255) + " has an invalid year. Please enter a valid 4 digit year between " + minYear + " and " + maxYear;
	}

	if (strng.indexOf(dtCh,pos2 + 1) != -1 || isInteger(stripCharsInBag(strng, dtCh)) == false){
		error = desc.substring(0, 1).toUpperCase() + desc.substring(1, 255) + " is invalid. Please enter a valid date";
	}
	
	return error;
}


// valid time and valid time format hh:mm am/pm
function checkTime(strng, desc) {

	var error = "";
	if(desc == null) {
		desc = 'the time';
	}
	
	var timePat = /^(\d{1,2}):(\d{2})\s(AM|am|PM|pm)$/;
	var matchArray = strng.match(timePat);
	
	alert("0: " + matchArray[0] + " -- 1: " + matchArray[1] + " -- 2: " + matchArray[2] + " -- 3: " + matchArray[3] + " -- 4: " + matchArray[4] + " -- 5: " + matchArray[5] + " -- 6: " + matchArray[6]);
	return;

	if (matchArray == null) {
		error = desc.substring(0, 1).toUpperCase() + desc.substring(1, 255) + " is not valid.\n";;
	}
	
	var hour = matchArray[1];
	var minute = matchArray[2];
	var ampm = matchArray[4];

	if (ampm=="") { 
		error = desc.substring(0, 1).toUpperCase() + desc.substring(1, 255) + " must contain 'AM' or 'PM'.\n";
	}

	if (hour < 0  || hour > 12) {
		error = "Hour must be between 1 and 12.";
	}

	if (minute<0 || minute > 59) {
		error = "Minute must be between 0 and 59.";
	}
	
	return error;
}


// non-empty textbox
function checkEmpty(strng, desc)
{
	var error = "";
	if(desc == null) {
		desc = 'text in the text box';
	}

	if (strng.length == 0) {
		error = "Please enter " + desc + ".\n"
	}
	return error;	  
}


// valid selector from dropdown list
function checkDropdown(choice, desc)
{
	var error = "";
	if(desc == null) {
		desc = 'an item in the drop-down list';
	}

	if (choice == 0) {
		error = "Please choose " + desc + ".\n";
	}    
	return error;
}


function checkRadio(radio, desc)
{
	var radio_choice = false;

	if(desc == null) {
		desc = 'radio button';
	}

	for (counter = 0; counter < radio.length; counter++) {
		if(radio[counter].checked) {
			radio_choice = true;
		}
	}

	if (!radio_choice) {
		return "Please select a " + desc + ".\n";
	}

	return "";
}

/**
 * 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
}


