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;
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>" + 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 += "Displaying " + currentItemLow + " of " + numItems + " Products"  ;
	} else if(viewallthistime) {
		theString += "Displaying 1-" + numItems + " of " + numItems + " Products"  ;
	} else {
		theString += "Displaying " + 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 += "Displaying Page " + 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 += "<table width=" + tableWidth + " cellspacing=0  cellpadding=0 class=bg-cart-box height=28 border=0>";
	content += "<tr>";
	content += "<td bgcolor=#C3C2C2 valign=top><img src=" + ftppath + "spacer.gif height=1 width=1 border=0 alt=perfume></td>";
	content += "<td align=center valign=middle class=myshopping width=12% height=28px background=http://www.satisnet.com/ystore123/1stperfume-new/images/bg_cart.gif><FONT class=shoppingheading>Image</FONT></td>";
	content += "<td bgcolor=#C3C2C2 valign=top><img src=" + ftppath + "spacer.gif height=1 width=1 border=0 alt=perfume></td>";
	content += "<td align=center valign=middle class=myshopping background=http://www.satisnet.com/ystore123/1stperfume-new/images/bg_cart.gif><FONT class=shoppingheading>Available Selection</FONT></td>";
	content += "<td bgcolor=#C3C2C2 valign=top><img src=" + ftppath + "spacer.gif height=1 width=1 border=0 alt=perfume></td>";
	content += "<td align=center valign=middle class=myshopping width=15% background=http://www.satisnet.com/ystore123/1stperfume-new/images/bg_cart.gif><FONT class=shoppingheading>Retail Price</FONT></td>";
	content += "<td bgcolor=#C3C2C2 valign=top><img src=" + ftppath + "spacer.gif height=1 width=1 border=0 alt=perfume></td>";
	content += "<td align=center valign=middle class=myshopping width=15% background=http://www.satisnet.com/ystore123/1stperfume-new/images/bg_cart.gif><FONT class=shoppingheading>Sale Price</FONT></td>";
	content += "<td bgcolor=#C3C2C2 valign=top><img src=" + ftppath + "spacer.gif height=1 width=1 border=0 alt=perfume></td>";
	content += "<td align=center valign=middle class=myshopping width=15% background=http://www.satisnet.com/ystore123/1stperfume-new/images/bg_cart.gif><FONT class=shoppingheading>Add</FONT></td>";
	content += "<td bgcolor=#C3C2C2 valign=top><img src=" + ftppath + "spacer.gif height=1 width=1 border=0 alt=perfume></td>";
	content += "</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 bgcolor=#C3C2C2 valign=top width=1><img src=" + ftppath + "spacer.gif height=1 width=1 border=0 alt=perfume></td>";
			content += "<td bgcolor=#FFFFFF align=center valign=middle class=border width=12%>";
			content += "<a href=" + itemnow.id + ".html title='"+ itemnow.name+ itemnow.abstr+"'>" + itemnow.listimg + "</a>";
			content += "</td>";
			content += "<td bgcolor=#C3C2C2 valign=top width=1><img src=" + ftppath + "spacer.gif height=1 width=1 border=0 alt=perfume></td>";
			content += "<td bgcolor=#FFFFFF align=center height=45 valign=middle class=border width=43%>";
			content += "<a href=" + itemnow.id + ".html title='"+ itemnow.name+ itemnow.abstr+"'><span class=product-name-listing>" + itemnow.name + "<br>" + itemnow.abstr + "<br></span></a>";
			content += "</td>";
			content += "<td bgcolor=#C3C2C2 valign=top width=1><img src=" + ftppath + "spacer.gif height=1 width=1 border=0 alt=perfume></td>";
			if (itemnow.price == "NaN.00"){
			content += "<td bgcolor=#FFFFFF align=center valign=middle class=border><FONT class=matter width=15%> Not Found </font></td>";
			content += "<td bgcolor=#C3C2C2 valign=top width=1><img src=" + ftppath + "spacer.gif height=1 width=1 border=0 alt=perfume></td>";
			}else{
				if(itemnow.price == 0){
					if(itemnow.saleprice == "0.00"){
						content += "<td bgcolor=#FFFFFF align=center valign=middle class=border style=\"font-weight:normal;\"><FONT class=matter width=15%>Out of Stock</font></td>";
					}else{
						content += "<td bgcolor=#FFFFFF align=center valign=middle class=border style=\"font-weight:normal;\"><FONT class=matter width=15%>Not Found</font></td>";
					}
				}else{
				content += "<td bgcolor=#FFFFFF align=center valign=middle class=border><FONT class=matter width=15%> $" + itemnow.price + "</font></td>";
				}
			content += "<td bgcolor=#C3C2C2 valign=top width=1><img src=" + ftppath + "spacer.gif height=1 width=1 border=0 alt=perfume></td>";
			}
			content += "<td bgcolor=#FFFFFF align=center valign=middle class=border width=15%>";
			content += "<nobr><span class="
			content += itemnow.saleprice == 0 ? "" : "sale";
			content += "price>";
			content += itemnow.useprice == 0 ? "<font style=\"font-weight:normal;\">Out of Stock</font>" : "$" + itemnow.useprice;
			content += "</span></nobr>";
			content += "</td>";
			content += "<td bgcolor=#C3C2C2 valign=top width=1><img src=" + ftppath + "spacer.gif height=1 width=1 border=0 alt=perfume></td>";
			if (itemnow.saleprice == "0.00" && itemnow.price == "0.00"){
			content += "<td bgcolor=#FFFFFF align=center valign=middle class=border width=15% style=\"font-weight:normal;\">Out of Stock</td>";
			}else{
			content += "<td bgcolor=#FFFFFF align=center valign=middle class=border width=15%><input name=vwquantity"+currentItemLow + i+" type=text value=1 size=3><input name=vwitem"+currentItemLow + i+" type=checkbox value="+itemnow.id+"><input name=vwattr" + currentItemLow +i + "_" + itemnow.options + " type=hidden value='"+ itemnow.abstr +"'></td>";
			}
			content += "<td bgcolor=#C3C2C2 valign=top width=1><img src=" + ftppath + "spacer.gif height=1 width=1 border=0 alt=perfume></td>";
			content += "</tr><tr><td bgcolor=#C3C2C2 valign=top width=1><img src=" + ftppath + "spacer.gif height=1 width=1 border=0 alt=perfume></td></tr>";
		}
			
	}
	content += "</table>";
	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;
	}
}
