
////////////////////////////////////////
//
// FORM VALIDATION
//
function hideUnhideEmail(hideCheckBox)
{
	var frm = document.emailSponsors;
	var isChecked = hideCheckBox.checked;
	var selected = '';
	
	switch (hideCheckBox.name) {
		case "huOne":		selected = "recpientRowTwo";		break;
		case "huTwo":		selected = "recpientRowThree";		break;
		case "huThree":		selected = "recpientRowFour";		break;
		case "huFour":		selected = "recpientRowFive";		break;
	}

	if (frm) {
		if (document.getElementById) {

			var stuff = document.getElementById(selected);
			
			if (stuff) {
				if (isChecked)
					stuff.style.display = 'block';
				else
					stuff.style.display = 'none';
			}
		}		
	}
}


function hideUnhideStuff(hideCheckBox)
{
	var frm = document.emailSponsors;
	var isChecked = hideCheckBox.checked;

	if (frm) {
		if (document.getElementById) {

			var mailheader = document.getElementById("premadeEmailHeader");
			var customSubject = document.getElementById("customSubject");
			var mailfooter = document.getElementById("premadeEmailFooter");

			if (mailheader && customSubject && mailfooter) {
				if (isChecked) {
					mailheader.style.display = 'none';
					customSubject.style.display = 'block';
					mailfooter.style.display = 'none';
				}
				else {
					mailheader.style.display = 'block';
					customSubject.style.display = 'none';
					mailfooter.style.display = 'block';
				}
			}
		}		
	}
}

function trimAll(sString) 
{
	while (sString.substring(0,1) == ' ')
		sString = sString.substring(1, sString.length);

	while (sString.substring(sString.length - 1, sString.length) == ' ')
		sString = sString.substring(0, sString.length - 1);
	
	return sString;
}

function appendFirstToNewUserName(firstNameTextBox)
{
	var frm = document.signup;
	var tFirstName = trimAll(firstNameTextBox.value);
	var tLastName = "";
	
	if (frm) {
		for (var j = 0; j < frm.length; j++) {
			if (frm.elements[j].name == "lastname")
				tLastName = trimAll(frm.elements[j].value);
		}

		for (var j = 0; j < frm.length; j++) {
			if (frm.elements[j].name == "username")
				if (tLastName)
					frm.elements[j].value = tFirstName+" "+tLastName;
					//frm.elements[j].value = tFirstName+tLastName;
				else
					frm.elements[j].value = tFirstName;
		}
	}
}

function appendLastToNewUserName(lastNameTextBox)
{
	var frm = document.signup;
	var tLastName = trimAll(lastNameTextBox.value);
	var tFirstName = "";
	
	if (frm) {
		for (var j = 0; j < frm.length; j++) {
			if (frm.elements[j].name == "firstname")
				tFirstName = trimAll(frm.elements[j].value);
		}

		for (var j = 0; j < frm.length; j++) {
			if (frm.elements[j].name == "username")
				if (tFirstName)
					frm.elements[j].value = tFirstName+" "+tLastName;
					//frm.elements[j].value = tFirstName+tLastName;
				else
					frm.elements[j].value = tLastName;
		}
	}
}


function validateUJRSFormCheckbox(frmCheckBoxButton)
{
	var frm = document.UJRSForm;
	var tValue = frmCheckBoxButton.value;
	var tChecked = frmCheckBoxButton.checked;
		
	if (frm) {
		if (tChecked == true) {
			// uncheck the radio next to the newly checked checkbox
			for (var i = 0; i < frm.length; i++) {
				if (frm.elements[i].name == "ujrsFrmRadio[]") {
					if (frm.elements[i].value == tValue)
						frm.elements[i].checked = false;
				}
			}
		}			
	}
}


function validateUJRSFormRadio(frmRadioButton)
{
	var frm = document.UJRSForm;
	var tValue = frmRadioButton.value;
	var tChecked = frmRadioButton.checked;

	if (frm) {
		if (tChecked == true) {
			// uncheck the checkbox next to the newly checked radio, and check all other checkboxes
			for (var i = 0; i < frm.length; i++) {
				if (frm.elements[i].name == "ujrsFrmChkBox[]") {
					if (frm.elements[i].value == tValue)
						frm.elements[i].checked = false;
					else
						frm.elements[i].checked = true;
				}
			}
		}			
	}
}


