//Simply warns the user when making an item Active that it will go Live
function warnActive(frmID) {
	if (document.forms[frmID].status.options[document.forms[frmID].status.selectedIndex].value == "Active") {
		alert("This item will be released LIVE within the next hour.");
		}
	}
	
// Checks the user wants to delete the item
function confirmDelete(frmID) {
	if (confirm("Are you sure you want to delete this item? This cannot be undone.")) {
		document.forms[frmID].edit.value="";
		document.forms[frmID].submit();
		}
	else {
		return false;
		}
	}
	
// Checks the user wants to delete the item
function confirmDeleteFeed(frmID) {
	if (confirm("Are you sure you want to delete this feed? Affiliates accessing it directly will display empty panels.")) {
		document.forms[frmID].edit.value="";
		document.forms[frmID].submit();
		}
	else {
		return false;
		}
	}
	
// Checks the user wants to delete the item in Bulk Items
function confirmDeleteBulk(frmID) {
	if (confirm("Are you sure you want to delete the selected items? This cannot be undone.")) {
		document.forms[frmID].submit();
		}
	else {
		return false;
		}
	}
	
// If a field is marked "exclusive" in /content/categories.txt, this function unticks and
// disables other boxes when it is checked, and vice versa.
function checkExclusive(element,frmID) {
	var theForm = document.forms[frmID];
	var frmFieldName;
	var frmFieldType;
	var exclusiveField = eval("document.forms[frmID]."+element);
	if (exclusiveField.checked == true) {
   		for(i=0; i<theForm.elements.length; i++){
			frmFieldName = theForm.elements[i].name;
			frmFieldType = theForm.elements[i].type;
			if (frmFieldName.indexOf("cat")!=-1 && frmFieldType == "checkbox" && frmFieldName != element) {			
				theForm.elements[i].checked = false;
				theForm.elements[i].disabled = true;
				}
			}
		}
	else {
		for(i=0; i<theForm.elements.length; i++){
			frmFieldName = theForm.elements[i].name;
			frmFieldType = theForm.elements[i].type;
			if (frmFieldName.indexOf("cat")!=-1 && frmFieldType == "checkbox" && frmFieldName != element) {			
				theForm.elements[i].disabled = false;
				}
			}
		}
	}

function checkAll(frmID,matchName) {
	var theForm = document.forms[frmID];
	var frmFieldName;
	var frmFieldType;
  	for(i=0; i<theForm.elements.length; i++){
		frmFieldName = theForm.elements[i].name;
		frmFieldType = theForm.elements[i].type;
		if (frmFieldName.indexOf(matchName)!=-1 && frmFieldType == "checkbox") {			
			theForm.elements[i].checked = theForm.checkall.checked;
			}
		}
	}
	
// Simply reminds users altering the property field in item input that the link may now be irrelevant.
function changeFeedLinkReminder(frmID) {
	var fval = document.forms[frmID].plink.value;
	if (fval) {
		alert("Please double-check the link you have specified is still relevant to the property selected.");
		}
	}

function toggleSchedule(frmID) {
	var schedule = document.forms[frmID].schedule.checked;
	var status = true;
	if (schedule) {
		status = false;
		document.forms[frmID].status.disabled = true;
		}
	else {
		status = true;
		document.forms[frmID].status.disabled = false;
		}
	document.forms[frmID].syear.disabled = status;
	document.forms[frmID].eyear.disabled = status;
	document.forms[frmID].smonth.disabled = status;
	document.forms[frmID].emonth.disabled = status;
	document.forms[frmID].sday.disabled = status;
	document.forms[frmID].eday.disabled = status;
	document.forms[frmID].shour.disabled = status;
	document.forms[frmID].ehour.disabled = status;
	}
	
