// TODO: At some point should take out the email list manager related functions into their own javascript file 
// since they are being called in a few places outside of the team area

function checkToDisplayPwd()
{
	var privacy = obj("Privacy");
	var pwdTitle = obj("PwdTitle");
	var pwd = obj("Password");
	var confTitle = obj("ConfTitle");
	var confPwd = obj("ConfPwd");
	if (privacy.value == 1) {
		
		// Need to display password fields for user to fill in
		pwdTitle.style.display = "block";
		pwd.style.display = "block";
		confTitle.style.display = "block";
		confPwd.style.display = "block";
	}
	else {
		
		pwdTitle.style.display = "none";
		pwd.style.display = "none";
		confTitle.style.display = "none";
		confPwd.style.display = "none";
	}
	
	return true;
}

function checkInputs()
{

	var privacy = obj("Privacy");
	if (privacy.value == 1) {
		var pwdTitle = obj("PwdTitle");
		var pwd = obj("Password");
		var confTitle = obj("ConfTitle");
		var confPwd = obj("ConfPwd");
		
		if (pwd.value.length < 8) {
			alert ("Password must be at least 8 characters.  Please enter in a password of at least 8 characters");
			return false;
		}
		
		if (!isAlphaNumeric(pwd.value)) {
			alert ("Password must be alphanumeric.  Please enter in an alphanumeric password.");
			return false;
		}
		
		if (pwd.value != confPwd.value) {
			alert("Passwords do not match.  Please confirm your team site password.");
			return false;
		}
	}

	return true;
}

