function MM_swapImgRestore() 
{ //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function Verifica_Email(Valor){
	var regmail = /^[\w!#$%&'*+\/=?^`{|}~-]+(\.[\w!#$%&'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
	if (regmail.test(Valor)){
		//alert("E-mail válido");
		return true
	} 
	else{
		//alert("E-mail inválido");
		return false;
	}

	if (regmail.test($("#email").val())){
		//alert("E-mail válido");
		return true
	} 
	else{
		//alert("E-mail inválido");
		return false;
	}	
}

function tecla(a, Formulario, Recipient)
{
	try
	{
	    var Key = a.keyCode || a.which;
	    
		//QDO FOR TECLADO ENTER
		if(Key == "13")
		{
			var campo = Recipient;
			campo.value = 1;
			Formulario.submit();
		}
	}
	catch(err)
	{
		//alert(err);
	}
}

function pularcampo(wobj, alvo, maxlenght)
{
	try
	{
		if (document.getElementById(wobj).value.length==maxlenght)
		{
			var Obj = document.getElementById(alvo);
			Obj.focus();
		}
	}
	catch(err)
	{
		//alert(err);
	}
}

// Máscara de Campos Texto

addEvent = function(o, e, f, s)
{
    var r = o[r = "_" + (e = "on" + e)] = o[r] || (o[e] ? [[o[e], o]] : []), a, c, d;
    r[r.length] = [f, s || o], o[e] = function(e)
	{
        try
	{
            (e = e || event).preventDefault || (e.preventDefault = function(){e.returnValue = false;});
            e.stopPropagation || (e.stopPropagation = function(){e.cancelBubble = true;});
            e.target || (e.target = e.srcElement || null);
            e.key = (e.which + 1 || e.keyCode + 1) - 1 || 0;
        }catch(f){}
        for(d = 1, f = r.length; f; r[--f] && (a = r[f][0], o = r[f][1], a.call ? c = a.call(o, e) : (o._ = a, c = o._(e), o._ = null), d &= c !== false));
        return e = null, !!d;
    }
};

removeEvent = function(o, e, f, s){
    for(var i = (e = o["_on" + e] || []).length; i;)
        if(e[--i] && e[i][0] == f && (s || o) == e[i][1])
            return delete e[i];
    return false;
};

MaskInput = function(f, m){
	function mask(e){
		var patterns = {"1": /[A-Z]/i, "2": /[0-9]/, "4": /[\xC0-\xFF]/i, "8": /./ },
			rules = { "a": 3, "A": 7, "9": 2, "C":5, "c": 1, "*": 8};
		function accept(c, rule){
			for(var i = 1, r = rules[rule] || 0; i <= r; i<<=1)
				if(r & i && patterns[i].test(c))
					break;
				return i <= r || c == rule;
		}
		var k, mC, r, c = String.fromCharCode(k = e.key), l = f.value.length;
		(!k || k == 8 ? 1 : (r = /^(.)\^(.*)$/.exec(m)) && (r[0] = r[2].indexOf(c) + 1) + 1 ?
			r[1] == "O" ? r[0] : r[1] == "E" ? !r[0] : accept(c, r[1]) || r[0]
			: (l = (f.value += m.substr(l, (r = /[A|9|C|\*]/i.exec(m.substr(l))) ?
			r.index : l)).length) < m.length && accept(c, m.charAt(l))) || e.preventDefault();
	}
	for(var i in !/^(.)\^(.*)$/.test(m) && (f.maxLength = m.length), {keypress: 0, keyup: 1})
		addEvent(f, i, mask);
};

function formataMoeda(o, n, dig, dec){
	o.c = !isNaN(n) ? Math.abs(n) : 2;
	o.dec = typeof dec != "string" ? "," : dec, o.dig = typeof dig != "string" ? "." : dig;
	addEvent(o, "keypress", function(e){
		if(e.key > 47 && e.key < 58){
			var o, s, l = (s = ((o = this).value.replace(/^0+/g, "") + String.fromCharCode(e.key)).replace(/\D/g, "")).length, n;
			if(o.maxLength + 1 && l >= o.maxLength) return false;
			l <= (n = o.c) && (s = new Array(n - l + 2).join("0") + s);
			for(var i = (l = (s = s.split("")).length) - n; (i -= 3) > 0; s[i - 1] += o.dig);
			n && n < l && (s[l - ++n] += o.dec);
			o.value = s.join("");
		}
		e.key > 30 && e.preventDefault();
	});
}

// Fim Máscara de Campos Texto

//Função TRIM
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, "");
}

