/* FUNCTIONS FOR ACTION SCREEN - PlayerGroup */

function fn_groupoptions (groupid) {
	var gt = document.getElementById('grouptoggle' + groupid);
	var go = document.getElementById('groupoptions' + groupid);
	var gl = document.getElementById('grouplink' + groupid);
	var gn = document.getElementById('groupname' + groupid);

	if (gl.innerHTML.indexOf('expand') != -1) {
		go.className = 'groupoptions';
		gn.className = 'groupname groupnameactive';
		gl.innerHTML = expandInnerHTML('collapse');
	} else {
		go.className = 'groupoptions hiddenSection';
		gn.className = 'groupname';
		gl.innerHTML = expandInnerHTML('expand');
	}
	
}

function fn_deletegroup (groupname,linkURL) {
	if (confirm('Are you sure you wish to delete the [' + groupname + '] group?')) {
		location.href = fn_EncryptURLParamsJS('playergroupedit.cfm', linkURL);
	}
}

function fn_cancelmembership (groupname,linkURL) {
	if (confirm('Are you sure you wish to cancel your membership to the [' + groupname + '] group?')) {
		location.href = fn_EncryptURLParamsJS('playergroup.cfm', linkURL);
	}
}

function fn_Action() {
	//take the appropriate action on all selected players
	if (document.form.updateAction.options[document.form.updateAction.selectedIndex].value == "") {
		alert('Please choose which action you wish to take ...');
	} else {
		//show the appropriate confirmation message
		updateAction = document.form.updateAction.options[document.form.updateAction.selectedIndex].value;
		if (updateAction == 'Cluster_Join') {
			document.location.href = 'clientpersonaledit.cfm?editmode=add';
		} else if (updateAction == 'Admin_Enable_Selected') {
			tmp = fn_ActiveStatus('Y','selected');
		} else if (updateAction == 'Admin_Enable_All') {
			tmp = fn_ActiveStatus('Y','all');
		} else if (updateAction == 'Admin_Disable_Selected') {
			tmp = fn_ActiveStatus('N','selected');
		} else if (updateAction == 'Admin_Disable_All') {
			tmp = fn_ActiveStatus('N','all');
		}						
	}
}

function fn_AdminActiveStatus(id) {
	var active = document.getElementById('adminContactActive'+id).checked ? 'Y' : 'N';
	//update the status
	rpc_Call('','playerGroupClusterAdminActiveStatus','id='+id+'&active='+active,'fn_AdminActiveUpdated');
}

function fn_AdminActiveUpdated(xmlDoc) {
	//init
	var msg = '';
	var id = '';
	var active = '';
	var activeChecked = false;
	
	//check the update result
	var id = rpc_SingleResult(xmlDoc,"id");
	var updated = rpc_SingleResult(xmlDoc,"updated");
	var active = rpc_SingleResult(xmlDoc,"active");

	//determine the message to display to the user
	if (updated == 'N') {
		msg = 'There was a problem updating your active status - please try again.  If this problem persists, please contact iSportz.';
	} else if (updated == 'Y') {
		if (active == 'Y') {
			msg = "Your administrator status has been updated.\n\nDepending on your admin priority, you may now start receiving email requests from cluster members.";
		} else {
			msg = "Your administrator status has been updated.\n\nAdministrative email requests you previously received will now be sent to the next prioritised administrator.";
		}
	}

	//set the toggle display
	activeChecked = (active == 'Y' ? true : false);
	if (document.getElementById('adminContactActive'+id).checked != activeChecked) {
		document.getElementById('adminContactActive'+id).checked = activeChecked;
	}

	//update the status label
	document.getElementById('membershipStatus'+id).innerHTML = (activeChecked ? 'Active Admin' : 'Inactive Admin');

	//inform the user
	if (msg != '') {
		alert(msg);
	}
}

function fn_ActiveStatus (active,selType) {
	if (selType == 'all') {
		selTypeMsg = 'all';
	} else {
		selTypeMsg = 'the selected';
	}
	if (selType != 'all') {
		if (!fn_checkselection('chkSelected',true)) {
			return false;
		}
	}
	if (confirm(msg)) {
		//if doing all then make sure all clusters are selected
		if (selType == 'all') {
			for (var i = 0; i < document.form.elements.length; i++) {
			    var e = document.form.elements[i];
				if ((e.name.substring(0,11) == 'chkSelected') && (e.type == 'checkbox')) {
					e.checked = true;
			    }
			}
		}
		//take the action by submitting the form
		document.form.active.value = active;		
		document.form.command.value = "update";		
		document.form.updateAction.selectedIndex = 0;
		document.form.submit();		
	}		
}