function ShowTabs(tab) 
{
	// Main tabs
	var sendToListBox = obj("SendToListBox");
	var sendToList = obj("SendToList");
	var manageEmailListBox = obj("ManageEmailListBox");
	var manageEmailList = obj("ManageEmailList");
	
	// Secondary tabs
	var addEmails = obj("AddEmails");
	var addEmailsBox = obj("AddEmailsBox");
	var editEmails = obj("EditEmails");
	var editEmailsBox = obj("EditEmailsBox");
	var deleteEmails = obj("DeleteEmails");
	var deleteEmailsBox = obj("DeleteEmailsBox");
	var msgArea = obj("MessageArea");	
	msgArea.innerHTML = '';
	
	// TODO: Get rid of any existing emails
	sendMsg = obj("SendMsg");
	sendMsg.innerHTML = '';
	sendMsg.style.display = 'none';
	
	// Get rid of any existing messages
	error = obj("Error");
	error.innerHTML = '';
	error.style.display = 'none';
	
	msg = obj("Msg");
	msg.innerHTML = '';
	msg.style.display = 'none';
	
	editMsg = obj("EditMsg");
	editMsg.innerHTML = '';
	editMsg.style.display = 'none';

	deleteMsg = obj("DeleteMsg");
	deleteMsg.innerHTML = '';
	deleteMsg.style.display = 'none';

	switch (tab) {
	
		case 1:
			sendToList.style.backgroundColor = "#F0F0F0";
			sendToList.style.borderTop = "1px solid #04234a";	
			sendToList.style.borderRight = "1px solid #04234a";
			sendToList.style.borderLeft = "1px solid #04234a";
			sendToList.style.borderBottom = "none";	
			sendToList.style.textDecoration = 'none';			
			sendToListBox.style.display = 'block';
			sendToList.style.color = "#04234a";
			
			manageEmailList.style.backgroundColor = "#848484";
			manageEmailList.style.borderBottom = "1px solid #04234a";
			manageEmailListBox.style.display = 'none';
			manageEmailList.style.color = "#ffffff";
			manageEmailList.style.border = "none";
			
			break;
			
		case 2:
			manageEmailList.style.backgroundColor = "#F0F0F0";
			manageEmailList.style.borderTop = "1px solid #04234a";	
			manageEmailList.style.borderRight = "1px solid #04234a";
			manageEmailList.style.borderLeft = "1px solid #04234a";
			manageEmailList.style.borderBottom = "none";	
			manageEmailList.style.color = "#04234a";
			manageEmailList.style.textDecoration = 'none';
			manageEmailListBox.style.display = 'block';
			manageEmailListBox.style.backgroundColor = "#F0F0F0";

			sendToList.style.border = "none";	
			sendToList.style.backgroundColor = "#848484";
			sendToList.style.borderBottom = "1px solid #04234a";
			sendToListBox.style.display = 'none';
			sendToList.style.color = "#ffffff";
			
			// Default submenu here as well
			addEmails.style.backgroundColor = "#ffffff";
			addEmails.style.borderTop = "1px solid #04234a";	
			addEmails.style.borderRight = "1px solid #04234a";
			addEmails.style.borderLeft = "1px solid #04234a";
			addEmails.style.borderBottom = "none";	
			addEmailsBox.style.display = 'block';
			addEmails.style.color = "#04234a";
			
			editEmails.style.border = "none";
			editEmails.style.backgroundColor = "#A4A4A4";
			editEmails.style.borderBottom = "1px solid #04234a";
			editEmails.style.color = "#ffffff";		
			editEmailsBox.style.display = 'none';

			deleteEmails.style.border = "none";
			deleteEmails.style.backgroundColor = "#A4A4A4";
			deleteEmails.style.borderBottom = "1px solid #04234a";
			deleteEmails.style.borderLeft = "1px solid #04234a";
			deleteEmails.style.color = "#ffffff";		
			deleteEmailsBox.style.display = 'none';			
			
			break;
			
		case 3:
			addEmails.style.backgroundColor = "#ffffff";
			addEmails.style.borderTop = "1px solid #04234a";	
			addEmails.style.borderRight = "1px solid #04234a";
			addEmails.style.borderLeft = "1px solid #04234a";
			addEmails.style.borderBottom = "none";	
			addEmailsBox.style.display = 'block';
			addEmails.style.color = "#04234a";
			
			editEmails.style.border = "none";
			editEmails.style.backgroundColor = "#A4A4A4";
			editEmails.style.borderBottom = "1px solid #04234a";
			editEmails.style.color = "#ffffff";		
			editEmailsBox.style.display = 'none';

			deleteEmails.style.border = "none";
			deleteEmails.style.backgroundColor = "#A4A4A4";
			deleteEmails.style.borderBottom = "1px solid #04234a";
			deleteEmails.style.borderLeft = "1px solid #04234a";
			deleteEmails.style.color = "#ffffff";		
			deleteEmailsBox.style.display = 'none';
			break;
			
		case 4:
			editEmails.style.backgroundColor = "#ffffff";
			editEmails.style.borderTop = "1px solid #04234a";	
			editEmails.style.borderRight = "1px solid #04234a";
			editEmails.style.borderLeft = "1px solid #04234a";
			editEmails.style.borderBottom = "none";	
			editEmails.style.color = "#04234a";
			editEmailsBox.style.display = 'block';
			
			addEmails.style.border = "none";
			addEmails.style.backgroundColor = "#A4A4A4";
			addEmails.style.borderBottom = "1px solid #04234a";
			addEmails.style.color = "#ffffff";		
			addEmailsBox.style.display = 'none';

			deleteEmails.style.border = "none";
			deleteEmails.style.backgroundColor = "#A4A4A4";
			deleteEmails.style.borderBottom = "1px solid #04234a";
			deleteEmails.style.color = "#ffffff";		
			deleteEmailsBox.style.display = 'none';
			break;
			
		case 5:
			deleteEmails.style.backgroundColor = "#ffffff";
			deleteEmails.style.borderTop = "1px solid #04234a";	
			deleteEmails.style.borderRight = "1px solid #04234a";
			deleteEmails.style.borderLeft = "1px solid #04234a";
			deleteEmails.style.borderBottom = "none";	
			deleteEmailsBox.style.display = 'block';
			deleteEmails.style.color = "#04234a";
			
			editEmails.style.border = "none";
			editEmails.style.backgroundColor = "#A4A4A4";
			editEmails.style.borderBottom = "1px solid #04234a";
			editEmails.style.color = "#ffffff";		
			editEmailsBox.style.display = 'none';
			
			addEmails.style.border = "none";
			addEmails.style.backgroundColor = "#A4A4A4";
			addEmails.style.borderBottom = "1px solid #04234a";
			addEmails.style.borderRight = "1px solid #04234a";
			addEmails.style.color = "#ffffff";		
			addEmailsBox.style.display = 'none';

			break;
	}

	return true;
}

