function abre_janela(div_id, nao_fecha)
{
	// Zera conteúdo da div
	if (document.getElementById('flag_janela_nome').value != div_id && nao_fecha != true)
	{
		if (document.getElementById(document.getElementById('flag_janela_nome').value+'_conteudo'))
			document.getElementById(document.getElementById('flag_janela_nome').value+'_conteudo').innerHTML = '';
	}

	temp = document.getElementById('flag_janela').value;
	if(temp == 'false'){
		document.getElementById('flag_janela').value = 'true';
	}else{
		//se ele nao quer que feche a anterior
		if(nao_fecha != true)
		{
			fecha_janela(document.getElementById('flag_janela_nome').value);
		}
	}
	
	//se nao vai fechar a anterior, nao seta a atual como atual
	if(nao_fecha != true)
	{
		document.getElementById('flag_janela_nome').value = div_id;
	}
	// Arrumar posição
	//regula_div(div_id);
	
	// Pegando a DIV
	var div_total = document.getElementById(div_id);
	var iframe_total = document.getElementById(div_id+'_iframe');
	//var div_total_fundo = document.getElementById(div_id + "_fundo");
	
	// Seta a altura e largura do iframe da janela
	iframe_total.style.height = div_total.clientHeight;
	iframe_total.style.width = div_total.clientWidth;
		
	// Tornar visível
	div_total.style.visibility = 'visible';
	iframe_total.style.visibility = 'visible';
	//div_total_fundo.style.visibility = 'visible';
}

function fecha_janela(div_id)
{
	
	// Pegando a DIV
	var div_total = document.getElementById(div_id);
	var iframe_total = document.getElementById(div_id+'_iframe');
	//var div_total_fundo = document.getElementById(div_id + "_fundo");
	
	// Tornar visível
	div_total.style.visibility = 'hidden';
	iframe_total.style.visibility = 'hidden';
	//div_total_fundo.style.visibility = 'hidden';

	// Zera conteúdo da div
	if (document.getElementById('flag_janela_nome').value != div_id)
	{
		document.getElementById(div_id+'_conteudo').innerHTML = '';
	}
}

// Função que coloca o iframe atrás do div do menu
// Para utilizar ela precisa ter criado um iframe no corpo do html
// Quando for para esconder o iframe passar id_menu vazio
function menu_iframe(id_menu, esconder)
{
	var iframe_menu = document.getElementById('iframe_menu');

if (!esconder)
	{
		var div_menu = document.getElementById(id_menu);
		
		iframe_menu.style.display = '';
		iframe_menu.style.height = div_menu.clientHeight;
		iframe_menu.style.width = div_menu.clientWidth;
		
		iframe_menu.style.left = getPosicaoElemento(id_menu).left;
		iframe_menu.style.top = getPosicaoElemento(id_menu).top;
	}
	else
	{
		iframe_menu.style.display = 'none';
	}
	
	return true;
}

// Função que coloca o iframe atrás do div do submenu
// Para utilizar ela precisa ter criado um iframe no corpo do html
// Quando for para esconder o iframe passar id_submenu vazio
function submenu_iframe(id_submenu, esconder, iframe)
{	
	//var iframe_submenu = document.getElementById('iframe_submenu');
	var iframe_submenu = iframe;
	
	if (!esconder)
	{
		var div_submenu = document.getElementById(id_submenu);
		
		iframe_submenu.style.display = '';
		iframe_submenu.style.height = div_submenu.clientHeight;
		iframe_submenu.style.width = div_submenu.clientWidth;
		
		iframe_submenu.style.left = getPosicaoElemento(id_submenu).left;
		iframe_submenu.style.top = getPosicaoElemento(id_submenu).top;
	}
	else
	{
		iframe_submenu.style.display = 'none';
	}
	
	return true;
}

// Função que retorna o top e o left do elemento
function getPosicaoElemento(elemID)
{
    var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 && 
        typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return {left:offsetLeft, top:offsetTop};
}

