function TESTA(CNUMB,CTYPE, who)
{
  if(Verify(CNUMB, CTYPE))
    return true;
  else
  {
    alert(CTYPE + " não é válido!"); 
    who.focus();
    who.select();
    return false;
  }
  return false;
}

function ClearStr(str, char)
{
  while((cx=str.indexOf(char))!=-1)
  {		
    str = str.substring(0,cx)+str.substring(cx+1);
  }
  return(str);
}

function ParseNumb(c)
{
  c=ClearStr(c,'-');
  c=ClearStr(c,'/');
  c=ClearStr(c,',');
  c=ClearStr(c,'.');
  c=ClearStr(c,'(');
  c=ClearStr(c,')');
  c=ClearStr(c,' ');
  if((parseFloat(c) / c != 1))
  {
    if(parseFloat(c) * c == 0)
      return(c);
    else
      return(0);
  }
  else
  {
    return(c);
  }
}

function Verify(CNUMB,CTYPE)
{
  CNUMB=ParseNumb(CNUMB)
  if(CNUMB == 0)
  {
    return(false);
  }
  else
  {
    g=CNUMB.length-2;
    if(TestDigit(CNUMB,CTYPE,g))
    {
      g=CNUMB.length-1;
      if(TestDigit(CNUMB,CTYPE,g))
      {	
        return(true);
      }
      else
      {
        return(false);
      }
    }
    else
    {
      return(false);
    }
  }
}

function TestDigit(CNUMB,CTYPE,g)
{
  var dig=0;
  var ind=2;
  for(f=g;f>0;f--)
  {
    dig+=parseInt(CNUMB.charAt(f-1))*ind;
    if (CTYPE=='CNPJ')
    { if(ind>8) {ind=2} else {ind++} }
    else
    { ind++ }
  }
  dig%=11;
  if(dig<2)
  {
    dig=0;
  }
  else
  {
    dig=11-dig;
  }
  if(dig!=parseInt(CNUMB.charAt(g)))
  {
    return(false);
  }
  else
  {
    return(true);
  }
}

function SetFocus()
{
document.getElementById('NR').focus();
}
function Limpar()
{
document.Form1.reset();
document.getElementById('NR').focus();
}
function Vaidest()
{
if (Form1.cnpjdest.value == "")
{
alert("Informe o CNPJ / CPF do Destinatario.");
document.getElementById("cnpjdest").focus();
document.getElementById("cnpjdest").select();
return;
}
if (Form1.cnpjdest.value.length != 11)
{
if (!TESTA(document.getElementById('cnpjdest').value, 'CNPJ', document.getElementById('cnpjdest')))
return;
}
else
{
if (!TESTA(document.getElementById('cnpjdest').value, 'CPF', document.getElementById('cnpjdest')))
return;
}
//CPF nao tem senha
if ((document.getElementById('cnpjdest').value.length != 11) && (document.getElementById('senha').value == ""))
{
alert("Informe a Senha.");
document.getElementById("senha").focus();
document.getElementById("senha").select();
return;
}
document.Form1.submit();
}
function Vai()
{
if (document.getElementById('cnpj').value == "")
{
alert("Informe o CNPJ do remetente.");
document.getElementById("cnpj").focus();
document.getElementById("cnpj").select();
return;
}
if (document.getElementById('NR').value == "")
{
alert("Informe pelo menos uma Nota Fiscal.");
document.getElementById("NR").focus();
return;
}
if (document.getElementById('cnpj').value.length != 11)
{
if (!TESTA(document.getElementById('cnpj').value, 'CNPJ', document.getElementById('cnpj')))
return;
}
else
{
if (!TESTA(document.getElementById('cnpj').value, 'CPF', document.getElementById('cnpj')))
return;
}
document.Form1.submit();
}

function showmapa(param)
{
  var tagname
  tagname = '_blank';
  action = "http://www.ssw.inf.br/mapa/mapa.php?" + param;
  janela=window.open(action, tagname, "fullscreen=0,menubar=0,location=0,toolbar=0,status=0," +
                                      "scrollbars=0,resizable=0,width=655" +
                                      ",height=720,top=1,left=1");
  return true;
}




