<!--

function FindObject(n, d) {
  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 = FindObject(n, d.layers[i].document);
  if ( !x && d.getElementById ) x = d.getElementById(n);
  return x;
}

function ValidateForm() {
  var k = 0, i, p, q, nm, test, val, num, vmin, vmax, errors = "", args = ValidateForm.arguments;
  for ( i = 0; i < ( args.length - 2 ); i += 3 ) {
    test = args[i+2]; val = FindObject(args[i]);
    if ( val ) {
      nm = ( args[i+1] != "" )?args[i+1]:val.name; nm = "Campo " + nm;
      if ( val.type == "select-one" || val.type == "select-multiple" ) {
        if ( val.options && val.selectedIndex <= 0 ) errors += "- " + nm + " deve ser selecionado.\n";
      } else {
        //if ( trim(( val = val.value ), " ") != "" ) {
        val = val.value;
        if ( trim(val, " ") != "" ) {
          if ( test.indexOf("isEmail") != -1 ) {
            p = val.indexOf("@"); if ( p < 1 || p == ( val.length - 1 ) ) errors += "- " + nm + " deve ser um endereço de e-mail válido.\n";
          } else if ( test != "R" ) {
            p = val.indexOf(","); if ( p != -1 ) val = val.substring(0, p) + "." + val.substring(p + 1);
            num = parseFloat(val); if ( val != "" + num && val.indexOf(".") == -1 ) errors += "- " + nm + " deve conter um número.\n";
            if ( test.indexOf("inRange") != -1 ) {
              p = test.indexOf(":"); vmin = parseFloat(test.substring(8,p)); vmax=parseFloat(test.substring(p+1));
              if ( num < vmin || vmax < num ) errors += "- " + nm + " deve conter um número entre " + vmin + " e " + vmax + ".\n";
            }
          }
        } else if ( test.charAt(0) == "R" ) errors += "- " + nm + " é requerido.\n";
      }
    }
  }
  return errors;
}

function Dimension(element) {
  this.x = -1;
  this.y = -1;
  this.w = 0;
  this.h = 0;
  if ( element == document ) {
    this.x = element.body.scrollLeft;
    this.y = element.body.scrollTop;
    this.w = element.body.clientWidth;
    this.h = element.body.clientHeight;
  } else if ( element != null ) {
    var e = element;
    var l = e.offsetLeft;
    while ( ( e = e.offsetParent ) != null ) { 
      l += e.offsetLeft; 
    }
    var e = element;
    var t = e.offsetTop;
    while( ( e = e.offsetParent ) != null ) { 
      t += e.offsetTop; 
    }
    this.x = l;
    this.y = t;
    this.w = element.offsetWidth;
    this.h = element.offsetHeight;
  }
}

function TRhover(obj, odd, evt) {
  if ( evt == "over" ) {
    obj.style.background = "LightYellow";
    obj.style.cursor = "pointer";
  } else {
    if ( odd == 1 ) obj.style.background = "WhiteSmoke";
    else obj.style.background = "Gainsboro";
    obj.style.cursor = "default";
  }
}

