function checklength(val)
{
	for(var j=val.length;j > 0;j--)
	{
		if(val.charAt(j-1) != ' ')
		{
			return true;
		}
	}
	return false;
}

function emailCheck(val)
{
var EmailOk  = true
var Temp     = val;
var AtSym    = Temp.indexOf('@')
var Period   = Temp.lastIndexOf('.')
var Space    = Temp.indexOf(' ')
var Length   = Temp.length - 1   // Array is from 0 to length-1

	if ((AtSym < 1) ||                     // '@' cannot be in first position
    (Period <= AtSym+1) ||             // Must be atleast one valid char btwn '@' and '.'
    (Period == Length ) ||             // Must be atleast one valid char after '.'
    (Space  != -1))                    // No empty spaces permitted
 	{
    	  return false;
 	}
   	return true;
}					

function valForm(frm)
{
	// Start - Code added for TD- 29353.
	if(document.getElementById('submitButton').src.indexOf('submit.jpg') != -1)
	{
		return false;
	}
	// End - Code added for TD- 29353.
	if(frm.fname.value == '')
    {
		alert("Please enter first name.");
		frm.fname.style.backgroundColor='yellow';
		return false;
	}//end if
	if(frm.lname.value == '')
    {
		alert("Please enter last name.");
		frm.lname.style.backgroundColor='yellow';
		return false;
	}//end if
	if(!emailCheck(frm.emailAddress.value))
	{
		alert("Please enter a valid email address.");
		if(!document.layers) frm.emailAddress.style.backgroundColor='yellow';
		frm.emailAddress.focus();
		return false;
	}
	
	if(frm.phoneNumber.value == '')
    {
		alert("Please enter phone number.");
		frm.phoneNumber.style.backgroundColor='yellow';
		return false;
	}//end if
	

	if(frm.phoneNumber.value != '')
    {
		var phone = frm.phoneNumber.value;
		var phone_array = phone.split("-");
		if(phone_array.length==3)
		{
			for(var i=0;i<phone_array.length;i++)
			{
				if(i==2)
				{
					if( (isNaN(phone_array[i])) || !(phone_array[i].length==4))
					{
						alert("Please enter a valid phone number.  See example.");
						frm.phoneNumber.style.backgroundColor='yellow';
						return false;
					}
				}
				else
				{
					if( (isNaN(phone_array[i])) || !(phone_array[i].length==3))
					{
						alert("Please enter a valid phone number.  See example.");
						frm.phoneNumber.style.backgroundColor='yellow';
						return false;
					}
				}
			}
		}
		else
		{
			alert("Please enter a valid phone number.  See example.");
			frm.phoneNumber.style.backgroundColor='yellow';
			return false;
		}
	}

	if(frm.dob2.value == null)
    {
		alert("Please select date of birth.");
		if(!document.layers) frm.dob2.style.backgroundColor='yellow';
		return false;
	}//end if
	if (frm.dob2.value != null) {
		if(!isValidDate(frm.dob2.value))
			return false;
	}

	//start - code added for date of birth validation

	var now = new Date();
	var thisYear = now.getFullYear();
	var dob = frm.dob2.value;
	var splitter =  dob.lastIndexOf('/');
	var year = dob.substring(splitter+1, dob.length);
	var diff = thisYear - year;
	
	if (diff < 18)
	{
		alert("You must be 18 years of age to enter the Sweepstakes");
		frm.dob2.style.backgroundColor='yellow';
		return false;
	}
	else if(diff == 18)
	{
		var thisMonth = now.getMonth() + 1;
		var check = dob.indexOf('/');
		var month = dob.substring(0, check);
		var monthDiff = thisMonth - month
		if(monthDiff < 0 )
		{
			alert("You must be 18 years of age to enter the Sweepstakes");
			frm.dob2.style.backgroundColor='yellow';
			return false;
		}
		else if(monthDiff == 0)
		{
			var thisDay = now.getDate();
			var day = dob.substring(check+1, splitter);
			var dayDiff = thisDay - day;
			if (dayDiff < 0 )
			{
				alert("You must be 18 years of age to enter the Sweepstakes");
				frm.dob2.style.backgroundColor='yellow';
				return false;
			}			
		}
	}

	// Start - Code added for TD- 29349.
	if(frm.fNFLteam.selectedIndex == 0)
	{
		alert("Please select a Team.")
		return false;
	}
	// End - Code added for TD- 29349.

	// Start - Code to limit the user entry of sweepstakes data
	
	var cookieValue = readCookie("sweepstakesEntryConstraint");
	if(cookieValue != null)
	{
		if(cookieValue == "1")
		{
			createCookie("sweepstakesEntryConstraint","6",1);			
		}
		else if(cookieValue == "6")
		{
			alert("We’re sorry.  It appears that you have entered the sweepstakes six times today.  Please come back tomorrow to enter again.");
			return false;
		}
	}
	else
	{
		createCookie("sweepstakesEntryConstraint","1",1);		
	}

	// End - Code to limit the user entry of sweepstakes data

	return true;
}
function openRules() {
	var win = window.open('rules.jsp','rules','width=400,height=350,scrollbars=yes,resizable=no');
	win.focus();
}

// Start - These functions are for cookie creation, reding cookie and deleting the cookie

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

// End - These functions are for cookie creation, reading cookie and deleting the cookie


function isValidDate(dateStr) {

 var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert("Date is not in a valid format.")
return false;
}
month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[4];
if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!")
return false
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
   }
}
return true;  // date is valid
}