<!--//
/*******************************************************************************************
NAME: HGITeam.js
PURPOSE: Handles HGI team functions
AUTHOR: Michael Bane (mtb)
DATE: 04/02/2003
MODIFICATION HISTORY:

*******************************************************************************************/
function addHGITeam(cboHGITeamAvailable,cboHGITeamAssigned,cboHGIPrimaryAvailable,cboHGIPrimaryAssigned){
	// Add each selected available entry to the assigned listing	
	for (var i = 0; i < cboHGITeamAvailable.options.length; i++){
		if (cboHGITeamAvailable.options[i].selected){  	
			// Create new item	
			var newItem = cboHGITeamAssigned.options.length;
			// Add it to the assigned tem
			cboHGITeamAssigned.options[newItem] = new Option(cboHGITeamAvailable.options[i].text);
			cboHGITeamAssigned.options[newItem].value = cboHGITeamAvailable.options[i].value;
			// Also add it to the available primary	
			newItem = cboHGIPrimaryAvailable.options.length;
			cboHGIPrimaryAvailable.options[newItem] = new Option(cboHGITeamAvailable.options[i].text);
			cboHGIPrimaryAvailable.options[newItem].value = cboHGITeamAvailable.options[i].value;
			
			// No reason to select an entry
			//if (cboHGITeamAssigned.options.length == 1)
			//	cboHGITeamAssigned.options[newItem].selected = true;
			
			// remove the item from source
			for (var j = i; j < cboHGITeamAvailable.options.length-1; j++){
				cboHGITeamAvailable.options[j].value = cboHGITeamAvailable.options[j+1].value;
				cboHGITeamAvailable.options[j].text = cboHGITeamAvailable.options[j+1].text;
			}
			cboHGITeamAvailable.options[j] = null;
		}
	}
	clearSelections();
}
function removeHGITeam(cboHGITeamAvailable,cboHGITeamAssigned,cboHGIPrimaryAvailable,cboHGIPrimaryAssigned){
	var newItem = 0;
	var skipItem = false;
	var hasBeenAdded = false;
	var sourceRemoved = false;
	// remove each selected assigned listing to the	available entries
	for (var i = 0; i < cboHGITeamAssigned.options.length; i++){
		//alert(cboHGITeamAssigned.options[i].text + ' (selected) = ' + cboHGITeamAssigned.options[i].selected);
		if (cboHGITeamAssigned.options[i].selected){  	
			skipItem = false;
			hasBeenAdded = false;
			// Create new item	
			newItem = cboHGITeamAvailable.options.length;
			var itemValue = cboHGITeamAssigned.options[i].value;
			// If the entry is a the primary contact remove it
			if(cboHGIPrimaryAssigned.length > 0) {
				if(itemValue == cboHGIPrimaryAssigned.options[0].value){
					var answer = confirm (cboHGIPrimaryAssigned.options[0].text + ' is the primary contact.\nDo you really want to remove this entry?')
					if (answer){
					
					

						// Add it to the available team
						newItem = cboHGITeamAvailable.options.length;
						cboHGITeamAvailable.options[newItem] = new Option(cboHGIPrimaryAssigned.options[0].text);
						cboHGITeamAvailable.options[newItem].value = cboHGIPrimaryAssigned.options[0].value;
						cboHGIPrimaryAssigned.options[0] = null;
						hasBeenAdded = true;
					}else{
						skipItem = true;
					}
				}
			}	
			if(!(skipItem)){
				// Add it to the available team (only if not already added by the primary add code)
				if(!(hasBeenAdded)){
					
					newItem = cboHGITeamAvailable.options.length;
					//alert('availableIndex = ' + newItem);
					cboHGITeamAvailable.options[newItem] = new Option(cboHGITeamAssigned.options[i].text);
					cboHGITeamAvailable.options[newItem].value = cboHGITeamAssigned.options[i].value;
				}
				// remove the item from source (assigned team)
				for (var j = i; j < cboHGITeamAssigned.options.length-1; j++){
					cboHGITeamAssigned.options[j].value = cboHGITeamAssigned.options[j+1].value;
					cboHGITeamAssigned.options[j].text = cboHGITeamAssigned.options[j+1].text;
				}//for (var j = i; j < cboHGITeamAssigned.options.length-1; j++){
				cboHGITeamAssigned.options[j] = null;
				sourceRemoved = true;
				
				// Also remove it from the available primary	
				var matchIndex = 0;
				var itemInList = false;			
				for (var x = 0; x < cboHGIPrimaryAvailable.options.length; x++){
					/*alert('itemValue = ' + itemValue  + '\n' +
						'cboHGIPrimaryAvailable.options[' + x + '].value = ' + 
						cboHGIPrimaryAvailable.options[x].value);
					*/
					if(cboHGIPrimaryAvailable.options[x].value == itemValue){
						itemInList = true;
						matchIndex = x;
						break;
					}
				}//for (var x = 0; x < cboHGIPrimaryAvailable.options.length; x++){
				if(itemInList) {
					for (var p = matchIndex; p < cboHGIPrimaryAvailable.options.length-1; p++){
						cboHGIPrimaryAvailable.options[p].value = cboHGIPrimaryAvailable.options[p+1].value;
						cboHGIPrimaryAvailable.options[p].text = cboHGIPrimaryAvailable.options[p+1].text;
					}
					cboHGIPrimaryAvailable.options[p] = null;
				}//if(itemInList) {
				// if we have removed an entry we need to go back one to prevent an item
				// from being skipped
				if(sourceRemoved) i--;
			} //if(!(skipItem)){
			//alert('i = ' + i);
			
			
			
		}//if (cboHGITeamAssigned.options[i].selected){  	
		
	}//for (var i = 0; i < cboHGITeamAssigned.options.length; i++){
	clearSelections();
}
function addHGIPrimary(cboHGITeamAvailable,cboHGITeamAssigned,cboHGIPrimaryAvailable,cboHGIPrimaryAssigned){
	var newItem = 0;
	// Replace the assigned listing	with the selected available entry  
	for (var i = 0; i < cboHGIPrimaryAvailable.options.length; i++){
		if (cboHGIPrimaryAvailable.options[i].selected){  	
			// Remove the previously assigned entry from the assigned list
			if(cboHGIPrimaryAssigned.length > 0){
				newItem = cboHGIPrimaryAvailable.options.length;
				// Add it to the assigned item (old assigned becomes available)
				cboHGIPrimaryAvailable.options[newItem] = new Option(cboHGIPrimaryAssigned.options[0].text);
				cboHGIPrimaryAvailable.options[newItem].value = cboHGIPrimaryAssigned.options[0].value;
			}
			// Create new item	
			 newItem = cboHGITeamAssigned.options.length;
			// Add it to the assigned item (old available becomes assigned)
			cboHGIPrimaryAssigned.options[0] = new Option(cboHGIPrimaryAvailable.options[i].text);
			cboHGIPrimaryAssigned.options[0].value = cboHGIPrimaryAvailable.options[i].value;

			// No reason to select an entry
			//if (cboHGIPrimaryAssigned.options.length == 1)
			//	cboHGIPrimaryAssigned.options[0].selected = true;
			
			// remove the item from source
			for (var j = i; j < cboHGIPrimaryAvailable.options.length-1; j++){
				cboHGIPrimaryAvailable.options[j].value = cboHGIPrimaryAvailable.options[j+1].value;
				cboHGIPrimaryAvailable.options[j].text = cboHGIPrimaryAvailable.options[j+1].text;
			}
			
			cboHGIPrimaryAvailable.options[j] = null;
			
			clearSelections();
			
			break;
		}
	}

}
function removeHGIPrimary(cboHGITeamAvailable,cboHGITeamAssigned,cboHGIPrimaryAvailable,cboHGIPrimaryAssigned){
	// Add each selected available entry to the assigned listing	
	for (var i = 0; i < cboHGIPrimaryAssigned.options.length; i++){
		if (cboHGIPrimaryAssigned.options[i].selected){  	
			// Create new item	
			var newItem = cboHGIPrimaryAvailable.options.length;
			// Add it to the assigned tem
			cboHGIPrimaryAvailable.options[newItem] = new Option(cboHGIPrimaryAssigned.options[i].text);
			cboHGIPrimaryAvailable.options[newItem].value = cboHGIPrimaryAssigned.options[i].value;
			
			// No reason to select an entry
			//if (cboHGIPrimaryAvailable.options.length == 1)
			//	cboHGIPrimaryAvailable.options[newItem].selected = true;
			
			// remove the item from source
			for (var j = i; j < cboHGIPrimaryAssigned.options.length-1; j++){
				cboHGIPrimaryAssigned.options[j].value = cboHGIPrimaryAssigned.options[j+1].value;
				cboHGIPrimaryAssigned.options[j].text = cboHGIPrimaryAssigned.options[j+1].text;
			}
			cboHGIPrimaryAssigned.options[j] = null;
		}
	}
	clearSelections();
}


