// JavaScript Document

function checkEditProfile(thisForm) {
	var why = "";
	var changes = "";
	why += checkUsername(thisForm.editusername.value, 'username');
	why += checkUsername(thisForm.editforumusername.value, 'forum username');
	why += isDifferent(thisForm.editpassword1.value, thisForm.editpassword2.value, 'passwords');
	if (thisForm.editpassword1.value != "") {
		why += checkPassword(thisForm.editpassword1.value);
	}
	why += checkEmail(thisForm.editemail1.value);
	why += isDifferent(thisForm.editemail1.value, thisForm.editemail2.value, 'email addresses');
	
	changes += shouldbeDifferent(thisForm.editemail1.value, thisForm.editemail_hidden.value, 'Email ');
	changes += shouldbeDifferent(thisForm.editusername.value, thisForm.editusername_hidden.value, 'Username ');
	changes += shouldbeDifferent(thisForm.editforumusername.value, thisForm.editforumusername_hidden.value, 'Forum username ');
	if (thisForm.editpassword1.value != "") {
		changes += "You have entered a new password.\n";	
	}
	
	if (why != "") {
		//alert(why);
		alert(why);
		return false;
	}
	else if (changes == "") {
		alert("You have not made any changes, you must change something if you want to edit your account.");
		return false;
	}
	else {
		if (fRet = confirm('You have made the following changes.\n\n' + changes + '\nDo you wish to continue?')) {
			return true;
		}
	}
	return false;
}

function resendPasswordCheck(thisForm) {
	var why = "";
	why += checkUsername(thisForm.resendusername.value, 'username');
	why += checkEmail(thisForm.resendemail.value);
	if (why != "") {
		 //alert(why);
		 alert(why);
		 return false;
	}
	return true;
}

function checkContactForm(thisForm) {
	var why = "";
	why += checkName(thisForm.name.value, 'name');
	why += checkEmail(thisForm.email1.value);
	why += isDifferent(thisForm.email1.value, thisForm.email2.value, 'email addresses');
	if (thisForm.subject.value == "") {
		why += "You must select a subject.\n";
	}
	if (thisForm.contacttext.value == "") {
		why += "You have not entered anything into the main text.\n";
	}
	if (why != "") {
		 //alert(why);
		 alert(why);
		 return false;
	}
	return true;
}

