function $(id){
	return document.getElementById(id)	
}

document.getElementsByClassName = function (needle) {
  var my_array = document.getElementsByTagName("*");
  var retvalue = new Array();
  var i;
  var j;

  for (i = 0, j = 0; i < my_array.length; i++) {
    var c = " " + my_array[i].className + " ";
    if (c.indexOf(" " + needle + " ") != -1)
      retvalue[j++] = my_array[i];
  }
  return retvalue;
}

function addEvent(obj, evType, fn) {
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, true);
		return true;
	} else if (obj.attachEvent) {
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} else {
		return false;
	}
}

function insertStyleSheet() {
	var style = document.createElement("link");	
	style.href = "/styles/calc/js.css";
	style.media = "all";
	style.rel = "stylesheet";
	style.type = "text/css";
	var head = document.getElementsByTagName("head");
	head[0].appendChild(style);
}

function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        ro = new XMLHttpRequest();
		ro.overrideMimeType('text/xml');
    }
    return ro;
}

var http = createRequestObject();

function sendRequest(action, theId) {
	var find_str;
	switch (action) {
		case "add":
			findStr = "itemA";
			break;
		case "update":
			findStr = "itemR";
			break;
	}
	theId = theId.replace(findStr, "");
	theId = theId.replace("A", "");
	http.open("GET", "process.php?action=" + action + "&items_id=" + theId, true);
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function handleResponse() {
	if (http.readyState == 4) {
		var response = http.responseXML.documentElement;
		var action = response.childNodes[0].firstChild.data;
		var itemsId = response.childNodes[1].firstChild.data;
		
		document.getElementById("volumeM3").innerHTML = response.childNodes[7].firstChild.data + "m&#179;";
		document.getElementById("volumeF3").innerHTML = response.childNodes[8].firstChild.data + "ft&#179;";
		
		document.getElementById("xvolumeM3").innerHTML = response.childNodes[7].firstChild.data + "m&#179;";
		document.getElementById("transitsM").innerHTML = response.childNodes[15].firstChild.data;
		document.getElementById("lutonsM").innerHTML = response.childNodes[16].firstChild.data;
		
		document.getElementById("xvolumeF3").innerHTML = response.childNodes[8].firstChild.data + "ft&#179;";
		document.getElementById("transitsF").innerHTML = response.childNodes[15].firstChild.data;
		document.getElementById("lutonsF").innerHTML = response.childNodes[16].firstChild.data;
		
		document.getElementById("areaMFull").innerHTML = response.childNodes[9].firstChild.data + " sq m";
		document.getElementById("areaMThreeQuarters").innerHTML = response.childNodes[10].firstChild.data + " sq m";
		document.getElementById("areaMHalf").innerHTML = response.childNodes[11].firstChild.data + " sq m";

		document.getElementById("areaFFull").innerHTML = response.childNodes[12].firstChild.data + " sq ft";
		document.getElementById("areaFThreeQuarters").innerHTML = response.childNodes[13].firstChild.data + " sq ft";
		document.getElementById("areaFHalf").innerHTML = response.childNodes[14].firstChild.data + " sq ft";
		
		switch (action) {
			case "add":
				var quantity = response.childNodes[4].firstChild.data;
				if (quantity == "1") {
					var description = response.childNodes[2].firstChild.data;
					var room = response.childNodes[3].firstChild.data;
					var volm3 = response.childNodes[5].firstChild.data;
					var volft3 = response.childNodes[6].firstChild.data;
					addRow(itemsId, description, room, quantity, volm3, volft3);
				} else {
					var volm3 = response.childNodes[5].firstChild.data;
					var volft3 = response.childNodes[6].firstChild.data;
					updateRow(itemsId, quantity, volm3, volft3);
				}
				break;
			case "update":
				var quantity = response.childNodes[4].firstChild.data;
				var volm3 = response.childNodes[5].firstChild.data;
				var volft3 = response.childNodes[6].firstChild.data;
				updateRow(itemsId, quantity, volm3, volft3);
				break;
		}
    }
}

function createInventory() {
	var current = document.getElementById("current");
	
	var awaitingSelections = document.getElementById("awaitingSelections");
	awaitingSelections.innerHTML = "Current inventory items";
	awaitingSelections.className = "";
	
	var empty = document.getElementById("empty");
	current.removeChild(empty);
	
	var summary = document.createElement("div");
	summary.id = 'summary';
	
	var h2 = document.createElement("h2");
	h2.innerHTML = 'Your calculator summary';
	summary.appendChild(h2);
		
	var xtransitsm = document.createElement("p");
	xtransitsm.className = "m3";
	xtransitsm.innerHTML = '<strong><span id="xvolumeM3">x</span></strong> will typically fill <strong><span id="transitsM">x</span> transit vans</strong> or <strong><span id="lutonsM">x</span> Luton vans</strong>';
	summary.appendChild(xtransitsm);
	
	var xtransitsf = document.createElement("p");
	xtransitsf.className = "ft3";
	xtransitsf.innerHTML = '<strong><span id="xvolumeF3">x</span></strong> will typically fill <strong><span id="transitsF">x</span> transit vans</strong> or <strong><span id="lutonsF">x</span> Luton vans</strong>';
	summary.appendChild(xtransitsf);
	
	var p = document.createElement("p");
	p.innerHTML = "Based on your information you will require a room with an area of:";	
	summary.appendChild(p);
	
	var p = document.createElement("p");
	p.className = "ft3 sizes";
	var strong = document.createElement("strong");
	strong.innerHTML = "Filled to 8ft high&#58; ";
	var span = document.createElement("span");
	span.id = "areaFFull";
	span.innerHTML = "x sq ft";
	strong.appendChild(span);
	p.appendChild(strong);
	summary.appendChild(p);
	
	var p = document.createElement("p");
	p.className = "ft3 sizes";
	var strong = document.createElement("strong");
	strong.innerHTML = "Filled to 6ft high&#58; ";
	var span = document.createElement("span");
	span.id = "areaFThreeQuarters";
	span.innerHTML = "x sq ft";
	strong.appendChild(span);
	strong.appendChild(span);
	p.appendChild(strong);
	summary.appendChild(p);
	
	var p = document.createElement("p");
	p.className = "ft3 sizes";
	var strong = document.createElement("strong");
	strong.innerHTML = "Filled to 4ft high&#58; ";
	var span = document.createElement("span");
	span.id = "areaFHalf";
	span.innerHTML = "x sq ft";
	strong.appendChild(span);
	p.appendChild(strong);
	summary.appendChild(p);
	
	var p = document.createElement("p");
	p.className = "m3 sizes";
	var strong = document.createElement("strong");
	strong.innerHTML = "Filled to 2.4m high&#58; ";
	var span = document.createElement("span");
	span.id = "areaMFull";
	span.innerHTML = "x sq m";
	strong.appendChild(span);
	p.appendChild(strong);
	summary.appendChild(p);
	
	var p = document.createElement("p");
	p.className = "m3 sizes";
	var strong = document.createElement("strong");
	strong.innerHTML = "Filled to 1.8m high&#58; ";
	var span = document.createElement("span");
	span.id = "areaMThreeQuarters";
	span.innerHTML = "x sq m";
	strong.appendChild(span);
	p.appendChild(strong);
	summary.appendChild(p);
	
	var p = document.createElement("p");
	p.className = "m3 sizes";
	var strong = document.createElement("strong");
	strong.innerHTML = "Filled to 1.2m high&#58; ";
	var span = document.createElement("span");
	span.id = "areaMHalf";
	span.innerHTML = "x sq m";
	strong.appendChild(span);
	p.appendChild(strong);
	summary.appendChild(p);
	
	var br = document.createElement("br");
	summary.appendChild(br);
	
	var printInventory = document.createElement("p");
	printInventory.id = "printThis";
	printInventory.innerHTML = '<a href="/print.php" target="_blank">Print my inventory</a>';
	summary.appendChild(printInventory);
	
	var top = document.getElementById("top");
	top.appendChild(summary);
	
	var inventory = document.createElement("table");
	inventory.id = "inventory";
	inventory.className = "inventory";
	var thead = document.createElement("thead");
	var tr = document.createElement("tr");
	var th1 = document.createElement("th");
	th1.className = "col1";
	th1.innerHTML = "Item";
	tr.appendChild(th1);
	
	var th2 = document.createElement("th");
	th2className = "col2";
	th2.innerHTML = "Room";
	tr.appendChild(th2);
	
	var th3 = document.createElement("th");
	th3className = "col3";
	th3.innerHTML = "Qty";
	tr.appendChild(th3);
	
	var th4 = document.createElement("th");
	th4.className = "col4 m3";
	th4.innerHTML = "m&#179;";
	tr.appendChild(th4);
	
	var th5 = document.createElement("th");
	th5.className = "col5 ft3";
	th5.innerHTML = "ft&#179;";
	tr.appendChild(th5);	
	
	var th6 = document.createElement("th");
	th6.className = "col6 rowControl";
	th6.innerHTML = "";
	tr.appendChild(th6);

	var th7 = document.createElement("th");
	th7.className = "col7 rowControl";
	th7.innerHTML = "";
	tr.appendChild(th7);	
	
	thead.appendChild(tr);
	inventory.appendChild(thead);
	
	var tbody = document.createElement("tbody");
	tbody.id = "inventoryRows";
	inventory.appendChild(tbody);
	
	current.appendChild(inventory);
	
	var totalVolume = document.createElement("p");
	totalVolume.id = "totalVolume";
	
	var strong = document.createElement("strong");
	strong.innerHTML = "Estimated total volume: ";
	
	var volumeM3 = document.createElement("span");
	volumeM3.id = "volumeM3";
	volumeM3.className = "m3";
	volumeM3.innerHTML = "m";
	
	var volumeF3 = document.createElement("span");
	volumeF3.id = "volumeF3";
	volumeF3.className = "ft3";
	volumeF3.innerHTML = "ft";
	
	strong.appendChild(volumeM3);
	strong.appendChild(volumeF3);
	
	totalVolume.appendChild(strong);
	
	current.appendChild(totalVolume);
	
	var p = document.createElement("p");
	p.style.textAlign = "right";
	p.innerHTML = "See the summary at the top to discover <br />which room size will be right for you."
	
	current.appendChild(p);
	
	var destroy = document.createElement("a");
	destroy.id = "destroy";
	destroy.href = "destroy.php?unit=" + units;
	destroy.accessKey = "e";
	destroy.className = "emptyList";
	destroy.innerHTML = "Empty inventory";
	
	var p = document.createElement("p");
	p.appendChild(destroy);
	
	current.appendChild(p);
}

function destroyInventory() {
	var current = document.getElementById("current");
	var awaitingSelections = document.getElementById("awaitingSelections");
	awaitingSelections.innerHTML = "Awaiting your selections...";
	awaitingSelections.className = "dull";
	var inventory = document.getElementById("inventory");
	current.removeChild(inventory);
	var empty = document.createElement("img");
	empty.id = "empty";
	empty.src = "/images/estimator_sample.gif";
	empty.width = "328";
	empty.height = "199";
	empty.alt="sample space estimate";
	current.appendChild(empty);
	var totalVolume = document.getElementById("totalVolume");
	current.removeChild(totalVolume);
	var destroy = document.getElementById("destroy");
	current.removeChild(destroy.parentNode);
	var summary = document.getElementById("summary");
	var top = document.getElementById("top");
	top.removeChild(summary);
}

function addRow(theId, theDescription, theRoom, theQuantity, theVolumeM3, theVolumeFT3) {
	var inventoryRows = document.getElementById("inventoryRows");
	var tr = document.createElement("tr");
	tr.id = "inventoryRow" + theId;
	var td1 = document.createElement("td");
	td1.innerHTML = theDescription;
	tr.appendChild(td1);
	
	var td2 = document.createElement("td");
	td2.innerHTML = theRoom;
	tr.appendChild(td2);
	
	var td3 = document.createElement("td");
	td3.id = "inventoryRowQ" + theId;
	td3.className = "centreTd";
	td3.innerHTML = theQuantity;
	tr.appendChild(td3);
	
	var td4 = document.createElement("td");
	td4.id = "inventoryRowVM3" + theId;
	td4.className = "m3";
	td4.innerHTML = theVolumeM3;
	tr.appendChild(td4);
	
	var td5 = document.createElement("td");
	td5.id = "inventoryRowVF3" + theId;
	td5.className = "ft3";
	td5.innerHTML = theVolumeFT3;
	tr.appendChild(td5);
	
	var td6 = document.createElement("td");
	td6.className = "rowControl";
	var a = document.createElement("a");
	a.id = "itemAA" + theId;
	a.className = "add";
	a.innerHTML = "&darr;";
	a.href="#";
	a.onclick = function() {return addItem(this.id);};
	td6.appendChild(a);
	tr.appendChild(td6);
	
	var td7 = document.createElement("td");
	td7.className = "rowControl";
	var a = document.createElement("a");
	a.id = "itemR" + theId;
	a.className = "remove";
	a.innerHTML = "&darr;";
	a.href="#";
	a.onclick = function() {return removeItem(this.id);};
	td7.appendChild(a);
	tr.appendChild(td7);
		
	var rows = new Array();
	rows.push(tr);
	for (var i = 0; i < inventoryRows.childNodes.length; i ++) {
		rows.push(inventoryRows.childNodes[i]);
	}
	for (var i = 0; i < rows.length; i ++) {
		inventoryRows.appendChild(rows[i]);
	}
	updateinventoryRowsClasses();
}

function updateRow(theId, theQuantity, theVolumeM3, theVolumeFT3) {
	if (theQuantity != "0") {
		var inventoryRowQ = document.getElementById("inventoryRowQ" + theId);
		inventoryRowQ.innerHTML = theQuantity;
		var inventoryRowVM3 = document.getElementById("inventoryRowVM3" + theId);
		inventoryRowVM3.innerHTML = theVolumeM3;
		var inventoryRowVF3 = document.getElementById("inventoryRowVF3" + theId);
		inventoryRowVF3.innerHTML = theVolumeFT3;
	} else {
		removeRow(theId);	
	}
	updateinventoryRowsClasses();
}

function removeRow(theId) {
	var inventoryRows = document.getElementById("inventoryRows");
	inventoryRow = document.getElementById("inventoryRow" + theId);
	inventoryRows.removeChild(inventoryRow);
}

function updateinventoryRowsClasses() {
	var inventoryRows = document.getElementById("inventoryRows");
	if (inventoryRows.childNodes.length != 0) {
		for (var x = 0; x < inventoryRows.childNodes.length; x ++) {
			if (x % 2 == 0) {
				inventoryRows.childNodes[x].className = "on";
			} else {
				inventoryRows.childNodes[x].className = "";
			}
		}
	} else {
		destroyInventory();
	}
}

function addItem(theID) {
	var empty = document.getElementById("empty");
	if (empty != null) {
		createInventory();
	}
	sendRequest("add", theID);
	return false;
}

function removeItem(theID) {
	sendRequest("update", theID);
	return false;
}

function clearItem(theID) {
	sendRequest("clear", theID);
	return false;
}

function changeUnit(theUnit) {
	document.getElementById("unit").href = "/styles/calc/unit" + theUnit + ".css";
	document.getElementById("units").value = theUnit;
	if (document.getElementById("printThis")) document.getElementById("printThis").firstChild.href = "/print.php?units=" + theUnit;
	if (document.getElementById("destroy")) document.getElementById("destroy").href = "/destroy.php?units=" + theUnit;
	return true;
}

function check_room()
{
	
	var pagesObj = document.getElementById("room_select");
	window.location = "room.php?id=" + pagesObj.options[pagesObj.selectedIndex].value;
}

function bodyLoad() {
	insertStyleSheet();
	
	if (document.getElementById("printThis") && document.getElementById("printThis").href == "#") document.getElementById("printThis").onclick = function() {window.print();}; 
	var adders = document.getElementsByClassName("add");	
	for (var x = 0; x < adders.length; x ++) {
		adders[x].href = "/";
		adders[x].onclick = function() {return addItem(this.id)};
	}
	var removers = document.getElementsByClassName("remove");	
	for (var x = 0; x < removers.length; x ++) {
		removers[x].href = "/";
		removers[x].onclick = function() {return removeItem(this.id)};
	}
	if (document.getElementById('unitF3')) document.getElementById('unitF3').onfocus = function() {return changeUnit("f");};
	if (document.getElementById('unitM3')) document.getElementById('unitM3').onfocus = function() {return changeUnit("m");};
	if (document.getElementById('rooms_id')) document.getElementById('rooms_id').onchange = function() {document.getElementById('filter_rooms').submit();};
}

addEvent(window, "load", bodyLoad);

function IsNumeric(strString){
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }

function isnum(value, id){
	if(IsNumeric(value)){
		
	} else {
		var len = value.length;
		var temp = value.substr(0, len-1);
		$(id).value = temp;
	}
}

function validateOrder(){
	if($('email').value==""){
		$('error').style.display='block'
		return false;	
	}
	else
	{
		$('error').style.display='none'
	}
	if($('telephone_field').value==""){
		$('error').style.display='block'
		return false;	
	}
	else
	{
		$('error').style.display='none'
	}
	
}

function updateBasketQty(id, qty){
	$(id).value = qty;
}
