/*
 * 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';  	
  	var randomnumber=Math.floor(Math.random()*11);
    window.open(url,randomnumber,'width='+w+',height='+h+',toolbar=no,statusbar=no,scrollbars=yes,resizable=yes');
  }
  
function PopupImagem(ImagemURL) {
window.open( "conteudo/verFoto.jsp?foto="+ImagemURL, "", "resizable=1,HEIGHT=200,WIDTH=200,scrollbars=1,top=0,left=0");
   }


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;

}

function estaVazio(field){
try{
	if(field){
		var type = field.type;		
		if(type == 'text') return verificaCampoVazio(field);
		if(type == 'select-one') return verificaComboVazio(field);		
		return verificaCampoVazio(field);
	}else{
		return true;
	}
 }catch(e){
 	return true;
 }
}

// 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 getElementbyClass(classname){
	var inc=0
	var alltags=document.all? document.all : document.getElementsByTagName("*");
	customcollection=new Array();
	for (i=0; i<alltags.length; i++){
		if (alltags[i].className==classname){		
			customcollection[inc++]=alltags[i]
		}
	}
	return customcollection;	
}


function truncTextArea(obj, num) {
    var re = /\n/g;
    var rec = obj.value;
    var newrec = rec.replace(re, "");
    //alert(obj.value.length+" - "+newrec.length);
    re = /\r/g;
    newrec = newrec.replace(re, "");
    //alert(obj.value.length+" - "+newrec.length);
    if(newrec.length > num) {
       obj.value = newrec.substring(0, num);
       return false;
    }
        return true;
}
function go(url) {
	window.location.href=url;	
}

function formatar(src, mask){ //funcao para formatar qualquer campo.Ex.:cep,cpf,telefone,cnpj.
  document.onkeydown = checkKeycode
  function checkKeycode(e) {
   var keycode;
   if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
     if(keycode != 8){
      
      var i = src.value.length; 
      var saida = mask.substring(0,1); 
      var texto = mask.substring(i); 
     if (texto.substring(0,1) != saida){ 
     	src.value += texto.substring(0,1); 
     }
   }
  }
}
