var CurrentSortColor="LightGreen";
function SortNumber(a,b) 
{
	var jsSortType=parent.Config.SortType;
	var i=jsSortType;var tempA=0;var tempB=0;
	try
	{
		if (!isNaN(parseFloat(a[i]))) tempA=parseFloat(a[i]);
		if (!isNaN(parseFloat(b[i]))) tempB=parseFloat(b[i]);
	}
	catch(e)
	{
		alert('An Error occurred: ' + e.message + '\nLocation: SortNumber');return 0;
	}
	return parseFloat(tempA-tempB);
}
function SortAlpha(a,b)
{
	try
	{
		var jsSortType=parent.Config.SortType;
		var i=jsSortType;var firstString=a[i].toString().toLowerCase();var secondString=b[i].toString().toLowerCase();
		var firstLength=parseInt(firstString.length);var secondLength=parseInt(secondString.length);
		var aFirstChar;var aSecondChar;
		for (var x=0;x<Math.min(firstLength,secondLength);x++) 
		{
			aFirstChar=parseInt(firstString.charCodeAt(x));
			aSecondChar=parseInt(secondString.charCodeAt(x));
			if (aFirstChar!=aSecondChar) return parseInt(aFirstChar-aSecondChar);
		}
		if (firstLength>secondLength) return 1;
		else if (firstLength<secondLength) return -1;
		else return 0;
	}
	catch(e)
	{
		alert('An Error occurred: ' + e.message + '\nLocation: SortAlpha');
	}
	return 0;
}
function SortItems(type) 
{
	try
	{
		window.status="Sorting, please wait...";
		var oldtype=parent.Config.SortType;
		var Items=parent.Data.Items;
		with (parent) 
		{
			Config.SortType=type.toLowerCase();
			if (Config.SortType!=oldtype) Config.SortDirection="asc";
			else 
			{
				Config.SortDirection=(Config.SortDirection=="asc") ? "des" : "asc";
			}
			if (type=="dimensiona" || type=="dimensionb") Items.sort(SortNumber);
			else Items.sort(SortAlpha);
			if (Config.SortDirection=="des") Items.reverse();
		}
		UI.Middle.Show();
		window.status="Page Load complete, please proceed.";
	}
	catch(e)
	{
		alert('An Error occurred: ' + e.message + '\nLocation: SortItems');
	}
	return true;
}
function DoSortDisplay(alphaOrNumber)
{
	try
	{
		var jsSortDirection=parent.Config.SortDirection;		
		var theHTML="";var asc="(A..Z)";var desc="(Z..A)";
		if (alphaOrNumber.toLowerCase()!='alpha') 
		{	
			asc="(1..3)";desc="(3..1)";
		}
		theHTML+="<font size='-1' color='" + CurrentSortColor + "'> ";
		theHTML+=(jsSortDirection=="asc") ? asc : desc;
		theHTML+="</font>";
	}
	catch(e)
	{
		alert('An Error occurred: ' + e.message + '\nLocation: DoSortDisplay');
	}
	return theHTML;
}

var UI=parent.UI;
UI.Middle=new Object();
UI.Middle.Show=function()
	{
		try
		{
			var theHTML="";
			UI.Middle.Clear();
			var Items=parent.Data.Items;
			var jsSelectedItem=parent.Item.SelectedID; 
			if (arguments.length>0) jsSelectedItem=arguments[0];
			if (parseInt(jsSelectedItem)==0) divFramedSRQ.innerHTML="";
			else if (jsSelectedItem=='clear') 
			{
				UI.Middle.Clear();
				return true;
			}
			window.status="Populating Search Results Qualifiers, please wait...";
			var iItems=parseInt(Items.length);
			if (iItems>0) 
			{
				theHTML+="<table cellpadding='3' cellspacing='0' border='1' width='100%' align='center' bgcolor='lightyellow'>";
				theHTML+="<thead width='100%'>";
				theHTML+=UI.Middle.TH("ItemID") + UI.Middle.TH("Description") + UI.Middle.TH("Brand") + UI.Middle.TH("ManufacturerID") + UI.Middle.TH("DimensionA") + UI.Middle.TH("DimensionB");
				theHTML+="</thead>";
				for(var x=0;x<Items.length;x++) 
				{
					var aItem=parent.Data.Items[x];	
					if (aItem.itemid!=0 && aItem.length!=0)
					{
						theHTML+="<tr bgColor='";
						theHTML+=(jsSelectedItem==aItem.itemid) ? SelectedItemColor + "' " : UnselectedItemColor + "' title='Click here to see detail about this item' ";
						theHTML+="onclick=\"UI.Bottom.Show('" + aItem.itemid + "');\"";
						theHTML+=">";					
						theHTML+=UI.Middle.TD("BUTTON",aItem);
						theHTML+=UI.Middle.TD(aItem.itemid,aItem);
						theHTML+=UI.Middle.TD(aItem.description,aItem);
						theHTML+=UI.Middle.TD(aItem.brand,aItem);
						theHTML+=UI.Middle.TD(aItem.manufacturerid,aItem);
						theHTML+=UI.Middle.TD(Nearest16th(parseFloat(aItem.dimensiona)),aItem);
						theHTML+=UI.Middle.TD(Nearest16th(parseFloat(aItem.dimensionb)),aItem);					
						theHTML+="</tr>";
					}
					delete aItem;
				}
				theHTML+="</table>";
			} 
			else theHTML="<div align='center'>No Results Found</div>";
			divFramedSRQ.innerHTML=theHTML;
			window.status="Page Load complete, please proceed."
		}
		catch(e)
		{
			alert('An Error occurred: ' + e.message + '\nLocation: UI.Middle.Show');
		}
		return true;
	}
