//variaveis de ambiente usadas pelas funçoes

//variáveis usadas pelas funções clareiaDIV e escureceDIV
var opacidadeEscura = 100;
var opacidadeClara = 0;


function validaFormulario(formulario, acao, acaoAJAX) {
	
	var nome_formulario = document.getElementById(formulario);
		
	if ( nome_formulario.hasChildNodes() ) {
		var elementos_input    = nome_formulario.getElementsByTagName("input");
		var elementos_select   = nome_formulario.getElementsByTagName("select");
		var elementos_textarea = nome_formulario.getElementsByTagName("textarea"); 

		for ( i = 0; i < elementos_input.length; i++ ) {
			if ( (elementos_input[i].getAttribute("type") == "text") || (elementos_input[i].getAttribute("type") == "password") || (elementos_input[i].getAttribute("type") == "file") ) {
				if ( elementos_input[i].getAttribute("div") != "" ) {
					
					if ( elementos_input[i].getAttribute("extra") == "" ) {
						if ( elementos_input[i].value == "" ) {
							var erro = true;
							mostraDIV(elementos_input[i].getAttribute("div"));
						} else {
							escondeDIV(elementos_input[i].getAttribute("div"));
						}
					}

					if ( (elementos_input[i].getAttribute("extra") == "senha")  ) {
						var j = i + 1;

						if ( elementos_input[i].value == "" || elementos_input[j].value == "" ) {
							var erro = true;
							mostraDIV(elementos_input[i].getAttribute("div"));
						} else {
							
							if ( (elementos_input[i].value != "") || (elementos_input[j].value != "") ) {
								
								escondeDIV(elementos_input[i].getAttribute("div"));
								
								if ( elementos_input[i].getAttribute("extra") == "senha" ) {
									if ( elementos_input[i].value != elementos_input[j].value ) {
										var erro = true;
										mostraDIV(elementos_input[i].getAttribute("div2"));
									} else {
										escondeDIV(elementos_input[i].getAttribute("div2"));
									}
								}
							}
						}
					}
					
					if ( elementos_input[i].getAttribute("extra") == "numero" ) {
						
						if ( elementos_input[i].value == "" || elementos_input[i].value == 0 ) {
							var erro = true;
							mostraDIV(elementos_input[i].getAttribute("div"));
						} else {
							
							if ( (elementos_input[i].value != "") ) {
								escondeDIV(elementos_input[i].getAttribute("div"));
								
								if ( isNaN(elementos_input[i].value) ) {
									var erro = true;
									mostraDIV(elementos_input[i].getAttribute("div2"));
								} else {
									escondeDIV(elementos_input[i].getAttribute("div2"));
								}
							}
						}
					}
					
					if ( elementos_input[i].getAttribute("extra") == "ddd" ) {
						
						if ( elementos_input[i].value == "" || elementos_input[i].value == 0 || elementos_input[i].value.length <= 1 ) {
							var erro = true;
							mostraDIV(elementos_input[i].getAttribute("div"));
						} else {
							
							if ( (elementos_input[i].value != "") ) {
								escondeDIV(elementos_input[i].getAttribute("div"));
								
								if ( isNaN(elementos_input[i].value) ) {
									var erro = true;
									mostraDIV(elementos_input[i].getAttribute("div2"));
								} else {
									escondeDIV(elementos_input[i].getAttribute("div2"));
								}
							}
						}
					}
					
					if ( elementos_input[i].getAttribute("extra") == "telefone" || elementos_input[i].getAttribute("extra") == "cep" ) {
						
						if ( elementos_input[i].value == "" || elementos_input[i].value == 0 || elementos_input[i].value.length <= 7 ) {
							var erro = true;
							mostraDIV(elementos_input[i].getAttribute("div"));
						} else {
							
							if ( (elementos_input[i].value != "") ) {
								escondeDIV(elementos_input[i].getAttribute("div"));
								
								if ( isNaN(elementos_input[i].value) ) {
									var erro = true;
									mostraDIV(elementos_input[i].getAttribute("div2"));
								} else {
									escondeDIV(elementos_input[i].getAttribute("div2"));
								}
							}
						}
					}
					
					
					if ( elementos_input[i].getAttribute("extra") == "email" ) {
						if ( elementos_input[i].value == "" ) {
							var erro = true;
							mostraDIV(elementos_input[i].getAttribute("div"));
						} else {
							if ( elementos_input[i].value != "" ) {
								
								escondeDIV(elementos_input[i].getAttribute("div"));
								
								if ( (elementos_input[i].value.indexOf("@") == -1) || (elementos_input[i].value.indexOf(".") == -1) || (elementos_input[i].value == "@.") ) {
	   								var erro = true;
									mostraDIV(elementos_input[i].getAttribute("div2"));
								} else {
									escondeDIV(elementos_input[i].getAttribute("div2"));
								}
							}
						}
					}
				} else {
					if ( elementos_input[i].getAttribute("alert") != "" ) {
				
						if ( elementos_input[i].getAttribute("extra") == "" ) {
							if ( elementos_input[i].value == "" ) {
								var erro = true;
								alert(elementos_input[i].getAttribute("alert"));
								break;
							}
						}
		
						if ( (elementos_input[i].getAttribute("extra") == "senha")  ) {
							var j = i + 1;
		
							if ( elementos_input[i].value == "" || elementos_input[j].value == "" ) {
								var erro = true;
								alert(elementos_input[i].getAttribute("alert"));
								break;
							} else {
								
								if ( (elementos_input[i].value != "") || (elementos_input[j].value != "") ) {
									
									if ( elementos_input[i].getAttribute("extra") == "senha" ) {
										if ( elementos_input[i].value != elementos_input[j].value ) {
											var erro = true;
											alert(elementos_input[i].getAttribute("alert2"));
											break;
										}
									}
								}
							}
						}
						
						if ( elementos_input[i].getAttribute("extra") == "numero" ) {
							
							if ( elementos_input[i].value == "" || elementos_input[i].value == 0 ) {
								var erro = true;
								alert(elementos_input[i].getAttribute("alert"));
								break;
							} else {
								
								if ( (elementos_input[i].value != "") ) {
									if ( isNaN(elementos_input[i].value) ) {
										var erro = true;
										alert(elementos_input[i].getAttribute("alert2"));
										break;
									} 
								}
							}
						}
						
						if ( elementos_input[i].getAttribute("extra") == "ddd" ) {
							
							if ( elementos_input[i].value == "" || elementos_input[i].value == 0 || elementos_input[i].value.length <= 1 ) {
								var erro = true;
								alert(elementos_input[i].getAttribute("alert"));
								break;
							} else {
								
								if ( (elementos_input[i].value != "") ) {
									if ( isNaN(elementos_input[i].value) ) {
										var erro = true;
										alert(elementos_input[i].getAttribute("alert2"));
										break;
									} 
								}
							}
						}
						
						if ( elementos_input[i].getAttribute("extra") == "telefone"  || elementos_input[i].getAttribute("extra") == "cep" ) {
							
							if ( elementos_input[i].value == "" || elementos_input[i].value == 0 || elementos_input[i].value.length <= 7 ) {
								var erro = true;
								alert(elementos_input[i].getAttribute("alert"));
								break;
							} else {
								
								if ( (elementos_input[i].value != "") ) {
									if ( isNaN(elementos_input[i].value) ) {
										var erro = true;
										alert(elementos_input[i].getAttribute("alert2"));
										break;
									} 
								}
							}
						}
						
												
						if ( elementos_input[i].getAttribute("extra") == "email" ) {
							if ( elementos_input[i].value == "" ) {
								var erro = true;
								alert(elementos_input[i].getAttribute("alert"));
								break;
							} else {
								if ( elementos_input[i].value != "" ) {
									
									if ( (elementos_input[i].value.indexOf("@") == -1) || (elementos_input[i].value.indexOf(".") == -1) || (elementos_input[i].value == "@.") ) {
		   								var erro = true;
										alert(elementos_input[i].getAttribute("alert2"));
										break;
									}
								}
							}
						}
					}
				}
			}
		}
		
		if ( !erro ) {
			for ( i = 0; i < elementos_select.length; i++ ) {
				if ( elementos_select[i].getAttribute("div") != "" ) {
					if ( elementos_select[i].selectedIndex == 0 ) {
						var erro = true;
						var id_div = document.getElementById(elementos_select[i].getAttribute("div"));
						mostraDIV(elementos_select[i].getAttribute("div"));
					} else {
						var id_div = document.getElementById(elementos_select[i].getAttribute("div"));
						escondeDIV(elementos_select[i].getAttribute("div"));
					}
				} else {
					if ( elementos_select[i].getAttribute("alert") != "" ) {
						if ( elementos_select[i].selectedIndex == 0 ) {
							var erro = true;
							alert(elementos_select[i].getAttribute("alert"));
							break;
						} 
					}
				}
			}
			
			for ( i = 0; i < elementos_textarea.length; i++ ) {
				if ( elementos_textarea[i].getAttribute("div") != "" ) {
					 
					var texto = document.getElementById(elementos_textarea[i].getAttribute("id")).value;
					
					if ( texto != null && texto.length > 0 ) {
						var id_div = document.getElementById(elementos_textarea[i].getAttribute("div"));
						escondeDIV(elementos_textarea[i].getAttribute("div"));
					} else {
						var erro = true;
						var id_div = document.getElementById(elementos_textarea[i].getAttribute("div"));
						mostraDIV(elementos_textarea[i].getAttribute("div"));
					}
				} else {
					if ( elementos_textarea[i].getAttribute("alert") != "" ) {
						 
						var texto = document.getElementById(elementos_textarea[i].getAttribute("id")).value;
						
						if ( texto != null && texto.length > 0 ) {
							var erro = false;
						} else {
							var erro = true;
							alert(elementos_textarea[i].getAttribute("alert"));
							return;
						}
					}
				}
			}
		}
		
		
		if ( !erro ) {
			if ( acao == "ajax" ) {
				enviaDadosAJAX(formulario, acaoAJAX);
			} else {
				enviaFormulario(formulario);
			}
		}
	}
}


