/* media options */
var bAllowClient = false;
var bAllowPrivateGroup = false;

//apply a template
function fn_ApplyMediaTemplate(code) {
	document.getElementById("playerMediaTemplateCode").value = code;
	document.getElementById("command").value = "refresh";
	document.form.submit();
}

//highlight the option being hovered over
function fn_WhoForMode_MouseOver(whoForMode) {
	document.getElementById('divWhoForMode_' + whoForMode).className = 'box rounded mo-selected';
}

//reset the option being hovered over
function fn_WhoForMode_MouseOut(whoForMode) {
	if (!document.getElementById('whoForMode_' + whoForMode).checked) {
		document.getElementById('divWhoForMode_' + whoForMode).className = 'box rounded mo-item';
	}
}

//return the mode who this is for
function fn_WhoForMode_Return() {
	if (document.getElementById('div_WhoForMode')) {
		var arrayElements = document.getElementById('div_WhoForMode').getElementsByTagName('input');
		for (var i=0;i<arrayElements.length;i++) {
			ele = arrayElements[i];
			if (ele.name == 'whoForMode') {
				if (ele.checked) {
					return (ele.id.split('_')[1]);
				}
			}
		}
	} else if (document.getElementById('whoForMode')) {
		return document.getElementById('whoForMode').value;
	}
	return '';
}

function fn_WhoForModeSet(whoForMode) {
	var arrayElements = document.getElementById('div_WhoForMode').getElementsByTagName('input');
	for (var i=0;i<arrayElements.length;i++) {
		ele = arrayElements[i];
		if (ele.name == 'whoForMode') {
			thisWhoForMode = ele.id.split('_')[1];
			if (thisWhoForMode == whoForMode) {
				thisChecked = true;
				thisClassName = 'box rounded mo-selected';
			} else {
				thisChecked = false;
				thisClassName = 'box rounded mo-item';
			}
			ele.checked = thisChecked;
			document.getElementById('divWhoForMode_' + thisWhoForMode).className = thisClassName;
		}
	}
	fn_WhoForMode_Change(whoForMode);
}

//change of the option
function fn_WhoForMode_Change(whoForMode) {

	var selectedWhoForMode = '';
	
	//highlight only the selected option
	var arrayElements = document.getElementById('div_WhoForMode').getElementsByTagName('input');
	for (var i=0;i<arrayElements.length;i++) {
		ele = arrayElements[i];
		if (ele.name == 'whoForMode') {
			thisWhoForMode = ele.id.split('_')[1];
			if (ele.checked) {
				selectedWhoForMode = thisWhoForMode;
				thisClassName = 'box rounded mo-selected';
			} else {
				thisClassName = 'box rounded mo-item';
			}
			document.getElementById('divWhoForMode_' + thisWhoForMode).className = thisClassName;
		}
	}

	//hide appropriate options for the user to choose
	if (document.getElementById('divClient')) {
		document.getElementById('divClient').className = 'hiddenSection';
	}
	if (document.getElementById('divGroup')) {
		document.getElementById('divGroup').className = 'hiddenSection';
	}
	if (document.getElementById('divMember')) {
		document.getElementById('divMember').className = 'hiddenSection';
	}
	
	//determine which options should be made available
	if (selectedWhoForMode == 'PersonalOnly') {
	} else if (selectedWhoForMode == 'PrivateMember') {
		document.getElementById('divMember').className = '';
	} else if (selectedWhoForMode == 'PrivateGroup') {
		fn_DisplayGroups(false);
		document.getElementById('divGroup').className = '';
		if (bAllowClient) {
			document.getElementById('divClient').className = '';
		}
	} else if (selectedWhoForMode == 'PublicGroup') {
		fn_DisplayGroups(true);
		document.getElementById('divGroup').className = '';
	} else if (selectedWhoForMode == 'GeneralPublic') {
	} else if (selectedWhoForMode == 'MyChannel') {
	} else if (selectedWhoForMode == 'OtherChannel') {
		document.getElementById('divMember').className = '';
	} else if (selectedWhoForMode == 'MineOtherChannel') {
		document.getElementById('divMember').className = '';
	}

	//store the public/private setting (only for cosmetic use - not stored)
	if (document.getElementById('whoForPublic')) {
		var listPublicWhoFors = ',PublicGroup,GeneralPublic,MyChannel,OtherChannel,MineOtherChannel,';
		if (listPublicWhoFors.indexOf(selectedWhoForMode) != -1) {
			document.getElementById('whoForPublic').value = 'Y';
		} else {
			document.getElementById('whoForPublic').value = 'N';
		}
	}
}

function fn_WhoForIsPublic() {
	if (document.getElementById('whoForPublic')) {
		return document.getElementById('whoForPublic').value;
	} else {
		return 'N';
	}
}

//validate the media options are chosen appropriately client-side to double up the server-side validation
function fn_WhoForValidate() {
	//get who this is for
	var whoForMode = fn_WhoForMode_Return();
	var msg = '';

	if (whoForMode == '') {
		alert('Sorry, due to your permissions and settings, there are not currently any options available for who you can submit this for.');
		return false;
	}
	
	if (whoForMode == 'PublicGroup') {
		if (trim(document.getElementById('AuthorisedToViewGroupID').value) == '') {
			document.getElementById('AuthorisedToViewGroupID').focus();
			alert('Please select the group this is for');
			return false;
		}

	} else if (whoForMode == 'PrivateGroup') {
		var bOptionSelected = false;
		if (bAllowClient) {
			if (trim(document.getElementById('AuthorisedToViewClientID').value) != '') {
				return true;
			}
		}
		if (bAllowPrivateGroup) {
			if (trim(document.getElementById('AuthorisedToViewGroupID').value) != '') {
				return true;
			}
		}
		if (!bOptionSelected) {
			//no option selected, so stop the submission
			msg = 'Please select a';
			if (bAllowClient) { 
				msg = msg + ' cluster'; 
			}
			if (bAllowPrivateGroup) { 
				if (bAllowClient) {
					msg = msg + ' or';
				}
				msg = msg + ' group'; 
			}
			msg = msg + ' this is for'; 
			alert(msg);
			return false;
		}

	} else if (whoForMode == 'PrivateMember' || whoForMode == 'OtherChannel' || whoForMode == 'MineOtherChannel') {
		if (trim(document.getElementById('ListPlayerEmails').value) == '') {
			document.getElementById('ListPlayerEmails').focus();
			alert('Please nominate the members this is for');
			return false;
		}
	}
	
	//got here so all must be good
	return true;
}
