//Define arrays describing the possible card choices
var arrCardName = new Array(22);
var arrCardImage = new Array(22);
var arrCardDescription = new Array(22);
var arrCardCode = new Array(22);
var arrCardWeight = new Array(22);
var arrCardURL = new Array(22);

//Define array describing what is currently selected in the slots
var arrSlotCode = new Array(6);

//constants
var CARD_WEIGHT = 1;
var CARD_DESCRIPTION = 2;
var CARD_NAME = 3;
var CARD_IMAGE = 4;
var CARD_URL = 5;

var DEFAULT_INSTRUCTIONS = "Explore the options of the RMC by clicking on each module and reading its description. <br >" +
							" <br >" +
							"Create the motion controller of your choice by clicking on the modules. <br >" +
							" <br >" +
							"For detailed technical specifications for this particular configuration, click 'Get Specs' below. <br >" + 
							"<br>" + 
							"Request a quote for the controller you create by clicking below.";

var CARD_IMG_LOC = "/rmcquote/images/quoteme100/";
var DELETE_CARD = "DELETE";
var CANCEL_SELECTION = "Return to General Instructions";

//Card data
arrCardCode[0] = "";
arrCardName[0] = "Expand the RMC";
arrCardImage[0] = CARD_IMG_LOC + "crd_choices2.gif";
arrCardDescription[0] = "These controllers are factory configured to have one communication, one CPU, and one to four I/O modules, providing up to 8 axes.	Almost any combination can be specified to fit almost any application: linear or rotary, position, pressure, or position/pressure control.	Click as indicated above to expand the controller.";
arrCardURL[0] = ""
arrCardWeight[0] = 0;

arrCardCode[1] = "DNET";
arrCardName[1]	= "Device Net";
arrCardImage[1] = CARD_IMG_LOC + "5.gif";
arrCardDescription[1] = "";
arrCardURL[1] = ""
arrCardWeight[1] = 1;

arrCardCode[2] = "DI/O";
arrCardName[2]	= "DI/O (18in, 8 out)";
arrCardImage[2] = CARD_IMG_LOC + "2.gif";
arrCardDescription[2] = "This module can provide an interface to any system or be used in stand-alone applications.	Response time is fast (control loop speed), but data transfer capability is limited.";
arrCardURL[2] = "/products/motion/rmc100/rmc100dio.php";
arrCardWeight[2] = 2;
altDIOCardName = "Communication DI/O Module";

arrCardCode[3] = "PROFI";
arrCardName[3] = "PROFIBUS-DP";
arrCardImage[3] = CARD_IMG_LOC + "3.gif";
arrCardDescription[3] = "PROFIBUS is one of the fastest industrial fieldbuses in terms of control data response times.	Messaging performance varies by PLC.";
arrCardURL[3] = "/products/motion/rmc100/rmc100profi.php"
arrCardWeight[3] = 3;
		
arrCardCode[4] = "MB+";
arrCardName[4]	= "Modbus Plus";
arrCardImage[4] = CARD_IMG_LOC + "4.gif";
arrCardDescription[4] = "This module allows tremendous data flexibility to maximize performance of this medium performance bus.";
arrCardURL[4] = "/products/motion/rmc100/rmc100mbp.php"
arrCardWeight[4] = 4;

arrCardCode[5] = "SERIAL";
arrCardName[5]	= "Serial (RS-232/422/485)";
arrCardImage[5] = CARD_IMG_LOC + "7.gif";
arrCardDescription[5] = "This module provides a cost-effective interface to a wide variety of host systems.	Both response time and messaging performance vary by PLC.";
arrCardURL[5] = "/products/motion/rmc100/rmc100serial"
arrCardWeight[5] = 5;
		
arrCardCode[6] = "ENET";
arrCardName[6]	= "Ethernet";
arrCardImage[6] = CARD_IMG_LOC + "6.gif";
arrCardDescription[6] = "Ethernet is often the best choice for ease-of-use and offers the highest data throughput.	Response times vary by protocol, but some, such as Allen-Bradley's EtherNet/IP, are quite deterministic."
arrCardURL[6] = "/products/motion/rmc100/rmc100enet.php"
arrCardWeight[6] = 6;

