/*
*******************************************************************************************
Purpose:	Validates that a certain number (or range) of checkboxes are checked
			Optional parrTextAnswers is for "Other" type textareas to be considered
			as additional checkboxes and thus included in validation.
Arguments:	pobjCheckbox = checkbox object
			pstrMessage = opt. message
			plngMin = opt. minimum # checked
			plngMax = opt. max # checked 
			parrTextAnswers = opt. array of textarea objects
Returns:	boolean
Created: 	07/12/1999
By: 		Neal Tillotson
Supports:	JavaScript 1.1 or higher		
Usage:		Programs-specific usage:
				var arrTextAnswers = new Array()
				arrTextAnswers[0] = document.frm_Questions.mbQA_Q1111
				arrTextAnswers[1] = document.frm_Questions.mbQA_Q1111
				if (!ValidCheckbox(document.form.checkboxobject,"",3,5,arrTextAnswers)) {return false};

			General usage:
				if (!ValidCheckbox(document.form.checkboxobject,"",1,1)) {return false};
				==> will validate that EXACTLY one checkbox is checked
				if (!ValidCheckbox(document.form.checkboxobject,"",2,0)) {return false};
				==> will validate that AT LEAST two checkboxes are checked
				if (!ValidCheckbox(document.form.checkboxobject,"",0,3)) {return false};
				==> will validate that NO MORE THAN three checkboxes are checked
				if (!ValidCheckbox(document.form.checkboxobject,"",1,3)) {return false};
				==> will validate that BETWEEN one and three checkboxes are checked

Calls:		IsBlank()
Wrappers:	inc_UW_InputValidation.js

Changed:	06/26/2001 
By:	   		Neal Tillotson: modified error message per Rick's instructions

Changed:	10/02/2001 
By:	   		Neal Tillotson: added IntegerToString() function
*******************************************************************************************
*/

function ValidCheckbox(pobjCheckbox, pstrMessage, plngMin, plngMax, parrTextAnswers)
{
	var i
	var j
	var blnKeepCounting
	var strMessage
	var lngChecked
	var strAnswerText
	var lngCheckboxLength
	var strName
	var lngVer
	var blnOneCheckbox = false

    // IE3 checkbox validation must be done by the VBS include file (invoked after submission)! 
    // IE3 does not recognize document object model for checkboxes.
    strName = navigator.appName;
    lngVer = parseInt(navigator.appVersion);
    if (strName == "Microsoft Internet Explorer" && lngVer < 4) {return true};

	lngChecked = 0
	blnKeepCounting = true
	lngCheckboxLength = pobjCheckbox.length

	if (typeof lngCheckboxLength == "undefined")
	{	
		// if there is only one checkbox, it is not an array!
		blnOneCheckbox = true;
	}
	
	if (typeof plngMin == "undefined") {plngMin = 1};
	if ((typeof plngMax == "undefined") || plngMax == 0) {plngMax = 999};  // 999 means no upper limit

	if (plngMin == 1) {strAnswerText = "answer"}
	else {strAnswerText = "answers"};
	
	if (plngMin == plngMax) {strMessage = "Please provide exactly "+IntegerToString(plngMin)+" "+strAnswerText+"."}
	else if (plngMax == 999) {strMessage = "Please provide at least "+IntegerToString(plngMin)+" "+strAnswerText+"."}
	else {strMessage = "Please provide "+IntegerToString(plngMin)+" to "+IntegerToString(plngMax)+" answers."};

	if (!((typeof pstrMessage == "undefined" || pstrMessage == ""))) {strMessage = pstrMessage};

	if (blnOneCheckbox)
	{
		if (pobjCheckbox.checked) {lngChecked++};
	}
	else
	{
		for (i=0; i < lngCheckboxLength; i++)			// loop through checkboxes until lngMax
		{	
			//alert("pobjCheckbox["+i+"].checked ="+pobjCheckbox[i].checked);
			if (pobjCheckbox[i].checked) 
			{
				lngChecked++;
			}
			if (lngChecked > plngMax) {blnKeepCounting = false; break};
		}
	}

	if (!(typeof parrTextAnswers == "undefined") && blnKeepCounting == true)
	{
		for (i=0; i < parrTextAnswers.length; i++)	// loop through text objects until lngMax
		{ 
			if (parrTextAnswers[i].value != "" && typeof parrTextAnswers[i].value != "undefined" && parrTextAnswers[i].value != null && !IsBlank(parrTextAnswers[i].value,"no"))
			{
				lngChecked++;
			}
			//alert("parrTextAnswers[i].value  = "+parrTextAnswers[i].value )
			if (lngChecked > plngMax) {blnKeepCounting = false; break};
		}
	}
	//alert("lngChecked = "+lngChecked)

	if (lngChecked < plngMin || lngChecked > plngMax) 
	{
		if (!(strMessage == "no")) {alert(strMessage)};  
		return false
	}; 
	return true;
}


/***********************************************************************
Purpose: 	Converts an integer to a literal string
Author: 	Neal 
Date:		11/01/2001
***********************************************************************/

function IntegerToString(n) {

	if (n > 9)
	{
		// Our style is to not spell out numbers greater than 9
		return n;
	}

	switch(n) {
		case 0: n= ""; break;
		case 1: n= "one"; break;
		case 2: n= "two"; break;
		case 3: n= "three"; break;
		case 4: n= "four"; break;
		case 5: n= "five"; break;
		case 6: n= "six"; break;
		case 7: n= "seven"; break;
		case 8: n= "eight"; break;
		case 9: n= "nine"; break;
		default: n = "Not a Number";
	}
	return n;
}