function ValidaPermissao(permissao) {
	elementos_div = document.getElementsByTagName("div");
	
	if ( permissao == "usuario" ) {
		for ( i = 0; i < elementos_div.length; i++ ) {
			if ( (elementos_div[i].getAttribute("permissao") == permissao) ) {
				div = document.getElementById(elementos_div[i].getAttribute("id"));
				div.style.display = "block";
			}
		}
	}
	
	if ( permissao == "supervisor" ) {
		for ( i = 0; i < elementos_div.length; i++ ) {
			if ( (elementos_div[i].getAttribute("permissao") == "usuario") || (elementos_div[i].getAttribute("permissao") == permissao) ) {
				var div = document.getElementById(elementos_div[i].getAttribute("id"));
				div.style.display = "block";
			}
		}
	}
	
	
	if ( permissao == "administrador" ) {
		for ( i = 0; i < elementos_div.length; i++ ) {
			if ( (elementos_div[i].getAttribute("permissao") == "usuario") || (elementos_div[i].getAttribute("permissao") == "supervisor") || (elementos_div[i].getAttribute("permissao") == permissao) ) {
				var div = document.getElementById(elementos_div[i].getAttribute("id"));
				div.style.display = "block";
			}
		}
	}
}


function pegaDados(formulario) {
	var parametros = new Array();
	
	for ( i = 0; i < formulario.elements.length; i++ ) {
		var parametro = encodeURIComponent(formulario.elements[i].name);
		parametro += "=";
		parametro += encodeURIComponent(formulario.elements[i].value);
			
		//alert(parametro);
			
		parametros.push(parametro);
	}
		
	return parametros.join("&");
}