arrCardCode[7] = "M";
arrCardName[7]	= "(M) MDT (2 axes, analog output)";
arrCardImage[7] = CARD_IMG_LOC + "9.gif";
arrCardDescription[7] = "This module (-M) interfaces with magnetostrictive displacement transducers as position feedback devices and provides analog drive outputs.";
arrCardURL[7] = "/products/motion/rmc100/rmc100mdt.php"
arrCardWeight[7] = 7;
		
arrCardCode[8] = "S";
arrCardName[8]	= "(S) SSI (2 axes, analog output)";
arrCardImage[8] = CARD_IMG_LOC + "10.gif";
arrCardDescription[8] = "This module (-S) interfaces with a wide variety of position feedback devices that use the Synchronous Serial Interface standard and provides analog drive outputs.	SSI devices typically offer absolute positions at high resolutions.";
arrCardURL[8] = "/products/motion/rmc100/rmc100ssi.php"
arrCardWeight[8] = 8;

arrCardCode[9] = "na";			//"SST"
arrCardName[9]	= "(SST) SSI (2 axes, stepper output)";
arrCardImage[9] = CARD_IMG_LOC + "16.gif";
arrCardDescription[9] = "This module (-SST) allows stepper control with a wide variety of position feedback devices using the Synchronous Serial Interface standard.";
arrCardURL[9] = "/products/motion/rmc100/rmc100ssi.php"
arrCardWeight[9] = 9;

arrCardCode[10] = "R";
arrCardName[10]	= "(R) Resolver (2 axes, analog output)";
arrCardImage[10] = CARD_IMG_LOC + "16.png";
arrCardDescription[10] = "This module (-R) interfaces with resolvers for position feedback and provides analog drive outputs for closed loop control.	Resolvers are absolute rotary transducers capable of operating in harsh environments.";
arrCardURL[10] = "/products/motion/rmc100/rmc100res.php"
arrCardWeight[10] = 10;

arrCardCode[11] = "Q";
arrCardName[11]	= "(Q) Quadrature (2 axes, analog out)";
arrCardImage[11] = CARD_IMG_LOC + "11.gif";
arrCardDescription[11] = "This module (-Q) interfaces with quadrature encoders and provides analog drive outputs for closed loop control.	The inputs can also be used alone for position reference (gearing).";
arrCardURL[11] = "/products/motion/rmc100/rmc100quad.php"
arrCardWeight[11] = 11;

arrCardCode[12] = "QST";
arrCardName[12]	= "(QST) Quadrature (2 axes, stepper output)";
arrCardImage[12] = CARD_IMG_LOC + "12.gif";
arrCardDescription[12] = "This module (-QST) provides stepper control.	The quadrature encoder inputs can be used for axis feedback or for position reference (gearing).";
arrCardURL[12] = "/products/motion/rmc100/rmc100step.php"
arrCardWeight[12] = 12;

arrCardCode[13] = "H";
arrCardName[13]	= "(H) Analog - 16-bit (4 inputs, 2 axes)";
arrCardImage[13] = CARD_IMG_LOC + "13.gif";
arrCardDescription[13] = "This module (-H) interfaces to analog position or pressure feedback devices for position, pressure, or position/pressure control.	Pressure inputs can be used together with position inputs on this or other I/O modules to provide position/pressure control.	Pressure control requires the RMC101 CPU.";
arrCardURL[13] = "/products/motion/rmc100/rmc100anlg.php"
arrCardWeight[13] = 14;

arrCardCode[14] = "G";
arrCardName[14]	= "(G) Analog - 16-bit (2 inputs, 2 axes)";
arrCardImage[14] = CARD_IMG_LOC + "15.gif";
arrCardDescription[14] = "This module (-G) provides cost-effective analog position and pressure control. Its 16-bit voltage inputs can also be used for position-pressure control. For 4-20mA current feedback, use the –H analog module. Pressure control requires the RMC101 CPU.";
arrCardURL[14] = "/products/motion/rmc100/rmc100anlg.php"
arrCardWeight[14] = 14.5; // decimal to make it weigh between 14 and 15 without renumbering

arrCardCode[15] = "SO";
arrCardName[15]	= "(SO) SSI (1 axis, SSI+analog out)";
arrCardImage[15] = CARD_IMG_LOC + "so.png";
arrCardDescription[15] = "This module (-SO) allows for synchronizing multiple RMCs to a single SSI position transducer.";
arrCardURL[15] = "/products/motion/rmc100/rmc100so.php"
arrCardWeight[15] = 8.5; // decimal to make it weigh between 8 and 9 without renumbering