UI.Middle.Clear=function()
	{
		try
		{
			divFramedSRQ.innerHTML="";
			UI.Bottom.Clear();
		}
		catch(e)
		{
			alert('An Error occurred: ' + e.message + '\nLocation: UI.Middle.Clear');
		}
		return true;
	}
UI.Middle.TH=function(name)
	{
		var result="";
		try
		{
			var sortType=parent.Config.SortType;
			var lowerCaseName=name.toString().toLowerCase();
			var moreThanOne=(parent.Data.Items.length > 1) ? true : false;
			var isSortType=(sortType==lowerCaseName) ? true : false;
			var isBoth=(isSortType && moreThanOne) ? true : false;
			var alphaOrNumericSort=(sortType=="dimensiona" || sortType=="dimensionb") ? "numeric" : "alpha";
			var title="";
			switch(lowerCaseName)
			{
				case "itemid":
					title="Our Item #";break;
				case "description":
					title="Description";break;
				case "brand":
					title="Brand";break;
				case "manufacturerid":
					title="Manufacturer #";break;
				case "dimensiona":
					title="Dimension A";break;
				case "dimensionb":
					title="Dimension B";break;
			}
			result+="<TH Title='Click here to sort this column.' vAlign='bottom'";
			result+=(lowerCaseName=="itemid") ? " colspan=2" : "";
			result+=(isBoth) ? (" bgColor='" + SortedColumnHeaderColor + "'") : "";
			result+=(moreThanOne) ? (" onClick=\"SortItems('" + lowerCaseName + "');\"") : "";
			result+=">" + title;
			result+=(isBoth) ? DoSortDisplay(alphaOrNumericSort) : "";
			result+=(moreThanOne) ? (" <input class='SortButton' type=button id=btnSort" + name + " value='S' onClick=\"SortItems('" + lowerCaseName + "');\">") : "";
			result+="</TH>";
		}
		catch(e)
		{
			alert('An Error occurred: ' + e.message + '\nLocation: UI.DoTH');
		}
		return result;
	}
UI.Middle.TD=function(innerText,item)
	{
		var result="";
		try
		{			
			var selectedItemID=parent.Item.SelectedID;
			var isButton=(innerText=="BUTTON") ? true : false;
			var isSelectedID=(selectedItemID==item.itemid) ? true : false;
			result+="<TD";
			result+=(!isButton) ? " Class='SRQText" : " Align='Center";
			result+="' bgColor='";
			result+=(isSelectedID) ? SelectedItemColor : UnselectedItemColor;
			result+="'>";
			if (isButton) 
				result+="<input Class='DisplayButton' type='button' id='btnDisplay" + item.itemid + "' name='btnDisplay" + item.itemid + "' onclick=\"UI.Bottom.Show('" + item.itemid + "');\">";
			else
			{
				result+="&nbsp;<font Color='";
				result+=(isSelectedID) ? SelectedItemFontColor : UnselectedItemFontColor;
				result+="'>" + innerText + "</font>";
			}
			result+="</TD>";
		}
		catch(e)
		{
			alert('An Error occurred: ' + e.message + '\nLocation: UI.DoTD');
		}
		return result;
	}					

parent.UI.Middle=UI.Middle;

UI.Bottom=new Object();
UI.Bottom.Clear=function()
	{
		parent.divSPI.innerHTML="";
	}
