str = document.location.search;

var cururl = document.URL;





var pagenum=1;

var n=cururl.indexOf('page=');

if(n > 0)

{

	str = cururl.substring(n+5,cururl.length);

	if(str.indexOf("&") > -1)

	{

		pagenum = Number(str.substr(0, str.indexOf("&"))); 

	}

	else

	{

		pagenum = Number(str);

	}



}



var psize = 3;

var srttype = 0;

var n=cururl.indexOf('sorttype=');



if(n>0)

{

	srttype = cururl.substring(n+9,cururl.length);

	var nn = srttype.indexOf('&');

	if(nn>0)

	{

		srttype = srttype.substring(0,nn);

	}

}



var n=cururl.indexOf('size=');



if(n>0)

{

	psize = cururl.substring(n+5,cururl.length);

	var nn = psize.indexOf('&');

	if(nn>0)

	{

		psize = psize.substring(0,nn);

	}

}



function sortByNameAscending(a,b)

{

	var x = a.productname.toLowerCase();

	var y = b.productname.toLowerCase();

	return ((x < y) ? -1 : ((x > y) ? 1 : 0));

}



function sortByNameDescending(a,b)

{

	var x = b.productname.toLowerCase();

	var y = a.productname.toLowerCase();

		return ((x < y) ? -1 : ((x > y) ? 1 : 0));

}



function sortByPriceAscending(a,b)

{

	var x = a.productprice;

	var y = b.productprice;

	return ((x < y) ? -1 : ((x > y) ? 1 : 0));

}



function sortByPriceDescending(a,b)

{

	var x = b.productprice;

	var y = a.productprice;

	return ((x < y) ? -1 : ((x > y) ? 1 : 0));

}



function sortingchange(val)

{

	ps = document.getElementById("top-paging").selectedIndex;

	if(val == 1)

	{

		index = document.getElementById("top-sorting").selectedIndex;

	}

	else

	{

		index = document.getElementById("bottom-sorting").selectedIndex;

	}



	url = url + "?page=" + pagenum + "&sorttype=" + index + "&size=" + ps;

	document.location.href = url;

}



function sortArray(val)

{	



	sorttype = val;



	if(val==1)

	{

		data.sort(sortByNameAscending);		

	}

	else if(val==2)

	{

		data.sort(sortByNameDescending);

	}

	else if(val==3)

	{

		data.sort(sortByPriceAscending);

	}

	else if(val==4)

	{

		data.sort(sortByPriceDescending);

	}



	document.getElementById("top-sorting").selectedIndex = srttype;

	document.getElementById("bottom-sorting").selectedIndex = srttype;

	

	document.getElementById("top-paging").selectedIndex = psize;

	document.getElementById("bottom-paging").selectedIndex = psize;



	pagingDisplay(len,pagenum);

	

}







function pagingsizechange(val)

{

	if(val == 'top')

	{

		index = document.getElementById("top-paging").selectedIndex;

	}

	else

	{

		index = document.getElementById("bottom-paging").selectedIndex;

	}

	

	if(index != 4)

		var pagelink = url + "?sorttype=" + srttype + "&size=" + index + "&page=1";

	else

		var pagelink = url + "?sorttype=" + srttype + "&size=" + index + "&page=404";

		

	document.location.href = pagelink;

	document.getElementById("bottom-paging").selectedIndex = index;

	

}







function dataadd(divid, productname, productprice, productoption, productid, productimagefix, productimage, productorderable, price, categoryid) 

{

	this.divid = divid;

	this.productname = productname;

	this.productprice = productprice;

	this.productoption = productoption;

	this.productid = productid;

	this.productimagefix = productimagefix;	

	this.productimage = productimage;	

	this.productorderable = productorderable;

	this.price = price;

	this.categoryid = categoryid;

}





function pagingDisplay(len, pagenum)

