

/*
 * This function monitors an input element, and keeps track of
 * the number of characters remaining. A SPAN/DIV element must
 * already be defined for the counter. Providing this element
 * in the position you want the counter is the only scripting
 * you need to do on the page, aside from calling this function
 * to register it. The onMouseUp handler is added to the input 
 * field automatically in this function.
 *
 * @param	Object	The object to monitor the characters for
 * @param	Object	The SPAN or DIV element to display remaining
 *					characters in
 * @param	Number	The maximum number of characters allowed
 * @return	NULL
 */
function createCharacterCounter( oMonitor, oCounter, nMax )
{
	if( !isValidObject(oMonitor) || !isValidObject(oCounter)  )
		return null;
	
	if( oMonitor.value.length > nMax )
	{
		oCounter.style.color = '#ff0000';
		if( !oCounter.style.fontSize )
			oCounter.style.fontSize = '7pt';
		oCounter.innerHTML = 'Limit exceeded by ' + (oMonitor.value.length - nMax) + ' characters';
	}	
	else
	{
		oCounter.style.color = '#000000';
		if( !oCounter.style.fontSize )
			oCounter.style.fontSize = '7pt';
		oCounter.innerHTML = ( nMax - oMonitor.value.length ) + ' characters remaining '; 	
	}
			
	oMonitor.setAttribute( "onKeyUp", "updateCharacterCounter( getObj('" + oCounter.getAttribute("id") + "'), ("+ nMax + " - this.value.length) );" );
}


/* 
 * Updates the a character counter to a new value for an input field.  This should only
 * be called when used by the createCharacterCounter function, and is added to the onKeyUp
 * event of a monitored input field
 *
 * @param	Object	The SPAN/DIV element that the characters are being displayed within
 * @param	Number	The number of characters remaining
 * @return	null;
 */
function updateCharacterCounter( oCounter, nRemaining )
{
	if( nRemaining < 0 )
	{
		oCounter.style.color = '#ff0000';
		oCounter.innerHTML = 'Limit exceeded by ' + (nRemaining * -1) + ' characters';
	}
	else
	{
		oCounter.style.color = '#000000';
		oCounter.innerHTML = nRemaining + ' characters remaining '; 	
	}

}



/*
 * Copies the OPTIONS from one select list to another.  By default, the selectedIndex for
 * the target object will be the same as for the source, unless a value or index are provided
 * to select. This can also be overridden with the 'bIgnoreSourceIndex' parameter
 *
 * @param	Object;	The source SELECT object to copy the options from
 * @param	Object;	The target SELECT object to copy the options into
 * @param	Number;	The index to select in the target list. Can be null
 * @param	Mixed;	The value of the option to select in the target list Can be null
 * @param	String;	The text of the option to select. Can be null
 * @param	Bool;	If set to TRUE, the function will ignore the selected index of the
 *					source element, even if no other value to select is provided
 * @return	NULL;
 */ 
function copySelectOptions( oSource, oTarget, nSelectedIndex, mSelectedValue, sSelectedText, bIgnoreSourceIndex )
{
	// Make sure they are valid objects first
	if( !isValidObject(oSource) || !isValidObject(oTarget) )
		return null;
		
	// Make sure they are select elemets
	if( oSource.type != 'select-one' || oTarget.type != 'select-one' )
		return null;
	
	// Reset the options in the target
	//oTarget.options.length = 0;
	
	if( nSelectedIndex  )
		selectedIndex = nSelectedIndex;
	else if ( !bIgnoreSourceIndex )
		selectedIndex = oSource.selectedIndex;
	
	for( i = 0; i < oSource.options.length; i++ )
	{
		// If a select value was provided, and this option is the right one, mark the index
		if( !nSelectedIndex && mSelectedValue != null && mSelectedValue == oSource.options[i].value )
			selectedIndex = i;
		else if ( !nSelectedIndex && sSelectedText != null && sSelectedText == oSource.options[i].text )
			selectedIndex = i;

		oTarget.options[i] = new Option( oSource.options[i].text, oSource.options[i].value );
	}
	
	oTarget.selectedIndex = selectedIndex;	
}



/*
 * Adds a new OPTION element to a select object
 *
 * @param	Object;	The object we are adding the option to
 * @param	String;	The value for this option
 * @param	String;	The text to display for the option
 * @return	null;
 */
function addOption( oSelect, sValue, sText )
{
	listSize = oSelect.options.length;
	newOption = new Option( sText, sValue );
	
	oSelect.options[listSize] = newOption;
}


/*
 * Determines whether or not a string is in a valid email address format
 *
 * @return	Bool;	Returns true if the string is in a valid email format,
 *					else false
 */
function emailIsValid( sEmail )
{
	// It must be at least 5 characters (@ . and 3 characters)
	if( sEmail.length < 5 )
		return false;
		
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	
	if (filter.test(sEmail))
		return true;
	else
		return false;	
}


/*
 * Sets the form focus on the input element passed in and moves the cursor to the end of the value in the field
 *
 * @param	Object	The form element object to focus on
 * @return 	void
 */
function focusInputField( oElement )
{
	if( !isValidObject(oElement) )
		return null;
		
	oElement.focus();
	oElement.value = oElement.value;
}


function submitFormOnEnter( oEvent, oSubmitForm )
{
	if(window.event && window.event.keyCode == 13 ) 
		oSubmitForm.submit();
	else if( !window.event && oEvent && oEvent.which == 13 ) 
		oSubmitForm.submit();
}




