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) {
    this.divid = did;
    this.productname = pname;
    this.productprice = price;
}

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==0)
	{
		data.sort(sortByNameAscending);		
	}
	else if(val==1)
	{
		data.sort(sortByNameDescending);
	}
	else if(val==2)
	{
		data.sort(sortByPriceAscending);
	}
	else if(val==3)
	{
		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 == 1)
	{
		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 == 1)
	{
		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;
//	document.getElementById("bottom-paging").selectedIndex = index;
//	viewpaging(len, 1);
	
}

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 = totalpage;
	}
	startpos = (curpage - 1) * pno;
	endpos = (curpage * pno) - 1;	
	
	var str='<div class="cat-product-row">';			
	var count = 0;
	var itemno=0;
	
	if(curpage == 404)
	{
		ototal = itotal;
	}
	else
	{
		ototal = endpos - startpos;
	}
	str = "<table border='0' cellspacing='0' cellpadding='0' width='100%' class='vertical' id='contents-table'><tr bgcolor='#ffffff'>";
	itemno = 1;
	for(i=0;i<len;i++)
	{
		if((i>=startpos && i<=endpos) || (curpage==404))
		{
			dhtml = document.getElementById(data[i].divid).innerHTML;
			if(itemno == 3)
			{
				str = str + "<td width='33%' class='horizontal-seperator vertical-seperator'>" +  dhtml;
			}
			else
			{
				str = str + "<td width='33%' class='horizontal-seperator'>" +  dhtml;
			}
			itemno++;
			if(itemno == 4)
			{
				itemno = 1;
				str += "</tr><tr bgcolor='#ffffff'>";
			}
		}	
	}	
	
	for(i=itemno; i < 4; i++)
	{
		str = str + "<td width='33%'>&nbsp;</td>";			
	}
	str = str + "</tr></table>";
	
	document.getElementById("pagedisplay").innerHTML=str;
	
	
	distxt= " ";
	/*if(curpage==404)
	{
		distxt = distxt + "<span>View All</span> Page ";
	}
	else
	{
		distxt = distxt + "<a href=javascript:pagechange("+len+",404);>View All</a> Page ";
	}

	if(curpage!=404 && curpage>1)
	{
			distxt = distxt + "<a href=javascript:pagechange("+len+"," + (curpage - 1) + ");>Prev</a> ";
	}*/
	
	writepages(totalpage);
	
/*	if(curpage!=404 && curpage<totalpage)
	{			
			distxt = distxt + "<a href=javascript:pagechange("+len+"," + (curpage + 1) + ");> Next</a>";
			}*/
	
	
	

	
	
}


function writepages(totalpages)
{

	var distxt = "";
	if(totalpages > 10)
	{
		numarray = new Array();
		if(pagenum > 4)
		{
			startno = pagenum - 2;
			endno = pagenum + 2;
			
			if(pagenum == 1)
			{
				distxt = "<a href='#' class='active'>1</a> <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 += "<a href='#' class='active'>" + i + "</a> ";
				}
				else
				{
					distxt += "<a href=javascript:pagechange(" + totalpages + "," + i + ");>" + i + "</a> ";
				}
			}
			if(endno == totalpage - 1)
			{
				if(pagenum == totalpages)
				{
					distxt += "<a href='#' class='active'>" + totalpages + "</a>";
				}
				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 = 5;
			distxt = "<a href=javascript:pagechange(" + totalpages + ",1);>1</a> ";
			for(i=startno; i <= endno; i++)
			{
				if(i == pagenum)
				{
					distxt += "<a href='#' class='active'>" + i + "</a> ";
				}
				else
				{
					distxt += "<a href=javascript:pagechange(" + totalpages + "," + i + ");>" + i + "</a> ";
				}
			}
			distxt += "<span>......</span> <a href=javascript:pagechange(" + totalpages + "," + totalpages + ");>" + totalpages + "</a>";
		}
	}
	else
	{
		distxt = "";
		for(i=1; i <= totalpages; i++)
		{
				if(i == pagenum)
				{
					distxt += "<a href='#' class='active'>" + i + "</a> ";
				}
				else
				{
					distxt += "<a href=javascript:pagechange(" + totalpages + "," + i + ");>" + i + "</a> ";
				}
		}
	}
	

	
	document.getElementById('top-numbers').innerHTML=distxt;
	document.getElementById('bottom-numbers').innerHTML=distxt;
}
