function GetXmlHttpObject() 
{
	var xmlhttp = false;
	
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E)
		{
			xmlhttp = false;
		}
	}
	@else
		xmlhttp = false;
	@end @*/

	if (!xmlhttp && typeof XMLHttpRequest != "undefined") 
	{ 
		try 
		{ 
			xmlhttp = new XMLHttpRequest(); 
		} 
		catch (e) 
		{ 
			xmlhttp = false; 
		} 
	} 
	return xmlhttp; 
}

function carGetListOfModelsForTestDrive(wsUrl, brandElId, clientElId, progressElId, modelId)
{
	var soapMessage =	"<?xml version='1.0' encoding='utf-8'?>\n" +
						"<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>\n" +
						"<soap:Body>\n" +
						"<GetTestDriveModels xmlns='http://ephautoklub.cms.snt.hr/'>" +
						"<brandId>" + carGetBrandId(brandElId) + "</brandId>" +
						"<rowCount>0</rowCount>" +
						"</GetTestDriveModels>" +
						"</soap:Body>" +
						"</soap:Envelope>"

	var soapAction = "\"http://ephautoklub.cms.snt.hr/GetTestDriveModels\"";

	carGetListOfModelsBase(wsUrl, brandElId, clientElId, progressElId, modelId, soapMessage, soapAction)
}

function carGetListOfModels(wsUrl, brandElId, clientElId, progressElId, modelId)
{
	var soapMessage =	"<?xml version='1.0' encoding='utf-8'?>\n" +
						"<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>\n" +
						"<soap:Body>\n" +
						"<GetModels xmlns='http://ephautoklub.cms.snt.hr/'>" +
						"<brandId>" + carGetBrandId(brandElId) + "</brandId>" +
						"<coachTypeId>0</coachTypeId>" +
						"<motorTypeId>0</motorTypeId>" +
						"<priceId>0</priceId>" +
						"<rowCount>0</rowCount>" +
						"</GetModels>" +
						"</soap:Body>" +
						"</soap:Envelope>"
	
	var soapAction = "\"http://ephautoklub.cms.snt.hr/GetModels\"";
						
	carGetListOfModelsBase(wsUrl, brandElId, clientElId, progressElId, modelId, soapMessage, soapAction)
}

function carGetBrandId(brandElId)
{
	var brandId = 0;
	var brandEl = document.getElementById(brandElId);
	if(brandEl)
	{
		brandId = brandEl.options[brandEl.selectedIndex].value;
	}
	
	return brandId;
}

function carGetListOfModelsBase(wsUrl, brandElId, clientElId, progressElId, modelId, soapMessage, soapAction)
{
	brandId = carGetBrandId(brandElId);
	
	var modelsEl = document.getElementById(clientElId);
	
	if (modelsEl)
	{
	
		if (brandId == 0)
		{
			modelsEl.options.length = 1;
			return;
		}

		var xmlhttp = GetXmlHttpObject();
		if(xmlhttp)
		{
			xmlhttp.open("POST", wsUrl, true);

			manageProgressBar(progressElId, true);

			xmlhttp.onreadystatechange = function()
			{
				if (xmlhttp.readyState == 4)
				{
					var returnedText = xmlhttp.responseText;

					if (modelsEl)
					{
						var nameValuePairs = returnedText.split("#");
						
						modelsEl.options.length = 1;
						if (nameValuePairs.length > 2)
						{
							for (var i=1; i<nameValuePairs.length-1; i++)
							{
								var modelValuePairs = nameValuePairs[i].split("|");

								if (modelValuePairs && modelValuePairs.length == 2)
								{
									var id = modelValuePairs[0];
									var name = modelValuePairs[1].replace('_and_', '&');

									modelsEl.options[i] = new Option(name, id);
									if (modelId == id) modelsEl.options[i].selected = true;
								}			
							}
						}
						
						manageProgressBar(progressElId, false);
					}
				}
			}

 			xmlhttp.setRequestHeader("SOAPAction", soapAction)
			xmlhttp.setRequestHeader("Content-Type", "text/xml")

			xmlhttp.send(soapMessage);
		}
	}
}

function manageProgressBar(progressElId, display)
{
	var progressEl = document.getElementById(progressElId);
	if (progressEl)
	{
		if (display)
		{
			progressEl.style.display = 'block';
		}
		else
		{
			progressEl.style.display = 'none';
		}
	}
}
