//gets an element based on an ID
<!--
function getElementById(id)
{	
	if(document.getElementById)
		return document.getElementById(id);
	else if(document.all)
	{
		return document.all[id];
	}
	else
	{									
		if(document.layers[id])
			return document.layers[id];

		if(document.images[id])
			return document.images[id];

		for(var i=0; i<document.forms.length; ++i)
		{
			if(document.forms[i].id == id)
				return document.forms[i];
		}			

		//find the corresponding link for this anchor
		for(var j=0; j<document.anchors.length; ++j)
		{
			if(document.anchors[j].name == id)
			{					
				for(var i=0; i<document.links.length; ++i)
				{									
					if(document.anchors[j].text == document.links[i].text)
						return document.links[i];
				}
			}
		}

		for(var i=0; i<document.embeds.length; ++i)
		{
			if(document.embeds[i].id == id)
				return document.embeds[i];
		}

		for(var i=0; i<document.forms.length; ++i)
		{
			if(document.forms[i].elements[id])
				return document.forms[i].elements[id];		
		}
	}
	
	return null;
}


//does not work in old netscape because old netscape sucks
function setText(elementID, text)
{
	var element = getElementById(elementID);

	if(element && element.innerHTML)
	{
		//the nice IE way
		element.innerHTML = text;
	}
}

//sets the value of a component
function setValue(elementID, value)
{
	var element = getElementById(elementID);

	if(element && element.value)
	{
		element.value = value;
	}
}

//associates a rollover image with an image tag
function setRolloverImage(imageID, rolloverImageSrc)
{
	imagetag = getElementById(imageID);
	
	var original = new Image();
	original.src = imagetag.src;
	var rollover = new Image();
	rollover.src = rolloverImageSrc;
	
	imagetag.ro_origImage = original;
	imagetag.ro_rollImage = rollover;
	
	imagetag.onmouseover = mouseOverFunc;
	imagetag.onmouseout = mouseOutFunc;
}

//sets the message to be displayed in the status bar when the mouse is over this
function setLinkStatusMessage(elementID, message)
{
	var element = getElementById(elementID);
	
	element.sm_status = message;
		
	element.onmouseover = mouseOverFunc;
	element.onmouseout = mouseOutFunc;
	
}

//this is the preferred way to do it, inline with HTML, this way is garunteed to work in netscape
//  override the onmouseover handler
function setStatusMessage(message)
{
	var element = this;
	
	element.sm_status = message;
	
	element.onmouseover = mouseOverFunc;
	element.onmouseout = mouseOutFunc;
	
	
	//do the mouseover func

	return element.onmouseover();	
}

//sets an autotab whenever a field reaches a certain amount	
function setAutoTab(fromElementID, toElementID, numChars)
{
	var from = getElementById(fromElementID);
	
	if(from == null)
		return;
	
	//set some custom properties for the autotab
	from.at_toElement = getElementById(toElementID);
	from.at_numChars = numChars;
			
	from.onkeyup = keyupFunc;
}

//possible values are: number|integer|date
//	dateformat is an optional string that sets the date format
function setDataType(elementID, datatype, dateformat)
{
	var element = getElementById(elementID);
	if(element == null)
		return;
	
	element.dt_datatype = datatype;
	if(dateformat)
		element.dt_date_format = dateformat;
	else
		element.dt_date_format = "mm/dd/yyyy";
	
	element.onkeyup = keyupFunc;
	element.onchange = changeFunc;
	element.onblur = blurFunc;
}

function mouseOverFunc()
{
	if(this.src && this.ro_rollImage)
		this.src = this.ro_rollImage.src;
	
	if(this.sm_status)
	{
		self.defaultStatus="";
		self.status = this.sm_status;
	}
	
	return true;
}

function mouseOutFunc()
{	
	if(this.src && this.ro_origImage)
		this.src = this.ro_origImage.src;	
		
	if(this.sm_status)
		self.status = "";
		
	return true;
}		

function changeFunc()
{		
	var OLD_NETSCAPE = navigator.appName.indexOf("Netscape") != -1 && navigator.appVersion.indexOf("4.") != -1;

	if(this.dt_datatype && this.dt_datatype == "date")
	{
		formatDate(this);			
	}

	//padding
	if(this.pad_type)
	{
		pad(this, this.pad_length, this.pad_char, this.pad_type);			
	}
	
	return true;
}	

function blurFunc()
{
	//make sure we don't leave this control if our datatype is invalid
	if(this.dt_datatype)
	{
		if(this.dt_datatype == "integer")
		{
			if(this.value.match(/\D+/))
			{
				this.value = "";
				this.focus();
			}
		}
		else if(this.dt_datatype == "number")
		{
			var match = this.value.match(/[^0-9.]/g);
			var dindex = this.value.indexOf(".");
			if(!match && dindex != -1)
			{
				//do a second test, ensuring that there is only 1 decimal point
				if(this.value.indexOf(".", dindex+1) != -1)
				{
					match = true;
				}
			}
			
			if(match)
			{					
				this.value = "";
				this.focus();
			}
			
		}
	}
}

