var NUM_CHARS = '0123456789';

function ValidateCNPJ(value) {
  var mv;
  var d1, r, pd, sd;

  if (value.length!=14)
    return false;
  else if (value=='00000000000000')
    return true;
  else {
    for (var i=0; i<14; i++)
      if (NUM_CHARS.indexOf(value.charAt(i))==-1)
        return false;

    mv = '543298765432';
    d1 = 0; for (var i=0; i<12; i++)  d1 += parseInt(value.charAt(i)) * parseInt(mv.charAt(i));
    r = d1 - parseInt(d1/11) * 11;
    pd = r<2 ? 0 : 11 - r;

    mv = '6543298765432';
    d1 = 0; for (var i=0; i<13; i++)  d1 += parseInt(value.charAt(i)) * parseInt(mv.charAt(i));
    r = d1 - parseInt(d1/11) * 11;
    sd = r<2 ? 0 : 11 - r;

    return (pd+''+sd==value.substring(12,14));
  }
}

function ValidateCPF(value) {

  if (value.length!=11)
    return false;
  else {
    for (var i=0; i<11; i++)
      if (NUM_CHARS.indexOf(value.charAt(i))==-1)
        return false;

    var d1 = 0; for (var i=0; i<9; i++) d1 += parseInt(value.charAt(i)) * (i+1);
    d1 %= 11; if (d1 == 10) d1 = 0;

    var d2 = 0; for (var i=1; i<9; i++) d2 += parseInt(value.charAt(i)) * i;
    d2 += d1 * 9; d2 %= 11; if (d2 == 10) d2 = 0;

    return (d1+''+d2==value.substring(9,11));
  }
}

function ValidateCNPJCPF(value) {

  var Temp;

  Temp = '';

  for (var i=0; i<value.length; i++)
    if (NUM_CHARS.indexOf(value.charAt(i))!=-1)
      Temp = Temp + value.charAt(i);

  value = Temp;

  if (value.length == 11)
    return ValidateCPF(value);
  else if (value.length == 14)
    return ValidateCNPJ(value);
  else
    return false;
}

function y2k(number) {
  return (number<1000) ? number + 1900 : number;
}

function ValidateDate(value) {

  if ( (value.length != 10) || (value.substring(2,3) != sep) || (value.substring(5,6) != sep) )
    return false;
  else {
    var date  = value.substring(0,2);
    var month = value.substring(3,5);
    var year  = value.substring(6,10);
    var test  = new Date(year,month-1,date);

    if ( (year != y2k(test.getYear())) || (month-1 != test.getMonth()) || (date != test.getDate()) )
      return false;
  }
}

function ValidateEmail(value) {
  return ( (value.length>3) && (value.indexOf('@')>0) );
}

function convertToUpper (field)
{
  field.value=field.value.toUpperCase();
}

function convertToLower (field)
{
  field.value=field.value.toLowerCase();
}

function icListMarkAll(){
  for (var i=0;i<document.icList.elements.length;i++)
  {
    var e = document.icList.elements[i];
    if ((e.name != 'icbMarkAll') && (e.type=='checkbox'))
    {
      e.checked = document.icList.icbMarkAll.checked;
    }
  }
}

function ShowWindowCad(link,windowname)
{
  window.open(link,windowname,'top=20, left=50, width=700, height=500, toolbar=1, status=1, menubar=0, location=0, directories=0, scrollbars=1, resizable=1');
}

function ShowWindowCadNotHaveMenu(link,windowname)
{
  window.open(link+'&NotHaveMenu=S',windowname,'top=20, left=50, width=700, height=500, toolbar=1, status=1, menubar=0, location=0, directories=0, scrollbars=1, resizable=1');
}

function ShowWindow(link,width,height,top,left)
{
  window.open(link,'','top='+top+', left='+left+', width='+width+', height='+height+', toolbar=0, status=0, menubar=0, location=0, directories=0, scrollbars=0, resizable=0');
}