arrCardCode[16] = "A";
arrCardName[16]	= "(A) Analog - 12-bit (4 inputs)";
arrCardImage[16] = CARD_IMG_LOC + "14.gif";
arrCardDescription[16] = "This module (-A) provides 12-bit analog inputs to be used together with another I/O module for position/pressure control, or alone for position or velocity reference.	Pressure control requires the RMC101 CPU.";
arrCardURL[16] = "/products/motion/rmc100/rmc100anlg.php"
arrCardWeight[16] = 16;

arrCardCode[17] = "D";
arrCardName[17]	= "(D) DI/O (18in, 8 out)";
arrCardImage[17] = CARD_IMG_LOC + "2.gif";
arrCardDescription[17] = "This module (-D) supports fast (control loop speed) response to external inputs and triggering of external outputs.";
arrCardURL[17] = "/products/motion/rmc100/rmc100dio.php"
arrCardWeight[17] = 16;

arrCardCode[18] = "RMC100";
arrCardName[18]	= "RMC100 CPU";
arrCardImage[18] = CARD_IMG_LOC + "8.gif";
arrCardDescription[18] = "The RMC100 supports up to 8 axes of position control.	The RMC101 controls up to 8 position or pressure (single-loop) axes or up to 4 position/pressure (dual-loop) axes or any combination of single- and dual-loop axes not exceeding 8 total loops.	You must select an analog I/O module for pressure inputs before you can select the RMC101 CPU.";
arrCardURL[18] = "/products/motion/rmc100/cpu100.php"
arrCardWeight[18] = 17;

arrCardCode[19] = "RMC101";
arrCardName[19]	= "RMC101 CPU";
arrCardImage[19] = CARD_IMG_LOC + "8.gif";
arrCardDescription[19] = "The RMC101 controls up to 8 position or pressure (single-loop) axes or up to 4 position/pressure (dual-loop) axes or any combination of single- and dual-loop axes not exceeding 8 total loops.	This CPU, which contains the Pressure/Force Control Firmware Option, requires at least one analog I/O module for pressure inputs.	If pressure control is not needed, the RMC100 can be used for up to 8 axes of position control.";
arrCardURL[19] = "/products/motion/rmc100/cpu100.php"
arrCardWeight[19] = 18;

arrCardCode[20] = "CHOICE";
arrCardName[20] = "Expand the RMC";
arrCardImage[20] = CARD_IMG_LOC + "crd_choices2.gif";
arrCardDescription[20] = "These controllers are factory configured to have one communication, one CPU, and one to four I/O modules, providing up to 8 axes.	Almost any combination can be specified to fit almost any application: linear or rotary, position, pressure, or position/pressure control.	Click as indicated above to expand the controller.";
arrCardURL[20] = ""
arrCardWeight[20] = 100;

arrCardCode[21] = "BLANK";
arrCardName[21]	= "Blank (no communication)";
arrCardImage[21] = CARD_IMG_LOC + "1.gif";
arrCardDescription[21] = "The blank slot cover allows an RMC to be built without a communication module.";
arrCardURL[21] = ""
arrCardWeight[21] = 20;

arrCardCode[22] = "BL";
arrCardName[22]	= "Blank Slot Cover";
arrCardImage[22] = CARD_IMG_LOC + "1.gif";
arrCardDescription[22] = "The blank slot cover allows an RMC to be built with an empty slot to allow room for later expansion.";
arrCardURL[22] = ""
arrCardWeight[22] = 21;

arrCardCode[23] = "EMPTY";
arrCardName[23]	= "Empty Slot";
arrCardImage[23] = CARD_IMG_LOC + "1.gif";
arrCardDescription[23] = "";
arrCardURL[23] = ""
arrCardWeight[23] = 23;

//Assign initial values to the slots.
arrSlotCode[0] = arrCardCode[6];
arrSlotCode[1] = arrCardCode[18];
arrSlotCode[2] = arrCardCode[8];
arrSlotCode[3] = arrCardCode[20];
arrSlotCode[4] = arrCardCode[20];
arrSlotCode[5] = arrCardCode[20];