String.prototype.ltrim = function() {
	return this.replace(/^\s+/, "");
}

String.prototype.rtrim = function() {
	return this.replace(/\s+$/, "");
}

function mostra(Formulario)
{
	try
	{
		Formulario.submit();
		//document.form.cidade.focus();
	}
	catch(err)
	{
		//alert(err);
	}
}

function validaCNPJ(cnpj)
{
	//cnpj = cnpj.value;
	//var cnpj = document.getElementById(val).value;
	//REMOVE CARACTERES INVALIDOS
	// /gi REMOVEM O QUE SE PROCURA EM TODA A STRING E NÃO DIFERENCIA MINUSCULA DE MAIUSCULA
	cnpj = cnpj.replace(".","");
	cnpj = cnpj.replace(".","");
	cnpj = cnpj.replace("/","");
	cnpj = cnpj.replace("-","");
	
	var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;

	digitos_iguais = 1;

	for (var i=0; i < cnpj.length - 1; i++)
	{
		if (cnpj.charAt(i) != cnpj.charAt(i + 1))
		{
			digitos_iguais = 0;
			break;
		}
	}

	if (!digitos_iguais)
	{
		tamanho = cnpj.length - 2
		numeros = cnpj.substring(0,tamanho);
		digitos = cnpj.substring(tamanho);
		soma = 0;
		pos = tamanho - 7;

		for (var i=tamanho; i >= 1; i--)
		{
			soma += numeros.charAt(tamanho - i) * pos--;
			if (pos < 2)
			{
				pos = 9;
			}
		}
		resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;

		if (resultado != digitos.charAt(0))
		{
			//alert('CNPJ inválido!!');
			return false;
		}

		tamanho = tamanho + 1;
		numeros = cnpj.substring(0,tamanho);
		soma = 0;
		pos = tamanho - 7;
		
		for (i = tamanho; i >= 1; i--)
		{
			soma += numeros.charAt(tamanho - i) * pos--;
			if (pos < 2)
			{
				pos = 9;
			}
		}

		resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;

		if (resultado != digitos.charAt(1))
		{
			//alert('CNPJ inválido!!');
			return false;
		}

		return true;
	}
	else
	{
		//alert('CNPJ inválido!!');
		return false;
	}
}

function validaCPF(ipt)
{
	ipt = ipt.value;
	//var ipt = document.getElementById(val).value;
	//REMOVE CARACTERES INVALIDOS
	// /gi REMOVEM O QUE SE PROCURA EM TODA A STRING E NÃO DIFERENCIA MINUSCULA DE MAIUSCULA
	ipt = ipt.replace(".","");
	ipt = ipt.replace(".","");
	//ipt = ipt.replace(".","");
	ipt = ipt.replace("-","");
	ipt = ipt.replace("/","");
	
	if ((ipt == "00000000000") || (ipt == "11111111111") || (ipt == "22222222222") || (ipt == "33333333333")
		|| (ipt == "44444444444") || (ipt == "55555555555") || (ipt == "66666666666") || (ipt == "77777777777")
		|| (ipt == "88888888888") || (ipt == "99999999999"))
	{
		//alert('CPF inválido!!');
		return false;
	}
	
	var p1cpf = new String(ipt.substr(0, 3));
	var p2cpf = new String(ipt.substr(3, 3));
	var p3cpf = new String(ipt.substr(6, 3));

	var c = p1cpf + p2cpf + p3cpf;

	var dv = ipt.substr(9,2);
	var d1 = 0;

	for (i = 0; i < 9; i++)
	{
		d1 += c.charAt(i)*(10-i);
	}
	if (d1 == 0)
	{
		//alert('CPF inválido!!');
		return false;
	}

	d1 = 11 - (d1 % 11);

	if (d1 > 9)
		d1 = 0;

	if (dv.charAt(0) != d1)
	{
		//alert('CPF inválido!!');
		return false;
	}

	d1 *= 2;

	for (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)
	{
		//alert('CPF inválido!!');
		return false;
	}

	return true;
}

