/*
 * Created by: Ramon <ramon.silva@realweb.com.br>
 * data:06/2009
*/

	//Função que retorna true ou false, pra validação de campos
	function validaForm(){

		var valida = true;
		var obj = null;
		
		$(".campoForm.obrigatorio").each(function(){
			if($(this).val() == '') {
				valida = false;
				obj = -1;
			}
		});
		
		if(valida) { // só verifica os campos, se todos os obrigatórios foram preenchidos de alguma forma
		
				$(".campoForm").each(function(){
					//Verifica os tipos de validação
					var v = validacoes(this);
					if(v[0] == false){
						valida = v[0];
						obj = v[1];
					}
				});
		}
		
		return [valida, obj];
	}
		
	function validacoes(that){
		var valida = true;
		var obj = null;
		
		if($(that).hasClass("texto") ){ 
		
				if($(that).val() == '' && $(that).hasClass("obrigatorio")){ // se o valor for igual a vazio e for obrigatório
					valida = false;
					obj = that;
				}
		} else if ($(that).hasClass("email")) { //validação de email

				if(!validaEmail($(that).val()) && ($(that).hasClass("obrigatorio") || $(that).val() != '')){
					
					 valida = false; 
					 obj = that;
				}
		} else if ($(that).hasClass("numero") && ($(that).hasClass("obrigatorio") || $(that).val() != '')) {
				if(isNaN($(that).val()) || ($(that).hasClass("obrigatorio") && $(that).val() == '')){
				
					valida = false;
					obj = that;						
					
				}
			
		} else if ($(that).hasClass("data")) {
				
				if(!validaData($(that).val()) && ($(that).hasClass("obrigatorio") || $(that).val() != '')){
					valida = false; 
					obj = that;
				}
		} else if ($(that).hasClass("cpf") && ($(that).hasClass("obrigatorio") || $(that).val() != '')) {
		
				if(!validaCPF($(that).val()) && ($(that).hasClass("obrigatorio") || $(that).val() != '')){
				
					valida = false;
					obj = that;						
					
				}
			
		}
		
		return [valida, obj];
	}

	function validaCampo(obj){
		var v = validacoes(obj);
		return v[0];
	}
	
	function validaEmail(email){
			var rexp = /^([0-9a-zA-Z]+([_.-]?[0-9a-zA-Z]+)*@[0-9a-zA-Z]+[0-9,a-z,A-Z,.,-]*(.){1}[a-zA-Z]{2,4})+$/;
			return rexp.test(email);
	}
	
	function validaNumero(numero){
			return !isNaN(numero);
	}
	
	function validaData(data){
			var rexp = /^(([012]?[0-9]|[3][01])\/([0]?[1-9]|[1][0-2])\/(19|20)\d{2})$/;
			return rexp.test(data);
	}
	
	function validaCPF ( numcpf ) {
		 var x    = 0;
		 var soma = 0;
		 var dig1 = 0;
		 var dig2 = 0;
		 var _ret = false;
		 var txt  = "";
		 var numcpf1="";
		 var len = numcpf.length; 
			var x = len -1;
		 // var numcpf = "12345678909";
		 for( var i = 0; i <= len - 3; i++ ) 
			{
			var y = numcpf.substring( i, i + 1 );
			soma = soma + ( y * x );
			x = x - 1;
			txt = txt + y;
		 }// End for()

		 dig1 = 11 - ( soma % 11 );

		 if( dig1 == 10 )
			{
				dig1 = 0;
			}// End if

		 if (dig1 == 11) dig1=0 ;
		 numcpf1 = numcpf.substring(0,len - 2) + dig1 ;
		 x = 11; soma=0;
		 for (var i=0; i <= len - 2; i++) {
			soma = soma + (numcpf1.substring(i,i+1) * x);
			x = x - 1;
		 }
		 dig2= 11 - (soma % 11);
		 if (dig2 == 10) dig2=0;
		 if (dig2 == 11) dig2=0;
		 //alert ("Digito Verificador : " + dig1 + "" + dig2);
		 if ((dig1 + "" + dig2) == numcpf.substring(len,len-2)) {
			_ret = true;
		 }
			else
			{
				_ret = false;
			}
		 return _ret;

  }// End metodo valid_CPF()
	
	//Função responsável por montar a query de envio do ajax
	function montaQueryURL(){
		var query = '';
		$(".campoForm").each(function(){
			if($(this).val() != '') {
				query += query == '' ? '' : '&';
				query += this.id + "=" + $(this).val();
			}
		});
	
		return query;
	}
	
	//função responsável por fazer as máscaras nos forms
	function mascaraForm(){
		
		//todos os campos que tiverem a classe textDefault, armazenará o texto na propriedade textDefault, que foi criado dinamicamente
		$(".campoForm.textDefault").each(function(){
			$(this).attr("textDefault", this.value);
		})
		
		//o texto que vem no input desaparecerá, se o texto for igual ao inicial 
		$(".campoForm.textDefault").focus(function(){
			if( $(this).attr("textDefault") == this.value ) $(this).val("");
		});
		
		// se a caixa estiver vazia, voltará ao texto default
		$(".campoForm.textDefault").blur(function(){
			if( '' == this.value ) this.value = $(this).attr("textDefault");
		});
		
		$(".campoForm.data").keyup(function(){
			var len = $(this).val().length;
			switch(len){
				case 2:
				case 5:
					$(this).val($(this).val()+'/');
				break;
			}
		});

		$(".campoForm.numero").keyup(function(){
			var len = 0;
			while(!validaNumero($(this).val()) && $(this).val().length > 0) {//Enquanto não validar a caixa de texto, vai retirando o ultimo caracter da caixa;
				len = $(this).val().length-1;
				$(this).val($(this).val().substr(0,len));
			}
			
		});	
	}
	