function displayUJRSFormConfirmation(pUJRSForm)
{
	var cancelCount = 0;
	var rejectionCount = 0;
	var joinOccurring = false;
	
	for (var i = 0; i < pUJRSForm.length; i++) {
		if (pUJRSForm.elements[i].name == "ujrsFrmCancelReqChkBox[]") {
			if (pUJRSForm.elements[i].checked == true)
				cancelCount++;
		}
		if (pUJRSForm.elements[i].name == "ujrsFrmChkBox[]") {
			if (pUJRSForm.elements[i].checked == true)
				rejectionCount++;
		}
		if (pUJRSForm.elements[i].name == "ujrsFrmRadio[]") {
			if (pUJRSForm.elements[i].checked == true)
				joinOccurring = true;
		}
	}

	if ((cancelCount == 0) && (rejectionCount == 0) && (joinOccurring == false))
		return true;
	else if (((cancelCount == 1) || (rejectionCount > 0)) && (joinOccurring == false))
		return confirm("You are choosing to cancel " + cancelCount + " team join request(s).\n\nYou are choosing to reject " + rejectionCount + " team join invitation(s).\n\nYou will need to submit a new join request when you  \ncome to regret your decision.\n\nAre you sure you want to do this?");
	else if (joinOccurring == true)
		return confirm("You are choosing to join a team and cancel all other  \ninvitations and requests.\n\nYou will need to quit the team you are now joining  \nand submit a new join team request when you come  \nto regret your decision.\n\nAre you sure you want to do this?");

	// should never hit this, but we should return false, just in case.
	return false;
}


function validateTJRSFormCancelCheckbox(frmCancelBox)
{
	var frm = document.TJRSForm;
	var tValue = frmCancelBox.value;
	var tChecked = frmCancelBox.checked;
	var memberIncrement = 0;
		
	if (frm) {
		if (tChecked == true) {
			// uncheck the radio next to the newly checked checkbox
			for (var i = 0; i < frm.length; i++) {
				if (frm.elements[i].name == "tjrsFrmAccReqChkBox[]") {
					if ((frm.elements[i].value == tValue) && (frm.elements[i].checked == true)) {
						frm.elements[i].checked = false;
						memberIncrement++;
					}
				}
				if (frm.elements[i].name == "tjrsFrmTxtArea") {
					frm.elements[i].value = parseInt(frm.elements[i].value) + memberIncrement;
				}
			}
		}			
	}
}


function validateTJRSFormAcceptCheckbox(acceptBox)
{
	var frm = document.TJRSForm;
	var tValue = acceptBox.value;
	var tChecked = acceptBox.checked;
	var spotsLeft = 0;
	var j = 0;
	
	if (frm) {
		for (j = 0; j < frm.length; j++) {
			if (frm.elements[j].name == "tjrsFrmTxtArea")
				spotsLeft = parseInt(frm.elements[j].value);
		}
		
		if ((tChecked == true) && (spotsLeft == 0)) {
			acceptBox.checked = false;
			return;
		}	

		// uncheck the radio next to the newly checked checkbox
		for (var i = 0; i < frm.length; i++) {
			if (tChecked == true) {
				if (frm.elements[i].name == "tjrsFrmJoinReqCanChkBox[]") {/*"tjrsFrmCanChkBox[]") {*/
					if (frm.elements[i].value == tValue) {
						frm.elements[i].checked = false;
					}
				}
			}

			if (frm.elements[i].name == "tjrsFrmTxtArea") {
				if (tChecked == true)
					frm.elements[i].value = (spotsLeft - 1);
				else
					frm.elements[i].value = (spotsLeft + 1);
			}
		}
	}
}


