function setInputClass(elem, defVal)
{
	if((typeof(elem.defVal) == 'undefined') || (elem.defVal == "null") || (elem.defVal == ""))
	{
		// never allow the default value to be null
		if (elem.defVal == "null" || elem.defVal == null)
		{
			elem.defVal = "";
		}
	    elem.setAttribute('defVal', defVal);
	    elem.defVal = defVal;
	}

	// if the value is ever null, switch it back to the default value
	if (elem.value == null || elem.value == "null")
    {
    	elem.value = elem.defVal;
    	classname(elem, 'del', 'chg');
    }

	classname(elem, 'swap', 'act', '');

	var act = classname(elem, 'check', 'act');
	var def = (elem.value == elem.defVal || elem.value.trim() == "");

	if (def && act)
	{
        elem.value = "";
    }
    else if (def)
    {
        elem.value = defVal;
        classname(elem, 'switch', 'chg', 'def');
	}
    else
    {
        classname(elem, 'switch', 'def', 'chg');
    }
	if (act)
	{
//        elem.select();
    }
}


function setMandatory(elem)
{
	if (classname(elem, 'check', 'def'))
	{
		classname(elem, 'add', 'man');
	}
	else
	{
		classname(elem, 'del', 'man');
	}
}

/**
 * onblur for inputs included with time_picker and time_period tags (see formTags.php)
 *
 * formats user input into 12-hour format (eg 17:30 becomes 5:30pm)
 *
 * @param html_node elem The input you're "blurring on" (er, off)
 * @param string usr_val The value the input had at page load time
 * @param string def_val The input's default value
 *
 * @return boolean true if the time entered is valid (clearning warning styles on input(s)
 * and setting the input's value, false otherwise (setting warning styles)
 */
function formatTime(elem, usr_val, def_val)
{
	// Get the parent div this elem belongs in, and the hidden elem corresponding to the elem
	var parent_div = get(elem, PARENT_BY_TAG, 'div');

	if (elem.value == usr_val || elem.value == def_val)
	{
		classname(elem, 'del', 'string_err');
		classname(elem, 'del', 'invalid');
		return true;
	}
	else
	{
		// Remove colons, spaces and periods
		var parsed_val = elem.value.replace(/[\. ]/g,'');

		var has_am = false;
		var has_pm = false;

		// Check for am or pm to appear in the time
		if (/(A|a)(M|m)?/.test(parsed_val))
		{
			has_am = true;
			parsed_val = parsed_val.replace(/(A|a)(M|m)?/g,'');
		}
		if (/(P|p)(M|m)?/.test(parsed_val))
		{
			has_pm = true;
			parsed_val = parsed_val.replace(/(P|p)(M|m)?/g, '');
		}

		// Check that if ':' exists, the hours and minutes are correct
		if (/:/.test(parsed_val))
		{
			var split_num = parsed_val.split(/:/);

			if(split_num[0].length > 2 || split_num[1].length > 2)
			{
				classname(elem, 'add', 'string_err');
				classname(elem, 'add', 'invalid');
				return false;
			}
			else
			{
				parsed_val = parsed_val.replace(/:/g, '');
			}
		}
		// Check that the characters are all numbers
		if (/[^0-9]/.test(parsed_val))
		{
			// Error, something other than numbers exist in the string
			classname(elem, 'add', 'invalid');
			classname(elem, 'add', 'string_err');
			return false;
		}

		// Check that the length of the time string corresponds to the right range of time
		if (parsed_val.length == 1)
		{
			if (has_am == false && has_pm == false)
			{
				classname(elem, 'add', 'string_err');
				classname(elem, 'add', 'invalid');
				return false;
			}
			else
			{
				classname(elem, 'del', 'string_err');
				classname(elem, 'del', 'invalid');
				parsed_val = '0' + parsed_val + '00';
			}
		}
		if (parsed_val.length == 2)
		{
			if (((has_am == false) && (has_pm == false)) || (parseInt(parsed_val) > 12))
			{
				classname(elem, 'add', 'string_err');
				classname(elem, 'add', 'invalid');
				return false;
			}
			else
			{
				classname(elem, 'del', 'string_err');
				classname(elem, 'del', 'invalid');
				parsed_val = parsed_val + '00';
			}
		}
		if (parsed_val.length > 4)
		{
			classname(elem, 'add', 'string_err');
			classname(elem, 'add', 'invalid');
			return false;
		}

		// Check that the time is between 0 and 2400
		if ((parsed_val < 0) || (parsed_val > 2400))
		{
			// Error, time is less than 0 or greater than 2400
			classname(elem, 'add', 'string_err');
			classname(elem, 'add', 'invalid');
			return false;
		}

		// Check that the last two digits are between 00 and 59
		var t_length = parsed_val.length;
		if ((parsed_val[t_length - 2]) > 5)
		{
			classname(elem, 'add', 'string_err');
			classname(elem, 'add', 'invalid');
			return false;
		}

		// Extra check for 12:00am
		if (parsed_val[0] == 1 && parsed_val[1] == 2 && has_am == true)
		{
			parsed_val = parsed_val.replace(/.{2}/, '0');
		}

		// Check that if time is >= 1300, and pm is given, then this is wrong
		if ((parsed_val >= 1300) && (has_pm == true))
		{
			classname(elem, 'add', 'string_err');
			classname(elem, 'add', 'invalid');
			return false;
		}

		// Value given has passed all the above tests, so it is valid
		classname(elem, 'del', 'string_err');
		classname(elem, 'del', 'invalid');


		if (parsed_val.length == 3)
		{
			parsed_val = '0' + parsed_val;
		}

		if ((parsed_val < 1200) && (has_pm == true))
		{
			parsed_val = parseInt(parsed_val, 10) + 1200;
		}

		// Recombine the colon back into the time, and display it
		parsed_val = parsed_val.toString();

		var hour = parseInt(parsed_val.charAt(0) + parsed_val.charAt(1), 10);
		var minutes = parsed_val.charAt(2) + parsed_val.charAt(3);

		var ampm = null;

		if (hour == 24 || hour < 12)
		{
			ampm = 'am';

			if (hour == 24 || hour == 0)
			{
				hour = '12';
			}
		}
		else
		{
			ampm = 'pm';
			hour = (hour != 12) ? hour % 12 : hour;
		}

		elem.value = hour + ':' + minutes + ampm;
	}
}