function QuoteMe(sPartNo,type)
{
	/*Uncomment the following line of code to turn on passing the "type" parameter to the form */ 
	//OpenWindow("/rmcquote/quoteme.php?" + sPartNo + "&type=" + type);
	window.location.href = "/rmcquote/quoteme.php?" + sPartNo + "&type=" + type;
	
	/*Comment the following line of code to turn on passing the "type" parameter to the form */
	//OpenWindow("/rmcquote/quoteme.shtml?" + sPartNo);
}

function InitCards()
{
	var i;
	
	lyrWrt("instructions", DEFAULT_INSTRUCTIONS);
	
	for (i = 0; i < arrSlotCode.length; i++)
		lyrWrt("imgslot" + i, "<img src=\"" + GetCardInfo(arrSlotCode[i], CARD_IMAGE) + "\" border=\"0\" width=\"37\" height=\"220\">");
	lyrWrt("partno", GetPartNo());
	
}

function ResetAppControls()
{
	//Set Text Area to nothing
	$('#txtDescription').text("");
	
	//Reset Pressure Check Box to unchecked
	$('#chkPressure').attr('checked', false);
	
	//Reset Control Loop Text Box
	$('#txtControlLoop').text("1 ms");
}

function OpenWindow(url,name)
{
	var newwindow;
	newwindow = window.open(url, name);
	newwindow.focus();
}

/************************************************************************************
*Writes out information using the correct syntax for the specific browser
************************************************************************************/
function lyrWrt(element,text) 
{	
	document.getElementById(element).innerHTML = text;
}

/************************************************************************************
*Given the code, will search through the card information and return the requested data
************************************************************************************/
function GetCardInfo(code, info)
{
	var i;
	var val;
	val = "";
	for (i = 0; i < arrCardCode.length; i++)
	{
		if (code == arrCardCode[i])
		{
			//Values used instead of variable label to account for Netscape 4.x bug
			switch (info)
			{
				case CARD_WEIGHT:
					val = arrCardWeight[i];	break;
				case CARD_DESCRIPTION:
					val = arrCardDescription[i]; break;
				case CARD_NAME:
					val = arrCardName[i]; break;
				case CARD_IMAGE:
					val = arrCardImage[i]; break;
				case CARD_URL:
					val = arrCardURL[i]; break;
				default:

			}
		}
	
	}
	return (val);

}

/************************************************************************************
*Determine the loop time
************************************************************************************/
function CalcControlLoop()
{
	var i;
	var Slot;
	var cMDT;
	var cSSI;
	var cSO;
	var cSSIStep;
	var cAna12;
	var cAna16;
	var cAnaG;
	var cQuad;
	var cStep;
	var cDIO;
	var cBlanks;
	var cResolver;
	
	Slot = "";
	cMDT = cSSI = cSO= cSSIStep = cAna12 = cResolver = cAna16 = cAnaG = cQuad = cStep = cDIO = cBlanks = cPos = cAux = 0;

	for ( i= 2; i < arrSlotCode.length; i++ )
	{
	
		Slot = arrSlotCode[i];
		switch(Slot)
		{
			case "M":
				cMDT++; break
			case "S":
				cSSI++; break;
			case "SO":
				cSO++; break;
			case "SST":
				cSSIStep++; break;
			case "R":
				cResolver++; break;
			case "A":
				cAna12++; break;
			case "H":
				cAna16++; break;
			case "G":
				cAnaG++; break;
			case "Q":
				cQuad++; break;
			case "QST":
				cStep++; break;
			case "D":
				cDIO++; break;
			case "BLANK":
				cBlanks++
		}
	}

	// Determine the loop time.	Begin by counting the
	//	number of position and auxiliary axes.
	cPos= cMDT*2 + cSSI*2 + cSO*2 + cSSIStep*2 + cQuad*2 + cStep*2 + cAna16*2 + cAnaG*2 + cResolver*2;
	cAux= cAna16*2 + cAna12*4;

	LoopTime = 1;
	if (( cPos > 4 )
	||	( cPos == 4 && cAux > 0 )
	||	( cPos == 2 && cAux > 4 ))
	{
		LoopTime= 2;
	}

	return (LoopTime);
}


