
//-----------------------------------------------------
//ItemInsert
//This function takes the selected option from the select 
//box and inserts it into the new select box
//-----------------------------------------------------
function ItemInsert(sFormName,sElName)
{
 	var fMatch;	
	var nListLength,nSendLength;
	var sOutputString;
	var counter;
	
	nListLength = eval("document."+sFormName+"."+sElName+"_from.length;");
	nSendLength = eval("document."+sFormName+"."+sElName+"_to.length;");
	
	
	counter = nSendLength+1;
	
	//iterate options to look for selected
	for(var i=0;i<nListLength;i++)
		{
			fMatch = "fill";
			//if selected drop in	
			if(eval("document."+sFormName+"."+sElName+"_from.options[i].selected;"))
			{	
				//check the list in the new select box if no match then fill.				
				for(var j=0; j<nSendLength;j++)
				{			
					if (eval("document."+sFormName+"."+sElName+"_from.options[i].value;") == eval("document."+sFormName+"."+sElName+"_to.options[j].value;"))
					{
						
						alert(eval("document."+sFormName+"."+sElName+"_to.options[j].text;")+" is already No. "+ (j+1)  +" on the list.");
						eval("document."+sFormName+"."+sElName+"_to.selectedIndex=j;");
						
						fMatch="cancelFill"
						sOutputString=j;
					}
				}
				
				if(fMatch=="fill")
				{
					
					
					var oNewOption = new Option(eval("counter+'. '+document."+sFormName+"."+sElName+"_from.options[i].text;"),eval("document."+sFormName+"."+sElName+"_from.options[i].value;"));
		
					eval("document."+sFormName+"."+sElName+"_to.options[document."+sFormName+"."+sElName+"_to.options.length] = oNewOption;");
					sOutputString=eval("document."+sFormName+"."+sElName+"_to.options[document."+sFormName+"."+sElName+"_to.options.length];");
					
					
					
				sOutputString="true";
				counter++;
				}
			}
		}
		
	return sOutputString;
}










//-----------------------------------------------------
//ItemScrub
//This function deletes the selected item from the select box
//-----------------------------------------------------
function ItemScrub(sFormName,sElName)
{
 	var nSendLength;
	var sHolder
	var sNumberHolder;
	var sNameHolder;
	var fDirtyList;
	var istart,iend,counter;
	
	istart="empty";	
	fDirtyList = "false";
	counter=1;
	
	nSendLength = eval("document."+sFormName+"."+sElName+"_to.length;");
	
	//iterate options to look for selected
	for(var i=nSendLength-1;i>=0;i--)
		{
			//if selected drop in and scrub list
			if(eval("document."+sFormName+"."+sElName+"_to.options[i].selected;"))
			{	
				if(fDirtyList=="false")
				{
					iend=i-1;
					
				}
				istart=i;
				fDirtyList = "true";	
				eval("document."+sFormName+"."+sElName+"_to.options[i]=null;")
			}
		}
		
		//renumber the list
		if(fDirtyList=="true")
		{
			for(var j=istart;j<nSendLength;j++)
				{
					sHolder = eval("document."+sFormName+"."+sElName+"_to.options[j].text;");
					
					//get name of Item to be renumbered
					sNameHolder = sHolder.substr(sHolder.indexOf(".")+1);
					//set number from last good location
					sNumberHolder = istart+counter;
					
					//set option
					eval("document."+sFormName+"."+sElName+"_to.options[j].text=sNumberHolder+'.'+sNameHolder;");
					counter++;
				}
		}
}



//-----------------------------------------------------
//MoveMe
//This Function will move the selected item up or down in the 
//select box
//-----------------------------------------------------
function MoveMe(direction,sFormName,sElName,sWorkOnSelectSuffix)
{
	
 	var modDir;
	var nSendLength;
	var oHolder;
	var iNew;
	var posOld,posNew;
	var sNameNew,sNameOld;
	
	nSendLength = eval("document."+sFormName+"."+sElName+"_to.length;");
	
	switch(direction)
	{
		case "up":
			modDir = -1;
			break;
		case "down":
			modDir = 1;
			break;
	}
	
	
	
	//iterate options to look for selected
	for(var i=0;i<nSendLength;i++)
	{	
		//if selected drop in	
		if(eval("document."+sFormName+"."+sElName+"_to.options[i].selected;"))
		{
				
				//set new position			
				iNew = i+modDir;
				
				//check to see if new position is within the box
				if(iNew>=0 && iNew<=nSendLength-1)
				{			
					
					//here we move the items
					//hold the item to be displaced  in the temp var oHolder					
					
					var oHolderNew = new Option(eval("document."+sFormName+"."+sElName+"_to.options[iNew].text;"),eval("document."+sFormName+"."+sElName+"_to.options[iNew].value;"));
					var oHolderOld = new Option(eval("document."+sFormName+"."+sElName+"_to.options[i].text;"),eval("document."+sFormName+"."+sElName+"_to.options[i].value;"));

					posNew = oHolderNew.text.substr(0,oHolderNew.text.indexOf("."))
					sNameNew = oHolderOld.text.substr(oHolderOld.text.indexOf(".")+1)
					
					posOld = oHolderOld.text.substr(0,oHolderOld.text.indexOf("."))
					sNameOld = oHolderNew.text.substr(oHolderNew.text.indexOf(".")+1)
					
					
					oHolderOld.text = posNew + "." + sNameNew;
					oHolderNew.text = posOld + "." + sNameOld;
					//switch moving item into the new position
					eval("document."+sFormName+"."+sElName+"_to.options[iNew] = oHolderOld;");
					
					
					//switch displaced item into the old position
					eval("document."+sFormName+"."+sElName+"_to.options[i] = oHolderNew;");
					
					
				}
		}
	}

	//refocus on the moved item
	eval("document."+sFormName+"."+sElName+"_to.selectedIndex=iNew;");
	
	return iNew;
}




