var numItems=0;
var arrExtras;

function SearchKeyword(pattern)
{    
	var intCount=0;
	var arrFiltered = new Array(numItems);
	var arrFilteredText = new Array(numItems);
	var arrFilteredValue = new Array(numItems);

	var exitLoop;
	var strSearch = pattern.value.toUpperCase();	
	strSearch = String(strSearch);	

	if (pattern != "") //***Only do check if searchbox text is not empty
	{
		for (var i=0; i<numItems; i++)
		{
			var strListBoxText = arrExtras[i];
			
			var newArray = strListBoxText.match(strSearch);				
			if (eval(newArray))
			{
				if (newArray[0] == strSearch)    //***Match found
				{	
					arrFiltered[intCount] = arrExtras[i];
					arrFilteredText[intCount] = arrExtrasText[i];
					arrFilteredValue[intCount] = arrExtrasValue[i];
										
					intCount++;
				}
			}	
		}			
	}
	
	document.ConsFinderForm.lstModel.options.length = 0;
	for (var d=0; d<=intCount; d++)
	{
	   document.ConsFinderForm.lstModel.options[d] = new Option(arrFilteredText[d],arrFilteredValue[d]);
	}
	document.ConsFinderForm.lstModel.options[0].selected = true;	
}

function BuildArrayOfItems()
{
		numItems = document.ConsFinderForm.lstModel.length;
		arrExtras = new Array(numItems);
		arrExtrasText = new Array(numItems);
		arrExtrasValue = new Array(numItems);
	
		for (var i=0; i<numItems; i++)
		{
			arrExtras[i] = document.ConsFinderForm.lstModel.options[i].text.toUpperCase();
			arrExtrasText[i] = document.ConsFinderForm.lstModel.options[i].text;
			arrExtrasValue[i] = document.ConsFinderForm.lstModel.options[i].value;		
		}	
}

function selectAllOptions(obj) 
{
	for (var i=0; i<obj.options.length; i++)
	{
		obj.options[i].selected = true;
	}
	
	return false;
}

function deleteAllOptions(obj) 
{
	for (var i=0; i<obj.options.length; i++)
	{
		obj.options[i].selected = false;
	}
	
	return false;
}

