function sortNumber(a,b)
{
return a - b;
}		
function sortProducts()
{
var option = document.getElementById("sortoption").value;
if (option !="Sort")
{
var myhtml = new Array();
var price = new Array();
var azName = new Array();
var sortedPrice = new Array();
var sortedazName = new Array();
var sortedArray = new Array();
var unsortedArray = new Array();
var j=0;
	var divElems = document.getElementsByTagName("div");
		 for (i=0;i<divElems.length; i++){
			if (divElems[i].id){
			var strId = divElems[i].id;
			if (strId == "sort"){
					myhtml[j] = divElems[i].parentNode.parentNode;
					var checkID = divElems[i].firstChild.id.split("|");
					azName[j] = checkID[1];
					sortedazName[j] = checkID[1];
					var prodRef = checkID[0];
					var priceID = "sort-" + prodRef;
					var priceElem = document.getElementById(priceID);
					if (!priceElem)
					{
					var sortID = "Zsort-" + prodRef;
					priceElem = document.getElementById(sortID);
					}
					if (prodRef=="DL0001")
					{
					price[j] = 0;
					sortedPrice[j] = 0;
					}
					else
					{
					var priceCheck = priceElem.firstChild.id.split("|£");
					price[j] = priceCheck[1];
					sortedPrice[j] = priceCheck[1];
					}
					j++;
				}	
			}
		}
var sortedHtml = new Array();
if (option=="az")
	{
	sortedArray = sortedazName.sort();
	unsortedArray = azName;
	}
if (option=="za")
	{
	sortedArray = sortedazName.sort().reverse();
	unsortedArray = azName;
	}
if (option=="12")
	{
	sortedArray = sortedPrice.sort(sortNumber);
	unsortedArray = price;
	}
if (option=="21")
	{
	sortedArray = sortedPrice.sort(sortNumber).reverse();
	unsortedArray = price;
	}
		        for (x=0; x< sortedArray.length; x++)
        {
                for (y=0; y<=myhtml.length; y++)
                {
                        if (unsortedArray[y]==sortedArray[x])
                        {
                                sortedHtml[x]=myhtml[y];
                                unsortedArray[y] = -1; 
                                break;
                        }

                }

        }
var theTable = document.getElementById('ProductTable');
var fragment = document.getElementById("fragment-sort");
var newtbody = document.createElement("tbody");
theTable.removeChild(theTable.firstChild);
theTable.appendChild(newtbody);
if(fragment)
{
var firstrow = fragment.parentNode.parentNode;
theTable.firstChild.appendChild(firstrow);
}
for (x=0; x< sortedHtml.length; x++)
{
theTable.firstChild.appendChild(sortedHtml[x]);
}
}
}
