var defaultSort, defaultContentsFormat, contentsFormat, tableWidth, numColumns, numPerPage, numPages, contentsElements, showDefaultSort, 

    detailedSortInfo, showPagingModes, viewAll, sortBy, sortHighLow, pageLinkPrefix, rowpad, columnpad, mystoreid, columnSeparatorColor, 

    rowSeparatorColor, listModeBG1, listModeBG2, sortLinkSeparator, modeLinkSeparator, pageLinkSeparator, globalPageNum, globalViewallthistime,

    currentItem, currentItemImage, numArrowDivs, showListMode, addToCartImage, showBrandSort, strikePrice, showWrapMode, showQuantity, listModeShowImages, showProductionSort, showMinQtySort;

	cell1, cell2, cell3, cell4, mrow

var pageItems = new Array();

var oldSortBy = "";

function mycontent(id, code, name, listimg, price, saleprice, options, abstr) {

	if (!window.items) window.items = new Array();

	this.id=id.toLowerCase();

	this.code=code;

	this.name=name;

	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.sortby="name";

	this.defaultsort=window.items.length;

	window.items[window.items.length] = this;

}

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;

	if (hiBound - loBound == 1)

	{

		if (theArray[loBound].sortby > theArray[hiBound].sortby)

		{

			temp = theArray[loBound];

			theArray[loBound] = theArray[hiBound];

			theArray[hiBound] = temp;

		}

		return;

	}

	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;

	if (loBound < hiSwap - 1)

		sortArray(theArray, loBound, hiSwap - 1);

	if (hiSwap + 1 < hiBound)

		sortArray(theArray, hiSwap + 1, hiBound);

};

function changeClass(which,theclass) {

	which.className = theclass;	

}

function writePageLinks( viewallThisTime, pageNum) {

	if(numPages > 1) {

		var pageLinks = "<span id=pagelinks><b>" + pageLinkPrefix+ "</b>";

		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 % 21 == 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 += "<b>Displaying</b> " + currentItemLow + " of " + numItems + " Products"  ;

	} else if(viewallthistime) {

		theString += "<b>Displaying</b> 1-" + numItems + " of " + numItems + " Products"  ;

	} else {

		theString += "<b>Displaying</b> " + currentItemLow + "-" + currentItemHigh + " of " + numItems + " Products" ;

	}

	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 += "<b>Displaying Page</b> " + pageNum + " of " + numPages;

	localContent += "</span>";

	var pageofDiv = FIND("pageof");

	if(pageofDiv) pageofDiv.innerHTML = localContent;

	pageofDiv = FIND("pageof2");

	if(pageofDiv) pageofDiv.innerHTML = localContent;

};

function writeUpDownArrow(divname) {

	if(divname) {

		localContent = "";

		if(sortHighLow) localContent += "&nbsp;" + upArrow; else localContent += "&nbsp;" + 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=javascript:writePage(2)>" + rightArrow + "</a>";

		} else if (pageNum == numPages) {

			leftArrowString += "<a href=javascript:writePage(" + (numPages - 1) + ")>" + leftArrow + "</a>";

			rightArrowString += rightArrowG;

		} else {

			leftArrowString += "<a href=javascript:writePage(" + (pageNum - 1) + ")>" + leftArrow + "</a>"

			rightArrowString += "<a href=javascript:writePage(" + (pageNum + 1) + ")>" + 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;

		}

	}

};