function mostraDIV(div) {
	//alert(div);
	nome_div = document.getElementById(div);
	nome_div.style.display = "block";
}


function escondeDIV(div, fundo) {
	nome_div = document.getElementById(div);
	nome_div.style.display = "none";
	
	if ( fundo == "sim" ) {
		document.getElementById('div_clara').style.display = "none";
		document.getElementById('div_escura').style.display = "none";
	}
}

function limpaFormulario(formulario) {
	document.getElementById(formulario).reset();
}

function enviaFormulario(formulario) {
	document.getElementById(formulario).submit();
}


function validaSelect(nome_select, valor, acao) {
	var select = document.getElementById(nome_select);
	
	if ( acao == "mostraDIV" ) {
		if ( document.getElementById(nome_select).options[document.getElementById(nome_select).selectedIndex].value == valor ) {
			var div = select.getAttribute("extra");
			mostraDIV(div);
		}
	}
	
}


function enviaSelect(formulario, select, mensagem, confirm) {
	var nome_formulario = document.getElementById(formulario);
	var nome_select = document.getElementById(select);
	var valor_select = nome_select.selectedIndex;
	
	if ( confirm == 1 ) {
		if ( valor_select == 0 ) {
			alert("Selecione uma opção!");
			return false;
		} else if ( window.confirm(mensagem) ) {
			enviaFormulario(formulario);
		} else {
			return false;
		}
	} else {
		if ( valor_select == 0 ) {
			alert(mensagem);
			return false;
		} else if ( formulario != "" ) {
			enviaFormulario(formulario);
		}
	}
}


