var defaultSort, defaultContentsFormat, contentsFormat, tableWidth, numColumns, numPerPage, numPages, contentsElements, showDefaultSort,
    detailedSortInfo, showPagingModes, viewAll, sortBy, sortHighLow, pageLinkPrefix, rowpad, columnpad, storename, columnSeparatorColor,
    rowSeparatorColor, listModeBG1, listModeBG2, sortLinkSeparator, modeLinkSeparator, pageLinkSeparator, globalPageNum, globalViewallthistime,
    currentItem, currentItemImage, numArrowDivs, showListMode, addToCartImage, showBrandSort, strikePrice, showWrapMode, showQuantity, listModeShowImages;
var pageItems = new Array();
var oldSortBy = "";

function pagingItem(id, code, name, img, listimg, price, saleprice, options, abstr, orderable, brand, mappricing, linktype, link,extra) {
	if (!window.items) window.items = new Array();
	this.id=id.toLowerCase();
	this.code=code;
	this.name=name;
	this.img=img;
	this.listimg=listimg;
	this.price=round_decimals(price,2);
	this.saleprice=round_decimals(saleprice,2);
	if(saleprice) this.useprice=round_decimals(saleprice,2); else this.useprice=round_decimals(price,2);
	this.options=options;
	this.abstr=abstr;
	this.orderable=orderable;
	this.brand=brand;
	this.mappricing=mappricing;
	this.linktype=linktype;
	this.link=link;
	this.extra=extra;
	this.sortby="name";
	this.defaultsort=window.items.length;
	window.items[window.items.length] = this;
}