/************************************************************************************
*Given the slot, displays all the valid card choices
************************************************************************************/
function DisplayCardChoices(slot)
{
	var val;
	var SlotHRef;
	var j;
	var i;
	var iLowSlot;
	var iHighSlot;
	var bDIODisable;
				
	bDIODisable = false;

	//Set slot pointer
	SetSlotPointer(slot, -1)

	//Alternate card name exception for DI/O
	if ("DI/O" == arrSlotCode[slot])
		lyrWrt("txtName",altDIOCardName );
	else
		lyrWrt("txtName",GetCardInfo(arrSlotCode[slot], CARD_NAME));


	//document.forms[0].txtDescription.value = GetCardInfo(arrSlotCode[slot], CARD_DESCRIPTION); 
	$('txtDescription').text(GetCardInfo(arrSlotCode[slot], CARD_DESCRIPTION));

	lyrWrt("moreinfo", " ");
	
	if ("" != GetCardInfo(arrSlotCode[slot], CARD_URL))
		lyrWrt("moreinfo", "<a href=\"javascript:OpenWindow('" + GetCardInfo(arrSlotCode[slot], CARD_URL) + "','moreinfo');\">Get more info</a>");

	//Update instructions to list of cards
	//The following code is for demo.	
	//Intended to be replaced with more dynamic information
	val = "Choose a module from the following list:";
	val += "<div style='margin-left: 5px;'>";

	switch (slot)
	{
		case "0":
			iLowSlot = 2;
			iHighSlot = 6;
			break;
			
		//CPU slot
		case "1":
			iLowSlot = 18;
			iHighSlot = 19;
			break;
			
		case "2":
			iLowSlot = 7;
			iHighSlot = 15;
			break;

		default:
			iLowSlot = 7;
			iHighSlot = 17;
	}

	bDIODisable = CheckDIOCardOccurances(slot);
	
	
	for (i = iLowSlot; i <= iHighSlot; i++)
	{
		if ("D" == arrCardCode[i])
		{
			//If the DI/O card occurs, then it's no longer a valid option -- Disable the selection of the 
			//DI/O card in slots 4-6.
			if (!bDIODisable)
			{
				SlotHRef = "javascript:InsertCard(" + slot + ", \"" + arrCardCode[i] + "\");";
				val += "<a href='" + SlotHRef + "'>" + GetCardInfo(arrCardCode[i], CARD_NAME) + "</a><br/>";
			}
			else	//Display but do not make DI/O an available selection
			{
				val += GetCardInfo(arrCardCode[i], CARD_NAME) + "<br/>";
			}
		}
		//Rule check for RMC100CPU.	If not available, then disable, vs. hide.
		else if (!CPURuleCheck() && ("RMC101" == arrCardCode[i]) ) 
		{
			val += GetCardInfo(arrCardCode[i], CARD_NAME) + "<br/>";
		}
		else if ("na" != arrCardCode[i])
		{
			SlotHRef = "javascript:InsertCard(" + slot + ", \"" + arrCardCode[i] + "\");";
			val += "<a href='" + SlotHRef + "'>" + GetCardInfo(arrCardCode[i], CARD_NAME) + "</a><br/>";
		}				
	}
	
	//Display BLANK for left-most slot
	if(slot == 0)
	{
		SlotHRef = "javascript:InsertCard(" + slot + ", \"" + arrCardCode[21] + "\");";
		val += "<a href='" + SlotHRef + "'>" + GetCardInfo(arrCardCode[21], CARD_NAME) + "</a><br/>";
	}
	
	// Show the blank cover on any expansion slot
	if(slot > 1)
	{
		SlotHRef = "javascript:InsertCard(" + slot + ", \"" + arrCardCode[22] + "\");";
		val += "<a href='" + SlotHRef + "'>" + GetCardInfo(arrCardCode[22], CARD_NAME) + "</a><br/>";
	}
	
	//Write out "Return to General Instructions" link at bottom
	SlotHRef = "javascript:InsertCard(" + slot + ", \"" + CANCEL_SELECTION + "\");";
	val += "<br><a href='" + SlotHRef + "'>" + CANCEL_SELECTION + "</a><br/>";
	
	//Write out "Delete: link at bottom when required
	if (arrSlotCode[slot] != arrCardCode[20] && 
		 (slot > 2) )
	{
		SlotHRef = "javascript:InsertCard(" + slot + ", \"" + DELETE_CARD + "\");";
		val += "<a href='" + SlotHRef + "'>" + DELETE_CARD + "</a><br/>";
	}
	val += "</div>";
	lyrWrt("instructions", val);
}

/*******************************************************************************************
Rules for display of D (DI/O)
	1.	Can only occur in slot 1
	2.	Can only occur once between slot 4 and 6
********************************************************************************************/

