//Variáveis de esco
documentall = document.all;

//Funcao Semelhante ao document.getElementById();
function $(){var elements=new Array(); var x=arguments.length; for (var i=0; i < x; i++){var element=arguments[i]; if (typeof element == 'string'){element=document.getElementById(element);} if (arguments.length == 1){return element;} elements.push(element);}}
//Funcao Semelhante ao document.getElementById().value;
function $F(){var elements=new Array(); var x=arguments.length; for (var i=0; i < x; i++){var element=arguments[i]; if (typeof element == 'string'){element=document.getElementById(element).value;} if (arguments.length == 1){return element;} elements.push(element);}}
//Redireciona para alguma página
function Redireciona(url) {window.location=url;}
//Executa um Bloco Ajax
/*
	var AJAX = new Ajax();
	AJAX.connect('teste.php','POST','x=20&y=41','teste');
	arquivo; metodo(POST | GET); parametros GET e POST;  funcao retorno;
*/
function Ajax() { var xmlhttp, bComplete = false; try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { try { xmlhttp = new XMLHttpRequest(); } catch (e) { xmlhttp = false; alert('** Atenção: ** - Seu Navegador não suporta Ajax.') }}} if (!xmlhttp) return null; this.connect = function(sURL, sMethod, sVars, fnDone) { sVars = "RndSeed"+Math.ceil(Math.random()*1999)+"="+Math.ceil(Math.random(8000)*10000000)+"&"+sVars; if (!xmlhttp) return false; bComplete = false; sMethod = sMethod.toUpperCase(); try { if (sMethod == "GET") { xmlhttp.open(sMethod, sURL+"?"+sVars, true); sVars = ""; } else { xmlhttp.open(sMethod, sURL, true); xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1."); xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); } xmlhttp.onreadystatechange = function(){ if (xmlhttp.readyState == 4 && !bComplete) { if(fnDone != null) { bComplete = true; var v1 = xmlhttp.responseText; eval(fnDone+"(xmlhttp.responseText)"); } }}; xmlhttp.send(sVars); } catch(z) { return false; } return true; }; return this; }
//Mostra algum Objeto Escondido
function MostraObjeto(){$(arguments[0]).style.display='block';}
//Esconde algum Objeto
function EscondeObjeto(){$(arguments[0]).style.display='none';}
//Pega os tamanhos das páginas, area Interna e Resolucao.
function getPageSize() { var xScroll, yScroll; if (window.innerHeight && window.scrollMaxY) { xScroll = document.body.scrollWidth; yScroll = window.innerHeight + window.scrollMaxY; } else if (document.body.scrollHeight > document.body.offsetHeight){ xScroll = document.body.scrollWidth; yScroll = document.body.scrollHeight; } else { xScroll = document.body.offsetWidth; yScroll = document.body.offsetHeight; } var windowWidth, windowHeight; if (self.innerHeight) { windowWidth = self.innerWidth; windowHeight = self.innerHeight; } else if (document.documentElement && document.documentElement.clientHeight) { windowWidth = document.documentElement.clientWidth; windowHeight = document.documentElement.clientHeight; } else if (document.body) { windowWidth = document.body.clientWidth; windowHeight = document.body.clientHeight; } if(yScroll < windowHeight){ pageHeight = windowHeight; } else { pageHeight = yScroll; } if(xScroll < windowWidth){ pageWidth = windowWidth; } else { pageWidth = xScroll; } arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight); return arrayPageSize; }
//Gera na Tela um ShowMessage estilo lightbox
function StartShowMessage(TipoMensagem,Mensagem) { var PageSize = getPageSize();var flg_already_exists = false;if($('SHOWMESSAGE') == undefined){		var a = document.createElement("div"); } else { var a = $('SHOWMESSAGE'); flg_already_exists = true; } var HTML = "<div style=\"position:absolute;top:0px;left:0px;color:white;background-image:url('overlay.png');width:"+PageSize[2]+'px'+";height:"+PageSize[3]+"px;filter:alpha(opacity:90)\"><table width='100%' height='100%' border='0' style='z-index:200'> <tr> <td align='center'><table width='450' border='0' class='destaque_aviso'> <tr> <th align='center' style='background-color:#a1a1a1;border:1px solid #666666'>"+TipoMensagem+"</th> </tr> <tr> <td align='center'>"+unescape(Mensagem)+"</td> </tr> <tr> <td align='center'><input type='button' value='Fechar' style='border:1px solid #666666;background-color:#f2f2f2;font-weight:bold;font-size:10px;' onclick=\"StartShowMessage__hide()\"></td> </tr> </table> </td> </tr> </table></div>"; a.innerHTML = HTML; a.id = 'SHOWMESSAGE'; if(!flg_already_exists) { document.body.appendChild(a); } else { MostraObjeto('SHOWMESSAGE'); } }
//Função responsável por Esconder o Showmessage
function StartShowMessage__hide() { $('SHOWMESSAGE').innerHTML = ''; EscondeObjeto('SHOWMESSAGE') }
//Função responsável por Submeter um formulário
function FormSubmit() { $(arguments[0]).submit(); }

// Funções nao Base Prototype
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){

var val2 = '';
var strCheck = '0123456789';
var len = valor.length;
	if (len== 0){
		return 0.00;
	}

	if (currency ==true){	
		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{
			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;

if (whichCode == 8 && !documentall) {	

	if (event.preventDefault){
			event.preventDefault();
		}else{
			event.returnValue = false;
	}
	var valor = obj.value;
	var x = valor.substring(0,valor.length-1);
	obj.value= demaskvalue(x,true).formatCurrency();
	return false;
}

FormataReais(obj,'.',',',event);
} 


function backspace(obj,event){
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 =""; 
	obj.value += y;
	
	if (event.preventDefault){ 
			event.preventDefault();
		}else{ 
			event.returnValue = false;
	}
	return false;

	}
}

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 == 0 || whichCode == 9 || whichCode == 13 || whichCode == 16 || whichCode == 17 || whichCode == 27 || whichCode == 34 || whichCode == 35 || whichCode == 36) 
{
	return true;
}

if (e.preventDefault){
		e.preventDefault()
	}else{
		e.returnValue = false
}

var key = String.fromCharCode(whichCode);
if (strCheck.indexOf(key) == -1) return false;

fld.value += key;

var len = fld.value.length;
var bodeaux = demaskvalue(fld.value,true).formatCurrency();
fld.value=bodeaux;

  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;
}

function abre_popup_cobertura(url_cobertura)
{
	var altura = 520;
	var largura = 780;

	//pega a resolução do visitante
	var w = screen.width;
	var h = screen.height;

	//divide a resolução por 2, obtendo o centro do monitor
	var meio_w = w/2;
	var meio_h = h/2;

	//diminui o valor da metade da resolução pelo tamanho da janela, fazendo com q ela fique centralizada
	var altura2 = altura/2;
	var largura2 = largura/2;
	var meio1 = meio_h-altura2;
	var meio2 = meio_w-largura2;

	window.open(url_cobertura,'public','width='+largura+',height='+altura+',top='+meio1+',left='+meio2+',toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=0');	
}