// $Id: ajax.js,v 1.11 2005/09/13 09:18:31 koliada Exp $
var req     = null;         // we only have one connection 
var baseurl = '/main/shops/ajax/?';     

function loadXMLDoc(url, callback_function) 
{
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) 
    {
        req = new XMLHttpRequest();
        req.onreadystatechange = callback_function;
        req.open("GET", url, true);
        req.send(null);
        return true;
    // branch for IE/Windows ActiveX version
    } 
    else if (window.ActiveXObject) 
    {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) 
        {
            req.onreadystatechange = callback_function;
            req.open("GET", url, true);
            //request.setRequestHeader("Accept-Charset", "windows-1251;q=1");
            req.send();
	        return true;
        }
        else
        {
        	return false;
        }
    }
    else
    {
    	// will use home-made RPC
    	
    	var span = document.body.appendChild(document.createElement('SPAN'));
    	span.style.display = 'none';
    	span.innerHTML = 'span<scr'+'ipt></scr'+'ipt>';
    	url += '&js=1';
    	setTimeout( function() 
    			    {
        				var s = span.getElementsByTagName("script")[0];
        				s.language = "JavaScript";
        				if (s.setAttribute) s.setAttribute('src', url); else s.src = url;
      				}
      				, 10);
    	window.jsOnReadyStateChange = function() { callback_function(true); };
    	return true;
    }
}

function update(caller_form, clear)
{
	var url = baseurl + 'type=1';
	if (!clear)
	{
		for (var i=0; i<caller_form.sections.options.length; i++)
		{
			if (caller_form.sections.options[i].selected)
			{
				url += '&sections[]=' + caller_form.sections.options[i].value;
			}
		}
		for (var i=0; i<caller_form.services.options.length; i++)
		{
			if (caller_form.services.options[i].selected)
			{
				url += '&services[]=' + caller_form.services.options[i].value;
			}
		}
		
		for (var i=0; i<caller_form.equipment.options.length; i++)
		{
			if (caller_form.equipment.options[i].selected)
			{
				url += '&equipment[]=' + caller_form.equipment.options[i].value;
			}
		}
		for (var i=0; i<caller_form.shops.options.length; i++)
		{
			if (caller_form.shops.options[i].selected)
			{
				url += '&shops[]=' + caller_form.shops.options[i].value;
			}
		}
	}
	
	if (!loadXMLDoc(url, 
		function(caller_form_)
		{
			return function(js)
			{
				if (req != null)
				{
					// only if req shows "complete"
					if (req.readyState == 4) 
					{
						// only if "OK"
					    if (req.status == 200) 
					    {
					        var response = req.responseXML.documentElement;
					        //alert(req.responseText);
					        //ta = document.getElementById("ta");
					        //ta.value = req.responseText;
					        if (response)
					        {
					        	var sections = response.getElementsByTagName('section');
					        	/*str = "";
					        	for (var i=0; i<sections.length; i++)
					        	{
					        		str += msg1[i].firstChild.nodeValue + "\n";
					        	}
					        	alert(str);*/
					        	var services = response.getElementsByTagName('service');
					        	var equipment = response.getElementsByTagName('equipment');
					        	var shops = response.getElementsByTagName('shop');
					        	
					        	var sel_sections = response.getElementsByTagName('sel_section');
					        	var sel_services = response.getElementsByTagName('sel_service');
					        	var sel_equipment = response.getElementsByTagName('sel_equipment');
					        	var sel_shops = response.getElementsByTagName('sel_shop');
					        	
								updateSelects(sections, services, equipment, shops, caller_form_, sel_sections, sel_services, sel_equipment, sel_shops);

					        	var msg1 = response.getElementsByTagName('msg1');
					        	str = "";
					        	for (var i=0; i<msg1.length; i++)
					        	{
					        		str += "\nSections:\n";
					        		str += msg1[i].firstChild.nodeValue + "\n";
					        	}
					        	var msg2 = response.getElementsByTagName('msg2');
					        	for (var i=0; i<msg2.length; i++)
					        	{
					        		str += "\nServices:\n";
					        		str += msg2[i].firstChild.nodeValue + "\n";
					        	}
					        	var msg3 = response.getElementsByTagName('msg3');
					        	for (var i=0; i<msg3.length; i++)
					        	{
					        		str += "\nEquipment:\n";
					        		str += msg3[i].firstChild.nodeValue + "\n";
					        	}
					        	var msg4 = response.getElementsByTagName('msg4');
					        	for (var i=0; i<msg4.length; i++)
					        	{
					        		str += "\nShops:\n";
					        		str += msg4[i].firstChild.nodeValue + "\n";
					        	}
					        	if (str.length > 0)
					        	{
					        		alert("Lost elements:\n\n\n" + str);
					        	}
					        }
					    }
					    else
					    {
					    	//alert("There was a problem retrieving the XML data:\n" + req.statusText);
					    }
					}
				}
				else
				{
					var cities = new String(window.jsIn).split('|');
					updateCitySelect(cities, caller_form_);
				}
			}
		} (caller_form)))
	{
		// don`t support RPC, fall back to regular form submit
		caller_form.submit();
	}
}

function updateSelects(sections, services, equipment, shops, caller_form, sel_sections, sel_services, sel_equipment, sel_shops)
{
   	// empty previous city contents

	while (caller_form.sections.hasChildNodes())
	{
		caller_form.sections.removeChild(caller_form.sections.firstChild);
	}
	for (var i = 0; i < sections.length; i++)
	{
		var option = document.createElement('OPTION');
		option.setAttribute('value', sections[i].getAttribute("id"));
		if (in_array( sections[i].getAttribute("id"), sel_sections))
		{
			option.setAttribute('selected', true);
		}
		option.innerHTML = sections[i].firstChild.nodeValue;
		caller_form.sections.appendChild(option);
	}

	while (caller_form.services.hasChildNodes())
	{
		caller_form.services.removeChild(caller_form.services.firstChild);
	}
	for (var i = 0; i < services.length; i++)
	{
		var option = document.createElement('OPTION');
		option.setAttribute('value', services[i].getAttribute("id"));
		if (in_array( services[i].getAttribute("id"), sel_services))
		{
			option.setAttribute('selected', true);
		}
		option.innerHTML = services[i].firstChild.nodeValue;
		caller_form.services.appendChild(option);
	}

	while (caller_form.equipment.hasChildNodes())
	{
		caller_form.equipment.removeChild(caller_form.equipment.firstChild);
	}
	for (var i = 0; i < equipment.length; i++)
	{
		var option = document.createElement('OPTION');
		option.setAttribute('value', equipment[i].getAttribute("id"));
		if (in_array( equipment[i].getAttribute("id"), sel_equipment))
		{
			option.setAttribute('selected', true);
		}
		option.innerHTML = equipment[i].firstChild.nodeValue;
		caller_form.equipment.appendChild(option);
	}

	while (caller_form.shops.hasChildNodes())
	{
		caller_form.shops.removeChild(caller_form.shops.firstChild);
	}
	for (var i = 0; i < shops.length; i++)
	{
		var option = document.createElement('OPTION');
		option.setAttribute('value', shops[i].getAttribute("id"));
		if (in_array( shops[i].getAttribute("id"), sel_shops))
		{
			option.setAttribute('selected', true);
		}
		option.innerHTML = shops[i].firstChild.nodeValue;
		caller_form.shops.appendChild(option);
	}
}

function in_array( needle, array)
{
	for ( i=0; i<array.length; i++)
	{
		if (array[i].getAttribute("id") == needle)
		{
			return true;
		}
	}
	
	return false;
}

