function ValidatePersonal(theForm)
{
	bDoSurvey = false;
	
	theForm.firstname.value = prepValue(theForm.firstname.value);
	if (theForm.firstname.value == "")
	{
		alert("Please enter your First Name.");
		document.getElementById('FIRST').style.color = 'FF0000';
		theForm.firstname.focus(); 
		return false;
	}
	else
	{
		document.getElementById('FIRST').style.color = '000000';
	}
	
	theForm.lastname.value = prepValue(theForm.lastname.value);
	if (theForm.lastname.value == "")
	{
		alert("Please enter your Last Name.");
		document.getElementById('LAST').style.color = 'FF0000';
		theForm.lastname.focus();
		return false;
	}
	else
	{
		document.getElementById('LAST').style.color = '000000';
	}
	
	theForm.email.value = prepEmail(theForm.email.value);
	if (!validateEmail(theForm.email.value))
	{
		document.getElementById('EMAIL').style.color = 'FF0000';
		theForm.email.focus();
		return false;
	}
	else
	{
		document.getElementById('EMAIL').style.color = '000000';
	}
	
	if (theForm.password != null)
	{
		if (!ValidatePassword(theForm)) return false;
	}
	
	theForm.phone.value = prepNumber(theForm.phone.value);
	
	theForm.address1.value = prepValue(theForm.address1.value);
	theForm.address2.value = prepValue(theForm.address2.value);
	if (theForm.address1.value == "" || theForm.address1.value.length < 3)
	{	
		alert("Please enter your Address.");
		document.getElementById('STREET').style.color = 'FF0000';
		theForm.address1.focus();
		return false;
	}
	else
	{
		document.getElementById('STREET').style.color = '000000';
	}
	
	// city, state and zip can be left blank if outside the united states
	if (theForm.country.options[theForm.country.selectedIndex].text == "United States")
	{
		theForm.city.value = prepValue(theForm.city.value);
		if (theForm.city.value == "" || theForm.city.value.length < 2)
		{
			alert("Please enter your City.");
			document.getElementById('CITY').style.color = 'FF0000';
			theForm.city.focus();
			return false;
		}
		else
		{
			document.getElementById('CITY').style.color = '000000';
		}
		
		theForm.state.value = prepValue(theForm.state.value);
		if (theForm.state.value == "")
		{
			alert("Please enter your State/Province.");
			document.getElementById('STATE').style.color = 'FF0000';
			theForm.state.focus();
			return false;
		}
		else
		{
			document.getElementById('STATE').style.color = '000000';
		}
		
		theForm.zip.value = prepValue(theForm.zip.value);
		if (theForm.zip.value == "")
		{
			alert("Please enter your Zip/Postal Code.");
			document.getElementById('ZIP').style.color = 'FF0000';
			theForm.zip.focus();
			return false;
		}
		else
		{
			document.getElementById('ZIP').style.color = '000000';
		}
	}

	return true;
}

function ValidateGift(theForm)
{
	theForm.giftfirstname.value = prepValue(theForm.giftfirstname.value);
	if (theForm.giftfirstname.value == "")
	{
		alert("Please enter the gift Recipient's First Name.");
		document.getElementById('FIRST').style.color = 'FF0000';
		theForm.giftfirstname.focus(); 
		return false;
	}
	else
	{
		document.getElementById('FIRST').style.color = '000000';
	}
	
	theForm.giftlastname.value = prepValue(theForm.giftlastname.value);
	if (theForm.giftlastname.value == "")
	{
		alert("Please enter the gift Recipient's Last Name.");
		document.getElementById('LAST').style.color = 'FF0000';
		theForm.giftlastname.focus();
		return false;
	}
	else
	{
		document.getElementById('LAST').style.color = '000000';
	}
	
	theForm.giftemail.value = prepEmail(theForm.giftemail.value);
	if (!validateEmail(theForm.giftemail.value))
	{
		document.getElementById('EMAIL').style.color = 'FF0000';
		theForm.giftemail.focus();
		return false;
	}
	else
	{
		document.getElementById('EMAIL').style.color = '000000';
	}
	
	return true;
}

function ValidateEmail(theForm)
{
	theForm.email.value = prepEmail(theForm.email.value);
	if (!validateEmail(theForm.email.value))
	{
		document.getElementById('email').style.color = '#FF0000';
		theForm.email.focus();
		return false;
	}
	else
	{
		document.getElementById('email').style.color = '#000000';
	}
	
	return true;
}

function ValidateDescript(theForm)
{
    nSize = theForm.descript.value.length;
    if (nSize <= 0)
	{
		alert("Please enter a reason for contacting us.");
		document.getElementById('descript').style.color = '#FF0000';
		theForm.descript.focus();
		return false;
	}
	else
	{
		document.getElementById('descript').style.color = '#000000';
	}
	return true;
}

