//------------------------------------------------------
// testa se um campo de edição está vazio, se um grupo
// radio não foi marcado, se um checkbox não foi marcado
// ou se não foi selecionado valor em uma seleção
//------------------------------------------------------
function isEmpty(formulario,campo) {
	obj = _getObject(formulario,campo);
	if (!obj) return false;
	
	if ((obj.type == "text") || (obj.type == "hidden") || (obj.type == "password") || (obj.type == "textarea") || (obj.type == "file"))	{
		if (trim(obj.value) == "") return true;
		else return false;
	}
	else if (obj.type == "select-one" || obj.type == "select-multiple") {
		return checkSelect(obj);
	}
	else if(obj.type == 'checkbox') {
		if(obj.checked == true) return false;
	} else {
		return checkRadio(formulario,campo);
	}

	return true;
}

//------------------------------------------------------
// testa se uma caixa de seleção possui valores inseridos
// além do texto 'Primeira Opção' (se ele existir)
//------------------------------------------------------
function isSelectEmpty(formulario,campo) {
	obj = _getObject(formulario,campo);
	var minimo = (obj.options[0].value == "-1") ? 2 : 1;
	if (obj.length < minimo) {
		return true;
	}
	else {
		return false;
	}
}

//------------------------------------------------------
// retorna um elemento a partir do formulário e do campo
//------------------------------------------------------
function _getObject(formulario,campo) {
	return eval("document."+formulario+"."+campo);
}

//------------------------------------------------------
// verifica se foi selecionado um valor na seleção
//------------------------------------------------------
function checkSelect(campo) {
  for (i=0;i<campo.options.length;i++) {
    if ((campo.options[i].selected == true) && (campo.options[i].value != "") && (campo.options[i].value != -1))
      return false;
  }
  return true;
}

//------------------------------------------------------
// verifica se o grupo radio possui um valor selecionado
//------------------------------------------------------
function checkRadio(formulario,campo) {
	radio_button = eval("document."+formulario+"."+campo);
	for (i = 0; i < radio_button.length; i++) {
		if (radio_button[i].checked == true) return false;
	}
	return true;
}

//------------------------------------------------------
// elimina os espaços em branco duplos, à esquerda e à
// direita da string passada como parâmetro
//------------------------------------------------------
function trim (str) {
  var string="";
  var achabranco;
  var proxbranco;
  var semduplos = 1;
  str = String(str);
  if (str.length >= 1)
  {
    while (semduplos == 1)
    {
      for (var i=0;i<str.length;i++)
      {
        achabranco = str.charAt(i);
        if (achabranco == " ")
        {
          proxbranco = str.charAt(i+1);
          if (proxbranco == " ")
          {
            str = replace_string(str," ","",i+1);
          }
        }
        semduplos = 0;
      }
      for (var i=0;i<str.length;i++)
      {
        if (str.length == 1)
          break;
        achabranco = str.charAt(i);
        if (achabranco == " ")
        {
          proxbranco = str.charAt(i+1);
          if (proxbranco == " ")
            semduplos = 1;
        }
      }
    }
  }
  if ((str == " ") || ((str.length == 1) && (str.charCodeAt(0) == 10)))
    return "";
  else
    return str;
}

function getFront(main,search,posbranco) {
  foundOffset = posbranco
  if (foundOffset == -1){
    return null
  }
  return main.substring(0,foundOffset)
}

function getEnd(main,search,posbranco) {
  foundOffset = posbranco
  if(foundOffset == -1){
    return null
  }
  return main.substring(foundOffset+search.length,main.length )
}

//------------------------------------------------------
// substitui espaços brancos múltiplos por simples
//------------------------------------------------------
function replace_string(main,search,replace,posbranco) {
  front = getFront(main,search,posbranco);
  end = getEnd(main,search,posbranco);
  if (front != null && end != null){
    return front + replace + end;
  }
  return main;
}

String.prototype.trim=function(){
     return this.replace(/^\s+|\s+$/gm,'');
}