function initRAC(){
	var inputs = document.getElementsByTagName("INPUT")
	for (i=0;i<inputs.length ;i++ ){

		// CONFIGURA CAMPOS DE EMAIL
		if(inputs[i].getAttribute("email") != null){
			inputs[i].onchange = function(){
				if(this.value.indexOf("@") == -1){
					alert("Por favor, preencher os campos assinalados corretamente!")
					this.className = "input_obrigatorio"
				}else{
					if(this.value.indexOf(".com") == -1 && this.value.indexOf(".gov") == -1 && this.value.indexOf(".net") == -1 && this.value.indexOf(".br") == -1){
//					this.value.length < 5 || this.value.indexOf('.') == -1 || this.value.indexOf(' ') != -1	
						alert("Por favor, preencher os campos assinalados corretamente!")
						this.className = "input_obrigatorio"
					}else{
						this.className = ""					
					}
				}
			}
		}

		// CONFIGURA CAMPOS DE CPF
		if(inputs[i].getAttribute("cpf") != null){
			inputs[i].onchange = function(){
				if(!isCpf(this.value)){
					alert("Por favor, preencher os campos assinalados corretamente!")
					this.className = "input_obrigatorio"
				}else{
					this.value = formatCpfCnpj(this.value, true, false)
					this.className = ""
				}			
			}
			inputs[i].onkeyup = function(evt){
				this.value = formatCpfCnpj(this.value, true, false)
			}
		}

		// CONFIGURA CAMPOS DE CNPJ
		if(inputs[i].getAttribute("cnpj") != null){
			inputs[i].onchange = function(){
				if(!isCnpj(this.value)){
					alert("Por favor, preencher os campos assinalados corretamente!")
					this.className = "input_obrigatorio"
				}else{
					this.value = formatCpfCnpj(this.value, true, true)
					this.className = ""
				}
			}
			inputs[i].onkeyup = function(evt){
				this.value = formatCpfCnpj(this.value, true, true)
			}
		}

		// CONFIGURA CAMPOS NUMÉRICOS
		if(inputs[i].getAttribute("numeric") != null){
			inputs[i].onkeypress = function(evt){
				var charCode = (document.all) ?  event.keyCode : evt.which
				if (charCode != 45 && (charCode > 31 && (charCode < 48 || charCode > 57)))
				return false;

				return true;
			}
		}
	
		// CONFIGURA CAMPOS OBRIGATÓRIOS
		if(inputs[i].getAttribute("mandatory") != null){
			// - Tirar estes comentários se deseja que já apareçam alertas assim que entrar na página
			// inputs[i].value = 'Obrigatório'
			// inputs[i].className = "input_obrigatorio"
			inputs[i].onblur = function(){
				if (this.value == 'Obrigatório' || this.value == ''){
					this.value = 'Obrigatório'
					this.className = "input_obrigatorio"
				}
			};
			inputs[i].onfocus = function(){
				if (this.value == 'Obrigatório'){
					this.value = ""
					this.className = ""
				}
			};
		};
	}
	var textareas = document.getElementsByTagName("TEXTAREA")
	for (i=0;i<textareas.length ;i++ ){
		if(textareas[i].getAttribute("mandatory") != null){
			// - Tirar estes comentários se deseja que já apareçam alertas assim que entrar na página
			// textareas[i].value = 'Obrigatório'
			// textareas[i].className = "input_obrigatorio"
			textareas[i].onblur = function(){
				if (this.value == 'Obrigatório' || this.value == ''){
					this.value = 'Obrigatório'
					this.className = "input_obrigatorio"
				}
			};
			textareas[i].onfocus = function(){
				if (this.value == 'Obrigatório'){
					this.value = ""
					this.className = ""
				}
			};
		};
	}
	var selects = document.getElementsByTagName("SELECT")
	for (i=0;i<selects.length ;i++ ){
		if(selects[i].getAttribute("mandatory") != null){
			// - Tirar estes comentários se deseja que já apareçam alertas assim que entrar na página
			// selects[i].className = "input_obrigatorio"
			selects[i].onblur = function(){
				if (this.value == 'Selecione...' || this.value == ''){
					this.className = "input_obrigatorio"
				}
			};
			selects[i].onfocus = function(){
				if (this.value == 'Selecione...' || this.value == ''){
					this.className = ""
				}
			};
		};
	}
	var _forms = document.getElementsByTagName("FORM")
	for (i=0;i<_forms.length ;i++ ){
		_forms[i].onsubmit = function(){
			erros = 0
			var selects = document.getElementsByTagName("SELECT")
			for (x=0;x<selects.length ;x++ ){
				if(selects[x].form){
					if(selects[x].form.name == this.name){
						if(selects[x].getAttribute("mandatory") != null){
							if (selects[x].value == 'Selecione...' || selects[x].value == ''){
								selects[x].className = "input_obrigatorio"
								erros = erros + 1
							};
						};
					};
				};
			}

			var textareas = document.getElementsByTagName("TEXTAREA")
			for (x=0;x<textareas.length ;x++ ){
				if(textareas[x].form){
					if(textareas[x].form.name == this.name){
						if(textareas[x].getAttribute("mandatory") != null){
							if (textareas[x].value == 'Obrigatório' || textareas[x].value == ''){
								textareas[x].value = 'Obrigatório'
								textareas[x].className = "input_obrigatorio"
								erros = erros + 1
							};
						};
					};
				};
			}

			var inputs = document.getElementsByTagName("INPUT")
			for (x=0;x<inputs.length ;x++ ){
				if(inputs[x].form){
					if(inputs[x].form.name == this.name){

						if(inputs[x].getAttribute("cpf") != null){
							if(inputs[x].value != ''){
								if(!isCpf(inputs[x].value)){
									inputs[x].className = "input_obrigatorio"
									erros = erros + 1
								}
							}
						}
						if(inputs[x].getAttribute("cnpj") != null){
							if(inputs[x].value != ''){
								if(!isCnpj(inputs[x].value)){
									inputs[x].className = "input_obrigatorio"
									erros = erros + 1
								}
							}
						}

						if(inputs[x].getAttribute("email") != null){
							if(inputs[x].value != ''){
								if(inputs[x].value.indexOf("@") == -1){
									inputs[x].className = "input_obrigatorio"
									erros = erros + 1
								}else{
									if(inputs[x].value.indexOf(".com") == -1 && inputs[x].value.indexOf(".gov") == -1 && inputs[x].value.indexOf(".net") == -1 && inputs[i].value.indexOf(".br") == -1){
										inputs[x].className = "input_obrigatorio"
										erros = erros + 1
									}
								}
							}
						}

						if(inputs[x].getAttribute("mandatory") != null){
							if(inputs[x].value == 'Obrigatório' || inputs[x].value == ''){ 
								inputs[x].value = 'Obrigatório'
								inputs[x].className = "input_obrigatorio"
								erros = erros + 1
							}
						}
					}
				}
			}
			if(erros > 0){
				alert('Por favor, preencher os campos assinalados corretamente!')
				scroll(0,0)
				return false
			}else{
				return true	
			}
		}
	}
};

