// JavaScript Document
////////////////////////////////////////
//////////////////// AJAX START
////////////////////////////////////////

var ajax_script = "ajax_io.php?";

function createHTTPObject() {
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try { 
			xmlhttp =  new ActiveXObject("Microsoft.XMLHTTP"); 
		} catch (e) {
			xmlhttp = new XMLHttpRequest();
		}
	} 	
	return xmlhttp;
}

var http = createHTTPObject();

//// Callback Handler (XML Reader / HTML Writer)
function ajax_XMLin() {
	
//	alert('retrieving XML');
	
	if(http.readyState == 4){
		if (http.status == 200) {
			
//			alert('XML Connected');
			
			xmldoc = http.responseXML.documentElement;
			
			products = xmldoc.getElementsByTagName("products");
			
			var type = xmldoc.getElementsByTagName("type")[0];
			
			type = type.firstChild.data;			
			
			if(type == "manufacturer"){
				
	//			alert(products.length);
				if(products.length > 0){
					var btnArea = getElement('btnarea');
					var oldFilter = getElement('newFilter');
					if(oldFilter != null){
						btnArea.removeChild(oldFilter);	
					}
					
					var newFilter = document.createElement('div');
					newFilter.setAttribute('class', 'filter');
					newFilter.setAttribute('id', 'newFilter');
					var newTitle = document.createElement('p');
					newTitle.innerHTML = "Select Model:-";
					newFilter.appendChild(newTitle);				
					var newSelect = document.createElement('select');
					newSelect.setAttribute("id","models");
					newSelect.setAttribute("onchange","ajaxcall('getProducts')");
					if(window.attachEvent){
						newSelect.attachEvent("onchange", addAjax);
					}
					
					var defaultOption = document.createElement('option');
					defaultOption.setAttribute("value", '--Please Select--');
					defaultOption.innerHTML = '--Please Select--';
					
					newSelect.appendChild(defaultOption);
					
					
					for (f = 0; f < products.length; f++) {
						
		//				alert(f);
						
						thisPrinterModel = products[f].getElementsByTagName("printerModel")[0].firstChild.data;
						
		//				addOption('subproducts', thisproduct, thisprodID);				
						
						var newOption = document.createElement('option');
						newOption.setAttribute("value", thisPrinterModel);
						newOption.innerHTML = thisPrinterModel;
						
						newSelect.appendChild(newOption);
		
						
					}
					newFilter.appendChild(newSelect);
					btnArea.appendChild(newFilter);				
				}
			}
			if(type == "model"){
				if(products.length > 0){
					var contentBody = getElement('contentbody');
					
					var oldContainer = getElement('ajaxContainer');
					
					if(oldContainer != null){
						contentBody.removeChild(oldContainer);
					}
					
					var list = document.getElementById('consumablesList');
					while(list.hasChildNodes()){
						list.removeChild(list.firstChild);
					}
					
					var listHead = document.createElement('li');
					listHead.innerHTML = "The following products are avaiable for this machine:";
					list.appendChild(listHead);
					
					for (f = 0; f < products.length; f++) {
						
						manufacturer = products[f].getElementsByTagName("manufacturer")[0].firstChild.data;
						printerModel = products[f].getElementsByTagName("printerModel")[0].firstChild.data;						
						reference = products[f].getElementsByTagName("reference")[0].firstChild.data;
						itemName = products[f].getElementsByTagName("itemName")[0].firstChild.data;
						
						var productText = document.createElement('li');
						
						productText.innerHTML = itemName + " Ref: " +reference;
						
						list.appendChild(productText);
						
						list.style.height = ((f+1) * 20) + "px";
					}
				}
			}
		} else {
			alert("There has been an error communicating with the server, please try again");
		}

	}
}

/// /// /// /// AJAX Command Handler
function ajaxcall(action){
		//alert(action);
		if(action == 'getPrinterModel'){
//			alert('start call');
			var rangeID = getValue('manufacturer');
			cmd = "ajaxCmd="+action+"&rangeID="+escape(rangeID);
			ajax_sendCmd(cmd);
		}
		if(action == 'getProducts'){
			var rangeID = getValue('models');
			cmd = "ajaxCmd="+action+"&rangeID="+escape(rangeID);
			ajax_sendCmd(cmd);
		}
}
/// /// /// ///

function ajax_sendCmd(cmd) {
//	alert('send command');
//	window.location = ajax_script+'rnd=' + Math.random()*4+'&'+cmd;
	
	http.open("GET", ajax_script+'rnd=' + Math.random()*4+'&'+cmd,true);
	if (http.overrideMimeType) {
		http.overrideMimeType('text/xml');
	}
	http.onreadystatechange = ajax_XMLin;
	http.send(null);
}




function getValue(el){
	return getElement(el).value;
}

function getElement(el){
	return document.getElementById(el);
}

function addOption(el, name, value){
//		alert("Element:"+el+" name:"+name+" value:"+value);
		try{
		 getElement(el).add(new Option(name, value), null) //add new option to end of "sample"
		}
		catch(e){ //in IE, try the below version instead of add()
		 getElement(el).add(new Option(name, value)) //add new option to end of "sample"
		}
}
function addAjax(){
	ajaxcall('getProducts');	
}