UI.Bottom.Show=function(itemid)
{
	var jsSelectedItem=itemid;
	var td=parent.UI.TD;
	parent.Item.SelectedID=itemid;
	UI.Middle.Show();
	var Items=parent.Data.Items;
	var theDiv=parent.divSPI;
	var theHTML="";var theImageSrc="";
	var theItemID="";var theRetailPrice="0";var theDescription="";var thePDF="";
	var theDimensionA="";var theDimensionB="";var extra="";
	var IsMatchedYet=0;		
	try
	{
		window.status="Populating Item # " + itemid + " details, please wait...";
		for (var x=0;x<Items.length;x++) 
		{
			if (Items[x].itemid==itemid) 
			{	
				var txt=Items[x].description;
				if (Items[x].imagesrc.length!=0) 
				{
					theImageSrc+="<img ";
					theImageSrc+="src='" + parent.Config.GraphicPathPrefix + Items[x].imagesrc + "'";
					theImageSrc+="/>";
					IsMatchedYet=1;
				} 
				else if (txt.length!=0) theImageSrc="No picture for...<br>" + txt;
				else theImageSrc="";
				theItemID=itemid.toString();
				theRetailPrice=Items[x].retailprice;
				if (parseFloat(theRetailPrice) > 0) theRetailPrice="$" + theRetailPrice;
				theDescription=Items[x].description;
				theDimensionA=parseFloat(Items[x].dimensiona);
				theDimensionB=parseFloat(Items[x].dimensionb);
				thePDF=Items[x].relateditemsfile;
			}
			if (IsMatchedYet) break;
		}
		theHTML+="<tr>";
		theHTML+="<td colspan='20' width='100%' bgcolor='#c6d6b7' align='left'>";
		theHTML+="<table cellpadding='3' cellspacing='0' border='0' width='100%' align='center'>";
		theHTML+="<tr>";
		extra="rowspan='" + ((CartEnabled) ? "4" : "3") + "' valign='Top'";
		theHTML+=td("10%","L",SRQTitle,"2","",extra);
		extra="rowspan='" + ((CartEnabled) ? "4" : "3") + "' valign='Center'";
		theHTML+=td("25%","C",theImageSrc,"5","",extra);
		theHTML+=td("20%","R","Our Item #:","4");
		theHTML+=td("15%","L","<input type='text' id='txtPartNumber' size=10 readonly value='" + theItemID + "'>","3");
		theHTML+=td("10%","R","Price:","2");		
		theHTML+="<td colspan='";
		theHTML+=(thePDF!="") ? "2' width='10%" : "4' width='20%";
		theHTML+="' align='left'>";
		theHTML+="<input type='text' id='txtRetailPrice' size=10 readonly value='";
		if (theRetailPrice != "" && theRetailPrice != void(0)) theHTML+=FormatCur(theRetailPrice);
		theHTML+="'>";
		theHTML+="</td>";
		if(thePDF!="")
		{
			theHTML+="<td colspan='2' width='10%' align='center'>";
			if(thePDF.toUpperCase()=="CHRIST.PDF")
				theHTML+="<img onClick=\"window.open('http://www.optimusFAP.com/parts/" + thePDF + "','','toolbar=false,menubar=false,resizable=true');\" onMouseOver=\"document.body.style.cursor='pointer';\" onMouseOut=\"document.body.style.cursor='default';\" Alt='Click here to view all parts related to this part.' Title='Click here to view all parts related to this part.' src='" + parent.Config.RelatedItemsGraphic + "'>";
			else
				theHTML+="<img onClick=\"window.open('" + parent.Config.RelatedItemsPathPrefix + thePDF + "','','toolbar=false,menubar=false,resizable=true');\" onMouseOver=\"document.body.style.cursor='pointer';\" onMouseOut=\"document.body.style.cursor='default';\" Alt='Click here to view all parts related to this part.' Title='Click here to view all parts related to this part.' src='" + parent.Config.RelatedItemsGraphic + "'>";
			theHTML+="</td>";
		}
		theHTML+="</tr>";
		theHTML+="<tr>";
		theHTML+=td("20%","R","Description:","4");
		theHTML+=td("50%","L","<textarea rows=2 cols=34 readonly id='txtDescription'>" + theDescription + "</textarea>","10");
		theHTML+="</tr>";		
		theHTML+="<tr>";
		theHTML+=td("20%","R","Dimension A:","4");
		theHTML+=td("50%","L","<input type='text' id='txtDimensionA' size=10 readonly value='" + Nearest16th(theDimensionA) + "'>&nbsp;&nbsp;&nbsp;Dimension B:&nbsp;&nbsp;&nbsp;<input type='text' id='txtDimensionB' size=10 readonly value='" + Nearest16th(theDimensionB) + "'>","10");
		theHTML+="</tr>";
		if (CartEnabled)
		{
			theHTML+="<tr>";		
			theHTML+=td("20%","R","Quantity:");
			theHTML+="<td colspan='10' width='50%' align='Left'>";
			theHTML+="<input type='Text' size=5 name='txtQuantity' id='txtQuantity' value='1'/>";
			theHTML+="&nbsp;&nbsp;&nbsp;";
			if (CartButtonURL.length>0)
				theHTML+="<img OnMouseOver=\"document.body.style.cursor='hand';\" OnMouseOut=\"document.body.style.cursor='default';\" src=\"" + CartButtonURL + "\" OnClick=\"AddToCart();\" Title=\"Click here to add this item to your cart.\"/>"
			else
				theHTML+="<input value=\"Add To Cart\" type=\"button\" OnClick=\"AddToCart('" + BasketID + "','" + theItemID + "','" + theDescription.replace("'","\\'") + "',frmMain.txtQuantity.value,'" + theRetailPrice + "','" + Nearest16th(theDimensionA) + "','" + Nearest16th(theDimensionB) + "');\" id=\"btnAddToCart\" name=\"btnAddToCart\" Title=\"Click here to add this item to your cart.\">";
			theHTML+="</td>";
			theHTML+="</tr>";
		}
		theHTML+="</table>";
		theHTML+="</td>";
		theHTML+="</tr>";
		theHTML+="<tr>";
		theHTML+=td("100%","L","&nbsp;","20","#c6d6b7");
		theHTML+="</tr>";
		theDiv.innerHTML=theHTML;
		parent.divEndOfDocument.scrollIntoView();
		delete theHTML;delete theDiv;
		window.status="Viewing details for Item " + theItemID + "     " + theDescription;
		window.defaultStatus=window.status;
	}
	catch(e)
	{
		if (theDiv.innerHTML.toUpperCase()=="PICTURE FAILED TO LOAD!") return true;
		else alert('An Error occurred: ' + e.message + '\nLocation: UI.Bottom.Show');
	}
	return true;
}
parent.UI.Bottom=UI.Bottom;	

