/***************************************************************************
 [validation.js]
 
 Copyright (C) 2004 Next IT Corporation, Inc. Spokane, WA. All Rights Reserved. 
 This document is confidential work and intellectual property of Next IT 
 Corporation. Permission to copy, distribute or use any portion of this file 
 is prohibited without the express written consent of Next IT Corporation.

*****************************************************************************/


/*
*	CheckSurvey_OnSubmit()
*
*	- Checks that the user has entered a value for all required fields
*	- Returns true or false
*/
function CheckSurvey_OnSubmit()
{
	try
	{
	var bResult = true;  // debug, set to true to activate submission of form.
	//alert("Beginning validation");  // debug
	
	this.HideErrorSpans(); // hide all errors, then show as necessary
	
	var oInputs = document.getElementsByTagName("INPUT"); // returns all textboxes, buttons
	var oTextAreaInputs = document.getElementsByTagName("TEXTAREA");
	var oDropDownInputs = document.getElementsByTagName("SELECT"); // returns all drop down lists
	
	
	// Validate input fields
	for(var index = 0; index < oInputs.length; index++)
	{
		var sTextArray = oInputs[index].id.split('_');
		
		if(sTextArray[0] == "sv")
		{
			// this input box belongs to InSurvey
						
			if( oInputs[index].type == "text" && oInputs[index].className == "REQ" )
			{
				// validate text boxes
				oInputs[index].value = this.TrimString(oInputs[index].value); // trim
			
				if(oInputs[index].value.length < 1)
				{
					// show error span for this question
					var sSpanID = "sv_ErrorSpan:" + oInputs[index].id;
					var oErrorSpan = document.getElementById(sSpanID);
					if(oErrorSpan != null)
					{
						oErrorSpan.style.display = "inline";
						bResult = false;
					}
				}
			} // end if input is a required textbox
			
			
			if(oInputs[index].type == "checkbox" && oInputs[index].className == "REQ")
			{
				// validate checkboxes
				
				// loop through all checkboxes in this group and verify that
				// at least one has been selected by the user
				if(oInputs[index].checked == false)
				{
					var bIsChecked = false;
					for(var j = 0; j < oInputs.length; j++)
					{
						if(j != index && oInputs[index].name == oInputs[j].name && oInputs[j].checked == true)
						{
							bIsChecked = true;
						}
					} // end for
					
					if(!bIsChecked)
					{
						// show error span for this question
						var tempArray = oInputs[index].name.split(':');
						// name = sv_chkgroup:QuestionPK
						
						var sSpanID = "sv_ErrorSpan:sv_chk_" + tempArray[1];
						var oErrorSpan = document.getElementById(sSpanID);
						if(oErrorSpan != null)
						{
							oErrorSpan.style.display = "inline";
							bResult = false;
						}
					}
				}
				
			} // end if input is a required checkbox
			
			
			if(oInputs[index].type == "checkbox")
			{
				// if this is an other checkbox, verify that text has been entered
				if(sTextArray[1] == "chkOther")
				{
					var tempArray = oInputs[index].name.split(':');
					// name = sv_chkgroup:QuestionPK
					
					// find corresponding textbox
					var sOtherBoxID = "sv_othr_" + tempArray[1];
					var txtOtherBox = document.getElementById(sOtherBoxID);
					
					
					if(oInputs[index].checked == true)
					{
						txtOtherBox.value = this.TrimString(txtOtherBox.value);
						if(txtOtherBox.value == "")
						{
							// show error span for this question
							var sSpanID = "sv_ErrorSpan:sv_chk_" + tempArray[1];
							var oErrorSpan = document.getElementById(sSpanID);
							if(oErrorSpan != null)
							{
								oErrorSpan.style.display = "inline";
								bResult = false;
							}
						}
					}
					else
					{
						// clear other textbox
						txtOtherBox.value = "";
					}
				} // end if other checkbox
			} // end if this is a checkbox
			
			
			if(oInputs[index].type == "radio" && oInputs[index].className == "REQ")
			{
				// validate radio buttons
				
				// loop through all radio buttons in this group and verify that
				// at one has been selected by the user
				if(oInputs[index].checked == false)
				{
					var bIsChecked = false;
					for(var j = 0; j < oInputs.length; j++)
					{
						if(j != index && oInputs[index].name == oInputs[j].name && oInputs[j].checked == true)
						{
							bIsChecked = true;
						}
					} // end for
					
					if(!bIsChecked)
					{
						// show error span for this question
						var tempArray = oInputs[index].name.split(':');
						// name = sv_chkgroup:QuestionPK
						
						var sSpanID = "sv_ErrorSpan:sv_rad_" + tempArray[1];
						var oErrorSpan = document.getElementById(sSpanID);
						if(oErrorSpan != null)
						{
							oErrorSpan.style.display = "inline";
							bResult = false;
						}
					}
				}
				
			} // end if input is a required radio button
			
			
			if(oInputs[index].type == "radio")
			{
			// if this is an other radio button, verify that text has been entered
				if(sTextArray[1] == "radOther")
				{
					var tempArray = oInputs[index].name.split(':');
					// name = sv_chkgroup:QuestionPK
					
					// find corresponding textbox
					var sOtherBoxID = "sv_othr_" + tempArray[1];
					var txtOtherBox = document.getElementById(sOtherBoxID);
					
					
					if(oInputs[index].checked == true)
					{
						txtOtherBox.value = this.TrimString(txtOtherBox.value);
						if(txtOtherBox.value == "")
						{
							// show error span for this question
							var sSpanID = "sv_ErrorSpan:sv_rad_" + tempArray[1];
							var oErrorSpan = document.getElementById(sSpanID);
							if(oErrorSpan != null)
							{
								oErrorSpan.style.display = "inline";
								bResult = false;
							}
						}
					}
					else
					{
						// clear other textbox
						txtOtherBox.value = "";
					}
				} // end if other textbox
			} // end if radio button
			
		
		} // end if this input box belongs to InSurvey
		
	} // end for each input element
	
	
	
	// validate each text area
	for(var index = 0; index < oTextAreaInputs.length; index++)
	{
		var sTextArray = oTextAreaInputs[index].id.split('_');
		
		if(sTextArray[0] == "sv")
		{
			// this input box belongs to InSurvey
			
			
			if(oTextAreaInputs[index].className == "REQ" )
			{
				// validate text boxes
				oTextAreaInputs[index].value = this.TrimString(oTextAreaInputs[index].value); // trim
			
				if(oTextAreaInputs[index].value.length < 1)
				{
					// show error span for this question
					var sSpanID = "sv_ErrorSpan:" + oTextAreaInputs[index].id;
					var oErrorSpan = document.getElementById(sSpanID);
					if(oErrorSpan != null)
					{
						oErrorSpan.style.display = "inline";
						bResult = false;
					}
				}
			} // end if input is a required textbox
		} // end if this is an InSurvey element
	} // end for each textarea
	
	
	
	// validate drop down lists
	for(var index = 0; index < oDropDownInputs.length;  index++)
	{
		var sTextArray = oDropDownInputs[index].id.split('_');
		
		if(sTextArray[0] == "sv")
		{
			// this input box belongs to InSurvey
			
			if(oDropDownInputs[index].className == "REQ" )
			{
				if(oDropDownInputs[index].selectedIndex < 1)
				{
					// user has not made a selection
					// show error span for this question
					var sSpanID = "sv_ErrorSpan:" + oDropDownInputs[index].id;
					var oErrorSpan = document.getElementById(sSpanID);
					if(oErrorSpan != null)
					{
						oErrorSpan.style.display = "inline";
						bResult = false;
					}
				}
			
			} // end if required
			
		} // end if this drop down belongs to InSurvey
	
	} // end for each drop down
	
	
	}
	catch(exception)
	{
		if(exception.message == null)
		{
			alert("Exception: " + exception.message);
		}
		else
		{
			alert("Exception: " + exception.description);
		}
		
		return false;
	}
	
	if(bResult == false)
	{
		alert("You must answer all required questions. [* = Required]");
	}
	
	return bResult; // if we do a postback
} // end CheckForm()



