//Validação CPF
function validaCPF(cpf) {

        cpf = cpf.replace (".","");
        cpf = cpf.replace (".","");
        cpf = cpf.replace ("-","");

		if(cpf == 'xxxxxxxxxxx'){
			return true;
		}

        erro = new String;
        if (cpf.length < 11) erro += "Sao necessarios 11 digitos para verificacao do CPF! \n\n";
        var nonNumbers = /\D/;
        if (nonNumbers.test(cpf)) erro += "A verificacao de CPF suporta apenas numeros! \n\n";
        if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
                erro += "Numero de CPF invalido!"
        }
        var a = [];
        var b = new Number;
        var c = 11;
        for (i=0; i<11; i++){
                a[i] = cpf.charAt(i);
                if (i < 9) b += (a[i] * --c);
        }
        if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
        b = 0;
        c = 11;
        for (y=0; y<10; y++) b += (a[y] * c--);
        if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
        if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
                erro +="Digito verificador com problema!";
        }
        if (erro.length > 0){
                return false;
        }
        return true;
}

//Validação CNPJ
function validaCNPJ(cnpj) {
	cnpj = cnpj.replace (".","");
	cnpj = cnpj.replace (".","");
	cnpj = cnpj.replace ("-","");
    cnpj = cnpj.replace ("/","");

	if (cnpj == 'xxxxxxxxxxxxxx'){
		return true;
	} else if (cnpj == '00000000000000') {
		return false;
	}

	var a = [];
	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] = cnpj.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 ((cnpj.charAt(12) != a[12]) || (cnpj.charAt(13) != a[13])){
		return false;
	} else {
		return true;
	}
}


/*MASCARA*/

function mascara(o,f){
    v_obj=o;
    v_fun=f;
    setTimeout("execmascara()",1);
}

function execmascara(){
	v_obj.value=v_fun(v_obj.value);
}

function leech(v){
    v=v.replace(/o/gi,"0")
    v=v.replace(/i/gi,"1")
    v=v.replace(/z/gi,"2")
    v=v.replace(/e/gi,"3")
    v=v.replace(/a/gi,"4")
    v=v.replace(/s/gi,"5")
    v=v.replace(/t/gi,"7")
    return v
}

function m_data(v){
    v=v.replace(/\D/g,"");                 //Remove tudo o que não é dígito
    v=v.replace(/^(\d\d)(\d)/g,"$1/$2");   //Coloca parênteses em volta dos dois primeiros dígitos
    v=v.replace(/(\d{2})(\d)/,"$1/$2");    //Coloca hífen entre o quarto e o quinto dígitos
    return v;
}

function m_cep(v){
    v=v.replace(/\D/g,"");                 //Remove tudo o que não é dígito
    v=v.replace(/(\d{5})(\d)/,"$1-$2");    //Coloca hífen entre o quarto e o quinto dígitos
    return v;
}

function m_telefone(v){
    v=v.replace(/\D/g,"")                 //Remove tudo o que não é dígito
    v=v.replace(/^(\d\d)(\d)/g,"($1) $2") //Coloca parênteses em volta dos dois primeiros dígitos
    v=v.replace(/(\d{4})(\d)/,"$1-$2")    //Coloca hífen entre o quarto e o quinto dígitos
    return v
}

function m_cnpj(v){
	v=v.replace(/\D/g,"") //Remove tudo o que não é dígito
	v=v.replace(/^(\d{3})(\d)/,"$1.$2") //Coloca ponto entre o segundo e o terceiro dígitos
	v=v.replace(/^(\d{3})\.(\d{3})(\d)/,"$1.$2.$3") //Coloca ponto entre o quinto e o sexto dígitos
	v=v.replace(/\.(\d{3})(\d)/,".$1/$2") //Coloca uma barra entre o oitavo e o nono dígitos
	v=v.replace(/(\d{4})(\d)/,"$1-$2") //Coloca um hífen depois do bloco de quatro dígitos
	return v
}