function FormatCur(amt)
{
	try
	{
		var decLoc=amt.indexOf(".") + 1;
		if (decLoc!=0 && (amt.length-decLoc) < 2)
		{
			if ((amt.length-decLoc)==1) amt=amt + "0";
			else if ((amt.length-decLoc)==2) amt=amt + "00";
		}
	}
	catch(e)
	{
		alert('An Error occurred: ' + e.message + '\nLocation: FormatCur');
	}
	return amt;
}

function Nearest16th(amt) 
{
	var theResult=0;
	try
	{
		var theOriginalAmount=amt;var the16th=.0625;
		var theInteger=parseInt(theOriginalAmount);var theDecimal=theOriginalAmount-theInteger;
		var theNearest16th=Math.round(theDecimal/the16th);
		if (theInteger==0) theInteger="";
		switch (theNearest16th)	
		{
			case 0:
				theResult=theInteger;break;
			case 1:
				theResult=theInteger + " " + theNearest16th + "/16";break;
			case 2:
				theResult=theInteger + " 1/8";break;
			case 3:
				theResult=theInteger + " " + theNearest16th + "/16";break;
			case 4:
				theResult=theInteger + " 1/4";break;
			case 5:
				theResult=theInteger + " " + theNearest16th + "/16";break;
			case 6:
				theResult=theInteger + " 3/8";break;
			case 7:
				theResult=theInteger + " " + theNearest16th + "/16";break;
			case 8:
				theResult=theInteger + " 1/2";break;
			case 9:
				theResult=theInteger + " " + theNearest16th + "/16";break;
			case 10:
				theResult=theInteger + " 5/8";break;
			case 11:
				theResult=theInteger + " " + theNearest16th + "/16";break;
			case 12:
				theResult=theInteger + " 3/4";break;
			case 13:
				theResult=theInteger + " " + theNearest16th + "/16";break;
			case 14:
				theResult=theInteger + " 7/8";break;
			case 15:
				theResult=theInteger + " " + theNearest16th + "/16";break;
			case 16:
				theResult=theInteger + 1;break;
		}
		if (theResult==0) theResult="N/A";
	}
	catch(e)
	{
		alert('An Error occurred: ' + e.message + '\nLocation: Nearest16th');
	}
	return theResult;
}

//var FormatCur=parent.FormatCur;
//var Nearest16th=parent.Nearest16th;
	
