// *************************************************************
// Printa conteúdo no layer indicado
// *************************************************************
function writetolayer(lay,txt) {
	var ie4 = (document.all) ? true : false;
	var ns4 = (document.layers) ? true : false;
	var ns6 = (document.getElementById && !document.all) ? true : false;
	if (ie4) {
		document.all[lay].innerHTML = txt;
	}
	if (ns4) {
		document[lay].document.write(txt);
		document[lay].document.close();
	}
	if (ns6) {
		over = document.getElementById([lay]);
		range = document.createRange();
		range.setStartBefore(over);
		domfrag = range.createContextualFragment(txt);
		while (over.hasChildNodes()) {
			over.removeChild(over.lastChild);
		}
		over.appendChild(domfrag);
   }
}

// *************************************************************
// Verifica se a resolução solicitada é válida ou não
// *************************************************************
function CheckMinScreenResolution(w, h) {
  if (screen.width >= w && screen.height >= h) {
    return true;
  } else {
    return false;
  }
}



// *************************************************************
// Permite apenas o uso de teclas numéricas
// *************************************************************
function IntegerMask(e) {
   var charCode = (navigator.appName == "Netscape") ? e.which : e.keyCode;
   if (charCode > 31 && (charCode < 48 || charCode > 57))
      return false;
   return true;
}



// *************************************************************
// Dá um submit num formulário através da tecla Enter
// *************************************************************
function enter4submit(e) {
  if (e.keyCode == 13) {
    SendMessage();
    return false;
  } else {
    return true;
  }
}



// *************************************************************
// Limpa todos os itens de um combobox
// *************************************************************
function ClearCombo(combo) {
  var i;
  for (i = (combo.length - 1); i >= 0; i--) {
    combo.options[i] = null;
  }
}



// *************************************************************
// Adiciona um item ao combobox
// *************************************************************
function AddItemCombo(combo, texto, valor) {
  combo.options[combo.length] = new Option(texto, valor);
}



// *************************************************************
// Abre uma janela popup, que pode ser centralizada ou não
// *************************************************************
function OpenWindow(url, winname, nWidth, nHeight, left, top, status, scrollbars) {
  var nTop, nLeft, janela, features;

  if (left != -1) nLeft = left
  else nLeft  = (screen.availWidth / 2) - (nWidth / 2);

  if (top != -1) nTop = top
  else nTop   = (screen.availHeight / 2) - (nHeight / 2);
  
  features = '';
  if (status == true)     features = features + 'status=yes, ';
  if (scrollbars == true) features = features + 'scrollbars=yes, ';

  janela = window.open(url, winname, features + 'resizable=no, width=' + nWidth + ', height=' + nHeight + ', left=' + nLeft + ', top=' + nTop);
  janela.focus();
}



// *************************************************************
// Verifica se os campos required estão preenchidos.
// *************************************************************
function CheckRequired(formulario) {
  var retorno = true;
  
  for (i = 0; i < formulario.elements.length; i++) {
    if ((formulario.elements[i].required == 'true') && (retorno == true)) {
      // Verifica campos texto
      if (formulario.elements[i].type == 'text' || formulario.elements[i].type == 'password' || formulario.elements[i].type == 'textarea') {
	    if (formulario.elements[i].value == '') {
	      alert('O campo ' + formulario.elements[i].alias + ' deve ser preenchido!');
		  formulario.elements[i].focus();
		  retorno = false;
	    }
	  }
	  // Verifica campos combobox
	  if (formulario.elements[i].type == 'select-one') {
	    retorno = CheckCombo(formulario.elements[i]);
	  }
	}
  }

  return retorno;
}



// *************************************************************
// Verifica se o combo possui algum valor selecionado.
// *************************************************************
function CheckCombo(combo) {
  var retorno = true;
  if (combo[combo.selectedIndex].value == '0') {
    alert('Selecione algum valor para o campo ' + combo.alias + '.');
	combo.focus();
	retorno = false;
  }
  
  return retorno;
}



// *************************************************************
// Verifica se o radio tem algum valor selecionado.
// *************************************************************
function CheckRadio(radio) {
  var retorno = false;
  if (radio.length > 1) {
    for (i = 0; i < radio.length; i++) {
      if ((radio[i].checked) && (retorno == false)) {
	    retorno = true;
      }
    }
  } else {
    if ((radio.checked) && (retorno == false)) {
      retorno = true;
    }
  }
  
  if (retorno == false) {
    if (radio.length > 1) {
      alert('Selecione uma das opções para o campo ' + radio[0].alias + '.')
	  radio[0].focus();
	} else {
      alert('Selecione uma das opções para o campo ' + radio.alias + '.')
	  radio.focus();
	}
  }
  
  return retorno;
}



// *************************************************************
// Retorna o valor do radio selecionado.
// *************************************************************
function ValueRadio(radio) {
  var retorno = "";
  for (i = 0; i < radio.length; i++) {
    if ((radio[i].checked) && (retorno == "")) {
      retorno = radio[i].value;
    }
  }
  
  return retorno;
}



// *************************************************************
// Abre a janela do chat
// *************************************************************
function OpenChat(isadmin, chat_id) {
  var url = '/onlinev2/chat/init.asp';
  var win = 'chat';
  
  // Verifica se o usuário é adm ou não
  if (!isadmin) {
    url = '/onlinev2/chat/init.asp?chat_id=' + chat_id;
	win = 'chat';
  } else {
    url = '/onlinev2/chat/init.asp?isadmin=true&chat_id=' + chat_id;
	win = 'chat';
  }
  
  OpenWindow(url, win, 400, 450, -1, -1, true, false);
}



// *************************************************************
// Abre a janela que exibe o resultado da enquete
// *************************************************************
function ShowEnqueteResult(enquete) {
  OpenWindow('/onlinev2/include/enquete/mostra_resultado.asp?registro_id=' + enquete, 'result', 350, 250, -1, -1, false, false);
}



// *************************************************************
// Envia os dados de resposta da enquete
// *************************************************************
function AnswerEnquete(enquete, alternativa) {
  if (CheckRadio(document.frmEnquete.rbtEnquete)) {
    OpenWindow('/onlinev2/include/enquete/responde.asp?master_id=' + enquete + '&registro_id=' + alternativa, 'result', 350, 250, -1, -1, false, false);
  } 
}