function m_cpf(v){
	v=v.replace(/\D/g,"") //Remove tudo o que não é dígito
	v=v.replace(/^(\d{3})(\d)/,"$1.$2") //Coloca ponto entre o segundo e o terceiro dígitos
	v=v.replace(/^(\d{3})\.(\d{3})(\d)/,"$1.$2.$3") //Coloca ponto entre o quinto e o sexto dígitos
	v=v.replace(/\.(\d{3})(\d)/,".$1-$2") //Coloca uma barra entre o oitavo e o nono dígitos
	return v
}


function jFormError(input, error) {
	// Pega o element
	var element = input.parent();
	if (element.is('.parent_label')) {
		element = element.parent();
	} else if (element.is('.duplo_label')) {
		element = element.prev();
	} else if (element.is('.duplo_parent_label')) {
		element = element.parent().parent();
	} else if (element.is('.duplo_parent_label-prev')) {
		element = element.parent().parent().prev();
	} else if (element.is('.triplo_label')) {
		element = element.prev().prev();
	} else if (element.is('.quadruplo_label')) {
		element = element.prev().prev().prev();
	} else if (input.is('.label_datepicker')) {
		element = element.parent().parent().parent();
	}

	// Escreve a div do erro
	if ( $('div#jFormError').html() == null ) {
		var data = '<div id="jFormError">';
			data += '</div>';

		$('body').append( data );
	}

	// Escreve o Erro
	$('div#jFormError').html( error );
	$('div#jFormError').insertBefore( element );
	$('div#jFormError').show();

	// Pega o offset
	var element = $('div#jFormError');

	// Sobe ate o erro
	idk.focus(element);
}

jQuery.fn.jField = function(options){
	// Inicial as configuracoes
	var options = options || {};

	// Configuracoes basicas ou nao
	options.name				= options.name;
	options.minLength			= options.minLength || 0;
	options.minLengthError		= options.minLengthError || false;
	options.type 				= options.type || 'alphasInt';
	options.typeError			= options.typeError || false;
	options.empty				= options.empty || 'yes';
	options.emptyError			= options.emptyError || false;
	options.showError			= options.showError || true;
	options.showIcon			= options.showIcon || 'true';
	options.showRequiredIcon	= options.showRequiredIcon || 'true';
	options.telDDD				= options.telDDD || false;

	// Metodos para o atributo
	var jField = this;
	jField.options = options;

	// Validacao imediata
	this.keyup(function(event){
		$.jForm.checkField( jField, this.id );
	});

	// Formata Valores Reais
	if (options.type == 'currency')
	{
		this.keypress(function(event){
			reais(this, event);
		});

		this.keydown(function(event){
			backspace(this, event);
		});
	}

	// Format Telefone
	if (options.type == 'tel') {
		this.unmask();
		this.mask('(99) 9999-9999');
	} else if (options.type == 'cpf') {
		// Format CPF
		this.unmask();
		this.mask('999.999.999-99');
	} else if (options.type == 'cnpj') {
		// Format CNPJ
		this.unmask();
		this.mask('99.999.999/9999-99');
	} else if (options.type == 'cep') {
		// Format CEP
		this.unmask();
		this.mask('99999-999');
	}

	// Valida imediata para SELECT
	if (options.type == 'select')
	{
		this.change(function(){
			$.jForm.checkField( jField, this.id );
		});
	}

	// Retorna o objeto jQuery + jField
	return jField;
}

/*
 * $(obj).jForm - Informacoes do formulario
 *
 * Pega os campos de $(obj).jField e, quando enviado, chama $.jForm
 */
jQuery.fn.jForm = function(jFields, messageBox){
	// Inicializa os campos
	var jForm 		= {};
	jForm.jFields	= jFields || {};

	// Configuracoes do jForm
	jForm.config 				= {}
	jForm.config.messageBox	= messageBox || 'msgErro';

	// Check inicial, eh requirido ?
	$.jForm.checkRequireds( jForm );

	// Form Enviado
	/*this.submit(function(){
		var valid = $.jForm.checkFields( jForm );

		if (!valid) {
			return false;
		} else {
			$.jForm.floatlizeFields( jForm );
		}
	});*/

	return jForm;
}

