function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

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 LmOver(obj)
{
	obj.style.backgroundColor='#99ccff';
}

function LmOut(obj)
{
	obj.style.backgroundColor='#ffffff';
}

function LmUp(path) {
  location.href = path;
}
	
function dadosCidades(valor)
{
	//verifica se o browser tem suporte a ajax
	try
	{
		ajax = new ActiveXObject("Microsoft.XMLHTTP");
	} 
	
	catch(e)
	{
		try
		{
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		}
		
		catch(ex)
		{
			try
			{
				ajax = new XMLHttpRequest();
			}
			
			catch(exc)
			{
				alert("Esse browser não tem recursos para uso do Ajax");
				ajax = null;
			}
		}
	}
	
	//se tiver suporte ajax
	if(ajax)
	{
		//deixa apenas o elemento 1 no option, os outros são excluídos
		document.getElementById("cidades").options.length = 1;
		
		idOpcao = document.getElementById("opcoes");
		
		ajax.open("POST", "inc/cidades.php", true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		
		ajax.onreadystatechange = function()
		{
			//enquanto estiver processando...emite a msg de carregando
			if(ajax.readyState == 1)
			{
				idOpcao.innerHTML = "Carregando...!";
	        }
			//após ser processado - chama função processXML que vai varrer os dados
            if(ajax.readyState == 4 )
			{
				if(ajax.responseXML)
				{
					processXML(ajax.responseXML);
				}
				else
				{
					//caso não seja um arquivo XML emite a mensagem abaixo
					idOpcao.innerHTML = "Selecione o estado";
				}
			}
		}
		
		//passa o código da seção escolhida
		var params = "estado="+valor;
		ajax.send(params);
	}
}
   
function processXML(obj)
{
	//pega a tag cidade
	var dataArray = obj.getElementsByTagName("cidade");
	
	//total de elementos contidos na tag cidade
	if(dataArray.length > 0)
	{
		//percorre o arquivo XML paara extrair os dados
		for(var i = 0 ; i < dataArray.length ; i++)
		{
			var item = dataArray[i];
			//contéudo dos campos no arquivo XML
			var codigo    =  item.getElementsByTagName("codigo")[0].firstChild.nodeValue;
			var nome =  item.getElementsByTagName("nome")[0].firstChild.nodeValue;
			
			idOpcao.innerHTML = "Selecione uma das cidades abaixo";
			
			//cria um novo option dinamicamente
			var novo = document.createElement("option");
			
			//atribui um ID a esse elemento
			novo.setAttribute("id", "opcoes");
			
			//atribui um valor
			novo.value = codigo;
			//atribui um texto
			novo.text  = nome;
			//finalmente adiciona o novo elemento
			document.getElementById("cidades").options.add(novo);
		}
	}
	else
	{
		//caso o XML volte vazio, printa a mensagem abaixo
		idOpcao.innerHTML = "Nenhuma cidade disponível";
	}
}


function mostra(nome)
{
	document.getElementById(nome).style.visibility = "visible";
}

function esconde(nome)
{
	document.getElementById(nome).style.visibility = "hidden";
}

function checarCampos(id)
{
	var campo = document.getElementById(id);
	if(campo.value.length == 0)
	{
		//alert("Campo obrigatório!");
		campo.style.backgroundColor = "#EFEFEF";
		campo.style.border = "1px solid #FF0000";
	}
	else
	{
		if(id == "telefoneResidencial" || id == "telefoneComercial")
		{
			if(campo.value.length < 9)
			{
				campo.style.backgroundColor = "#EFEFEF";
				campo.style.border = "1px solid #FF0000";
			}
			else
			{
				campo.style.backgroundColor = "#FFFFFF";
				campo.style.border = "1px solid #e0dfe3";
			}
		}
		else if(id == "dataNasc" || id == "dataEmissao")
		{
			if(!(/^((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}$/.test(campo.value)))
			{
				alert("Favor informar uma data válida.");
			}
			else
			{
				campo.style.backgroundColor = "#FFFFFF";
				campo.style.border = "1px solid #e0dfe3";
			}
		}
		else
		{
			campo.style.backgroundColor = "#FFFFFF";
			campo.style.border = "1px solid #e0dfe3";
		}
	}
}

function mascCEP(id)
{
	var campo = document.getElementById(id);

	if(campo.value.length == "5")
	{
		campo.value += "-";
	}
	
	if(campo.value.length == "9")
	{
		campo.value;
	}
}

function mascDATA(id)
{
	var campo = document.getElementById(id);

	if(campo.value.length == "2")
	{
		campo.value += "/";
	}

	if(campo.value.length == "5")
	{
		campo.value += "/";
	}
	
	if(campo.value.length == "10")
	{
		campo.value;
	}
}

function mascTEL(id)
{
	var campo = document.getElementById(id);

	if(campo.value.length == "4")
	{
		campo.value += "-";
	}
	
	if(campo.value.length == "9")
	{
		campo.value;
	}
}

function limpaCampo(id)
{
	campo = document.getElementById(id);
	
	if (campo.value == "dd/mm/aaaa" || campo.value == "hh:mm:ss")
	{
		campo.value = "";
	}
}

function checarCampos(id)
{
	var campo = document.getElementById(id);

	if(id != "dataTermino" && id != "horaTermino")
	{
		if(campo.value.length == 0)
		{
			campo.style.backgroundColor = "#EFEFEF";
			campo.style.border = "1px solid #FF0000";
		}
		else
		{
			if(id == "horaInicio" || id == "horaTermino")
			{
				if(campo.value != "")
				{
					if(campo.value.length < 8)
					{
						campo.style.backgroundColor = "#EFEFEF";
						campo.style.border = "1px solid #FF0000";
					}
					else
					{
						campo.style.backgroundColor = "#FFFFFF";
						campo.style.border = "1px solid #888888";
					}
				}
			}
			
			if(id == "dataInicio" || id == "dataTermino")
			{
				if(campo.value != "")
				{
					if(!(/^((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}$/.test(campo.value)))
					{
						campo.style.backgroundColor = "#EFEFEF";
						campo.style.border = "1px solid #FF0000";
					}
					else
					{
						campo.style.backgroundColor = "#FFFFFF";
						campo.style.border = "1px solid #888888";
					}
				}
			}
			else
			{
				campo.style.backgroundColor = "#FFFFFF";
				campo.style.border = "1px solid #888888";
			}
		}
	}
	else
	{
		if(id == "dataTermino")
		{
			if(campo.value != "")
			{
				if(!(/^((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}$/.test(campo.value)))
				{
					campo.style.backgroundColor = "#EFEFEF";
					campo.style.border = "1px solid #FF0000";
				}
				else
				{
					campo.style.backgroundColor = "#FFFFFF";
					campo.style.border = "1px solid #888888";
				}
			}
		}

		if(id == "horaTermino")
		{
			if(campo.value != "")
			{
				if(campo.value.length < 8)
				{
					campo.style.backgroundColor = "#EFEFEF";
					campo.style.border = "1px solid #FF0000";
				}
				else
				{
					campo.style.backgroundColor = "#FFFFFF";
					campo.style.border = "1px solid #888888";
				}
			}
		}
	}
}

//funcao para formatar qualquer campo.Ex.:cep,cpf,telefone,cnpj. 
function formatar(src, mask)
{
	var i = src.value.length;
	var saida = mask.substring(0,1);
	var texto = mask.substring(i);
	if (texto.substring(0,1) != saida)
	{
		src.value += texto.substring(0,1);
	}
}


function reSize()
{
	try{
	var oBody	=	ifrm.document.body;
	var oFrame	=	document.all("ifrm");
	
	oFrame.style.height = oBody.scrollHeight + (oBody.offsetHeight - oBody.clientHeight);
	oFrame.style.width = oBody.scrollWidth + (oBody.offsetWidth - oBody.clientWidth);
	}
	//An error is raised if the IFrame domain != its container's domain
	catch(e)
	{
		window.status =	'Error: ' + e.number + '; ' + e.description;
	}
}

function ShowCep()
{
	open('http://www.correios.com.br/servicos/cep/cep_default.cfm','ShowCep','location=no,resize=no, width=760,height=400,left=18,top=18,maximized=0,scrollbars=1');
}

function imprimir(secao, sub, cod)
{
	window.open('imprimir.php?secao='+secao+'&sub='+sub+'&codigo='+cod, 'imprimir', 'scrollbars=yes,toolbar=yes,scrolling=yes,location=no,directories=no,status=no,menubar=no,resizable=no,top=0,width=616,height=500,left=0');
}

function indique()
{
	window.open('indique.php', 'indique', 'scrollbars=yes,toolbar=no,scrolling=yes,location=no,directories=no,status=no,menubar=no,resizable=no,top=0,width=416,height=360,left=0');
}

function comentar(secao, sub, cod)
{
	window.open('comentar.php?secao='+secao+'&sub='+sub+'&codigo='+cod, 'comentar', 'scrollbars=yes,toolbar=no,scrolling=yes,location=no,directories=no,status=no,menubar=no,resizable=no,top=0,width=416,height=360,left=0');
}



//função para checar busca
function checar_frmBusca(frmBusca)
{
	if(document.getElementById("busca").value == "")
	{
		alert("Por favor preencha o campo com a palavra desejada.");
		document.getElementById("busca").focus();
		return (false);
	}

	if(document.getElementById("busca").value.length < 3)
	{
		alert("A palavra deve conter mais de 2 caracteres.");
		document.getElementById("busca").focus();
		return (false);
	}
}


//máscara para campo moeda
function MascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, 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;
    key = String.fromCharCode(whichCode); // Valor para o código da Chave
    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
    aux = '';
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) objTextBox.value = '';
    if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
    if (len == 2) objTextBox.value = '0'+ SeparadorDecimal + aux;
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--)
		{
			if (j == 3)
			{
				aux2 += SeparadorMilesimo;
				j = 0;
			}
            aux2 += aux.charAt(i);
            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
        objTextBox.value += aux2.charAt(i);
        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
    }
    return false;
}

//função para só digitar números
function sem_letras(){
    if ((window.event.keyCode < 48) | (window.event.keyCode > 57)){
        window.event.keyCode = 0
    }
}

dia = new Date();
ano = dia.getYear();
if(ano < 2000) // correçao para browsers que retornam 100 no ano 2000
{
	ano = 1900 + dia.getYear();
}

mes = new Array("Janeiro","Fevereiro","Março","Abril","Maio","Junho","Julho","Agosto","Setembro","Outubro","Novembro","Dezembro")

function escreveData()
{
	document.writeln(dia.getDate() + " de " +  mes[dia.getMonth()] + " de " + ano);
}

function submeterForm(form, destino)
{
	var formulario = document.getElementById(form);
	formulario.action = destino;
	formulario.submit();
}