// Code snippet from:http://members.aol.com/grassblad/html/moveSelItems.html
function moveItems(src,dest)
{
      var i = j = 0;
      var newItem = "";

      with (document.forms[0]) 
      {
         for (i = 0; i < src.options.length; i++)
         {
            if (src.options[i].selected)
            {  

               newItem = dest.options.length;
               dest.options[newItem] = new Option(src.options[i].text);
               dest.options[newItem].value = src.options[i].value;
               if (dest.options.length == 1)
                  dest.options[newItem].selected = true;

	            // remove the item from source
	            for (j = i; j < src.options.length-1; j++)
	            {
	               src.options[j].value = src.options[j+1].value;
	   		       src.options[j].text = src.options[j+1].text;
	            }//for (j = i; j < src.options.length-1; j++)
	            src.options[j] = null;
	         }//if (src.options[i].selected)
      	}//for (i = 0; i < src.options.length; i++)
   	}//with (document.forms[0]) 
}//function moveItems(src,dest)
function clearSelections(){
	$('cboHGITeamAvailable').selectedIndex = -1;
	$('cboHGITeamAssigned').selectedIndex = -1;
	$('cboHGIPrimaryAvailable').selectedIndex = -1;
	$('cboHGIPrimaryAssigned').selectedIndex = -1;
}
function storeHGITeam(){
	var strTeam = '';
	
	for(var i = 0; i < $('cboHGITeamAssigned').length; i++){
		if(strTeam != '') strTeam += '^';
		strTeam += $('cboHGITeamAssigned').options[i].value;			
	}

	$('TeamList').value = strTeam;
	if($('cboHGIPrimaryAssigned').length > 0)
		$('PrimaryID').value = $('cboHGIPrimaryAssigned').options[0].value;
}
//-->