/****************************************************************************************
* PURPOSE: form function library
*	setInitialFocus(form, field): puts focus on particular input element
*	selectText(element): selects the text of an input element
*	clearForm(form): clears form values except button labels
*	disableField(field): disables a form field
*	limitTextAreaLen(field, number of characters): limits a textarea to number of characters
****************************************************************************************/

/****************************************************************************************
* setInitialFocus: set the focus to the first form field
* 'form' is the form in the form array, usually 0 or 1.  'field' is the field in the
* elements array, usually 0.
*
* USAGE: generally set session("onLoad") for any fuseaction to call this function,
* if the element is null focus will gracefully fail.  Just a hint, put hidden fields at
* the end of your forms and then you can pretty much set the field to "0" without any 
* issues.
****************************************************************************************/
function setInitialFocus(form, field) {
	if (document.forms[form] != null) {
		var element = document.forms[form].elements[field];
		if (element != null) element.focus();
	}
}

/****************************************************************************************
* selectText: selects the text in an input field, pass it an element reference.  Mostly
* for Netscape since IE does this automatically.
****************************************************************************************/
function selectText(element) {
	element.select();
	element.focus();
}

/****************************************************************************************
* clearForm: clears each element value in a form except buttons, pass it a form 
* reference. onClick='javascript:clearForm(this.form);' usually does the trick.
****************************************************************************************/
function clearForm(form) {
	var elementCollection = form.elements;
	for(i = 0; i < elementCollection.length - 1; i++) 
		if (elementCollection[i].type != "submit" && elementCollection[i].type != "button")
			elementCollection[i].value = "";
}

/****************************************************************************************
* disableField: disables a form field
****************************************************************************************/
function disableField(field) {
		field.disabled = TRUE;
		return;
}

/****************************************************************************************
* limitText: adds maxlength functionality to a text area
****************************************************************************************/
function limitTextAreaLen(fld, maxChars) {
	var result = true;
	if (fld.value.length >= maxChars)
		result = false;
		
	if (window.event)
		window.event.returnValue = result;
		
	return result;
}

/****************************************************************************************
* searchStartDateBeforeEndDate: ensure the start date comes before the end date
****************************************************************************************/
function searchStartDateBeforeEndDate(startDate, endDate) {
	var startYear = right(startDate.value, 2);
	var endYear = right(endDate.value, 2);
	
	if (left(startYear, 1) <= '8') 
		startYear = '20' + startYear;
	else
		startYear = '19' + startYear;
	
	if (left(endYear, 1) <= '8') 
		endYear = '20' + endYear;
	else
		endYear = '19' + endYear;

	var _startDate = new Date(new Number(startYear), new Number(left(startDate.value, 2)) - 1, 1);
	var _endDate = new Date(new Number(endYear), new Number(left(endDate.value, 2)) - 1, 28);

    if (_startDate > _endDate) {
        return false;
    }
    
    return true;
}