function DataAction() {
  var args = DataAction.arguments, actn = "", errors = "";
  if ( args[0] ) {
    if ( typeof(args[0]) == "object" ) {
      args[0].blur();
      if ( args[0].name != "" ) actn = args[0].name.toUpperCase();
      else actn = args[0].value.toUpperCase();
    } else actn = args[0];
  }
  switch ( actn ) {
    case "PRIMEIRO":
      FindObject("Rec").value = "1";
      FindObject("Id").value = "";
      FindObject("Do").value = actn;
      break;
    case "ANTERIOR":
      var sRec = FindObject("Rec").value;
      if ( !sRec || isNaN(sRec) ) sRec = 1;
      else sRec = parseInt(sRec) - 1;
      FindObject("Rec").value = sRec;
      FindObject("Id").value = "";
      FindObject("Do").value = actn;
      break;
    case "PROXIMO":
      var sRec = FindObject("Rec").value;
      if ( !sRec || isNaN(sRec) ) sRec = 1;
      else sRec = parseInt(sRec) + 1;
      FindObject("Rec").value = sRec;
      FindObject("Id").value = "";
      FindObject("Do").value = actn;
      break;
    case "ULTIMO":
      FindObject("Rec").value = "99999999";
      FindObject("Id").value = "";
      FindObject("Do").value = actn;
      break;
    case "INSERIR":
      FindObject("Rec").value = "";
      FindObject("Id").value = "";
      FindObject("Do").value = actn;
      break;
    case "EXCLUIR":
      if ( confirm("Confirmar a exclusão?") ) {
        if ( args[1] ) FindObject("Rec").value = args[1];
        if ( args[2] ) FindObject("Id").value = args[2];
        FindObject("Do").value = actn;
      } else return false;
      break;
    case "EDITAR":
      FindObject("Do").value = actn;
      break;
    case "GRAVAR":
      var sTab = FindObject("Tab").value;
      if ( sTab == "Surveys" ) errors += ValidateForm("Svy_Name","Nome","R");
      if ( sTab == "Questions" ) {
        errors += ValidateForm("Qst_Summary","Sumário","R", "Qst_Question","Pergunta","R");
        if ( FindObject("Qst_Complete").checked ) errors += ValidateForm("Qst_Label","Legenda","R");
      }
      if ( sTab == "Choices" ) errors += ValidateForm("Chc_Choice","Opção","R");
      if ( sTab == "Conditionals" ) {
        errors += ValidateForm("Cnd_Question","Pergunta","R", "Cnd_Choice","Opção","R", "Cnd_JumpTo","Pular para","R");
        if ( !errors ) {
          var sQuestion = FindObject("Cnd_Question"); sQuestion = sQuestion[sQuestion.selectedIndex].value;
          var sJumpTo = FindObject("Cnd_JumpTo"); sJumpTo = sJumpTo[sJumpTo.selectedIndex].value;
          if ( sQuestion == sJumpTo ) errors += "- Pergunta base deve ser diferente da pergunta para pular.\n";
        }
      }
      FindObject("Do").value = actn;
      break;
    case "CANCELAR":
      FindObject("Do").value = actn;
      break;
    case "ATUALIZAR":
      FindObject("Rec").value = "";
      FindObject("Id").value = "";
      FindObject("Do").value = actn;
      break;
    case "SELECIONAR":
      FindObject("Rec").value = ( args[1] ) ? args[1] : "";
      FindObject("Id").value = ( args[2] ) ? args[2] : "";
      FindObject("Do").value = actn;
      break;
    case "MARCAR":
      FindObject("Rec").value = ( args[1] ) ? args[1] : "";
      FindObject("Id").value = ( args[2] ) ? args[2] : "";
      return void(0);
      break;
    case "DUPLICAR":
      if ( confirm("Confirmar a duplicação?") ) {
        FindObject("Rec").value = ( args[1] ) ? args[1] : "";
        FindObject("Id").value = ( args[2] ) ? args[2] : "";
        FindObject("Do").value = actn;
      } else return false;
      break;
    case "EXECUTAR":
    case "MAIS":
    case "MENOS":
    case "SAIR":
      FindObject("Do").value = actn;
      break;
    case "CALENDARIO":
      var div = new Dimension(FindObject(args[1]+"_D")), cal = FindObject(args[1]+"_Calendar");
      cal.style.top = div.y + div.h + "px"; cal.style.left = div.x + "px";
      var tcal = FindObject(args[1]+"TCalendar");
      tcal.width = "100%"; tcal.style.margin = "0";
      ToggleCalendar(args[1]);
      return void(0);
      break;
    case "TUDO":
      var dd, mm, yyyy;
      
      dd = args[1].substring(6,8); if ( dd.substring(0,1) == "0" ) dd = dd.substring(1,2);
      mm = args[1].substring(4,6); if ( mm.substring(0,1) == "0" ) mm = mm.substring(1,2);
      yyyy = args[1].substring(0,4);
      
      FindObject("Starting").value = dd + "/" + mm + "/" + yyyy; //args[1];
      FindObject("Starting_D").selectedIndex = parseInt(dd);
      FindObject("Starting_M").selectedIndex = parseInt(mm);
      FindObject("Starting_Y").value = parseInt(yyyy);
      
      dd = args[2].substring(6,8); if ( dd.substring(0,1) == "0" ) dd = dd.substring(1,2);
      mm = args[2].substring(4,6); if ( mm.substring(0,1) == "0" ) mm = mm.substring(1,2);
      yyyy = args[2].substring(0,4);
      
      FindObject("Ending").value = dd + "/" + mm + "/" + yyyy; //args[2];
      FindObject("Ending_D").selectedIndex = parseInt(dd);
      FindObject("Ending_M").selectedIndex = parseInt(mm);
      FindObject("Ending_Y").value = parseInt(yyyy);
      
      return void(0);
      break;
    case "IMPRIMIR":
      var prt;
      prt = FindObject("Report"); if ( prt ) PrintContent(prt,"Relatório");
      prt = FindObject("List"); if ( prt ) PrintContent(prt,"Planilha");
      return void(0);
      break;
    case "APAGAR":
      if ( CheckCount() <= 0 ) errors += '- Nenhuma coleta selecionada.\n';
      else if ( confirm("Confirmar o apagamento?") ) FindObject("Do").value = actn;
      else return false;
      break;
    case "FIM":
      window.close();
    default:
      return void(0);
      break;
  }
  if ( errors ) {
    alert("ATENÇÃO:\n\n" + errors);
    return false;
  } else FindObject("FormData").submit();
}

