var divcon = '';
var dummy  = new Array();
var stprice = 0;
var endprice = 0;
var iprice = 0;
var cururl = document.URL;
var pagenum = 1;
var psize = 0;
var srttype = 0;

var ototal = 0;
var pos = cururl.indexOf("?");
var prodlink = cururl.substr(0,pos-1);

var n=cururl.indexOf('page=');

if(n>0)
{
	pagenum = cururl.substring(n+5,cururl.length);
	var nn = pagenum.indexOf('&');
	if(nn>0)
	{
		pagenum=pagenum.substring(0,nn);
	}
}
pagenum = Number(pagenum);

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);
	}
}

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);
	}
}


function dataadd(did, pname, price, purl, pnew, psale, pimg, pimgh, pcaption, poprice, posprice, pmaxprice, pcontents, pcsale, poptions, pdrating) {
    this.divid = did;
    this.productname = pname;
    this.productprice = price;
	this.producturl = purl;
	this.productpnew = pnew;
	this.productpsale = psale;	
	this.productimg = pimg;
	this.productimgh = pimgh;
	this.productcaption = pcaption;
	this.productpoprice = poprice;
	this.productposprice = posprice;
	this.productpmaxprice = pmaxprice;
	this.productpcontents = pcontents;
	this.productpcsale = pcsale;
	this.productpoptions = poptions;
	this.productpdrating = pdrating;
	
}

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 sortArray(val)
{	

	sorttype = val;

	if(val==3)
	{
		data.sort(sortByNameAscending);
	}
	else if(val==4)
	{
		data.sort(sortByNameDescending);
	}
	else if(val==1)
	{
		data.sort(sortByPriceAscending);
	}
	else if(val==2)
	{
		data.sort(sortByPriceDescending);
	}

	document.getElementById("top-paging").selectedIndex = psize;
	document.getElementById("bottom-paging").selectedIndex = psize;
	
	document.getElementById("top-sorting").selectedIndex = srttype;
	document.getElementById("bottom-sorting").selectedIndex = srttype;

	viewpaging(itotal,pagenum);
	
}

function pagingsizechange(val)
{
	if(val == 'top')
	{
		index = document.getElementById("top-paging").selectedIndex;
	}
	else
	{
		index = document.getElementById("bottom-paging").selectedIndex;
	}

	url = pagelink + "?sorttype=" + srttype + "&size=" + index + "&page=" + pagenum;

	document.location.href = url;
	document.getElementById("bottom-paging").selectedIndex = index;
//	viewpaging(len, 1);
	
}

function sortingchange(val)
{
	if(val == "top")
	{
		index = document.getElementById("top-sorting").selectedIndex;
	}
	else
	{
		index = document.getElementById("bottom-sorting").selectedIndex;
	}

	url = pagelink + "?sorttype=" + index + "&size=" + psize + "&page=" + pagenum;

	document.location.href = url;
	
}

function pagechange(len, curpage)
{
	index = document.getElementById("top-paging").selectedIndex;	
	url = pagelink + "?sorttype=" + srttype + "&size=" + index + "&page=" + curpage;
	document.location.href = url;
}

