function toggleFieldValue(field,defaultValue,isCleared)
{
    var thisField = document.getElementById(field);

    if (isCleared == 0) {
        if (thisField.value == defaultValue) { thisField.value = ''; }
    } else if (isCleared == 1) {
        if (thisField.value == '') { thisField.value = defaultValue; }
    }
}

this.validNumbers = '0123456789';
this.validPhoneCharacters = '0123456789';
this.validZipCharacters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789- ';
this.validTextCharacters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-.\' ';

function FormValidation(source, value)
{
   var first_name = document.getElementById("first_name");
   var last_name = document.getElementById("last_name");
   var age_month = document.getElementById("age_month");
   var age_day = document.getElementById("age_day");
   var age_year = document.getElementById("age_year");
   var email = document.getElementById("email");
   var zipcode = document.getElementById("zipcode");
   var teams = document.getElementById("teams");
   first_name.className = 'valid';
   last_name.className = 'valid';
   age_month.className = 'valid';
   age_day.className = 'valid';
   age_year.className = 'valid';
   email.className = 'valid';
   zipcode.className = 'valid';
   teams.className = 'valid';
      
   if (first_name.value.length == 0 )
   {
      first_name.className = 'error';
      value.IsValid=false; //stops the form submit
      alert('First name is required');
      first_name.focus();
      return false;
   }
   else if (V_isTextValid(first_name.value) == false)
   {
      first_name.className = 'error';
      value.IsValid=false; //stops the form submit
      alert('First name cannot contain numbers');
      first_name.focus();
      return false;
   }
   else if (last_name.value.length == 0 )
   {
      last_name.className = 'error';
      value.IsValid=false; //stops the form submit
      alert('Last name is required');
      last_name.focus();
      return false;
   }
   else if (V_isTextValid(last_name.value) == false)
   {
      last_name.className = 'error';
      value.IsValid=false; //stops the form submit
      alert('Last name cannot contain numbers');
      last_name.focus();
      return false;
   }
   else if (age_month.value.length == 0 )
   {
      age_month.className = 'error';
      value.IsValid=false; //stops the form submit
      alert('Birth month is required');
      age_month.focus();
      return false;
   }
   else if (V_isNumeric(age_month.value) == false)
   {
      age_month.className = 'error';
      value.IsValid=false; //stops the form submit
      alert('Birth month should be numeric');
      age_month.focus();
      return false;
   }
   else if (age_day.value.length == 0 )
   {
      age_day.className = 'error';
      value.IsValid=false; //stops the form submit
      alert('Birth day is required');
      age_day.focus();
      return false;
   }
   else if (V_isNumeric(age_day.value) == false)
   {
      age_day.className = 'error';
      value.IsValid=false; //stops the form submit
      alert('Birth day should be numeric');
      age_day.focus();
      return false;
   }
   else if (age_year.value.length < 4 )
   {
      age_year.className = 'error';
      value.IsValid=false; //stops the form submit
      alert('Birth year is required and should be in 4 digit format');
      age_year.focus();
      return false;
   }
   else if (V_isNumeric(age_year.value) == false)
   {
      age_year.className = 'error';
      value.IsValid=false; //stops the form submit
      alert('Birth year should be numeric and 4 in digit format');
      age_year.focus();
      return false;
   }
   else if (email.value.length == 0 )
   {
      email.className = 'error';
      value.IsValid=false; //stops the form submit
      alert('Email address is required');
      email.focus();
      return false;
   }
   else if (V_validateEmail(email.value) == false)
   {
      email.className = 'error';
      value.IsValid=false; //stops the form submit
      alert('Email address appears to be invalid');
      email.focus();
      return false;
   }
   else if (zipcode.value.length == 0 )
   {
      zipcode.className = 'error';
      value.IsValid=false; //stops the form submit
      alert('Zipcode is Required');
      zipcode.focus();
      return false;
   }
   else if (V_isZipValid(zipcode.value) == false)
   {
      zipcode.className = 'error';
      value.IsValid=false; //stops the form submit
      alert('Zipcode appears to be invalid');
      zipcode.focus();
      return false;
   }
   else if (document.ucob.teams[document.ucob.teams.selectedIndex].value < 1 )
   {
      teams.className = 'error';
      value.IsValid=false; //stops the form submit
      alert('Please select a Team');
      teams.focus();
      return false;
   }
   else
   {
      value.IsValid =true;
      return true;
   }
}

function V_isNumeric(field)
{
	for (var i=0; i<field.length; i++)
	{
		if (this.validNumbers.indexOf(field.charAt(i),0) == -1) return false;
	}
	return true;
}

function V_isPhoneNumeric(field)
{
	for (var i=0; i<field.length; i++)
	{
		if (this.validPhoneCharacters.indexOf(field.charAt(i),0) == -1) return false;
	}
	return true;
}

function V_isZipValid(field)
{
	for (var i=0; i<field.length; i++)
	{
		if (this.validZipCharacters.indexOf(field.charAt(i),0) == -1) return false;
	}
	return true;
}

function V_isTextValid(field)
{
	for (var i=0; i<field.length; i++)
	{
		if (this.validTextCharacters.indexOf(field.charAt(i),0) == -1) return false;
	}
	return true;
}

function V_validateEmail(email)
{
	var str = email;
	var instancecounter = 0;

	str += '';
	intAt = str.indexOf( '@', 1 );		// the "@"
	intDot = str.lastIndexOf( '.' );		// the last "."
	namestr = str.substring( 0, intAt );		// everything before the "@"
	domainstr = str.substring( intAt +1, str.length );		// everything after the "@"
	toplevelstr = str.substring( intDot +1, str.length);		// everything after the last "."
	
	if ((str.indexOf(" ")!=-1) || (intAt == -1) || (intDot == -1 ) || (namestr.length == 0) || (domainstr.length == 0) || (intAt > intDot) || (domainstr.indexOf(".") <= 0) || (toplevelstr.length <= 1))
	{
		return false;
	} 
	else
	{
		// iterate through email address checking for
		// more than 1 @ sysmbol, or none at all
		for ( i = 0; i < str.length; i++ )
		{
			if ((str.substring(i,i+1)) == "@" )
			{
				instancecounter = instancecounter + 1;
			}
		}
		// Check to see if we have none, or more than one @ symbol
		if ((instancecounter > 1) || (instancecounter == 0 ))
		{
			return false;
		}
		else
		{
		    return true;
		}
	}
}
