var cSTATE_ERROR_MSG = "Please select a state or a territory from the State/Territory drop-down list.";

//------------------------------------------
//Sets the checkboxes the un/checked based on the 
//the value of vbSelectAll
function modifyCheckBoxes(vbSelectAll)
{

	var i;
	var chkBoxes;
	chkBoxes = document.getElementsByName("chkQuestionSelect");
	for ( i = 0; i < chkBoxes.length; i++ )
	{
		chkBoxes[i].checked = vbSelectAll;
    }

}

//Check the checkboxes
function selectAllCheckBoxes()
{
	modifyCheckBoxes(true);
}

//Uncheck all the check boxes
function resetCheckBoxes()
{
	modifyCheckBoxes(false);
}


function displayAlertMessage(errorMsg)
{
	// var sErrorMsgHeading = "Please correct the following error(s) to proceed: "
	var i;
	var output;
	output = "";
	for ( i = 0; i < errorMsg.length; i++ )
	{
		if ( output == "" )
		{
			output = errorMsg[i];
		}
		else 
		{
			output = output + "\n" + errorMsg[i];
		}
	}
	// alert(sErrorMsgHeading + output);
	alert(output);
}

function errorInCheckBoxes(vsCheckBoxName)
{
	var checkBoxes;
	switch(vsCheckBoxName)
	{
		case "chkQuestionSelect":
			 checkBoxes = document.frmSearchCriteria.chkQuestionSelect;
			 break;
		case "Org":
			 checkBoxes = document.frmSearchCriteria.Org;
			 break;
	}
	
	//checkBoxes = document.getElementsByName(vsCheckBoxName);
	for ( var i = 0; i < checkBoxes.length; i++ )
	{
		if ( checkBoxes[i].checked )
		{
			return false;
		}
	}
	
	return true;
}

function validateTopicSearch()
{
	var sErrorMsg;
	sErrorMsg = new Array();
	
	if ( errorInState() )
	{
		//sErrorMsg.push(cSTATE_ERROR_MSG);
		sErrorMsg = mcgPush(sErrorMsg, cSTATE_ERROR_MSG);
	}
	
	if ( errorInCheckBoxes("chkQuestionSelect") )
	{
		sErrorMsg = mcgPush(sErrorMsg, "Please select at least one Topic Area.");
	}
	
	return sErrorMsg;
}

function errorInOrgList()
{
	if ( document.frmSearchCriteria.Organizations.selectedIndex == 0 )
	{
		return true;
	}
	
	//if ( document.getElementById("OrgList") != null ) 
	//{
	//	if ( document.frmSearchCriteria.OrgList.selectedIndex == 0 ) 
	//	{
	//		return true;
	//	}
	//	if ( document.getElementById("OrgList").value == "" || document.getElementById("OrgList").value == "OrgListDefault")
	//	{
	//		return true;
	//	}
	//}
	
		
	return false;
}

function validateOrganizationSearch()
{
	var sErrorMsg;
	sErrorMsg = new Array();
	
	if ( errorInState() )
	{
		//sErrorMsg.push(cSTATE_ERROR_MSG);
		sErrorMsg = mcgPush(sErrorMsg, cSTATE_ERROR_MSG);
	}
	
	if ( errorInOrgList() )
	{
		sErrorMsg = mcgPush(sErrorMsg, "Please select an organization from the Organization drop-down list.");
	}
	
	return sErrorMsg;
}

function errorInState()
{
	if ( document.frmSearchCriteria.State == null )
	{
		return false;
	}
	
	if ( document.frmSearchCriteria.State.selectedIndex == 0 ) 
	{
		
		return true;
	}
	
	//if ( document.getElementById("State") == null || document.frmSearchCriteria.State == null) 
	//{
	//	return false;
	//}
	
	//if ( document.frmSearchCriteria.State.value == "" || document.frmSearchCriteria.State.value == "StateListDefault" )
	//{
	//		return true;
	//}
	
	return false;
}

function stateValidStaticPages(vsStateBoxName)
{
	if ( document.getElementsByName(vsStateBoxName) == null) 
	{
		return true;
	}
	
	if ( document.getElementsByName(vsStateBoxName).value == "" || document.getElementsByName(vsStateBoxName)(0).value == "StateListDefault")
	{
		alert(cSTATE_ERROR_MSG);
		return false;
	}
	
	return true;
}

function validateTop5Search()
{
	var sErrorMsg;
	sErrorMsg = new Array();
	
	if ( errorInState() )
	{
		sErrorMsg = mcgPush(sErrorMsg, cSTATE_ERROR_MSG);
	}
	
	if ( errorInCheckBoxes("Org") )
	{
		sErrorMsg = mcgPush(sErrorMsg, "Please select at least one of the 5 Most Popular Organizations.");
	}

	return sErrorMsg;
}

function mcgPush(arrayMsgs, sMsg)
{
	arrayMsgs [arrayMsgs.length] = sMsg;
	return arrayMsgs;
}


function validateParameters(isTop5, type)
{
	var sErrorMsgs;
	sErrorMsgs = "";
	
	if ( type.toUpperCase() == "TOPIC" )
	{
		sErrorMsgs = validateTopicSearch();
	}
	else if ( type.toUpperCase() == "ORGANIZATION" )
	{
		if ( isTop5.toUpperCase() == "TRUE" )
		{
			sErrorMsgs = validateTop5Search();
		}
		else
		{
			sErrorMsgs = validateOrganizationSearch();
		}
	}
	else
	{
		if ( isTop5.toUpperCase() == "FALSE" )   // load page (only Continue Top 5 Button showing)
		{
			sErrorMsgs = validateTop5Search();
		}
	}
	
	
	//need the length > 0 check for ns 4.79
	if ( sErrorMsgs != "" && sErrorMsgs.length > 0) 
	{
		displayAlertMessage(sErrorMsgs);
		return false;
	}
	else
	{
		return true;
	}
}
