function start() {
  top.mainFrame = top.frames[0].frames[0];
  top.innerFrame = top.frames[0];
  top.submitFrame = top.frames[1];
  top.setEnvironment();
  top.mainFrame.start();
}

/*********** INICIALIZACOES GLOBAIS ***********/
top.mainFrame = null;
top.submitFrame = null;
top.innerFrame = null;

top.xmlhttp = false;
try {
  top.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
  try {
    top.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch(e) {
    top.xmlhttp = false;
  }
}
if(!top.xmlhttp && typeof XMLHttpRequest != "undefined") {
  top.xmlhttp = new XMLHttpRequest();
}

top.showTR    = "table-row";
if(document.all) top.showTR = "block";

/********* FIM INICIALIZACOES GLOBAIS *********/

/*********** FUNCOES LOCALE ***********/
top.BRASIL = 76;
top.ARGENTINA = 32;
top.MEXICO = 484;
top.EUA = 840;
top.AFEGANISTAO = 4
top.DEFAULT_LOCALE = top.BRASIL;

top.changeLocale = function(lang, country) {
  top.location = top.getForward("ChangeLocale") + "&lang=" + lang + "&country=" + ((country)?country:"");
}

top.toggleState = function(v,hf,listBuffer,state,city) {
  if(v <= 0) {  //it shoud be checking for Country's ISO.
    top.setInnerHTML(state.id, state.text, top.mainFrame);
    if(city){ top.setInnerHTML(city.id, city.text, top.mainFrame); }
    return;  
  }
  for(var i = 0; i < listBuffer.length; i++) {
    if(listBuffer[i][0] == v) {
      if(listBuffer[i][1].length > 10){
        var estados = state.selectField.replace("$OPTIONS$",listBuffer[i][1]);
        top.setInnerHTML(state.id, estados, top.mainFrame);
        if(city){ top.setInnerHTML(city.id, city.text, top.mainFrame); }
      } else {
        top.setInnerHTML(state.id, state.textField, top.mainFrame);
        if(city){ top.setInnerHTML(city.id, city.textField, top.mainFrame); }
      }
      return true;
    }
  }
  top.loadLista("../actions/loadEstados.asp?c=" + v, listBuffer, v, hf, false);
}

/*
  v= valor selecionado
  hf = handler function, ponteiro para a função local
  listBuffer = buffer com as listas de opções ja retornadas
  city = objeto com informações do campo de cidade
*/
top.toggleCity = function(v,hf,listBuffer,city) {
  if(v <= 0) { 
    //top.setInnerHTML(city.id, city.text, top.mainFrame);
    return; 
  }
  for(var i = 0; i < listBuffer.length; i++) {
    if(listBuffer[i][0] == v) {
      if(listBuffer[i][1].length > 10){
        var cidades = city.selectField.replace("$OPTIONS$",listBuffer[i][1]);
        top.setInnerHTML(city.id, cidades, top.mainFrame);
      } else {
        top.setInnerHTML(city.id, city.textField, top.mainFrame);
      }
      return true;
    }
  }
  top.loadLista("../actions/loadCidades.asp?c=" + v, listBuffer, v, hf, false);
}
/********* FIM FUNCOES LOCALE *********/


/*********** FUNCOES PARA CARREGAR LISTAS GENERICAS ***********/
top.loadLista = function(p, l, v, f, a) {
  top.xmlhttp.open("GET", p, a);
  top.xmlhttp.onreadystatechange = function() {
    if(top.xmlhttp.readyState == 4) {
      return top.pushToLista(l, v, f);
    }
  }
  top.xmlhttp.send(null);
  if(!a) {
    return top.pushToLista(l, v, f);
  }
}

top.pushToLista = function(l, v, f) {
  l.push(new Array());
  l[l.length-1].push(v);
  l[l.length-1].push(top.xmlhttp.responseText);
  f(v);
}
/********* FIM FUNCOES PARA CARREGAR LISTAS GENERICAS *********/

/*********** FUNCOES GENERICAS ***********/
/* Habilita ou desabilita a linha que contém os links para
 * continuar o cadastramento
 */
top.toggleForwardLink = function () {
  var fl = top.getObj("forwardLinkDiv", top.mainFrame);

  if(fl != null) {
    if(fl.style.display == "none") {
      fl.style.display = "inline";
    } else {
      fl.style.display = "none";
    }
  }
}

/* Habilita ou desabilita um grupo de campos de acordo com
 * a escolha de sim ou não.
 */
top.toggleGrupo = function (grupo, tipo) {
  var el, img;

  for(var i = 0; i < grupo.length; i++){
    img = top.getObj(grupo[i][1], top.mainFrame);
    if(img != null) {
      img.src="../images/obrigat" + ((tipo == 0) ? "_1" : "") + ".gif";
    }

    el = grupo[i][0];
    if(el!=null){
      if(tipo == 0) { // false, não
        if(el.length && (el[0].type == "radio" || el[0].type == "checkbox")) {
          for(var j = 0; j < el.length; j++) {
            if(el[j].className.indexOf("disabled") == -1) {
              el[j].className += " disabled";
              el[j].checked = false;
            }
            el[j].disabled = true;
          }
        } else {
          switch(el.type) {
            case "text":
            case "textarea":
              el.value = "";
              break;
            case "select-one":
              el.selectedIndex = 0;
              break;
            case "select-multiple":
              el.selectedIndex = -1;
              break;
          }
          if(el.className.indexOf("disabled") == -1) {
            el.className += " disabled";
          }
          el.disabled = true;
        }
      } else { // true, sim
        var iDisabled;
        if(el.length && (el[0].type == "radio" || el[0].type == "checkbox")) {
          for(var j = 0; j < el.length; j++) {
            iDisabled = el[j].className.indexOf("disabled");
            if(iDisabled > -1) {
              el[j].className = el[j].className.substring(0, iDisabled) + el[j].className.substring(iDisabled + "disabled".length, el[j].className.length);
            }
            el[j].disabled = false;
          }
        } else {
          iDisabled = el.className.indexOf("disabled");
          if(iDisabled > -1) {
            el.className = el.className.substring(0, iDisabled) + el.className.substring(iDisabled + "disabled".length, el.className.length);
          }
          el.disabled = false;
        }
      }
    }
  }
}

/*Funções para carregar dados para alteração*/
top.fillDados = function(dados, form) {
  var campo;
  for(i = 1; i < dados.length; i++) {
    campo = eval("form." + dados[i][0]);
    if(campo) {
      if(campo.type) {
        switch (campo.type) {
          case "text":
          case "textarea":
          case "hidden":
          case "password":
            campo.value = dados[i][1];
            break;
          case "select-one":
          case "select-multiple":
            top.marcaSelect(campo,dados[i][1]);
            break;
          case "checkbox":
          case "radio":
          //caso seja apenas um radio ou checkbox ele não será um array e terá um type
          //para mais de um radio ou checkbox entra no outro if
            if(campo.value == dados[i][1]) {
              campo.checked = true;
            }
            break;
        }
      } else {
        if(campo[0].type) {
          campo = eval("form." + dados[i][0]);
          if(campo[0].type == "radio") {
            top.marcaRadio(campo, dados[i][1]);
          }
        }
      }
    }
  } 
}

top.marcaRadio = function(campo, valor) {
  for(m = 0; m < campo.length; m++) {
    if(campo[m].value == valor) {
      campo[m].checked = true;
      return;
    }
  }
}

top.marcaSelect = function(campo, valor) {
  for(m = 0; m < campo.length; m++) {
    if(campo[m].value == valor) {
      campo[m].selected = true;
      return;
    }
  }
}
/*FIM Funções para carregar dados para alteração*/
/********* FIM FUNCOES GENERICAS *********/

top.Central = function() {
  this.paginas = null;
  this.addPagina = function(pagina) {
    var pag = this.paginas;
    if(pag == null) {
      this.paginas = new top.Pagina(pagina);
      return;
    }
    while(pag.proxima != null) {
      pag = pag.proxima;
    }
    pag.proxima = new top.Pagina(pagina);
  }
 
  this.findPagina = function (nome) {
    var pag = this.paginas;
    while(pag.proxima != null) {
      if(pag.nome == nome) return pag;
      pag = pag.proxima;
    }
  }

  this.zeraPagina = function (nome) {
    this.findPagina(nome).html = "";
  }

  this.zeraTudo = function() {
    var pag = this.paginas;
    while(pag.proxima != null) {
      pag.html = "";
      pag = pag.proxima;
    }
  }

  this.preenche = function() {
    var pag = this.paginas;
    var ret = "0";
    while(pag != null) {
      if(pag.html == "") {
        ret += "," + pag.nome;
      } else {
        top.setInnerHTML(pag.nome, pag.html, top.mainFrame);
      }
      pag = pag.proxima;
    }
    return ret;
  }
}

top.Pagina = function(_nome) {
  this.nome = _nome;
  this.html = top.getInnerHTML(_nome, top.mainFrame);
  this.proxima = null;
}

/*AJAX*/
ajax = new Object();

/*Start the XMLHttpRequest object. Commonly known around the web*/
ajax.getXmlHttpRequest = function () {
  var xmlhttp = false;
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch(e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {
      xmlhttp = false;
    }
  }
  if(!xmlhttp && typeof XMLHttpRequest != "undefined") {
    xmlhttp = new XMLHttpRequest();
  }
  return xmlhttp;
}

/*
XMLHttpRequest wrap around functions - By Rafael Raposo
*/
ajax.sendIt = function(m, f, a, h, p) {
  var xmlhttp = this.getXmlHttpRequest();
  xmlhttp.open(m, f, a);

  if(m == "POST") {
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  }

  xmlhttp.onreadystatechange = function() {
    if(xmlhttp.readyState == 4) {
      if(xmlhttp.status == 200) {
        h(xmlhttp.responseText);
      } else {
        alert(xmlhttp.responseText)
      }
    }
  }
  xmlhttp.send(p);
  if(!a) {
    if(xmlhttp.readyState == 4) {
      if(xmlhttp.status == 200) {
        h(xmlhttp.responseText);
      } else {
        alert(xmlhttp.responseText)
      }
    }
  }
}

ajax.postIt = function(f, a, h, p) {
  top.sendIt("POST", f, a, h, p);
}

ajax.getIt = function(f, a, h) {
  top.sendIt("GET", f, a, h);
}

ajax.formToString = function (form) {
  var el, ret = "";
  for(var i = 0; i < form.elements.length; i++) {
    el = form.elements[i];
    if(el.disabled) continue;
    switch(el.type) {
      case "text":
      case "textarea":
      case "hidden":
        ret += el.name + "=" + el.value;
        break;
      case "select-one":
        ret += el.name + "=" + el[el.selectedIndex].value;
        break;
      case "select-multiple":
        ret += el.name + "=" + this.getMultipleValue(el);
        break;
      case "radio":
      case "checkbox":
        ret += el.name + "=" + this.getMultipleValue(eval("el.form." + el.name));
        i += (eval("el.form." + el.name)).length - 1;
        break;
    }
    ret += "&";
  }
  return encodeURI(ret.substring(0, ret.length-1));
}

ajax.getMultipleValue = function(el) {
  var ret = "";
  for(var i = 0 ;i < el.length; i++) {
    if(el[i].type == "radio" || el[i].type == "checkbox") {
      if(el[i].checked) {
        ret += el[i].value + ",";
      }
    } else {
      if(el[i].selected) {
        ret += el[i].value + ",";
      }
    }
  }
  return ret.substring(0, ret.length-1);
}
/**/

top.findForward = function(action, forward, frame) {
  if(!forward) {
    forward = "";
    frame = top.mainFrame;
  } else if(typeof forward != "string") {
    frame = forward;
    forward = "";
  } else if(!frame) {
    frame = top.mainFrame;
  }
  frame.location = top.getForward(action, forward);
}

top.getForward = function(action, forward) {
  return "../actions/forward.asp?action=" + action + "&forward=" + ((forward) ? forward : "");
}

top.submitForward = function(action) {
  return getForward(action, "submit");
}