/* round_decimals, pad_with_zeros, FIND, pagingSegment, sortArray */
function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {
    var value_string = rounded_value.toString()
    var decimal_location = value_string.indexOf(".")
    if (decimal_location == -1) {
        decimal_part_length = 0
        value_string += decimal_places > 0 ? "." : ""
    }
    else {
        decimal_part_length = value_string.length - decimal_location - 1
    }
    var pad_total = decimal_places - decimal_part_length
    if (pad_total > 0) {
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}

function FIND(item) {
	if (document.getElementById) return(document.getElementById(item));
	if (document.all) return(document.all[item]);
	return(false);
};

function pagingSegment(theArray,pageNum) {
	var newArray = new Array();
	for(i=(pageNum-1)*numPerPage; i<pageNum*numPerPage; i++) {
		newArray[i - (pageNum-1)*numPerPage] = theArray[i];
	}
	return newArray;
};

function sortArray(theArray, loBound, hiBound){
	var pivot, loSwap, hiSwap, temp;

	// Two items to sort
	if (hiBound - loBound == 1)
	{
		if (theArray[loBound].sortby > theArray[hiBound].sortby)
		{
			temp = theArray[loBound];
			theArray[loBound] = theArray[hiBound];
			theArray[hiBound] = temp;
		}
		return;
	}

	// Three or more items to sort
	pivot = theArray[parseInt((loBound + hiBound) / 2)];
	theArray[parseInt((loBound + hiBound) / 2)] = theArray[loBound];
	theArray[loBound] = pivot;
	loSwap = loBound + 1;
	hiSwap = hiBound;

	do {
		while (loSwap <= hiSwap && theArray[loSwap].sortby <= pivot.sortby)
			loSwap++;
		while (theArray[hiSwap].sortby > pivot.sortby)
			hiSwap--;
		if (loSwap < hiSwap)
		{
			temp = theArray[loSwap];
			theArray[loSwap] = theArray[hiSwap];
			theArray[hiSwap] = temp;
		}
	} while (loSwap < hiSwap);

	theArray[loBound] = theArray[hiSwap];
	theArray[hiSwap] = pivot;

	// 2 or more items in first section
	if (loBound < hiSwap - 1)
		sortArray(theArray, loBound, hiSwap - 1);

	// 2 or more items in second section
	if (hiSwap + 1 < hiBound)
		sortArray(theArray, hiSwap + 1, hiBound);
};

/* changeClass, writePageLinks, writeItemXofX, writePageXofX */
function changeClass(which,theclass) {
	which.className = theclass;	
}

function writePageLinks( viewallThisTime, pageNum) {
	if(numPages > 1) {
		var pageLinks = "<span id=pagelinks>" + pageLinkPrefix;
		for(i=1; i<=numPages; i++) {
			if(i == pageNum && !viewallThisTime)
				pageLinks += "<span class=chosen>" + i + "</span>";
			else
				pageLinks += "<a href=#top onclick=javascript:writePage(" + i + ",false)>" + i + "</a>";
			if(i % 18 == 0)
				pageLinks += "<br>";
			else
		        	if(i < numPages)
		        		pageLinks += "<span class=sep>" + pageLinkSeparator + "</span>";
		}
		if(viewAll) {
			pageLinks += "<span class=sep>" + pageLinkSeparator + "</span>";
			if(viewallThisTime)
				pageLinks += "<span class=chosen>View All</span>";
			else
				pageLinks += "<a href=#top onclick=javascript:changeSortAndPage(sortBy,false,0,true)>View All</a>";
		}
		pageLinks += "</span>";
	
		var thePageDiv = FIND("pagelinks");
		if(thePageDiv) thePageDiv.innerHTML = pageLinks;
		thePageDiv = FIND("pagelinks2");
		if(thePageDiv) thePageDiv.innerHTML = pageLinks;
	}
};

function writeItemXofX(viewallthistime,pageNum) {
	var currentItemLow = ((pageNum - 1) * numPerPage + 1);
	var currentItemHigh = pageNum * numPerPage;
	var numItems = window.items.length;

	if((currentItemLow <= numItems) && (numItems <= currentItemHigh)) {
		currentItemHigh = numItems;
	}
	var theString = "<span class=itemof>";
	if(currentItemLow == currentItemHigh) {
		theString += "showing product " + currentItemLow + " of " + numItems ;
	} else if(viewallthistime) {
		theString += "showing products 1-" + numItems + " of " + numItems ;
	} else {
		theString += "showing products " + currentItemLow + "-" + currentItemHigh + " of " + numItems ;
	}
	theString += "</span>";
	
	var theItemDiv = FIND("itemof");
	if(theItemDiv) theItemDiv.innerHTML = theString;
	theItemDiv = FIND("itemof2");
	if(theItemDiv) theItemDiv.innerHTML = theString;
};

function writePageXofX(viewallthistime,pageNum) {
	var localContent = "<span class=pageof>";
	if(viewallthistime)
		localContent += "Viewing all items";
	else
		localContent += "Page " + pageNum + " of " + numPages;

	//debug
	if(detailedSortInfo) {
		if(sortBy == "default")
			localContent += " (default sort)";
		else {
			localContent += " <span class=detailedsortinfo>(sorted by " + sortBy;
			if(sortHighLow) localContent += " high to low)"; else localContent += " low to high)</span>";
		}
	}

	localContent += "</span>";

	var pageofDiv = FIND("pageof");
	if(pageofDiv) pageofDiv.innerHTML = localContent;
	pageofDiv = FIND("pageof2");
	if(pageofDiv) pageofDiv.innerHTML = localContent;
};

/* writeArrows, writeUpDownArrow */
function writeUpDownArrow(divname) {
	if(divname) {
		localContent = "";
		if(sortHighLow) localContent += " " + upArrow; else localContent += " " + downArrow;
		divname.innerHTML = localContent;
	}
}

function writeArrows(viewallthistime,pageNum) {
	var localContent;
	var leftArrowString = "", rightArrowString = "";
	var upDownArrowDiv  = FIND(sortBy + "arrowdiv");
	var upDownArrowDiv2 = FIND(sortBy + "arrowdiv2");
	if(viewallthistime) {
		writeUpDownArrow(upDownArrowDiv);
		writeUpDownArrow(upDownArrowDiv2);
		leftArrowString += leftArrowG;
		rightArrowString += rightArrowG;
		for(i=1; i<=numArrowDivs; i++) {
			localDiv = FIND("rightArrowDiv" + i);
			if(localDiv) localDiv.innerHTML = rightArrowString;
			localDiv = FIND("leftArrowDiv" + i);
			if(localDiv) localDiv.innerHTML = leftArrowString;
		}
	} else {
		writeUpDownArrow(upDownArrowDiv);
		writeUpDownArrow(upDownArrowDiv2);
		if(numPages == 1) {
			leftArrowString += leftArrowG;
			rightArrowString += rightArrowG;
		} else if(pageNum == 1) {
			leftArrowString += leftArrowG;
			rightArrowString += "<a href='#paging' onClick=javascript:writePage(2);window.location='#paging';return false;>" + rightArrow + "</a>";
		} else if (pageNum == numPages) {
			leftArrowString += "<a href='#paging' onClick=javascript:writePage(" + (numPages - 1) + ");window.location='#paging';return false;>" + leftArrow + "</a>";
			rightArrowString += rightArrowG;
		} else {
			leftArrowString += "<a href='#paging' onClick=javascript:writePage(" + (pageNum - 1) + ");window.location='#paging';return false;>" + leftArrow + "</a>"
			rightArrowString += "<a href='#paging' onClick=javascript:writePage(" + (pageNum + 1) + "); window.location='#paging';return false;>" + rightArrow + "</a>";
		}
		for(i=1; i<=numArrowDivs; i++) {
			localDiv = FIND("rightArrowDiv" + i);
			if(localDiv) localDiv.innerHTML = rightArrowString;
			localDiv = FIND("leftArrowDiv" + i);
			if(localDiv) localDiv.innerHTML = leftArrowString;
		}
	}
};

/* pagingQuantity, pagingAddtoCart, pagingPrice */
function pagingPrice(localItem) {
	strikePrice = true
	var onsale = (localItem.saleprice && localItem.saleprice != 0);
	var usemap = localItem.mappricing;
	var localContent = "";
	if(localItem.price != 0) {
	    if(usemap && !onsale)
	    	localContent += mapPricing(localItem);
	    else {
	            localContent += "<span class=price>" + regularPriceText + " ";
	            if(onsale && strikePrice) localContent += "<s>";
	            localContent += "$" + localItem.price;
	            if(onsale && strikePrice) localContent += "</s>";
	            localContent += "</span><br>";
	    }
	}
	if(onsale && usemap) localContent += mapPricing(localItem);
	if(onsale && !usemap) localContent += "<span class=saleprice>" + salePriceText + " <b>$" + localItem.saleprice + "</b></span><br>";	
	return localContent;
}

function pagingQuantity(localItem) {
	var localContent = "";
	if(localItem.useprice != 0)
		if(localItem.orderable) {
			localContent += "<span class=qtytext>Quantity: ";
			localContent += "<input type=text name=vwquantity size=2 class=qtybox value=1>";
			localContent += "</span><br>";
		} else 
			localContent += "";	
	return localContent;
}

function pagingAddToCart(localItem) {
	var localContent = "";
	if(localItem.useprice != 0 && !localItem.mappricing)
		if(localItem.orderable) {
			localContent += "<form method=post action=http://order.store.yahoo.com/cgi-bin/wg-order?" + storename + ">";
			if(showQuantity) localContent += pagingQuantity(localItem);
			localContent += "<input type=hidden name=vwcatalog value=" + storename + ">";
			localContent += "<input type=hidden name=vwitem value=" + localItem.id + ">";
			if(addToCartImage != "") 
				localContent += addToCartImage;
			else
				localContent += "<input type=submit id=submit value='Add to Cart'>"
			localContent += "</form>";
		} else 
			localContent += "<span class=outofstock>Out of Stock</span>";	
	return localContent;
}

function writeVerticalInfo(currentRow, numRows) {
          var localContent = "";
          localContent += "<tr valign=top>";
          //VERTICAL - LOOP CELLS
          for(currentCol=0; currentCol<numColumns; currentCol++) {
                    localContent += "<td width=" + 100 / numColumns + "% height=100% align=center";
                    if(pageSegment[currentItem]) localContent += " onmouseover=changeClass(this,'vcell-on'); onmouseout=changeClass(this,'vcell'); class=vcell id=vcell" + currentItem;
                    localContent += ">";
                    if(pageSegment[currentItem]) {
                              localItem = pageSegment[currentItem];
                              if(contentsElements.indexOf("quantity")) showQuantity = true;
                              var elementArray = contentsElements.split("/");
                              for(i=0;i<elementArray.length;i++) {
                                        switch(elementArray[i]) {
                                                  case "name":
                                                            if(localItem.name != "") localContent += "<a href=" + ((localItem.linktype) ? localItem.link : localItem.id + '.html') + ">" + localItem.name + "</a><br>";
                                                  break
                                                  case "code":
                                                            if(localItem.code != "") localContent += "<span class=code>" + localItem.code + "</span><br>";
                                                  break
                                                  case "price":
                                                            localContent += pagingPrice(localItem);
                                                  break
                                                  case "abstract":
                                                            if(localItem.abstr != "") localContent += "<span class=abstract>" + localItem.abstr + "</span><br>";
                                                  break
                                                  case "brand":
                                                            if(localItem.brand != "") localContent += "<span class=brand>" + localItem.brand + "</span><br>";
                                                  break
                                                  case "order":
                                                            localContent += pagingAddToCart(localItem);
                                                  break
                                                  case "extra":
                                                           if(localItem.extra != "")  localContent += localItem.extra;
                                                  break
                                                  case "quantity": break
                                                  case "image": break
                                        }
										
                              }
                              currentItem++;
                    } else {
                              localContent += " ";
                    }
                    localContent += "</form></td>";
                    if(currentCol<numColumns-1 && columnpad > 0) 
                              localContent += writeCellPadding(currentItem);
          }
          localContent += "</tr>";
          if((currentRow < (numRows - 1)) && (rowpad > 0))
                    localContent += writeRowPadding((numColumns + (3 * (numColumns - 1))));
          return localContent;
};

function writeVerticalImage(currentRow, numRows) {
          var localContent = "";
          localContent += "<tr valign=bottom>";
          for(currentCol=0; currentCol<numColumns; currentCol++) {
                    localContent += "<td width=" + 100 / numColumns + "% height=100% align=center";
                    if(pageSegment[currentItemImage]) localContent += "  onmouseover=changeClass(FIND('vcell" + currentItemImage + "'),'vcell-on'); onmouseout=changeClass(FIND('vcell" + currentItemImage + "'),'vcell');";
                    localContent += ">";
                    if(pageSegment[currentItemImage]) {
                              localItem = pageSegment[currentItemImage];
                              localContent += "<table cellspacing=0 cellpadding=0><tr><td id=contents-image><a href=" + ((localItem.linktype) ? localItem.link : localItem.id + '.html') + ">" + localItem.img + "</a></td></tr></table>";
                              currentItemImage++;
                    } else {
                              localContent += " ";
                    }
                    localContent += "</td>";
                    if(currentCol<numColumns-1 && columnpad > 0) 
                              localContent += writeCellPadding(currentItemImage);
          }
          localContent += "</tr>";
          return localContent;
};

function writeVertical(pageNum, viewallthistime) {
          var localContent = "";
          var numRows = Math.ceil(pageSegment.length / numColumns);
          currentItem=0; currentItemImage = 0;
          for(currentRow=0; currentRow<numRows; currentRow++) {
                    if(pageSegment[currentItem]) {
                              if(contentsElements.indexOf("image") != -1) 
                                        localContent += writeVerticalImage(currentRow, numRows);
                              localContent += writeVerticalInfo(currentRow, numRows);
                    }
          }
          return localContent;
};

function writeEllInfoCell(currentRow, numRows) {
	var localContent = "";
	localContent += "<td width=" + 100 / numColumns + "% height=100% align=left onmouseover=changeClass(this,'ecell-on'); onmouseout=changeClass(this,'ecell'); class=ecell id=ecell" + currentItem + ">"
	if(pageSegment[currentItem]) {
		localItem = pageSegment[currentItem];
                if(contentsElements.indexOf("quantity")) showQuantity = true;
		var elementArray = contentsElements.split("/");
		for(i=0;i<elementArray.length;i++) {
			switch(elementArray[i]) {
				case "name":
					if(localItem.name != "") localContent += "<a href=" + ((localItem.linktype) ? localItem.link : localItem.id + '.html') + ">" + localItem.name + "</a><br>";
				break
				case "code":
					if(localItem.code != "") localContent += "<span class=code>" + localItem.code + "</span><br>";
				break
				case "price":
					localContent += pagingPrice(localItem);
				break
				case "abstract":
					if(localItem.abstr != "") localContent += "<span class=abstract>" + localItem.abstr + "</span><br>";
				break
				case "brand":
					if(localItem.brand != "") localContent += "<span class=brand>" + localItem.brand + "</span><br>";
				break
				case "order":
	                                localContent += pagingAddToCart(localItem);
				break
                                case "quantity": break
				case "image": break
			}
		}
		currentItem++;
	} else {
		localContent += " ";
	}
	localContent += "</td>";
	return localContent;
};

function writeEllImageCell() {
	var localContent = "";
	localContent += "<td align=center onmouseover=changeClass(FIND('ecell" + currentItem + "'),'ecell-on'); onmouseout=changeClass(FIND('ecell" + currentItem + "'),'ecell');>"
	if(pageSegment[currentItem]) {
		localItem = pageSegment[currentItem];
		localContent += "<a href=" + ((localItem.linktype) ? localItem.link : localItem.id + '.html') + ">" + localItem.img + "</a>";
	} else {
		localContent += " ";
	}
	localContent += "</td>";
	return localContent;
};

function writeEll(pageNum, viewallthistime) {
	var localContent = "";
	var numRows = Math.ceil(pageSegment.length / numColumns);
	currentItem=0; currentItemImage = 0;
	for(currentRow=0; currentRow<numRows; currentRow++) {
		if(pageSegment[currentItem]) {
			localContent += "<tr valign=top>";
			for(currentCol=0; currentCol<numColumns; currentCol++) {
				if(pageSegment[currentItem]) {
					if(contentsElements.indexOf("image") != -1) 
						localContent += writeEllImageCell();
					localContent += writeEllInfoCell();
				} else {
					if(contentsElements.indexOf("image") != -1) 
						localContent += "<td> </td>";
					localContent += "<td> </td>";
				}
				if(currentCol<numColumns-1 && columnpad > 0) 
					localContent += writeCellPadding();
			}
			localContent += "</tr>";
			if((currentRow < (numRows - 1)) && (rowpad > 0))
				localContent += writeRowPadding((numColumns * 2 + (3 * (numColumns - 1))));
		}
	}
	return localContent;
};

function writeWrapCell(currentRow, numRows) {
	var localContent = "";
	localContent += "<td width=" + 100 / numColumns + "% height=100% align=left onmouseover=changeClass(this,'wcell-on'); onmouseout=changeClass(this,'wcell'); class=wcell>"
	if(pageSegment[currentItem]) {
		localItem = pageSegment[currentItem];
		if(contentsElements.indexOf("quantity")) showQuantity = true;
		var elementArray = contentsElements.split("/");
		for(i=0;i<elementArray.length;i++) {
			switch(elementArray[i]) {
				case "image":
					localContent += "<a href=" + ((localItem.linktype) ? localItem.link : localItem.id + '.html') + ">" + localItem.img + "</a>";
				break
				case "name":
					localContent += "<a href=" + ((localItem.linktype) ? localItem.link : localItem.id + '.html') + ">" + localItem.name + "</a><br>";
				break
				case "code":
					if(localItem.code != "") localContent += "<span class=code>" + localItem.code + "</span><br>";
				break
				case "price":
					localContent += pagingPrice(localItem);
				break
				case "abstract":
					if(localItem.abstr != "") localContent += "<span class=abstract>" + localItem.abstr + "</span><br>";
				break
				case "brand":
					if(localItem.brand != "") localContent += "<span class=brand>" + localItem.brand + "</span><br>";
				break
	                        case "order":
	                                localContent += pagingAddToCart(localItem);
	                        break
	                        case "quantity": break;
			}
		}
		currentItem++;
	} else {
		localContent += " ";
	}
	localContent += "</td>";
	return localContent;
};

function writeWrap(pageNum, viewallthistime) {
	var localContent = "";
	var numRows = Math.ceil(pageSegment.length / numColumns);
	currentItem=0; currentItemImage = 0;
	for(currentRow=0; currentRow<numRows; currentRow++) {
		if(pageSegment[currentItem]) {
			localContent += "<tr valign=top>";
			for(currentCol=0; currentCol<numColumns; currentCol++) {
				if(pageSegment[currentItem])
					localContent += writeWrapCell();
				else
					localContent += "<td> </td>";
				if(currentCol<numColumns-1 && columnpad > 0) 
					localContent += writeCellPadding();
			}
			localContent += "</tr>";
			if((currentRow < (numRows - 1)) && (rowpad > 0))
				localContent += writeRowPadding((numColumns * 2 + (3 * (numColumns - 1))));
		}
	}
	return localContent;
};

/* writePage, changeSortAndPage */
function writePage(pageNum, viewallthistime) {
	if(tempstoreid != storename)
	      alert("We're sorry, but Advanced Paging may not be transferred from one store to another.  You must purchase one copy of this template for *each* store you intent to use it on.  Please contact SolidCactus immediately.");
	else {
		var localContent = "";
		if((numPerPage > -1) && !(viewallthistime)) pageSegment = pagingSegment(window.items,pageNum); else pageSegment = window.items;
		writePageLinks(viewallthistime,pageNum);
		writeItemXofX(viewallthistime,pageNum);
		writePageXofX(viewallthistime,pageNum);
		writeSortLinks("",viewallthistime);
		writeSortLinks("2",viewallthistime);
		writePagingModesLinks();
		writeArrows(viewallthistime,pageNum);
	
		var numRows = Math.ceil(pageSegment.length / numColumns);
		switch(contentsFormat) {
			case "LIST":
				localContent += writeAsList(viewallthistime,pageNum);
			break
			case "VERTICAL":
				localContent += "<table cellspacing=0 cellpadding=0 width=" + tableWidth + " border=0>";
				localContent += writeVertical(pageNum, viewallthistime);
				localContent += "</table>";
			break
			case "ELL":
				localContent += "<table cellspacing=0 cellpadding=0 width=" + tableWidth + " border=0>";
				localContent += writeEll(pageNum, viewallthistime);
				localContent += "</table>";
			break
			case "WRAP":
				localContent += "<table cellspacing=0 cellpadding=0 width=" + tableWidth + " border=0>";
				localContent += writeWrap(pageNum, viewallthistime);
				localContent += "</table>";
			break
			default:
				writeAsList(viewallthistime,pageNum);
		}	
	
		var theDiv = FIND("contents");
		theDiv.innerHTML = localContent;
				
		temp = document.location.href.split(".");
		temp2 = temp[temp.length - 2].split("/");
		var currentPageHTML = temp2[temp2.length - 1] + ".html";
		SetCookie('thePage', pageNum, exp);
		SetCookie('theSort', sortBy, exp);
		SetCookie('thePageName', currentPageHTML, exp);
		if(sortHighLow) SetCookie('highlow', "1", exp); else SetCookie('highlow', "0", exp);
		SetCookie('contentsformat', contentsFormat, exp);
		if(viewallthistime) SetCookie('viewall', "1", exp); else SetCookie('viewall', "0", exp);
		
		globalPageNum = pageNum;
		globalViewallthistime = viewallthistime;
	}//passed security test
};

function changeSortAndPage(newSortBy, newHighLow, pageNum, viewallthistime)
{
	sortBy = newSortBy;
	sortHighLow = newHighLow;
	updateSortBy(window.items, sortBy);
	sortArray(window.items,0,window.items.length-1);
	if(sortHighLow) window.items.reverse();

	if(viewallthistime)
		writePage(pageNum, true);
	else
		writePage(pageNum, false);
};

function writeCellPadding(num) {
	if(num) localnum = num; else localnum = currentItem;
	if(pageSegment[localnum]) {
		var localContent = '<td style="width:'
		                 +  round_decimals(columnpad / 2,0) + 'px;"></td>'
		                 +  '<td style="width:1px;color:#FFF;font-size:1px;border-left:1px solid '+columnSeparatorColor +'">.</td>'
		                 +  '<td style="width:'
		                 +  round_decimals(columnpad / 2,0) + 'px;"></td>'
		return localContent;
	} else {
		var localContent = '<td style="width:'
		                 +  round_decimals(columnpad / 2,0) + 'px;"></td>'
		                 +  '<td style="width:1px;"></td>'
		                 +  '<td style="width:'
		                 +  round_decimals(columnpad / 2,0) + 'px;"></td>'
		return localContent;
	}
}

function writeRowPadding(colspan) {
if(pageSegment[currentItem]) {
var localContent = "<tr><th><img src=/lib/" + storename + "/pspacer.gif width=1 height="
                 +  round_decimals(rowpad / 2,0) + "></th></tr>"
                 +  "<tr><th bgcolor=\"" + rowSeparatorColor +  "\" colspan=" + colspan + "><img src=/lib/"
                 +  storename + "/pspacer.gif width=1 height=1></th></tr>"
                 +  "<tr><th><img src=/lib/" + storename + "/pspacer.gif width=1 height="
                 +  round_decimals(rowpad / 2,0) + "></th></tr>";
return localContent;
} else
return "";
}

function initPaging() {
	var pageNum, viewallthistime = false, highlowthistime = false;
	var readName = GetCookie('thePageName');
	var readPage = GetCookie('thePage');
	var readSort = GetCookie('theSort');
	var readHighLow = GetCookie('highlow');
	var readContentsFormat = GetCookie('contentsformat');
	var readViewAll = GetCookie('viewall');
	if(readViewAll == "1") viewallthistime = true;

	if(readName || readSort || readContentsFormat) {
		temp = document.location.href.split(".");
		temp2 = temp[temp.length - 2].split("/");
		currentPageHTML = temp2[temp2.length - 1] + ".html";
		if(readName == currentPageHTML)
			(readPage == 0) ? pageNum = 1 : pageNum = parseInt(readPage);
		else
			pageNum = 1;
		readSort ? sortBy = readSort : sortBy = defaultSort;
		(readHighLow == 1 && readSort) ? highlowthistime = true : highlowthistime = false;
		(readContentsFormat == null) ? contentsFormat = defaultContentsFormat : contentsFormat = readContentsFormat;
	} else {
		pageNum = 1;
		sortBy = defaultSort;
		contentsFormat = defaultContentsFormat;
	}
	if(sortBy == "default")
		writePage(pageNum,viewallthistime);
	else
		changeSortAndPage(sortBy,highlowthistime,pageNum,viewallthistime);
};

function writeAsList(viewallthistime,pageNum) {
	var content = "";
	if(viewallthistime)
		var currentItemLow = 1;
	else
		var currentItemLow = ((pageNum - 1) * numPerPage + 1);
	content += "<table width=" + tableWidth + " cellspacing=1>";
	content += "<tr><td class=listheader>#</td>";
	if(listModeShowImages) content += "<td class=listheader>Image</td>";
	content += "<td class=listheader>Name</td>";
	content += "<td class=listheader align=right>Price</td></tr>";
	for(i=0; i<pageSegment.length; i++) {
		itemnow = pageSegment[i];
		if(itemnow) {
			content += "<tr valign=top bgcolor=";
			(i % 2 == 0) ? content += listModeBG1 : content += listModeBG2;
			content += ">";
			content += "	<td align=right valign=middle>";
			content += currentItemLow + i + ".";
			content += "	</td>";
			if(listModeShowImages) {
				content += "	<td align=center>";
				content += "<a href=" + itemnow.id + ".html>" + itemnow.listimg + "</a>";
				content += "	</td>";
			}
			content += "	<td width=\"100%\">";
			content += "		<a href=" + itemnow.id + ".html>" + itemnow.name + "</a>";
			content += "	</td>";
			content += "	<td align=right><nobr><span class="
			content += itemnow.saleprice == 0 ? "" : "sale";
			content += "price>";
			content += itemnow.useprice == 0 ? "" : "$" + itemnow.useprice;
			content += "</span></nobr>";
			content += "	</td>";
			content += "</tr>";
		}
	}
	content += "</table>";
	return content;
};

function updateSortBy(array, sortby) {
	/* find what value to sort the array by and store it to a variable local to each item */
	for(i=0; i<array.length; i++) {
		switch(sortby) {
			case "default" :
				array[i].sortby = array[i].defaultsort;
			break
			case "name" :
				array[i].sortby = array[i].name;
			break
			case "price" :
				array[i].sortby = parseFloat(array[i].useprice);
			break
			case "brand" :
				array[i].sortby = array[i].brand;
			break
			default : array[i].sortby = array[i].name;
		}
	}
};

function writeOneSortLink(linkid,linkname,sep,chosen,bot,viewallthistime) {
	var localContent = "";
	if(chosen) {
		localContent += "<a class=chosen href=#top onclick=javascript:changeSortAndPage(\"" + linkid + "\",";
		if(sortHighLow) localContent += "false"; else localContent += "true";
		localContent += ",1," + viewallthistime + ");>";
		localContent += linkname;
		localContent += "</a>";
	} else {
		localContent += "<a href=#top onclick=javascript:changeSortAndPage(\"" + linkid + "\",false,1," + viewallthistime + ");>";
		localContent += linkname;
		localContent += "</a>";
	}
	localContent += "<span id=" + linkid + "arrowdiv" + bot + "></span>";
	if(sep) localContent += sortLinkSeparator;
	return localContent;
}

function writeSortLinks(bot,viewallthistime) {
	var sortLinksDiv = FIND("sortlinks" + bot);
	if(sortLinksDiv) {
		var localContent = "Sort by <span id=sortlinks>";
		if(showBrandSort) var priceSep = true; else var priceSep = false;
		switch(sortBy) {
			case "name":
					if(showDefaultSort) localContent += writeOneSortLink("default","Default Sort",true,false,bot,viewallthistime);
					localContent += writeOneSortLink("name","Name",true,true,bot,viewallthistime);
					localContent += writeOneSortLink("price","Price",priceSep,false,bot,viewallthistime);
					if(showBrandSort) localContent += writeOneSortLink("brand","Manufacturer",false,false,bot,viewallthistime);
			break
			case "price":
					if(showDefaultSort) localContent += writeOneSortLink("default","Default Sort",true,false,bot,viewallthistime);
					localContent += writeOneSortLink("name","Name",true,false,bot,viewallthistime);
					localContent += writeOneSortLink("price","Price",priceSep,true,bot,viewallthistime);
					if(showBrandSort) localContent += writeOneSortLink("brand","Manufacturer",false,false,bot,viewallthistime);
			break
			case "brand":
					if(showDefaultSort) localContent += writeOneSortLink("default","Default Sort",true,false,bot,viewallthistime);
					localContent += writeOneSortLink("name","Name",true,false,bot,viewallthistime);
					localContent += writeOneSortLink("price","Price",priceSep,false,bot,viewallthistime);
					if(showBrandSort) localContent += writeOneSortLink("brand","Manufacturer",false,true,bot,viewallthistime);
			break
			default:
					if(showDefaultSort) {
						localContent += "<span class=chosen>";
						localContent += "Default Sort";
						localContent += "</span>";
						localContent += sortLinkSeparator;
					}
					localContent += writeOneSortLink("name","Name",true,false,bot,viewallthistime);
					localContent += writeOneSortLink("price","Price",priceSep,false,bot,viewallthistime);
					if(showBrandSort) localContent += writeOneSortLink("brand","Manufacturer",false,false,bot,viewallthistime);

		}
		localContent += "</span>";
		sortLinksDiv.innerHTML = localContent;
	}
};

function changeFormat(newFormat) {
	contentsFormat = newFormat;
	writePage(globalPageNum, globalViewallthistime);
}

function makeMAPWin(ww, wh) {
	w1=window.open('','w1','width='+ww+',height='+wh+',status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes');
	w1.document.open();
	w1.document.write("<font size=\"2\" face=\"Arial, Helvetica, sans-serif\"><b>Price \"Too Low to Display\" Explained<\/b><br> In order to provide the best support from our manufacturer's and distributors, we have agreed to abide by their \"MAP\" or minimum-advertised-price policy. You can see this price by clicking the product name and then selecting the Add to Cart button on the product information page. <br><br>Please be assured that simply adding an item to your cart does not obligate you to buy it--you can always delete the item from your cart if you decide not to purchase it.<p align=\"center\"><a href=\"javascript:window.close();\">Close Window</a></p></font>");
	w1.document.focus();
};

function mapPricing(useItem) {
	var mapstring = "<span class=map>";
	mapstring += "<form method=post action=http://order.store.yahoo.com/cgi-bin/wg-order?" + storename + ">"
	mapstring += '<input id=mapinput type=submit value="Click for instant price quote" height="16" width="155" border=0>';
	mapstring += ' <a href="javascript:makeMAPWin(300,350);">(Why?)</a><br>';
	mapstring += "<input type=hidden name=vwcatalog value=" + storename + ">"
	mapstring += "<input type=hidden name=vwitem value=" + localItem.id + ">";
	if(addToCartImage != "")
		mapstring += addToCartImage;
	else
		mapstring += "<input type=submit id=submit value='Add to Cart'>"
	mapstring += "</form>";
	mapstring += "</span>";
	return mapstring;
}

function writePagingModesLinks() {
	if(showPagingModes) {
		var localContent  = "<div id=modes>";
		(contentsFormat != "VERTICAL") ? localContent += "<a href=#top onclick=\"javascript:changeFormat('VERTICAL');\">" : localContent += "<span class=chosen>";
		localContent += "Style 1";
		(contentsFormat != "VERTICAL") ? localContent += "</a>" : localContent += "</span>";
		localContent += "<span class=sep>" + modeLinkSeparator + "</span>";

		(contentsFormat != "ELL") ? localContent += "<a href=#top onclick=\"javascript:changeFormat('ELL');\">" : localContent += "<span class=chosen>";
		localContent += "Style 2";
		(contentsFormat != "ELL") ? localContent += "</a>" : localContent += "</span>";

		if(showWrapMode) {
			localContent += "<span class=sep>" + modeLinkSeparator + "</span>";
			(contentsFormat != "WRAP") ? localContent += "<a href=#top onclick=\"javascript:changeFormat('WRAP');\">" : localContent += "<span class=chosen>";
			localContent += "Style 3";
			(contentsFormat != "WRAP") ? localContent += "</a>" : localContent += "</span>";
		}

		if(showListMode) {
			localContent += "<span class=sep>" + modeLinkSeparator + "</span>";

			(contentsFormat != "LIST") ? localContent += "<a href=#top onclick=\"javascript:changeFormat('LIST');\">" : localContent += "<span class=chosen>";
			localContent += "List Mode";
			(contentsFormat != "LIST") ? localContent += "</a>" : localContent += "</span>";
		}

		localContent += "</div>";

		var listModeDiv = FIND("pagingmodes");
		if(listModeDiv) listModeDiv.innerHTML = localContent;
		listModeDiv = FIND("pagingmodes2");
		if(listModeDiv) listModeDiv.innerHTML = localContent;
	}
}