//------------------------------------------------
//AutoPositionOption
//This function will take a finish position as a parameter 
//and call the MoveMe function until it is at the correct position
//------------------------------------------------
function AutoPositionOption(sFormName, sElName, newPos, sWorkOnSelectSuffix)
{
	//setting newPos to true position in the collection
	newPos = newPos-1
	
	var iNewPosition;
	var fStop;
	var nSendLength;
	var fValid
	var sDir
	
	sDir = "up";
	fValid = "true";
	
	//Set flag to "go"
	fStop="go";
	
	
	//check for which select box has control
	if(sWorkOnSelectSuffix=="_to")
	{

		//get selected position
		if((eval("document."+sFormName+"."+sElName+"_to.selectedIndex;"))>-1)
		{
			
			fValid=eval("document."+sFormName+"."+sElName+"_to.selectedIndex;");
			//set nSendLength
			nSendLength = eval("document."+sFormName+"."+sElName+"_to.length;");
			//set direction		
			if(newPos>fValid)sDir="down";
		}
		else{fValid="false";}
		
	}
	else
	{
		
		if((eval("document."+sFormName+"."+sElName+"_from.selectedIndex;"))>-1)
		{
			//Try to insert new option - returns index of matching item on the to list
			//of the selected item on the from list
			//or else it returns true if item is able to be inserted
			//if true then we want to set position to end of list
			//otherwise set it to correct option
			fValid = ItemInsert(sFormName,sElName);	
			//get new length
			nSendLength = eval("document."+sFormName+"."+sElName+"_to.length;");
			
			//check return value, true is inserted, otherwise it returns the 
			//current position of the element that exists
			if (fValid=="true")
			{
				//select item to last in list
				eval("document."+sFormName+"."+sElName+"_to.selectedIndex=nSendLength-1;");
				//now set fValid position
				fValid = nSendLength;
			}
			else
			{
				//select correct option
				eval("document."+sFormName+"."+sElName+"_to.selectedIndex=fValid;");
				//set direction
				if(newPos>fValid)sDir="down";
				
			}
		}
		else{fValid="false";}
	}
	

	//find current length	
	if(fValid!=newPos&&fValid!="false")
	{
		
		//check for a valid move, cancel flag if not.
		if(nSendLength <= parseInt(newPos)){fStop="stop";}
		
		
		
		//Move and loop
		while(fStop == "go")
		{
			iNewPosition = MoveMe(sDir,sFormName,sElName,sWorkOnSelectSuffix);
			
			if(iNewPosition== parseInt(newPos))
			{
				
				fStop="stop";
			}
			
		}
	}
}



/*--------------------------------------------------
|LoadSource function
|
|Accepts the filename from the src attribute in the 
|<xml> tag.
|
|Returns the oXMLDOM
----------------------------------------------------*/
function LoadSource(sourceObj)
{
	
	 var oXMLDOM=new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
	 oXMLDOM.async=false;
	 oXMLDOM.load(sourceObj.XMLDocument);
	
	 return oXMLDOM;	
 }



//this method accesses the appropriate xml element holding a comma delimited poll list 
//and fills the select_to box with it.
function FillPoll(sXMLElementID)
{
	var sTeamsString
	var aTeams ;
	 var oXMLDOC=new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
	 oXMLDOC.async=false;
	
	document.pollform.select_to.options.length = 0;
	
	eval("oXMLDOC = "+sXMLElementID+".XMLDocument;");
	
	sTeamsString = oXMLDOC.documentElement.text;
	
	aTeams = sTeamsString.split(",");

	var oNewOption
	for(var i=0;i<aTeams.length;i++)
	{
		
			oNewOption = new Option((i+1)+". "+aTeams[i],aTeams[i]);
			document.pollform.select_to.options[i] = oNewOption;
				
	}
}


function DoSubmit()
{

	var sTeamList;
	var sMsgHeader;
	var sMsgFooter;
	var iloop;
	
	sMsgHeader = "You have selected:\n\n";
	sMsgFooter = "\n\nIs this correct?";
	sTeamList = "";
	
	
	for(iloop=0;iloop<(document.pollform.select_to.options.length);iloop++)
	{	
		if(iloop>24){break;}
		sTeamList = sTeamList +(iloop+1)+". "+document.pollform.select_to.options[iloop].value+"\n";
	}
	
	if(confirm(sMsgHeader+sTeamList+sMsgFooter))
	{
		
		for(var j = 0; j<document.pollform.select_to.options.length;j++)
		{
			document.pollform.select_to.options[j].selected=true;		
		}
		
		//scrub list
		document.pollform.select_from.options.length = 0;
			
		document.pollform.submit();
	}
	
return;
}

