function getHttpRequest() {
	var xmlhttp;
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xhr = new ActiveXObject("Msxml2.XMLHTTP");
	}
	return xhr;
}

function clickCell(table_cell, page_id, no_options) {
	var td = document.getElementById('choice1');
	var td2 = document.getElementById('choice2');
	var td3 = document.getElementById('choice3');
	var td4 = document.getElementById('choice4');
	var xhr = getHttpRequest();
	xhr.onreadystatechange=function() {
		if (xhr.readyState == 4) {
			if (table_cell.id == 'choice1') {
				if (no_options == 2) {
					td.style.background = "url('/img/q_top_on2.png') no-repeat";
					td2.style.background = "url('/img/q_bottom2.png') no-repeat";
				} else if (no_options == 3) {
					td.style.background = "url('/img/q_top_on3.png') no-repeat";
					td2.style.background = "url('/img/q_bottom3.png') no-repeat";
					td3.style.background = "url('/img/q_bottom3.png') no-repeat";
				} else if (no_options == 4) {
					td.style.background = "url('/img/q_top_on4.png') no-repeat";
					td2.style.background = "url('/img/q_bottom4.png') no-repeat";
					td3.style.background = "url('/img/q_bottom4.png') no-repeat";
					td4.style.background = "url('/img/q_bottom4.png') no-repeat";
				}
			} else if (table_cell.id == 'choice2') {
				if (no_options == 2) {
					td.style.background = "url('/img/q_top2.png') no-repeat";
					td2.style.background = "url('/img/q_bottom_on2.png') no-repeat";
				} else if (no_options == 3) {
					td.style.background = "url('/img/q_top3.png') no-repeat";
					td2.style.background = "url('/img/q_bottom_on3.png') no-repeat";
					td3.style.background = "url('/img/q_bottom3.png') no-repeat";
				} else if (no_options == 4) {
					td.style.background = "url('/img/q_top4.png') no-repeat";
					td2.style.background = "url('/img/q_bottom_on4.png') no-repeat";
					td3.style.background = "url('/img/q_bottom4.png') no-repeat";
					td4.style.background = "url('/img/q_bottom4.png') no-repeat";
				}
			} else if (table_cell.id == 'choice3') {
				if (no_options == 3) {
					td.style.background = "url('/img/q_top3.png') no-repeat";
					td2.style.background = "url('/img/q_bottom3.png') no-repeat";
					td3.style.background = "url('/img/q_bottom_on3.png') no-repeat";
				} else if (no_options == 4) {
					td.style.background = "url('/img/q_top4.png') no-repeat";
					td2.style.background = "url('/img/q_bottom4.png') no-repeat";
					td3.style.background = "url('/img/q_bottom_on4.png') no-repeat";
					td4.style.background = "url('/img/q_bottom4.png') no-repeat";
				}
			} else if (table_cell.id == 'choice4') {
				td.style.background = "url('/img/q_top4.png') no-repeat";
				td2.style.background = "url('/img/q_bottom4.png') no-repeat";
				td3.style.background = "url('/img/q_bottom4.png') no-repeat";
				td4.style.background = "url('/img/q_bottom_on4.png') no-repeat";
			}	
			
		}
	}
	xhr.open('GET', '/ajax/ajax.php?p='+page_id+'&choice='+table_cell.id);
	xhr.send(null);
}

function clickRow (table_cell) {
	var page_id = table_cell.id;
	if (table_cell.style.backgroundImage.search("row_on.png") == -1) 
		var choice = 'choice1';
	else
		var choice = 'choice2';
	var xhr = getHttpRequest();
	xhr.onreadystatechange=function() {
		if (xhr.readyState == 4) {
			var redirect = xhr.responseText;
			//alert(redirect);
			location.href = '/page/'+redirect;
		}
	}
	xhr.open('GET', '/ajax/ajax.php?p='+page_id+'&choice='+choice);
	xhr.send(null);
}

function setChecks(many, page_id) {
	var xhr = getHttpRequest();
	xhr.onreadystatechange=function() {
		if (xhr.readyState == 4) {
			var id = xhr.responseText;
			location.href='/page/'+id;
		}
	}
	xhr.open('GET', '/ajax/ajax.php?action=setchecks&many='+many+'&id='+page_id);
	xhr.send(null);
}

function submitForm2(inputaa) {
	if (inputaa.type == 'image')
		var form = inputaa.parentNode.parentNode.parentNode.parentNode.parentNode;
	else
		var form = inputaa;
	var elements = form.elements;
	var labels = form.getElementsByTagName('label');
	var show_error = false;
	for (i=0;i<elements.length;i++) {
		if (elements[i].type != 'submit' && elements[i].type != 'checkbox') {
			var td = elements[i].parentNode.parentNode.children[2]; 
			if (labels[i].innerHTML.search(/\*/) != -1 && elements[i].value == '') {
				td.innerHTML = 'Dit veld is verplicht.';
				show_error = true;
				elements[i].style.backgroundColor = 'lightyellow';
			}
			if (td.innerHTML != '&nbsp;')
				show_error = true;
		}
	}
	if (show_error)
		return false;
	else
		return true;
}

