function CheckEmail(checkStr) {
// test if valid email address, must have @ and .
var checkEmail = "@.";
var EmailValid = false;
var EmailAt = false;
var EmailPeriod = false;
var EmailSpace = false;
var error = "";

if (checkStr.indexOf(" ") > -1) {
EmailSpace = true;
}

for (i = 0;i < checkStr.length;i++) {
ch = checkStr.charAt(i);

for (j = 0;j < checkEmail.length;j++) {

if (ch == checkEmail.charAt(j) && ch == "@")
EmailAt = true;

if (ch == checkEmail.charAt(j) && ch == ".")
EmailPeriod = true;

if (EmailAt && EmailPeriod)
break;

if (j == checkEmail.length)
break;
}

// if both the @ and . were in the string
if ((EmailAt) && (EmailPeriod) && (!EmailSpace)) {
EmailValid = true
break;
error = "";
	}
}


if (!EmailValid) {
error = "";

if ((!EmailAt) && (!EmailPeriod)) {
error += " - Email must contain an \"@\" and a \".\"\n";
}
if (!EmailAt) {
error += " - Email must contain an \"@\"\n";	
}
if (!EmailPeriod) {
error += " - Email must contain an \".\"\n";	
}
if (EmailSpace) {
error += " - Email must not contain a space\n";	
} else {
error = " - Email is invalid\n";	
	}
}

return error;
}

