/**
 * clears a select form element
 * 
 * @param targetSel - dom reference to the select element
 */
 
var mailsubject;
function clearSelect(targetSel) {
	if(targetSel!==null)
	for (var i=targetSel.length; i>=0; i--) {
		targetSel.options[i] = null;
	}
}
/**
 * sends a request under usage of a loading graphic - targettable has to be positioned relative
 *
 * @param srcref - reference to the select element with chosen data
 * @param target - form element name of target element
 */
function sendRequest(domref, target, select1, exe1, select2, select3,exe2, select4,exe3) {
	// skip if no target specified
	if(!target) return false;

	// save reference to next target
	if(domref) domref.followup = target;
	
	var req;
	try {
		req = window.XMLHttpRequest ? new XMLHttpRequest():
			new ActiveXObject("Microsoft.XMLHTTP");
	} catch (e) {
		// no AJAX Support
	}
	req.onreadystatechange = function() {
		if ((req.readyState == 4) && (req.status == 200)) {
			// merge empty line with response
			var data = eval('(' + req.responseText + ')');
			var targetRef = document.getElementById(target);
			var targetSel = targetRef.getElementsByTagName('select')[0];

			// make it visible
			targetRef.style.display = 'block';
			
			// clear old data
			clearSelect(targetSel);
			
			// fill with data from json response
			var i = 0;
			if (target=="material") {
				if (data.length==1){
				targetRef.style.display = 'none';
				for(var x in data) {
				if (data[x].standard !== null) targetSel.options[i++] = new Option(
					data[x].standard, 
					data[x].standard
				);
			}
			sendRequest(document.getElementById('hk').value, 'elem3', document.getElementById('hk').value,document.getElementById('ex1').value,data[x].standard,null,null,null,null);
			}
			
			else {
			targetSel.options[i++] = new Option('please choose...','');
			for(var x in data) {
				if (data[x].standard !== null) targetSel.options[i++] = new Option(
					data[x].standard, 
					data[x].standard
				);
			}
		}
		}
		else if (target=="execution1") {
				if (data.length==1){
				targetRef.style.display = 'none';
				for(var x in data) {
				if (data[x].standard !== null) targetSel.options[i++] = new Option(
					data[x].standard, 
					data[x].standard
				);
			}
			sendRequest(document.getElementById('hk').value, 'material', document.getElementById('hk').value,data[x].standard,null,null,null,null,null);
			}
			
			else {
			targetSel.options[i++] = new Option('please choose...','');
			for(var x in data) {
				if (data[x].standard !== null) targetSel.options[i++] = new Option(
					data[x].standard, 
					data[x].standard
				);
			}
		}
		}
		else if (target=="execution2") {
				if (data.length==1){
				targetRef.style.display = 'none';
				for(var x in data) {
				if (data[x].standard !== null) targetSel.options[i++] = new Option(
					data[x].standard, 
					data[x].standard
				);
			}
			sendRequest(document.getElementById('hk').value, 'elem4', document.getElementById('hk').value,document.getElementById('ex1').value,document.getElementById('hk1').value,document.getElementById('hk2').value,data[x].standard,null,null);
			}
			
			else {
			targetSel.options[i++] = new Option('please choose...','');
			for(var x in data) {
				if (data[x].standard !== null) targetSel.options[i++] = new Option(
					data[x].standard, 
					data[x].standard
				);
			}
		}
		}
		else if (target=="execution3") {
				if (data.length==1){
				targetRef.style.display = 'none';
				for(var x in data) {
				if (data[x].standard !== null) targetSel.options[i++] = new Option(
					data[x].standard, 
					data[x].standard
				);
			}
			sendRequest(document.getElementById('hk').value, 'elem5', document.getElementById('hk').value,document.getElementById('ex1').value,document.getElementById('hk1').value,document.getElementById('hk2').value,document.getElementById('ex2').value,document.getElementById('hk3').value,data[x].standard);
			}
			
			else {
			targetSel.options[i++] = new Option('please choose...','');
			for(var x in data) {
				if (data[x].standard !== null) targetSel.options[i++] = new Option(
					data[x].standard, 
					data[x].standard
				);
			}
		}
		}
		else if (target=="elem5"){
				if (data.length==1){
					targetRef.style.display = 'none';
				for(var x in data) {
				if (data[x].standard !== null) targetSel.options[i++] = new Option(
					data[x].standard, 
					data[x].standard
				);
			}
			sendRequest(this,'elem6');mailsubject = document.getElementById('hk').value+document.getElementById('ex1').value+document.getElementById('hk1').value+document.getElementById('hk2').value+document.getElementById('ex2').value+document.getElementById('hk3').value+document.getElementById('ex3').value+document.getElementById('hk4').value;document.getElementById('partnumber').innerHTML = mailsubject;;
			}
			
			else {
			targetSel.options[i++] = new Option('please choose...','');
			for(var x in data) {
				if (data[x].standard !== null) targetSel.options[i++] = new Option(
					data[x].standard, 
					data[x].standard
				);
			}
		}
		}
		else {
					targetSel.options[i++] = new Option('please choose...','');
			for(var x in data) {
				if (data[x].standard !== null) targetSel.options[i++] = new Option(
					data[x].standard, 
					data[x].standard);
		}
		}

			// clear all followups
			while(targetSel.followup) {
				targetRef = document.getElementById(targetSel.followup);

				// make it hidden
				targetRef.style.display = 'none';
				
				// mark next select
				targetSel = targetRef.getElementsByTagName('select')[0];
				
				// clear old data
				clearSelect(targetSel);
			}
		}
	}
	req.open('post', 'ajax.php');
	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	// send empty post with initial load
	req.send(domref !== null ? 'id='+domref.value+'&name='+domref.name+'&select1='+select1+'&exe1='+exe1+'&select2='+select2+'&select3='+select3+'&exe2='+exe2+'&select4='+select4+'&exe3='+exe3 : '');

	return false; // return false to avoid reload/recentering of the page
}
