//<script language="javascript">
/* Code needed for Login Manager applet */

function Add()
	{
	//document.edit_customer.action = "customerequip-addnewcustomer.cfm"
	//document.edit_customer.submit();
	self.location.pathname="/caterpillar/harvesting/customersupequip-addnew.cfm";
	}

function Edit(upd_this)
	{
	if(upd_this.seladdoption.length > 0)
		{
		var j=0;
		for(var i=0; i < upd_this.seladdoption.length; i++)
			{
			if(upd_this.seladdoption[i].selected) 
				{
				j++;
				upd_this.optionselected.value=i;
				}
			}
		if(j!=0 && j<2)
			{
			upd_this.action="customersupequip-editexist.cfm";
			upd_this.submit();
			}
		else
			{
			alert("You must select exactly one piece of support equipment from the customer list to edit");
			}
		}
	else
		{
		alert("You must have support equipment in the customer list to edit");
		}
	}

function TransformValue(selone, seltwo)
	{
	var transformvalue;
	for(var i=0; i<selone.options.length; i++)
		{
		if(selone.options[i].selected && selone.options[i].value != "")
			{
			transformvalue = selone.options[i].value;
			transformvalue = transformvalue.concat("-").concat(" -1- - - ");
			var no = new Option();
			no.value = transformvalue;
			if (selone.form.custsupequiplist.value.length!=0)
				{
				selone.form.custsupequiplist.value = selone.form.custsupequiplist.value.concat("\;");
				}
			selone.form.custsupequiplist.value = selone.form.custsupequiplist.value.concat(transformvalue);
			no.text = "[1]".concat(selone.options[i].text);
			no.selected = true;
			seltwo.options[seltwo.options.length] = no;
			selone.options[i].selected=false;
			BumpSpaceDown(selone);
			BumpSpaceDown(seltwo);
			if (sortitems) {SortD(seltwo);}
			}
		}
	}

function ResetForm(upd_this)
	{
	upd_this.reset();
	}

function ConfirmDelete() 
    {
        if (confirm("This will delete the current customer from the database.  Do you really want to do this?"))
            {
			document.list.action="customermgr-delete.cfm";
			document.list.submit();
            }
    }

function Update(upd_this)
	{
		if (_CF_checkedit_(upd_this))
		{
		document.edit_.submit();
		}
	}
	
function Cancel()
	{
	document.edit_.supcancelflag.value="true";
	document.edit_.action="customersupequip-list.cfm";
	document.edit_.submit();
	}

function ReturnHome()
	{
	self.location.pathname="/caterpillar/harvesting/home-genframes.cfm";
	}

function VerifyNoDoubles(selone, seltwo)
	{
//selone is selremoveoption
//seltwo is seladdoption

	var selectedoptions=new Array();
	//loop through selremoveoption
	for(var i=0; i < selone.length; i++)
		{
		//if you find an option that's selected...
		if(selone.options[i].selected)
			{
			//loop through seladdoption
			for(var j=0; j < seltwo.length; j++)
				{
				//if you find the selected option in selremoveoption == the current option in seladdoption...
				if(selone.options[i].value==jsListGetAt(seltwo.options[j].value, 1, "-"))
					{
					selectedoptions[selectedoptions.length]=i;
					selone.options[i].selected=false;
					var count=jsListGetAt(seltwo.options[j].value, 3, "-");
					seltwo.options[j].value=jsListSetAt(seltwo.options[j].value, 3, parseInt(count)+1, "-");
					seltwo.options[j].text="[".concat(parseInt(count)+1).concat("]").concat(selone.options[i].text);
					selone.form.custsupequiplist.value=jsListSetAt(selone.form.custsupequiplist.value, j+1, seltwo.options[j].value, "\;");
					}
				}
			}
		}
	TransformValue(selone,seltwo);
	for(var i=0; i<selectedoptions.length; i++)
		{
		selone.options[selectedoptions[i]].selected=true;
		}
	}

function PopulateCustomerMachines(sel)
	{
	/*conditional statement added to ensure that the script only runs when the page is loaded,
	otherwise we get the list doubled when we print in Netscape*/
	if(sel.length==1){
	var custsupequiplist = sel.form.custsupequiplist.value;
	for(var k=0; k<jsListLen(custsupequiplist, "\;"); k++)
		{
		var custmachvalue=jsListGetAt(custsupequiplist, k+1, "\;");
		var machid=jsListGetAt(custmachvalue, 1, "-");
		for(var i=0; i<sel.form.selremoveoption.length; i++)
			{
			if(sel.form.selremoveoption.options[i].value==machid)
				{
				var machdesc=sel.form.selremoveoption.options[i].text;
				if(jsListGetAt(custmachvalue, 3, "-")!=" ")
					{
					machdesc = "[".concat(jsListGetAt(custmachvalue, 3, "-")).concat("]").concat(machdesc);
					}
				else
					{
					machdesc = "[1]".concat(machdesc);
					}
				sel.options[sel.length]=new Option(machdesc, custmachvalue, false, false);
				BumpSpaceDown(sel);
				break;
				}
			}
		}
	DeselectAll(sel);
	}
	}

function RemoveList(selcontrol)
	{
	var removed=0;
	var selectedoptions=new Array();
	for(var i=0; i<selcontrol.length; i++)
		{
		if(selcontrol.options[i].selected)
			{
			if(jsListGetAt(selcontrol.options[i].value, 3, "-")<2)
				{
				selcontrol.form.custsupequiplist.value=jsListDeleteAt(selcontrol.form.custsupequiplist.value, i+1, "\;");
				removed++;
				}
			else
				{
				selcontrol.options[i].value=jsListSetAt(selcontrol.options[i].value, 3, parseInt(jsListGetAt(selcontrol.options[i].value, 3, "-"))-1, "-");
				selcontrol.form.custsupequiplist.value=jsListSetAt(selcontrol.form.custsupequiplist.value, i+1, selcontrol.options[i].value, "\;");
				var getnum = selcontrol.options[i].text.match(/^(\[)(\d+)(\].*)$/);
				selcontrol.options[i].text=String(getnum[1])+String(parseInt(getnum[2])-1)+String(getnum[3]);
				selcontrol.options[i].selected=false;
				selectedoptions[selectedoptions.length]=i-removed;
				}
			}
		}
	Remove(selcontrol);
	for(var i=0; i<selectedoptions.length; i++)
		{
		selcontrol.options[selectedoptions[i]].selected=true;
		}
	}
//</script>
