/*
specific validation handlers
*/
function validateTextField(input, lbl)
{
  theInputVal = theForm[input].value;

  if(theInputVal.length == "")
    setError(input, lbl, " Campo obligatorio");
  else
    clearError(input, lbl);
}

function validateEmail(input2chk, lbl)
{
	theInputVal = theForm[input2chk].value;
	
  if(!isEmail(theInputVal))
		setError(input2chk, lbl, " Email inv\341lido");
	else
		clearError(input2chk, lbl);
}




/*
global variables
*/
var theForm = null;
var theInputVal;
var validation = new Array();
var allGood;


/*
MAIN function
*/
function validate(form)
{
	allGood = true;

	theForm = document.forms[form];
	
	validation = eval(form);

	for(var i=0; i<validation.length; i++)
	{
		// if the function to call exists (index 2), call it
		if(validation[i][2])
		{
			var argumentList = new Array();

      // create the argument list to pass to apply
      for (var j=0; j<validation[i][3].length; j++)
        {
        argumentList.push(validation[i][3][j]);
        }

      eval(validation[i][2]).call(this, validation[i][0], argumentList);
		}
	}

	if(allGood)
		theForm.submit();
}


/*
mark/clear lbls with errors
*/
function setError(input2chk, lbl, text)
{
  var label = document.getElementById(lbl);
  if(label.getElementsByTagName("img").length == 1) {
    var icon = document.createElement("img"); icon.src = "http://www.jetblue.com/i/warning_small.gif";
    var span = document.createElement("span"); span.appendChild(document.createTextNode(text));
    document.getElementById(input2chk).className = "errorInput";
    label.appendChild(icon);
    label.appendChild(span);
    label.style.color = "#ff6600";
  }

  allGood = false;
}

function clearError(input2chk, lbl)
{
  var label = document.getElementById(lbl);
	document.getElementById(input2chk).className = "";
  if(label.getElementsByTagName("img").length > 1) {
    label.removeChild(label.getElementsByTagName("img").item(1));
    label.removeChild(label.getElementsByTagName("span").item(0));
  }
  document.getElementById(lbl).style.color = "#666666";
}


/*
analyze email string
*/
function isEmail(s)
{
	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;
 }


/*
analyze phone number string, make sure no back-to-back spaces
*/
function singleSpace(s)
{
	for(var i=0; i<s.length-1; i++)
	{
		if(s.charAt(i) == " " && s.charAt(i+1) == " ")
			return false;		
	}

	return true;
}


/*
compare strings, return true if strings match regardless of case
*/
function equalsIgnoreCase(str1, str2)
{
	var lowerCase = str1.toLowerCase();

	if(lowerCase == str2)
		return true;
	else
		return false;
}
