var msgErro = function(x, a, c){
	alert("Ocorreu algum erro durante esse processo.\r\n\r\n Entre em contato conosco ");
};

var init = function()
{
	//criar validacoes
	$.each($('.form'), function()
	{
		$(this).validate();
	});
	
	//criar trava
	$.each($('textarea[TravaQtd]'), function()
	{
		var o = $(this);
		var qtd = parseInt(o.attr('TravaQtd'));
		var lbl = $("#" + o.attr('TravaLabel'));
		o.keypress(function(e){
			if(this.value.length >= qtd)
			{
				this.value = this.value.substring(0, (qtd - 1));
				return;
			}
			lbl.text(qtd - this.value.length);
		});
		
		o.keydown(function(e){
			lbl.text(qtd - this.value.length);
		});
	});
			
	//criar WatermarkInput
			$.each($('input[Watermark]'), function()
			{
		
				var Watermark = $(this).attr('Watermark');
				var cor = "#666666";
				if($(this).css('color'))
				{
					cor = $(this).css('color');
				}
				$(this).Watermark(Watermark, cor);
		
			});

			//criar maskaras
			$.each($('input[Mask]'), function()
			{
		
				var Mask = $(this).attr('Mask');
				$(this).mask(Mask);
		
			});

			//seta focus no campo
			$.each($('*[@FocusInit]'), function()
			{
		
				var Focus = $(this).attr('FocusInit');
				if(Focus){
				
					$(this).focus();
					
				}
		
			});
};

var conf = function(msg){
	if(!msg){
		return confirm('Deseja realmente remover este registro ?');
	}
	else{
		return confirm(msg);
	}
};

var confURL = function(url){
	if(conf())
	{
		window.location.href = url;
	}
};

var LinhaCor = function()
{
	$.each($(".Grid"), function(){
				var conta = 1;
				$.each($("tbody tr", this), function(){
					if(conta == 0)
					{
						$(this).addClass("Alter");
						conta = 1;
					}
					else
					{
						conta = 0;
					}
				});
			});
};

function formatarValor(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 == 13) return true;  // Enter
	if (whichCode == 8 ) fld.value = '';
	else{
		key = String.fromCharCode(whichCode);  // Get key value from key code
		if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
		len = fld.value.length;
		for(i = 0; i < len; i++)
			if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
		aux = '';
		for(; i < len; i++)
			if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
		aux += key;
		len = aux.length;
		if (len == 0) fld.value = '';
		if (len == 1) fld.value = '0'+ decSep + '0' + aux;
		if (len == 2) fld.value = '0'+ decSep + aux;
		if (len > 2) {
			aux2 = '';
			for (j = 0, i = len - 3; i >= 0; i--) {
				if (j == 3) {
					aux2 += milSep;
					j = 0;
				}
				aux2 += aux.charAt(i);
				j++;
			}
			fld.value = '';
			len2 = aux2.length;
			for (i = len2 - 1; i >= 0; i--)
				fld.value += aux2.charAt(i);
			fld.value += decSep + aux.substr(len - 2, len);
		}
	}
	return false;
}
function formatarData(field, e)
{
	PermiteNumeros(e);
	if(field.value.length == 2 || field.value.length == 5)
	{
		field.value += '/';
	}
}

function formatarHora(field, e)
{
	PermiteNumeros(e);
	if(field.value.length == 2)
	{
		field.value += ':';
	}
}

function formatarCEP(field, e)
{
	PermiteNumeros(e);
	if(field.value.length == 5)
	{
		field.value += '-';
	}
}

function formatarCPF(field, e)
{
	PermiteNumeros(e);
	if(field.value.length == 3 ||
	   field.value.length == 7)
	{
		field.value += '.';
	}
	
	if(field.value.length == 11)
	{
		field.value += '-';
	}
}

function formatarCNPJ(field, e)
{
	PermiteNumeros(e);
	if(field.value.length == 2 ||
	   field.value.length == 6)
	{
		field.value += '.';
	}
	
	if(field.value.length == 10)
	{
		field.value += '/';
	}
	
	if(field.value.length == 15)
	{
		field.value += '-';
	}
}

function formatarTelefone(field, e)
{
	PermiteNumeros(e);
	if(field.value.length == 0)
	{
		field.value += '(';
	}

	if(field.value.length == 3)
	{
		field.value += ') ';
	}

	if(field.value.length == 9)
	{
		field.value += '-';
	}
}
function PermiteNumeros(e)
{
	var tecla = (window.Event) ? e.which : e.keyCode;
  	tecla     = String.fromCharCode(tecla);
  	if(!((tecla >= "0") && (tecla <= "9")))
  	{
		(navigator.appName == 'Netscape')? e.preventDefault() : e.keyCode=0;
	}
}

function selecionaAction(pagina, form)
{
	if(!form)
	{
		form = ".form";
	}
	$(form).attr("action", pagina);
	$(form).submit();
}