function popup(mypage, myname, w, h, scroll, st) {
    var winl = (screen.width - w) / 2;
    var wint = (screen.height - h) / 2;
    winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',status='+st
    win = window.open(mypage, myname, winprops)
    if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
    var theCells = null;
    var marked_row = new Array;

    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3

    // 4. Defines the new color
    // 4.1 Current color is the default one
    if (currentColor == ''
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor              = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
             && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
        if (theAction == 'out') {
            newColor              = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor              = (thePointerColor != '')
                                  ? thePointerColor
                                  : theDefaultColor;
            marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
                                  ? true
                                  : null;
        }
    } // end 4

    // 5. Sets the new color...
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
} // end of the 'setPointer()' function

function RestringeTeclas()
{
    var tecla=window.event.keyCode;
    if (tecla==8)
    {
        event.returnValue=false;
    }
}

function PeriodoEmDias(d1,d2) {
  d1 = new Date(d1.substring(6,10),d1.substring(3,5),d1.substring(0,2));
  d2 = new Date(d2.substring(6,10),d2.substring(3,5),d2.substring(0,2));
  dias = (d2.getTime() - d1.getTime());
  dias /= 86400000;
  return parseInt(dias);
}

function evtPlaca(campo,evt)
{
  tam = campo.value.length;

  var tecla = evt.keyCode ? evt.keyCode :
              evt.charCode ? evt.charCode :
		      evt.which ? evt.which : void 0;

	//alert(tam + ' - ' + tecla);

    if (tam < 3){
      if((tecla >=65 && tecla <= 90) || (tecla >= 97 && tecla <= 122))
        return true;
      else
        {
          if (tecla == 8 || tecla == 9)
            return true;
          else
            return false;
        }
  }else{
      if((tecla > 47 && tecla < 58))
        return true;
      else
        {
          if (tecla == 8 || tecla == 9)
            return true;
          else
            return false;
        }
  }
}

function pega_cidades(cduf,selectedcdmunicipio) {
    document.form.cdmunicipio.disabled = true;
	urlest = "/df/cidade-do-automovel/00/lib/filtro_municipios.php?cduf="+cduf+"&selectedcdmunicipio="+selectedcdmunicipio;
	if_hidden.location.href=urlest;
}

function pega_marca(cdcategoria,selectedcdfabricante)
{
  document.form.cdfabricante.disabled = true;
	urlest = "/gerenciador/lib/filtro_marca.php?cdcategoria="+cdcategoria+"&selectedcdfabricante="+selectedcdfabricante;
	if_hidden.location.href=urlest;
}

function pega_marca_venda(cdcategoria, selectedcdfabricante, cdfabricante)
{
  //eval ('document.form.'+cdfabricante+'.disabled = true');
	urlest = "/gerenciador/lib/filtro_marca.php?cdcategoria="+cdcategoria+"&selectedcdfabricante="+selectedcdfabricante+"&cdfabricante="+cdfabricante;
	if_hidden.location.href=urlest;
}

function pega_marca_venda_novo(cdcategoria, selectedcdfabricante, cdfabricante)
{
  //eval ('document.form.'+cdfabricante+'.disabled = true');
	urlest = "/gerenciador/lib/filtro_marca_novos.php?cdcategoria="+cdcategoria+"&selectedcdfabricante="+selectedcdfabricante+"&cdfabricante="+cdfabricante;
	if_hidden.location.href=urlest;
}