function viewpaging(len,curpage)
{	
	
	pagenum=curpage;
	pno = document.getElementById("top-paging").value;	
	

	
	totalpage=Math.ceil(len / pno);		

	if(curpage > totalpage && curpage != 404)
	{
		curpage = totalpage;
	}
	
	startpos = (curpage - 1) * pno;
	endpos = (curpage * pno) - 1;	
	
	var count = 0;
	var itemno=0;
	var itemnocat=0;
	
	if(curpage == 404)
	{
		ototal = itotal;
	}
	else
	{
		ototal = endpos - startpos;
	}
	var str = "<div class='p_box_main' id='paging_main'>";
	itemno = 1;
	itemnocat = 1;
	var lencount;
	var lencounter = 1;
	var count = 1;
	var totalseg = Math.ceil(len / pno);
	var curseg = 1;
	if(endpos < len)
	{
		lencount = (endpos - startpos) + 1;
	}
	else
	{
		lencount = (len - startpos);
	}
	lencount = Math.round((lencount / 3) - 0.51) * 3;

	for(i=0;i<len;i++)
	{
		var fixurl = "";
		if(livesite < 0.01)
		{}
		else
		{
			fixurl = "http://www.sleekhair.com/";
		}
		
		if((i>=startpos && i<=endpos) || (curpage==404))
		{
			if(itemno < 3)
			{
				dhtml = "<div class='p_box'><div class='p_box_right_bord'>";
			}
			else
			{
				dhtml = "<div class='p_box' style='padding-right:0px;'><div class='p_box_right_bord'  style='border:none;'>";
				itemno = 0;
			}
			itemno++;

			//dhtml += document.getElementById(data[i].divid).innerHTML;
			//Product Name
			dhtml += "<h2 style='height:55px;'><a href='"+ fixurl +"" + data[i].producturl + ".html' title='" + data[i].productname + "'>" + data[i].productname + "";
			//New Icon
			if(data[i].productpnew.length > 1)
			{
				dhtml += "<img src='http://site.sleekhair.com/store/images/new-paging.gif' border='0' vspace='0' width='39' height='13' hspace='0' alt='New' title='New'>";
			}
			//Sale Icon
			if(data[i].productpsale.length > 1)
			{
				dhtml += "<img src='http://site.sleekhair.com/store/images/sale-paging.gif' border='0' vspace='0' width='39' height='13' hspace='0' alt='Sale' title='Sale'>";
			}
			//Product Image
			var maxh = 120;
			var margin = Math.round((maxh - data[i].productimgh) / 2);
			dhtml += "</a></h2><div class='p_box_img'><div class='img_center'><a href='"+ fixurl +"" + data[i].producturl + ".html'><img src='" + data[i].productimg + "' align=top border=0 hspace=0 vspace=0 alt='" + data[i].productname + "' title='" + data[i].productname + "' style='margin-top: " + margin + "px; margin-bottom: " + margin + "px;'></a></div></div>";
			//Product Caption
			if(data[i].productcaption.length > 1)
			{
				dhtml += "<p style'height:65px;'>" + data[i].productcaption + "... <span><a href='"+ fixurl +"" + data[i].producturl + ".html' title='more'>more</a></span></p>";
			}
			else
			{
				dhtml += "<p style'height:65px;'></p>";
			}
			//Pd-rating
			if(data[i].productpdrating.length > 1){
				dhtml += "<div style='float:left;text-align:center;width:170px;'>" + data[i].productpdrating + "</div>";
			}
			//Product Price
			if(data[i].productpcontents == 0)
			{
				if(data[i].productpoprice > 0 || data[i].productposprice > 0)
				{
					// Max price
					if(data[i].productpmaxprice > 0)
					{
						dhtml += "<div class='p_price_new'>$";
						var tfprice = 0;
						if(data[i].productpoprice > 0)
						{
							tfprice = data[i].productpoprice;
						}
						else
						{
							tfprice = data[i].productposprice;
						}
						dhtml += tfprice;
						dhtml += "&nbsp;&nbsp;&nbsp;$" + data[i].productpmaxprice.toFixed(2) + "</div>";
					}
					else
					{
						if(data[i].productpoprice > 0 && data[i].productposprice > 0)
						{
							dhtml += "<div class='p_price_new'>$" + data[i].productpoprice.toFixed(2) + "</div>";
						}
						else
						{
							dhtml += "<div class='p_price_new'></div>";
						}
					}
				}
				else
				{
					dhtml += "<div class='p_price_new'></div>";
				}
				
				// Actual price
				var tfsprice = 0;
				if(data[i].productposprice > 0)
				{
					tfsprice = data[i].productposprice;
				}
				else
				{
					tfsprice = data[i].productpoprice;
				}
				if(data[i].productpcsale > 0)
				{
					dhtml += "<div class='p_price' style='color:#FF4201;'>Sale!&nbsp;";
				}
				else
				{
					dhtml += "<div class='p_price'>";
				}
				dhtml += "$" + tfsprice.toFixed(2) + "";
				
				if(data[i].productpoptions != '')
				{

					var cutarray =  data[i].productpoptions.split("CUT-HERE");
					var myArray = cutarray[0].split("SEPARATE");
					
					var ztemp = 0;
					st = data[i].productpoptions;
					var final = 0;
					for(z=0; z<myArray.length - 1; z++)
					{
						 st = myArray[z];
						 //oprice = data[i].productprice;
						 if(st.indexOf("(+") >= 0)
						 {
							var price = st.split("(+");
							var price1 = price[1].split(")");
							var temp = Number(price1[0]);
							if (final < temp)
							{
								final = temp;
							}
						 }

					}
					if(final < 0.001)
					{}
					else
					{
						final = final + Number(data[i].productprice.toFixed(2));
						dhtml += " - $" + final.toFixed(2) + "";
					}
				}
				// Rating 
				/*
				if(data[i].productpcontents == 0)
				{
					dhtml+="<code style='display:block;'><iframe src='http://site.sleekhair.com//store/scripts/showrating-cat.php?id=" + data[i].producturl + "&rating=4' frameborder='0' width='140' height='20' allowtransparency='100%' marginheight='0' scrolling='no' style='margin:0px; padding:0px; margin-left:15px; text-align:center;'></iframe></code>";
				}
				else
				{
					dhtml+="<code style='display:block;'></code>";
				}
				*/
				dhtml += "</div>";
				/*dhtml += "<code style='display:block;' id='ratingrp"+ i +"'></code>";
				dhtml += "<script src='http://site.sleekhair.com/store/scripts/rating_script.php?id=" + data[i].producturl + "&path=http://site.sleekhair.com/store&rating=4&divid=ratingrp"+ i +"'></script>";*/
			}
			else
			{
				dhtml += "<div class='p_price_new'></div>";
				dhtml += "<div class='p_price'></div>";
			}
			
			//
			str += dhtml;
			if(lencounter <= lencount)
			{
				if(itemnocat < 3)
				{
					str = str + "</div><div class='p_box_bottom_bord'></div></div>";
				}
				else
				{
					str = str + "</div><div class='p_box_bottom_bord'></div></div></div><div class='p_box_main'>";
					itemnocat = 0;
				}				
			}
			else
			{
				if(itemnocat < 3)
				{
					str = str + "</div><div style='float:left; width:150px; padding:0px; margin:0px 10px 0px 10px;'></div></div>";
				}
				else
				{
					str = str + "</div><div style='float:left; width:150px; padding:0px; margin:0px 10px 0px 10px;'></div></div></div><div class='p_box_main'>";
					itemnocat = 0;
				}						
			}
			
			itemnocat++;
			lencounter++;
			
		}	
	}	
	
	
	str = str + "</div>";

	document.getElementById("pagedisplay").innerHTML=str;
	
	
	distxt= " ";

	writepages(totalpage, startpos, endpos);
	
}