// Validate http links input into form fields.
function validateLink(frmID) {
	var fval = document.forms[frmID].plink.value;
	if (fval && fval.substr(0,7) != "http://") {
		alert("The Link URL must start with http://");
		document.forms[frmID].plink.focus();
		return false;
		}
	else if (fval && fval.indexOf(".")==-1) {
		alert("The Link URL format is invalid.");
		document.forms[frmID].plink.focus();
		return false;
		}
	}

// Validate the partial email address to make sure only the name prefix is supplied, not the domain
function validateNewEmail(frmID) {
	var fval = document.forms[frmID].newemail.value;
	if (fval.indexOf("@")!=-1) {
		alert("The new email address contains an @ when it shouldn't!");
		document.forms[frmID].newemail.focus();
		}
	}
	
// Check the password is alphanumeric in the operators User edit form.
function validatePassword(frmID) {
	var fval = document.forms[frmID].password.value;
	if (!isAlpha(fval)) {
		alert("The password must be alphanumeric");
		document.forms[frmID].password.focus();
		}
	}
	
// Utility - See above
function isAlpha(txt) {
	return ValidString(txt,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789');
	}
	
// Utility - See above
function ValidString(ChkString,ValidString) {
	for (i=0; i<ChkString.length; i++) {
		if (ValidString.indexOf(ChkString.substring(i,i+1)) == -1) return false;
		}
	return true;
	}

// in operators/import_feeds.php
function checkPropertySelected(frmID) {
	var prop = document.forms[frmID].property;
	var itemStatus = document.forms[frmID].itemstatus;
	if (prop.options[prop.options.selectedIndex].value) {
		return false;
		}
	else if (itemStatus.options[itemStatus.options.selectedIndex].value == "Release") {
		alert ("You must select a property to associate to all items from this feed before setting Action On Import to 'Release'");
		itemStatus.options.selectedIndex = 0;
		return false;
		}
	}
	
// in operators/import_feeds.php
function checkPropertyConfirm(frmID) {
	var prop = document.forms[frmID].property;
	if (prop.options[prop.options.selectedIndex].value) {
		if (!confirm("Are you sure that EVERY item in this feed will ALWAYS relate to this property?")) {
			prop.options.selectedIndex = 0;
			return false;
			}
		else {
			return true;
			}
		}
	}
	
// in operators/feeds.php
function validateFeedFrm(frmID,catList) {
	var exclusiveCategories = catList.split(",");
	var titleText = document.forms[frmID].title.value;
	var itemText = document.forms[frmID].description.value;
	var property = document.forms[frmID].property.options[document.forms[frmID].property.options.selectedIndex].value;
	var syear = document.forms[frmID].syear.options[document.forms[frmID].syear.options.selectedIndex].text;
	var eyear = document.forms[frmID].eyear.options[document.forms[frmID].eyear.options.selectedIndex].text;
	var smonth = document.forms[frmID].smonth.options[document.forms[frmID].smonth.options.selectedIndex].text;
	var emonth = document.forms[frmID].emonth.options[document.forms[frmID].emonth.options.selectedIndex].text;
	var sday = document.forms[frmID].sday.options[document.forms[frmID].sday.options.selectedIndex].text;
	var eday = document.forms[frmID].eday.options[document.forms[frmID].eday.options.selectedIndex].text;
	var shour = document.forms[frmID].shour.options[document.forms[frmID].shour.options.selectedIndex].text;
	var ehour = document.forms[frmID].ehour.options[document.forms[frmID].ehour.options.selectedIndex].text;
	if (!titleText || !itemText) {
		alert("An item Title and Description must be supplied");
		return false;
		}
	if (!property) {
		alert("Please select a Principal Property for this item");
		return false;
		}
	if (document.forms[frmID].schedule.checked) {
		var startdate = syear+smonth+sday+shour;
		var enddate = eyear+emonth+eday+ehour;
		if (parseInt(startdate) >= parseInt(enddate)) {
			alert ("This item is scheduled to Stop before it Starts!!");
			return false;
			}
		}
	var linkStatus = validateLink(frmID);
	if (linkStatus == false) {
		return false;
		}
	itemText = itemText.toLowerCase(); 
	var findWord = itemText.indexOf("affiliate");
	var catValue = false;
	for (var i=0, len=exclusiveCategories.length; i<len; ++i ) {
		if (exclusiveCategories[i]) {			
			catValue = eval("document.forms[frmID]."+exclusiveCategories[i]+".checked");
			if (catValue == true) {
				if (!confirm("Your selection will make this item available to affiliates only and not to players. Is this what you intended?")) {
					return false;
					}
				}
			}
		}
	if (catValue == false && findWord > -1) {
		if (!confirm("This item references affiliates, but you haven't selected the Affiliates catagory. Are you sure you want the general public to see this?")) {
			return false;
			}
		}
	else if (catValue == false) {
		if (!confirm("Your selection will make this item visible to players. Is this what you intended?")) {
			return false;
			}
		}
	document.forms[frmID].submit();
	}
	
// in operators/import_feeds.php
function validateImportedFeedFrm(frmID,catList) {
	var exclusiveCategories = catList.split(",");
	var titleText = document.forms[frmID].title.value;
	var furl = document.forms[frmID].url.value;
	if (!titleText || !furl) {
		alert("A feed Title and a URL must be supplied");
		return false;
		}
	var catValue = false;
	for (var i=0, len=exclusiveCategories.length; i<len; ++i ) {
		if (exclusiveCategories[i]) {			
			catValue = eval("document.forms[frmID]."+exclusiveCategories[i]+".checked");
			if (catValue == true) {
				if (!confirm("Your selection will make this item available to affiliates only and not to players. Is this what you intended?")) {
					return false;
					}
				}
			}
		}
	if (catValue == false) {
		if (!confirm("Your selection will make this item available to everyone including players. Is this what you intended?")) {
			return false;
			}
		}
	document.forms[frmID].submit();
	}
	
// In admin/property.php
function checkPropertyOwner(frmID) {
	var newOwner = document.forms[frmID].domain.value;
	var oldOwner = document.forms[frmID].oldowner.value;
	if (oldOwner != newOwner) {
		if (confirm("As you have changed the Owner, click OK to move existing feed items for this property to the new owner, or CANCEL to leave them where they are.")) {
			document.forms[frmID].switchitems.value = "y";
			}
		}
	document.forms[frmID].submit();
	}
	
// In admin/property.php
function addPropertyOwner(frmID) {
	var newOwner = document.forms[frmID].domain.value;
	if (!newOwner) {
		alert("Please supply the owner");
		}
	else {
		document.forms[frmID].submit();
		}
	}
	
//Used in webmasters/manual/style_library_1.php
function showDemo(divID,anchorID) {
	var thisDiv;
	var divs;
	if (document.getElementById(divID).style.display == "none") {
		document.getElementById(divID).style.display = 'block';
		document.getElementById(anchorID).innerHTML = '&laquo; Hide style code';
		divs = document.getElementsByTagName("div");
		anchors = document.getElementsByTagName("a");
		for (i = 0; i < divs.length; i++) {
			thisDiv = divs[i].id;
			if (thisDiv.substr(0,5) == "style" && thisDiv != divID) {
				divs[i].style.display = "none";
				}
			}
		for (i = 0; i < anchors.length; i++) {
			thisAnchor = anchors[i].id;
			if (thisAnchor.substr(0,6) == "anchor" && thisAnchor != anchorID) {
				anchors[i].innerHTML = "&raquo; Show style code";
				}
			}
		}
	else {		
		document.getElementById(divID).style.display = 'none';
		document.getElementById(anchorID).innerHTML = '&raquo; Show style code';
		}
	}
	
//Used in webmasters/manual/code_examples.php
function showCodeExample(divID) {
	if (document.getElementById(divID).style.display == "none") {
		document.getElementById(divID).style.display = 'block';
		}
	else {		
		document.getElementById(divID).style.display = 'none';
		}
	}
	