function validaData(Data)
 {
  var dma = -1;
  var data = Array(3);
  var ch = Data.charAt(0);
  if (Data.length != 10){
   return false;
  }
  for(i=0; i < Data.length && (( ch >= '0' && ch <= '9' ) || ( ch == '/' && i != 0 ) ); ){
   data[++dma] = '';
   if(ch!='/' && i != 0) return false;
   if(i != 0 ) ch = Data.charAt(++i);
   if(ch=='0') ch = Data.charAt(++i);
   while( ch >= '0' && ch <= '9' ){
    data[dma] += ch;
    ch = Data.charAt(++i);
   }
  }
  if(ch!='') return false;
  if(data[0] == '' || isNaN(data[0]) || parseInt(data[0]) < 1) return false;
  if(data[1] == '' || isNaN(data[1]) || parseInt(data[1]) < 1 || parseInt(data[1]) > 12) return false;
  if(data[2] == '' || isNaN(data[2]) || ((parseInt(data[2]) < 0 || parseInt(data[2]) > 99 ) && (parseInt(data[2]) < 1900 || parseInt(data[2]) > 9999))) return false;
  if(data[2] < 50) data[2] = parseInt(data[2]) + 2000;
  else if(data[2] < 100) data[2] = parseInt(data[2]) + 1900;
  switch(parseInt(data[1])){
   case 2: { if(((parseInt(data[2])%4!=0 || (parseInt(data[2])%100==0 && parseInt(data[2])%400!=0)) && parseInt(data[0]) > 28) || parseInt(data[0]) > 29 ) return false; break; }
   case 4: case 6: case 9: case 11: { if(parseInt(data[0]) > 30) return false; break;}
   default: { if(parseInt(data[0]) > 31) return false;}
  }
  return true;
}
//----------------------Põe o cursor no final
function setSelectionRange(input, selectionStart, selectionEnd) {
  if (input.setSelectionRange) {
    input.focus();
    input.setSelectionRange(selectionStart, selectionEnd);
  }
  else if (input.createTextRange) {
    var range = input.createTextRange();
    range.collapse(true);
    range.moveEnd('character', selectionEnd);
    range.moveStart('character', selectionStart);
    range.select();
  }
}
function setCaretToEnd (input) {
  setSelectionRange(input, input.value.length, input.value.length);
}
//----------------------
function janela(www,h,w)
{
var height = window.screen.height - 160;
var width = window.screen.width;
var top = (height- h)/2;
var left = (width - w)/2;
window.open(www,"nova","top=" + top + ",left="+ left +",width="+w+",heigth="+h+",toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,scrolling=yes,resizable=yes")
}
//--------


function window_select(url) {
      var height = window.screen.height - 160;
      var width = window.screen.width;
      var top = (height- 450)/2;
      var left = (width - 450)/2;
      flwindow_select = open(url, "MANAGER", "left="+ left +",top="+ top +",height=600,width=640,scrollbars=yes");
}

function window_select_seg(url) {
      var height = window.screen.height - 160;
      var width = window.screen.width;
      var top = (height- 400)/2;
      var left = (width - 400)/2;
      flwindow_select_seg = window.open(url, "", "left="+ left +",top="+ top +",height=600,width=530,scrollbars=yes");
}


/*function pega_veiculovalortabela(cdveiculo) {
	urlest = "/gerenciador/lib/pega_veiculovalortabela.php?cdveiculo="+cdveiculo;
	ifapphidden.location.href=urlest;
}*/

function cepmunicipio(cdmunicipio) {
    document.form.cep.disabled = true;
	urlest = "/gerenciador/lib/cepmunicipio.php?cdmunicipio="+cdmunicipio;
	if_hidden.location.href=urlest;
}

function FormataValor(campo,tammax,teclapres) {
	var tecla = teclapres.keyCode;
	vr = document.form[campo].value;
	ori = vr;
    if (vr.indexOf("0,00")==0)
      vr = vr.replace( "0,00", "" );
    if (vr.indexOf("0,0")==0)
      vr = vr.replace( "0,0", "" );
    if (vr.indexOf("0,")==0)
      vr = vr.replace( "0,", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	tam = vr.length;

	if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }
	if (tecla == 8 ){	tam = tam - 1 ; }


	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
	    if (tam == 0){
          document.form[campo].value = '';
        }
		if ( tam == 1 ){
	 		document.form[campo].value = '0,0' + vr ; }
		if ( tam == 2 ){
	 		document.form[campo].value = '0,' + vr ; }
	 	if ( (tam > 2) && (tam <= 5) ){
	 		document.form[campo].value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 6) && (tam <= 8) ){
	 		document.form[campo].value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 9) && (tam <= 11) ){
	 		document.form[campo].value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 12) && (tam <= 14) ){
	 		document.form[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 - 2, tam ) ; }
	 	if ( (tam >= 15) && (tam <= 17) ){
	 		document.form[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 ) ;}
        //----
       if ((document.form[campo].value == '')||(document.form[campo].value == '0,00')){
         document.form[campo].value = '0,000';
       }
        //----

	}else{
      if ((tecla != 9) && (tecla != 37) && (tecla != 39) && (tecla != 35) && (tecla != 36))
         event.returnValue = false;
    }

	for (var ct = 0; ct < document.form.elements.length; ct++) {
		if (document.form.elements[ct].name == document.form.elements[campo].name) {
			if ( !teclapres.shiftKey && tecla == 9 && document.form.elements[ct+1] && document.form.elements[ct+1].name == "senhaConta" && document.applets['tclJava'] ){
				document.applets['tclJava'].setFocus();
			}
		}
	}
}


