
//Javascript document

/**
*Checks for alphabetic characters
*/

//color setting in case there is an error
var txt_color = "000000";
var bg_color = "FFFF99";

function isLetter(c)
{
	//if the character passes the evaluation, it isn't a letter
	if(((c < "a") || (c > "z")) && ((c < "A") || (c > "Z")))
	{
		return false;
	}//end if
	return true;
}//end isAlpha()

/**
*Checks for alphabetic characters, hyphens and spaces
*/
function isAlpha(c)
{
	//if the character passes the evaluation, it is not a letter, hyphen or space
	if(((c < "a") || (c > "z")) && ((c < "A") || (c > "Z")) && ((c != "-") && (c != " ")))
	{
		return false;
	}//end if
	return true;
}//end isAlpha()

/**
*Checks for numeric characters
*/
function isNumber(c)
{
	//if the character passes the evaluation it isn't a number
	if((c < "0") || (c > "9"))
	{
		return false;
	}//end if
	return true;
}//end isNumber

/**
*Checks for alphanumeric characters, spaces, hyphens, periods and pound
*/
function isAlphaNum(c)
{
	//if the character passes the evaluation, then it isn't an alphanumeric character, space, period or number sign
	if(((c < "0") || (c > "9")) && (((c < "a") || (c > "z")) && ((c < "A") || (c > "Z")) && (c != " ") && (c != "-") && (c != ".") && (c != "#")))
	{
		return false;
	}//end if
	return true;
}//end isAlphaNum()

/**
 * Test if variable is empty.
 */
function IsEmpty(value) {
   if ((value.length==0) ||
   (value==null)) {
      return true;
   }
   else { return false; }
}

/**
 * Validation of the first name. 
 */
function checkFName()
{
	var fname = document.form.txtFName.value;
	
	if(fname == null || fname == "")
	{
		document.form.txtFName.style.color = "000000";
		document.form.txtFName.style.backgroundColor = "FFFF99";
		return false;
	}//end if
	
	for(var i = 0; i < fname.length; i+= 1)
	{
		var isGood = isAlpha(fname.charAt(i));
		
		if(!isGood)
		{
			document.form.txtFName.style.color = "000000";
			document.form.txtFName.style.backgroundColor = "FFFF99";
			return false;
		}//end if
	}//end for
	
	return true;
}//end checkFName()

/**
 * Validation of the last name. 
 */
function checkLName()
{
	var lname = document.form.txtLName.value;
	
	if(lname == null || lname == "")
	{
		document.form.txtLName.style.color = "000000";
		document.form.txtLName.style.backgroundColor = "FFFF99";
		return false;
	}//end if
	
	for(var i = 0; i < lname.length; i+= 1)
	{
		var isGood = isAlpha(lname.charAt(i));
		
		if(!isGood)
		{
			document.form.txtLName.style.color = "000000";
			document.form.txtLName.style.backgroundColor = "FFFF99";
			return false;
		}//end if
	}//end for
	
	return true;
}//end checkLName()

/**
 * Validation of the Address, test if empty
 */
function checkAddress()
{
	var address = document.form.txtAddress.value;
	
	if(address == null || address == "")
	{
		document.form.txtAddress.style.color = "000000";
		document.form.txtAddress.style.backgroundColor = "FFFF99";
		return false;
	}//end if
		
	return true;
}//end of function checkAddress()

/**
 * Validation of the checkPostal. 
 */
function checkPostal()
{
	var postalcode = document.form.txtPostal.value;
	
	if(postalcode == null || postalcode == "")
	{
		document.form.txtPostal.style.color = "000000";
		document.form.txtPostal.style.backgroundColor = "FFFF99";
		return false;
	}//end if
	
	
	return true;
}//end checkPostal()

/**
 * Validation of the City, test if empty
 */
function checkCity()
{
	var city = document.form.txtCity.value;
	
	if(city == null || city == "")
	{
		document.form.txtCity.style.color = "000000";
		document.form.txtCity.style.backgroundColor = "FFFF99";
		return false;
	}//end of if
	
	for( var i = 0; i< city.length; i+=1)
	{
		var isGood = isAlpha(city.charAt(i));
		
		if(!isGood)
		{
			document.form.txtCity.style.color = "000000";
			document.form.txtCity.style.backgroundColor = "FFFF99";
			return false;
		}//end if
	}//end of for
	return true;
}//end of function checkCity()

/**
 * Validation of the email.
 */ 
function checkEmail() 
{
	//obtain the value of the email field and use a regular expression to make sure the address is valid
	var email = document.form.txtEmail.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\.ca)|(\..{2,2}))$)\b/gi);
	
	//if the address is valid, return true
	if (!email)
	{
	   //set the text color to white and the background color to turquoise and return false
	   alert("Please enter valid email: 'xxx@xxx.xxx'");
	   document.form.txtEmail.style.color = "000000";
	   document.form.txtEmail.style.backgroundColor = "FFFF99";
	   return false;
	}//end else
	return true;
}//end checkEmail()

/**
 * Although this is the date of birth validation.
 * This method only validates the year field just to see if its empty or not.
 */
function checkDateOfBirth()
{
	var year = document.form.year.value;
	
	if((year == null || year == "")||(year.length != 4) || (isNaN(year)))
	{
		document.form.year.style.color = "000000";
		document.form.year.style.backgroundColor = "FFFF99";
		return false;		
				
	}//end else if()
	return true;
}//end checkDateOfBirth()

/**
 * validation of the rules check box.
 */
function checkRules()
{
	if (document.form.chkRules.checked)
	{ 
		return true;
		
	}else{
		alert("Please read the rules for the contest.");
		return false;
	}//end if
}//end checkRules()


/**
 * Validation of phone.
 */
function checkPhone()
{
	var phone = document.form.txtPhone.value;
	
	if(phone == null || phone == "")
	{
		document.form.txtPhone.style.color = "000000";
		document.form.txtPhone.style.backgroundColor = "FFFF99";
		return false;		
				
	}//end else if()
			
	return true;
}//end checkHomePhone()


/**
 * Call all methods and validate all of the fields.
 */
 
function checkAll()
{
	var fname= checkFName();
	var lname= checkLName();
	var address= checkAddress();
	var city = checkCity();
	var postal = checkPostal();
	var email = checkEmail();
	var dob = checkDateOfBirth();	
	var phone = checkPhone();
	var rules = checkRules();
		
	if(!fname) //first name
	{
		return false;
	}//end if
	
	
	if(!lname) //last name
	{
		return false;
	}//end if
	
	if(!address) //address 
	{
		return false;
	}//end if
	
	if(!city) //City
	{
		return false;
	}//end if
	
	if(!postal) //Postal Code
	{
		return false;
	}//end if
	
	if(!email) //Email
	{
		return false;
	}//end if
	
	if(!dob) //Date of Birth
	{
		return false;
	}//end if

	if(!phone) //Phone
	{
		return false;
	}//end if
	
	if(!rules) //rules
	{
		return false;
	}//end if
	

	return true;
}//** END of Function **

/**
 * This for will call this function
 * and return the fields to their original colors.
 */
function changeColor(c)
{
	c.style.color = "000000";
	c.style.backgroundColor = "FFFFFF";
}//end changeColor()