/*
specific validation handlers
*/
function validateNotEmpty(input2chk, row)
{
  theInputVal = theForm[input2chk].value;

  if(theInputVal.length == 0)
    setError(input2chk, row);
  else
    clearError(input2chk, row);
}

function validateAnyName(input2chk, row)
{
	// name can only contain letters or numbers
	var legalChars =  /^[a-zA-Z0-9]*$/;

	theInputVal = theForm[input2chk].value;

	if(!legalChars.test(theInputVal) || theInputVal == "")
		setError(input2chk, row);
	else
		clearError(input2chk, row);
}

function validateEmail(input2chk, row)
{
	theInputVal = theForm[input2chk].value;
	
	if(!isEmail(theInputVal))
		setError(input2chk, row);
	else
		clearError(input2chk, row);
}

function validatePhoneNumber(input2chk, row)
{
	// only numbers and space character allowed
	var legalChars = /^[0-9 ]*$/;

	theInputVal = theForm[input2chk].value;

	if(!legalChars.test(theInputVal) || !singleSpace(theInputVal))
		setError(input2chk, row);
	else
		clearError(input2chk, row);
}

function validatePasswordIfSet(input2chk, row)
  {
	theInputVal = theForm[input2chk].value;
	theInputVal = theForm[input2chk].value;
  if (theInputVal != "")
    {
    validatePassword(input2chk, row);
    theForm["passwordChanged"].value = true;
    }

  }

function passwordConfirmIfSet(input2chk, row, validateAgainst)
  {
	theInputVal = theForm[input2chk].value;
  if (theInputVal != "")
    passwordConfirm(input2chk, row, validateAgainst)
  }

function validatePassword(input2chk, row)
{
	// no spaces allowed in password
	var illegalChar = /^[ ]*$/;
	
	theInputVal = theForm[input2chk].value;

	if(theInputVal.length < 6 || theInputVal.length > 20 || illegalChar.test(theInputVal))
		setError(input2chk, row);
	else
		clearError(input2chk, row);
}

function passwordConfirm(input2chk, row, validateAgainst)
{
	var pw2chk = theForm[validateAgainst].value;
	var confField = theForm[input2chk].value;

	if(confField != pw2chk || confField == "")
		setError(input2chk, row);
	else
		clearError(input2chk, row);
}

function validateFile(input2chk, row, mType)
{
	theInputVal = theForm[input2chk].value;
	var startIndex = theInputVal.length - 4;
	var suffix = theInputVal.substr(startIndex, 4);

	if(mType == 0)
	{		
		if(equalsIgnoreCase(suffix,".jpg") || equalsIgnoreCase(suffix,"jpeg") || equalsIgnoreCase(suffix,".gif") || equalsIgnoreCase(suffix,".png"))
			clearError(input2chk, row);
		else
			setError(input2chk, row);
	}
	else if(mType == 1)
	{
		if(theInputVal == "")
			setError(input2chk, row);
		else
			clearError(input2chk, row);
	}
}


/*
global variables
*/
var theForm = null;
var theInputVal;
var validation = new Array();
var allGood;


/*
MAIN function
*/
function validate(form, mType)
{
	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])
		{
			// if a 4th field exists, pass it as an argument to validate against
			// if mType exists, pass it as an argument
			if(mType == 0 || mType == 1)
				eval(validation[i][2]).call(this, validation[i][0], validation[i][1], mType);
			else if(!validation[i][3] && (mType!=0 || mType!=1))
				eval(validation[i][2]).call(this, validation[i][0], validation[i][1]);
			else if(validation[i][3] && (mType!=0 || mType!=1))
				eval(validation[i][2]).call(this, validation[i][0], validation[i][1], validation[i][3]);
		}
	}

	if(allGood)
		theForm.submit();

}


/*
mark/clear rows with errors
*/
function setError(input2chk, row)
{
	document.getElementById(input2chk).className = "errorInput";
	allGood = false;
}

function clearError(input2chk, row)
{
	document.getElementById(input2chk).className = "";
}


/*
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;
}
