function ltrim ( s )  { 
   return s.replace( /^\s*/, "" ) 
} 

function rtrim ( s )  { 
   return s.replace( /\s*$/, "" ); 
} 


function trim ( s ) { 
   return rtrim(ltrim(s)); 
} 

function testaVazio(elemento, texto) {
  str = trim(elemento.value);
  if ((str=="") || (str.length==0) || (str=="Null")) {
	alert("Por favor, informe "+ texto + ".");
	elemento.focus();
	return false;
  }
  return true;
}

function testaEmail(elemento, texto) {
  str = trim(elemento.value);
  if(!testaVazio(elemento," um e-mail válido")) return false;
  var mstr = str.split("@");
  if( mstr.length <= 1 ){
     alert("Por favor, informe "+ texto + ".");
     return false;
 }  else {
   var mstr2 = mstr[1].split(".");
   if ( mstr2.length <= 1 ) {
     alert("Por favor, informe "+ texto + ".");
     return false;
   } else {
      var sufx = mstr2[1]
      if (sufx.length <= 1){
       alert("Por favor, informe "+ texto + ".");
       return false;
      }
   }
 }
  return true;
}

function testaTamanho(elemento, tam, texto) {
  str = elemento.value;
  if (str.length>tam) {
	alert(texto + " deve possuir no máximo " + tam +" caracteres.");
	elemento.focus();
	return false;
  }
  return true;
}

function validaTipoArquivo(objfile, extensions){
	var strfile = new String(objfile.value.toLowerCase());
	var arrext  = extensions.split(",");
	var test    = false;
	strfile     = strfile.substr((strfile.length-3), 3)
	for (i=0;i<arrext.length;i++){
		if ( parseInt( strfile.search(arrext[i]) ) != -1){
			test = true;
			break;
		}   
	}
	return test;
}

function validafoto(objfile){
	var test = true;
	if (objfile.value.length > 0) {
		test = false
		if (!validaTipoArquivo(objfile,"gif,jpg,peg")){
			alert("O arquivo selecionado não é uma imagem válida.");
		}else{
			test = true;  
		}
	} 
	return test;
}

function validaTipoArquivo(objfile, extensions){
   var strfile = new String(objfile.value.toLowerCase());
   var arrext  = extensions.split(",");
   var test    = false;
   strfile     = strfile.substr((strfile.length-3), 3)
   //alert(strfile)
   for (i=0;i<arrext.length;i++){
   	   //alert(arrext[i].length)
       if ( parseInt( strfile.search(arrext[i]) ) != -1){
         test = true;
		 //alert(parseInt( strfile.search(arrext[i]) ))
         break;
       }   
   }

   return test;
}

function validaarquivo(objfile){
	var test = true;
	if (objfile.value.length > 0) {
	  test = false
	  
		  if (validaTipoArquivo(objfile,"exe,com,bat,reg,vbs,pif,.js")){
		     //objfile.value = "";
		     alert("Por questão de segurança, não é permitido enviar arquivos executáveis.");
			 //objfile.focus()
		  }else{
		     test = true;  
		  }
	 }  
  return test;
}

function validaRadio(form,elemento,texto) {
	  var existe = false;
	  for (i=0; i<form.length;i++) {
		if (form.elements[i].name == elemento) {
		  if (form.elements[i].checked) {
		    existe=true;
		  }
		}
	  }
	  if (!existe) {
		alert('Selecione ao menos '+texto+'');
		return false;
	  }else {
	  	return true
	  }
}

//Formata o valor apos sair do campo
function ocMoeda(valor){
	if (valor.value.length != 0) {
		if((valor.value.indexOf(".") == -1) && (valor.value.indexOf(",") == -1)) valor.value = valor.value+",00";
		if (valor.value.indexOf(",") > -1){
			if (valor.value.indexOf(",") == (valor.value.length-1)) valor.value =  valor.value+"00";
			if (valor.value.indexOf(",") == (valor.value.length-2)) valor.value = valor.value+"0";
		}
		//Função que arredonda - Zeca - 
		if (valor.value.length > valor.value.indexOf(',')+3){
			valor.value = valor.value.substring(0,valor.value.indexOf(',')+1) + Math.round(valor.value.substring(valor.value.indexOf(",")+1, valor.value.indexOf(",")+3)+'.'+valor.value.substring(valor.value.indexOf(",")+4,valor.value.length ));
		}
	}
}


function testaCheck(campo, mensagem){
	var conta = 0;
	if (typeof(campo.length)!="undefined"){
		for (var i=0; i<campo.length; i++) {
			if (campo[i].checked) {
				if (campo[i].value != '') {conta++}
			}
		}
	}else{
		if (campo.checked) {conta++;}
	}
	if (conta == 0) {
		alert(mensagem);
		return false;		
	} else {
		return true;
	}
}
