// JavaScript Document
var STOCK_ID = 0;
var FIELD_START = 1;
var currentActive;
var sortingColumn = 0;
var ascending = true;
function sortByColumn(a, b) {
	var x = a[sortingColumn].toLowerCase();
	var y = b[sortingColumn].toLowerCase();
	if (ascending) return ((x < y) ? -1 : ((x > y) ? 1 : 0));
	return ((x > y) ? -1 : ((x < y) ? 1 : 0));
}

function drawStockList(sorting, subGroup, refreshList) {
	if (subGroup != '') {
		document.getElementById('menuImage').src = "images/" + subGroup.toLowerCase()  + "_on.gif";
	} else {
		document.getElementById('menuImage').src = "images/stocklist_on.gif";
	}
	var result = "<div id=\"tableLocation\">";
	result += "<table id=\"stockList\" cellspacing=\"0\" cellpadding=\"0\">";
	
	result += "<tr>";
	for (i=0; i < tableHeading.length; i++) {
		result += "<th scope=\"col\" onClick=\"drawStockList(" + (i+1);
		if (subGroup != '') result += ",'" + subGroup + "'";
		result += ")\">" + tableHeading[i][1] + "</th>";
	}
	result += "</tr>";
	if (sortingColumn == sorting) ascending = ! ascending;
	else ascending = true;
	if (refreshList) ascending = true;
	sortingColumn = sorting;
	stockList.sort(sortByColumn);
	for (i=0; i < stockList.length; i++) {
		var show = true;
		if (subGroup != '') {
			show = false;
			for (j=FIELD_START; j < stockList[i].length; j++) {
				if(stockList[i][j] == subGroup) show = true;
			}	
		} 
		if (show) {
			result += "<tr onMouseOver=\"setMouseOver(this);\" onMouseOut=\"setMouseOut(this);\" onClick=\"getStockItem('" + stockList[i][STOCK_ID] + "', this);\">";
		
			for (j=FIELD_START; j < stockList[i].length; j++) {
				result += "<td";
				result += ">" + ((stockList[i][j] != '')? stockList[i][j]:'&nbsp;')  + "</td>";
			}
			result += "</tr>";
		}
	}
	result += "</table>";
	result += "</div><div id=\"stockItemLocation\"></div></div>";
	document.getElementById("content").innerHTML =  result;
}

function setMouseOver(div) {
	div.style.backgroundColor = '#FFFFFF';
}

function setMouseOut(div) {
	if (currentActive != div) div.style.backgroundColor = '#A5B2C9';
}
function getStockItem(id, div) {
	document.getElementById("stockItemLocation").innerHTML = 'Loading...';
	if (currentActive != null) currentActive.style.backgroundColor = '#A5B2C9';
	div.style.backgroundColor = '#FFFFFF';
	currentActive = div;
	httpRequester.open("GET", "getStockItem.php?id=" + id, true);
	httpRequester.onreadystatechange = showItem;
	httpRequester.send(null);	
}

function showItem() {
	if ( httpRequester.readyState == 4) {
		if ( httpRequester.status == 200) {
			//window.alert (httpRequester.responseText);
			document.getElementById("stockItemLocation").innerHTML = httpRequester.responseText;
		} else {
//			window.alert ("Er is een fout opgetreden, probeer het nogmaals");
			document.getElementById("stockItemLocation").innerHTML = "An error occured, please wait a few seconds and try again";
		}
	}
}

function showPageResult() {
	if ( httpRequester.readyState == 4) {
		if ( httpRequester.status == 200) {
			//window.alert (httpRequester.responseText);
			document.getElementById("content").innerHTML = httpRequester.responseText;
		} else {
//			window.alert ("Er is een fout opgetreden, probeer het nogmaals");
			document.getElementById("content").innerHTML = "An error occured, please wait a few seconds and try again";
		}
	}
}

function requestItem(stockNumber) {
	document.getElementById('menuImage').src = "images/menu_off.gif";
	httpRequester.open("GET", "requestForm.php?stockNumber=" + stockNumber, true);
	httpRequester.onreadystatechange = showPageResult;
	httpRequester.send(null);		
}

function submitRequest() {
	httpRequester.open("POST", "submitForm.php", true);
	httpRequester.onreadystatechange = showPageResult;
	httpRequester.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	if (document.getElementById('name').value == '') {
		alert("please enter your name");
		return;
	}
	if (document.getElementById('email').value == '') {
		alert("please enter your e-mail");
		return;
	}
	param = 'name=' + document.getElementById('name').value;
	param += '&email=' + document.getElementById('email').value;
	param += '&subject=' + document.getElementById('subject').value;
	param += '&details=' + document.getElementById('details').value;
	param += '&body=' + document.getElementById('body').value;
	httpRequester.send(param);
}

function showImage(image) {
	document.getElementById('largeImageLocation').innerHTML = '<img src="stockImg.php?id=' + image + '&maxWidth=700&maxHeight=540">';
	//document.getElementById('leftFlash').style.display = 'none';	
	document.getElementById('largeImage').style.display = 'block';	
}

function hideImage() {
	document.getElementById('largeImage').style.display = 'none';	
}
function showPage(id) {
	switch (id) {
		case '2':
		document.getElementById('menuImage').src = "images/information_on.gif";
		break;
		case '3':
		document.getElementById('menuImage').src = "images/contact_on.gif";
		break;
	}
	httpRequester.open("GET", "getParagraphs.php?id=" + id, true);
	httpRequester.onreadystatechange = showPageResult;
	httpRequester.send(null);	
}

/* AJAX Initialization */
// JavaScript Document
function getHTTPRequestObject() {
  var xmlHttpRequest;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (exception1) {
      try {
        xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (exception2) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttpRequest = false;
  @end @*/
 
  if (!xmlHttpRequest && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlHttpRequest = new XMLHttpRequest();
    } catch (exception) {
      xmlHttpRequest = false;
    }
  }
  return xmlHttpRequest;
}

var httpRequester = getHTTPRequestObject(); // Create the xml http object on the page load