function displayTJRSFormConfirmation(pTJRSForm)
{
	var cancelCount = 0;
	var rejectionCount = 0;
	var acceptCount = 0;
	
	for (var i = 0; i < pTJRSForm.length; i++) {
		if (pTJRSForm.elements[i].name == "tjrsFrmInvCanChkBox[]") {/*== "tjrsFrmCanChkBox[]") {*/
			if (pTJRSForm.elements[i].checked == true)
				cancelCount++;
		}
		if (pTJRSForm.elements[i].name == "tjrsFrmJoinReqCanChkBox[]") {
			if (pTJRSForm.elements[i].checked == true)
				rejectionCount++;
		}
		if (pTJRSForm.elements[i].name == "tjrsFrmAccReqChkBox[]") {
			if (pTJRSForm.elements[i].checked == true)
				acceptCount++;
		}
	}

	if ((cancelCount == 0) && (rejectionCount == 0) && (acceptCount == 0))
		return true;
	else if ((cancelCount > 0) || (rejectionCount > 0) || (acceptCount > 0)) {
		return confirm("You are choosing to cancel " + cancelCount + " invitation(s) that you've sent to join your team.\n\nYou are choosing to reject " + rejectionCount + " team join request(s).\n\nYou are choosing to accept " + acceptCount + " team join request(s).\n\nYou will need to submit a new join request for each request  \nyou might have accidentally deleted.  You may also have to kick\npeople off your team.\n\nAre you sure you want to do this?");
	}
	
	// should never hit this, but we should return false, just in case.
	return false;
}


function validateJTIFormInviteCheckbox(inviteBox)
{
	var frm = document.JTIForm;
	var tValue = inviteBox.value;
	var tChecked = inviteBox.checked;
	var spotsLeft = 0;
	
	if (frm) {
		// uncheck the radio next to the newly checked checkbox
		for (var i = 0; i < frm.length; i++) {
			if (frm.elements[i].name == "jtiFrmTxtArea") {
				spotsLeft = parseInt(frm.elements[i].value);

				if (tChecked == true) {
					if (spotsLeft == 0) {
						inviteBox.checked = false;
						return;
					}
					else
						frm.elements[i].value = (spotsLeft - 1);
				}
				else
					frm.elements[i].value = (spotsLeft + 1);
			}
		}
	}
}


function validateMTRFormAltContactCheckbox(altConBox)
{
	var frm = document.MTRForm;
	var tValue = altConBox.value;
	var tChecked = altConBox.checked;
	var spotsLeft = 0;
	var altLeaderCount = 0;
	var altLeaderIncCount = 0;
	var numMemCount = 0;
	
	if (frm) {
		for (var j = 0; j < frm.length; j++) {
			if (frm.elements[j].name == "mtrFrmAltLdrTxtArea")
				altLeaderCount = parseInt(frm.elements[j].value);
		}
		
		if (tChecked == true) {
			// our max number of alts is 2.  Uncheck the box if the leader tries to exceed it.
			if (altLeaderCount < 2) {
				altLeaderCount = altLeaderCount + 1;
			}
			else {
				altConBox.checked = false;
				return;
			}
		}
		else
			altLeaderCount = altLeaderCount - 1;
		
		for (var i = 0; i < frm.length; i++) {
			// if we've checked a kick box, and the corresponding alt leader box is
			// checked, uncheck it to show the completeness of a kick
			if (frm.elements[i].name == "mtrFrmKickCheckBox[]") {
				if (tChecked == true) {
					if ((frm.elements[i].value == tValue) && (frm.elements[i].checked == true)) {
						frm.elements[i].checked = false;
						numMemCount++;
					}
				}
			}

			if (frm.elements[i].name == "mtrFrmChangeLeaderRadio[]") {
				if (tChecked == true) {
					if ((frm.elements[i].value == tValue) && (frm.elements[i].checked == true)) {
						frm.elements[i].checked = false;
					}
				}
			}

			if (frm.elements[i].name == "mtrFrmNumMemsTxtArea") {
				spotsLeft = parseInt(frm.elements[i].value);

				if (tChecked == true)
					frm.elements[i].value = (spotsLeft - numMemCount);
			}

			// update the alt leaders text area
			if (frm.elements[i].name == "mtrFrmAltLdrTxtArea")
				frm.elements[i].value = altLeaderCount;
		}
	}
}

