//-------------------------------------- NAVEGAÇÃO --------------------------------------//
function novaJanela(pagina,nome,width,height) {
  var x = (screen.width - width) / 2;
  var y = (screen.height - height) / 2;
  var win = window.open(pagina,nome,'dependent,status,top='+y+',left='+x+',width='+width+',height='+height);
  if (win.opener!=window) {
    win.opener = window;
  }
  win.focus();
  return win;
}

function novaJanelaRolagem(pagina,nome,width,height) {
  var x = (screen.width - width) / 2;
  var y = (screen.height - height) / 2;
  var win = window.open(pagina,nome,'dependent,status,scrollbars,top='+y+',left='+x+',width='+width+',height='+height);
  if (win.opener!=window) {
    win.opener = window;
  }
  win.focus();
  return win;
}

function novaJanelaRelatorio(pagina,nome) {
  var x = (screen.width - 670) / 2;
  var y = (screen.height - 480) / 2;
  var win = window.open(pagina,nome,'dependent,status,menubar,scrollbars,top='+y+',left='+x+',width=670,height=480');
  if (win.opener!=window) {
    win.opener = window;
  }
  win.focus();
  return win;
}

function novaJanelaRelatorioHtml(nome,html) {
  var x = (screen.width - 670) / 2;
  var y = (screen.height - 480) / 2;
  var win = window.open('',nome,'dependent,status,menubar,scrollbars,top='+y+',left='+x+',width=670,height=480');
  if (win.opener!=window) {
    win.opener = window;
  }
  win.document.open();
  win.document.write(html);
  win.document.close();
  win.focus();
  return win;
}

function novaJanelaRelatorioAjustavel(pagina,nome,width,height) {
  var x = (screen.width - 670) / 2;
  var y = ((screen.height - 480) / 2) - 32;
  var win = window.open(pagina,nome,'dependent,status,menubar,scrollbars,resizable,top='+y+',left='+x+',width=670,height=480');
  if (win.opener!=window) {
    win.opener = window;
  }
  win.focus();
  return win;
}

function navigateWindow(win,url) {
  win.location.href = new String(url);
}

function navigate(url) {
  window.location.href = new String(url);
}



//-------------------------------------- AJAX --------------------------------------//
var xmlHttp = null;
var callHttpQueue = [];
var callHttpQueueCount = 0;
function callHttpRun() {
  var values = callHttpQueue[callHttpQueueCount];

  xmlHttp.open("POST",values[0],true);
  xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  xmlHttp.onreadystatechange = function() {
                                 if (xmlHttp.readyState==4) {
                                   var values = callHttpQueue[callHttpQueueCount];

                                   // IMPLEMENTAR TRATAMENTO DE EXCEPTIONS

                                   if (values[1]!=null) {
                                     try { values[1](xmlHttp.responseXML,values[3]); } catch(e) { }
                                   }

                                   callHttpQueueCount++;
                                   if (callHttpQueueCount<callHttpQueue.length)
                                     setTimeout("callHttpRun()",50);
                                 }
                               };
  var params = "";
  if (values[2]!=null) {
    for (var i=0; i<values[2].length; i++) {
      if (i>0) params = params + "&";
      params = params + values[2][i] + "=" + values[3][i];
    }
  }

  xmlHttp.send(params);
}
function callHttp(uri,callback,params,values) {
  if (xmlHttp==null) {
    try {
      xmlHttp = new XMLHttpRequest();
    } catch(ee) {
      try {
        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
      } catch(e) {
        try {
          xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch(E) {
          xmlHttp = null;
          alert("Seu navegador não suporta a leitura de dados remotos.");
        }
      }
    }
  }

  if (xmlHttp!=null) {
    callHttpQueue[callHttpQueue.length] = [uri,callback,params,values];

    if ((callHttpQueueCount + 1)==callHttpQueue.length)
      callHttpRun();
  }
}