function arredonda(numero, numero_casas_decimais)
{
	var temp_numero = numero.toString();
	var temp = temp_numero.split('.');
	casas_decimais = temp[1];
	
	// Se não tem casas decimais, criar
	if(casas_decimais == null)
	{
		casas_decimais = "0";
	}
	
	// Colocando no formato
	while(casas_decimais.length < numero_casas_decimais)
	{
		casas_decimais = casas_decimais + "0";
	}
	
	// Pegando casas úteis
	casas_uteis = casas_decimais.substr(0, numero_casas_decimais);

	// Pegando o resto
	resto = casas_decimais.substr(numero_casas_decimais, 1);
	
	// Arredondando caso necessário
	if(resto >= 5)
	{
		casas_uteis = parseInt(casas_uteis)+1;
	}
	
	// Montando numero
	temp[1] = casas_uteis;
	numero = temp.join(".");
	
	return(numero);
}

function janela_debug()
{
	if(document.getElementById('execute').height == 0)
	{
		document.getElementById('execute').height = "90%";
		document.getElementById('execute').width = "90%";
	}
	else
	{
		document.getElementById('execute').height = 0;
		document.getElementById('execute').width = 0;		
	}
}


function muda_img_menu(numero)
{
	var menu_atual = document.getElementById('menu_atual').value;
	
	document.getElementById('menu_' + menu_atual).src = '../images/ordem_servico_menu_0'+ menu_atual +'.jpg';
	
	document.getElementById('menu_atual').value = numero;
	
	document.getElementById('menu_'+numero).src = '../images/ordem_servico_menu_at_0'+ numero +'.jpg';
	
	return true;
}

function formata(obj, mascara)
{
 // Tecla
 if (window.event)
		tecla = window.event.keyCode;
	else
		tecla = obj.which;
 
 // Se não estiver apagando
	if(tecla != 8)
	{
		// Divide a string
		temp = mascara.split('');
		if(temp[obj.value.length+1])
		{
			if(temp[obj.value.length+1] != '%')
			{
				setTimeout('document.getElementById(\''+obj.id+'\').value += \''+temp[obj.value.length+1]+'\';', 1);
			}
		} else {
			setTimeout('document.getElementById(\''+obj.id+'\').value = document.getElementById(\''+obj.id+'\').value.substr(0, '+mascara.length+');', 1);
		}
	}
}

function isCPFCNPJ(campo,pType){
   if( isEmpty( campo ) ){return true;}

   var campo_filtrado = "", valor_1 = " ", valor_2 = " ", ch = "";
   var valido = false;
        
   for (i = 0; i < campo.length; i++){
      ch = campo.substring(i, i + 1);
      if (ch >= "0" && ch <= "9"){
         campo_filtrado = campo_filtrado.toString() + ch.toString()
         valor_1 = valor_2;
         valor_2 = ch;
      }
      if ((valor_1 != " ") && (!valido)) valido = !(valor_1 == valor_2);
   }
   if (!valido) campo_filtrado = "12345678912";

   if (campo_filtrado.length < 11){
      for (i = 1; i <= (11 - campo_filtrado.length); i++){campo_filtrado = "0" + campo_filtrado;}
   }

 if(pType <= 1){
  if ( ( campo_filtrado.substring(9,11) == checkCPF( campo_filtrado.substring(0,9) ) ) && ( campo_filtrado.substring(11,12)=="") ){return true;}
 }

 if((pType == 2) || (pType == 0)){
  if (campo_filtrado.length >= 14){
   if ( campo_filtrado.substring(12,14) == checkCNPJ( campo_filtrado.substring(0,12) ) ){ return true;}
  }
 }
 
 return false;
}

function checkCNPJ(vCNPJ){
   var mControle = "";
   var aTabCNPJ = new Array(5,4,3,2,9,8,7,6,5,4,3,2);
   for (i = 1 ; i <= 2 ; i++){
      mSoma = 0;
      for (j = 0 ; j < vCNPJ.length ; j++)
         mSoma = mSoma + (vCNPJ.substring(j,j+1) * aTabCNPJ[j]);
      if (i == 2 ) mSoma = mSoma + ( 2 * mDigito );
      mDigito = ( mSoma * 10 ) % 11;
      if (mDigito == 10 ) mDigito = 0;
      mControle1 = mControle ;
      mControle = mDigito;
      aTabCNPJ = new Array(6,5,4,3,2,9,8,7,6,5,4,3);
   }
   return( (mControle1 * 10) + mControle );
}