function writepages(totalpages, stpageno, endpageno)
{

	var distxt = "<code style='font-size:10px; font-family:Verdana, Arial, Helvetica, sans-serif; color:#333333; font-weight:bold; font-size:10px;'>Page : </code>";
	
	if(pagenum > 1)
	{
		distxt += "<a href='javascript:pagechange(" + totalpages + ", " + (pagenum - 1) + " );' style='font-size:10px; font-family:Verdana, Arial, Helvetica, sans-serif; color:#BD0042; font-weight:bold;'><< Prev</a> ";
	}
	
	
	if(totalpages > 26)
	{
		numarray = new Array();
		if(pagenum > 19)
		{
			startno = pagenum - 10;
			endno = pagenum + 10;
			
			if(pagenum == 1)
			{
				distxt += "<span>1</span> <span>......</span> ";
			}
			else
			{
				distxt += "<a href='javascript:pagechange(" + totalpages + ", 1);'>1</a> <span>......</span> ";
			}
			if(endno >= totalpages)
			{
				endno = totalpages - 1;
			}
			for(i=startno; i <= endno; i++)
			{
				if(i == pagenum)
				{
					distxt += "<span>" + i + "</span> ";
				}
				else
				{
					distxt += "<a href=javascript:pagechange(" + totalpages + "," + i + ");>" + i + "</a> ";
				}
			}
			if(endno == totalpage - 1)
			{
				if(pagenum == totalpages)
				{
					distxt += "<span>" + totalpages + "</span>";
				}
				else
				{
					distxt += " <a href=javascript:pagechange(" + totalpages + "," + totalpages + ");>" + totalpages + "</a>";
				}
			}
			else
			{
				if(pagenum == totalpages)
				{
					distxt += "<span>......</span> <a href='#'>" + totalpages + "</a>";
				}
				else
				{
					distxt += "<span>......</span> <a href=javascript:pagechange(" + totalpages + "," + totalpages + ");>" + totalpages + "</a>";
				}
			}
		}
		else
		{
			startno = 2;
			endno = 20;
			
			
			if(pagenum == 1)
			{
				distxt += "<span>1</span> ";
			}
			else
			{
				distxt += "<a href='javascript:pagechange(" + totalpages + ", 1);'>1</a> <span>......</span> ";
			}

			for(i=startno; i <= endno; i++)
			{
				if(i == pagenum)
				{
					distxt += "<span>" + i + "</span> ";
				}
				else
				{
					distxt += "<a href=javascript:pagechange(" + totalpages + "," + i + ");>" + i + "</a> ";
				}
			}
			distxt += "<span>......</span> <a href=javascript:pagechange(" + totalpages + "," + totalpages + ");>" + totalpages + "</a>";
		}
	}
	else
	{
//		distxt = "<span style='color: #000;'>Page </span> <img src='previous.gif'> ";
		for(i=1; i <= totalpages; i++)
		{
				if(i == pagenum)
				{
					distxt += "<span>" + i + "</span> ";
				}
				else
				{
					distxt += "<a href=javascript:pagechange(" + totalpages + "," + i + ");>" + i + "</a> ";
				}
		}
	}
	if(pagenum < totalpages)
	{
		distxt += " <a href='javascript:pagechange(" + totalpages + ", " + (pagenum + 1) + " );' style='font-size:10px; font-family:Verdana, Arial, Helvetica, sans-serif; color:#BD0042; font-weight:bold;'>Next >> </a>";
	}
	if(endpageno > itotal)
	{
		endpageno = itotal;
	}
	else
	{
		endpageno++;
	}
	var pagedisplay = "<div>Showing products : " + (stpageno + 1) + "-" + endpageno + " of " + itotal + "</div>";
	

document.getElementById('pnot_top').innerHTML=pagedisplay;
document.getElementById('pnot_bottom').innerHTML=pagedisplay;
	document.getElementById('top-numbers').innerHTML=distxt;
	document.getElementById('bottom-numbers').innerHTML=distxt;
}