function CheckDIOCardOccurances(slot)
{
	var i;
	var bOccured;

	bOccured = false;
	
	for (i = 3; i < arrSlotCode.length; i++)
	{
		if ("D" == arrSlotCode[i])
			bOccured = true;
	}
	return (bOccured);
}

/************************************************************************************
*CPU rule checking to allow or not allow the RMC101
************************************************************************************/
function CPURuleCheck()
{
	var i;
	var bFoundAnalog;
	
	bFoundAnalog = false;
	
	//Check for an analog card (A, G, H).	If so, then enable the RMC101
	for (i = 0; i < arrSlotCode.length; i++)
	{
		if (arrSlotCode[i] == "A" ||
			arrSlotCode[i] == "G" ||
			arrSlotCode[i] == "H")
		{
			bFoundAnalog = true;
		}
	}
	if (true == bFoundAnalog)
	{
		$('#chkPressure').attr('disabled', false);
	}
	else
	{
		//Since no analog, then slot 2 to RMC100
		$('#chkPressure').attr('disabled', true);
		$('#chkPressure').attr('checked', false);
		arrSlotCode[1] = arrCardCode[18];
	}
	
	return (bFoundAnalog);
}

/************************************************************************************
*Moves the slot pointer
************************************************************************************/
function SetSlotPointer(slot,iNewCard)
{
	for (i = 0; i < arrSlotCode.length; i++)
	{
			lyrWrt("slotpointer" + i, "<img src=/rmcquote/images/common/emptyslotarrow.gif>");
	}
	
	//Set the correct if not -1.	-1 means to just reset the pointers, but do not
	//set the arrow position.
	if ((-1 != slot) && (-1 != iNewCard))
		lyrWrt("slotpointer" + slot, "<img src=/rmcquote/images/common/slotarrow.gif>");
	else 
		if (-1 == iNewCard)
			lyrWrt("slotpointer" + slot, "<img src=/rmcquote/images/common/slotarrow.gif>");

	//Display Get More Info URL
	if ("" != GetCardInfo(arrSlotCode[slot], CARD_URL))
		lyrWrt("moreinfo", "<a href=\"javascript:OpenWindow('" + GetCardInfo(arrSlotCode[slot], CARD_URL) + "','moreinfo');\">Get more info</a>");
}

/************************************************************************************
*Performs act of inserting the new card and applying all business rules.
************************************************************************************/
function InsertCard(slot, iNewCard)
{
	//Cancel choice selection and set text back to instructions.
	if (iNewCard == CANCEL_SELECTION)
	{
		lyrWrt("instructions", DEFAULT_INSTRUCTIONS);
		return;
	}				
	else if (iNewCard == DELETE_CARD) //if "DELETE", then set to choice image
	{
		iNewCard = arrCardCode[20];				
	}

	arrSlotCode[slot] = iNewCard;
	lyrWrt("imgslot" + slot, "<img src=\"" + GetCardInfo(arrSlotCode[slot], CARD_IMAGE) + "\" border=\"0\" width=\"37\" height=\"220\">");
	
	//Alternate card name exception for DI/O
	if ("DI/O" == iNewCard)
		lyrWrt("txtName",altDIOCardName );
	else
		lyrWrt("txtName",GetCardInfo(iNewCard, CARD_NAME));
	
	$('#txtDescription').text(GetCardInfo(iNewCard, CARD_DESCRIPTION));

	//If the next slot is "BLANK", then set to card choice message
	if (slot != (arrSlotCode.length - 1) )
		if ( arrSlotCode[slot + 1] == "BLANK" )
		{
			arrSlotCode[slot + 1] == arrCardCode[19];
			lyrWrt("imgslot" + i, "<img src=\"" + GetCardInfo(arrSlotCode[19], CARD_IMAGE) + "\" border=\"0\">");
		}
	
	//If slot 2 and RMC101 is selected, then check chkPressure
	if ( 1 == slot )
	{
		if (arrSlotCode[1] == arrCardCode[19])
			$('#chkPressure').attr('checked', true);
		else
			$('#chkPressure').attr('checked', false);
	}

	iControlLoop = CalcControlLoop();
	$('#txtControlLoop').text(iControlLoop + " ms");
	
	CPURuleCheck();
	ResortSlots(slot, iNewCard);
	lyrWrt("partno", GetPartNo());

	//The slot has been updated.	Revert back to the default instructions
	lyrWrt("instructions", DEFAULT_INSTRUCTIONS);
}