NUM_DIGITOS_CPF  = 11;
NUM_DIGITOS_CNPJ = 14;
NUM_DGT_CNPJ_BASE = 8;

function formatCpfCnpj(pCpfCnpj, pUseSepar, pIsCnpj) {
	if (pIsCnpj==null) pIsCnpj = false;
	if (pUseSepar==null) pUseSepar = true;
	var maxDigitos = pIsCnpj? NUM_DIGITOS_CNPJ: NUM_DIGITOS_CPF;
	var numero = unformatNumber(pCpfCnpj);

	numero = numero.lpad(maxDigitos, '0');

	if (!pUseSepar) return numero;

	if (pIsCnpj) {
		reCnpj = /(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})$/;
		numero = numero.replace(reCnpj, "$1.$2.$3/$4-$5");
	} else {
		reCpf  = /(\d{3})(\d{3})(\d{3})(\d{2})$/;
		numero = numero.replace(reCpf, "$1.$2.$3-$4");
	}
	return numero;
} 

function dvCpfCnpj(pEfetivo, pIsCnpj) {
	if (pIsCnpj==null) pIsCnpj = false;
	var i, j, k, soma, dv;
	var cicloPeso = pIsCnpj? NUM_DGT_CNPJ_BASE: NUM_DIGITOS_CPF;
	var maxDigitos = pIsCnpj? NUM_DIGITOS_CNPJ: NUM_DIGITOS_CPF;
	var calculado = formatCpfCnpj(pEfetivo + "00", false, pIsCnpj);
	calculado = calculado.substring(0, maxDigitos-2);
	var result = "";
	for (j = 1; j <= 2; j++) {
		k = 2;
		soma = 0;
		for (i = calculado.length-1; i >= 0; i--) {
			soma += (calculado.charAt(i) - '0') * k;
			k = (k-1) % cicloPeso + 2;
		}
		dv = 11 - soma % 11;
		if (dv > 9) dv = 0;
		calculado += dv;
		result += dv
	}
	return result;
}

function isCpf(pCpf) {
	var numero = formatCpfCnpj(pCpf, false, false);
	if (numero.length > NUM_DIGITOS_CPF) return false;

	var base = numero.substring(0, numero.length - 2);
	var digitos = dvCpfCnpj(base, false);
	var algUnico, i;
	if (numero != "" + base + digitos) return false;
	algUnico = true;
	for (i=1; algUnico && i<NUM_DIGITOS_CPF; i++) {
		algUnico = (numero.charAt(i-1) == numero.charAt(i));
	}
	return (!algUnico);
} 

function isCnpj(pCnpj) {
	var numero = formatCpfCnpj(pCnpj, false, true);
	if (numero.length > NUM_DIGITOS_CNPJ) return false;
	var base = numero.substring(0, NUM_DGT_CNPJ_BASE);
	var ordem = numero.substring(NUM_DGT_CNPJ_BASE, 12);
	var digitos = dvCpfCnpj(base + ordem, true);
	var algUnico;
	if (numero != "" + base + ordem + digitos) return false;
	algUnico = numero.charAt(0) != '0';
	for (i=1; algUnico && i<NUM_DGT_CNPJ_BASE; i++) {
		algUnico = (numero.charAt(i-1) == numero.charAt(i));
	}
	if (algUnico) return false;
	if (ordem == "0000") return false;
	return (base == "00000000"
		|| parseInt(ordem, 10) <= 300 || base.substring(0, 3) != "000");
}
function unformatNumber(pNum) {
	return String(pNum).replace(/\D/g, "").replace(/^0+/, "");
}
String.prototype.trim = function() {
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
String.prototype.lpad = function(pSize, pCharPad) {
	var str = this;
	var dif = pSize - str.length;
	var ch = String(pCharPad).charAt(0);
	for (; dif>0; dif--) str = ch + str;
	return (str);
}
window.onload = initRAC;