function fixContentHeight() {
	var windowHeight = document.documentElement.clientHeight;
	var footer = document.getElementById('footerRow');
	var content = document.getElementById('contentRow');
	var h = windowHeight - footer.clientHeight;
	content.style.height = h + 'px';
	var n = content.clientHeight;
	if (n != h) {
		h = h - n + h;
		if (h > 0)
			content.style.height = h + 'px';
	}
}
function setContactLink(ln,n) {
	var l = document.getElementById(ln);
	if (l != null) {
		var a = '@';
		var d = '.';
		var p = ':'
		var s = '';
		var e = n+s+a+'gef'+s+'ran'+s+d+s+'co'+s+d+s+'uk';
		var t = document.createTextNode(e);
		l.appendChild(t);
		l.href = 'ma'+s+'il'+s+'to'+s+p+s+e;
	}
}

var prevContentHeightLoad = window.onload;
window.onload = function() {
	if (prevContentHeightLoad)
		prevContentHeightLoad();
	fixContentHeight();
	setContactLink('ContactLink','sales');
	setContactLink('EnquiriesLink','sales');
}
window.onresize = fixContentHeight;

function MoveSelected(ListSourceID, ListDestinationID) {
	var listSource = document.getElementById(ListSourceID);
	var listDestination = document.getElementById(ListDestinationID);

	// Check if listSource have selected items.
	for(var i = listSource.options.length - 1; i >= 0; i--) {
		if(listSource.options[i].selected) {
			var newOption = document.createElement('OPTION');
			newOption.text = listSource.options[i].text;
			newOption.value = listSource.options[i].value;

			if (listDestination.options.add) {
				listDestination.options.add(newOption);
			}
			else {
				listDestination.add(newOption, null);
			}
			listSource.options[i] = null;
		}
	}
}

function SaveListIds(ListSourceID, HiddenDestinationID) {
	var listSource = document.getElementById(ListSourceID);
	var s = ";"; // this is then to check this method has been executed
	for(var i = listSource.options.length - 1; i >= 0; i--) {
		s = s + listSource.options[i].value + ";";
	}
	var hiddenDestination = document.getElementById(HiddenDestinationID);
	hiddenDestination.value = s;
}

var
  dtString = 0,
  dtNumber = 1,
  sortDown = 8595,
  sortUp = 8593;

function compNumbers(n1, n2)
{
  //Init. params
  var o1 = Number(n1[0]);
  var o2 = Number(n2[0]);

  //Comparing
  if (o1 > o2)
    return 1;
  else if (o1 < o2)
    return -1;
  else
    return 0;
}

function compStrings(n1, n2)
{
  //Init. params
  var o1 = n1[0] + "-" + n1[1];
  var o2 = n2[0] + "-" + n2[1];

  //Comparing
  if (o1 > o2)
    return 1;
  else if (o1 < o2)
    return -1;
  else
    return 0;
}

function compNumbersUp(n1, n2) {
	return -1 * compNumbers(n1, n2);
}
function compStringsUp(n1, n2) {
	return -1 * compStrings(n1, n2);
}

function sortTable(tableID, colIndex, dataType)
{
	var t = document.getElementById(tableID);
	var r = t.rows;
	//Init. uninited params
	if (!dataType) dataType = dtString;
	
	// determine sort order
	var sortOrder = sortDown;
	var headerHtml = r[0].cells[colIndex].innerHTML;
	if (headerHtml.charCodeAt(headerHtml.length - 1) == sortOrder) {
		sortOrder = sortUp;
	}

	//Go thru all the table to get all data
	var dataRows = new Array();
	var rowsMap = new Array();
	for (var i = 1; i < r.length; ++i) {
		var currCells = r[i].cells;

		if (currCells[colIndex] && currCells[colIndex].tagName.toLowerCase() == "td") {
			var dataItem = new Array();
			dataItem[0] = (currCells[colIndex].innerText ? currCells[colIndex].innerText : currCells[colIndex].textContent);
			if (dataItem[0])
				dataItem[0] = dataItem[0].toLowerCase();
			else
				dataItem[0] = "";
			dataItem[1] = i;

			//All TDs
			var trCells = new Array();
			for (var j = 0; j < currCells.length; ++j)
				trCells[j] = currCells[j].innerHTML;

			dataItem[2] = trCells;

			//Think about rows mapping
			rowsMap[rowsMap.length] = i;

			//Fin
			dataRows[dataRows.length] = dataItem;
		}
	}

	//Sort data that we got
	if (sortOrder == sortUp) {
		if (dataType == dtNumber)
			dataRows.sort(compNumbersUp);
		else
			dataRows.sort(compStringsUp);
	} else {
		if (dataType == dtNumber)
			dataRows.sort(compNumbers);
		else
			dataRows.sort(compStrings);
	}

	//And move it in the table
	for (var i = 0; i < dataRows.length; ++i) {
		var currCells = r[rowsMap[i]].cells;
		for (var j = 0; j < currCells.length; ++j)
			currCells[j].innerHTML = dataRows[i][2][j];
	}

	// remove arrows from all header cells
	for (var i = 0; i< r[0].cells.length; ++i) {
		var s = r[0].cells[i].innerHTML;
		if (s.charCodeAt(s.length - 1) == sortDown || s.charCodeAt(s.length - 1) == sortUp) {
			s = s.substr(0,s.length - 1);
		} else {
			if (s.substr(s.length - 4) == "arr;") {
				s = s.substr(0,s.length - 6);
			}
		}
		r[0].cells[i].innerHTML = s;
	}
	
	// add arrow to sort column
	r[0].cells[colIndex].innerHTML = r[0].cells[colIndex].innerHTML + String.fromCharCode(sortOrder); // "&darr;";
}

function ResizeTextBox(sender, eventargs) {
	var element = sender.get_element();
	var els = element.getElementsByTagName("TEXTAREA");
	if (els.length == 0) return;
	var ta = els[0];
	ta.style.height = element.style.height;
	/* element.children[2].style.width = element.style.width; - his line not needed as the control has width set to 100% */
	return false;
}