function checkAddUpdateForm(thisForm) {
	var why = "";
	if (thisForm.positionfinished.value == "") {
		why += "You must select a position.\n";
	}
	if (thisForm.leaguemovement.value == "") {
		why += "You must select your league movement.\n";
	}
	if ((thisForm.positionfinished.value != "") && (thisForm.leaguemovement.value != "")) {
		if ((thisForm.positionfinished.value > 7) && (thisForm.leaguemovement.value == "promoted")) {
			why += "You did not finish in a high enough position to be promoted.  Check your selection.\n";
		}
		if ((thisForm.positionfinished.value < 18) && (thisForm.leaguemovement.value == "relegated") && (thisForm.leagueid.value == "1")) {
			why += "You did not finish in a low enough position to be relegated.  Check your selection.\n";
		}
		if ((thisForm.positionfinished.value < 22) && (thisForm.leaguemovement.value == "relegated") && (thisForm.leagueid.value == "2")) {
			why += "You did not finish in a low enough position to be relegated.  Check your selection.\n";
		}
		if ((thisForm.positionfinished.value < 21) && (thisForm.leaguemovement.value == "relegated") && (thisForm.leagueid.value == "3")) {
			why += "You did not finish in a low enough position to be relegated.  Check your selection.\n";
		}
		if ((thisForm.positionfinished.value < 23) && (thisForm.leaguemovement.value == "relegated") && (thisForm.leagueid.value == "4")) {
			why += "You did not finish in a low enough position to be relegated.  Check your selection.\n";
		}
		if ((thisForm.positionfinished.value < 21) && (thisForm.leaguemovement.value == "relegated") && (thisForm.leagueid.value == "5")) {
			why += "You did not finish in a low enough position to be relegated.  Check your selection.\n";
		}
		if ((thisForm.positionfinished.value > 17) && (thisForm.leaguemovement.value != "relegated") && (thisForm.leagueid.value == "1")) {
			why += "If you lower than 17th place, you must of been relegated.  Check your selection.\n";
		}
		if ((thisForm.positionfinished.value > 21) && (thisForm.leaguemovement.value != "relegated") && (thisForm.leagueid.value == "2")) {
			why += "If you lower than 21st place, you must of been relegated.  Check your selection.\n";
		}
		if ((thisForm.positionfinished.value > 20) && (thisForm.leaguemovement.value != "relegated") && (thisForm.leagueid.value == "3")) {
			why += "If you lower than 20th place, you must of been relegated.  Check your selection.\n";
		}
		if ((thisForm.positionfinished.value > 22) && (thisForm.leaguemovement.value != "relegated") && (thisForm.leagueid.value == "4")) {
			why += "If you lower than 22nd place, you must of been relegated.  Check your selection.\n";
		}
		if ((thisForm.positionfinished.value > 20) && (thisForm.leaguemovement.value != "relegated") && (thisForm.leagueid.value == "5")) {
			why += "If you lower than 20th place, you must of been relegated.  Check your selection.\n";
		}
		if ((thisForm.leaguemovement.value == "relegated") && (thisForm.leagueid.value > "5")) {
			why += "You can not be relegated from here and remain in the game.  Check your selection, or if you want to end the game go to options.\n";
		}
		if ((thisForm.positionfinished.value == 2) && (thisForm.leaguemovement.value != "promoted") && ((thisForm.leagueid.value == "2") || (thisForm.leagueid.value == "3"))) {
			why += "You finished 2nd, so you must of been promoted.  Check your selection.\n";
		}
		if ((thisForm.positionfinished.value == 3) && (thisForm.leaguemovement.value != "promoted") && (thisForm.leagueid.value == "4")) {
			why += "You finished 3rd, so you must of been promoted.  Check your selection.\n";
		}
		if ((thisForm.positionfinished.value == 1) && (thisForm.leaguemovement.value != "promoted") && (thisForm.leagueid.value != "1")) {
			why += "You finished 1st, so you must of been promoted.  Check your selection.\n";
		}
	}
	if (why) {
		alert(why);
		return false;
	}
	else {
		return true;
	}
}

function checkCreateGameForm(thisForm) {
	var why = "";
	if (thisForm.team.value == "") {
		why += "You must select a team.\n";
	}
	if (thisForm.season.value == "") {
		why += "You must select a season.\n";
	}
	if (thisForm.league.value == "") {
		why += "You must select a league.\n";
	}
	if (why) {
		alert(why);
		return false;
	}
	else {
		return true;
	}
}


function checkNumbers(thisForm) {
	var why = "";
	if (thisForm.validationcode.value.length < 5) {
		alert(thisForm.validationcode.value.length);
		why += "The code must be at least 5 numbers long.\n"
	}
	var numericExpression = /^[0-9]+$/;
	if(!thisForm.validationcode.value.match(numericExpression)) {
		thisForm.validationcode.value = "";
		why += "Only numbers can be entered in this box.\n"
	}
	if (why != "") {
		thisForm.validationcode.value = "";
		alert(why);
		return false;
	}
	else {
		return true;
	}
}

function clickclear(thisfield, defaulttext) {
	if (thisfield.value == defaulttext) {
		thisfield.value = "";
	}
}

function clickrecall(thisfield, defaulttext) {
	if (thisfield.value == "") {
		thisfield.value = defaulttext;
	}
}

function checkRegisterForm(theForm) {
	var why = "";
	why += checkUsername(theForm.username.value, 'username');
	why += checkUsername(theForm.forumusername.value, 'forum username');
	why += isDifferent(theForm.password1.value, theForm.password2.value, 'passwords');
	why += checkPassword(theForm.password1.value);
	why += checkEmail(theForm.email1.value);
	why += isDifferent(theForm.email1.value, theForm.email2.value, 'email addresses');
	
	if (why != "") {
		 //alert(why);
		 alert(why);
		 return false;
	}
	return true;
}

