
function initFormAjax() {
	if(document.addEventListener) {
		document.getElementById('ufcountry').addEventListener('change',getList,false);
		document.getElementById('ufsize').addEventListener('change',getList,false);
		document.getElementById('ufcolour1').addEventListener('change',getList,false);
		document.getElementById('ufcolour2').addEventListener('change',getList,false);
	}
	if(document.attachEvent) {
		document.getElementById('ufcountry').attachEvent('onchange',getList,false);
		document.getElementById('ufsize').attachEvent('onchange',getList,false);
		document.getElementById('ufcolour1').attachEvent('onchange',getList,false);
		document.getElementById('ufcolour2').attachEvent('onchange',getList,false);
	}
	getList();
}

function getList() {

	var theBaseURL = "/_identifier_search.asp?";
	var theForm = document.getElementById("universalForm");
	var theFields = theForm.elements;

	for(var i=0; i<theFields.length; i++){
		theBaseURL = theBaseURL + (theFields[i].name + "=" + theFields[i].value + "&");
	}
	theBaseURL = theBaseURL.substr(0, theBaseURL.length - 1);
	grabFile(theBaseURL);
}

// AJAX code forking
function getHTTPOject() {
	var xhr = false;
	if (window.ActiveXObject) {
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				xhr = false;
			}
		}
	} else if (window.XMLHttpRequest) {
		try {
			xhr = new XMLHttpRequest();
		} catch(e) {
			xhr = false;
		}
	}
	return xhr;
}


// AJAX - Grabs a predefined file to pull back into the page
function grabFile(file) {
	var request = getHTTPOject();
	if (request) {
		request.onreadystatechange = function() {
		displayResponse(request);
		};
		request.open( "GET", file, true );
		request.send(null);
	}
}

//AJAX - Once the file is found this decides what to do with it.
function displayResponse(request) {
	if (request.readyState == 4) {
		//document.getElementById("universalForm").innerHTML = request.responseText;
		//get the select boxes
		var theForm = document.getElementById("universalForm");
		var colour1Options = theForm.colour1.options;
		var colour2Options = theForm.colour2.options;
		var featureOptions = theForm.features.options;
		var colour1Selected = theForm.colour1.value;
		var colour2Selected = theForm.colour2.value;
		var featureSelected = theForm.features.value;
		
		//reset them
		
		try {
			//execute the returned js with arrays of options
			eval(request.responseText);
			
			// clear options and update with new values
			colour1Options.length=1;
			//colour1Options[0] = new Option('- select option -','');
			for(var i=0; i<arrColours.length; i++) {
				colour1Options[i+1] = new Option(arrColours[i],arrColours[i]);
				if(arrColours[i] == colour1Selected) colour1Options[i+1].selected=true;
			}
			
			colour2Options.length=1;
			//colour2Options[0] = new Option('- select option -','');
			for(var i=0; i<arrColours.length; i++) {
				colour2Options[i+1] = new Option(arrColours[i],arrColours[i]);
				if(arrColours[i] == colour2Selected) colour2Options[i+1].selected=true;
			}
			
			featureOptions.length=1;
			//featureOptions[0] = new Option('- select option -','');
			for(var i=0; i<arrFeatures.length; i++) {
				featureOptions[i+1] = new Option(arrFeatures[i],arrFeatures[i]);
				if(arrFeatures[i] == featureSelected) featureOptions[i+1].selected=true;
			}
		}
		//if there's an error, do nothing.
		catch(e) {}
		
	}
}