function HighlightTabs(instance, color)
{
	htmlItem = ChooseObject(instance);
	htmlItem.style.textDecoration = 'underline';
	htmlItem.style.backgroundColor = color;
	return true;
}

function UnHighlightTabs(instance, color)
{
	htmlItem = ChooseObject(instance);
	htmlItem.style.textDecoration = 'none';
	htmlItem.style.backgroundColor = color;
	return true;
}

function ChooseObject(instance)
{
	var htmlItem = "";
	switch(instance) {
		case 1:
			htmlItem = obj("SendToList");
			break;
		case 2:
			htmlItem = obj("ManageEmailList");
			break;
			
	}
	return htmlItem;
}

function EditEmail()
{
	// Check to see that at least one object is selected
	var emailList = obj("EmailListEdit");
	
	var emailID = ''
	var emailSelected = false;
	for (i=0; i < emailList.length; i++) {
		
		if (emailList[i].selected == true) {
			emailSelected = true;
			emailID = emailList[i].value;
			emailAddress = emailList[i].innerHTML;
			break;
		}
		
	}
	
	if (!emailSelected) {
		alert("Please select at least one email address to edit.");
		return false;
	}
	
	// User selected an email address so let's display our edit box
	var editBox = obj("EditBox");
	var emailListEdit = obj("EmailListEdit");
	var eAddr = obj("EmailAddress");
	var eID = obj("EmailID");
	eAddr.value = emailAddress;
	eID.value = emailID;
	editBox.style.display = 'block';
	
	return true;
}

function CloseEditBox()
{
	var editBox = obj("EditBox");
	editBox.style.display = 'none';
	
	return true;
}

function UpdateEmailAddr()
{
	var emailAddress = obj("EmailAddress");
	var emailID = obj("EmailID");
	var teamID = obj("TeamID");

	// Check new email
	if (!CheckAllEmails(emailAddress)) {
		return false;
	}
	
	// Confirm email update
	if (!confirm("Are you sure you want to update this email address?")) {
		return false;
	}
	
	return true;
}

function DeleteEmail()
{
	// Check to see that at least one object is selected
	var emailList = obj("EmailListDelete");
	
	var emailID = ''
	var emailSelected = false;
	for (i=0; i < emailList.length; i++) {
		
		if (emailList[i].selected == true) {
			emailSelected = true;
			break;
		}
		
	}
	
	if (!emailSelected) {
		alert("Please select at least one email address to delete.");
		return false;
	}
	
	// Confirm email update
	if (!confirm("Are you sure you want to delete these email addresses?")) {
		return false;
	}
		
	return true;
}

function setTestEmail(value)
{
	var testEmailTitle = obj("TestEmailTitle");
	var testEmailAddress = obj("TestEmailAddress");
	var emailTestAddr = obj("EmailTestAddr");
	var LRUsers = document.getElementsByName("LRUsers");
	
	if (value == 1) {
		testEmailTitle.style.display = 'block';
		testEmailAddress.style.display = 'block';
		emailTestAddr.value = '';
		
		for (i=0; i < LRUsers.length; i++) {
			if (LRUsers[i].value == 0) {
				LRUsers[i].checked = true;
			}
		}
	}
	else {
		testEmailTitle.style.display = 'none';
		testEmailAddress.style.display = 'none';
		emailTestAddr.value = '';
	}
	
	return true;
}

function checkForTest()
{
	var LRUsers = document.getElementsByName("LRUsers");
	var TestEmail = document.getElementsByName("TestEmail");
	
	// Clear out test emails user wants 
	for (j=0; j < TestEmail.length; j++) {
		if (TestEmail[j].value == 1 && TestEmail[j].checked == true) {
			
			for (i=0; i < LRUsers.length; i++) {
		
				if (LRUsers[i].value == 1 && LRUsers[i].checked == true) {
					alert("You cannot send email to LocalReplay members in test mode.  Please set \"Send Test Email\" to No if you wish to send this email to your team email list and LocalReplay members.");
					LRUsers[i].checked = false;
				}
				else if (LRUsers[i].value == 0) {
					LRUsers[i].checked = true
				}
			}
		}
	}

	return true;
}

