function getXmlHttpRequest()
{
	/* all browsers */
	if (window.XMLHttpRequest)
	{
		return new XMLHttpRequest();
	}
	/* except for IE */
	else if (window.ActiveXObject)
	{
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return false;
}

function ajax_refresh()
{
	/* get Ajax object */
	var ajax = getXmlHttpRequest();

	/* fetch the form object */
	var form = document.forms['prinssearchform'];

	/* set the value of the cmd field */
	form.elements['cmd'].value = 'search_systems';

	/* create string variable */
	var formResult = '';

	/* iterate through the form elements */
	for (var i = 0; i < form.elements.length; i++)
	{
		/* check if the resultstring is empty */
		if (formResult != '')
		{
			/* and an ampersand to the string */
			formResult += '&';
		}

		/* check if the element is a radio button */
		if (form.elements[i].type == 'radio')
		{
			/* check if the element is selected */
			if (form.elements[i].checked == true)
			{
				/* add the fieldname and its value to the result string */
				formResult += form.elements[i].name + '=' + encodeURIComponent(form.elements[i].value);
			}
		}
		else
		{
			/* add the fieldname and its value to the result string */
			formResult += form.elements[i].name + '=' + encodeURIComponent(form.elements[i].value);
		}

		/* disable the form element */
		form.elements[i].disabled = true;
	}

	/* do Ajax request (JSON string expected) */
	ajax.onreadystatechange = function()
	{
		/* check request status */
		if (ajax.readyState == 4)
		{
			/* create string variable with ajax response text as value */
			eval('var json = ' + ajax.responseText);

			/* iterate through the form elements */
			for (var i = 0; i < form.elements.length; i++)
			{
				/* enable fields */
				form.elements[i].disabled = false;
			}

			/* update car model select box */

			/* check if there are options in the result */
			if (json.carmake_options)
			{
				/* reset car make options */
				form.car_make.options.length = 1;

				/* iterate through the options */
				for (key in json.carmake_options)
				{
					/* get current amount of options */
					count = form.car_make.options.length;

					/* add the option to the select box */
					form.car_make.options[count] = new Option(json.carmake_options[key], key);

					/* check if this option should be selected */
					if (json.carmake_selected && json.carmake_options[key] == json.carmake_selected)
					{
						/* set this option to be selected */
						form.car_make.selectedIndex = count;
					}
				}
			}

			/* check if there are options in the result */
			if (json.carmodel_options)
			{
				/* reset car make options */
				form.car_model.options.length = 1;

				/* iterate through the options */
				for (key in json.carmodel_options)
				{
					/* get current amount of options */
					count = form.car_model.options.length;

					/* add the option to the select box */
					form.car_model.options[count] = new Option(json.carmodel_options[key], key);

					/* check if this option should be selected */
					if (json.carmodel_selected && json.carmodel_options[key] == json.carmodel_selected)
					{
						/* set this option to be selected */
						form.car_model.selectedIndex = count;
					}
				}
			}

			/* update car capacity select box */

			/* check if there are options in the result */
			if (json.carcapacity_options)
			{
				/* reset car make options */
				form.car_capacity.options.length = 1;

				/* iterate through the options */
				for (key in json.carcapacity_options)
				{
					/* get current amount of options */
					count = form.car_capacity.options.length;

					/* add the option to the select box */
					form.car_capacity.options[count] = new Option(json.carcapacity_options[key], key);

					/* check if this option should be selected */
					if (json.carcapacity_selected && json.carcapacity_options[key] == json.carcapacity_selected)
					{
						/* set this option to be selected */
						form.car_capacity.selectedIndex = count;
					}
				}
			}

			/* update car years select box */

			/* check if there are options in the result */
			if (json.caryear_options)
			{
				/* reset car make options */
				form.car_year.options.length = 1;

				/* iterate through the options */
				for (key in json.caryear_options)
				{
					/* get current amount of options */
					count = form.car_year.options.length;

					/* add the option to the select box */
					form.car_year.options[count] = new Option(json.caryear_options[key], key);

					/* check if this option should be selected */
					if (json.caryear_selected && json.caryear_options[key] == json.caryear_selected)
					{
						/* set this option to be selected */
						form.car_year.selectedIndex = count;
					}
				}
			}

			/* reset the cursor */
			document.body.style.cursor = '';
		}
	}

	/* create ajax request */
	ajax.open('POST', form.action, true);

	/* set request header */
	ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	/* send ajax request */
	ajax.send(formResult);

	/* set UI cursor to wait icon (hourglass) */
	document.body.style.cursor = 'wait';

}

function SearchNumber(language)
{
	var oForm = document.prinssearchform;
	oForm.cmd.value = 'shownumberresult';
	oForm.action = "/" + language + "/search_results.html";
	oForm.submit();
}

function SearchSelect(language)
{
	var oForm = document.prinssearchform;
	oForm.cmd.value = 'showselectresult';
	oForm.action = "/" + language + "/search_results.html";
	oForm.submit();
}

function SearchSystems(field, language)
{
	document.prinssearchform.searchfield.value = field;
	var oForm = document.prinssearchform;
	//language = typeof(language) == 'undefined' ? 'nl' : language;
	//oForm.action = "/" + language;
	oForm.action = "/en/markets/cars/cars.html";
	ajax_refresh();
}

function toggleSearchButton(box, lang)
{
	enable = (box.selectedIndex > 0) ? true : false;
	imgSrcEnabled = '/data/static/images/search_system_show_' + lang + '.gif';
	imgSrcDisabled = '/data/static/images/search_system_show_' + lang + '_g.gif';
	searchbutton = document.getElementById('searchbutton');
	searchbutton.src = (enable === true) ? imgSrcEnabled : imgSrcDisabled;
	searchbutton.onclick = (enable === true) ? function() {SearchSelect(lang);} : '';
	searchbutton.style.cursor = (enable === true) ? 'pointer' : 'default';
}

function clearOptions(boxid)
{
	document.getElementById(boxid).options.length = 1;
}
