// JavaScript Document

function print_r(theObj){
  if(theObj.constructor == Array || theObj.constructor == Object){
    document.getElementById('response').innerHTML +="<ul>";
    for(var p in theObj){
      if(theObj[p].constructor == Array || theObj[p].constructor == Object){
		document.getElementById('response').innerHTML +="<li>["+p+"] => "+typeof(theObj)+"</li>";
        document.getElementById('response').innerHTML +="<ul>";
        print_r(theObj[p]);
        document.getElementById('response').innerHTML +="</ul>";
      } else {
		document.getElementById('response').innerHTML +="<li>["+p+"] => "+theObj[p]+"</li>";
      }
    }
    document.getElementById('response').innerHTML +="</ul>";
  }
}

function addOption(theSelect, id, text){
	var el = document.createElement('OPTION');
    el.value = "index.php?option=com_content&task=category&sectionid=5&id="+id;
    el.text = text;
 	// First try the DOM2 method ...
	try {
		theSelect.add(el,null);
	}	
	catch (e) {// ... And if that doesn't work use the IE-only method
		theSelect.add(el);
	}
}

function clearOptions(theSelect){
	//alert(theSelect.options.length);
	var vt_itens = theSelect.options;
	for(i in vt_itens)
		theSelect.remove(vt_itens.index);
	return true;
}

function filter(codArea, codLocal, selectServico, publicoAlvo){
	selectServico.disabled = true;
	//alert('entrou: área='+codArea+', local='+codLocal+', público='+publicoAlvo+", select_servico="+selectServico.id);
	var url = '/site_sesi/classes/filter_ajax.php';
	var ajaxFilter = new Ajax.Request(url,
										  {
											method: 'post',
											parameters: 'ok=1&cod_area='+codArea+'&cod_local='+codLocal+'&publico_alvo='+publicoAlvo+'&select_servico='+selectServico.id,
											onSuccess: handleResultsText,
											onFailure: handleFailure
										  }
									  );
}


function handleResultsText(response){
	if(response.responseText == null){
		alert('Nenhuma informação encontrada para os filtros selecionados');
	} else {
		theResponse = response.responseText;
		items = theResponse.split('|');
		theSelect = items[1];
		clearOptions($(theSelect));
		addOption($(theSelect), 0, "");
		items = items[0].split(':');
		for(i=0; i<items.length; i++){
			optItems = items[i].split('#');
			if(typeof(optItems[1]) != 'undefined')
				addOption($(theSelect), optItems[0], optItems[1]);
		}
		//alert('alert');
		$(theSelect).disabled = false;
	}
	$()		
	//alert(response.responseXML);
}


function recurseXML(node, theSelect, level){
	alert("here"+level+", nodename="+node.getAttribute("name"));
	if(level == 20)
		return;
	for(i = 0; i < node.childNodes.length; i++){
			alert("i="+i);
			addOption(theSelect, node.childNodes[i].getAttribute("id"), node.childNodes[i].getAttribute("name"));
			if(node.childNodes[i].hasChildNodes)
				recurseXML(node.childNodes[i], theSelect, level+1);
	}
}

function handleResultsXML(response){

	if(response.responseXML == null){
		alert('Nenhuma informação encontrada para os filtros selecionados');
	} else {
		xmlDoc = response.responseXML.documentElement;
		theSelect = xmlDoc.childNodes[1].getAttribute("id");
		clearOptions($(theSelect));
		addOption($(theSelect), 0, "");
		addOption($(theSelect), xmlDoc.firstChild.getAttribute("id"), xmlDoc.firstChild.getAttribute("name"));
		if(xmlDoc.firstChild.hasChildNodes)
			recurseXML(xmlDoc.firstChild, $(theSelect), 0);
		$(theSelect).disabled = false;
	}
	$()		
	//alert(response.responseXML);
}

function handleFailure(response){
	alert('falha de comunicaçáo com o banco de dados.');
}