function selecionaCheckbox(formulario, checkbox) {
	nome_formulario = document.getElementById(formulario);
	nome_checkbox = document.getElementById(checkbox);
	
	if ( nome_checkbox.checked ) {        		
		for ( i=0; i < nome_formulario.elements.length; i++) {
			if( nome_formulario.elements[i].type == "checkbox" ) {
				nome_formulario.elements[i].checked = 1
			}
		}
	} else {
		for ( i=0; i < nome_formulario.elements.length; i++ ) {
			if( nome_formulario.elements[i].type == "checkbox" ) {
				nome_formulario.elements[i].checked = 0
			}
		}
	}
}


function marcaInput(input) {
	input.style.borderColor="#FFA620";
	input.style.color="#006699";
}


function desmarcaInput(input) {
	input.style.borderColor="#006699";
	input.style.color="#006699";
}


function mudaBotao(botao, tipo) {
	if ( tipo == "menu" ) {
		botao.style.background="#006699";
		botao.style.color="#FFFFFF";
	} else {
		botao.style.background="#FFA620";
		botao.style.color="#FFFFFF";
		botao.style.borderColor="#006699";
	}
}

function voltaBotao(botao, tipo) {
	if ( tipo == "menu" ) {
		botao.style.background="#FFFFFF";
		botao.style.color="#006699";
	} else {
		botao.style.background="#006699";
		botao.style.color="#FFFFFF";
		botao.style.borderColor="#006699";
	}
}

function voltaHistorico() {
	history.back();
}

function trocaPagina(pagina) {
	location.replace(pagina);
}

function trocaCursor(tipo) {
	if ( tipo == "link" ) {
		document.cursor = "pointer";
	} else if ( tipo == "texto" ) {
		style.cursor = "text";
	} else {
		style.cursor = "default";
	}
}


function insereHTML(elemento, conteudo) {
	var div_principal = document.getElementById(elemento);
	
	div_principal.innerHTML = conteudo;
}


function centralizaDIV(div) {
	
	if ( typeof( window.innerWidth ) == 'number' ) {
    	//quando não for Internet Explorer
    	margemAltura = ((window.innerHeight / 2) - 200);
  	} else if ( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE6 no 'standards compliant mode'
	    margemAltura = ((document.documentElement.clientHeight / 2) - 200);
	} else if ( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    	//compatível com IE4
    	margemAltura = ((document.body.clientHeight / 2) - 200);
  	}
	
	//alert(div);
	
	document.getElementById(div).style.marginTop = margemAltura+"px";
}


function expandeDIV(div) {
	var div_principal = document.getElementById("div_principal");
	/*
	if ( typeof( window.innerWidth ) == 'number' ) {
    	//quando não for Internet Explorer
    	altura = window.innerHeight - 30;
    	largura = window.innerWidth;    	
  	} else if ( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE6 
	    altura = document.documentElement.clientHeight - 30;
	    largura = document.documentElement.clientWidth;
	} else if ( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    	//compatível com IE4
    	altura = document.body.clientHeight - 30;
    	largura = document.body.clientWidth;
  	}
  	*/
  	
  	var largura = 10;
  	var altura = div_principal.offsetHeight;
  	document.getElementById(div).style.height = altura+"px";
  	document.getElementById(div).style.witdth = largura+"px";
}


function setaValorCampo(tipo, campo, valor) {
	if ( tipo == "input" ) {
		document.getElementById(campo).value = valor;
	}
	
	if ( tipo == "select" ) {
		
		var select = document.getElementById(campo)
		
		for ( i = 1; i <= select.lenght; i++ ) {
			if ( select.options[i].value == valor ) {
				select.options[i].selected == true;
			}
		}
	}
}


function escureceDIV() {
	opacidadeEscura--;
	
	if ( opacidadeEscura >= 0 ) {
    	document.getElementById("div_escura").style.opacity = opacidadeEscura/10;
    	document.getElementById("div_escura").style.filter = 'alpha(opacity='+opacidadeEscura*10+')';
	}
}


function clareiaDIV() {
	opacidadeClara++;
	
	if ( opacidadeClara <= 7 ) {
    	document.getElementById("div_clara").style.opacity = opacidadeClara/10;
    	document.getElementById("div_clara").style.filter = 'alpha(opacity='+opacidadeClara*10+')';
	}
}


function DecHex(dec2) { 
	var hex_chars = "0123456789ABCDEF"; 
	var n1 = hex_chars.charAt(Math.floor(dec2 / 16)); 
	var n2 = hex_chars.charAt(dec2 % 16); 
	
	return n1 + n2; 
}


