var matriz = new Array();
var i=0;

function nuevo_campo (nombre,campo,nulo,tipo){
		matriz[i]=new Array(nombre,campo,nulo,tipo);
		i++;
}

function evaluar_campo(campo,tipo,siguiente) {
  var sintaxisEmail = new RegExp ("^[a-z0-9_.-]+@{1}[a-z0-9_.-]+[\.]{1}[a-z0-9_.-]+$","i");
  var sintaxisTelefono = new RegExp ("^[0-9]{9,}$");
  var sintaxisCP = new RegExp ("^[0-9]{5}$");
  var sintaxisDNI = new RegExp ("^[0-9]{8}[a-zA-Z]{1}$");
  var sintaxisCIF = new RegExp ("^[a-zA-Z][\-]{1}[0-9]{8}$");
  var sintaxisNumero = new RegExp ("^[0-9]+$");
  var sintaxisNoCero = new RegExp ("^[1-9]+$");
  switch (tipo) {     
    case 'pass': 
      if (campo.value != siguiente.value)
      {
        alert("Las contraseñas no coinciden. Escríbalas de nuevo...")
        campo.value = "";
        siguiente.value = "";
        campo.select()
      }
      if ((campo.value.length < 4) || (campo.value.length > 12))
      {
        alert("Las contraseñas deben tener entre 4 y 12 caracteres.\nEscríbalas de nuevo...")
        campo.value = "";
        siguiente.value = "";
        campo.select()
        return false
      }break;
    case 'email':
      if (campo.value != ''){
      if (campo.value.search(sintaxisEmail) == -1)
      {
        alert("Debe introducir un email válido...")
        campo.select()
        return false
      }}break;    
  }
  return true
}// Fin funcion 'evaluar_campo'

function ReemplazaTextArea(f,campo){
    var api = FCKeditorAPI.GetInstance(campo);    
    eval('f.'+campo).value = api.GetHTML();
}

function validar(f,matriz,operacion) {
	
var longitud = matriz.length;
var haEntrado = false;
for (i=0;i<longitud;i++) {

  var nombre = matriz[i][0];
  var nombrecampo = matriz[i][1];
  var campo = eval('f.'+matriz[i][1]);
  var nulo = matriz[i][2];
  var tipo = matriz[i][3]; 

  if (tipo == 'textarea'){
    if (operacion == ''){
      if (campo.text == nulo){
        textoalert = "El campo "+nombre+" debe tener un valor";
	haEntrado = true;
	i = longitud;
      }
    }else{
      ReemplazaTextArea(f,nombrecampo);
    }
  } 
  if (campo.value == nulo){
    textoalert = "El campo "+nombre+" debe tener un valor";
    haEntrado = true;
    i = longitud;  
  }
}
if (haEntrado){
  alert(textoalert);
  return false
}else{
  if (operacion == ''){
    texto = '¿Está seguro de enviar el formulario?';
  }else{
      if (operacion == 'insertar'){
          texto = '¿Está seguro de crear un nuevo registro?';
      }else{
          texto = '¿Está seguro de actualizar los datos?';
      }
  }
  if (confirm(texto)){
    return true
  }else{
    return false
  }
}
}


