// JavaScript Document

function limpaLabel(campo,textoDefault,pass){
	if (campo.value == textoDefault){
		if (pass == "true"){
			var nome = campo.name;
			var size = campo.size;
			document.getElementById(nome + "Form").innerHTML = "<input type=\"password\" name=\"" + nome + "\" id=\"" + nome + "\" size=\"" + size + "\" class=\"input\" value=\"\" onFocus=\"limpaLabel(this,'" + textoDefault + "','true')\" onBlur=\"carregaLabel(this,'" + textoDefault + "','true')\" onload=\"this.focus();\">";

			setTimeout("document.getElementById('" + nome + "').focus()",250);
		}else{
			campo.value = "";
		}
	}
}

function carregaLabel(campo,textoDefault,pass){
	if (campo.value == ""){
		if (pass == "true"){
			var nome = campo.name;
			var size = campo.size;
			document.getElementById(nome + "Form").innerHTML = "<input type=\"text\" name=\"" + nome + "\" id=\"" + nome + "\" size=\"" + size + "\" class=\"input\" value=\"" + textoDefault + "\" onFocus=\"limpaLabel(this,'" + textoDefault + "','true')\" onBlur=\"carregaLabel(this,'" + textoDefault + "','true')\">";
		}else{
			campo.value = textoDefault;
		}
	}
}

function obrigatorios(sForm){
	var obrigatorios = eval("sForm._obrigatorios.value");
	var defaults = eval("sForm._defaults.value");
	
	if (obrigatorios != ""){
		obrigatorios = obrigatorios.split(",");
		defaults = defaults.split(",");
		
		for (var i = 0; i < obrigatorios.length; i++){			
			if (eval("sForm." + obrigatorios[i] + ".value") == "" || eval("sForm." + obrigatorios[i] + ".value") == defaults[i]){
				alert("Campo obrigatório não preenchido!");
				eval("sForm." + obrigatorios[i] + ".focus();");
				return false;
				break;
			}
		}
	}
}

function validaEmail(Wparam,pDefault){
	if (Wparam.value != "" && Wparam.value != pDefault){
		if (Wparam.value.indexOf('@', 0) == -1 || Wparam.value.indexOf('.', 0) == -1) { 
			alert("E-mail invalido!");
			Wparam.focus()
			return false;
		}
	}
}

function buscar($busca,$secao,$nomeSecao){
	window.location="?s=" + $secao + "&p=&acao=" + $busca + "&c=" + $nomeSecao;
}


// JavaScript Document
if (navigator.appName.indexOf('Microsoft') != -1){   
    clientNavigator = "IE";   
}else{   
    clientNavigator = "Other";   
}

function validarData(campo){
	/*********************************************************
	* Função para validar uma data informada                 *
	* sintaxe: validarData(input text)                       *
	* ex:  onBlur="validarData(this)"                        *
	**********************************************************/
	
	if (campo.value != ""){
		var expReg = /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[1-2][0-9]\d{2})$/;
		var msgErro = "";
		
		if ((campo.value.match(expReg)) && (campo.value!='')){
			var dia = campo.value.substring(0,2)*1;
			var mes = campo.value.substring(3,5)*1;
			var ano = campo.value.substring(6,10)*1;
			if((mes == 4 || mes == 6 || mes == 9 || mes == 11) && dia > 30){
				msgErro = "Dia incorreto! O mês especificado contém no máximo 30 dias.";

			} else{
				if(ano%4 != 0 && mes == 2 && dia > 28){
					msgErro = "Data incorreta! O mês especificado contém no máximo 28 dias.";
	
				} else{
					if(ano%4==0 && mes==2 && dia>29){
						msgErro = "Data incorreta!! O mês especificado contém no máximo 29 dias.";
					}
				}
			}
		} else {
			msgErro = "Formato inválido de data.";
		}
		
		if (msgErro != ""){
			alert(msgErro);
			try {
				setTimeout("document.forms[0]."+campo.name+".focus();"+
                	"document.forms[0]."+campo.name+".select();",1);
			}catch(e) {}
			campo.focus();
			campo.select();
			return false;
		}
	}
}

function somenteNumeros(evnt,permitidos){ 
	/****************************************************************************************
	* Função para permitir somente digitação de número                                      *
	* sintaxe: somenteNumeros(event, [caracteres não numéricos permitidos])                 *
	* ex:  onKeyPress="return somenteNumeros(event,',.;');"                                 *
	****************************************************************************************/
	
	if (evnt.charCode == undefined){
		var tecla = evnt.keyCode;
	}else{
		var tecla = evnt.charCode;
	}
	
	var teste = false;
	if (permitidos != undefined && permitidos != ""){
		if (permitidos.indexOf(String.fromCharCode(tecla)) != -1) teste = true;
	}
	
    if (clientNavigator == "IE"){ 
        if (tecla < 48 || tecla > 57){   
            return teste;   
        }   
    }else{
        if ((tecla < 48 || tecla > 57) && evnt.keyCode == 0){
            return teste;
        }
    }   
}   
  
function mascaraData(input, evnt){  
	/*********************************************************
	* Função para aplicar máscara de formato data e permitir *
	* somente digitação de números no campo                  *
	* sintaxe: mascaraData(this, event)                      *
	* ex: onKeyPress="return mascaraData(this, event);"      *
	**********************************************************/

	if (somenteNumeros(evnt) == false){
		return false;
	}

    if (input.value.length == 2 || input.value.length == 5){   
        if(clientNavigator == "IE"){   
            input.value += "/";   
        }else{   
            if(evnt.keyCode == 0){   
                input.value += "/";   
            }   
        }   
    } 
}


function mascaraMoeda(campo, evnt, dolar){
	/***********************************************************************
	* Função para aplicar máscara em campos tipo moeda                     *
	* sintaxe: mascaraMoeda(campo, evento, dolar)                          *
	* ex brasil: onKeyPress="return mascaraMoeda(this, event);"    	       *
	* ex dólar: onKeyPress="return mascaraMoeda(this, event, 'true');"     *
	***********************************************************************/
	
	if (somenteNumeros(evnt) == false || evnt.keyCode == 9){
		return false;
	}
	
	campo.value = campo.value.replace(/\D/g,""); //Remove tudo o que não é dígito
	
	if (dolar != undefined){
		campo.value = campo.value.replace(/(\d{1})$/,".$1"); //Coloca a virgula
		campo.value = campo.value.replace(/(\d+)(\d{3},\d{2})$/g,"$1,$2"); //Coloca o primeiro ponto
	}else{
		campo.value = campo.value.replace(/(\d{1})$/,",$1"); //Coloca a virgula
		campo.value = campo.value.replace(/(\d+)(\d{3},\d{2})$/g,"$1.$2"); //Coloca o primeiro ponto
	}
	
	var qtdLoop = (campo.value.length-3)/3;
	var count = 0;
	
	while (qtdLoop > count){
		count++;
		if (dolar != undefined){
			campo.value = campo.value.replace(/(\d+)(\d{3}.*)/,"$1,$2"); //Coloca o resto dos pontos
		}else{
			campo.value = campo.value.replace(/(\d+)(\d{3}.*)/,"$1.$2"); //Coloca o resto dos pontos
		}
	}
	
	campo.value = campo.value.replace(/^(0)(\d)/g,"$2"); //Coloca hífen entre o quarto e o quinto dígitos
}

function maxLenght(campo, tamanho){
	if (campo.value.length >= tamanho){
		return false;
	}
}