/*
 * Copyright (c) 1997-2009, Ron Lussier. All rights reserved.
 *
 * This software is an unpublished work subject to a confidentiality agreement
 * and protected by copyright and trade secret law. Unauthorized copying,
 * redistribution or other use of this work is prohibited. All copies must
 * retain this copyright notice. Any use or exploitation of this work without
 * authorization could subject the perpetrator to criminal and civil liability.
 *
 * The above copyright notice does not indicate actual or intended publication
 * of this source code.
 */

/*---------------------------------------------------------------------------------------------------------
 * This file contains the code used to add coaching to text fields, allowing users to be cheered on
 * as they give us all of their information.  Go team!
 *---------------------------------------------------------------------------------------------------------*/

if (typeof EB == "undefined") {
	/**
	 * The EARTHENBERM global namespace object.
	 */
	var EB = {};
	}

if (typeof EB.coach == "undefined") {
	/**
	 * The EARTHENBERM coaching namespace object.
	 */
	EB.coach = {};
	}

/**
 * This field will set the contents of a specified block-level element (a DIV, SPAN, or P) to a user-prompting
 * message based on a table of messages as well as how much information has been typed into the field.
 *
 * The text to be displayed comes from a 'cheers table'.  This is a JavaScript data structure containing
 * various parameters as well as cheers for each of your input fields.
 *
 * The parameters supported within the cheers table are as follows:
 *
 *		$defaultColor:	The color to use for text when the field has focus and the the field
 *						constraints have not yet been met.  This should be a valid HTML color string.
 *		$errorColor: 	The color to use for messages when the number items in the field is invalid and
 *						does not have focus.  This should be a valid HTML color string.
 *		$validColor: 	The color to use for messages when the number items in the field is correct.
 *						This should be a valid HTML color string.
 *		$cheerPrefix:	A string to add to the beginning of any cheer message.
 *		$cheerSuffix:	A string to add to the end of any cheer message.
 *
 * Each cheer in the table is labeled with a key, usually the ID of the input field to which it applies.
 * Within a 'cheer', there can be fields labeled 'tooFew', 'tooMany', and 'messages'. The first two fields 
 * contain the text to be displayed if there is too little or too much text in the field, respectively.
 *
 * The 'messages' field is an array of arrays.  Each array contains two elements.  The first is the bottom
 * threshold for displaying that message, and should be an integer.  The second field is the message to be
 * displayed if that threshold is reached.  Messages should be in increasing order of threshold, i.e.,
 * the largest threshold should be last.
 *
 * Here is a sample cheers declaration:
 * 
 *	var cheers =
 *		{
 *			$defaultColor: '#000000',
 *			$errorColor: '#B00000',
 *			$validColor: '#00B000',
 *			$cheerPrefix: '(',
 *			$cheerSuffix: ')',
 *			answer100:
 *				{
 *					tooFew: "Can you tell us more that you like about working here?",
 *					messages:
 *						[
 *							[5,  "Keep typing!  We can't wait to hear what you have to say!"],
 *							[15, "This is great!"],
 *							[20, "Keep going, you're on a roll!"],
 *							[50, "Wow, you really love this company!"],
 *							[100,"Amazing!"]
 *						]
 *				},
 *			answer101:
 *				{
 *					tooFew: "We need you to give us some drawbacks to this employer.",
 *					messages:
 *						[
 *							[5, "What would make this company a better place to work?"],
 *							[15, "Is there anything else that would improve this employer?"],
 *							[20, "Are there any other downsides?"],
 *							[100,"It seems your experience with this Employer wasn't too good."],
 *							[200,"I feel sad."]
 *						]
 *				}
 *		}
 *
 * @param cheerField	The element (or element ID) into which the user-prompting text should be placed.
 * @param cheers		The 'cheers' table.  See the documentation for this table above.
 * @param key			The key of the cheer to be displayed.  This is (by convention) the field ID of the 
 *						input field where the user is typing.
 * @param count	The number of items (words, chars, etc) in the field.
 * @param min			The minimum number of items required in the field.
 * @param max			The maximum number of items required in the field.
 * @param hasFocus		This optional parameter indicates whether the field has focus, and defaults to 'true'.
 *						If this field is 'false', then no cheers will be displayed.  Rather, the field will
 *						simply be validated.  If there are errors, then either the 'tooMany' or 'tooFew'
 *						message will be displayed.  Otherwise the cheerField will be set to a blank message.
 */
EB.coach.cheer = function( cheerField, cheers, key, count, min, max, hasFocus ) {
	cheerField = id( cheerField );

	if (typeof hasFocus != 'boolean') {
		hasFocus = true;
	}

	var		myCheer = cheers[key];
	
	if (typeof myCheer != 'object') {
		// Nothing to do!
		return;
	}

	var		cheerPrefix = cheers.$cheerPrefix;
	var		cheerSuffix = cheers.$cheerSuffix;

	if (typeof cheerPrefix != 'string') {
		cheerPrefix = '';
	}

	if (typeof cheerSuffix != 'string') {
		cheerSuffix = '';
	}

	if (hasFocus) {
		var		messages = myCheer.messages;
	
		if (typeof messages != 'object') {
			// No messages!
			return;
		}
	
		var		cheered = false;
	
		for (var i = (messages.length - 1); i >= 0; i--) {
			var		thisMessage = messages[i];
			var		threshold = thisMessage[0];
	
			if (count >= threshold) {
				cheerField.innerHTML = cheerPrefix + thisMessage[1] + cheerSuffix;
				cheered = true;
				break;
			}
		}
	
		if (!cheered) {
			// No cheer for you!
			cheerField.innerHTML = '';
		}
	}
	else {
		var		msg = '';

		if (count < min) {
			if (typeof myCheer.tooFew == 'string') {
				msg = cheerPrefix + myCheer.tooFew + cheerSuffix;
			}
		}
		else if ((max > 0) && (count > max)) {
			if (typeof myCheer.tooMany == 'string') {
				msg = cheerPrefix + myCheer.tooMany + cheerSuffix;
			}
		}

		cheerField.innerHTML = msg;
	}

	/*
	 * Set the color for the cheer.  Note that we want to set the color even if the cheer is empty,
	 * because this color may be (and is) copied to other related fields.
	 */
	var		valid = true;
	var		defaultColor = cheers.$defaultColor;
	var		errorColor = cheers.$errorColor;
	var		validColor = cheers.$validColor;
	
	if (count < min) {
		valid = false;
	}
	else if ((max > 0) && (count > max)) {
		valid = false;
	}
	
	if (hasFocus) {
		if (valid) {
			if (typeof cheers.$validColor == 'string') {
				cheerField.style.color = cheers.$validColor;
			}
		}
		else {
			if (typeof cheers.$defaultColor == 'string') {
				cheerField.style.color = cheers.$defaultColor;
			}
		}
	}
	else {
		if (valid) {
			if (typeof cheers.$validColor == 'string') {
				cheerField.style.color = cheers.$validColor;
			}
		}
		else {
			if (typeof cheers.$errorColor == 'string') {
				cheerField.style.color = cheers.$errorColor;
			}
		}
	}
}
