function trim_last(str_val)
{
	var l;
	l=str_val.length;
	str_val=str_val.substr(0,l-1);
	return str_val;
}

function data_collect()
{
	Fill_SemenIDs();
	
	var SemenIDs=document.bull_form.SemenIDs.value;
	var Qtys=document.bull_form.Qtys.value;

	if(SemenIDs=='')
	{
		alert('Please add at least one bull\'s semen to enquiry list');
		return false;
	}
	document.bull_form.acshon.value=1;
	return true;
}

function FindItem(ID_List,ItemID)
{
	if (ID_List=="") return false;
	
	ID_Arr = ID_List.split(',');
	Arr_Len = ID_Arr.length;
	
	for(i=0;i<Arr_Len;i++)
	{
		if (parseInt(ID_Arr[i])==parseInt(ItemID)) return true;
	}	
	return false;
}

function AddItem(ID_List,ItemID,Qty_List,Qty)
{
	if (ID_List=='')
	{
	 ID_List=ItemID;
	 Qty_List=Qty;
	}
	else
	{
	 ID_List+=','+ItemID;
	 Qty_List+=','+Qty;
	}
	document.bull_form.Qtys.value=Qty_List;
	return ID_List;
}

function AddQty(ID_List,ItemID,Qty_List,Qty)
{
	ID_Arr = ID_List.split(',');
	Arr_Len = ID_Arr.length;
		
	Qty_Arr = Qty_List.split(',');
	
	New_Qty_List='';
		
	for(i=0;i<Arr_Len;i++)
	{
		if (parseInt(ID_Arr[i])==parseInt(ItemID))
			New_Qty_List+=Qty+',';
		else
			New_Qty_List+=Qty_Arr[i]+',';					
	}
	
	if (New_Qty_List!='')
		Qty_List=trim_last(New_Qty_List);
	
	document.bull_form.Qtys.value=Qty_List;	
	return ID_List;	
}

function RemoveItem(ID_List,ItemID,Qty_List,Qty)
{
	if (ID_List=='')
	{
		ID_List='';
		document.bull_form.Qtys.value="";
		return ID_List;		
	}
	
	ID_Arr = ID_List.split(',');
	Arr_Len = ID_Arr.length;
	New_List='';
	
	Qty_Arr = Qty_List.split(',');
	New_Qty_List='';
		
	for(i=0;i<Arr_Len;i++)
	{
		if (parseInt(ID_Arr[i])!=parseInt(ItemID))
		{
			New_List+=ID_Arr[i]+',';
			New_Qty_List+=Qty_Arr[i]+',';
		}		
	}
				
	if (New_List!='')
	{
		ID_List=trim_last(New_List);
		Qty_List=trim_last(New_Qty_List);
	}
	else
	{
	 	ID_List='';
		Qty_List='';
	}
	document.bull_form.Qtys.value=Qty_List;	
	return ID_List;
}

function Fill_SemenIDs()
{
	var n=parseInt(document.bull_form.n.value)*2;
	var SemenIDs=document.bull_form.SemenIDs.value;
	
	for(x=0;x<n;)
	{
		Qty_Val=document.bull_form.elements[x].value;
		ID_Val=document.bull_form.elements[x+1].value;
		if(Qty_Val=="")
			Qty_Val=0;
		
		found=FindItem(SemenIDs,ID_Val);
		
		if(document.bull_form.elements[x+1].checked)
		{
			var Qtys=document.bull_form.Qtys.value;
			if (!found)	
				SemenIDs=AddItem(SemenIDs,ID_Val,Qtys,Qty_Val); //If not found, add to list
			else
				SemenIDs=AddQty(SemenIDs,ID_Val,Qtys,Qty_Val); //If not found, add to list			
		}
		else
		{
			var Qtys=document.bull_form.Qtys.value;
			if (found)
				SemenIDs=RemoveItem(SemenIDs,ID_Val,Qtys,Qty_Val); //If found, remove from list				
		}
		x=x+2;		
	}
	document.bull_form.SemenIDs.value=SemenIDs;	
}

function prev_rec()
{
	Fill_SemenIDs();
	document.bull_form.i.value=parseInt(document.bull_form.i.value)-(parseInt(document.bull_form.n.value)+2);
	document.bull_form.start.value=parseInt(document.bull_form.start.value)-parseInt(document.bull_form.MaxRec.value);
	document.bull_form.submit();
}

function next_rec()
{
	Fill_SemenIDs();
	document.bull_form.start.value=parseInt(document.bull_form.start.value)+parseInt(document.bull_form.MaxRec.value);
	document.bull_form.submit();
}

function MM_Win(theURL,winName,features) 
{
  window.open(theURL,winName,features);
}
