//This variables are used to verify the validity of the characters written
//in the fields, will be scanned to find out wether a given character is
//valid or not within a specific field.
var alphabet  = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
var alphabetnospace  = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var salphabet = "áéíóúñÑÁÉÍÓÚ";
var digits    = "0123456789";
var alphaChars         = alphabet + salphabet + " .&-!?():;";
var extendedAlphaChars = alphabet + salphabet + digits + " .#-/,;:&()";
var categoryChars    = alphabet + salphabet + " -";
var activityChars    = alphabet + salphabet + digits + " -.&,;;?!()%$'\"#@/\\";
var emailChars             = alphabet + digits + "-.@_";
var phoneChars             = digits + " -()" + alphabet;
var accountChars       = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + digits + "_";

function trim(input)
{
	if(input)
	{
		var lre = /^\s*/;
		var rre = /\s*$/;
		input = input.replace(lre, "");
		input = input.replace(rre, "");
		return input;
	}

	return input;
}

function validatefield(fieldValue,fieldName,isRequired,
                       minimumLength,validateChars,validCharsArray)
{
	fieldValue = trim(fieldValue);

   if(isRequired)
      {
            if(fieldValue == "")
            {
                  alert("Debe escribir un valor para el campo " + fieldName);
                  return false;
            }
      }

      if(minimumLength > 0 && fieldValue.length > 0)
      {
            if(fieldValue.length < minimumLength)
            {
                  alert("El campo " + fieldName + " deberá tener almenos " + minimumLength + " caracteres.");
                  return false;
            }
      }

      if(validateChars)
      {
            for(var x = 0; x < fieldValue.length ; x++)
            {
                  if(validCharsArray.lastIndexOf(fieldValue.charAt(x)) < 0)
                  {
                        if(confirm("El campo " + fieldName + " contiene caracteres inválidos.\n  Le gustaria ver una lista de caracteres válidos para el campo  " + fieldName + " ?"))
                        {
                              alert("La lista de caracteres válidos para el campo " + fieldName + " es:\n" + validCharsArray);
                        }
                        return false;
                  }
            }
      }

      //This is a valid field according to the given parameters
      return true;
}

/* This function will be used with the automathon procedure for e-Mail validation */

function getColumn(character)
{
      if(alphabet.lastIndexOf(character) >= 0)
            return 0;
      if(digits.lastIndexOf(character) >= 0)
            return 1;
      if(character == ".")
            return 2;
      if(character == "_")
            return 3;
      if(character == "-")
            return 4;
      if(character == "@")
            return 5;
      if(character == "endl")
            return 6;

      return 7;
}

function verifyemail(theemail)
{
    //if the previous was true then do some parsing for any present e-mail addresses
      if(theemail != "")
      {
            //we will use an automathon to do the parsing, so let's define the state's table:
            var states = new Array(13);
            states[0]  = new Array(1,7,7,7,7,7,7,7);
            states[1]  = new Array(1,1,1,1,1,2,8,8);
            states[2]  = new Array(3,3,9,9,9,9,9,9);
            states[3]  = new Array(3,3,4,10,3,10,10,10);
            states[4]  = new Array(5,5,11,11,11,11,11,11);
            states[5]  = new Array(5,5,4,12,5,12,6,12);
            states[6]  = new Array(0,0,0,0,0,0,0,0);
            states[7]  = new Array(-1,-1,-1,-1,-1,-1,-1,-1);
            states[8]  = new Array(-2,-2,-2,-2,-2,-2,-2,-2);
            states[9]  = new Array(-3,-3,-3,-3,-3,-3,-3,-3);
            states[10] = new Array(-4,-4,-4,-4,-4,-4,-4,-4);
            states[11] = new Array(-5,-5,-5,-5,-5,-5,-5,-5);
            states[12] = new Array(-6,-6,-6,-6,-6,-6,-6,-6);

            var laststate = 0;
            for(var x=0;x <= theemail.length;)
            {
                  if(x == theemail.length) //This means endl is reached
                        laststate = states[laststate][getColumn("endl")];
                  else //get the state in a normal fashion
                        laststate = states[laststate][getColumn(theemail.charAt(x))];

                  switch(laststate)
                  {
                        case 0:
                              //This is the case everything is ok so just increase x to leave the for !!!
                              x++;
                              break;
                        case 1:
                        case 2:
                        case 3:
                        case 4:
                        case 5:
                        case 6:
                              x++;
                              break;
                        case 7:
                        case 8:
                        case 9:
                        case 10:
                        case 11:
                        case 12:
                              alert("El e-Mail es inválido.");
                              return false;
                  }
            }

            return true;
      }
      else
      {
            alert("El e-Mail es inválido.");
            return false;
      }
}
