//http_request is global handle to XMLHTTP instance
var http_request; 
function getMultiple(ob) 
{ 
  var arSelected = new Array(); 
  for (var i = 0; i < ob.options.length; i++) 
    if (ob.options[ i ].selected) 
      arSelected.push(ob.options[ i ].value);
  return arSelected;
}

function DisableEnableForm(xHow){ //xForm,
  var xForm = document.getElementsByName('frm_proprietati')[0];
  objElems = xForm.elements;
  for(i=0;i<objElems.length;i++){
    objElems[i].disabled = xHow;
  }
}

function makeRequest(action) { //url,
   //send a request to the server (GET url) an set action as trigger for the response

   http_request=false; 
   DisableEnableForm(true);
   var ajax_div = document.getElementById('ajax');
   
   ajax_div.innerHTML = '<img src="images/ajax-loader.gif" width="31" height="31" border="0" alt="">';
   ajax_div.style.display = 'block';

   if (window.XMLHttpRequest) { // Mozilla, Safari,...
       http_request = new XMLHttpRequest();
       if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
       }
   } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
       }   catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
           }
   } 

    if (!http_request) {
        alert('Cannot create an XMLHTTP instance');
        return false;
    } else {
		var url, functie;

		var tip_anunt_id = document.getElementById('tip_anunt_id');
		var cat_id = document.getElementById('cat_id');
		var country_id = document.getElementById('country_id');
		var judet_id = document.getElementById('judet_id');
		var zona_id = document.getElementById('zona_id');
		var cauta = document.getElementById('cauta');
		//var optiuni = getMultiple(cat_id);
    cauta.disabled = true;
		switch (action)
		{
			case 'updateCats': {
				url = 'ajax_cats.php?tip_anunt_id=' + tip_anunt_id.value;
				functie = "update('cat_id')";
			}break;
			case 'updateCountries': {
        if (cat_id.multiple)
        {
          var optiuni = getMultiple(cat_id);
          url = 'ajax_countries.php?m=1&cat_id=' + optiuni.toString() + '&tip_anunt_id=' + tip_anunt_id.value;
        }
        else
        {        
		  url = 'ajax_countries.php?cat_id=' + cat_id.value + '&tip_anunt_id=' + tip_anunt_id.value;
        }
				functie = "update('country_id')";
			}break;
			case 'updateJudete': {
        if (cat_id.multiple)
        {
          var optiuni = getMultiple(cat_id);
  				url = 'ajax_judete.php?m=1&cat_id=' + optiuni.toString() + '&tip_anunt_id=' + tip_anunt_id.value + '&country_id=' + country_id.value;
        }
        else
        {        
  				url = 'ajax_judete.php?cat_id=' + cat_id.value + '&tip_anunt_id=' + tip_anunt_id.value + '&country_id=' + country_id.value;
  			}
				functie = "update('judet_id')";

			}break;
			case 'updateZones': {
        if (cat_id.multiple)
        {
          var optiuni = getMultiple(cat_id);
				  url = 'ajax_zones.php?m=1&state_id=' + judet_id.value + '&cat_id=' + optiuni.toString() + '&tip_anunt_id=' + tip_anunt_id.value;
        }
        else
        {			
				  url = 'ajax_zones.php?state_id=' + judet_id.value + '&cat_id=' + cat_id.value + '&tip_anunt_id=' + tip_anunt_id.value;
				}
				functie = "update('zona_id')";
			}break;
		}
        //alert(url);
		http_request.onreadystatechange = new Function(functie + ';');
	    http_request.open('GET',url, true);
        http_request.send(null);
    }

}

function getResponse(type) {
   
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
           if (type=='xml') {
              return http_request.responseXML;
           } else {
              return http_request.responseText;
           }
        } else {
            alert('getResponse:: There was a problem with the request. Status:'+ http_request.status);
            return false;
        }
    } else {
        return false;
    }
}

function update(select) {
	
	var xmldoc = getResponse('xml');
	
	if (!xmldoc) return false;
	
	var opts = xmldoc.getElementsByTagName('options')[0];
	
	if (!opts) {
		//alert('Fisierul XML rezultat contine erori(nu este corect formatat/contine caractere nepermise)');
		return false;
	}
	if ((select=='cat_id')||(select=='country_id')||(select=='judet_id')||(select=='zona_id'))
	{
    var selObj = document.getElementById(select);
		var elems = opts.getElementsByTagName('option');

		//remove all elements from given selectbox
		selObj.length = 0;

		for(i = 0; i < elems.length; i++) {
			val = elems[i].getAttribute("value");
			txt = elems[i].getAttribute("text");
			selObj[i] = new Option(txt, val);
		}
	}
	
	var tip_anunt_id = document.getElementById('tip_anunt_id');
	var cat_id = document.getElementById('cat_id');
	var country_id = document.getElementById('country_id');
	var judet_id = document.getElementById('judet_id');
	var zona_id = document.getElementById('zona_id');

	var cauta = document.getElementById('cauta');

	switch (select)
	{
		case 'cat_id': {
			
			makeRequest('updateCountries');
		}break;
		case 'country_id': {
			
			makeRequest('updateJudete');
		}break;
		case 'judet_id': {
			
			makeRequest('updateZones');
		}break;
		case 'zona_id': {
			DisableEnableForm(false);
			var ajax_div = document.getElementById('ajax');
   
			ajax_div.innerHTML = '';
			ajax_div.style.display = 'none';
		}break;
	}
}