function PrintContent(o,t) {
  if ( typeof(o) == "object" ) str = o.innerHTML
  else str = document.getElementById(o).innerHTML;
  newwin = window.open("", "PRINT", "left=40,top=40,width=640,height=480");
  with ( newwin ) {
    document.write("<!DOCTYPE HTML>\n");
    document.write("<HTML>\n<HEAD>\n");
    document.write("<TITLE>"+t+"</TITLE>\n");
    document.write("<META HTTP-EQUIV=\"Content-Script-Type\" CONTENT=\"text/javascript\" />\n");
    document.write("<META HTTP-EQUIV=\"Content-Style-Type\" CONTENT=\"text/css\" />\n");
    document.write("<scr"+"ipt>\n");
    document.write("function check_state() {\n");
    document.write("  if ( document.readyState == \"complete\" ) window.close();\n");
    document.write("  else setTimeout(\"check_state()\", 2000);\n");
    document.write("}\n");
    document.write("function print_window() {\n");
    document.write("  window.print();\n");
    document.write("  check_state();\n");
    document.write("}\n");
    document.write("<\/scr"+"ipt>\n");
    document.write("<style>\n");
    document.write("BODY, TH, TD { font-size: 10pt }\n");
    document.write("</style>\n");
    document.write("</HEAD>\n");
    document.write("<BODY onload=\"print_window()\">\n");
    document.write(str);
    document.write("</BODY>\n</HTML>\n");
    document.close();
  }
}

function ShowHide(r) {
  var img = FindObject("IMG_" + r);
  var tr = FindObject("TR_" + r);
  if ( img && tr ) {
    if ( tr.style.display != "" ) {
      img.src = "images/minus.gif";
      tr.style.display = "";
    } else {
      img.src = "images/plus.gif";
      tr.style.display = "none";
    }
  }
}

function CheckAll(f) {
  var df;
  if ( !f ) df = document.forms[0];
  else if ( typeof(f) == "object" ) df = f;
  else if ( isNaN(f) ) df = document.forms(f);
  else df = document.forms[f];
  new_value = ( !df.elements[0].checked );
  for ( var i = 0; i < df.elements.length; i++ ) {
    df.elements[i].checked = new_value;
  }
}

function CheckCount(f) {
  var df;
  if ( !f ) df = document.forms[0];
  else if ( typeof(f) == "object" ) df = f;
  else if ( isNaN(f) ) df = document.forms(f);
  else df = document.forms[f];
  counter = 0;
  for ( var i = 0; i < df.elements.length; i++ ) {
    if ( df.elements[i].type == "checkbox") {
      if ( df.elements[i].checked ) counter++;
    }
  }
  return counter;
}

function CharsLeft() {
  var args = CharsLeft.arguments;
  if ( args.length > 1 ) {
    var val = args[0].value, hlp = FindObject("LIM_" + args[0].name);
    len = parseInt(args[1]) - val.length;
    if ( len == parseInt(args[1]) ) hlp.innerHTML = "&nbsp;";
    else if ( len > 0 ) hlp.innerHTML = "Restam " + len + " caracteres!";
    else if ( len <= 0 ) {
      hlp.innerHTML = "Espaço esgotado!";
      args[0].value = val.substring(0, parseInt(args[1]));
    }
  }
}

function NumberOnly(myfield, e) {
  if ( myfield.length == 0 ) myfield.value = 0;
  var key;
  var keychar;
  if ( window.event ) key = window.event.keyCode;
  else if ( e ) key = e.which;
  else return true;
  keychar = String.fromCharCode(key);
  if ( (key == null) || (key == 0) || (key == 8) || (key == 9) || (key == 13) || (key == 27) ) return true;
  else if ( (("0123456789").indexOf(keychar) > -1) ) return true;
  else return false;
}

function trim(str, chars) {
  return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
  chars = chars || "\\s";
  return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
  chars = chars || "\\s";
  return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

//-->

