function vardump( a ){
	aux = "";
	if( typeof a == "object" ){
		var espaco = ( typeof arguments[1] != "undefined" )?arguments[1]:"..";
		if( typeof a.nodeType != "undefined"){
			return "[NODE] = ( '"+ a.innerHTML.substr( 0, 15 ) +"...' )\n";
		}
		aux += typeof a + " {\n";
		for( i in a ){
			aux += espaco + "["+i+"]" + " = " + vardump( a[i], espaco + ".." );
		}
		aux += espaco + "}\n";
	}else{
		aux += typeof a + " '" + a + "'\n"
	}
	return aux;
}
function isArray()
{
  if (typeof arguments[0] == 'object')
  {
    var criterion = arguments[0].constructor.toString().match(/array/i);
   return (criterion != null);
  }
  return false;
}
$(document).ready(function(){
	var valores=new Array();
	var campos=new Array();
	var textos=new Array();
	var comboGlobal=new Array();
	var tcampos="";
	var vals="";
	var txts = "";
	var xmlGlobal="";
	$("select").each(function(i){
		arr_chave = $("select").eq(i).attr("name");
		tcampos+=$("select").eq(i).attr("name")+"|";
		valores[$("select").eq(i).attr("name")]=[];
		textos[$("select").eq(i).attr("name")]=[];
		$.each($("select:eq("+i+") option"), function(nv){
			vals+=$("select").eq(i).attr("name")+"[value][]="+$(this).val()+"&";
			txts+=$("select").eq(i).attr("name")+"[text][]="+$(this).text()+"&";
			valores[$("select").eq(i).attr("name")][nv]=$(this).val();
			textos[$("select").eq(i).attr("name")][nv]=$(this).text();
		});
		$("select").eq(i).attr('tabindex',i);
		campos[i] = $("select").eq(i).attr("name");
	});
	$.ajax({
		type:"POST",
		url:"teste.php",
		data:vals+txts,
		dataType:"xml",
		success: function(xml)
		{
			xmlGlobal = xml;
			$("select").each(function(i){
				var combo=new Array();
				combo[""]=$("select:eq("+i+") option:eq(0)").text();
				var tagnamesel = $(this).attr("name");
				$($(this).attr("name"), xml).each(function(i){
					$(this).children().each(function(){
						combo[$(this).attr("value")] = $(this).text();
						comboGlobal[tagnamesel] = [$(this).attr("value")];
						comboGlobal[tagnamesel][$(this).attr("value")]=$(this).text();
					})
				})
				$("#"+$(this).attr("name")).html("");
				$("#"+$(this).attr("name")).addOption(combo, false);
			})
			//alert($("select").html());
			$("select:enabled:not(:last)").change(function(){
				var tagSel = new Array();
				var selecoes = new Array();
				var vals="";
				var txts = "";
				var nome_campo = $(this).attr("name");
				var php_selected = "";
				var initial=jQuery.inArray(nome_campo, campos);
				var c=initial+1;

				//alert($("select:not(:enabled) option:eq(1)").text());

				$("select:gt("+initial+")").each(function(i){
					var combo=new Array();
					combo[""]=$("select:eq("+c+") option:eq(0)").text();
					$($(this).attr("name"), xmlGlobal).each(function(i){
						$(this).children().each(function(){
							combo[$(this).attr("value")] = $(this).text();
						})
					})
					$("#"+$(this).attr("name")).html("");
					$("#"+$(this).attr("name")).addOption(combo, false);
					c++;
				});
				$("select:gt("+initial+")").each(function(i){
					$("#"+$(this).attr("name")).selectOptions($("#"+$(this).attr("name")).selectedValues(),true);
					$(this).children().each(function(){
						vals+=campos[initial+1]+"[value][]="+$(this).val()+"&";
						txts+=campos[initial+1]+"[text][]="+$(this).text()+"&";
					})
					initial++;
				});

				var selecteds = $("select").selectedValues();
				if($("select:eq(0):not(:enabled) option:eq(1)").text()!="")
				{
					selecteds[0] = $("select:not(:enabled) option:eq(1)").text();
				}
				//alert(selecteds);
				//alert($("select:not(:enabled)").selectedValues());

				jQuery.each(selecteds, function(i){
					if(this!="")
					{
//						php_selected+=$("select:not(:enabled) option:eq(1)").text()+"&";
						php_selected+=campos[i]+"[selected]="+this+"&";
					}
				});
				$.ajax({
					type:"POST",
					url:"teste.php",
					data:php_selected+vals+txts,
					dataType:"xml",
					success: function(xml2)
					{
						var child = "";
						var valSel=new Array();
						var comboSel = new Array();
						numSel = $("select:enabled").size();
						//alert(numSel);
						$("selected",xml2).children().each(function(i){
							tagSel[i]=this.tagName;
							selecoes[this.tagName]=$(this).text();
						});
						jQuery.each(tagSel, function(i){
							var comboTmp = new Array();
							var tagAtual=tagSel[i];
							comboSel[tagAtual]=[];
							$(tagSel[i], xmlGlobal).children().each(function(){
								comboTmp[$(this).attr("value")] = $(this).text();
							//	alert($(this).attr("value"));
								comboSel[tagAtual] = comboTmp;
							})
						})
						$("select").each(function(i){
							var combo2 = new Array();
							combo2[""]=$("select:eq("+i+") option:eq(0)").text();
							var depura = $(this).attr("name");
							var condition = false;
							$($(this).attr("name"),xml2).each(function(i){
								$(this).children().each(function(){
									combo2[$(this).attr("value")] = $(this).text();
								})
								if($(this).children().size()=="1")
								{
									condition=true;
								}
								valSel[depura] = $("#"+depura+" option:selected").text();
							})
							$("#"+$(this).attr("name")).html("");
							//alert($(this).attr("name")+"\n"+vardump(combo2));
							$("#"+$(this).attr("name")).addOption(combo2, condition);
							$("#"+$(this).attr("name")).selectOptions(valSel[$(this).attr("name")]);
							if(typeof(selecoes[$(this).attr("name")])!="undefined")
							{
								//alert($(this).attr("name"));
								$("#"+$(this).attr("name")).addOption(comboSel[$(this).attr("name")], false);
								$("#"+$(this).attr("name")).selectOptions(selecoes[$(this).attr("name")]);
								//$("#"+$(this).attr("name")).selectOptions(selecoes[$(this).attr("name")]);
							}
							$(this).children().each(function(){
								//alert($(this).val());
								if($(this).text()=="") $(this).remove();
							})
						})
					}
				});
			})
		}
	})
})