function FormataInteiro(campo,tammax,teclapres) {
	var tecla = teclapres.keyCode;
	vr = document.form[campo].value;
	ori = vr;
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	tam = vr.length;
	if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }
	if (tecla == 8 ){	tam = tam - 1 ; }
	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
		if ( tam <= 3 ){
	 		document.form[campo].value = vr ;
        }else
	 	if ( (tam > 3) && (tam <= 6) ){
	 		document.form[campo].value = vr.substr( 0, tam - 3 ) + '.' + vr.substr( tam - 3, tam ) ;
        }else
	 	if ( (tam > 6) && (tam <= 9) ){
	 		document.form[campo].value = vr.substr( 0, tam - 6 ) + '.' + vr.substr( tam - 6, 3 ) + '.' + vr.substr( tam - 3, tam ) ;
        }
	}else{
      if ((tecla != 9) && (tecla != 37) && (tecla != 39) && (tecla != 35) && (tecla != 36))
         event.returnValue = false;
    }
}




function FormataPercentual(campo,tammax,teclapres) {
	var tecla = teclapres.keyCode;
	vr = document.form[campo].value;
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	tam = vr.length;

	if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }

	if (tecla == 8 ){	tam = tam - 1 ; }

	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
		if ( tam <= 3 ){
	 		document.form[campo].value = vr ; }
	 	if ( (tam > 3) && (tam <= 6) ){
	 		document.form[campo].value = vr.substr( 0, tam - 3 ) + ',' + vr.substr( tam - 3, tam ) ; }
	}

}

function validacpf(campo){
    var i;
    s = document.form[campo].value;
    var c = s.substr(0,9);
    var dv = s.substr(9,2);
    var d1 = 0;
    for (i = 0; i < 9; i++)
    {
      d1 += c.charAt(i)*(10-i);
    }
    if (d1 == 0){
      return false;
    }

    d1 = 11 - (d1 % 11);
    if (d1 > 9) d1 = 0;
    if (dv.charAt(0) != d1)
    {
     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)
    {
     return false;
    }
    return true;
}

function somentenumero(evt)
{
  var tecla = evt.keyCode ? evt.keyCode :
              evt.charCode ? evt.charCode :
		      evt.which ? evt.which : void 0;
  if((tecla > 47 && tecla < 58) || tecla == 9)  // numeros || tab
    return true;
  else
    {
      if (tecla != 8) // backspace
        return false;
      else
        return true;
    }
}

//-----------------------------------------CNPJ BEGIN
function TESTA()
	{
	if(VerifyCNPJ(document.forms[0].CNPJ.value) == 1)
		{
		alert("CNPJ válido!");
		}
	else
		{
		alert("CNPJ não é válido!");
		}
	document.forms[0].CNPJ.focus();
	return;
	}



// Aqui inicia as funções de teste do CNPJ


function isNUMB(c)
	{
	if((cx=c.indexOf(","))!=-1)
		{
		c = c.substring(0,cx)+"."+c.substring(cx+1);
		}
	if((parseFloat(c) / c != 1))
		{
		if(parseFloat(c) * c == 0)
			{
			return(1);
			}
		else
			{
			return(0);
			}
		}
	else
		{
		return(1);
		}
	}

