if(typeof FillSelect != 'function') { //controleer of functie al bestaat
	function FillSelect(selectId, xmlPage, params, emptyText)
	{
		var thisSelect = document.getElementById(selectId);
		var thisSelected = document.getElementById(selectId).value;
		thisSelect.options.length = 0; // empty select
		thisSelect.options[0] = new Option('loading...', '', false);

		// haal select gegevens op
		var xmlHttp = getXmlHttp();
		xmlHttp.open("POST", xmlPage, true);
		
		// Voor POST request zijn deze headers nodig
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		
		xmlHttp.onreadystatechange = function() {
			if(xmlHttp.readyState == 4 && xmlHttp.status == 200) // Ok
			{
				var xml = xmlHttp.responseXML;
				var item = xml.getElementsByTagName('item');

				thisSelect.options.length = 0; // empty select
				
				if (item.length > 0)
					thisSelect.options[0] = new Option(emptyText, '', false);

				var selectedOption = 0;
					
				for (i = 0; i < item.length; i++)
				{
					if (thisSelected == item[i].getElementsByTagName('id')[0].firstChild.nodeValue)
						selectedOption = i+1;
					thisSelect.options[i+1] = new Option(item[i].getElementsByTagName('text')[0].firstChild.nodeValue, item[i].getElementsByTagName('id')[0].firstChild.nodeValue, selectedOption);
				}
				
				if (item.length == 1)
					selectedOption = 1;

				thisSelect.selectedIndex = selectedOption;
				
				if ( (selectedOption > 0) && (thisSelect.onchange) )
					thisSelect.onchange();
			}
		}
		xmlHttp.send(params);
	}
}


if(typeof getListWiel != 'function') { //controleer of functie al bestaat
	function getListWiel(so)
	{
		var params = 'so=' + so;

		if (document.getElementById('wiel_merk'))
			params += '&merk=' + document.getElementById('wiel_merk').value;
		
		if (document.getElementById('wiel_dia'))
			params += '&dia=' + document.getElementById('wiel_dia').value;
		
		if (document.getElementById('wiel_seizoen'))
			params += '&seizoen=' + document.getElementById('wiel_seizoen').value;

		if (document.getElementById('wiel_br'))	
			params += '&br=' + document.getElementById('wiel_br').value;

		if (document.getElementById('wiel_bg'))	
			params += '&bg=' + document.getElementById('wiel_bg').value;
			
		if (document.getElementById('wiel_steek'))
			params += '&steek=' + document.getElementById('wiel_steek').value;
		
		FillWithHtmlContent('artikelen_lijst', 'gethtml/select_wiel.php', params, true);
		
		return false;
	}
}

if(typeof getListBand != 'function') { //controleer of functie al bestaat
	function getListBand(so)
	{
		document.getElementById('merkmenu').style.display = 'none';
		
		var params = 'so=' + so;
		
		params += '&code=' + document.getElementById('band_code').value;
		
		var merken = document.getElementsByName('band_merk');
		for (var i=0; i < merken.length; i++)
			if (merken[i].checked)
				params += '&merk[]=' + merken[i].value;
		
		FillWithHtmlContent('artikelen_lijst', 'gethtml/select_band.php', params, true);
		
		return false;
	}
}

if(typeof getListToebehoren != 'function') { //controleer of functie al bestaat
	function getListToebehoren(so)
	{
		var params = 'so=' + so;
		if (document.getElementById('toebehoren_merk'))
			params += '&merk=' + document.getElementById('toebehoren_merk').value;
		if (document.getElementById('toebehoren_draad'))
			params += '&draad=' + document.getElementById('toebehoren_draad').value;
		if (document.getElementById('toebehoren_kopvorm'))
			params += '&kopvorm=' + document.getElementById('toebehoren_kopvorm').value;
		if (document.getElementById('toebehoren_gewicht'))
			params += '&gewicht=' + document.getElementById('toebehoren_gewicht').value;
		
		FillWithHtmlContent('artikelen_lijst', 'gethtml/select_toebehoren.php', params, true);
	}
}

if(typeof getListAWB != 'function') { //controleer of functie al bestaat
	function getListAWB(so)
	{
		var params = 'so=' + so;
		
		params += '&merk=' + document.getElementById('wiel_merk').value;
		params += '&dia=' + document.getElementById('wiel_dia').value
		params += '&band=' + document.getElementById('band').value;

		FillWithHtmlContent('artikelen_lijst', 'gethtml/select_auto_wiel_bandenmaat.php', params, true);
		
		return false;
	}
}

if(typeof getListSetje != 'function') { //controleer of functie al bestaat
	function getListSetje(awb, so)
	{
		document.getElementById('merkmenu').style.display = 'none';
		
		var params = 'awb=' + awb + '&so=' + so;
		params += '&seizoen=' + document.getElementById('band_seizoen').value;
		
		var merken = document.getElementsByName('band_merk');
		for (var i=0; i < merken.length; i++)
			if (merken[i].checked)
				params += '&merk[]=' + merken[i].value;

		FillWithHtmlContent('artikelen_lijst', 'gethtml/select_setje.php', params, true);
		
		return false;
	}
}

