// Javascript sort functions for Actinic section pages
// Provided as open source by ActiveStock.co.uk
// Richard Morrow 2009

// standard function to allow numerical sorting
function sortNumber(a,b)
{
return a - b;
}	

// the main sort function	
function sortProducts()
{
// the element with id "sortoption" is the drop down box with the types of sorting you want the user to choose
var option = document.getElementById("sortoption").value;
// the option "sort" is the default value at the top of the drop down list, usually saying "Sort by:"
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");
// loop through all the <div> elements and look for ones with the id "sort" (this needs setting up in Actinic)
		 for (i=0;i<divElems.length; i++){
			if (divElems[i].id){
			var strId = divElems[i].id;
			if (strId == "sort"){
			// the <div id="sort"> is placed at the top of the product layout so we need to
			// navigate up the document node tree by 2 to get to the <tr> that contains the
			// complete product row (this assumes you are using a table layout for product lists)
					myhtml[j] = divElems[i].parentNode.parentNode;
					// the first child element of the <div id="sort"> should have and id="<ProductReference>|<ProductName>"
					var checkID = divElems[i].firstChild.id.split("|");
					azName[j] = checkID[1];
					sortedazName[j] = checkID[1];
					var prodRef = checkID[0];
					var priceID = "sort-" + prodRef;
					// to get the price we need to look for an element with id="sort-<ProductReference>"
					// This is because we can't use the Actinic variable <ProductPriceRaw> because there
					// is a bug that means it is not reset when you change a product price
					var priceElem = document.getElementById(priceID);
					// if this element doesn't exist, it's probably because this product has no price
					// i.e. the prices are linked to components
					if (!priceElem)
					{
					//so in this case we use an element in the component layout with id="Zsort-<ProductReference>"
					var sortID = "Zsort-" + prodRef;
					priceElem = document.getElementById(sortID);
					}
					// The first child element of the "priceElem" element should have id="<ProductReference>|<TaxInclusivePrice>"
					// This is the workaround for the <ProductPriceRaw> problem mentioned above
					// We strip out the £ character so we get a number that we can sort with (change to your relevant currency symbol!)
					var priceCheck = priceElem.firstChild.id.split("|£");
					price[j] = priceCheck[1];
					sortedPrice[j] = priceCheck[1];
					j++;
				}	
			}
		}
var sortedHtml = new Array();
// Now we need to check what sorting option we're going to use
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;
	}
	// Now we should have 2 arrays - one sorted as required and one not sorted
	// Now we need to use that to figure out how to sort the product rows
		        for (x=0; x< sortedArray.length; x++)
        {
                for (y=0; y<=myhtml.length; y++)
                {
                        if (unsortedArray[y]==sortedArray[x])
                        {
								// Set the sortedHtml array to the same as the sorted name / price array
                                sortedHtml[x]=myhtml[y];
								// set the unsorted to a value that means it won't get selected more than once
                                unsortedArray[y] = -1; 
                                break;
                        }

                }

        }
// The table that holds all the products should have id="ProductTable"
// If you have a fragment at the top of the page you probably wnat it to 
// stay there, so give its first element an id="fragment-sort"
var theTable = document.getElementById("ProductTable");
var fragment = document.getElementById("fragment-sort");
// Now empty the product table and put in a blank new table
var newtbody = document.createElement("tbody");
theTable.removeChild(theTable.firstChild);
theTable.appendChild(newtbody);
// If there is a fragment, add it to the new table first
if(fragment)
{
var firstrow = fragment.parentNode.parentNode;
theTable.firstChild.appendChild(firstrow);
}
// Now loop through the sortedHtml array and append to the table to get the end result!!
for (x=0; x< sortedHtml.length; x++)
{
theTable.firstChild.appendChild(sortedHtml[x]);
}
}
}