/*
 * Funções básicas de javascript
 * @author Marison Souza Gomes
 * Copyright Maven IT Solutions 2006	
*/
 
 // Abre uma nova janela personalizada
function abreJanela(url,w,h){
  	if(w==null || w=='') w='800';
  	if(h==null || h=='') h='600';  	
    window.open(url,'4','width='+w+',height='+h+',toolbar=no,statusbar=no,scrollbars=yes,resizable=yes');
}
  
function trim(sString){
	while (sString.substring(0,1) == ' '){
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' '){
		sString = sString.substring(0,sString.length-1);
	}
		return sString;
}

// verifica se existe algum checkbox marcado
function verificaCheckbox(check) {

 var existe=false;
 for(var i=0;i<check.length;i++) {
 	if(check[i].checked) {
 		existe=true;
 	}
 }
 if(check.checked) {
 	existe=true;
 }
	return existe;

}

// verifica se um campo esta vazio ou não existe
function verificaCampoVazio(campo) {	
  return (campo==null || campo.value==null || trim(campo.value).length<=0 || campo.value=='');
}

// verifica se tem um dado válido em uma combo existente
function verificaComboVazio(combo) {

  return (combo==null  || combo.options==null || combo.selectedIndex==-1
          || combo.options[combo.selectedIndex]==null ||
          combo.options[combo.selectedIndex].value==null || 
            combo.options[combo.selectedIndex].value.length<=0);
}

function verificaTamanhoTexto(campo,max,nullable){
	if(!nullable){
		if(verificaCampoVazio(campo)) return true;
	}
	return campo.value.length>max;
}

function doAction(codigo,act){
	with(document.actionsForm){
		action="./"+act;
		itemId.value=codigo;
		showMouseWaiting(true);
		submit();
	}
}

function goTo(url){	
	window.location=url;
}

function criarParamHidden(frm,name,valor) {
    try {
        if (document.forms) {
                var stamp = document.createElement("INPUT");	
                stamp.name = name;
                stamp.type = "hidden";
                stamp.value = valor;
                frm.appendChild(stamp);            
        }
    }catch (e) {}
}

function getRange(){
	var range = null;
	if (window.getSelection)	
		range = window.getSelection();	
	else if (document.getSelection)	
		range = document.getSelection();	
	else if (document.selection)	
		range = document.selection.createRange();

	return range;
} 

function getSel()
{
	var txt = '';
	var foundIn = '';
	if (window.getSelection)	
		txt = window.getSelection();	
	else if (document.getSelection)	
		txt = document.getSelection();	
	else if (document.selection)	
		txt = document.selection.createRange().text;	
	else return;
	return txt;
}

function numero(evtKeyPress) {
      var nTecla = 0;
      if (document.all) {
  	    nTecla = evtKeyPress.keyCode;
      } else {
    	  nTecla = evtKeyPress.which;
      }
  return ((nTecla> 47 && nTecla <58) || nTecla == 8 || nTecla == 127 || nTecla == 0 || nTecla == 9 || nTecla == 13);
}

function numeroBlur(field){
	if(isNaN(field.value)){
		alert('Você informou um valor não numérico para esse campo');
		field.value="";
	}else{
		return true;
	}
}