/**
 * helper for checkTimePeriod()
 *
 * converts param to string of HHMM (eg, 1730 for '5:30pm') format for validation
 * calculations made in checkTimePeriod()
 *
 * @return string The converted value (string is expected in checkTimePeriod())
 */
function getTimeValue(time_str)
{
	time_str = time_str.replace(/:/, '');

	var ampm_regexp = /[AaPp][Mm]?/;
	var ampm = ampm_regexp.exec(time_str);

	time_str = parseInt(time_str.replace(/\D/g, ''), 10);

	// if time string falls on noon or midnight hours
	var midnight_or_noon = time_str >= 1200 && time_str <= 1259;

	if (midnight_or_noon && is_array(ampm) && ampm[0] == 'am')
	{
		// convert 12:00am - 12:59am to values in the range 0-59
		time_str -= 1200;
	}
	else if (!midnight_or_noon && is_array(ampm) && ampm[0] == 'pm')
	{
		// convert 1pm - 11pm values to values in the range 1300-2300
		time_str += 1200;
	}

	// convert to string
	return time_str + '';
}

/**
 * onblur event for inputs used in time_period tag
 *
 * checks that end time follows start time (does not account for different dates yet, this is a bug!)
 *
 * @param html_node input node you're blurring off of (either the start or end time input)
 *
 * @return boolean true if the time span is valid (clearing warning styles from inputs),
 * false otherwise (setting warning styles on inputs)
 */