function ValidateSource(theForm)
{
    nSize = theForm.source.value.length;
    if (nSize <= 0)
	{
		alert("Please tell us how you heard of GameHouse.");
		document.getElementById('source').style.color = '#FF0000';
		theForm.source.focus();
		return false;
	}
	else
	{
		document.getElementById('source').style.color = '#000000';
	}

	return true;

}

function ValidatePhone(theForm)
{
    nSize = theForm.phone.value.length;
     if (nSize <= 0)
	{
		alert("Please enter your phone number.");
		document.getElementById('phone').style.color = '#FF0000';
		theForm.phone.focus();
		return false;
	}
	else
	{
		document.getElementById('phone').style.color = '#000000';
	}
	return true;
}

function ValidateName(theForm)
{
    nSize = theForm.player.value.length;
     if (nSize <= 0)
	{
		alert("Please enter your name.");
		document.getElementById('NAME').style.color = '#FF0000';
		theForm.player.focus();
		return false;
	}
	else
	{
		document.getElementById('NAME').style.color = '#000000';
	}
	return true;
}

function ValidateComments(theForm)
{
    nSize = theForm.comments.value.length;
     if (nSize <= 0)
	{
		alert("Please enter your comments.");
		document.getElementById('COMMENTS').style.color = '#FF0000';
		theForm.comments.focus();
		return false;
	}
	else
	{
		document.getElementById('COMMENTS').style.color = '#000000';
	}
	return true;
}

function ValidateMessage(theForm)
{
    nSize = theForm.subj.value.length;
     if (nSize <= 0)
	{
		alert("Please enter your comments.");
		document.getElementById('MESSAGE').style.color = '#FF0000';
		theForm.subj.focus();
		return false;
	}
	else
	{
		document.getElementById('MESSAGE').style.color = '#000000';
	}
	return true;
}

function ValidateDropDown(theForm, fieldName, fieldText, alertText)
{
    if (document.getElementById(fieldName).value == 0)
    {
		alert(alertText);
		document.getElementById(fieldText).style.color = '#FF0000';
		return false;
    }
    else
    {
	document.getElementById(fieldText).style.color = '#000000';
    }
    return true;
}


function ValidatePassword(theForm)
{
	nSize = theForm.password.value.length;
	theForm.password.value = prepPassword(theForm.password.value);
	nNewSize = theForm.password.value.length;
	
	if (nSize != nNewSize && nNewSize > 0)
	{
		alert("Your FunPass Password can only contain letters and numbers.\r\nNon-alphanumeric characters have been removed.");
		document.getElementById('PASSWORD').style.color = '#FF0000';
		theForm.password.focus();
		return false;
	}
	
	if (theForm.password.value == "")
	{
		alert("Please enter your FunPass Password.");
		document.getElementById('PASSWORD').style.color = '#FF0000';
		theForm.password.focus();
		return false;
	}
	else
	{
		document.getElementById('PASSWORD').style.color = '#000000';
	}
	
	return true;
}

function ValidateLogin(theForm)
{
	if (!ValidateEmail(theForm)) return false;
	if (!ValidatePassword(theForm)) return false;
	return true;
}

function ValidateUpdate(theForm)
{
	theForm.name.value = prepValue(theForm.name.value);
	if (theForm.name.value == "")
	{
		alert("Please enter your Full Name.");
		document.getElementById('NAME').style.color = '#FF0000';
		theForm.name.focus(); 
		return false;
	}
	else
	{
		document.getElementById('NAME').style.color = '#000000';
	}
	
	theForm.address.value = prepValue(theForm.address.value);
	if (theForm.address.value == "")
	{	
		alert("Please enter your Address.");
		document.getElementById('ADDRESS').style.color = '#FF0000';
		theForm.address.focus();
		return false;
	}
	else
	{
		document.getElementById('ADDRESS').style.color = '#000000';
	}
	
	theForm.city.value = prepValue(theForm.city.value);
	if (theForm.city.value == "")
	{
		alert("Please enter your City.");
		document.getElementById('CITY').style.color = '#FF0000';
		theForm.city.focus();
		return false;
	}
	else
	{
		document.getElementById('CITY').style.color = '#000000';
	}
	
	theForm.state.value = prepValue(theForm.state.value);
	if (theForm.state.value == "")
	{
		alert("Please enter your State/Province.");
		document.getElementById('STATE').style.color = '#FF0000';
		theForm.state.focus();
		return false;
	}
	else
	{
		document.getElementById('STATE').style.color = '#000000';
	}
	
	theForm.zip.value = prepValue(theForm.zip.value);
	if (theForm.zip.value == "")
	{
		alert("Please enter your Zip/Postal Code.");
		document.getElementById('ZIP').style.color = '#FF0000';
		theForm.zip.focus();
		return false;
	}
	else
	{
		document.getElementById('ZIP').style.color = '#000000';
	}
	
	if (!ValidatePayment(theForm)) return false;
	
	return true;
}

