function $(Id) {
    try {
        if (document.layers) { // netscape
            return document.layers[Id];
        }
        if (document.getElementById) { // DOM; IE5, NS6, Mozilla, Opera
            return document.getElementById(Id);
        }
        if (document.all) { //Proprietary DOM; IE4
            return document.all[Id];
        }
        if (document[Id]) { //Netscape alternative
            return document[Id];
        }
    } catch(e) {
        return document.getElementById(Id);
    }
}

//---------------------------------------------------------------------------------
//Função que limpa a cor do campo identificado como obrigatorio.
//Sintaxe: onfocus="mudaCor('NOME DO CAMPO');"
//---------------------------------------------------------------------------------
function mudaCor(c){
	document.getElementById(c).style.background = "#FFFFFF";
}


//---------------------------------------------------------------------------------
//Função que limita a quantidade de caracteres digitados pelo usuário!
//Sintaxe: onKeyDown="txtConta(Mensagem,contador,125);" 
//Sintaxe: onKeyUp="txtConta(Mensagem,contador,125);"
//---------------------------------------------------------------------------------
function txtConta(txtMensagem, txtConta, limite) {
	with(document.all){
		if (txtMensagem.value.length > limite){	
			txtMensagem.value = txtMensagem.value.substring(0, limite);
		}else {
			txtConta.value = limite - txtMensagem.value.length;
		}
	}
}


//---------------------------------------------------------------------------------
//Função que valida o email
//Sintaxe: Onkeydown="ValidaEmail(this.value)"
//---------------------------------------------------------------------------------
function ValidaEmail(campo){
	var i=campo.indexOf("@")
	var l=campo.length
	var j=campo.lastIndexOf(".")
	if((l<8)||(i<2)||((l-i)<5)||(j==-1)||((j-i)<3)||((l-j)<3)||((l-j)>5)){
		alert("E-Mail Inválido!")
		return(false);
	}
}

//---------------------------------------------------------------------------------
//Função que formata os campos de acordo com a mascara informada
//Sintaxe: OnKeyPress="Mascara(this, '## ####-####');
//---------------------------------------------------------------------------------
function Mascara(src, mask){
	var i = src.value.length;
	var saida = mask.substring(0,1);
	var texto = mask.substring(i)
	if (texto.substring(0,1) != saida){
		src.value += texto.substring(0,1);
	}
}

//---------------------------------------------------------------------------------
//Função que só permite números
//Sintaxe: Onkeypress="SoNumero()"
//---------------------------------------------------------------------------------
function SoNumero(){
	if ( (window.event.keyCode >= 48) && (window.event.keyCode <= 57) ){
		return(true)
	}else{
		window.event.keyCode = 0;
		return(false);
	}
}

//---------------------------------------------------------------------------------
//Função que só permite números e Virgula
//Sintaxe: Onkeypress="NumeroeVirgula()"
//---------------------------------------------------------------------------------
function NumeroeVirgula(){
	
	if (window.event.keyCode != 44){
		if ((window.event.keyCode >= 48) && (window.event.keyCode <= 57)){
			return(true)
		}else{
			window.event.keyCode = 0;
			return(false);
		}
	}else{
		return(true)
	}
}

//---------------------------------------------------------------------------------
//Função Verifica Campo Nulo(Vazio)
//Sintaxe: onkeypress = VerificaCampo(campo,'Mensagem');
//---------------------------------------------------------------------------------
function VerificaCampo(campo){
	if(campo.value.lengh < 3 || campo.value == ""){
		alert("Verifique o preenchimento dos campos obrigatórios!\nTodos os campos precisam conter mais de 3 caracteres!")
		campo.focus();
		campo.style.background = "#BDCBDB";
		campo.style.color = "#ffffff";
		return(false);
	}
}


