//-------------------------------------------------------------------------------------------
// save.js - checks whether the user's saved their contribution
// Part of the Community Sites software package
// Developed by Ian Grant and Jack Latimer
// Copyright Community Sites 2004, 2005
// For further information, see www.communitysites.co.uk or email info@communitysites.co.uk
//-------------------------------------------------------------------------------------------

// flag for saved data
var isSaved = true;

// determines whether we're in a situation where data might be lost
var checkRequired = false;

// set alert messages for all links on the page...
function setSaveAlert() {
	checkRequired = true;

	var i, a;
  	for (i=0; (a = document.getElementsByTagName('a')[i]); i++) {
		// note: checks for gallery preview links, as these are already disabled by their onclick event
        if ((a.href != "") && (a.href.indexOf(".aspx#") == -1) && (a.href.indexOf("page.aspx?id=&") == -1) && (a.href.indexOf("page_id=_") == -1)) {
			a.onclick = checkQuit;
		}
  	}
}

// ask the user whether they want to carry on...
function checkQuit() {
	quit = true;
	// if we're about to lose work...
	if (checkRequired) {
		// and there's unsaved work that might be lost...
		if ((! isSaved) || (document.getElementById("txtIsSaved").value != "1")) {
			quit = confirm("Your unsaved work will be lost.  Continue?");
		}
	}

	return quit;
}

// find out whether the work's been saved
// - not being used currently, as we're using the confirm method above instead
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 != "1")) {
			var url = "contributesave.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(item) {
	if (confirm("This will delete the " + item + " forever.  Continue?")) {
		return true;
	} else {
		return false;
	}
}

// delete the image (or not)
function checkRemoveImage() {
	if (confirm("This will remove the photo from your page.  Continue?")) {
		return true;
	} else {
		return false;
	}
}

// log out (or not)
function checkLogout() {
	if (confirm("You are about to log out. 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;

		obj = document.getElementById(btn);
		if (obj) {
			// this is a complete fudge, but we need to make the search button check that we don't have
			// saved work in PageMaker, and this is the easiest place to do it
			if (obj.id.indexOf("btnSearchInstant") >= 0) {
				if (! checkQuit()) {
					return false;
				}
			}

			obj.click();
		}
	}
}

//Match spaces at beginning and end of text and replace with null strings
function strtrim(input) {
    output = new String();
    output = input;

    return output.replace(/^\s+/,'').replace(/\s+$/,'');
}

// trims the strings contained within these text boxes, to avoid any validation issues
function trimIdValues(username, password) {
    if (document.getElementById(username)) {
        document.getElementById(username).value = strtrim(document.getElementById(username).value);
    }
    if (document.getElementById(password)) {
        document.getElementById(password).value = strtrim(document.getElementById(password).value);
    }
}

// returns the width of a specified image element
function getImageWidth(image) {
    if (document.getElementById(image)) {
        if (document.getElementById(image).offsetWidth) {
            return document.getElementById(image).offsetWidth;
        } else {
            return document.getElementById(image).style.pixelWidth;
        }
    }
}

// returns the height of a specified image element
function getImageHeight(image) {
    if (document.getElementById(image)) {
        if (document.getElementById(image).offsetHeight) {
            return document.getElementById(image).offsetHeight;
        } else {
            return document.getElementById(image).style.pixelHeight;
        }
    }
}

// get and store the size of a linked image
function getImageSize(submit) {
    // get and store the image dimensions
    if (document.getElementById("txtImageLinkWidth")) {
        document.getElementById("txtImageLinkWidth").value = getImageWidth("imgImageLink");
    }
    if (document.getElementById("txtImageLinkHeight")) {
        document.getElementById("txtImageLinkHeight").value = getImageHeight("imgImageLink");
    }

    // if requested, resubmit....
    if ((submit) && (document.getElementById("btnImageLinkAdd"))) {
        document.getElementById("btnImageLinkAdd").click();
    }
}

// enables the button for adding an image link if there's a valid-ish value in the text box
function checkImageLink() {
    if ((document.getElementById('txtImageAddress')) && (document.getElementById('btnImageLink'))) {
        if (document.getElementById('txtImageAddress').value.indexOf('://') != -1) {
            document.getElementById('btnImageLink').disabled=false;
        } else {
            document.getElementById('btnImageLink').disabled=true;
        }
    }
}
