// flag for saved data
var isSaved = true;

// determines whether we're in a situation where data might be lost
var checkRequired = true;

// find out whether the work's been saved
function checkSaved() {
	// if we're about to lose work...
	if (checkRequired) {
		// and there's unsaved work that might be lost...
		if ((! isSaved) || (document.getElementById("txtIsSaved").value != "true")) {
			var url = "contribute_emergency_save.aspx";

			// if there's any new content in the browser, add it to the url
			if (! isSaved) {
				url += "?";
				if (document.getElementById("txtUserName")) {
					url += "name=" + escape(document.getElementById("txtUserName").value) + "&";
				}
				if (document.getElementById("txtUserEmail")) {
					url += "email=" + escape(document.getElementById("txtUserEmail").value) + "&";
				}
				if (document.getElementById("chkUserPermission")) {
					if (document.getElementById("chkUserPermission").checked) {
						url += "useemail=Y&";
					} else {
						url += "useemail=N&";
					}
				}
				if (document.getElementById("txtPageTitle")) {
					url += "title=" + escape(document.getElementById("txtPageTitle").value) + "&";
				}
				if (document.getElementById("txtPageContent")) {
					url += "content=" + escape(document.getElementById("txtPageContent").value) + "&";
				}
			}

			// open the save window
			openURLInNewWindow("Save?", "save", url, 300, 300, "");
		}
	}
}

// something's changed, so highlight the button
function setChanged() {
	isSaved = false;
	document.getElementById("btnFiles").className = "panelbuttonreminder";
}

// tells the system that no save check is required
function setNoCheck() {
	checkRequired = false;
}

// opens a document in a new window
function openURLInNewWindow(title,name,url,width, height, additionalParams) {
	var newWindow = null;

	params = '';

	if (width > 0) {
		if (width < screen.availWidth) {
			// if we're specifying the width and height, then centre it on the screen
			dialogLeft = Math.ceil((screen.width / 2) - (width / 2));
		} else {
			dialogLeft = 0;
			width = screen.availWidth;
		}
	} else {
		// if no width and height are specified, then fill the screen
		dialogLeft = 0;
		width = screen.availWidth;
	}

	if (height > 0) {
		if (height < screen.availHeight) {
			// if we're specifying the width and height, then centre it on the screen
			dialogTop = Math.ceil((screen.height / 2) - (height / 2));
		} else {
			dialogTop = 0;
			height = screen.availHeight;
		}
	} else {
		// if no width and height are specified, then fill the screen (allowing space for taskbar)
		dialogTop = 0;
		height = screen.availHeight;
	}

	if ((additionalParams.indexOf('menubar') != -1) && (is.ie4up)) {
		// ie mucks up the height if we include a menu bar, so do some adjustment
		params = 'width=' + (width - 5) + ',height=' + (height - 23) + ',dependent,left=' + dialogLeft + ',top=' + dialogTop;
	} else {
		params = 'width=' + width + ',height=' + height + ',dependent,left=' + dialogLeft + ',top=' + dialogTop;
	}

	if (additionalParams != '') {
		params = params + ',' + additionalParams
	}

	newWindow = window.open (url, '', params);
	if (newWindow) {
		newWindow.document.close();
		newWindow.focus();
		return newWindow;
	} else {
		return null;
	}
}

// delete the file (or not)
function checkDelete() {
	if (confirm("This will delete your page forever.  Continue?")) {
		return true;
	} else {
		return false;
	}
}

// allows a default button to be set up, activated when enter is pressed
function HandleDefaultButton(btn, event) {
  	if ((event.which == 13) || (event.keyCode == 13)){
	   	event.returnValue=false;
	   	event.cancel = true;
	   	if (document.getElementById(btn)) {
		   document.getElementById(btn).click();
		}
	}
}

// if the user isn't logged in, check that they want to continue
function checkUnknownUser() {
	if (confirm("If you register and log in before you create pages, you will be able to come back and make changes to them in the future.  But you can still make a page right now, if you like.\n\nDo you want to continue?")) {
		return true;
	} else {
		return false;
	}
}

