// JEffects.js
// contém todos os efeitos do site

var JEffects = {
	handleEnter : function(field, event) {
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which
				: event.charCode;
		if (keyCode == 13) {
			var i;
			for (i = 0; i < field.form.elements.length; i++)
				if (field == field.form.elements[i])
					break;
			i = (i + 1) % field.form.elements.length;
			field.form.elements[i].focus();
			return false;
		} else
			return true;
	},
	msgAlert : function(titulo, msg) {
		$('body')
				.after(
						'<div style="height: 200px; min-height: 109px; width: auto;" class="ui-dialog-content ui-widget-content" id="dialog"><p id="msg-erro-content"></p></div>');
		$('#msg-erro-content').html(msg);
		$('#dialog').dialog( {
			title : titulo,
			bgiframe : true,
			modal : true,
			resizable : false,
			draggable : false,
			close : function(ev, ui) {
				$('#dialog').remove();
			},
			buttons : {
				"OK" : function() {
					$(this).dialog("close");
				}
			}
		});
	},
        confirmDialog: function(msg, url) {
            if(confirm(msg)) {
                window.location = url;
            }
        }
}

// constrole do menu
JEffects.menu = {
	menuEscolhido : null,

	exibeMenu : function(emenu) {
		JEffects.exibeItem(emenu);
		if (JEffects.menu.menuEscolhido != null)
			JEffects.escondeItem(JEffects.menu.menuEscolhido);
		if (JEffects.menu.menuEscolhido != emenu)
			JEffects.menu.menuEscolhido = emenu;
		else
			JEffects.menu.menuEscolhido = null;
	}
}
// fim pacote JEffects.menu