function HideErrorSpans()
{
	try
	{
		var oErrorSpans = document.getElementsByTagName("SPAN");
		
		for(var i = 0; i < oErrorSpans.length; i++)
		{
			// check that span is for InSurvey
			if(oErrorSpans[i].id != null && oErrorSpans[i].id.length >= 13 && oErrorSpans[i].id.substring(0, 13) == "sv_ErrorSpan:")
			{
				// hide this span
				oErrorSpans[i].style.display = "none";
			}
		} // end for each span
	}
	catch(exception)
	{
		if(exception.message == null)
		{
			alert("Exception: " + exception.message);
		}
		else
		{
			alert("Exception: " + exception.description);
		}
		
		return s;
	}
} // end HideErrorSpans()




/*
*	TrimString(string s)
*
*	- Trims leading and trailing whitespace from the string
*/
function TrimString(s)
{
	try
	{
		
		if(s.length == 0)
		{
			return s;
		}
		
		// trim leading whitespace
		while(s.charAt(0) == ' ' && s.length > 0)
		{
			s = s.substring(1, s.length);
		}
		
		if(s.length == 0)
		{
			return s;
		}
		
		// trim trailing whitespace
		while(s.length > 0 && s.charAt(s.length - 1) == ' ')
		{
			s = s.substring(0, s.length - 1);
		}
		
	}
	catch(exception)
	{
		if(exception.message == null)
		{
			alert("Exception: " + exception.message);
		}
		else
		{
			alert("Exception: " + exception.description);
		}
		
		return s;
	}
	
	return s;

} // end TrimString()
