function changeInputColor(control){
  control.style.backgroundColor = '#FFFFFF';
}

function putWrongBackground(control){
  control.style.background = '#FDCD11';
}

function checkFields(){
  var idsToCheck = new Array('requirednombre', 'requiredemail', 'requiredtelefono');
  var idsInteger = new Array('requiredtelefono');
  return checkEmpty(idsToCheck) && checkIntegers(idsInteger);
}

function checkEmpty(idsToCheck){
  var control;
  var b = false;
  var i = 0;
  for (i = 0; i < idsToCheck.length; i++){
    control = document.getElementById(idsToCheck[i]);
    if (control != null){
      if ((control.type == 'select-one' && control.selectedIndex == 0)
        || ((control.type == 'text' || control.type == 'textarea') && control.value.length == 0)){
        b = true;
        putWrongBackground(control);
      }
    }
  }
  if (b){
    alert('Los campos marcados son obligatorios');
  }
  return !b;
}

function checkIntegers(ids){
  var bWrong = false;
  for (i = 0; i < ids.length; i++){
    var bAux = checkIsInteger(ids[i]);
    if (!bWrong)
      bWrong = !bAux;
  }
  if (bWrong){
    alert('Los campos marcados tienen que ser num\xe9ricos');
  }
  return !bWrong;
}

function checkIsInteger(id){
  var bWrong = false;
  var tbx = document.getElementById(id);
  if (tbx != null){
    if (!isInteger(tbx.value) || tbx.value < 0){
      putWrongBackground(tbx);
      bWrong = true;
    }
  }
  return !bWrong;
}

function isInteger (s){
  var i;
  if (isEmpty(s))
    if (isInteger.arguments.length == 1)
      return 0;
    else
      return (isInteger.arguments[1] == true);

  for (i = 0; i < s.length; i++){
    var c = s.charAt(i);
    if (!isDigit(c))
      return false;
  }

  return true;
}

function isEmpty(s){
  return ((s == null) || (s.length == 0))
}

function isDigit (c){
  return ((c >= "0") && (c <= "9"))
}

function checkLength(id,length){
  var control = document.getElementById(id);
  var b = false;
  if (control.value.length == length)
    b = true;
  if (!b){
    putWrongBackground(control);
    alert('El campo marcado debe tener una longitud de ' + length + ' caracteres');
  }
  return b;
}
