function enableButton(id) {
	var obj = document.getElementById(id)
	//if (obj && obj.nodeName == "INPUT" && obj.type == "submit")
	if (obj)
		obj.disabled = false;
}

function move(fbox,tbox) {

   tbox.disabled = true;
   fbox.disabled = true;

   for (var i = 0; i < fbox.options.length; i++) {
      if (fbox.options[i].selected && fbox.options[i].value != "") {
         var option = fbox.options[i];
         fbox.remove(i);
         tbox.options[tbox.options.length] = option;
         i--;

      }
   }
   tbox.disabled = false;
   fbox.disabled = false;

   enableButton('bSave');

   // 7/22/2004 wrong even with quicksort, javascript just cannot handle the load.
   // Sorting 2000 simple string causes a browser dialog popped up.  Sorting complex
   // object (Option element) simply takes too long.  Removed sorting.
}

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 removeWorkAroundText(box) {
  box.options[box.options.length - 1] = null;
}


function SortD(box)  {
  var temp_opts = new Array();
  var temp = 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;
        temp_opts[x].text = temp_opts[y].text;
        temp_opts[y].text = temp;
        temp = temp_opts[x].value;
        temp_opts[x].value = temp_opts[y].value;
        temp_opts[y].value = temp;
      }
    }
  }
  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 selectAll(tbox) {
  for (var i = 0; i < tbox.options.length; i++) {
    tbox.options[i].selected = true;
  }
}

// this is the generic function to check whether the specified field
// has a value or not.
function validateRequired(which, message) {
    // if (which && document.layers || document.all) {
   if (which) {
      if (which.value != "") {
          return(true);
      } else {
          alert("Please input in the required field: " + message);
          which.focus();
          return(false);
      }
   }
   // }
    return(true);
}


function validateName(textstr, fieldName) {
  var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
  var name = textstr.value;
  for (var i = 0; i < name.length; i++) {
    if (valid.indexOf(name.substr(i, 1)) == -1) {
      alert("You have entered an invalid " + fieldName + ".\nValid characters include a-z, A-Z, 0-9, '-', and '_'.");
      return false;
    }
  }
  return true;
}


// validate textarea - limit is 1023 characters
function validateDesc(textstr) {
  var textval = textstr.value
  if (textval.length > 1023) {
    alert("Group description you have entered is not valid.  Please re-enter with less than 1024 characters.");
    return false;
  }

  return true;
}


// free functions from netscape for checking email format
var defaultEmptyOK = false;
var whitespace = " \t\n\r";

function isEmpty(s) {
	return ((s == null) || (s.length == 0))
}

// Returns true if string s is empty or
// whitespace characters only.
function isWhitespace (s) {
	var i;
	// Is s empty?
	if (isEmpty(s)) return true;
	// Search through string's characters one by one
	// until we find a non-whitespace character.
	// When we do, return false; if we don't, return true.
	for (i = 0; i < s.length; i++) {
		// Check that current character isn't whitespace.
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1) return false;
	}
	// All characters are whitespace.
	return true;
}

function isEmail (s) {
	if (isEmpty(s))
	if (isEmail.arguments.length == 1) return defaultEmptyOK;
	else return (isEmail.arguments[1] == true);
	// is s whitespace?
	if (isWhitespace(s)) return false;
	// there must be >= 1 character before @, so we
	// start looking at character position 1
	// (i.e. second character)
	var i = 1;
	var sLength = s.length;
	// look for @
	while ((i < sLength) && (s.charAt(i) != "@")) { i++ }
	if ((i >= sLength) || (s.charAt(i) != "@")) return false;
	else i += 2;
	// look for .
	while ((i < sLength) && (s.charAt(i) != ".")) { i++ }
	// there must be at least one character after the .
	if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
	else return true;
}

function abrir(direccion, barradirectorios, pantallacompleta, herramientas, direcciones, estado, barramenu, barrascroll, cambiatamano, ancho, alto, sustituir){

	posLeft = ((screen.width - ancho) / 2);
	posTop = ((screen.height - alto) / 2);

	var opciones = "directories=" + barradirectorios +
		",fullscreen=" + pantallacompleta +
		",toolbar=" + herramientas +
		",location=" + direcciones +
		",status=" + estado +
		",menubar=" + barramenu +
		",scrollbars=" + barrascroll +
		",resizable=" + cambiatamano +
		",width=" + ancho +
		",height=" + alto +
		",left=" + posLeft +
		",top=" + posTop;

	var ventana = window.open(direccion,'','opciones','sustituir');
	ventana.resizeTo(ancho, alto);
	ventana.moveTo(posLeft, posTop);
	ventana.focus();
}

// Abre una imagen de las curvas y tablas de crecimiento
function abrirImagenTabla(imagen){
	var ventana=window.open(imagen,'','scrollbars=1,width=630,height=700','1');
	ventana.focus();
}

// Redondear a nDec decimales
function redondea(sVal, nDec){
	var n = parseFloat(sVal);
	var s = "0.00";
	if (!isNaN(n)){
		n = Math.round(n * Math.pow(10, nDec)) / Math.pow(10, nDec);
		s = String(n);
		s += (s.indexOf(".") == -1? ".": "") + String(Math.pow(10, nDec)).substr(1);
		s = s.substr(0, s.indexOf(".") + nDec + 1);
	}
	return s;
}

//Calcular el IMC
function calcular_imc(){
	var peso=document.getElementsByName("form_peso")[0].value;
	var talla=document.getElementsByName("form_talla")[0].value;

	if(peso>0 && talla>0){
		document.getElementsByName("form_imc")[0].value=redondea(peso/(Math.pow(talla/100,2)),2);
	}
}

// Pedir confirmacion para eliminar la revision
function confirmar(id){
	if (confirm("Realmente desea eliminar la revision '" + id + "'?")){
		alert("Eliminando la revision " + id + " ...");
		return true;
	}
	else{
		alert("Revision " + id + " NO eliminada.");
		return false;
	}
}

// Pedir confirmacion para eliminar la prenda
function confirmar_prenda(id){
	if (confirm("Realmente desea eliminar la prenda con ID='" + id + "'?")){
		alert("Eliminando la prenda " + id + " ...");
		return true;
	}
	else{
		alert("Prenda " + id + " NO eliminada.");
		return false;
	}
}

// Pedir confirmacion para eliminar el episodio
function confirmar_episodio(id){
	if (confirm("Realmente desea eliminar el episodio '" + id + "'?")){
		alert("Eliminando el episodio " + id + " ...");
		return true;
	}
	else{
		alert("Episodio " + id + " NO eliminado.");
		return false;
	}
}