{

	pno = document.getElementById("top-paging").value;	

	curpage = pagenum;

	totalpage=Math.ceil(len / pno);		

	startpos = (curpage - 1) * pno;

	endpos = (curpage * pno) - 1;	

	

	if(endpos > len)

		var enditem = len;

	else

		var enditem = endpos + 1;

	

	var count=0;

 	var divcon = "";

	

	var itemdisplay = "Showing " + (startpos + 1) + " - " + enditem  + " of " + len;

	var paging_display = "Page " + pagenum + " of " + totalpage;





	for(i=0;i<len;i++)

	{

		if((i>=startpos && i<=endpos) || (curpage==404))

		{

	

			if(count < 3)

			{

				if(data[i].categoryid == 'sales')

				{

					divcon += "<div class='productlist_box_sale'>";

				}

				else

				{

					divcon += "<div class='productlist_box'>";

				}

			}

			else

			{

				if(data[i].categoryid == 'sales')

				{

					divcon += "<div class='productlist_box_sale' style='margin-right: 0px;'>";

				}

				else

				{

					divcon += "<div class='productlist_box' style='margin-right: 0px;'>";

				}

			}

			//divcon += document.getElementById(data[i].divid).innerHTML;

			

			// Product Name

			if(data[i].productname.length > 65)

				productname = data[i].productname.substr(0, 62) + "...";

			else

				productname = data[i].productname;

			

			divcon += "<h2 style='height: 45px;'><a href='" + data[i].productid + ".html' title='" + data[i].productname + "'>" + productname + "</a></h2>";

			

			// Product Image

			if(data[i].productimagefix == 1)

			{

				divcon += "<div class='img_center' style='height: 221px; '><span></span><a href='" + data[i].productid + ".html'><img src='" + data[i].productimage + "' width='175' height='221' border='0' hspace='0' vspace='0' alt='" + data[i].productname + "' title='" + data[i].productname + "'></a></div>";

			}

			else

			{

				divcon += "<div class='img_center' style='height: 221px; '><span></span><a href='" + data[i].productid + ".html'><img src='" + data[i].productimage + "' border='0' hspace='0' vspace='0' alt='" + data[i].productname + "' title='" + data[i].productname + "'></a></div>";

			}

			

			// Product Price

			if(data[i].categoryid == 'sales')

			{

				divcon += "<div style='float:left; width:116px;'><span class='price' style='font-size:10px;'>Retail Price:$" + data[i].price.toFixed(2) + "</span><span class='price'>Sale Price:$" + data[i].productprice.toFixed(2) + "</span></div>";

			}

			else

			{

				divcon += "<span class='price'>$" + data[i].productprice.toFixed(2) + "</span>";

			}

			// Product Add to Cart



			if(data[i].productorderable == 1)

			{

				if(data[i].productoption !=  988)

				{

					if(data[i].categoryid == 'sales')

					{

						divcon += "<div style='float:right;'><input src='http://site.ashleyklein.com/ystore/images/add_to_cart_sale.gif' onclick=\"return OpenAddtoCartPopupAll(false, this, " + data[i].productoption + ");\" type='image' alt='Add to Cart' title='Add to Cart'  class='addtocart' ></div>";

					}

					else

					{

						divcon += "<input src='http://site.ashleyklein.com/ystore/images/button_add_to_cart2.gif' onclick=\"return OpenAddtoCartPopupAll(false, this, " + data[i].productoption + ");\" type='image' alt='Add to Cart' title='Add to Cart'  class='addtocart' >";

					}

				}

				else

				{

					if(data[i].categoryid == 'sales')

					{

						divcon += "<div style='float:right;'><a href='http://order.store.yahoo.net/cgi-bin/wg-order?yhst-89504165598813+" + data[i].productid + "'><img src='http://site.ashleyklein.com/ystore/images/add_to_cart_sale.gif' border='0' alt='Add to Cart' title='Add to Cart'  class='addtocart' ></a></div>";

					}

					else

					{

						divcon += "<a href='http://order.store.yahoo.net/cgi-bin/wg-order?yhst-89504165598813+" + data[i].productid + "'><img src='http://site.ashleyklein.com/ystore/images/button_add_to_cart2.gif' border='0' alt='Add to Cart' title='Add to Cart'  class='addtocart' ></a>";

					}

				}

			}

			else

			{

				divcon += "<img src='http://site.ashleyklein.com/ystore/images/out_of_stock.gif' alt='Out of Stock' title='Out of Stock'  class='addtocart' >";

			}

			

			divcon += "</div>";

				

			count = count + 1;



			if(count > 3)

			{

				count=0;

			}

		}	

	}	



	document.getElementById("pagedisplay").innerHTML=divcon;

/*	document.getElementById("itemdisplay").innerHTML=itemdisplay;

	document.getElementById("paging_display").innerHTML=paging_display;

	

	document.getElementById("itemdisplay1").innerHTML=itemdisplay;

	document.getElementById("paging_display1").innerHTML=paging_display;*/

	

	writepages();

	



}



function writepages()

{

 



var curpage=new Number(pagenum)

       var prevpagenum=curpage-1

       var nextpagenum=curpage+1

       displayTxt = " "

      

	  if((curpage>1) && (curpage!=404))

                   displayTxt = displayTxt + "&nbsp;<a href='" + url + "?page=" + prevpagenum + "&sorttype=" + srttype + "&size=" + psize + "' class='next_pre'>< Prev</a> ";

				   

	   displayTxt += "  Page  [ ";

       for(var i=1;i<=totalpage;i++)

       {

			if(i==curpage)

			{

				displayTxt = displayTxt + "<span>" + i + "</span> ";

				if(i==totalpage)

				{

				}

				else

			  {

			  displayTxt = displayTxt + "| ";

			  }

			}

			else

			{

				displayTxt = displayTxt + "<a href='" + url + "?page=" + i + "&sorttype=" + srttype + "&size=" + psize + "'>" + i + "</a> ";

				if(i==totalpage)

				{

				}

				else

			    {

				  displayTxt = displayTxt + "| ";

			    }

			}

			 

	   }



       if(curpage < totalpage)

                       displayTxt = displayTxt + "] <a  href='" + url + "?page=" + nextpagenum + "&sorttype=" + srttype + "&size=" + psize + "' class='next_pre'> Next > </a> ";



		displayTxt = displayTxt + "]";

		

		if(curpage == 404)

		{

			document.getElementById('main').innerHTML="";

       		if(document.getElementById('main-1'))

			{

				   document.getElementById('main-1').innerHTML="";              

			}

		}

		else

		{

			document.getElementById('main').innerHTML=displayTxt;

       		if(document.getElementById('main-1'))

			{

				   document.getElementById('main-1').innerHTML=displayTxt;              

			}

		}

}