function LIMP(c)
	{
	while((cx=c.indexOf("-"))!=-1)
		{
		c = c.substring(0,cx)+c.substring(cx+1);
		}
	while((cx=c.indexOf("/"))!=-1)
		{
		c = c.substring(0,cx)+c.substring(cx+1);
		}
	while((cx=c.indexOf(","))!=-1)
		{
		c = c.substring(0,cx)+c.substring(cx+1);
		}
	while((cx=c.indexOf("."))!=-1)
		{
		c = c.substring(0,cx)+c.substring(cx+1);
		}
	while((cx=c.indexOf("("))!=-1)
		{
		c = c.substring(0,cx)+c.substring(cx+1);
		}
	while((cx=c.indexOf(")"))!=-1)
		{
		c = c.substring(0,cx)+c.substring(cx+1);
		}
	while((cx=c.indexOf(" "))!=-1)
		{
		c = c.substring(0,cx)+c.substring(cx+1);
		}
	return(c);
	}

function VerifyCNPJ(CNPJ)
	{
	CNPJ = LIMP(CNPJ);
	if(isNUMB(CNPJ) != 1)
		{
		return(0);
		}
	else
		{
		if(CNPJ == 0)
			{
			return(0);
			}
		else
			{
			g=CNPJ.length-2;
			if(RealTestaCNPJ(CNPJ,g) == 1)
				{
				g=CNPJ.length-1;
				if(RealTestaCNPJ(CNPJ,g) == 1)
					{
					return(1);
					}
				else
					{
					return(0);
					}
				}
			else
				{
				return(0);
				}
			}
		}
	}
function RealTestaCNPJ(CNPJ,g)
	{
	var VerCNPJ=0;
	var ind=2;
	var tam;
	for(f=g;f>0;f--)
		{
		VerCNPJ+=parseInt(CNPJ.charAt(f-1))*ind;
		if(ind>8)
			{
			ind=2;
			}
		else
			{
			ind++;
			}
		}
		VerCNPJ%=11;
		if(VerCNPJ==0 || VerCNPJ==1)
			{
			VerCNPJ=0;
			}
		else
			{
			VerCNPJ=11-VerCNPJ;
			}
	if(VerCNPJ!=parseInt(CNPJ.charAt(g)))
		{
		return(0);
		}
	else
		{
		return(1);
		}
	}
//-----------------------------------------CNPJ END

function FormataData(input, evt){
  var tecla = evt.keyCode ? evt.keyCode :
              evt.charCode ? evt.charCode :
		      evt.which ? evt.which : void 0;
   if ((tecla<48)||(tecla>57)){
      event.returnValue = false;
	} else {
       if ((input.value.length==2)||(input.value.length==5))
       		input.value=input.value + "/" ;
  	}
}

//-----------------------------------------------------------------
function FormataNumero(nNumero)
{
	var n ;
	sNumero = String(nNumero) ;
	nPonto = sNumero.indexOf(".") ;
	if (nPonto==-1)
	{
		sNumero = sNumero + ".00"
		nPonto = sNumero.indexOf(".") ;
	}
	sNumero = sNumero.substring(0,nPonto+3) ;
	sNumero = Replace(sNumero,".",",") ;
	nTamanho = sNumero.length ;
	nVirgula = sNumero.indexOf(",") ;
	sResultado = sNumero.substring(nVirgula,nVirgula+3) ;

	while ( sResultado.length < 3 )
		sResultado = sResultado + "0" ;
	nMilhar=0 ;
	for (n=nVirgula-1;n>=0;n--)
	{
		if (nMilhar==3)
		{
			nMilhar=0 ;
			sResultado="."+sResultado ;
		}
		sResultado=sNumero.charAt(n)+sResultado ;
		nMilhar++ ;
	}
	return sResultado ;
}
//--------------------------------------------------------------
function Replace(Expressao, StringProcurada, NovaString)
{
    Expressao = Expressao.toString();
    var sStringRet = ""
    var iTamExpressao = 0
    var i = 0
    if (Expressao!="")
    {
      sStringRet = Expressao
	  while ( i < sStringRet.length)
	  {
		if (sStringRet.charAt(i)==StringProcurada)
		{
		    sStringRet = sStringRet.substring(0,i) + NovaString + sStringRet.substring(i+1,sStringRet.length)
            if ( StringProcurada != "" )
	           i = i - 1
	    }
	    i = i + 1
	  }
    }
	return (sStringRet)
}
//--------------------------------------------------------------------------------------
function Arredonda( valor , casas ){
   var novo = Math.round( valor * Math.pow( 10 , casas ) ) / Math.pow( 10 , casas );
   return( novo );
}