function CheckDate(pObj) {
  var expReg = /^((0[1-9]|[12]\d)\/(0[1-9]|1[0-2])|30\/(0[13-9]|1[0-2])|31\/(0[13578]|1[02]))\/(19|20)?\d{2}$/;
  var aRet = true;
  if ((pObj) && (pObj.value.match(expReg)) && (pObj.value != '')) {
    var dia = pObj.value.substring(0,2);
    var mes = pObj.value.substring(3,5);
    var ano = pObj.value.substring(6,10);
    if ((mes == 4 || mes == 6 || mes == 9 || mes == 11 ) && dia > 30)
      aRet = false;
    else
      if ((ano % 4) != 0 && mes == 2 && dia > 28)
        aRet = false;
      else
        if ((ano%4) == 0 && mes == 2 && dia > 29)
          aRet = false;
  }  else
    aRet = false;  
  return aRet;
}

/*
	A variável txtdataInformada deve ser passada no seguinte padrão dd/mm/yyyy.
*/
function CompareDate(txtInformada)
{
	var data = new Date();
	var txtDataAtual = '';
	//var txtInformada = document.cadastro.data.value;

	if (data.getDate().toString().length == 1)
		txtDataAtual += '0' + data.getDate() + '/';
	else
		txtDataAtual += data.getDate() + '/';
		
	if (data.getMonth().toString().length == 1 && data.getMonth() < 9)
		txtDataAtual += '0' + (data.getMonth() + 1) + '/';
	else
		txtDataAtual += (data.getMonth() + 1) + '/';
		
	txtDataAtual += data.getFullYear();

	//dataMaior(txtDataAtual, txtInformada);

	if(Date.parse(txtDataAtual.split("/").reverse().join("/")) > Date.parse(txtInformada.split("/").reverse().join("/")))			
		alert('Data informada é menor que data de hoje');
	else if(Date.parse(txtDataAtual.split("/").reverse().join("/")) == Date.parse(txtInformada.split("/").reverse().join("/")))
		alert('Data informada é igual que data de hoje');
	else if(Date.parse(txtDataAtual.split("/").reverse().join("/")) < Date.parse(txtInformada.split("/").reverse().join("/")))
		alert('Data informada é maior que data de hoje');
}

function MontaCSSLoading()
{	
	var myWidth = 0, myHeight = 0;
	if(typeof(window.innerWidth) == 'number')
	{
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	}
	else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
	{
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	}
	else if(document.body && (document.body.clientWidth || document.body.clientHeight))
	{
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	
	if (myWidth == "" || myWidth == "0")
		myWidth = screen.width;
	if (myHeight == "" || myHeight == "0")
		myHeight = screen.height;
	
	
	document.writeln("<style type='text/css'>");
	document.writeln(".divCesta{");
	document.writeln("position: absolute;");
	document.writeln("-moz-opacity: 0.5; opacity: .50; filter: alpha(opacity=50);");
	document.writeln("background-color:#fbfbfb;");
	document.writeln("font-size: 10px;");
	document.writeln("margin-right: 10px;");
	document.writeln("padding-top: 25%;");
	document.writeln("z-index: 9999;");
	//document.writeln("width: " + (screen.width) + "px;");
	//document.writeln("height: " + (screen.height) + "px;");
	document.writeln("width: " + (myWidth) + "px;");
	//document.writeln("height: " + (myHeight) + "px;");
	document.writeln("height: 75%;");
	document.writeln("overflow: auto;");
	document.writeln("}");
	document.writeln("</style>");
}

function Ajustar_Iframe(_Iframe, _Mais)
{
	if(document.getElementById && !(document.all))
	{
		_Iframe.height = document.body.offsetHeight + _Mais;
	}
	else if(document.all)
	{
		_Iframe.height = document.body.scrollHeight + _Mais;
	}
}