//---------------------------------------------------------------------------------
//Função que transforma o resultado XML com XSL
//Parametros: arqXML(Arquivo que retorna o xml), arqXSL(arquivo de formataçao XSL), obj(Objeto que irá receber o conteudo formatado)
//Sintaxe: TransformarXML("arquivoXML.asp","arquivoXSL.xsl","obj")
//---------------------------------------------------------------------------------
function TransformarXML(arqXML,arqXSL,obj) {
	var ConteudoTransformado;
	var objDIV = document.getElementById(obj);
	var objXML;
	var objXSL;
	
	//Para funcionar no Internet Explorer
	if (window.ActiveXObject) {
		//Carregando o Arquivo XML
		var objXML = new ActiveXObject("Microsoft.XMLDOM");
		objXML.async = false;
		objXML.load(arqXML);
		//Carregando o Arquivo XSL
		var objXSL = new ActiveXObject("Microsoft.XMLDOM");
		objXSL.async = false;
		objXSL.load(arqXSL);
		//Transformação
		ConteudoTransformado = objXML.transformNode(objXSL);
		objDIV.innerHTML = ConteudoTransformado;
		
	//Para funcionar no Mozilla/Firefox
	}else if(window.XMLHttpRequest) {
		var objXSLTProcessor;
		var objXMLDoc;
		var objXSLT;
		//Criando o processador de XLST
		objXSLTProcessor = new XSLTProcessor();
		
		//Carregando o arquivo XSL
		objXSL = new XMLHttpRequest();
		objXSL.open("GET", arqXSL, false);
		objXSL.send(null);
		
		objXSLT = objXSL.responseXML;
		objXSLTProcessor.importStylesheet(objXSLT);
		
		//Carregando o arquivo XML
		objXML = new XMLHttpRequest();
		objXML.open("GET", arqXML, false);
		objXML.send(null);
		
		objXMLDoc = objXML.responseXML;
		
		ConteudoTransformado = objXSLTProcessor.transformToFragment(objXMLDoc, document);
		objDIV.innerHTML = "";
		objDIV.appendChild(ConteudoTransformado);
	}
}
//---------------------------------------------------------------------------------
//Função que Remove Mascara
//Sintaxe: SoNumero(valor)
//---------------------------------------------------------------------------------
function removemascara(valor){
	valor = valor.replace(".","");
	valor = valor.replace(".","");
	valor = valor.replace("-","");
	valor = valor.replace("/","");

	return valor;
}


//---------------------------------------------------------------------------------
//Função que troca "," por "."
//Sintaxe: FormatarCampo(valor)
//---------------------------------------------------------------------------------
function FormatarCampo(campo) {
	vr = campo.value;
	vr = vr.replace( ",", "." );
	vr = vr.replace( ",", "." );
	vr = vr.replace( ",", "." );
	vr = vr.replace( ",", "." );
	vr = vr.replace( ",", "." );
	return(true);
}

//---------------------------------------------------------------------------------
//Funções Genéricas para o objeto XML
//---------------------------------------------------------------------------------
var JJDK = new Object();
JJDK.criarDOM = function(){
	try{
		var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async = false;
	}
	catch(e){ 
		//Correção p/ manipular xml no firefox (04/06/2008 - Helton Lìdio)
		xmlDoc = document.implementation.createDocument("", "", null);
		Node.prototype.__defineGetter__("xml", function (){
			return (new XMLSerializer()).serializeToString(this);
		});
	}
	xmlDoc.async = false;
	this.xmlDoc = xmlDoc;
	return xmlDoc;
};
JJDK.criarHTTP = function(){
	try{
		var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch(e){
		var xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
};

function FormatarCGCCompleto(campo) {
	tecla = window.event.keyCode;
	vr = campo.value;
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	tam = vr.length + 1;

	if ( tecla != 9 && tecla != 8 && tecla != 13 && tecla != 37 && tecla != 39 && tecla != 46 ) {
		if ( tam <= 2 ) 
	 		campo.value = vr ;
	 	if ( (tam > 2) && (tam <= 5) ) 
	 		campo.value = vr.substr( 0, 2 ) + '.' + vr.substr( 2, tam ) ;
	 	if ( (tam > 5) && (tam <= 8) ) 
	 		campo.value = vr.substr( 0, 2 ) + '.' + vr.substr( 2, 3 ) + '.' + vr.substr( 5, tam ) ;
	 	if ( (tam > 8) && (tam <= 12) ) 
	 		campo.value = vr.substr( 0, 2 ) + '.' + vr.substr( 2, 3 ) + '.' + vr.substr( 5, 3 ) + '/' + vr.substr( 8, tam ) ;
	 	if ( (tam > 12) && (tam <= 16) ) 
	 		campo.value = vr.substr( 0, 2 ) + '.' + vr.substr( 2, 3 ) + '.' + vr.substr( 5, 3 ) + '/' + vr.substr( 8, 4 ) + '-' + vr.substr( 12, tam ) ;
	}
}