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, pcaption, price, purl, pimg)
{
    this.divid = did;
    this.productname = pname;
    this.productcaption = pcaption;
    this.productprice = price;
	this.producturl = purl;
	this.productimg = pimg;
}

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,tmp,curpage)
{
	if(tmp==404)
	{
		index=4;
	}
	else
	{
		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='cat_dispaly' 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 = "";
		//fixurl = "http://www.elighters.com/";
	
		if((i>=startpos && i<=endpos) || (curpage==404))
		{
			if(itemno < 3)
			{
				dhtml = "<div class='cat_box' style='background: transparent url(http://site.elighters.com/default/images/cat_seperator.gif) no-repeat scroll right top; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; height: 325px;'>";
			}
			else
			{
				dhtml = "<div class='cat_box' style='height: 355px;'>";
				itemno = 0;
			}
			
			itemno++;
	
			dhtml += "<h3 style='height: 65px;'> <a href='"+ fixurl +"" + data[i].producturl + ".html' title='" + data[i].productname + "'>" + data[i].productname + "</a></h3>";
			
			dhtml += "<div style='height: 130px;'><a href='"+ fixurl +"" + data[i].producturl + ".html'><img src='" + data[i].productimg + "' border=0 hspace=0 vspace=0 alt='" + data[i].productname + "' title='" + data[i].productname + "'></a></div>";
			
			
			if(data[i].productprice>0)
			{
				dhtml += " <span style='float: left;' class='price'>Price : </span><span style='font-size: 14px; color: rgb(42, 42, 42);'>$" + data[i].productprice.toFixed(2) + "</span>";
			}
			if(data[i].productcaption=="")
			{
				dhtml += "<div style='text-align:left;width:145px; height:55px;float:left;color:#000; display:block'>&nbsp;</div>";
			}
			else
			{
				dhtml += "<div style='width:145px;text-align:left; font-size:11px; height:55px; color:#000; float:left; display:block; float:left; '>" + data[i].productcaption + "..</div>";
			}
			
			dhtml += "<div style='height: 19px; float:right;'><form method='post' action=http://order.store.yahoo.net/cgi-bin/wg-order?a-zlighters+";
			dhtml += data[i].producturl;
			dhtml += ">   <input name='vwitem' value='" + data[i].producturl  + "' type='hidden'>  <input name='vwcatalog' value='a-zlighters' type='hidden'> <input src='http://site.elighters.com/default/images/button_add_to_cart1.gif' style='margin-top: 5px;' type='image'>    </form></div> ";
				
			dhtml += "</div>";
		
			str += dhtml;
			
			itemnocat++;
			lencounter++;
			
		}	
	}	
	
	
	str = str + "</div>";

	document.getElementById("pagedisplay").innerHTML=str;
	
	distxt= " ";

	writepages(totalpage, startpos, endpos);
}


function writepages(totalpages, stpageno, endpageno)
{

	var distxt = "<a href='javascript:pagechange(" + totalpages + ",404," + (pagenum) + " );'><img src='http://site.elighters.com/default/images/viewall.gif' ></a><br>";
	
	if(pagenum > 1)
	{
		distxt += "<b><a href='javascript:pagechange(" + totalpages + ",0," + (pagenum - 1) + " );'>  < Prev </a> </b>";
	}
	
	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 + ",0, 1);'> 1 </a> <span>......</span> ";
			}
			if(endno >= totalpages)
			{
				endno = totalpages - 1;
			}
			for(i=startno; i <= endno; i++)
			{
				if(i == pagenum)
				{
					distxt += "<span>  " + i + " &nbsp;</span> ";
				}
				else
				{
					distxt += "<a href=javascript:pagechange(" + totalpages + ",0,"+ i +");> " + i + " </a>";
				}
			}
			if(endno == totalpage - 1)
			{
				if(pagenum == totalpages)
				{
					distxt += "<span> " + totalpages + " </span>";
				}
				else
				{
					distxt += "<a href=javascript:pagechange(" + totalpages + ",0," + totalpages + ");> " + totalpages + " </a>";
				}
			}
			else
			{
				if(pagenum == totalpages)
				{
					distxt += "<span>......</span> <a href='#'> " + totalpages + " </a>";
				}
				else
				{
					distxt += "<span>......</span> <a href=javascript:pagechange(" + totalpages + ",0," + totalpages + ");> " + totalpages + " </a>";
				}
			}
		}
		else
		{
			startno = 2;
			endno = 20;
			
			
			if(pagenum == 1)
			{
				distxt += "<span> 1 </span> ";
			}
			else
			{
				distxt += "<a href='javascript:pagechange(" + totalpages + ",0, 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 + ",0," + i + ");> " + i + " </a> ";
				}
			}
			distxt += "<span>......</span> <a href=javascript:pagechange(" + totalpages + ",0," + 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 + ",0," + i + ");> " + i + " </a> ";
				}
		}
	}
	if(pagenum < totalpages)
	{
		distxt += "<b> <a href='javascript:pagechange(" + totalpages + ",0, " + (pagenum + 1) + " );'>  Next >  </a></b>";
	}
	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;
}