if(typeof changeWW != 'function') { //controleer of functie al bestaat
	function changeWW(id, aantal)
	{
		var params = 'id=' + id	+ '&aantal=' + aantal;
			
		FillWithHtmlContent('winkelwagen', 'gethtml/winkelwagen_aanpassen.php', params, false);
	}
}

if(typeof SelectSelectedOption != 'function') { //controleer of functie al bestaat
	function SelectSelectedOption(selectContainer, optionValue)
	{
		for (var i = 0; i < selectContainer.options.length; i++)
		{
 			if (selectContainer.options[i].value == optionValue)
  				selectContainer.selectedIndex = i;
		}
	}
}

if(typeof FillAddress != 'function') { //controleer of functie al bestaat
	function FillAddress(adresString)
	{
		var name = '';
		var name2 = '';
		var contact = '';
		var address = '';
		var zipcode = '';
		var city = '';
		var country = '';
		
		adresArr = adresString.split(';');
		if (adresArr.length == 7)
		{
			name = adresArr[0];
			name2 = adresArr[1];
			contact = adresArr[2];
			address = adresArr[3];
			zipcode = adresArr[4];
			city = adresArr[5];
			country = adresArr[6];
		}
		
		document.getElementById('f_naam').value = name;
		document.getElementById('f_naam2').value = name2;
		document.getElementById('f_contact').value = contact;
		document.getElementById('f_adres').value = address;
		document.getElementById('f_postcode').value = zipcode;
		document.getElementById('f_plaats').value = city;
		document.getElementById('f_land').value = country;
	}
}

if(typeof getListOrders != 'function') { //controleer of functie al bestaat
	function getListOrders(so)
	{
		var params = 'so=' + so;

		if (document.getElementById('order_sort'))
			params += '&s=' + document.getElementById('order_sort').value;
		if (document.getElementById('order_month'))
			params += '&m=' + document.getElementById('order_month').value;
		if (document.getElementById('order_year'))
			params += '&y=' + document.getElementById('order_year').value;
		
		FillWithHtmlContent('artikelen_lijst', '/gethtml/select_order.php', params, true);
	}
}


function clearVehicle()
{
	var params = "clear=true";
	var xmlHttp = getXmlHttp();
	xmlHttp.open("POST", "/getxml/voertuig_select.php", true);
		
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
		
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4 && xmlHttp.status == 200) // Ok
		{
			location.reload(true);
		}
	}
	
	xmlHttp.send(params);
}

function dealerLogout()
{
	var params = "logoff=true";
	var xmlHttp = getXmlHttp();
	xmlHttp.open("POST", "/getxml/login.php", true);
		
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
		
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4 && xmlHttp.status == 200) // Ok
		{
			location.reload(true);
		}
	}
	
	xmlHttp.send(params);
}

function SwitchStatus(what, status)
{
	var params = "s=" + status;
	var xmlHttp = getXmlHttp();
	xmlHttp.open("POST", "/getxml/"+what+".php", true);
		
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
		
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4 && xmlHttp.status == 200) // Ok
		{
			location.reload(true);
		}
	}
	
	xmlHttp.send(params);
}

function openPopup(url, params, width, height)
{
	document.getElementById('popup_content').style.width = width + 'px';
	document.getElementById('popup_content').style.height = height + 'px';
	document.getElementById('popup').style.display = 'block';
	FillWithHtmlContent('popup_content', url, params, true);
}

function closePopup()
{
	document.getElementById('popup').style.display = 'none';
	document.getElementById('popup_content').innerHTML = '';
}

function bestelArtikel(groep, id)
{
	var file = '';
	if (id != null)
	{
		switch (groep)
		{
			case 1: file = 'wiel_overzicht.php'; break;
			case 2: file = 'band_overzicht.php'; break;
			case 3: file = 'setje_overzicht.php'; break;
			case 4: file = 'toebehoren_overzicht.php'; break;
		}
		
		if (file != '')	
			openPopup('/popup/'+file, 'id='+id, 800, 500);
	}
}

function zoekArtikel(s)
{
	if (s == '')
	{
		alert ('Vul een artikelnummer in');
		return false;
	}
	
	var params = "s=" + s;
	var xmlHttp = getXmlHttp();
	xmlHttp.open("POST", "/getxml/zoek_artikel.php", true);
	
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4 && xmlHttp.status == 200) // Ok
		{
			var xml = xmlHttp.responseXML;
			var groep = xml.getElementsByTagName('groep');
			var id = xml.getElementsByTagName('id');
	
			if ((groep.length > 0) && (id.length > 0))
			{
				groep = parseInt(groep[0].firstChild.nodeValue);
				id = parseInt(id[0].firstChild.nodeValue);
				bestelArtikel(groep, id);
			}
			else
				alert ('Artikelnummer niet gevonden');
		}
	}
	
	xmlHttp.send(params);
}