function changeLocation(input) {
	var xhr = getHttpRequest();
	xhr.onreadystatechange=function() {
		if (input.value == '') {
			var table = document.getElementById('location_table');
			table.style.display = 'none';
		} else {
			if (xhr.readyState == 4) {
				var text = xhr.responseText;
				var text_split = text.split(",");
				var id = text_split[0];
				var name = text_split[1];
				var address = text_split[3];
				var postal = text_split[4];
				var city = text_split[5];
				var phone = text_split[6];
				var table = document.getElementById('location_table');
				table.style.display = 'inline';
				table.children[0].children[0].children[1].innerHTML = name;
				table.children[0].children[1].children[1].innerHTML = address;
				table.children[0].children[2].children[1].innerHTML = postal;
				table.children[0].children[3].children[1].innerHTML = city;
				table.children[0].children[4].children[1].innerHTML = phone;
				
			}
		}
	}
	xhr.open('GET', '/ajax/ajax.php?action=changeLocation&vestiging='+input.value);
	xhr.send(null);
}

function getLocation(input) {
	var xhr = getHttpRequest();
	var t_name = document.getElementById('vest_name');
	var t_addr = document.getElementById('vest_addr');
	var t_postal = document.getElementById('vest_post');
	var t_city = document.getElementById('vest_wp');
	var t_phone = document.getElementById('vest_tel');
	var t_arrow = document.getElementById('arrow_def');
	xhr.onreadystatechange=function() {
		if (input.value == '') {
			t_name.style.display = 'none';
			t_addr.style.display = 'none';
			t_postal.style.display = 'none';
			t_city.style.display = 'none';
			t_phone.style.display = 'none';
			arrow_def.style.height = '313px';
			arrow_def.style.display = 'none';
		} else {
			if (xhr.readyState == 4) {
				var text = xhr.responseText;
				var text_split = text.split(",");
				var id = text_split[0];
				var name = text_split[1];
				var address = text_split[3];
				var postal = text_split[4];
				var city = text_split[5];
				var phone = text_split[6];
				t_name.children[1].innerHTML = name;
				t_addr.children[1].innerHTML = address;
				t_postal.children[1].innerHTML = postal;
				t_city.children[1].innerHTML = city;
				t_phone.children[1].innerHTML = phone;
				t_name.style.display = '';
				t_addr.style.display = '';
				t_postal.style.display = '';
				t_city.style.display = '';
				t_phone.style.display = '';	
				arrow_def.style.height = '208px';
				arrow_def.style.display = '';
			}
		}
	}
	xhr.open('GET', '/ajax/ajax.php?action=changeLocation&vestiging='+input.value);
	xhr.send(null);
}

function getLocationAddress(div) {
	var gegevens = document.getElementById('gegevens');
	var vestiging = div.id;
	var xhr = getHttpRequest();
	xhr.onreadystatechange=function() {
		if (xhr.readyState == 4) {
			var text = xhr.responseText;
			var text_split = text.split(",");
			var name = text_split[0];
			var address = text_split[1];
			var postal = text_split[2];
			var city = text_split[3];
			var phone = text_split[4];
			var email = text_split[5];
			gegevens.innerHTML = "<b>"+name+"</b><br />"+address+"<br />"+postal+" "+city+"<br /><b>T: </b>"+phone+"<br /><b>E: </b>"+email;
			div.className = 'blue';
		}
	}
	xhr.open('GET', '/ajax/www_ajax.php?v='+vestiging);
	xhr.send(null);
}

function removeLocation(div) {
	var gegevens = document.getElementById('gegevens');
	gegevens.innerHTML = '&nbsp;';
	div.className = 'orange';
}

function checkChoice(this_id, next_id) {
	var xhr = getHttpRequest();
	xhr.onreadystatechange=function() {
		if (xhr.readyState == 4) {
			var text = xhr.responseText;
			if (text == 'false')
				alert('Maak eerst een keuze');
			else
				location.href='/tab/kiezen/'+next_id;
		}
	}
	xhr.open('GET', '/ajax/www_ajax.php?action=checkChoice&this='+this_id);
	xhr.send(null);
}

function changeRow(div, page_id) {
	var div1 = document.getElementById('choice1');
	var div2 = document.getElementById('choice2');
	var div3 = document.getElementById('choice3');
	var div4 = document.getElementById('choice4');
	var choice = div.id;
	var xhr = getHttpRequest();
	xhr.onreadystatechange=function() {
		if (xhr.readyState == 4) {
			
		}
	}
	if (div.style.backgroundImage.search("row_bg_off.png") == -1) {
		div.style.backgroundImage = "url('/img/row_bg_off.png')";
		div1.style.backgroundImage = "url('/img/row_bg_off.png')";
		div2.style.backgroundImage = "url('/img/row_bg_off.png')";
		div3.style.backgroundImage = "url('/img/row_bg_off.png')";
		div4.style.backgroundImage = "url('/img/row_bg_off.png')";
	} else {
		div.style.backgroundImage = "url('/img/row_bg_on.png')";
		if (div.id != 'choice1')
			div1.style.backgroundImage = "url('/img/row_bg_off.png')";
		if (div.id != 'choice2')
			div2.style.backgroundImage = "url('/img/row_bg_off.png')";
		if (div.id != 'choice3' && div3 != null)
			div3.style.backgroundImage = "url('/img/row_bg_off.png')";
		if (div.id != 'choice4' && div4 != null)
			div4.style.backgroundImage = "url('/img/row_bg_off.png')";
	}
	xhr.open('GET', '/ajax/www_ajax.php?p='+page_id+'&choice='+choice);
	xhr.send(null);
}
