/********************************************************************************
/********************************************************************************
/********************************************************************************
/*	Filename: mocc_lib_form.js																		
/*	current version: 1.3
/*	date: 2008-11-08
/*	Description: provides methods to manipulate/read form elements
/*																	
/*																											
/*	Dependencies: mocc_lib_string.js
/*				  mocc_lib_elems.js
/*	Contributors (chronological order, starting with originating author):									
/*					1. Cliff Chambers (CC)		
/*					2. Matt Olinger (MO)
/*																											
/************************************************************************************
/*	version history:													
/************************************************************************************
/*	Version 1, created 2007-09-12 (CC)					
/*	Version 1.1, added getRadioVal 2008-02-09 (MO)
/*	Version 1.2, added autoTab and getElementIndex 2008-09-07 (MO)
/*	Version 1.3, added removeListItems 2008-11-08 (MO)
/********************************************************************************
/********************************************************************************
/********************************************************************************


/********************************************************************************
/*	FUNCTION checkGroup																					
/*	Parameters: listx = form/checkbox object					
/*	delim = optional parameter, delimiter choice that will separate values on return
/*																											
/*	Returns: all values of like-named checkboxes for specific forms separated by chosen delimiter 						
/*			unless chosen delimiter is a space											
/*	Dependencies: method escapestr, method trim														
/*																											
/*********************************************************************************/

function checkGroup(listx,delim) {
	if (delim==undefined || delim.trim()=='') delim='#'; //checks to see if requested delimiter was not sent or is a space, if so replaces with default 
	var delx = delim.escapestr(); // escapes delimiter
	var messix="";
	for(i=0; i < listx.length; i++) {
		if(listx[i].checked==true) { //if checked aadds value to string with delimiter
			messix+=listx[i].value + delx;
		}
	}
	var messix=messix.substring(0, messix.length-1); //strips last character, as its is an extra delimiter
	return messix;
}


/********************************************************************************
/*	FUNCTION noenter																					
/*	Parameters: none					
/*																											
/*	Returns: disables enter key on form input field so that you must click to submit 						
/*	Dependencies: none														
/*																											
/*********************************************************************************/


function noenter() {
  return !(window.event && window.event.keyCode == 13); 
}

/********************************************************************************
/*	FUNCTION ismaxlength																					
/*	Parameters: obj=field					
/*																											
/*	Returns: enforces maxlength attribute on form fields (ex: textarea)						
/*	Dependencies: none														
/*																											
/*********************************************************************************/
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);
}

/********************************************************************************
/*	FUNCTION maxlen
/*	Parameters: e=field
/*					len=max length
/*					msg=boolean (true = show message)
/*					fieldName=name of e, only used in message if shown
/*																											
/*	Returns: nothing
/*	Dependencies: none														
/*																											
/*********************************************************************************/
function maxlen(e,len,msg,fieldName) {
	if (e.value.length>len) {
		e.value=e.value.substr(0,len);
		if (msg) window.alert('The ' + fieldName + ' field cannot contain more than ' + len + ' characters.\nThe input has been truncated for you.');
	}
}

/********************************************************************************
/*	FUNCTION getRadioVal
/*	Parameters: f=the radio button object (document.form.radioname)
/*																											
/*	Returns: the value of the selected radio button
/*	Dependencies: none														
/*																											
/*********************************************************************************/
function getRadioVal(f) {
	if (f.length!=undefined) {
		for (var i=0;i<f.length;i++) if (f[i].checked) return f[i].value;
	} else {
		return f.value;
	}
}

/********************************************************************************
/*	FUNCTION clearRadioGroup
/*	Parameters: f=the radio button (or checkbox) object (document.form.radioname)
/*																											
/*	Returns: none
/*	Dependencies: none														
/*																											
/*********************************************************************************/
function clearRadioGroup(f) {
	if (f.length!=undefined) {
		for (var i=0;i<f.length;i++) f[i].checked=false;
	}
}

/********************************************************************************
/*	FUNCTION autoTab
/*	when the input length of the current field reaches the max length, tab to the
/*	next field automatically
/*
/*	Parameters: ob=the object in the form that is currently active
/*					max=the max length of the string that goes into the current field
/*																											
/*	Returns: nothing
/*	Dependencies: getElementIndex (in this file also)
/*																											
/*********************************************************************************/
// ===================================================================
// getElementIndex and autoTab written by
// Author: Matt Kruse <matt@mattkruse.com>
// WWW: http://www.mattkruse.com/
//
// minor modifications have been made
// code inserted with written permission from the author

function getElementIndex(ob) {
	var theform = ob.form;
	for (var i=0; i<theform.elements.length; i++) {
		if (ob.name == theform.elements[i].name) {
			return i;
			}
		}
	return -1;
}

function autoTab(ob, max) {
	if (ob.value.length<max) return 1;
	if (navigator.platform.toUpperCase().indexOf("SUNOS") != -1) {
		ob.blur(); return; // Sun's onFocus() is messed up
		}
	var theform = ob.form;
	var i = getElementIndex(ob);
	var j=i+1;
	if (j >= theform.elements.length) { j=0; }
	if (i == -1) { return; }
	while (j != i) {
		if ((theform.elements[j].type!="hidden") && 
		    (theform.elements[j].name != theform.elements[i].name) && 
			(!theform.elements[j].disabled)) {
			theform.elements[j].focus();
			break;
			}
		j++;
		if (j >= theform.elements.length) { j=0; }
		}
}

/********************************************************************************
/*	FUNCTION removeListItems
/*	clears list items from a list whose indices are between (and including) low and high
/*																											
/*	Parameters: f=field
/*					low=first (lowest) index to be removed, 0 if not specified
/*					high=last (highest) index to be removed, highest index if not specified
/*																											
/*	Returns: nothing
/*	Dependencies: none														
/*																											
/*********************************************************************************/
function removeListItems(f,low,high) {
	if (high==undefined) high=f.options.length;
	else if (high>f.options.length-1) { high=f.options.length-1; }
	
	if (low==undefined) low=0;
	else if (low<0) low=0;
	else if (low>high) low=high;

	for (var i=high-1; i>=low; i--){
		f.options[i] = null;
	}
}

/********************************************************************************
/*	FUNCTION setCharsLeft
/*	sets an element's text to begString + <chars left> + endString
/*	  <chars left> is determined by the number of characters in the text field
/*	    and the maxlength of that text field
/*																											
/*	Parameters: divID = ID of the div/element that will contain the final text
/*		    txtEL = ID of the element that is checked for length
/*		    begString = prepended to the result string
/*		    endString = appended to the result string
/*																											
/*	Returns: nothing
/*	Dependencies: none														
/*																											
/*********************************************************************************/
function setCharsLeft(divID,txtEl,begString,endString) {
	var mlength=txtEl.getAttribute? parseInt(txtEl.getAttribute("maxlength")) : -1;
	if (mlength>=0) {
		setDiv(divID,begString+" "+(mlength-txtEl.value.length)+" "+endString);
	}
}

/********************************************************************************
/*	FUNCTION setRadioVal
/*	sets a radio groups selected option based on parameter val
/*																											
/*	Parameters: f = the radio button object
/*		    val = the value of the radio button that should be selected
/*																											
/*	Returns: nothing
/*	Dependencies: none														
/*																											
/*********************************************************************************/
function setRadioVal(f,val) {
	if (f.length!=undefined) {
		for (var i=0;i<f.length;i++) {
			if (f[i].value==val) {
				f[i].checked=true;
				break;
			} else {
				f[i].checked=false;
			}
		}
	}
}

