 var allCasas = null;
 var inputsGlobs = null;
 
 function setAllCasasVar(listaCasas){
 	allCasas = new Array();
 	idxAllCasas = 0;
 	inputsGlobs = new Array();
 	idxInputsGlob = 0;
 	listaCasas = listaCasas.replace(/\{|\}/g,"");
 	aux = 0;
 	for(i = listaCasas.indexOf("="); i > -1; i = listaCasas.indexOf("=",i+1)){
 		casa = listaCasas.substring(aux,i);
 		casa = casa.replace(/(^\s)|(\s$)/g,"");
 		aux2 = listaCasas.indexOf("=",i+1);
 		if(aux2 == -1){
 			aux2 = listaCasas.length;
 		}
 		else{
 			aux2 = listaCasas.lastIndexOf(",",aux2);
 		}
 		filhos =  listaCasas.substring(i+1,aux2);
 		filhos = filhos.replace(/\[|(\](\,)?)/g,"");
 		filhos = filhos.replace(/(^\s)|(\s$)/g,"");
 		if(filhos != 'null'){
 			inputsGlobs[idxInputsGlob] = casa;
 			idxInputsGlob++;
 			filhosArray = filhos.split(",");
 			for(idx = 0; idx < filhosArray.length; idx++){
 				allCasas[idxAllCasas] = filhosArray[idx].replace(/(^\s)|(\s$)/g,"");
 				idxAllCasas++;
 			}
 		}
 		aux = aux2+1;
 	}
 }


 function chekAll(checkedCasas,command){
	var casa = allCasas;
 	if(checkedCasas == null || checkedCasas == ''){
 		//Marca tudo!
 		if(document.getElementById('divTRF') != null){
	 		document.getElementById('inputTRF').checked = 'checked';
 			document.getElementById('divTribunaisSelectedTRF').innerHTML = 'Todos selecionados';
 		}
 		if(document.getElementById('divTRT') != null){
	 		document.getElementById('inputTRT').checked = 'checked';
 			document.getElementById('divTribunaisSelectedTRT').innerHTML = 'Todos selecionados';
 		}
 		if(document.getElementById('divTRE') != null){
	 		document.getElementById('inputTRE').checked = 'checked';
 			document.getElementById('divTribunaisSelectedTRE').innerHTML = 'Todos selecionados';
 		}
 		if(document.getElementById('divTJ') != null){
	 		document.getElementById('inputTJ').checked = 'checked';
 			document.getElementById('divTribunaisSelectedTJ').innerHTML = 'Todos selecionados';
 		}
 		document.getElementById('check').checked = 'checked';
 		document.getElementById('uncheck').checked = '';
 		if(document.getElementById('input2TRF') != null){
	 		if(document.getElementById('divTRF') != null){
		 		document.getElementById('input2TRF').checked = 'checked';
	 			document.getElementById('divTribunaisSelected2TRF').innerHTML = 'Todos selecionados';
	 		}
	 		if(document.getElementById('divTRT') != null){
		 		document.getElementById('input2TRT').checked = 'checked';
	 			document.getElementById('divTribunaisSelected2TRT').innerHTML = 'Todos selecionados';
	 		}
	 		if(document.getElementById('divTRE') != null){
		 		document.getElementById('input2TRE').checked = 'checked';
	 			document.getElementById('divTribunaisSelected2TRE').innerHTML = 'Todos selecionados';
	 		}
	 		if(document.getElementById('divTJ') != null){
		 		document.getElementById('input2TJ').checked = 'checked';
	 			document.getElementById('divTribunaisSelected2TJ').innerHTML = 'Todos selecionados';
	 		}
	 		document.getElementById('check2').checked = 'checked';
	 		document.getElementById('uncheck2').checked = '';
 		}
 	
	  	for(z=0; z<casa.length; z++)
		{
			inputCasa = document.getElementById('input'+casa[z]);
			if(	inputCasa != null ){
				if(command == 'check'){
					inputCasa.checked = true;
				}
				else{
					inputCasa.checked = false;
				}
			}
		}
 	}
 	else{
		var casa = allCasas;
		var tribunais = checkedCasas.split(",");
		//Marca só as requisitadas...
		for(y=0; y<casa.length; y++){
		  	z=0;
		  	tribunalAtual = casa[y];
		  	for(; z < tribunais.length; z++){
				if(tribunais[z] == tribunalAtual){
					break;
				}
			}
			tribunal = document.getElementById("input"+tribunalAtual);
			if(tribunal != null){
				if(command == 'check' && z < tribunais.length){
					tribunal.checked = true;
					
				}else{
					tribunal.checked = false;
					realName = tribunalAtual;
					while(document.getElementById('para'+realName) == null){
						if(realName.length < 2){
							break;
						}
						realName = realName.substring(0,realName.length - 1);
					}
					if(document.getElementById('para'+realName) != null){
						if(	document.getElementById('selecione'+realName) != null &&
							document.getElementById('selecione'+realName).innerHTML.indexOf("<iput") > -1)
						{
							document.getElementById('para'+realName).style.display = "none";
							if(document.getElementById('selecione'+realName) != null){
								document.getElementById('selecione'+realName).style.display = "none";
							}
							if(document.getElementById('para2'+realName) != null){
								if(	document.getElementById('selecione2'+realName) != null &&
									document.getElementById('selecione2'+realName).innerHTML.indexOf("<iput") > -1)
								{
									document.getElementById('para2'+realName).style.display = "none";
									if(document.getElementById('selecione2'+realName) != null){
										document.getElementById('selecione2'+realName).style.display = "none";
									}
								}
							}
						}
					}
				}
			}
		}
 	}
	checkMarkedInput();
 	
 	if(command == 'uncheck'){
 		if(document.getElementById('divTRF') != null){
	 		document.getElementById('inputTRF').checked = '';
 			document.getElementById('divTribunaisSelectedTRF').innerHTML = 'Nenhum selecionado';
 		}
 		if(document.getElementById('divTRT') != null){
	 		document.getElementById('inputTRT').checked = '';
 			document.getElementById('divTribunaisSelectedTRT').innerHTML = 'Nenhum selecionado';
 		}
 		if(document.getElementById('divTRE') != null){
	 		document.getElementById('inputTRE').checked = '';
 			document.getElementById('divTribunaisSelectedTRE').innerHTML = 'Nenhum selecionado';
 		}
 		if(document.getElementById('divTJ') != null){
	 		document.getElementById('inputTJ').checked = '';
 			document.getElementById('divTribunaisSelectedTJ').innerHTML = 'Nenhum selecionado';
 		}
 		document.getElementById('check').checked = '';
 		document.getElementById('uncheck').checked = 'checked';
 		if(document.getElementById('input2TRF') != null){
	 		if(document.getElementById('divTRF') != null){
		 		document.getElementById('input2TRF').checked = '';
	 			document.getElementById('divTribunaisSelected2TRF').innerHTML = 'Nenhum selecionado';
	 		}
	 		if(document.getElementById('divTRT') != null){
		 		document.getElementById('input2TRT').checked = '';
	 			document.getElementById('divTribunaisSelected2TRT').innerHTML = 'Nenhum selecionado';
	 		}
	 		if(document.getElementById('divTRE') != null){
		 		document.getElementById('input2TRE').checked = '';
	 			document.getElementById('divTribunaisSelected2TRE').innerHTML = 'Nenhum selecionado';
	 		}
	 		if(document.getElementById('divTJ') != null){
		 		document.getElementById('input2TJ').checked = '';
	 			document.getElementById('divTribunaisSelected2TJ').innerHTML = 'Nenhum selecionado';
	 		}
	 		document.getElementById('check2').checked = '';
	 		document.getElementById('uncheck2').checked = 'checked';
 		}
 	}
 }
 
 function addTribunalSelected(tribunalTipo,selected){
 	
 	divSelection = document.getElementById('divTribunaisSelected'+tribunalTipo);
 	textoSelect = divSelection.innerHTML;
 	textHasModify = false;
 	
 	//Desmarcando elementos
 	document.getElementById('uncheck').checked = '';
 	document.getElementById('check').checked = '';
 	
 	//Obtendo Casas Relacionadas ao 'tribunalTipo' e um boolean para os checks
 	allCasasChecked = true;
 	listaCasas = new Array();
	j = 0;
	for(i = 0; i < allCasas.length; i++){
		if(allCasas[i].indexOf(tribunalTipo) == 0){
			inputCasa = document.getElementById('input'+allCasas[i]);
			if(inputCasa != null){
				listaCasas[j] = inputCasa;
				j++;
				if(!(inputCasa.checked || inputCasa.checked == 'checked')){
					allCasasChecked = false;
				}
			}
		}
		else{
			if(listaCasas.length > 0){
				break;
			}
		}
	}
	if(!allCasasChecked){
		inputSelected = document.getElementById('input'+selected);
		if(selected != null && inputSelected.checked){
			if(textoSelect.indexOf("Nenhum") == 0 || textoSelect.indexOf("Todos") == 0){
				textoSelect = "";
			}
			textoSelect = selected.replace(/(^\s+)|(\s+$)/g,"") + "," + textoSelect;
			textHasModify = true;
		}
		else{
			if(document.getElementById('input'+tribunalTipo) != null){
				document.getElementById('input'+tribunalTipo).checked = false;
			}
			textoSelect = "";
			for(i = 0; i < listaCasas.length; i++){
				inputCasa = listaCasas[i];
				if(inputCasa != null && (inputCasa.checked || inputCasa.checked == 'checked')){
					textoSelect = inputCasa.value + "," + textoSelect;
					if(!textHasModify){
						textHasModify = true;
					}
					if(textoSelect.length > 25){
						break;
					}
				}
			}
		}
	}
	else{
		textoSelect = "Todos selecionados";
		if(document.getElementById('input'+tribunalTipo) != null){
			document.getElementById('input'+tribunalTipo).checked = true;
		}
		textHasModify = true;
		i = 0;
		for(; i < allCasas.length; i++){
			inputCasa = document.getElementById('input'+allCasas[i]);
			if(inputCasa != null && !inputCasa.checked){
				document.getElementById('uncheck').checked = true;
 				document.getElementById('check').checked = false;
				break;
			}
			
		}
		if(i == allCasas.length){
			document.getElementById('uncheck').checked = false;
			document.getElementById('check').checked = true;
		}
	}
	textoSelect = textoSelect.replace(/(^\,\s*)|(\s*\,$)/g,"");
	if(textoSelect.length > 25){
		textoSelect = textoSelect.substring(0,22) + "...";
	}
	if(!textHasModify){
		textoSelect = "Nenhum selecionado";
	}
 	document.getElementById('divTribunaisSelected'+tribunalTipo).innerHTML = textoSelect;
 }
 
 function showTribunalDivs(tribunalTipo,footer){
 	for(i = 0; i < inputsGlobs.length; i++){
 		if(document.getElementById('div'+inputsGlobs[i]) != null){
 			goshme.util.toogle('div'+inputsGlobs[i],false);
 		}
 	}
	divTribunal = document.getElementById('div'+tribunalTipo);
	if(divTribunal != null && divTribunal.innerHTML.toLowerCase().indexOf("<input") != -1){
	 	show = document.getElementById('div'+tribunalTipo).style.display;
	 	if(footer == null){
			offsetTrail = document.getElementById('selecione'+tribunalTipo);
		}
		else{
			offsetTrail = document.getElementById('selecione2'+tribunalTipo);
		}
 		var x = 0;
	  	var y = 0;
		while(offsetTrail){
			x = x + offsetTrail.offsetLeft;
			y = y + offsetTrail.offsetTop;
			offsetTrail = offsetTrail.offsetParent;
		}
		var browserVersion = navigator.appVersion;
		var browserName = navigator.appName;
		if( browserName == "Microsoft Internet Explorer" && browserVersion.indexOf("MSIE 7") != -1 ){
			y = y - 110;
			url = window.location + "";
			if(url.indexOf("busca") != -1){
				x = x - 150;
				y = y + 100;
			}
		}
	 	if( (show != 'none' && show != '') && divTribunal.style.top == y + 'px' ){
	 		goshme.util.toogle('div'+tribunalTipo,false);
	 	}else{
		  	divTribunal.style.top = y + 'px';
		  	divTribunal.style.left = x + 'px';
	 		goshme.util.toogle('div'+tribunalTipo,true);
	 	}
 	}
 }
 
 function markInput(id,footer,link){

	name = 'input';
 	name2 = 'input2';
 	if(footer != null){
 		name = name2;
 		name2 = 'input';
 	}
 	if(document.getElementById('div'+id) != null || id == 'STF' || id == 'STJ' || id == 'TST' || id == 'TSE' || id == 'STM'){
	 	inputEle = document.getElementById(name+id);
	 	if(link == null){
	 		if(document.getElementById(name2+id) != null){
			 	document.getElementById(name2+id).checked = inputEle.checked;
		 	}
	 	}
	 	else{
		 	if(inputEle != null){
			 	if(inputEle.checked == 'checked' || inputEle.checked){
			 		inputEle.checked = false;
			 	}
			 	else{
			 		inputEle.checked = true;
			 	}
		 	}
		 	if(document.getElementById(name2+id) != null){
			 	document.getElementById(name2+id).checked = inputEle.checked;
		 		
		 	}
	 	}
	 	markAll(id,footer);
	 }
	 else{
	 	if(document.getElementById(name+id) != null){
	 		document.getElementById(name+id).checked = "";
	 	}
	 }
 }
 
 function markAll(tribunalTipo,footer){
 	
 	casa = new Array();
	identifier = "";
	for(i = 0; i < inputsGlobs.length; i++){
		if(tribunalTipo.indexOf(inputsGlobs[i]) == 0){
			identifier = inputsGlobs[i];
			break;
		}
	}
	j = 0;
	for(i = 0; i < allCasas.length; i++){
		if(allCasas[i].indexOf(identifier) == 0){
			casa[j] = document.getElementById('input' + allCasas[i]);
			j++; 
		}
		else{
			if(casa.length > 0){
				break; 
			}
		}
	}
	marcado = document.getElementById("input"+tribunalTipo);
	if(footer != null){
		marcado = document.getElementById("input2"+tribunalTipo);
	}
	if(marcado != null){
		for(z=0; z<casa.length; z++)
		{	
			casa[z].checked = marcado.checked;
		}
	}
	checkMarkedInput(tribunalTipo);
 }
 
 function checkMarkedInput(idTribunal){
 	mark = true;
 	msg = "Todos selecionados";
 	if(idTribunal == null){
	 	k = 0;
	 	l = 0;
	 	allMarks = true;
	 	allNonMarks = true;
	 	for(; k < allCasas.length; k++){
	 		if(allCasas[k].indexOf(inputsGlobs[l]) != 0 ){
	 			if(document.getElementById('input'+inputsGlobs[l]) != null){
		 			document.getElementById('input'+inputsGlobs[l]).checked = mark;
		 		}
	 			if(document.getElementById('divTribunaisSelected'+inputsGlobs[l]) != null){
					if(!mark){
						addTribunalSelected(inputsGlobs[l],null);
					}
					else{
						document.getElementById('divTribunaisSelected'+inputsGlobs[l]).innerHTML = msg;
							if(document.getElementById('divTribunaisSelected2'+inputsGlobs[l]) != null){
								document.getElementById('divTribunaisSelected2'+inputsGlobs[l]).innerHTML = msg;
							}
					}
				}
				mark = true;
	 			msg = "Todos selecionados";
				l++;
	 		}
	 		inputCasa = document.getElementById('input'+allCasas[k]);
	 		if(mark && !(inputCasa.checked || inputCasa.checked == "checked")){
	 			 mark = false;
	 			 msg = "Nenhum selecionado";
				 allMarks = false;
	 		}
	 		if(allNonMarks && (inputCasa.checked || inputCasa.checked == "checked")){
				allNonMarks = false;
			}
	 	}
		if(document.getElementById('input'+inputsGlobs[l]) != null){
			document.getElementById('input'+inputsGlobs[l]).checked = mark;
		}
		if(document.getElementById('divTribunaisSelected'+inputsGlobs[l]) != null){
			if(!mark){
				addTribunalSelected(inputsGlobs[l],null);
			}
			else{
				document.getElementById('divTribunaisSelected'+inputsGlobs[l]).innerHTML = msg;
					if(document.getElementById('divTribunaisSelected2'+inputsGlobs[l]) != null){
						document.getElementById('divTribunaisSelected2'+inputsGlobs[l]).innerHTML = msg;
					}
			}
		}
		document.getElementById("check").checked = allMarks;
		document.getElementById("uncheck").checked = allNonMarks;
	 	if(document.getElementById("check2") != null){
	 		document.getElementById("check2").checked = allMarks;
	 		document.getElementById("uncheck2").checked = allNonMarks;
	 	}
 	}
 	else{
 		j = 0;
		for(i = 0; i < allCasas.length; i++){
			if(allCasas[i].indexOf(idTribunal) == 0){
				inputCasa = document.getElementById('input'+allCasas[i]);
				if(inputCasa != null && !(inputCasa.checked || inputCasa.checked == 'checked')){
						mark = false;
						break;
				}
			}
		}
		if(document.getElementById('input'+idTribunal) != null){
			document.getElementById('input'+idTribunal).checked = mark;
		}
		if(document.getElementById('divTribunaisSelected'+idTribunal) != null){
			if(!mark){
				addTribunalSelected(idTribunal,null);
			}
			else{
				document.getElementById('divTribunaisSelected'+idTribunal).innerHTML = msg;
				if(document.getElementById('divTribunaisSelected2'+idTribunal) != null){
					document.getElementById('divTribunaisSelected2'+idTribunal).innerHTML = msg;
				}
			}
		}
		mark = true;
		unMark = true;
		for(x = 0; x < allCasas.length; x++){
			inputCasa = document.getElementById('input'+allCasas[x]);
			if(!inputCasa.checked){
				mark = false;
			}
			if(inputCasa.checked){
				unMark = false;
			}
		}
		document.getElementById("check").checked = mark;
		document.getElementById("uncheck").checked = unMark;
	 	if(document.getElementById("check2") != null){
	 		document.getElementById("check2").checked = mark;
	 		document.getElementById("uncheck2").checked = unMark;
	 	}
 	}
 }
 