/*
 * $.jForm - Checagem do formulario
 *
 * Quando o formulario e enviado, chama-se o $.jForm e faz a checagem
 */
$.jForm = {
	// Remove todas as classes de validacao e deixa apenas a que for ativada
	activeClass:
		function( jField, id, className ){
			var showIcon = jField.options.showIcon;

			if (showIcon == 'false')
				return false;

			$('#' + id).parents('label').removeClass('obrigatorio');
			$('#' + id).parents('label').removeClass('naoValida');
			$('#' + id).parents('label').removeClass('valida');

			if (className != false) {
				$('#' + id).parents('label').addClass(className);
			}
		}

	// Diz se o valor e valido ou nao, retorna a mensagem de erro
	,isValid:
		function( jField, id ){
			var minLength  		= jField.options.minLength;
			var minLengthError  	= jField.options.minLengthError;
			var type 				= jField.options.type;
			var typeError 			= jField.options.typeError;
			var value 				= jField.val();
			var empty				= jField.options.empty;
			var emptyError 			= jField.options.emptyError;
			var output 				= {valid: true, empty: false, error: false, showError: true};

			if (type == 'select')
			{
				if (empty == 'no' && (value == 0 || value == '0'))
				{
					output = {valid: false, empty: true, error: 'Selecione um valor em "' + jField.options.name + '"' };
				}
				else
				{
					output.valid = true;
					output.empty = true;
					output.error = false;
				}
			}
			else
			{
				// Regex Match
				if (type == 'int' && value.match(/[^\d]/))
				{
					jField.val( value.replace(/\D/g, '') );
					output = {valid: false, error: 'Preencha apenas com n&uacute;meros o campo "' + jField.options.name + '"' };
				}
				else if (type == 'cel' && (value.match(/[^\d]/) || (value.substr(0,1) != 7 && value.substr(0,1) != 8 && value.substr(0,1) != 9))) {
					jField.val( value.replace(/\D/g, '') );

					output = {valid: false, error: 'Preencha com um n&uacute;mero v&aacute;lido o campo "' + jField.options.name + '"' };
				}
				else if (type == 'currency' && !value.match(/^([0-9,.]+)$/))
				{
					output = {valid: false, error: 'Coloque valores inteiros sem pontos e centavos separados por v&iacute;rgula em "' + jField.options.name + '". Exemplo 12345,67' };
				}
				else if (type == 'date' && !value.match(/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/))
				{
					output = {valid: false, error: 'O formato da data em "' + jField.options.name + '" deve ser dd/mm/aaaa. Exemplo 04/10/2006' };
				}
				else if (type == 'date-biggest')
				{
					if (!value.match(/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/)) {
						output = {valid: false, error: 'O formato da data em "' + jField.options.name + '" deve ser dd/mm/aaaa. Exemplo 04/10/2006' };
					} else {
						var parts = value.split('/');
						var valueDate = new Date(new Number(parts[2]), (new Number(parts[1])) - 1, new Number(parts[0]));
						var atualDate = new Date();

						if (valueDate.getTime() < atualDate.getTime()) {
							output = {valid: false, error: 'A data em "' + jField.options.name + '" deve ser maior que a data atual' };
						}
					}
				}
				else if (type == 'float' && !value.match(/^([0-9]+\.?([0-9]{0,}))$/))
				{
					output = {valid: false, error: 'As casas decimais de "' + jField.options.name + '" tem que ser separado por ponto. Exemplo 9.99' };
				}
				else if (type == 'email' && !value.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/))
				{
					output = {valid: false, error: 'Preencha o campo "' + jField.options.name + '" com um e-mail v&aacute;lido' };
				}
				else if (type == 'email_confirmation')
				{
					var value2 = $('#' + id.replace('Confirma', '') ).val();

					if (value != value2 || !value.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)) {
						output = {valid: false, error: 'O e-mail e sua confirma&ccedil;&atilde;o devem ser iguais' };
					}
				}
				else if (type == 'password' && value.length < minLength)
				{
					output = {valid: false, error: 'Preencha o campo "' + jField.options.name + '" com uma senha v&aacute;lida' };
				}
				else if (type == 'password_confirmation')
				{
					var value2 = $('#' + id.replace('Confirma', '') ).val();
					if (value != value2 || value.length < minLength) {
						output = {valid: false, error: 'A senha e sua confirma&ccedil;&atilde;o devem ser iguais' };
					}
					/*


						$.jForm.activeClass( jField, id.replace('Confirma', ''), 'naoValida' );
					} else {
						$.jForm.activeClass( jField, id, 'valida' );
						$.jForm.activeClass( jField, id.replace('Confirma', ''), 'valida' );
					}
					*/
				}
				else if (type == 'cpf' && !validaCPF(value))
				{
						output = {valid: false, error: 'Preencha o campo "' + jField.options.name + '" com um cpf v&aacute;lido. No formato 000.000.000-00' };
				}
				else if (type == 'cnpj' && !validaCNPJ(value))
				{
						output = {valid: false, error: 'Preencha o campo "' + jField.options.name + '" com um cnpj v&aacute;lido. No formato 00.000.000/0000-00.' };
				}
				else if (type == 'tel' || type == 'cel')
				{
					if (value.match(/^\(__\) ____-____$/)) {
						value = '';
					} else if (!value.match(/^\(([0-9]{2})\) ([0-9]{4})-([0-9]{4})$/)) {
						output = {valid: false, error: 'Coloque um telefone v&aacute;lido para o campo "' + jField.options.name + '".' };
					}
				}
				else if (type == 'cep')
				{
					if (value.match(/^_____-___$/)) {
						value = '';
					} else if (!value.match(/^([0-9]{5})-([0-9]{3})$/)) {
						output = {valid: false, error: 'Coloque um CEP v&aacute;lido para o campo "' + jField.options.name + '".' };
					}
				}

				if (output.valid == false && typeError != false)
					output.error = typeError;

				// Not Empty
				if (empty == 'no' && !value)
				{
					output.error = emptyError || '&Eacute; obrigat&oacute;rio o preenchimento de "' + jField.options.name + '"';
					output.empty = true;
				} else if (type == 'currency' && empty == 'no') {
					var numero = new Number(value.replace(',', '').replace('.', ''));

					if (numero == 0) {
						output.error = emptyError || '&Eacute; obrigat&oacute;rio o preenchimento de "' + jField.options.name + '"';
						output.empty = true;
					}
				}

				// Min Length
				if (value && value.length < minLength)
				{
					output.error = minLengthError || 'Os caracteres m&iacute;nimos para "' + jField.options.name + '" s&atilde;o ' + minLength;
					output.empty = true;
				}

				// Empty
				if (empty == 'yes' && !value)
				{
					output.valid = true;
					output.empty = true;
					output.error = false;
					output.showError = false;

					//alert('label#label_' + id + ' - ' + $('label#label_' + id).css('display'));
				}
				//alert( 'label#label_' + id + ' - ' + $('label#label_' + id).css('display') );

				// Nao esta visivel
				if ($('#' + id).parents('label').css('display') == 'none')
				{
					output.valid = true;
					output.empty = true;
					output.error = false;
					output.showError = false;
				}
			}

			return output;
		}

	// Diz se o campo eh obrigatorio ou nao
	,isRequired:
		function(jField){
			if (jField.options.empty == 'yes')
				return false;

			return true;
		}

	// Ve se o valor e valido ou nao, muda class do label
	,checkField:
		function( jField, id ){
			var output = $.jForm.isValid( jField, id );

			// O campo esta validado, e pode estar vazio
			if (output.showError == false)
			{
				$.jForm.activeClass( jField, id, false );

				return output;
			}

			// Ocorreu um erro na validacao
			if (output.error != false)
				$.jForm.activeClass( jField, id, 'naoValida' );
			else
				$.jForm.activeClass( jField, id, 'valida' );

			return output;
		}

	// Para cada jField ve se e valido e retorna o erro no DIV
	,checkFields:
		function( jForm ){
			var error = 'nothing';
			for (id in jForm.jFields)
			{
				var output = $.jForm.checkField( jForm.jFields[id], id );

				if (output.error != false && error == 'nothing') {
					error = output.error;
					break;
				}
			}

			if (error != 'nothing')
			{
				jFormError( jForm.jFields[id], error );

				return false;
			}

			return true;
		}

	// Para cada jField ve se eh requirido ou nao ... coloca a classe
	,checkRequireds:
		function( jForm ){
			for (id in jForm.jFields)
			{
				var required = $.jForm.isRequired( jForm.jFields[id], id );
				var type = jForm.jFields[id].options.type;
				var value = jForm.jFields[id].val();
				var showIcon = jForm.jFields[id].options.showIcon;
				var showRequiredIcon = jForm.jFields[id].options.showRequiredIcon;

				if (((required && !value && type != 'select') || (type == 'select' && (value == 0 || value == '0'))) && showIcon == 'true' && showRequiredIcon == 'true') {
					$('#' + id).parent('label').addClass('obrigatorio');
				}
			}
		}

	// Troca virgulas(,) por pontos(.) pro MySQL entender
	,floatlizeFields:
		function( jForm ){
			for (id in jForm.jFields)
			{
				var jField = jForm.jFields[id];
				var type = jField.options.type;
				var value = jField.val();

				if (type == 'float' || type == 'currency')
				{
					value = value.replaceAll('.', '');
					value = value.replaceAll(',', '.');
					jField.val( value );
				}
			}
		}
};