function validateMTRFormKickUserCheckbox(kickBox, currentRole)
{
	var frm = document.MTRForm;
	var tValue = kickBox.value;
	var tChecked = kickBox.checked;
	var spotsLeft = 0;
	var altLeaderCount = 0;
	var altLeaderDecCount = 0;
	var isAltLeader = false;
	
	if (currentRole == 2)
		isAltLeader = true;
	
	if (frm) {
		for (var i = 0; i < frm.length; i++) {
			// if we've checked a kick box, and the corresponding alt leader box is
			// checked, uncheck it to show the completeness of a kick
			if (frm.elements[i].name == "mtrFrmAltCheckBox[]") {
				if (tChecked == true) {
					if ((frm.elements[i].value == tValue) && (frm.elements[i].checked == true)) {
						frm.elements[i].checked = false;
						altLeaderDecCount++;
					}
				}
				else {
					// if the leader unchecked the kick box AND the user is currently an alt leader, we
					// need to display that he still is!
					if (isAltLeader && (frm.elements[i].value == tValue)) {
						frm.elements[i].checked = true;
						altLeaderDecCount--;
					}
				}
			}
			
			if (frm.elements[i].name == "mtrFrmChangeLeaderRadio[]") {
				if (tChecked == true) {
					if ((frm.elements[i].value == tValue) && (frm.elements[i].checked == true)) {
						frm.elements[i].checked = false;
					}
				}
			}

			// update the num members text area to show how many open
			// roster spots they'll have
			if (frm.elements[i].name == "mtrFrmNumMemsTxtArea") {
				spotsLeft = parseInt(frm.elements[i].value);

				if (tChecked == true)
					frm.elements[i].value = (spotsLeft + 1);
				else
					frm.elements[i].value = (spotsLeft - 1);
			}

			// update the alt leaders text area
			if (frm.elements[i].name == "mtrFrmAltLdrTxtArea") {
				altLeaderCount = parseInt(frm.elements[i].value);
				frm.elements[i].value = altLeaderCount - altLeaderDecCount;
			}
		}
	}
}

function validateMTRFormChangeLeaderRadio(changeLeaderRadio, currentRole)
{
	var frm = document.MTRForm;
	var tValue = changeLeaderRadio.value;
	var tChecked = changeLeaderRadio.checked;
	var spotsLeft = 0;
	var altLeaderCount = 0;
	var altLeaderDecCount = 0;
	var isAltLeader = false;
	var numMemCount = 0;
	
	if (currentRole == 2)
		isAltLeader = true;
	
	if (frm) {
		for (var i = 0; i < frm.length; i++) {
			if (frm.elements[i].name == "mtrFrmKickCheckBox[]") {
				if (tChecked == true) {
					if ((frm.elements[i].value == tValue) && (frm.elements[i].checked == true)) {
						frm.elements[i].checked = false;
						numMemCount++;
					}
				}
			}
			
			// if we've checked a changeLeaderRadio, and the corresponding alt leader box is
			// checked, uncheck it to show the completeness of a kick
			if (frm.elements[i].name == "mtrFrmAltCheckBox[]") {
				if (tChecked == true) {
					if ((frm.elements[i].value == tValue) && (frm.elements[i].checked == true)) {
						frm.elements[i].checked = false;
						altLeaderDecCount++;
					}
				}
				else {
					// if the leader unchecked the changeLeaderRadio AND the user is currently an alt leader, we
					// need to display that he still is!
					if (isAltLeader && (frm.elements[i].value == tValue)) {
						frm.elements[i].checked = true;
						altLeaderDecCount--;
					}
				}
			}

			if (frm.elements[i].name == "mtrFrmNumMemsTxtArea") {
				spotsLeft = parseInt(frm.elements[i].value);

				if (tChecked == true)
					frm.elements[i].value = (spotsLeft - numMemCount);
			}

			// update the alt leaders text area
			if (frm.elements[i].name == "mtrFrmAltLdrTxtArea") {
				altLeaderCount = parseInt(frm.elements[i].value);
				frm.elements[i].value = altLeaderCount - altLeaderDecCount;
			}
		}
	}
}

//
// END FORM VALIDATION
//
////////////////////////////////////////




