var twoperiods = ["79825"];

function find(a,v)
{
	var count = 0;
	for(var i = 0; i < a.length; i++)
	{
		if(a[i] == v)
		{
			count++;
		}
	}
	return count;
}

function selectedChoices(a)
{
	var count = 0;
	for(var i = 0; i < a.length; i++)
	{
		var x = find(twoperiods,a[i].getValue());
		
		if(x == 0)
		{
			count++;
		}
		else
		{
			count += 2;
		}
	}		
	return count;
}

function OnClientDragDropStart_StudentSchedule(source, target) 
{ 
	debugger;
	//return true;
	var targetParentNode;
	if(target.getDepth() == 0)
	{
		targetParentNode = target;
	}
	else
	{
		targetParentNode = target.getParentTreeNode();
	}

	var childNodes;
		
	if(source.getParentTreeNode() == targetParentNode)
	{
		return true;
	}
	else
	{
		//if(targetParentNode.getValue() == "1" && targetParentNode.getNodes().length >= NumChoices[0])
		if(targetParentNode.getValue() == "1" && selectedChoices(targetParentNode.getNodes()) >= NumChoices[0])
		{
			alert("You have already added the maximum number of first choices.");
			return false;
		}
		//else if(targetParentNode.getValue() == "2" && targetParentNode.getNodes().length >= NumChoices[1])
		else if(targetParentNode.getValue() == "2" && selectedChoices(targetParentNode.getNodes()) >= NumChoices[1])
		{
			alert("You have already added the maximum number of second choices.");
			return false;
		}
		
		childNodes = targetParentNode.getNodes();
		
		for(var i = 0; i < childNodes.length; i++)
		{
			//if(source.getValue() == childNodes[i].getValue())
			if(source.getValue() == childNodes[i].getValue())
			{
				alert("Class " + childNodes[i].getText() + " already exists in list.");
				return false;
			}
		}
		
		if(find(twoperiods,source.getValue()) == 1)
		{
			if(targetParentNode.getValue() == "1" && selectedChoices(targetParentNode.getNodes()) + 2 > NumChoices[0])
			{
				alert("You don't have enough space to add this two period class.");
				return false;
			}
			else if(targetParentNode.getValue() == "2" && selectedChoices(targetParentNode.getNodes()) + 2 > NumChoices[1])
			{
				alert("You don't have enough space to add this two period class.");
				return false;
			}
		}

		if(limit != null && limit && school != null && school == 1060)
		{
			var courses = ['16115', '16314', '16514', '94414', '27411', '27412','70215','74215','74225','97332','97333', '94410', '94420', '94430', '27410', '15510', '15520', '15530', '9310'];
			//if(targetParentNode.getValue() == "1" && targetParentNode.getNodes().length == 7)
			if(targetParentNode.getValue() == "1" && selectedChoices(targetParentNode.getNodes()) + find(twoperiods,source.getValue()) + 1 == NumChoices[0])
			{
//				for(i = 0; i < courses.length; i++)
//				{
//					if(source.getValue() == courses[i])
//					{
//						return true;
//					}
					
//					for(j = 0; j < childNodes.length; j++)
//					{
//						if(childNodes[j].getValue() == courses[i])
//						{
//							return true;
//						}
//					}
//				}

				if(find(courses,source.getValue()) == 1)
				{
					return true;
				}
				
				for(i = 0; i < childNodes.length; i++)
				{
					if(find(courses,childNodes[i].getValue()) == 1)
					{
						return true;
					}
				}
				
				//5_31_2006 Advisors
				//alert("The " + NumChoices[0] + "th choice may only be used for the following classes: Academic Decathlon, Jazz Band, College Bridge, College Excel, Theatre Academy II - One Period & Production, Theatre Academy III - One Period & Production, and all Business and Finance courses.");
				//return false;
				return true;
			}
			
			//if(targetParentNode.getValue() == "2" && (grade == 9 || grade == 10) && targetParentNode.getNodes().length == 2)
			if(targetParentNode.getValue() == "2" && (grade == 9 || grade == 10) && selectedChoices(targetParentNode.getNodes()) == 2)
			{
				//alert('Current freshman and sophomores may only have 2 second choices.');
				//return false;
			}
		}
	}
	return true;
}

function ViewUsers_ClientClickStart(node)
{
	if(node.isCheckable())
	{
		if(node.isChecked())
		{
			node.uncheck();
		}
		else
		{
			node.check();
		}
		return false;
	}
	return true;
}

function OnClientClick(node)
{
	//node.unselect();
	return true;
}

function DisplayPopupHelp(ModuleId)
{
	return overlib(eval('PopupHelp' + ModuleId + 'Text'),CAPTION,eval('PopupHelp' + ModuleId + 'Caption'),
		STICKY,AUTOSTATUSCAP,CLOSECLICK,DRAGGABLE,ABOVE,OFFSETY,20,WRAP,WRAPMAX,350,FGCOLOR,"whitesmoke",
		CGCOLOR,'#dcdce6',BGCOLOR,'#dcdce6',CAPCOLOR,'black',TEXTFONT,'Tahoma',CAPTIONFONT,'Tahoma',
		CLOSECOLOR,'black');
}