String.prototype.replaceAll = function(a, b) {
	var c = this;
	while (c.indexOf(a) != -1) {
		c = c.replace(a, b);
	}

	return c;
};


/////// Mascaramento ////////
documentall = document.all;
/*
* fun??o para formata??o de valores monet?rios retirada de
* http://jonasgalvez.com/br/blog/2003-08/egocentrismo
*/

function formatamoney(c) {
	var t = this; if(c == undefined) c = 2;
	var p, d = (t=t.split("."))[1].substr(0, c);
	for(p = (t=t[0]).length; (p-=3) >= 1;) {
		t = t.substr(0,p) + "." + t.substr(p);
	}
	return t+","+d+Array(c+1-d.length).join(0);
}

String.prototype.formatCurrency=formatamoney

function demaskvalue(valor, currency){
	/*
	* Se currency ? false, retorna o valor sem apenas com os n?meros. Se ? true, os dois ?ltimos caracteres s?o considerados as
	* casas decimais
	*/
	var val2 = '';
	var strCheck = '0123456789';
	var len = valor.length;
	if (len== 0){
		return 0.00;
	}

	if (currency ==true){
		/* Elimina os zeros ? esquerda
		* a vari?vel  <i> passa a ser a localiza??o do primeiro caractere ap?s os zeros e
		* val2 cont?m os caracteres (descontando os zeros ? esquerda)
		*/

		for(var i = 0; i < len; i++)
			if ((valor.charAt(i) != '0') && (valor.charAt(i) != ',')) break;

		for(; i < len; i++){
			if (strCheck.indexOf(valor.charAt(i))!=-1) val2+= valor.charAt(i);
		}

		if(val2.length==0) return "0.00";
		if (val2.length==1)return "0.0" + val2;
		if (val2.length==2)return "0." + val2;

		var parte1 = val2.substring(0,val2.length-2);
		var parte2 = val2.substring(val2.length-2);
		var returnvalue = parte1 + "." + parte2;
		return returnvalue;

	}
	else{
			/* currency ? false: retornamos os valores COM os zeros ? esquerda,
			* sem considerar os ?ltimos 2 algarismos como casas decimais
			*/
			val3 ="";
			for(var k=0; k < len; k++){
				if (strCheck.indexOf(valor.charAt(k))!=-1) val3+= valor.charAt(k);
			}
	return val3;
	}
}

