// Popula os campos do formulário utilizando um objeto JSON
function populaForm(json) {
	if (json) {
      for (name in json) {
         //alert(name + ' = ' + json[name]);
         if (json[name]) populaCampo(name, json[name]);
      }
   }
}

// Função para popular um campo independente do tipo de INPUT
function populaCampo(campo, valor) {
   var obj = document.getElementById(campo);
   
   if (obj == null) return;
   if (valor == null) return;
   
   var nome = obj.tagName;
   var tipo = obj.getAttribute('type');

   // Se for do tipo TEXT
   if (tipo == 'text') {
      obj.value = valor;
   }
	
   // Se for do tipo SELECT
   if (tipo == 'select-one') {
      obj.value = valor;
	   if (obj.onchange) obj.onchange();
   }

   // Se for do tipo RADIO
   if (tipo == 'radio') {
      valores = document.getElementsByName(campo);
      for (i=0; i < valores.length; i++) {
         if (valores[i].value == valor) valores[i].checked = true;
      }
   }
   
   // Se for do tipo CHECK
   if (tipo == 'checkbox') {
      if (valor == 1) 
         obj.checked = true
      else 
         obj.checked = false;
   }
}

function delay(millis) 
{
   var date = new Date();
   var curDate = null;

   do { curDate = new Date(); } 
   while(curDate-date < millis);
} 

// Abre um janela modal
function openModalWindow(strURL, strArgument, intWidth, intHeight) {
   var intTop  = ((screen.height - intHeight) / 2);
   var intLeft = ((screen.width - intWidth) / 2);
   var strEnderec=strURL;
   var strAjustesIE='status=0; help=0; center:yes; dialogWidth:'+intWidth+'px; dialogHeight:'+intHeight+'px';
   var strAjustesNS='width='+intWidth+', height='+intHeight+', status=0, scrollbars=1, menubar=0, dependent=1, left='+intLeft+', top='+intTop;

   with (window.navigator) {
      switch (appName) {
         case 'Microsoft Internet Explorer':
            var x = window.showModalDialog(strEnderec, strArgument, strAjustesIE);
            break;
         case 'Netscape':
            var x = window.open(strEnderec, 'Default', strAjustesNS);
            break;
      }
   }
}

// Função para abrir janela padrão
function openr(pagina, frame) {
   if (screen.width == 800 || screen.width == 1024)
   {
      janela=window.open(pagina, frame, "width=355, height=100, resizable=no, scrollbars=no, left=0, top=0")
      janela.focus()
   }
   else
   {
      janela=window.open(pagina, frame, "width=355, height=100, resizable=no, scrollbars=no, left=0, top=0")
      janela.focus()
   }
}

// Função para abrir janela com tamanho definido
function opend(pagina, frame, w, h, barras) {
   janela=window.open(pagina, frame, "width=" + w + ",height=" + h + ",status=no,toolbar=no,resizable=yes,scrollbars=" + barras + ",left=0,top=0")
   janela.focus()
}

function openn(pagina, frame) {
   janela=window.open(pagina, frame, "resizable=yes, scrollbars=yes, toolbar=yes, status=no, left=0, top=0")
   janela.focus()
}

// Modifica a cor das linhas das tabelas
function Move(obj){
	obj.style.backgroundColor = '#EEEEEE';
}

// Modifica a cor das linhas das tabelas
function Mout(obj){
	obj.style.backgroundColor = '#FFFFFF';
}