function checkTimePeriod(elem)
{
	var parent_div = get(elem, PARENT_BY_TAG, 'div');
	var elem_value = getTimeValue(elem.value);

	// Check that end time > start time if elem selected is the "from" time
	if (classname(elem, 'check', 'shown_to_value'))
	{
		var from_elem = get(parent_div, CHILD_BY_CLASS, 'shown_from_value');

		var from_value = getTimeValue(from_elem.value);

		// If either one of them are empty, then no problems
		if (elem_value == '' || from_value == '')
		{
			// Check that the other text display doesn't already have a string error
			if (classname(from_elem, 'check', 'string_err') == false)
			{
				classname(from_elem, 'del', 'invalid');
			}
			classname(elem, 'del', 'invalid');
			return true;
		}
		else
		{
			if (classname(from_elem, 'check', 'string_err'))
			{
				return false;
			}

			// If end time < start time, error
			if (parseInt(elem_value, 10) < parseInt(from_value, 10))
			{
				classname(from_elem, 'add', 'invalid');
				classname(elem, 'add', 'invalid');
				return false;
			}
			// If end time > start time, no problem
			else
			{
				if (classname(from_elem, 'check', 'string_err') == false)
				{
					classname(from_elem, 'del', 'invalid');
				}

				if (classname(elem, 'check', 'string_err') == false)
				{
					classname(elem, 'del', 'invalid');
				}
				return true;
			}
		}
	}
	// Check that end time > start time if elem selected is the "to" time
	else if (classname(elem, 'check', 'shown_from_value'))
	{
		var to_elem = get(parent_div, CHILD_BY_CLASS, 'shown_to_value');
		var to_value = getTimeValue(to_elem.value);

		// If either one of them are empty, then no problems
		if (elem_value == '' || to_value == '')
		{
			if (classname(to_elem, 'check', 'string_err') == false)
			{
				classname(to_elem, 'del', 'invalid');
			}

			classname(elem, 'del', 'invalid');
			return true;
		}
		else
		{
			if (classname(to_elem, 'check', 'string_err'))
			{
				return false;
			}
			// If end time < start time, error
			if (parseInt(elem_value, 10) > parseInt(to_value, 10))
			{
				classname(to_elem, 'add', 'invalid');
				classname(elem, 'add', 'invalid');
				return false;
			}
			// If end time > start time, no problem
			else
			{
				if (classname(to_elem, 'check', 'string_err') == false)
				{
					classname(to_elem, 'del', 'invalid');
				}

				if (classname(elem, 'check', 'string_err') == false)
				{
					classname(elem, 'del', 'invalid');
				}
				return true;
			}
		}
	}
}



// will ensure that the value in an input has a $ at the top and a "," for every three digits
function makeMoneyFormat(elem)
{
	if (elem.value.length > 0 && !/[\d]+/.test(elem.value) && !/\d+\.\d+/.test(elem.value))
	{
		classname(elem, 'add', 'warn');
		return false;
	}
	else
	{
		classname(elem, 'del', 'warn');

		elem.value = elem.value.replace(/[^\d\.]*/, '');
		elem.value = elem.value.replace(/\s*\.+\s*/, '.');
		elem.value = elem.value.replace(/\s+/, '');

		var split_val = elem.value.split(/\./g);
		var dollar = split_val[0];
		var cents = split_val[1];

		if (dollar != "")
		{
			if (cents != null)
			{
				cents = cents.substring(0,2);

				if (cents.length == 1)
				{
					cents += "0";
				}
			}

			elem.value = "$" + addCurrencyCommas(dollar);

			if (cents != null && cents.length > 0)
			{
				elem.value += "." + cents;
			}
			else
			{
				elem.value += ".00";
			}
		}
	}
} // end makeMoneyFormat


// adds a commas after every third digit
function addCurrencyCommas(value)
{
	if (!/,/.test(value))
	{
		//important to use the precalculated ('original') length throughout, as it will be changing as we add commas
		var length = value.length;

		//work from right to left, adding a comma every third character (but not at start or end)
		for (var current_char = length - 3; current_char > 0; current_char -= 3)
		{
			value = value.substring(0,current_char) + "," + value.substring(current_char);
		}
	}

	return value;
} // end addCurrencyCommas

function pickList(eaid)
{
	window.open (PATH_TO_ROOT + '/public/listItemPicker.php?f_eaid='+eaid ,'itempicker','scrollbars=yes,resizable=yes,width=340,height=500,alwaysRaised=yes,left=450,top=150');
}

function selectListItem(item_id, item_name, item_code, eaid)
{
	var display_id="display_"+eaid;
	var theText=item_name;
	if(item_code)
		theText += " (" + item_code + ")";
	document.getElementById(eaid).value=item_id;
	document.getElementById(display_id).innerHTML=theText;
}

function pickPerson(eaid)
{
	window.open (PATH_TO_ROOT + '/public/peoplePicker.php?f_eaid='+eaid+'&f_admin=1' ,'peoplepicker','scrollbars=yes,resizable=yes,width=340,height=500,alwaysRaised=yes,left=450,top=100');
}

function selectPerson(p_id, p_name, eaid)
{
	var display_id="display_"+eaid;
	var theText=p_name;
	document.getElementById(eaid).value=p_id;
	document.getElementById(display_id).innerHTML=theText;
}

function signOff(form_attached_id)
{
	xone.call('evaluationXone::signOff', {'form_attached_id': form_attached_id}, 'signOffCB');
}

function signOffCB(xr)
{
	window.opener.location.reload();
	window.close();
}