function reais(obj,event){

var whichCode = (window.Event) ? event.which : event.keyCode;
/*
Executa a formata??o ap?s o backspace nos navegadores !document.all
*/
if (whichCode == 8 && !documentall) {
/*
Previne a a??o padr?o nos navegadores
*/
	if (event.preventDefault){ //standart browsers
			event.preventDefault();
		}else{ // internet explorer
			event.returnValue = false;
	}
	var valor = obj.value;
	var x = valor.substring(0,valor.length-1);
	obj.value= demaskvalue(x,true).formatCurrency();
	return false;
}
/*
Executa o Formata Reais e faz o format currency novamente ap?s o backspace
*/
FormataReais(obj,'.',',',event);
} // end reais


function backspace(obj,event){
/*
Essa fun??o basicamente altera o  backspace nos input com m?scara reais para os navegadores IE e opera.
O IE n?o detecta o keycode 8 no evento keypress, por isso, tratamos no keydown.
Como o opera suporta o infame document.all, tratamos dele na mesma parte do c?digo.
*/

var whichCode = (window.Event) ? event.which : event.keyCode;
if (whichCode == 8 && documentall) {
	var valor = obj.value;
	var x = valor.substring(0,valor.length-1);
	var y = demaskvalue(x,true).formatCurrency();

	obj.value =""; //necess?rio para o opera
	obj.value += y;

	if (event.preventDefault){ //standart browsers
			event.preventDefault();
		}else{ // internet explorer
			event.returnValue = false;
	}
	return false;

	}// end if
}// end backspace

