sortitems = 1;  // Automatically sort items within lists? (1 or 0)

function move(fbox,tbox) {
	for (var i=0; i<fbox.options.length; i++) {
		if (fbox.options[i].selected && fbox.options[i].value != "") {
			var no = new Option();
			no.value = fbox.options[i].value;
			no.text = fbox.options[i].text;
			tbox.options[tbox.options.length] = no;
			fbox.options[i].value = "";
			fbox.options[i].text = "";
		}
	}
	BumpUp(fbox);
	if (sortitems) SortD(tbox);
    if (fbox.options.length-1>0){
        fbox.options[0].selected=true;
    }
}

function BumpUp(box)  {
	for (var i=0; i<box.options.length; i++) {
		if (box.options[i].value == "")  {
			for (var j=i; j<box.options.length-1; j++)  {
				box.options[j].value = box.options[j+1].value;
				box.options[j].text = box.options[j+1].text;
			}
			var ln = i;
			break;
		}
	}
	if (ln < box.options.length)  {
		box.options.length -= 1;
		BumpUp(box);
	}
}



function SortD(box)  {
	var temp_opts = new Array();
	var temp = new Object();
	var valor = new Object();
	for (var i=0; i<box.options.length; i++)  {
		temp_opts[i] = box.options[i];
	}
	for (var x=0; x<temp_opts.length-1; x++)  {
		for (var y=(x+1); y<temp_opts.length; y++)  {
			if (temp_opts[x].text > temp_opts[y].text)  {
				temp = temp_opts[x].text;
				valor = temp_opts[x].value;
				temp_opts[x].text = temp_opts[y].text;
				temp_opts[x].value = temp_opts[y].value;
				temp_opts[y].text  = temp;
				temp_opts[y].value = valor;
		    }
		}
	}
	for (var i=0; i<box.options.length; i++)  {
		box.options[i].value = temp_opts[i].value;
		box.options[i].text = temp_opts[i].text;
	}
}


function selecionaLista(box){
	for (var i=0; i<box.options.length; i++) {
		box.options[i].selected = true;
	}
}


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("Informe "+ texto + ".");
	elemento.focus();
	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 testaVazioCombo(elemento,texto){
  str = elemento.options[elemento.selectedIndex].value;
  if ((str=="") || (str.length==0) || (str=="Null")) {
	alert("Informe "+ texto + ".");
	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)
   //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 validafoto(objfile){
	var test = true;
	if (objfile.value.length > 0) {
	  test = false
	
	  if (!validaTipoArquivo(objfile,"gif,jpg,peg")){
	     //objfile.value = "";
	     alert("O arquivo selecionado não é uma imagem válida.");
		 //objfile.focus()
	  }else{
	     test = true;  
	  }
	} 
	  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 '+texto+'.');
		return false;
	  }else {
	  	return true
	  }
}

var numerosValidos = "0123456789,";

function testaNumero(campo, mensagem){
	valor = campo.value;
	for(i = 0; i < valor.length; i++){
		if(numerosValidos.indexOf(valor.charAt(i)) == -1){
			alert("O campo "+mensagem+" só deve conter números.");
			campo.focus();
			return false;
		}
	}
	return true;
}