function ShowWindowNotHaveMenu(link,width,height,top,left)
{
  window.open(link+'&NotHaveMenu=S','','top='+top+', left='+left+', width='+width+', height='+height+', toolbar=0, status=0, menubar=0, location=0, directories=0, scrollbars=0, resizable=0');
}

function ShowWindowDefineHW(link,height,width)
{
  window.open(link,'','top=100, left=100, width='+width+', height='+height+', toolbar=0, status=0, menubar=0, location=0, directories=0, scrollbars=1, resizable=0');
}

function ShowWindowDefineHWNotHaveMenu(link,height,width)
{
  window.open(link+'&NotHaveMenu=S','','top=100, left=100, width='+width+', height='+height+', toolbar=0, status=0, menubar=0, location=0, directories=0, scrollbars=1, resizable=0');
}

function ShowNewWindow(link)
{
  window.open(link,'','top=0, left=0, width=800, height=600, toolbar=1, status=1, menubar=0, location=0, directories=0, scrollbars=1, resizable=1');
}

function ShowNewWindowNotHaveMenu(link)
{
  window.open(link+'&NotHaveMenu=S','','top=0, left=0, width=800, height=600, toolbar=1, status=1, menubar=0, location=0, directories=0, scrollbars=1, resizable=1');
}

function ShowHelp(link)
{
  var w = 480, h = 340;

  if (document.all) {
     /* the following is only available after onLoad */
     w = document.body.clientWidth;
     h = document.body.clientHeight;
  }
  else if (document.layers) {
     w = window.innerWidth;
     h = window.innerHeight;
  }

  var popW = 300, popH = 200;

  var leftPos = (w-popW)/2, topPos = (h-popH)/2;

  msgWindow = window.open(link, 'windowduvida', 'top='+topPos+', left='+leftPos+', width=332, height=129, toolbar=0, status=0, menubar=0, location=0, directories=0, scrollbars=0, resizable=0');
  if (msgWindow.opener == null)
    msgWindow.opener = self;
}

function ClearDesc(Desc)
{
  var Result;
  var i;
  var LengthString;

  LengthString = Desc.length;
  Result = '';

  for (i=0; i<=LengthString; i++)
  {
    if (Desc.charAt(i)!='+')
      Result = Result + Desc.charAt(i);
    else
      Result = Result + ' ';
  }
  return Result;
}

function mascara_data(dt_field){
    var mydata = '';
    mydata = mydata + dt_field.value;
    if (mydata.length == 2){
        mydata = mydata + '/';
        dt_field.value = mydata;
    }
    if (mydata.length == 5){
        mydata = mydata + '/2003';
        dt_field.value = mydata;
    }
}

function mascara_hora(hr_field){
    var myhora = '';
    myhora = myhora + hr_field.value;
    if (myhora.length == 2){
        myhora = myhora + ':';
        hr_field.value = myhora;
    }
    if (myhora.length == 5){
        myhora = myhora + ':00';
        hr_field.value = myhora;
    }
}

function displaydatetime()
{
  if (!document.layers && !document.all)
    return;
  var today;
  var timeLocal;
  today = new Date();
  timeLocal = today.toLocaleString();
  //timeLocal = today.getHours()+':'+today.getMinutes()+':'+today.getSecondes();
  if (document.layers)
  {
    document.layers.clockLocal.document.write(timeLocal);
    document.layers.clockLocal.document.close();
  }
  else if (document.all)
  {
  clockLocal.innerHTML = timeLocal;
  }
  setTimeout("displaydatetime()", 500)
}

function LogoutFlexTool()
{
  parent.ibMenu.location.href='/cgi-bin/flextool.dll/html/pub/menu';
  parent.ibMain.location.href='/cgi-bin/flextool.dll/html/pub/LoginUser.ipMain?Logout=S';
}

function LogoutApp()
{
  parent.ibMenu.location.href='menu';
  parent.ibMain.location.href='LoginUser.ipMain?Logout=S';
}


function GoAcompanhamento()
{
  parent.ibMenu.location.href='/cgi-bin/flextool.dll/html/app/AcompanhamentoAtendimento';
}