function FormataReais(fld, milSep, decSep, e) {
var sep = 0;
var key = '';
var i = j = 0;
var len = len2 = 0;
var strCheck = '0123456789';
var aux = aux2 = '';
var whichCode = (window.Event) ? e.which : e.keyCode;

//if (whichCode == 8 ) return true; //backspace - estamos tratando disso em outra fun??o no keydown
if (whichCode == 0 ) return true;
if (whichCode == 9 ) return true; //tecla tab
if (whichCode == 13) return true; //tecla enter
if (whichCode == 16) return true; //shift internet explorer
if (whichCode == 17) return true; //control no internet explorer
if (whichCode == 27 ) return true; //tecla esc
if (whichCode == 34 ) return true; //tecla end
if (whichCode == 35 ) return true;//tecla end
if (whichCode == 36 ) return true; //tecla home

/*
O trecho abaixo previne a a??o padr?o nos navegadores. N?o estamos inserindo o caractere normalmente, mas via script
*/

if (e.preventDefault){ //standart browsers
		e.preventDefault()
	}else{ // internet explorer
		e.returnValue = false
}

var key = String.fromCharCode(whichCode);  // Valor para o c?digo da Chave
if (strCheck.indexOf(key) == -1) return false;  // Chave inv?lida

/*
Concatenamos ao value o keycode de key, se esse for um n?mero
*/
fld.value += key;

var len = fld.value.length;
var bodeaux = demaskvalue(fld.value,true).formatCurrency();
fld.value=bodeaux;

/*
Essa parte da fun??o t?o somente move o cursor para o final no opera. Atualmente n?o existe como mov?-lo no konqueror.
*/
  if (fld.createTextRange) {
    var range = fld.createTextRange();
    range.collapse(false);
    range.select();
  }
  else if (fld.setSelectionRange) {
    fld.focus();
    var length = fld.value.length;
    fld.setSelectionRange(length, length);
  }
  return false;

}