function MoveItem(Source, Destination, Direction)
{
	//*** I did try to simplify the coding into mini functions - but it didn't work
	//*** Feel free to come up with an elegant coding than below :-)
	
	if (Direction == "right")
	{
		if (Source.selectedIndex == -1)
		{
			alert('Please select from Available Companies');
			return false;
		}
							
		for(i=0;i<Source.length;i++)
		{
			if (Source.options[i].selected)
			{
				Destination.options[Destination.length] = new Option(Source.options[Source.selectedIndex].text, Source.options[Source.selectedIndex].value); //***Create a new instance of Source to be put in the Destination box
				Destination.options[Destination.length-1].text = Source.options[Source.selectedIndex].text; //***Append the item in Destination
				Source.options[Source.selectedIndex] = null; //***Remove the selected index from Source
				i--; //*** make the loop go through them all
			}
		}
	}

	if (Direction == "left")
	{		
		if (Destination.selectedIndex == -1)
		{
			alert('Please select from Selected Companies');
			return false;
		}
		
		var strRemoveCompaniesIDList = "";
		var DelTempCount;
		var DelCounter,NumItemsRemoved;
		DelCounter = 0;
		NumItemsRemoved = 0;
			
		//***First get the total number of items selected (removal)				
		for (CountRemoval=0;CountRemoval<Destination.length;CountRemoval++) 
		{	
			if (Destination.options[CountRemoval].selected)
			{			
				NumItemsRemoved = NumItemsRemoved + 1;
			}
		}
		
		for(i=0;i<Destination.length;i++)
		{
			if (Destination.options[i].selected)
			{			
			
				//***Do work out putting the selected items into a string format (removal)	
				DelCounter = DelCounter + 1;

				var strComma = ",";
									
				if (DelCounter == NumItemsRemoved)
				{
					strComma = "";
				}
							
				if (i == Destination.length-1)
				{
					strComma = "";
				}

				strRemoveCompaniesIDList += Destination.options[i].value+strComma;
				document.theForm.RemoveCompaniesListID.value = strRemoveCompaniesIDList;

				DelTempCount = i;
				
				Source.options[Source.length] = new Option(Destination.options[Destination.selectedIndex].text, Destination.options[Destination.selectedIndex].value); //***Create a new instance of Destination to be put in the Source box
				Source.options[Source.length-1].text = Destination.options[Destination.selectedIndex].text; //***Append the item in Source
				Destination.options[Destination.selectedIndex] = null; //***Remove the Destination index from Source
				i--; //*** make the loop go through them all
			}
		}
		
		return false;
			
		if (DelCounter>0)
		{
			if (DelCounter == 1)
			{
				strComma = "";
				strRemoveCompaniesIDList = "";
				
				strRemoveCompaniesIDList += Destination.options[DelTempCount].value+strComma;
				document.theForm.RemoveCompaniesListID.value = strRemoveCompaniesIDList;
			}
		}
	}
	
	var strCompaniesIDList = "";
	var TempCount;
	var Counter,Count,NumItemsSelected;
	Counter = 0;
	NumItemsSelected = 0;
			
	//***First get the total number of items selected				
	for (Count=0;Count<Destination.length;Count++) 
	{			
		NumItemsSelected = NumItemsSelected + 1;
	}

	//***Do work out putting the selected items into a string format		   
	for (Count=0;Count<Destination.length;Count++) 
	{	  
		Counter = Counter + 1;
		
		var strComma = ",";
											
		if (Counter == NumItemsSelected)
		{
			strComma = "";
		}
									
		if (Count == Destination.length-1)
		{
			strComma = "";
		}
		
		strCompaniesIDList += Destination.options[Count].value+strComma;
		document.theForm.SelectedCompaniesListID.value = strCompaniesIDList;
		
		TempCount = Count;
	}
	
	if (Counter>0)
	{
		if (Counter == 1)
		{
			strComma = "";
			strCompaniesIDList = "";
			
			strCompaniesIDList += Destination.options[TempCount].value+strComma;
			document.theForm.SelectedCompaniesListID.value = strCompaniesIDList;
		}
	}
	
	return false;
}

function ValidateCompanyList(obj) 
{
	if (obj.options.length <= 0)
	{
		alert('Please select from Available Companies into Selected Companies');
	    return false;
	}
	else
	{
		document.theForm.submit();
	}
	return false;
}


function SendModelInfo(obj) 
{
	if (obj.selectedIndex == -1)
	{
		alert('Please choose a model from the Model Listbox');
	    return false;
	}
	else
	{
		document.ConsFinderForm.submit();
	}
	return false;
}


function CostCentrePerLinePopUp(ReqID,LineNo)
{
	var w = 1240, h = 1028;

	var popW = 400, popH = 400;

	var leftPos = (w-popW)/2, topPos = (h-popH)/2;
    window.open("SetCostCentrePerLine.asp?ID=" + ReqID + "&Line=" + LineNo, "" ,"width=" + popW + ",height=" + popH + ",top=" + topPos + ",left=" + leftPos);
}

function SubmitSetCCForm() 
{
	//document.CostCenterPerLine.submit();
        window.close();
}

function RecoupCheckoutValues()
{
        if (window.opener.document.Order.RequNo != undefined)
	{
		window.opener.document.Order.RequNo.value=opener.document.Order.RequNo.value;
	}	
        if (window.opener.document.Order.OrderTitle != undefined) 
	{
		window.opener.document.Order.OrderTitle.value=opener.document.Order.OrderTitle.value;
	}
        if (window.opener.document.Order.DeliveryAddress != undefined) 
	{
		window.opener.document.Order.DeliveryAddress.value=opener.document.Order.DeliveryAddress.value;
	}
        if (window.opener.document.Order.CostCentre != undefined)
	{
		window.opener.document.Order.CostCentre.value=opener.document.Order.CostCentre.value;
	}
        if (window.opener.document.Order.blnWholeOrder != undefined)
	{
		window.opener.document.Order.blnWholeOrder.value="N";
		if (opener.document.Order.wholeorder.checked == true)
		{
			window.opener.document.Order.blnWholeOrder.value="Y";
		}
	}	
	window.opener.document.Order.blnRefresh.value="Y";
	window.opener.document.forms.Order.submit();
}