function sendOutEmail()
{
	// Check for subject
	var subject = obj("EmailSubject");
	if (subject.value == "") {
		alert ("Please enter in a subject for your email.");
		return false;
	}
	
	// Check for email text
	var text = obj("EmailText");
	if (text.value == "") {
		alert ("Please enter in some text for your email.");
		return false;
	}
	
	// Check if we're sending out a test email
	msg = "Are you sure you want to send out this email to your team list?";
	for (var i=0; i < document.SendOutEmail.TestEmail.length; i++) {
   		if (document.SendOutEmail.TestEmail[i].checked) {
      		if (document.SendOutEmail.TestEmail[i].value == 1) {
				if (!CheckTestEmails(obj('EmailTestAddr'))) {
					return false;
				}
				
				msg = "Are you sure you want to send out this email to your test email address?";
			}
      	}
   	}
	
	// See if user really wants to send this out
	if (!confirm(msg)) {
		return false;
	}
	
	return true;
}

function CheckTestEmails(instance)
{
	if(instance.value == "")
	{
		alert("Please enter at least one test email address.");
		return false;
	}

	tmpInstance = instance.value.replace(/\n/g, ",");
	tmpInstance = tmpInstance.replace(/\r/g, ",");
	if(tmpInstance.indexOf(",") > -1)
	{

		var email = tmpInstance.split(",");
		for(i=0; i < email.length; i++)
		{
			if(email[i].length > 0)
			{
				if(!checkMail(email[i].replace(/^\s+|\s+$/g, '')))
				{
					alert("In-valid test email address (" + email[i] + ") entered. Please enter valid test email addresses (ie name@server.com)");
					return false;
				}
				
			}
		}
	}
	else
	{
		if(!checkMail(tmpInstance.replace(/^\s+|\s+$/g, '')))
		{
			alert("In-valid test email address (" + instance.value + ") entered. Please enter valid test email addresses (ie name@server.com)");
			return false;
		}
	}
	
	// Looks like we passed our tests so submit the form
	return true;
}

function displayHelp(instance, value, e, x, y)
{
	getMousePosition(e);
	
	if (value == 1) {
		
		if (browser.isSafari) {
			yValue = y - 35;
			instance.style.top = yValue  + "px";;
		}
		else
			instance.style.top = y + "px";
			
		instance.style.left = x  + "px";
		instance.style.display = 'block';
		
	}
	else {
		instance.style.display = 'none';
	}
	return true;
}

function checkRecaps()
{
	var teamGameRecaps = obj("TeamGameRecaps");
	var recapSelected = false;
	
	for (i = 0; i < teamGameRecaps.length; i++) {
		if (teamGameRecaps[i].selected == true) {
			recapSelected = true;
			break;
		}
	}
	
	// If there are no recaps selected warn user
	if (!recapSelected) {
		alert("Please select a game recap to view.");
		return false;
	}
		
	return true;
}

function checkContactInput()
{
	var userName = obj("UserName");
	var userEmail = obj("UserEmail");
	var userComment = obj("UserComment");
	
	if (userName.value == "") {
		alert("Please enter in a name to specify who the comment is coming from.");
		return false;
	}

	if (!checkMail(userEmail.value)) {
		alert("The email address you entered is invalid.  Please provide a valid email address.")
		return false;
	}

	if (userComment.value == "") {
		alert("Please add a comment or question to send to this team.");
		return false;
	}

	return true;
}

function DeleteMember(teamID, user, username, type)
{
	if(confirm("Are you sure you want to remove this person from the team?") ) 
	{
		location.href= "/team/change_members.php?id=" + escape(teamID) + "&DeleteUserID=" + escape(user) + "&DeleteUserName=" + escape(username) + "&Type=" + escape(type);
	}
}