JEffects.masks = {
	numeros : function(obj) {
		var objValue = obj.value;
		while (/[^0-9]/.test(objValue))
			objValue = objValue.replace(/[^0-9]/, '');
		obj.value = objValue;
	},
	hora : function(vHora) {
		hora = vHora.value;
		nHora = hora.replace(/:/, '');
		nHora = nHora.replace(/[^0-9]/, '');
		var h = nHora.substring(0, 2);
		var m = nHora.substring(2, 4);
		var s = nHora.substring(4, 6);
		if (nHora.length == 1)
			vHora.value = h;
		if (nHora.length == 2)
			vHora.value = h + ":";
		if (nHora.length == 3)
			vHora.value = h + ":" + m;
		if (nHora.length == 4)
			vHora.value = h + ":" + m + ":";
		if (nHora.length == 5)
			vHora.value = h + ":" + m + ":" + s;
		if (nHora.length == 6)
			vHora.value = h + ":" + m + ":" + s;
	},
	data : function(obj) {
		var pass = obj.value;
		var expr = /[0123456789]/;
		for (i = 0; i < pass.length; i++) {
			// charAt -> retorna o caractere posicionado no índice especificado
			var lchar = obj.value.charAt(i);
			var nchar = obj.value.charAt(i + 1);
			if (i == 0) {
				// search -> retorna um valor inteiro, indicando a posiçao do
				// inicio da primeira
				// ocorrencia de expReg dentro de instStr. Se nenhuma ocorrencia
				// for encontrada o método retornara -1
				// instStr.search(expReg);
				if ((lchar.search(expr) != 0) || (lchar > 3)) {
					obj.value = "";
				}
			} else if (i == 1) {
				if (lchar.search(expr) != 0) {
					// substring(indice1,indice2)
					// indice1, indice2 -> será usado para delimitar a string
					var tst1 = obj.value.substring(0, (i));
					obj.value = tst1;
					continue;
				}
				if ((nchar != '/') && (nchar != '')) {
					var tst1 = obj.value.substring(0, (i) + 1);
					if (nchar.search(expr) != 0)
						var tst2 = obj.value.substring(i + 2, pass.length);
					else
						var tst2 = obj.value.substring(i + 1, pass.length);
					obj.value = tst1 + '/' + tst2;
				}
			} else if (i == 4) {
				if (lchar.search(expr) != 0) {
					var tst1 = obj.value.substring(0, (i));
					obj.value = tst1;
					continue;
				}
				if ((nchar != '/') && (nchar != '')) {
					var tst1 = obj.value.substring(0, (i) + 1);
					if (nchar.search(expr) != 0)
						var tst2 = obj.value.substring(i + 2, pass.length);
					else
						var tst2 = obj.value.substring(i + 1, pass.length);
					obj.value = tst1 + '/' + tst2;
				}
			}
			if (i >= 6) {
				if (lchar.search(expr) != 0) {
					var tst1 = obj.value.substring(0, (i));
					obj.value = tst1;
				}
			}
		}
		if (pass.length > 10)
			obj.value = obj.value.substring(0, 10);
		return true;
	},
	telefone : function(objTelefone) {
		telefone = objTelefone.value;
		fTelefone = telefone.replace(/[^0-9]/, '');
		fTelefone = fTelefone.replace(/ /, '');
		fTelefone = fTelefone.replace(/\(/, '');
		fTelefone = fTelefone.replace(/\)/, '');
		if (fTelefone.length > 8) {
			var ddd = fTelefone.substring(0, 2);
			var resto = fTelefone.substring(2, 10);
			objTelefone.value = "(" + ddd + ") " + resto;
		} else {
			objTelefone.value = fTelefone;
		}
	},
	valor : function(obj) {
		var valor = obj.value;
		while (/[^0-9]/.test(valor)) {
			valor = valor.replace(/[^0-9]/, '');
		}
		decimal = valor.substring((valor.length - 2), valor.length);
		if (valor.length == 3)
			inteiro = valor.substring(0, 1);
		else
			inteiro = valor.substring(0, (valor.length - 2));
		if (valor.length <= 2)
			obj.value = valor;
		if (valor.length > 2) {
			var tamInteiro = inteiro.length;

			obj.value = inteiro + "," + decimal;
		}
	},
	formataMoeda : function(objTextBox) {
		var caract = /[^0-9]/;
		var valor = objTextBox.value;
		while (caract.test(valor))
			valor = valor.replace(caract, '');
		caract = /^0/;
		while (caract.test(valor))
			valor = valor.replace(caract, '');
		var str = '';
		if (valor.length == 1)
			str = "0,0" + valor;
		if (valor.length == 2)
			str = "0," + valor;
		if (valor.length > 2) {
			var div;
			var decimal = valor.substr(valor.length - 2, 2);
			var inteiro = valor.substring(0, valor.length - 2);
			if (inteiro.length <= 3) {
				str = inteiro + "," + decimal;
			} else {
				i = 1;
				var resto = inteiro.length % 3;
				var tam = inteiro.length - resto;
				div = tam / 3;
				var textFormated = '';
				var init = resto;
				if (resto > 0)
					textFormated = inteiro.substr(0, resto) + ".";
				while (div != 0) {
					textFormated = textFormated + inteiro.substr(init, 3) + ".";
					init = init + 3;
					div--;
				}
				str = textFormated.replace(/\.$/, str) + "," + decimal;
			}
		}
		objTextBox.value = str;
	},

	cnpj : function(Campo, teclapres) {
		if (window.event) {
			var tecla = teclapres.keyCode;
		} else {
			tecla = teclapres.which;
		}
		var vr = new String(Campo.value);
		vr = vr.replace(".", "");
		vr = vr.replace(".", "");
		vr = vr.replace("/", "");
		vr = vr.replace("-", "");

		tam = vr.length + 1;

		if (tecla != 9 && tecla != 8) {
			if (tam > 2 && tam < 6)
				Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, 3);
			if (tam >= 6 && tam < 9)
				Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, 3) + '.'
						+ vr.substr(5, 3);
			if (tam >= 9 && tam < 13)
				Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, 3) + '.'
						+ vr.substr(5, 3) + '/' + vr.substr(8, 4);
			if (tam >= 13)
				Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, 3) + '.'
						+ vr.substr(5, 3) + '/' + vr.substr(8, 4) + '-'
						+ vr.substr(12, 2);
		}
	},
	inscricaoEstadual : function(obj) {
		var txt = obj.value;
		while (/[^0-9]/.test(txt))
			txt = txt.replace(/[^0-9]/, '');
		var c1 = txt.substr(0, 2);
		var c2 = txt.substr(2, 3);
		var c3 = txt.substr(5, 3);
		// var c4 = ;
		if (txt.length > 0 && txt.length <= 2)
			obj.value = c1;
		if (txt.length >= 3 && txt.length <= 5)
			obj.value = c1 + "." + c2;
		if (txt.length >= 6 && txt.length <= 8)
			obj.value = c1 + "." + c2 + "." + c3;
		if (txt.length >= 9) {
			obj.value = c1 + "." + c2 + "." + c3 + "-" + txt.substr(8, 1);
		}
	},
	registroJucepa : function(obj) {
		var txt = obj.value;
		for (i = 0; i < txt.length + 1; i++)
			txt = txt.replace(/[^0-9]/, '');
		obj.value = txt;
	},
	cep : function(obj) {
		var txt = obj.value;
		for (i = 0; i < txt.length + 1; i++)
			txt = txt.replace(/[^0-9]/, '');
		var c1 = txt.substr(0, 2);
		var c2 = txt.substr(2, 3);
		var c3 = txt.substr(5, 3);
		if (txt.length > 0 && txt.length <= 2)
			obj.value = c1;
		if (txt.length >= 3 && txt.length <= 5)
			obj.value = c1 + "." + c2;
		if (txt.length >= 6)
			obj.value = c1 + "." + c2 + "-" + c3;
	},
	cpf : function(campo, teclapres) {
		var vr = campo.value;
		while (/[^0-9]/.test(vr))
			vr = vr.replace(/[^0-9]/, '')
		var str = "";
		var t = vr.length;
		if (t >= 1 && t <= 3)
			str = vr.substr(0, 3);
		if (t >= 4 && t <= 6)
			str = vr.substr(0, 3) + "." + vr.substr(3, 3);
		if (t >= 7 && t <= 9)
			str = vr.substr(0, 3) + "." + vr.substr(3, 3) + "."
					+ vr.substr(6, 3);
		if (t >= 10)
			str = vr.substr(0, 3) + "." + vr.substr(3, 3) + "."
					+ vr.substr(6, 3) + "-" + vr.substr(9, 2);
		campo.value = str;
	},
	codigoNaturezaJuridica : function(objTextBox) {
		var str = objTextBox.value;
		while (/[a-zA-Z]/.test(str))
			str = str.replace(/[a-zA-Z]/, "");
		str = str.replace(/[a-zA-Z]/, "");
		objTextBox.value = str;
	},
	numericaMilhar : function(objTextBox) {
		var str = objTextBox.value;
		var caract = /[^0-9\.]/;
		while (caract.test(str))
			str = str.replace(caract, "");
		str = str.replace(caract, "");
		objTextBox.value = str;
	},
	numericaPorcentagem : function(objTextBox) {
		var str = objTextBox.value;
		var caract = /[^0-9]/;
		while (caract.test(str))
			str = str.replace(caract, "");
		str = str.replace(caract, "");
		if (str.length > 5)
			str = str.substr(0, str.length - (str.length - 5));
		if (str.length < 2)
			objTextBox.value = str;
		if (str.length == 2)
			objTextBox.value = str.substr(0, 1) + ","
					+ str.substr(str.length - 1, 1);
		if (str.length == 3)
			objTextBox.value = str.substr(0, 1) + ","
					+ str.substr(str.length - 2, 2);
		if (str.length == 4)
			objTextBox.value = str.substr(0, 2) + ","
					+ str.substr(str.length - 2, 2);
		if (str.length == 5)
			objTextBox.value = str.substr(0, 3) + ","
					+ str.substr(str.length - 2, 2);
	},
	number_format : function(number, decimals, dec_point, thousands_sep) {
		// Formats a number with grouped thousands
		//
		// version: 906.1806
		// discuss at: http://phpjs.org/functions/number_format
		// + original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
		// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
		// + bugfix by: Michael White (http://getsprink.com)
		// + bugfix by: Benjamin Lupton
		// + bugfix by: Allan Jensen (http://www.winternet.no)
		// + revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
		// + bugfix by: Howard Yeend
		// + revised by: Luke Smith (http://lucassmith.name)
		// + bugfix by: Diogo Resende
		// + bugfix by: Rival
		// + input by: Kheang Hok Chin (http://www.distantia.ca/)
		// + improved by: davook
		// + improved by: Brett Zamir (http://brett-zamir.me)
		// + input by: Jay Klehr
		// + improved by: Brett Zamir (http://brett-zamir.me)
		// + input by: Amir Habibi (http://www.residence-mixte.com/)
		// + bugfix by: Brett Zamir (http://brett-zamir.me)
		// * example 1: number_format(1234.56);
		// * returns 1: '1,235'
		// * example 2: number_format(1234.56, 2, ',', ' ');
		// * returns 2: '1 234,56'
		// * example 3: number_format(1234.5678, 2, '.', '');
		// * returns 3: '1234.57'
		// * example 4: number_format(67, 2, ',', '.');
		// * returns 4: '67,00'
		// * example 5: number_format(1000);
		// * returns 5: '1,000'
		// * example 6: number_format(67.311, 2);
		// * returns 6: '67.31'
		// * example 7: number_format(1000.55, 1);
		// * returns 7: '1,000.6'
		// * example 8: number_format(67000, 5, ',', '.');
		// * returns 8: '67.000,00000'
		// * example 9: number_format(0.9, 0);
		// * returns 9: '1'
		// * example 10: number_format('1.20', 2);
		// * returns 10: '1.20'
		// * example 11: number_format('1.20', 4);
		// * returns 11: '1.2000'
		// * example 12: number_format('1.2000', 3);
		// * returns 12: '1.200'
		var n = number, prec = decimals;
	
		var toFixedFix = function(n, prec) {
			var k = Math.pow(10, prec);
			return (Math.round(n * k) / k).toString();
		};
	
		n = !isFinite(+n) ? 0 : +n;
		prec = !isFinite(+prec) ? 0 : Math.abs(prec);
		var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
		var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;
	
		var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); // fix
		// for
		// IE
		// parseFloat(0.55).toFixed(0)
		// = 0;
	
		var abs = toFixedFix(Math.abs(n), prec);
		var _, i;
	
		if (abs >= 1000) {
			_ = abs.split(/\D/);
			i = _[0].length % 3 || 3;
	
			_[0] = s.slice(0, i + (n < 0))
					+ _[0].slice(i).replace(/(\d{3})/g, sep + '$1');
			s = _.join(dec);
		} else {
			s = s.replace('.', dec);
		}
	
		var decPos = s.indexOf(dec);
		if (prec >= 1 && decPos !== -1 && (s.length - decPos - 1) < prec) {
			s += new Array(prec - (s.length - decPos - 1)).join(0) + '0';
		} else if (prec >= 1 && decPos === -1) {
			s += dec + new Array(prec).join(0) + '0';
		}
		return s;
	},
	semNumeros: function(obj) {
		var exp = /[0-9]/;
		var str = obj.value;
		while(exp.test(str)) {
			str = str.replace(exp, '');
		}
		obj.value = str;
			
	},
	limpaCampoNumerico: function(obj) {
		if(obj.value == "0") {
			obj.value = "";
		}
	},
	completaCampoNumerico: function(obj) {
		if(obj.value == "") {
			obj.value = "0";
		}
	}
}