function writePage(pageNum, viewallthistime) {

	if(tempstoreid != mystoreid)

	      alert("We're sorry, but Power Paging may not be working on another. please contact to satisnet.com.");

	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 += writeAsList(viewallthistime,pageNum);

			break

			default:

				writeAsList(viewallthistime,pageNum);

		}	

		var theDiv = FIND("contents");

		theDiv.innerHTML = localContent;

		var thefstart = FIND("myform");

		thefstart.innerHTML = "<form><input type=text></form>";

		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;

	}

};

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 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 += "<div class=bg-header-mcart><div class=wtext-image>Image</div><div class=wtext-name "+cell2+">Available Selection</div><div class=wtext-price>Retail Price</div><div class=wtext-sale>Sale Price</div><div class=wtext-add>Add</div></div>";

	content += "<div class=spacer></div><div class=mcart-contener>";

	for(i=0; i<pageSegment.length; i++) {

		itemnow = pageSegment[i];

		if(itemnow && itemnow.useprice != 0) {

			content += "<div class=mcart-row "+mrow+">";

			content += "<div class=mcart-cell mcart-cell1 "+cell1+">";

			content += "<a href=" + itemnow.id + ".html title='"+ itemnow.name+ itemnow.abstr+"'>" + itemnow.listimg + "</a>";

			content += "</div>";

			content += "<div class=mcart-cell mcart-cell2 "+cell2+">";

			content += itemnow.useprice == 0 ? "<a href='javascript:void(0);' title='"+ itemnow.name+ itemnow.abstr+"'><span class=p-name>" + itemnow.name + "<br><span class=p-abstract>" + itemnow.abstr + "</span><br></span></a>" : "<a href=" + itemnow.id + ".html title='"+ itemnow.name+ itemnow.abstr+"'><span class=p-name>" + itemnow.name + "<br><span class=p-abstract>" + itemnow.abstr + "</span><br></span></a>";

			content += "</div>";

			if (itemnow.price == "NaN.00" || itemnow.price == 0.00){

						if(itemnow.price == 0.00 && itemnow.saleprice == 0.00){

			content += "<div class=mcart-cell mcart-cell3 "+cell3+"> Out Of Stock </div>";

			}

			else{content += "<div class=mcart-cell mcart-cell3 "+cell3+"> Not Found </div>";}

			}else{

			content += "<div class=mcart-cell mcart-cell3 "+cell3+"> $" + itemnow.price + "</div>";

			}

			content += "<div class=mcart-cell mcart-cell4 "+cell4+">";

			content += "<nobr><span class="

			content += itemnow.saleprice == 0 ? "" : "sale";

			content += "price>";

			content += itemnow.useprice == 0 ? "<div style='font:normal 11px arial,tahoma,verdana;padding-top:12px;'>Out Of Stock</div>" : "<div style='padding-top:11px;'>$" + itemnow.useprice +"</div>";

			content += "</span></nobr>";

			content += "</div>";

			if (itemnow.saleprice == "0.00" && itemnow.price== "0.00"){

			content += "<div class=mcart-cell5 style='padding-top:12px;'><font style='font:normal 11px arial,tahoma,verdana;'>Out of Stock</font></div>";

			}else{

			content += "<div class=mcart-cell5><input name=vwquantity"+currentItemLow + i+" type=text value=1 size=3 class=qtyboxmcart><input name=vwitem"+currentItemLow + i+" type=checkbox value="+itemnow.id+"><input name=vwattr" + currentItemLow +i + "_" + itemnow.options + " type=hidden value='"+ itemnow.abstr +"'></div>";

			}

			content += "</div><div class=spacer></div>";

		}

			

	}

	content += "</div>";

	return content;

};

function updateSortBy(array, sortby) {

	for(i=0; i<array.length; i++) {

		switch(sortby) {

			case "default" :

				array[i].sortby = array[i].name;

			break

			case "name" :

				array[i].sortby = array[i].name;

			break

			case "size" :

				array[i].sortby = array[i].size;

			break

			case "price" :

				array[i].sortby = parseInt(array[i].useprice);

			break

			default : array[i].sortby = array[i].name;

		}

	}

};

function writeOneSortLink(linkid,linkname,sep,chosen,bot,viewallthistime) {

	var localContent = "";

	if(chosen) {

		localContent += "<div id=remotebox><a class=chosen href=#top onclick=javascript:changeSortAndPage(\"" + linkid + "\",";

		if(sortHighLow) localContent += "false"; else localContent += "true";

		localContent += ",1," + viewallthistime + ");>&nbsp;";

		localContent += linkname;

		

		localContent += "<span id=" + linkid + "arrowdiv" + bot + "></span>&nbsp;</a>";

		localContent += "</div><div id=myfloat>" + sortLinkSeparator + "</div>";

	} else {

		localContent += "<div id=remotebox><a href=#top onclick=javascript:changeSortAndPage(\"" + linkid + "\",false,1," + viewallthistime + ");>&nbsp;";

		localContent += linkname;

		localContent += "&nbsp;</a></div><div id=myfloat>" + sortLinkSeparator + "</div>";

	}

	return localContent;

}

function writeSortLinks(bot,viewallthistime) {

	var sortLinksDiv = FIND("sortlinks" + bot);

	if(sortLinksDiv) {

		var localContent = "<span id=sortlinks><div id=myfloat><span id=sortlinks_heading>SORT BY &nbsp;</span></div> ";

		if(showBrandSort) var priceSep = true; else var priceSep = false;

		if(showMinQtySort) var priceSep = true; else var priceSep = false;

		if(showProductionSort) var priceSep = true; else var priceSep = false;

		switch(sortBy) {

			case "name":

					localContent += writeOneSortLink("name","Alphabetically",true,true,bot,viewallthistime);

					localContent += writeOneSortLink("price","Price",priceSep,false,bot,viewallthistime);

			break

			case "price":

					localContent += writeOneSortLink("name","Alphabetically",true,false,bot,viewallthistime);

					localContent += writeOneSortLink("price","Price",priceSep,true,bot,viewallthistime);

			break

			default:

					localContent += writeOneSortLink("name","Alphabetically",true,false,bot,viewallthistime);

					localContent += writeOneSortLink("price","Price",priceSep,false,bot,viewallthistime);

	}

		localContent += "</span>";

		sortLinksDiv.innerHTML = localContent;

	}

};

function changeFormat(newFormat) {

	contentsFormat = newFormat;

	writePage(globalPageNum, globalViewallthistime);

}

function writePagingModesLinks() {

	if(showPagingModes) {

	    var listModeDiv = FIND("pagingmodes");

		if(listModeDiv) listModeDiv.innerHTML = localContent;

		listModeDiv = FIND("pagingmodes2");

		if(listModeDiv) listModeDiv.innerHTML = localContent;

	}

}