function pegaDadosOld(f) { 
	var acao = f.action; 
	var metodo = f.method.toLowerCase(); 
	  
	var send = new Array(); 
	var elementos = f.elements; 
  
	for ( var i = 0; i < elementos.length; i++ ) { 
		var e = elementos[i]; 
		
		if ( !e.name ) 
			continue; 
 
		var nVal = ""; 
 
		for ( var x = 0; x < e.value.length; x++ ) { 
			codeA = e.value.charCodeAt(x); 
			codeA = DecHex(codeA); 
			nVal += "%" + codeA; 
		} 
  
		var tipo = e.type.toLowerCase(); 
 
		if ( tipo != "checkbox" && tipo != "radio" ) { 
			send[send.length] = e.name + "=" + nVal; 
		} else { 
			if ( e.checked ) { 
				send[send.length] = e.name + "=" + nVal; 
			} 
		} 
	} 
 
	send = send.join("&");

	alert(send);
	
	return send;
}

/*
function centralizaDIV(div){
	document.getElementById(div).style.top = ((((document.body.clientHeight*50)/100)+window.pageYOffset)-(parseInt(document.getElementById(div).style.height.substr(0,document.getElementById(div).style.width.length-2))) )+'px';
	document.getElementById(div).style.left = (((document.body.clientWidth*50)/100)-(parseInt(document.getElementById(div).style.height.substr(0,document.getElementById(div).style.height.length-2))))+'px';
}
*/


function mostraConteudo(div_display, div_filho) {
	
	var div_pai = document.getElementById(div_display);
	
	var elementos_div = div_pai.getElementsByTagName("div");
	
	for ( i = 0; i < elementos_div.length; i++ ) {
		if ( elementos_div[i].getAttribute("extra") == "display" ) {
			if ( elementos_div[i].getAttribute("id") != div_filho ) {
				escondeDIV(elementos_div[i].getAttribute("id"));
			}
		}
	}
	
	//alert(div_filho);
	mostraDIV(div_filho);
}

function trocaPaginacao(url) {
	var select = document.getElementById("select_paginas");
	var indice = select.selectedIndex;
	var pagina = select.options[indice].value; 
	
	location.replace(url+"paginacao="+pagina);
}


function insereElementoFilho(elemento_pai, tipo, nome, valor, atributo, valor_atributo, troca) {
	
	var elemento = document.createElement(tipo);

	elemento.setAttribute("id", nome);
	elemento.setAttribute("name", nome);
	
	if ( tipo == "input" || tipo == "img" ) {
		elemento.setAttribute(atributo, valor_atributo);
	}
	
	if ( tipo == "input" ) {
		elemento.setAttribute("value", valor);
	}
		
	if ( tipo == "img" ) {
		elemento.setAttribute("src", valor);
	}
	
	if ( tipo == "span" ) {
		var	elementoValor = document.createTextNode(valor)
		elemento.appendChild(elementoValor);
	}
	
	if ( troca == "sim" ) {
		var elemento_anterior = document.getElementById(nome);
		
		if ( elemento_anterior ) {
			document.getElementById(elemento_pai).replaceChild(elemento, elemento_anterior);
		} else {
			document.getElementById(elemento_pai).appendChild(elemento);
		}
	} else {
		document.getElementById(elemento_pai).appendChild(elemento);
	}
	
}

function removeElementoFilho(elemento_pai, elemento_filho) {
	var pai = document.getElementById(elemento_pai);
	var filho = document.getElementById(elemento_filho);
	
	pai.removeChild(filho);
}

function mostraImagem(caminho_imagem, elemento_destino) {
	var div_imagem = document.getElementById(elemento_destino);
	
	var imagem = document.createElement("img");
	imagem.setAttribute("src", caminho_imagem);
	
	div_imagem.appendChild(imagem);
	
}

function limpaElemento(elemento_destino, tipo_elemento) {
		
	var elemento = document.getElementById(elemento_destino);
	var elementos = elemento.getElementsByTagName(tipo_elemento);
	
	for ( i = 0; i < elementos.length; i++ ) {
		elemento.removeChild(elementos[i]);
	}
	
}

function iniciarMenu() {
	if ( document.all && document.getElementById ) {
		navRoot = document.getElementById("menu");
		for ( i=0; i < navRoot.childNodes.length; i++ ) {
			node = navRoot.childNodes[i];
			if ( node.nodeName=="LI" ) {
				node.onmouseover = function() {
					this.className+=" over";
  				}
  				node.onmouseout = function() {
  					this.className = this.className.replace
					(" over", "");
   				}
   			}
  		}
 	}
}