function keyupFunc()
{
	var OLD_NETSCAPE = navigator.appName.indexOf("Netscape") != -1 && navigator.appVersion.indexOf("4.") != -1;
	//check datatype verification
	if(!OLD_NETSCAPE && this.dt_datatype && this.dt_datatype != "date")
	{		
		var c = this.value.charAt(this.value.length-1);
		if(this.dt_datatype == "number")
		{				
			if((c < "0" || c > "9") &&  c != ".")
			{
				this.value = (this.value.length == 0 ? "" : this.value.substring(0, this.value.indexOf(c)));
				return true;
			}
		}
		else if(this.dt_datatype == "integer")
		{				
			if(c < "0" || c > "9")
			{
				this.value = (this.value.length == 0 ? "" : this.value.substring(0, this.value.indexOf(c)));
				return true;
			}
		}
		
	}		
	
	//check autotab
	if(this.at_numChars && this.value.length >= this.at_numChars)
		this.at_toElement.focus();
	
	return true;
}



var MONTHS = new Array();
	MONTHS[0] = "January"; 
	MONTHS[1] = "February"; 
	MONTHS[2] = "March"; 
	MONTHS[3] = "April"; 
	MONTHS[4] = "May"; 
	MONTHS[5] = "June"; 
	MONTHS[6] = "July"; 
	MONTHS[7] = "August"; 
	MONTHS[8] = "September"; 
	MONTHS[9] = "October"; 
	MONTHS[10] = "November"; 
	MONTHS[11] = "December";
	
var SEPARATORS = new Array();
	SEPARATORS[0] = "\\"; 
	SEPARATORS[1] = "."; 
	SEPARATORS[2] = "/"; 
	SEPARATORS[3] = "-";	//possible separators
	
//	var DATE_FORMAT = "mm/dd/yyyy";
	//possible formats
	// mm 		= two digit month
	// m 		= one digit month (or 2 if needed)
	// MONTH 	= name of the month (i.e. March)
	// MON		= abbreviation of the month, using first 3 letters (i.e. Mar)
	// dd 		= two digit day
	// d 		= one digit day (or 2 if needed)
	// yy 		= two-digit year
	// yyyy 	= four-digit year
	

//makes the value of the given element equal to the properly formatted date
function changeToDateValue(element, month, day, year)
{
	var s = element.dt_date_format;
	//just do a series of replaces
	s = s.replace("mm", month < 10 ? "0" + month : month);
	s = s.replace("m", month);		
	s = s.replace("dd", day < 10 ? "0" + day : day);
	s = s.replace("d", day);
	
	//four digit year
	var myyear = new String(year);
	//this function will prepend a "20" to the date if the year is before 20, and a "19" if it is greater than or equal to 20
	var century = ((year % 100) < 20 ? "20" : "19");
	
	if(year < 1000)
	{
		//add this year's first 2 digits to the year's last 2 digits
		myyear = century + myyear.charAt(myyear.length-2) + myyear.charAt(myyear.length-1);	
	}		
	s = s.replace("yyyy", myyear);	//4-digit year
	
	//2-digit year
	s = s.replace("yy", year % 100);
	
	//full month names
	s = s.replace("MONTH", MONTHS[month-1]);
	s = s.replace("MON", getAbbreviation(MONTHS[month-1]));
	
	element.value = s;
}

function getAbbreviation(month)
{
	return month.substring(0, 3);
}

function containsSeparator(value)
{
	for(var i=0; i<SEPARATORS.length; ++i)
	{
		if(value.indexOf(SEPARATORS[i]) != -1)
			return true;
	}
	
	return false;
}

//returns -1 if not found
function indexOfSeparator(firstIndex, value)
{
	for(var i=firstIndex; i<value.length; ++i)
	{
		if(containsSeparator(value.charAt(i)))
			return i;
	}
	
	return -1;
}

function startsWith(base, value)
{
	return base.indexOf(value) == 0;
}

function indexOfMonth(value)
{
	for(var i=0; i<MONTHS.length; ++i)
	{
		if(MONTHS[i].toUpperCase() == value.toUpperCase())
			return i;
		if(getAbbreviation(MONTHS[i]).toUpperCase() == value.toUpperCase())
			return i;
		if(startsWith(value.toUpperCase(), (getAbbreviation(MONTHS[i]).toUpperCase())))
			return i;
	}
	
	return -1;
}

function containsMonth(value)
{
	for(var i=0; i<MONTHS.length; ++i)
	{
		if(value.indexOf(MONTHS[i]) != -1)
			return true;
		if(value.indexOf(getAbbreviation(MONTHS[i])) != -1)
			return true;
	}
	
	return false;
}