/************************************************************************************
*Generates the part number
************************************************************************************/
function GetPartNo()
{
	var i;
	var Slot;
	var cENET
	var cMDT;
	var cSSI;
	var cSO;
	var cSSIStep;
	var cAna12;
	var cAna16;
	var cAnaG;
	var cQuad;
	var cStep;
	var cDIO;
	var cBlanks;
	var cBl;
	var cResolver;
	var sPartNo;

	sPartNo = "";
	Slot = "";
	cMDT = cSSI = cSO = cSSIStep = cAna12 = cAna16 = cAnaG = cQuad = cStep = cDIO = cBlanks = cResolver = cPos = cAux = cBl= 0;

	for ( i= 2; i < arrSlotCode.length; i++ )
	{
	
		Slot = arrSlotCode[i];
		switch(Slot)
		{
			case "M":
				cMDT++; break
			case "S":
				cSSI++; break;
			case "SO":
				cSO++; break;
			case "SST":
				cSSIStep++; break;
			case "A":
				cAna12++; break;
			case "H":
				cAna16++; break;
			case "G":
				cAnaG++; break;
			case "Q":
				cQuad++; break;
			case "QST":
				cStep++; break;
			case "R":
				cResolver++; break;
			case "D":
				cDIO++; break;
			case "BLANK":
				cBlanks++; break;
			case "BL":
				cBl++; break;
		}
	}

	//Create and display part no
	sPartNo += arrSlotCode[1];

	if (cMDT > 0) sPartNo += "-M" + cMDT;
	if (cSSI> 0) sPartNo += "-S" + cSSI;
	if (cSO> 0) sPartNo += "-SO" + cSO;
	if (cSSIStep > 0) sPartNo += "-SST" + cSSIStep;
	if (cResolver > 0) sPartNo += "-R" + cResolver;
	if (cQuad > 0) sPartNo += "-Q" + cQuad;
	if (cStep > 0) sPartNo += "-QST" + cStep;
	if (cAna16 > 0) sPartNo += "-H" + cAna16;
	if (cAnaG > 0) sPartNo += "-G" + cAnaG;
	if (cAna12 > 0) sPartNo += "-A" + cAna12;
	if (cDIO > 0) sPartNo += "-D" + cDIO;
	if (cBl > 0) sPartNo += "-BL" + cBl;
	sPartNo += "-" + arrSlotCode[0];

	return (sPartNo);	
}

/************************************************************************************
*Rearanges the cards into the correct order.
************************************************************************************/
function ResortSlots(slot, sNewCard)
{
	var i;
	var j;
	var sCurrentCard;
	var iSelectedCardLoc = slot;

	for (i = 2; i < arrSlotCode.length; i++)
	{
		for (j = i; j < arrSlotCode.length; j++)
		{
			sCurrentCard = arrSlotCode[i];

			// Compare the weights of the cards (positioning weight) and shift, if necessary.
			if (GetCardInfo(sCurrentCard, CARD_WEIGHT) > GetCardInfo(arrSlotCode[j], CARD_WEIGHT) )	
			{

				//Remember the position of the card we just added
				if (i == iSelectedCardLoc)
					iSelectedCardLoc = j
				else if (j == iSelectedCardLoc)
					iSelectedCardLoc = i

				//Switch the cards
				arrSlotCode[i] = arrSlotCode[j];
				arrSlotCode[j] = sCurrentCard;
			}
		}
	}
	//Apply array to actual slots
	for (i = 2; i < arrSlotCode.length; i++)
		lyrWrt("imgslot" + i, "<img src=\"" + GetCardInfo(arrSlotCode[i], CARD_IMAGE) + "\" border=\"0\" width=\"37\" height=\"220\">");
	
	//Move the pointer to the recently added cards location
	SetSlotPointer(iSelectedCardLoc,0);
}

/************************************************************************************
*Based on the checkbox for Pressure/Force, select the RMC100 or 101
************************************************************************************/
function ChooseCPU()
{
	if (true == $('#chkPressure').attr('checked'))
		InsertCard(1, arrCardCode[19]);
	else
		InsertCard(1, arrCardCode[18]);

	SetSlotPointer(1, 0);
}