function checkCPF(vCPF){
   var mControle = ""
   var mContIni = 2, mContFim = 10, mDigito = 0;
   for (j = 1 ; j <= 2 ; j++){
      mSoma = 0;
      for (i = mContIni ; i <= mContFim ; i++)
         mSoma = mSoma + (vCPF.substring((i-j-1),(i-j)) * (mContFim + 1 + j - i));
      if (j == 2 ) mSoma = mSoma + ( 2 * mDigito );
      mDigito = ( mSoma * 10 ) % 11;
      if (mDigito == 10) mDigito = 0;
      mControle1 = mControle;
      mControle = mDigito;
      mContIni = 3;
      mContFim = 11;
   }
   return( (mControle1 * 10) + mControle );
}


function ajax (url, sincrono, retorno)
{
	assincrono = (sincrono == true)? false : true;
	
	var xmlhttp = false;
	
	if (window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
	}
	else
	{
		if (window.ActiveXObject)
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	
	if (xmlhttp)
	{  
		xmlhttp.open('GET', url, assincrono);
	
		xmlhttp.send(null);
	
		if(assincrono)
		{
			xmlhttp.onreadystatechange=function()
			{
				if (xmlhttp.readyState==4)
				{
					if (retorno == true)
						retorno = xmlhttp.responseText;
					else
					{	alert(xmlhttp.responseText);	
						eval(xmlhttp.responseText);
					}
				}
			}
		}
		else
		{
			if (retorno == true)
				retorno = xmlhttp.responseText;
			else
			{
				//alert(xmlhttp.responseText);
				eval(xmlhttp.responseText);
			}
		}
	}
	
	if(retorno)
		return retorno;
	else
		return false;
}

//juridico //fisico
function troca(value) 
{
	if (value == 1)
	{
		parent.document.getElementById('juridico').style.display = '';
		parent.document.getElementById('juridico1').style.display = '';
		parent.document.getElementById('fisico').style.display = 'none';
	}
	else
	{
		parent.document.getElementById('juridico').style.display = 'none';
		parent.document.getElementById('juridico1').style.display = 'none';
		parent.document.getElementById('fisico').style.display = '';
	}
}

function prisma(numero_atual)
{
	if(numero_atual == null || parseInt(numero_atual) == 15)
	{
		numero_atual = 0;
	}
	
	var numero = parseInt(numero_atual) + 1;
	parent.document.getElementById('numero_prisma').innerHTML = numero;
	parent.document.getElementById('ordem_servico_prisma').value = numero;
	
	return true;
}

//adiciona mascara de cnpj
function mascaraCNPJ(cnpj,event)
{
 if(mascaraInteiro(event) == false)
  event.returnValue = false;
 return formataCampo(cnpj, '00.000.000/0000-00', event);
}

//adiciona mascara de data
function mascaraData(data, event)
{
 if(mascaraInteiro(event) == false)
  event.returnValue = false;
 return formataCampo(data, '00/00/0000', event);
}

//valida data
function validaData(data){
	if(data.value != '')
	{
		exp = /\d{2}\/\d{2}\/\d{4}/
		if(!exp.test(data.value))
		{
			alert('Data Inválida!');
			data.select();
			return false;
		}
	}
	return true;
}

//adiciona mascara ao CPF
function mascaraCPF(cpf, event)
{
 if(mascaraInteiro(event) == false)
  event.returnValue = false;
 return formataCampo(cpf, '000.000.000-00', event);
}

//adiciona mascara ao CEP
function mascaraCEP(cep, event)
{
 if(mascaraInteiro(event) == false)
  event.returnValue = false;
 return formataCampo(cep, '00.000-000', event);
}

//valida numero inteiro com mascara
function mascaraInteiro(event)
{
 if (event.keyCode < 48 || event.keyCode > 57)
 {
  event.returnValue = false;
  return false;
 }
 return true;
}

//formata de forma generica os campos
function formataCampo(campo, Mascara, evento) 
{
 var boleanoMascara;

 var Digitato = evento.keyCode;
 exp = /\-|\.|\/|\(|\)| /g
 campoSoNumeros = campo.value.toString().replace( exp, "" );

 var posicaoCampo = 0;
 var NovoValorCampo="";
 var TamanhoMascara = campoSoNumeros.length;;

 if (Digitato != 8) // backspace
 { 
  for(i=0; i<= TamanhoMascara; i++) 
  {
   boleanoMascara = ((Mascara.charAt(i) == "-") || (Mascara.charAt(i) == ".") || (Mascara.charAt(i) == "/"))
   boleanoMascara = boleanoMascara || ((Mascara.charAt(i) == "(") || (Mascara.charAt(i) == ")") || (Mascara.charAt(i) == " "))
   if (boleanoMascara) 
   {
    NovoValorCampo += Mascara.charAt(i);
    TamanhoMascara++;
   } 
   else 
   {
    NovoValorCampo += campoSoNumeros.charAt(posicaoCampo);
    posicaoCampo++;
   }
  }
  campo.value = NovoValorCampo;
  return true;
 } 
 else 
  return true;
}

function Limpar(valor, validos) 
{
 // retira caracteres invalidos da string
 var result = "";
 var aux;
 for (var i=0; i < valor.length; i++) {
  aux = validos.indexOf(valor.substring(i, i+1));
  if (aux>=0) {
   result += valor.substring(i, i+1);
  }
 }
 return result;
}

function keypress(e) 
{
 var x = '';
 if (document.all)
 {
  var evnt = window.event;
  x = evnt.keyCode;
 }
 else
 {
  x = e.keyCode;
 }   
 return x;
}

//Formata número tipo moeda usando o evento onKeyUp
function formataMoeda(campo, event, negativo) 
{
 var tecla = keypress(event);
 
 if(negativo == true)
 {
  aceita = "-0123456789";
 }
 else
 {
  aceita = "0123456789";
 }

 vr  = Limpar(campo.value, aceita);
 tam = vr.length;
 dec = 2;

 if (tam < 18 && tecla != 8)
 { 
  tam = vr.length; 
 }

 campo.value = vr ;
 
 if ( (tam <= dec) && (tam > 0) )
 {
  campo.value = "," + vr.substr( tam - dec, tam ) ; 
 }
 if ( (tam > dec) && (tam <= 5) )
 {
  campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ; 
 }
 if ( (tam >= 6) && (tam <= 8) )
 {
  campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; 
 }
 if ( (tam >= 9) && (tam <= 11) )
 {
  campo.value = vr.substr( 0, tam - 8 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; 
 }
 if ( (tam >= 12) && (tam <= 14) )
 {
  campo.value = vr.substr( 0, tam - 11 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; 
 }
 if ( (tam >= 15) && (tam <= 17) )
 {
  campo.value = vr.substr( 0, tam - 14 ) + "." + vr.substr( tam - 14, 3 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - 2, tam ) ;
 }
}

//Formata número tipo moeda
function formatMoney(moeda)
{
 if (moeda != '')
 {
  moeda = moeda.toFixed(2);
  
  vr  = Limpar(moeda,"0123456789");
  tam = vr.length;
  dec = 2;
 
  result = vr ;
  
  if ( (tam <= dec) && (tam > 0) )
  {
   result = "," + vr.substr( tam - dec, tam ) ; 
  }
  if ( (tam > dec) && (tam <= 5) )
  {
   result = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ; 
  }
  if ( (tam >= 6) && (tam <= 8) )
  {
   result = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; 
  }
  if ( (tam >= 9) && (tam <= 11) )
  {
   result = vr.substr( 0, tam - 8 ) + "." + vr.subprodustr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; 
  }
  if ( (tam >= 12) && (tam <= 14) )
  {
   result = vr.substr( 0, tam - 11 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; 
  }
  if ( (tam >= 15) && (tam <= 17) )
  {
   result = vr.substr( 0, tam - 14 ) + "." + vr.substr( tam - 14, 3 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - 2, tam ) ;
  }
 }
 else
  result = '0,00';
 
 return result;
}

<!-- INSERIDOS 18/10/2007 -->
function isCPFCNPJ(campo,pType){
   if( isEmpty( campo ) ){return true;}

   var campo_filtrado = "", valor_1 = " ", valor_2 = " ", ch = "";
   var valido = false;
        
   for (i = 0; i < campo.length; i++){
      ch = campo.substring(i, i + 1);
      if (ch >= "0" && ch <= "9"){
         campo_filtrado = campo_filtrado.toString() + ch.toString()
         valor_1 = valor_2;
         valor_2 = ch;
      }
      if ((valor_1 != " ") && (!valido)) valido = !(valor_1 == valor_2);
   }
   if (!valido) campo_filtrado = "12345678912";

   if (campo_filtrado.length < 11){
      for (i = 1; i <= (11 - campo_filtrado.length); i++){campo_filtrado = "0" + campo_filtrado;}
   }

	if(pType <= 1){
		if ( ( campo_filtrado.substring(9,11) == checkCPF( campo_filtrado.substring(0,9) ) ) && ( campo_filtrado.substring(11,12)=="") ){return true;}
	}

	if((pType == 2) || (pType == 0)){
		if (campo_filtrado.length >= 14){
			if ( campo_filtrado.substring(12,14) == checkCNPJ( campo_filtrado.substring(0,12) ) ){ return true;}
		}
	}
	
	return false;
}

function checkCNPJ(vCNPJ){
   var mControle = "";
   var aTabCNPJ = new Array(5,4,3,2,9,8,7,6,5,4,3,2);
   for (i = 1 ; i <= 2 ; i++){
      mSoma = 0;
      for (j = 0 ; j < vCNPJ.length ; j++)
         mSoma = mSoma + (vCNPJ.substring(j,j+1) * aTabCNPJ[j]);
      if (i == 2 ) mSoma = mSoma + ( 2 * mDigito );
      mDigito = ( mSoma * 10 ) % 11;
      if (mDigito == 10 ) mDigito = 0;
      mControle1 = mControle ;
      mControle = mDigito;
      aTabCNPJ = new Array(6,5,4,3,2,9,8,7,6,5,4,3);
   }
   return( (mControle1 * 10) + mControle );
}

function checkCPF(vCPF){
   var mControle = ""
   var mContIni = 2, mContFim = 10, mDigito = 0;
   for (j = 1 ; j <= 2 ; j++){
      mSoma = 0;
      for (i = mContIni ; i <= mContFim ; i++)
         mSoma = mSoma + (vCPF.substring((i-j-1),(i-j)) * (mContFim + 1 + j - i));
      if (j == 2 ) mSoma = mSoma + ( 2 * mDigito );
      mDigito = ( mSoma * 10 ) % 11;
      if (mDigito == 10) mDigito = 0;
      mControle1 = mControle;
      mControle = mDigito;
      mContIni = 3;
      mContFim = 11;
   }
   return( (mControle1 * 10) + mControle );
}

function isEmpty(pStrText){
	var	len = pStrText.length;
	var pos;
	var vStrnewtext = "";

	for (pos=0; pos<len; pos++){
		if (pStrText.substring(pos, (pos+1)) != " "){
			vStrnewtext = vStrnewtext + pStrText.substring(pos, (pos+1));
		}
	}

	if (vStrnewtext.length > 0)
		return false;
	else
		return true;
}


// MASCARAS VERIFICAÇÕES *************************************************************************
//formata de forma generica os campos
function formataCampo(campo, Mascara, tecla) 
{
	var boleanoMascara;
	var Digitato = tecla;
	
	exp = /\-|\.|\/|\(|\)| /g
	
	campoSoNumeros = campo.value.toString().replace( exp, "" );
	
	var posicaoCampo = 0;
	var NovoValorCampo="";
	var TamanhoMascara = campoSoNumeros.length;;
	
	if (Digitato != 8) // backspace
	{ 
		for(i=0; i<= TamanhoMascara; i++) 
		{
			boleanoMascara = ((Mascara.charAt(i) == "-") || (Mascara.charAt(i) == ".") || (Mascara.charAt(i) == "/"))
			boleanoMascara = boleanoMascara || ((Mascara.charAt(i) == "(") || (Mascara.charAt(i) == ")") || (Mascara.charAt(i) == " "))
			if (boleanoMascara) 
			{
				NovoValorCampo += Mascara.charAt(i);
				TamanhoMascara++;
			} 
			else 
			{
				NovoValorCampo += campoSoNumeros.charAt(posicaoCampo);
				posicaoCampo++;
			}
		}
		campo.value = NovoValorCampo;
		return true;
	} 
	else 
		return true;
}

// onkeydown="return dateFormat(this, event);"
function dateFormat(campo, e)
{
	var key, keychar, reg;
	
	if(window.event)  key = e.keyCode;
    else if(e.which)  key = e.which;
    else return true;

    if ( key == 8 || key == 9 ) return true;

    if ( (key >= 48 && key <= 57) || (key >= 96 && key <= 105) || (key == 193) )
    	return formataCampo(campo, '%%/%%/%%%%', e);
    
    keychar = String.fromCharCode(key);
    reg = /[^\d]/;

    return !reg.test(keychar);
}