function ValidatePayment(theForm)
{
	theForm.cc.value = prepCC(theForm.cc.value);
	if (theForm.cc.value == "" || theForm.cc.value.length < 13)
	{
		alert("Please enter your Credit Card Number.");
		document.getElementById('CC').style.color = '#FF0000';
		theForm.cc.focus();
		return false;
	}
	else
	{
		document.getElementById('CC').style.color = '#000000';
	}
	
	if (theForm.cvc != null)
	{
		theForm.cvc.value = prepCC(theForm.cvc.value);
		if (theForm.cvc.value == "" || theForm.cvc.value.length < 3)
		{
			alert("Please enter your Credit Card Security Code.");
			document.getElementById('CVC').style.color = '#FF0000';
			theForm.cvc.focus();
			return false;
		}
		else
		{
			document.getElementById('CVC').style.color = '#000000';
		}
	}

	return true;
}

function prepValue(value)
{
	if (value != null && value.length > 0)
	{
		value = value.replace(new RegExp("[^A-Za-z0-9\\-_.,\\' ]", "g"), "");
	}
	
	return value;
}

function prepPassword(value)
{
	if (value != null && value.length > 0)
	{
		value = value.replace(new RegExp("[^A-Za-z0-9]", "g"), "");
	}
	
	return value;
}

function prepCC(value)
{
	if (value != null && value.length > 0)
	{
		if (value.indexOf("gamehouse") == 0) return value;
		value = value.replace(new RegExp("[^0-9\\- ]", "g"), "");
	}
	
	return value;
}

function prepNumber(value)
{
	if (value != null && value.length > 0)
	{
		value = value.replace(new RegExp("[^0-9\\(\\)\\- ]", "g"), "");
	}
	
	return value;
}

function prepEmail(emailAddr)
{
	if (emailAddr != null && emailAddr.length > 0)
	{
		emailAddr = emailAddr.replace(/ /g, "");
		emailAddr = emailAddr.replace(/,/g, ".");
		emailAddr = emailAddr.replace(/@@/g, "@");
		while (emailAddr.indexOf("..") != -1) emailAddr = emailAddr.replace(/\.\./g, ".");
		while (emailAddr.charAt(emailAddr.length - 1) == ".") emailAddr = emailAddr.slice(0, -1);
		if (emailAddr.indexOf("www.") == 0) emailAddr = emailAddr.slice(4);
		emailAddr = emailAddr.replace(new RegExp("[^A-Za-z0-9!@\$%\\-_.\\']", "g"), "");
	}
	
	return emailAddr;
}

function validateEmail(emailAddr)
{
	// invalid character definition
	var invalidChars = "\\(\\)\*<>@,;:\\\\\\\"\\.\\[\\]";
	
	// valid user or domain definition
	var validChars = "\[^\\s" + invalidChars + "\]";
	
	// quoted user definition
	var quotedUser = "(\"[^\"]*\")";
	
	// atom definition
	var atom = validChars + '+';
	
	// word definition
	var word = "(" + atom + "|" + quotedUser + ")";
	
	// valid user@domain pattern
	var emailPat = /^(.+)@(.+)$/;
	
	// valid user pattern
	var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
	
	// valid symbolic domain pattern
	var domainPat = new RegExp("^" + atom + "(\\." + atom +")*$");
	
	// valid ip domain pattern
	var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	
	// test user@domain pattern
	var matchArray = emailAddr.match(emailPat);
	if (matchArray == null)
	{
		alert("Please enter your correct Email Address.");
		return false
	}
	var user = matchArray[1];
	var domain = matchArray[2];

	// test user pattern
	if (user.match(userPat) == null)
	{
		// user is not valid
		alert("Please enter your correct email user name.");
		return false;
	}
	
	// test ip domiain pattern
	var IPArray = domain.match(ipDomainPat);
	if (IPArray != null)
	{
		for (var i = 1; i <= 4; i++)
		{
			if (IPArray[i] > 255)
			{
				alert("Please enter your correct email IP domain.");
				return false;
			}
		}
		return true;
	}

	// test symbolic domiain pattern
	var domainArray = domain.match(domainPat);
	if (domainArray == null)
	{
		alert("Please enter your correct email domain.");
		return false;
	}
	
	// test end of symbolic domiain
	var atomPat = new RegExp(atom, "g");
	var domArr = domain.match(atomPat);
	var len = domArr.length;
	if (domArr[domArr.length-1].length < 2 || domArr[domArr.length-1].length > 4)
	{
		alert("Please enter your correct email domain.");
		return false;
	}
	
	// test for host name preceding the domain
	if (len < 2)
	{
		alert("Please enter your correct email domain.");
		return false;
	}

	// valid email address
	return true;
}

