
var Validacoes = {
	"itens":[],
	"nome_campos":[],
	"adicionaValidacao":function(){},
	"mascara_CPF":function(e,campoID){if( e.which && e.which > 13 ) {setTimeout("Validacoes._delay_mascara_CPF('" + campoID + "')" , 1);}},
	"mascara_CNPJ":function(e,campoID){if( e.which && e.which > 13 ) {setTimeout("Validacoes._delay_mascara_CNPJ('" + campoID + "')" , 1);}},
	"mascara_Data": function(e,campoID){if( e.which && e.which > 13 ) {setTimeout("Validacoes._delay_mascara_Data('" + campoID + "')" , 1);}},
	"mascara_Numero":function(e,campoID) {setTimeout("Validacoes._delay_mascara_Numero('" + campoID + "')" , 1);},
	"mascara_Telefone":function(e,campoID) {if( e.which && e.which > 13 ) {setTimeout("Validacoes._delay_mascara_Telefone('" + campoID + "')" , 1);}},
	"_delay_mascara_Numero":function(campoID){
		vtext = $(campoID).value;
		var patt1=new RegExp("[^01234567890]","g");
		vtext = vtext.replace( patt1, "" );
		$(campoID).value = vtext;

	},
	"_delay_mascara_Data":function (campoID) {
		vtext = $(campoID).value;		
		var patt1=new RegExp("[^01234567890]","g");
		vtext = vtext.replace( patt1, "" ) 
		vstart = vtext.length
		if( vtext.length >= 2 )
			{vstart ++;}
		if( vtext.length >= 4 )
			{vstart ++;}
		vtext +=  "________";
		vtext = vtext.substr(0,8);
		
		vtext2 = vtext.substr(0,2)  +"/" + vtext.substr(2,2) + "/" + vtext.substr(4,4)
		$(campoID).value = vtext2;
		if( $(campoID).setSelectionRange )
			{$(campoID).setSelectionRange( vstart , 10 );}
		else
			{	var range = $(campoID).createTextRange();
				range.collapse(true);
				range.moveEnd('character', 10);
				range.moveStart('character', vstart);
				range.select();
			}

	},
	"_delay_mascara_CPF": function (campoID) {
		vtext = $(campoID).value;		
		var patt1=new RegExp("[^01234567890]","g");
		vtext = vtext.replace( patt1, "" ) 
		vstart = vtext.length
		if( vtext.length >= 3 )
			{vstart ++;}
		if( vtext.length >= 6 )
			{vstart ++;}
		if( vtext.length >= 9 )
			{vstart ++;}
		vtext +=  "______________"
		vtext = vtext.substr(0,11);
		vtext2 = vtext.substr(0,3)  +"." + vtext.substr(3,3) + "." + vtext.substr(6,3) + "-" + vtext.substr(9,2)
		$(campoID).value = vtext2;
		if( $(campoID).setSelectionRange )
			{$(campoID).setSelectionRange( vstart , 14 );}
		else
			{	var range = $(campoID).createTextRange();
				range.collapse(true);
				range.moveEnd('character', 14);
				range.moveStart('character', vstart);
				range.select();
			}

		if( vstart >= 11 )
			{Form.Element.activate($(campoID).next("input"))}
	},
	"_delay_mascara_CNPJ": function (campoID) {
		vtext = $(campoID).value;		
		var patt1=new RegExp("[^01234567890]","g");
		vtext = vtext.replace( patt1, "" ) 
		vstart = vtext.length
		if( vtext.length >= 2 )
			{vstart ++;}
		if( vtext.length >= 5 )
			{vstart ++;}
		if( vtext.length >= 8 )
			{vstart ++;}
		if( vtext.length >= 12 )
			{vstart ++;}

		vtext +=  "______________"
		vtext = vtext.substr(0,14);
		vtext2 = vtext.substr(0,2)  +"." + vtext.substr(2,3) + "." + vtext.substr(5,3) + "/" + vtext.substr(8,4) + "-" + vtext.substr(12,2)
		$(campoID).value = vtext2;
		if( $(campoID).setSelectionRange )
			{$(campoID).setSelectionRange( vstart , 18 );}
		else
			{	var range = $(campoID).createTextRange();
				range.collapse(true);
				range.moveEnd('character', 18);
				range.moveStart('character', vstart);
				range.select();
			}

		if( vstart >= 14 )
			{Form.Element.activate($(campoID).next("input"))}
	},
	"_delay_mascara_Telefone": function( campoID ) {
		vtext = $(campoID).value;		
		var patt1=new RegExp("[^01234567890]","g");
		vtext = vtext.replace( patt1, "" ) 
		vstart = vtext.length
		if( vtext.length >= 4 )
			{vstart ++;}

		vtext +=  "______________"
		vtext = vtext.substr(0,8);
		vtext2 = vtext.substr(0,4)  +"-" + vtext.substr(4,4)
		$(campoID).value = vtext2;
		if( $(campoID).setSelectionRange )
			{$(campoID).setSelectionRange( vstart , 9 );}
		else
			{	var range = $(campoID).createTextRange();
				range.collapse(true);
				range.moveEnd('character', 9);
				range.moveStart('character', vstart);
				range.select();
			}
	
	},
	"valida_CPF":function(valor){
			var patt1=new RegExp("[^01234567890]","g");
			vCPF = valor.replace( patt1, "" ) 
			var c = vCPF.substr(0,9); 
			var dv = vCPF.substr(9,2); 
			var d1 = 0; 
			for (var i=0; i<9; i++) { 
			 d1 += c.charAt(i)*(10-i); 
			 } 
			if (d1 == 0) return false; 
			d1 = 11 - (d1 % 11); 
			if (d1 > 9) d1 = 0; 
			if (dv.charAt(0) != d1){ 
			 return false; 
			} 
			d1 *= 2; 
			for (var i = 0; i < 9; i++)    { 
			  d1 += c.charAt(i)*(11-i); 
			} 
			d1 = 11 - (d1 % 11); 
			if (d1 > 9) d1 = 0; 
			if (dv.charAt(1) != d1){ 
			 return false; 
			} 
			return true; 
		},
	"valida_CNPJ":function(valor){
			var patt1=new RegExp("[^01234567890]","g");
			vCNPJ = valor.replace( patt1, "" ) 
			var a = new Array(); 
			var b = new Number; 
			var c = [6,5,4,3,2,9,8,7,6,5,4,3,2]; 
			for (i=0; i<12; i++){ 
			 a[i] = vCNPJ.charAt (i); 
			 b += a[i] * c[i+1]; 
			} 
			if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x } 
			b = 0; 
			for (y=0; y<13; y++) { 
			 b += (a[y] * c[y]); 
			} 
			if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; } 
			if ((vCNPJ.charAt(12) != a[12]) || (vCNPJ.charAt(13) != a[13])){ 
			 return false; 
			} 

			return true; 
	},
	"valida_data":function(campoID){
		    exp = /\d{2}\/\d{2}\/\d{4}/
		    if(!exp.test(data.value))
        		{return false;}
			else
				{return true;}
		},
	"valida_Numero":function(campoID){return true},
	"start":function(){
		vCampos = $$("input.mascara")
		vCampos.each( function( meucampo, index ) 
			{
				tipoValida = "";

				if( meucampo.hasClassName("numero") )
				{
					tipoValida = "numero";
					meucampo.observe( "keypress" , Validacoes.mascara_Numero.bindAsEventListener( Validacoes , meucampo.id ) );
				} else if(meucampo.hasClassName("data") )
				{
					tipoValida = "data";
					meucampo.observe( "keypress" , Validacoes.mascara_Data.bindAsEventListener( Validacoes , meucampo.id ) );
				} else if(meucampo.hasClassName("CPF") )
				{
					tipoValida = "CPF";
					meucampo.observe( "keypress" , Validacoes.mascara_CPF.bindAsEventListener( Validacoes , meucampo.id ) );
				} else if(meucampo.hasClassName("telefone") )
				{
					tipoValida = "telefone";
					meucampo.observe( "keypress" , Validacoes.mascara_Telefone.bindAsEventListener( Validacoes , meucampo.id ) );
				} else if(meucampo.hasClassName("CNPJ") )
				{
					tipoValida = "CNPJ";
					meucampo.observe( "keypress" , Validacoes.mascara_CNPJ.bindAsEventListener( Validacoes , meucampo.id ) );
				} 


				if ( tipoValida != "" )
				{
					Validacoes.nome_campos.push( meucampo.id );
					Validacoes.itens[ meucampo.id ] = tipoValida;
				}			
			}
			
		);
	},
	"validaCampos":function () {
			for( i = 0 ; i < Validacoes.nome_campos.length ; i ++ )
			{
				vcampo = Validacoes.nome_campos[i];
				if( Validacoes.itens[ vcampo ] == "data" && !Validacoes.valida_data( $(vcampo).value ) )
				{
					alert( "Formato de Data Inválida" );
					$(vcampo).focus();
					break;

				} else if (Validacoes.itens[ vcampo ] == "CPF" && !Validacoes.valida_CPF( $(vcampo).value ) )
				{
					alert( "CPF Inválido" );
					$(vcampo).focus();
					break;
				} else if (Validacoes.itens[ vcampo ] == "CNPJ" && !Validacoes.valida_CNPJ( $(vcampo).value ) )
				{
					alert( "CNPJ Inválido" );
					$(vcampo).focus();
					break;
				}
			}
	}
}


document.observe("dom:loaded", function() {
  Validacoes.start();
});