function checkName (strng, name) {
 	var error = "";
 	if (strng == "") {
		error = "You didn't enter a " + name + ".\n";
 	}
 	if ((strng.length < 4) || (strng.length > 20)) {
    error = "The "+ name +" is the wrong length, must be between 4 and 20 long.\n";
	}
	var illegalChars = /\W\s/;
  // allow only letters, numbers, underscores and spaces
  if (illegalChars.test(strng)) {
  	error = "The " + name + " contains illegal characters.\n";
   } 
	 return error;
}


function checkUsername (strng, name) {
 	var error = "";
 	if (strng == "") {
		error = "You didn't enter a " + name + ".\n";
 	}
 	if ((strng.length < 4) || (strng.length > 20)) {
    error = "The "+ name +" is the wrong length, must be between 4 and 20 long.\n";
	}
	var illegalChars = /\W/;
  // allow only letters, numbers, and underscores
  if (illegalChars.test(strng)) {
  	error = "The " + name + " contains illegal characters.\n";
   } 
	 return error;
}

function isDifferent(strng1, strng2, name) {
  var error = "";
  if (strng1 != strng2) {
     error = "The " + name + " are diffrent.\n";
  }
	return error;
}

function shouldbeDifferent(strng1, strng2, name) {
  if (strng1 != strng2) {
     return name + " has been changed.\n";
  }
	else {
		return "";
	}
}

function checkPassword (strng) {
	var error = "";
	if (strng == "") {
		error = "You didn't enter a password.\n";
	}
	var illegalChars = /[\W_]/; // allow only letters and numbers
	if ((strng.length < 6) || (strng.length > 20)) {
		error = "The password is the wrong length, 6-20 chars.\n";
	}
	else if (illegalChars.test(strng)) {
		error = "The password contains illegal characters.\n";
	}
	else if (!((strng.search(/(a-z)+/))
		&& (strng.search(/(A-Z)+/))
		&& (strng.search(/(0-9)+/)))) {
		error = "The password must contain at least one uppercase letter, one lowercase letter,	and one numeral.\n";
	}
	return error;
}

function checkEmail (strng) {
	var error = "";
	var emailFilter=/^.+@.+\..{2,3}$/;
	if (!(emailFilter.test(strng))) { 
				 error = "Please enter a valid email address.\n";
	}
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
	if (strng.match(illegalChars)) {
		 error = "The email address contains illegal characters.\n";
	}
	return error;
}

















//Dropshadow Stuff
addEvent(window,"load",initDropShadowYellow);
addEvent(window,"load",initDropShadowWhite);

function initDropShadowYellow() {
    if (!document.createElement) return;
    
    // Sigh, IE doesn't do getElementsByTagName("*")
    if (document.all) {
        var els = document.all;
    } else {
        var els = document.getElementsByTagName("*");
    }
    for (i=0;i<els.length;i++) {
        if ((' '+els[i].className+' ').indexOf(' dropshadowyellow ') != -1) {
            DS_process(els[i])
        }
    }
}

function initDropShadowWhite() {
    if (!document.createElement) return;
    
    // Sigh, IE doesn't do getElementsByTagName("*")
    if (document.all) {
        var els = document.all;
    } else {
        var els = document.getElementsByTagName("*");
    }
    for (i=0;i<els.length;i++) {
        if ((' '+els[i].className+' ').indexOf(' dropshadowwhite ') != -1) {
            DS_process(els[i])
        }
    }
}

function DS_process(e) {
    // Make a duplicate of this element, with all its subelements
    var nel = e.cloneNode(1);
    // Set its class to shadowed
    nel.className = "shadowed";
    nel.className += e.className.replace('dropshadow','');
    // Set floating text colour
    textColour = e.getAttribute("textColour");
    if (textColour) nel.style.color = textColour;
    textColor = e.getAttribute("textColor");
    if (textColor) nel.style.color = textColor;
    // Add it to the document
    e.parentNode.insertBefore(nel,e);
    i++;
    nel.style.top = (e.offsetTop - 1) + "px";
    nel.style.left = (e.offsetLeft - 2) + "px";
}

function addEvent(obj, evType, fn) {
  /* adds an eventListener for browsers which support it
     Written by Scott Andrew: nice one, Scott */
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
  } else if (obj.attachEvent){
	var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
	return false;
  }
}
