//------------------------------------------------------------------------------------
// $Workfile: incValidation.js $
// Purpose: This file contains a client-side validation function.
//------------------------------------------------------------------------------------
// $Revision: 1.2 $
// $Date: 2009/10/30 15:26:59EDT $
// $Author: DANIEL CARTER (CK3X) $
//------------------------------------------------------------------------------------

/*
'---------------------------------------------------------------------------------------------
'Name: validateParameters
'Purpose: Validates the user's responses using client-side scripting
'Inputs: 
'	ParameterList - This is one long string in the following format:
'		Input1Name~Input1Validation1#Input1Validation1ErrorMessage*Input1Validation2#Input1Validation2ErrorMessage^Input2Name~Input2Validation1#Input2Validation1ErrorMessage
'Output: Boolean [indicating whether validation passed or failed
'Required Pre-conditions: None
'Description: See Purpose
'---------------------------------------------------------------------------------------------
*/

	function validateParameters(ParameterList)
	{	
		var sarrAllParamsDetailsArray, sarrSingleParamDetailsArray, sarrSingleParamAllValidationsArray, sarrSingleParamSingleValidationArray, i, j, k, sParamValue, m, sErrorMessage, bFoundErrorForCurrentParam, sOtherParamValue, sCurrentParam, sFirstErrorElement, sElementType;

		// initialize the error message string
		sErrorMessage = '';
		
		// initialize the first error string
		sFirstErrorElement = '';

		if (ParameterList != null && ParameterList.length > 0)
		{

			// first, split the incoming string into parts for each param
			sarrAllParamsDetailsArray = ParameterList.split("^");
		
			// loop through the main array of params
			for (i = 0; i < sarrAllParamsDetailsArray.length; i++)
			{
				// now split one param string into its parts
				sarrSingleParamDetailsArray = sarrAllParamsDetailsArray[i].split("~");
				// then, split the 3rd part of this array into parts for each validation for this particular param
				sarrSingleParamAllValidationsArray = sarrSingleParamDetailsArray[2].split("*");
				
				// set this string to be evaluated (also used later for errors)
				sCurrentParam = "document.frmQuestions." + sarrSingleParamDetailsArray[0];

				// now, get the value of this param
				sParamValue = eval(sCurrentParam + ".value");
				
				//if this param's input type is checkbox or radio, the line above returned null
				//in that case...
				if (sParamValue == null)
				{
					for(m = 0; m < eval("document.frmQuestions." + sarrSingleParamDetailsArray[0] + ".length"); m++)
					{
						// set this string to be evaluated
						sCurrentParam = "document.frmQuestions." + sarrSingleParamDetailsArray[0] + "[" + m + "]";

						if (eval(sCurrentParam + ".checked") == true)
						{
							if (sParamValue == null)
								sParamValue = eval(sCurrentParam + ".value");
							else
								sParamValue = sParamValue + "~" + eval(sCurrentParam + ".value");
						}
					}
					// set this to the first checkbox/radio for this input (used later for error case)
					sCurrentParam = "document.frmQuestions." + sarrSingleParamDetailsArray[0] + "[0]";
				}
				
				// init this bool to see whether we've found an error or not
				bFoundErrorForCurrentParam = false;

				// loop through each validation type for this param
				for (j = 0; j < sarrSingleParamAllValidationsArray.length; j++)
				{
					// split the validation string into its parts to see the details of this validation
					sarrSingleParamSingleValidationArray = sarrSingleParamAllValidationsArray[j].split("#")

					// figure out what type of validation we want to perform
					switch(sarrSingleParamSingleValidationArray[0])
					{
						// we just want to verify that there is at least some value
						case "exists":
							if (sParamValue == null)
							{
								bFoundErrorForCurrentParam = true;
								break;
							}
							else if (sParamValue.length == 0)
							{
								bFoundErrorForCurrentParam = true;
								break;
							}
							break;
						// verify that there is at least some value IF another param also has some value
						case "existsif":
							// this param may be optional, so if the user entered nothing don't run this validation
							if (sParamValue.length == 0) break;
							sOtherParamValue = eval("document.frmQuestions.Q_" + sarrSingleParamSingleValidationArray[2] + ".value");
							if (sOtherParamValue == null)
							{
								bFoundErrorForCurrentParam = true;
								break;
							}
							else if (sOtherParamValue.length == 0)
							{
								bFoundErrorForCurrentParam = true;
								break;
							}
							break;
						// note, "numeric" check actually requires it to be a positive integer
						case "numeric":
							// this param may be optional, so if the user entered nothing don't run this validation
							if (sParamValue.length == 0) break;
							// since the parseInt func will turn "33abc" into "33" and not throw
							// up an error, we need to loop through each char and make sure its
							// a valid int
							for (k = 0; k < sParamValue.length; k++)
							{
								if (isNaN(parseInt(sParamValue.charAt(k))))
								{
									bFoundErrorForCurrentParam = true;
									break;
								}
							}
							break;
						// check if the param value is between certain numbers
						case "between":
							// this param may be optional, so if the user entered nothing don't run this validation
							if (sParamValue.length == 0) break;
							if ((parseFloat(sParamValue) < parseFloat(sarrSingleParamSingleValidationArray[2])) || (parseFloat(sParamValue) > parseFloat(sarrSingleParamSingleValidationArray[3])))
								bFoundErrorForCurrentParam = true;
							break;
						// check if the param value is less than a certain number			
						case "lessthan":
							// this param may be optional, so if the user entered nothing don't run this validation
							if (sParamValue.length == 0) break;
							if (parseFloat(sParamValue) > parseFloat(sarrSingleParamSingleValidationArray[2]))
								bFoundErrorForCurrentParam = true;
							break;
						// check if the param value is greater than a certain number			
						case "greaterthan":
							// this param may be optional, so if the user entered nothing don't run this validation
							if (sParamValue.length == 0) break;
							if (parseFloat(sParamValue) < parseFloat(sarrSingleParamSingleValidationArray[2]))
								bFoundErrorForCurrentParam = true;
							break;
						// check if the param value is of a certain length
						case "length":
							// this param may be optional, so if the user entered nothing don't run this validation
							if (sParamValue.length == 0) break;
							if (sParamValue.length != parseFloat(sarrSingleParamSingleValidationArray[2]))
								bFoundErrorForCurrentParam = true;
							break;
						// This should only happen if this is a custom validation
						default:
							// if this is a custom validation, then we can ignore it (it will be handled server side)
							if (sarrSingleParamSingleValidationArray[0].substring(0, 6) != "Custom")
								alert("Unknown Validation Type: " + sarrSingleParamSingleValidationArray[0]);
					} // switch(sarrSingleParamSingleValidationArray[0])
					
					// check if we found an error for this param
					if (bFoundErrorForCurrentParam)
					{
						// if this is the first error, just set our error string equal to the current error message
						if (sErrorMessage.length == 0)
						{
							sErrorMessage = sarrSingleParamSingleValidationArray[1] + " (Question " + sarrSingleParamDetailsArray[1] + ")"
							// also, note this as the first error param, for sending focus later
							sFirstErrorElement = sCurrentParam;
						}
						// otherwise, append it to end of error string
						else
						{
							sErrorMessage = sErrorMessage + "\n" + sarrSingleParamSingleValidationArray[1]  + " (Question " + sarrSingleParamDetailsArray[1] + ")"
						}
						
						//also, let's break from inside FOR loop, since we already found an error for this particular input
						break;
					}				
				} // for (j = 0; j < sarrSingleParamAllValidationsArray.length; j++)
			} // for (i = 0; i < sarrAllParamsDetailsArray.length; i++)
		} // (ParameterList != null && ParameterList.length > 0)
			
		// Display error message is necessary and return proper boolean
		if (sErrorMessage.length > 0)
		{
			alert(sErrorMessage);
			
			sElementType = eval(sFirstErrorElement + ".type");

			// shift focus to the first element that has an error
			eval(sFirstErrorElement + ".focus()");
			
			// if it's a textbox element, then also highlight the text
			if (sElementType == 'text')
				eval(sFirstErrorElement + ".select()");			
			
			return false;
		}
		else
			return true;
	
	} // end function validateParameters
	
	
	
	
	function disableEnter()
	{
		if (event.keyCode == 13)
		{
			event.cancelBubble = true;
			event.returnValue = false;
         }
	}