function TrimString(str) {
str  = this != window? this : str;
return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function isEmpty(TmpString) {

if (!TmpString) {
return true;	
}

TmpString = TrimString(TmpString);

if (TmpString.length <= 0) {
return true;
}

return false;
}

function convertToAscii(str) {
var str = str.toLowerCase();
var convert = "";

for (var i=0; i<str.length; i++) {
convert += str.charCodeAt(i);
}

return convert;
}

function showError(Errors) {
Errors = Errors.toLowerCase();
alert("The following error(s) occurred:\n" + Errors.substring(Errors,Errors.length-1) + "\n\nSorry can not Process the form");
return false;
}

function CheckFormDefaults(FormName) {
var alertsay = "";
if ((document.getElementById(FormName).FirstName) && (((isEmpty(document.getElementById(FormName).FirstName.value)) || (document.getElementById(FormName).FirstName.value == "First Name")))) {
	alertsay += "- Please enter your first name\n";
}
if ((document.getElementById(FormName).LastName) && (((isEmpty(document.getElementById(FormName).LastName.value)) || (document.getElementById(FormName).LastName.value == "Last Name")))) {
	alertsay += "- Please enter your last name\n";
}
if ((document.getElementById(FormName).EmailAddress) && (((isEmpty(document.getElementById(FormName).EmailAddress.value)) || (document.getElementById(FormName).EmailAddress.value == "Email Address")))) {
	alertsay += "- Please enter a email address\n";
} else if ((document.getElementById(FormName).EmailAddress) && (((!isEmpty(document.getElementById(FormName).EmailAddress.value)) || (document.getElementById(FormName).EmailAddress.value != "Email Address")))) {
	alertsay += CheckEmail(document.getElementById(FormName).EmailAddress.value);
}
if ((document.getElementById(FormName).SecurityImage) && (isEmpty(document.getElementById(FormName).SecurityImage.value))) {
	alertsay += "- Please enter the security code\n";
} else if ((document.getElementById(FormName).SecurityImage) && (!isEmpty(document.getElementById(FormName).SecurityImage.value))) {
	TmpSecurityCode = convertToAscii(document.getElementById(FormName).SecurityImage.value);
	if (TmpSecurityCode != document.getElementById(FormName).SockVar.value) {
		alertsay += "- The security code does not match\n";
	}
}
return alertsay;
}

function CheckContact() {
var alertsay = CheckFormDefaults("ContactForm");
	if ((isEmpty(document.getElementById("ContactForm").Comments.value)) || (document.getElementById("ContactForm").Comments.value == "Comments")) {
		alertsay += "- Please enter your comments\n";
	}
	if (alertsay) {
		showError(alertsay);
		return false;
	} else {
		return true;
	}
}

function CheckAdvertising() {
var alertsay = CheckFormDefaults("AdvertisingForm");
	if (alertsay) {
		showError(alertsay);
		return false;
	} else {
		return true;
	}
}

function CheckArea() {
	var alertsay = "";
	if (isEmpty(document.getElementById("editForm").Area.value)) {
		alertsay += "- Please Enter the Area Name\n";
	}
	if (alertsay) {
		showError(alertsay);
		return false;
	} else {
		return true;
	}
}

function CheckArticle() {
	var alertsay = "";
	if (isEmpty(document.getElementById("editForm").LeagueID.value)) {
		alertsay += "- Please select the League\n";
	}
	if (isEmpty(document.getElementById("editForm").LeagueTypeID.value)) {
		alertsay += "- Please select the League Type\n";
	}
	if (isEmpty(document.getElementById("editForm").Name.value)) {
		alertsay += "- Please Enter the Name\n";
	}
	if (isEmpty(document.getElementById("editForm").DatePosted.value)) {
		alertsay += "- Please Enter the DatePosted\n";
	}
	if (alertsay) {
		showError(alertsay);
		return false;
	} else {
		return true;
	}
}

function CheckReview() {
	var alertsay = "";
	if (isEmpty(document.getElementById("editForm").CategoryID.value)) {
		alertsay += "- Please select the Category\n";
	}
	if (isEmpty(document.getElementById("editForm").Company.value)) {
		alertsay += "- Please Enter the Company\n";
	}
	if (isEmpty(document.getElementById("editForm").Rating.value)) {
		alertsay += "- Please select the Rating\n";
	}
	if ((isEmpty(document.getElementById("editForm").NewImage.value)) && (isEmpty(document.getElementById("editForm").ImageID.value))) {
		alertsay += "- Please select a company image\n";
	}
	if (alertsay) {
		showError(alertsay);
		return false;
	} else {
		return true;
	}
}

function CheckBanner() {
	var alertsay = "";
	if (isEmpty(document.getElementById("editForm").CategoryID.value)) {
		alertsay += "- Please select the Category\n";
	}
	if (isEmpty(document.getElementById("editForm").Company.value)) {
		alertsay += "- Please Enter the Company\n";
	}
	if (isEmpty(document.getElementById("editForm").BannerType.value)) {
		alertsay += "- Please select the banner type\n";	
	} else if (document.getElementById("editForm").BannerType.value != "Code") {
		
	if ((isEmpty(document.getElementById("editForm").NewImage.value)) && (isEmpty(document.getElementById("editForm").Banner.value))) {
		alertsay += "- Please select a banner image\n";
		}
	} else {
	
	if ((isEmpty(document.getElementById("editForm").BannerCode.value))) {
		alertsay += "- Please enter the code for the banner\n";
		}
	}
	if (isEmpty(document.getElementById("editForm").BannerPosition.value)) {
		alertsay += "- Please select the Banner Position\n";
	}
	if (alertsay) {
		showError(alertsay);
		return false;
	} else {
		return true;
	}
}

function CheckGame() {
	var alertsay = "";
	if (isEmpty(document.getElementById("editForm").CategoryID.value)) {
		alertsay += "- Please select the Category\n";
	}
	if (isEmpty(document.getElementById("editForm").Company.value)) {
		alertsay += "- Please Enter the Company\n";
	}
	if (isEmpty(document.getElementById("editForm").GameTitle.value)) {
		alertsay += "- Please Enter the GameTitle\n";
	}
	if ((isEmpty(document.getElementById("editForm").NewImage.value)) && (isEmpty(document.getElementById("editForm").GameImageID.value))) {
		alertsay += "- Please select a game image\n";
	}
	if (alertsay) {
		showError(alertsay);
		return false;
	} else {
		return true;
	}
}

function CheckUser() {
var alertsay = "";
	if (isEmpty(document.getElementById("editForm").FullName.value)) {
		alertsay += "- Please enter the Full Name\n";
	}
	if (isEmpty(document.getElementById("editForm").Email.value)) {
		alertsay += "- Please enter a email address\n";
	} else {
		alertsay += CheckEmail(document.getElementById("editForm").Email.value);
	}
	if ((!isEmpty(document.getElementById("editForm").Password.value)) || (document.getElementById("editForm").ProcessType.value == "AddRecord")) {
		if (isEmpty(document.getElementById("editForm").Password.value)) {
			alertsay += "- Please enter a Password\n";
		} else if ((!isEmpty(document.getElementById("editForm").Password.value)) && (isEmpty(document.getElementById("editForm").ConfirmPassword.value))) {
			alertsay += "- Please enter your Confirm your Password\n";
		} else if (document.getElementById("editForm").Password.value != document.getElementById("editForm").ConfirmPassword.value) {
			alertsay += "- Your Passwords do not match\n";
		}
	}
	if (alertsay) {
		showError(alertsay);
		return false;
	} else {
		return true;
	}
}

function addToSelect(selectBox, value, ID) {
	window.opener.addToOptionList(selectBox, value, ID)
	self.close();
}

function addToOptionList(selectBox, optionValue, optionText) {
	optionList = document.getElementById(selectBox);
	optionList[optionList.length] = new Option(optionValue, optionText);
}

function setCompany(Company) {
	document.getElementById("Company").value = Company;
}

function activeBoxes() {
	bannerTitle = document.getElementById("BannerTitle");
	bannerURL = document.getElementById("URL");
	bannerIsImage = document.getElementById("BannerAsImage");
	bannerIsCode = document.getElementById("BannerAsCode");
	if (!isEmpty(document.getElementById("BannerType").value)) {
		bannerType = document.getElementById("BannerType").value;
		if (bannerType == "Flash") {
			bannerTitle.disabled  = true;
			bannerURL.disabled  = true;
			bannerIsImage.style.display = "block";
			bannerIsCode.style.display  = "none";
		}	else if (bannerType == "Code") {
			bannerTitle.disabled  = true;
			bannerURL.disabled  = true;
			bannerIsImage.style.display = "none";
			bannerIsCode.style.display  = "block";
		}	else {
			bannerTitle.disabled  = false;
			bannerURL.disabled  = false;
			bannerIsImage.style.display = "block";
			bannerIsCode.style.display  = "none";
		}
	}
}

function formControls() {
	if (document.getElementById("FirstName")) {
		FirstName = document.getElementById("FirstName");
		FirstName.onfocus = new Function("focusValues(FirstName, 'First Name')");
		FirstName.onblur = new Function("blurValues(FirstName, 'First Name')");
	}
	
	if (document.getElementById("LastName")) {
		LastName = document.getElementById("LastName");
		LastName.onfocus = new Function("focusValues(LastName, 'Last Name')");
		LastName.onblur = new Function("blurValues(LastName, 'Last Name')");
	}
	if (document.getElementById("EmailAddress")) {
		EmailAddress = document.getElementById("EmailAddress");
		EmailAddress.onfocus = new Function("focusValues(EmailAddress, 'Email Address')");
		EmailAddress.onblur = new Function("blurValues(EmailAddress, 'Email Address')");
	}
	
	if (document.getElementById("Comments")) {
		Comments = document.getElementById("Comments");
		Comments.onfocus = new Function("focusValues(Comments, 'Comments')");
		Comments.onblur = new Function("blurValues(Comments, 'Comments')");
	}
		if (document.getElementById("Enquiry")) {
		Enquiry = document.getElementById("Enquiry");
		Enquiry.onfocus = new Function("focusValues(Enquiry, 'Your Enquiry')");
		Enquiry.onblur = new Function("blurValues(Enquiry, 'Your Enquiry')");
	}
	if (document.getElementById("SecurityImage")) {
		SecurityImage = document.getElementById("SecurityImage");
		SecurityImage.value = "";
	}
	if (document.getElementById("YourName")) {
		YourName = document.getElementById("YourName");
		YourName.onfocus = new Function("focusValues(YourName, 'Your Name')");
		YourName.onblur = new Function("blurValues(YourName, 'Your Name')");
	}
	if (document.getElementById("YourEmailAddress")) {
		YourEmailAddress = document.getElementById("YourEmailAddress");
		YourEmailAddress.onfocus= new Function("focusValues(YourEmailAddress, 'Your Email Address')");
		YourEmailAddress.onblur = new Function("blurValues(YourEmailAddress, 'Your Email Address')");
	}
	if (document.getElementById("FriendName")) {
		FriendName = document.getElementById("FriendName");
		FriendName.onfocus = new Function("focusValues(FriendName, 'Friends Name')");
		FriendName.onblur = new Function("blurValues(FriendName, 'Friends Name')");
	}
	if (document.getElementById("FriendEmailAddress")) {
		FriendEmailAddress = document.getElementById("FriendEmailAddress");
		FriendEmailAddress.onfocus= new Function("focusValues(FriendEmailAddress, 'Friends Email Address')");
		FriendEmailAddress.onblur= new Function("blurValues(FriendEmailAddress, 'Friends Email Address')");
	}
	if (document.getElementById("Message")) {
		Message = document.getElementById("Message");
		Message.onfocus = new Function("focusValues(Message, 'Your Message')");
		Message.onblur = new Function("blurValues(Message, 'Your Message')");
	}
	if ((document.getElementById("BannerCode")) && document.getElementById("BannerType")) {
		bannerType = document.getElementById("BannerType");
		bannerType.onchange = new Function("activeBoxes()");
		activeBoxes();
	}
}

function focusValues(formElement, value) {
	if (formElement.value == value) {
		formElement.value = "";
	}
}

function blurValues(formElement, value) {
	if (formElement.value == "") {
		formElement.value = value;
	}
}