/*this method will determine if a date is in the given field and will format it accordingly to a certain format string

	The format string contains m, d, yy, mm, dd, or yyyy, or one of these: LONG, SHORT, FULL
	Example "mm/dd/yyyy"
	
	It tries to infer what the user intended to enter, if it can't, it sets the field to INVALID and selects it
*/
function formatDate(element)
{
	//first we need to figure out the month, day and year
	var month;
	var day;
	var year;
	
	var input = element.value;
	
	if(containsSeparator(input))
	{
		//ASSUMPTION num/num/num	where '/' is any generic separator
	
		//attempt to infer based on separators
		//we assume that there are 2 separators
		var values = new Array();
		
		
		var sep = indexOfSeparator(0, input);
		if(sep != -1)
		{
			values[0] = input.substring(0, sep);
			
			//get next separator
			var sep2 = indexOfSeparator(sep+1, input);
			if(sep2 != -1)
			{
				values[1] = input.substring(sep+1, sep2);
				
				if(indexOfSeparator(sep2+1, input) == -1)
				{
					//if there are no extraneous ones, get c
					values[2] = input.substring(sep2+1, input.length);
					
					//verify that everything is a number
					for(var i=0; i<values.length; ++i)
					{
						values[i] = new Number(values[i]);							
					}
					
					if(!isNaN(values[0]) && !isNaN(values[1]) && !isNaN(values[2]))
					{
						//check for 4 digit year at front or back							
						if(values[0] >= 1000)
						{
							//	m/d/yy or m/d/yyyy
							year = values[0];
							month = values[1];
							day = values[2];
						}
						else
						{	
							//	yyyy/m/d
							year = values[2];
							month = values[0];
							day = values[1];
						}							
					}
				}
			}
		}			
	}
	else if(!isNaN(new Number(input)) && input.length == 8)
	{
		//this is a single number, so it is probably the special dollar bank formatted date
		//yyyymmdd
		
		//i.e.: 20040716
		
		year = new Number(input.substring(0, 4));
		month = new Number(input.substring(4, 6));
		day = new Number(input.substring(6, 8));						
	}
	else
	{
		//attempt to infer the date from strings
		
		//get rid of any commas and periods
		input = input.replace(/\,/g, " ");
		input = input.replace(/\./g, " ");
		
		//truncate any multiple whitespace to a single spaces
		input = input.replace(/\s+/g, " ");
		
		//split string on spaces
		var split = input.split(/\s/g);							

		//first, see if we're starting with a digit
		var num = new Number(split[0]);	
		if(!isNaN(num))
		{			
			// ex:  12 March 2004
			day = num;
			month = indexOfMonth(split[1]) + 1;
			year = new Number(split[2]);
		}
		else
		{			
			// ex. March 12 2004
			month = indexOfMonth(split[0]) + 1;
			day = new Number(split[1]);
			year = new Number(split[2]);
		}
	}
	
	//verify that the values are kosher
	if(!isNaN(month) && !isNaN(day) && !isNaN(year) && month<=12 && month>0 && day<=31 && day>0 && year>0)
	{
		//all values are good, so ship off the values to the value changing function
		changeToDateValue(element, month, day, year);
		return;
	}
	
	element.value = "INVALID DATE";						
}

function setPadding(elementID, length, padChar, padType)
{

	var element = getElementById(elementID);
	if(element == null)
		return;
		
	element.pad_length = length;
	element.pad_char = padChar;
	element.pad_type = padType;			
		
	element.onchange = changeFunc;
}

//prepend is a boolean, if true, the pad is prepended
//pad_char must be a single character, we will always take character 1 from the string

function pad(element, num_digits, pad_char, pad_type)
{		
	var PAD_TYPE_PREPEND = "prepend";	//values are prepended to the input
	var PAD_TYPE_APPEND = "append";	//values are appended to the input
	var PAD_TYPE_NORMALIZE = "normalize";	//values are evenly distributed between the front and back of the input
	
	pad_char = pad_char.charAt(0);	//get the first char of pad string so we keep our length correct
	var input = new String(element.value);
	
	if(input.length == num_digits)
		return;	//we're good
	else if(input.length > num_digits)
	{
		//remove less significant digits
		input = input.substring(0, num_digits);
	}
	else
	{
		//we have less characters, so we must pad
		var pre = true;
		for(var i=input.length; i<num_digits; ++i)
		{
			if(pad_type == "prepend")
				input = pad_char + input;
			else if(pad_type == "append")
				input += pad_char;
			else if(pad_type == "normalize")
			{
				if(pre)
					input = pad_char + input;
				else
					input += pad_char;
				pre = !pre;
			}				
		}
	}
	
	element.value = input;
}

function isNetscape4x()
{
	return navigator.appName.indexOf("Netscape") != -1 
		&& navigator.appVersion.charAt(0) == "4";
}

function isIE()
{
	return navigator.appName.indexOf("Internet Explorer") != -1;
}

// 2006.03.30 C.Martin
// Sets a maxlength for a TextArea control
// Usage: <textarea maxlength="150" onKeyUp="return isMaxLength(this)">
function isMaxLength(obj)
{
	var mlength=obj.getAttribute?parseInt(obj.getAttribute("maxlength")):"";
	if(obj.getAttribute && obj.value.length>mlength)
		obj.value=obj.value.substring(0,mlength)
}

// -->

