//-------------------------------------------------------------------------------------------
// display.js - routines for the ISADG/A2A archiving system
// Part of the Community Sites software package
// Developed by Ian Grant and Jack Latimer
// Copyright Community Sites 2004, 2005, 2006, 2007, 2008
// For further information, see www.communitysites.co.uk or email info@communitysites.co.uk
//-------------------------------------------------------------------------------------------

// confirmation
function confirmation(message) {
	return confirm(message);
}

// delete the file (or not)
function checkDelete(item) {
	if (confirm("This will delete the " + item + " forever.  Continue?")) {
		return true;
	} else {
		return false;
	}
}

// show if hidden or hide if shown 
function toggleSection(id, is_visible_by_default) {
	if (document.getElementById(id)) {
		if (document.getElementById(id).style.display != "") {
			if (document.getElementById(id).style.display == "none")  {
				document.getElementById(id).style.display = "block";
				if (document.getElementById("txtOpenSections")) {
					document.getElementById("txtOpenSections").value += id + ",";
				}
				if (document.getElementById("txtClosedSections")) {
					document.getElementById("txtClosedSections").value = document.getElementById("txtClosedSections").value.replace(id + ",", "");
				}
			} else {
				document.getElementById(id).style.display = "none";
				if (document.getElementById("txtClosedSections")) {
					document.getElementById("txtClosedSections").value += id + ",";
				}
				if (document.getElementById("txtOpenSections")) {
					document.getElementById("txtOpenSections").value = document.getElementById("txtOpenSections").value.replace(id + ",", "");
				}
			}
		} else {
			if ((is_visible_by_default) && (document.getElementById(id).className.indexOf("hidden") == -1)) {
				document.getElementById(id).style.display = "none";
				if (document.getElementById("txtClosedSections")) {
					document.getElementById("txtClosedSections").value += id + ",";
				}
				if (document.getElementById("txtOpenSections")) {
					document.getElementById("txtOpenSections").value = document.getElementById("txtOpenSections").value.replace(id + ",", "");
				}
			} else {
				document.getElementById(id).style.display = "block";
				if (document.getElementById("txtOpenSections")) {
					document.getElementById("txtOpenSections").value += id + ",";
				}
				if (document.getElementById("txtClosedSections")) {
					document.getElementById("txtClosedSections").value = document.getElementById("txtClosedSections").value.replace(id + ",", "");
				}
			}
		}				
	}
}

function toggleLevelFieldForm(chkbox, formsection) {
	// if the checkbox is checked, that means we want to copy that section of the form from somewhere else rather than 
	// filling it in manually, so we hide the form section
	if (document.getElementById(formsection)) {
		if (chkbox.checked) {
			document.getElementById(formsection).style.display = "none";
		} else {
			document.getElementById(formsection).style.display = "block";
		}
	}
}

// clear this upload
function clearRadUploads(upload) {
	if (upload) {
		var fileInputs = upload.GetFileInputs();
		for (var i=0; i<fileInputs.length; i++)
		{
			upload.ClearFileInputAt(i);
		}
	}
}


//doNotRadUpload - a global variable, indicating that the file inputs should be cleared.
// It is set to true in the OnClientClick property of the upload button
var doNotRadUpload = true;
function OnClientSubmitting(progressManager, args) {
	if (doNotRadUpload)	{
		//Clear the file inputs
		var upload = GetRadUpload('RadUploadImage');
		if (upload) {
			clearRadUploads(upload);
		}
		upload = GetRadUpload('RadUploadDocument');
		if (upload) {
			clearRadUploads(upload);
		}
		upload = GetRadUpload('RadUploadMultimedia');
		if (upload) {
			clearRadUploads(upload);
		}
	}
}

// sets up the rad
function enableRadCancel(id) {
	RadUploadNameSpace.RadProgressArea.prototype._CancelRequest =
 		RadUploadNameSpace.RadProgressArea.prototype.CancelRequest;
		RadUploadNameSpace.RadProgressArea.prototype.CancelRequest = function()	{
			// clear the upload
			var upload = GetRadUpload(id);
			clearRadUploads(upload);

		  	// and cancel
			this._CancelRequest();
		}
}

function showProgress(type) {
	// hide everything by default
	if (document.getElementById('RadUploadImageProgress')) {
		document.getElementById('RadUploadImageProgress').style.display = 'none';
	}
	if (document.getElementById('RadUploadDocumentProgress')) {
		document.getElementById('RadUploadDocumentProgress').style.display = 'none';
	}
	if (document.getElementById('RadUploadMultimediaProgress')) {
		document.getElementById('RadUploadMultimediaProgress').style.display = 'none';
	}

	// and show the indicator requested
	if (document.getElementById('RadUpload' + type + 'Progress')) {
		document.getElementById('RadUpload' + type + 'Progress').style.display = 'block';
	}
}

var posx = 0;
var posy = 0;
function getMousePosition(e) {
	//if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
}



function showMenu(thislink, event, id, reference, allow_add, allow_edit, allow_move, allow_recycle, allow_delete, allow_view_in_hierarchy) {
	if (document.getElementById("htmlMenuArea")) {
		document.getElementById("htmlMenuArea").style.display = "block";

		// position the menu next to the mouse cursor
		getMousePosition(event);
		document.getElementById("htmlMenuArea").style.left = posx + "px";
		document.getElementById("htmlMenuArea").style.top = posy + "px";
		
		// give it a title
		if (document.getElementById("htmlMenuTitle")) {
			document.getElementById("htmlMenuTitle").innerHTML = reference;
		}

		// show the links as appropriate, and change their paramters so that they work with the chosen record
		if (document.getElementById("htmlMenuAdd")) {
			if (allow_add) {
				document.getElementById("htmlMenuAdd").style.display = "block";
				document.getElementById("htmlMenuAdd").href = "admin.aspx?ar=" + id;
			} else {
				document.getElementById("htmlMenuAdd").style.display = "none";
			}
		}

		if (document.getElementById("htmlMenuEdit")) {
			if (allow_edit) {
				document.getElementById("htmlMenuEdit").style.display = "block";
				document.getElementById("htmlMenuEdit").href = "admin.aspx?er=" + id;
			} else {
				document.getElementById("htmlMenuEdit").style.display = "none";
			}
		}

		if (document.getElementById("htmlMenuMove")) {
			if (allow_move) {
				document.getElementById("htmlMenuMove").style.display = "block";
				document.getElementById("htmlMenuMove").href = "admin.aspx?mt=" + id;
			} else {
				document.getElementById("htmlMenuMove").style.display = "none";
			}
		}

		if (document.getElementById("htmlMenuRecycle")) {
			if (allow_recycle) {
				document.getElementById("htmlMenuRecycle").style.display = "block";
				document.getElementById("htmlMenuRecycle").href = "admin.aspx?r=" + id;
			} else {
				document.getElementById("htmlMenuRecycle").style.display = "none";
			}
		}

		if (document.getElementById("htmlMenuDelete")) {
			if (allow_delete) {
				document.getElementById("htmlMenuDelete").style.display = "block";
				document.getElementById("htmlMenuDelete").href = "admin.aspx?d=" + id;
			} else {
				document.getElementById("htmlMenuDelete").style.display = "none";
			}
		}

		if (document.getElementById("htmlMenuViewInHierarchy")) {
			if (allow_view_in_hierarchy) {
				document.getElementById("htmlMenuViewInHierarchy").style.display = "block";
				document.getElementById("htmlMenuViewInHierarchy").href = "admin.aspx?sv=" + id + "#sv";
			} else {
				document.getElementById("htmlMenuViewInHierarchy").style.display = "none";
			}
		}
	}

	return false;
}

function closeMenu() {
	if (document.getElementById("htmlMenuArea")) {
		document.getElementById("htmlMenuArea").style.display = "none";
	}
	
	return false;
}

// sync the selections in two identical lists
function syncLists(thislist, thatlist) {
	if (document.getElementById(thatlist)) {
		document.getElementById(thatlist).selectedIndex = thislist.selectedIndex;
	}
}

// adds the current scroll position to the link parameters, in order to restore it when the page is reloaded
function addY(thislink) {
	var y = document.body.scrollTop	+ document.documentElement.scrollTop;
	thislink.href += "&ypos=" + y;
}
// restore the last scroll position
function restoreY(y) {
	document.body.scrollTop = y;
	document.documentElement.scrollTop = y;
}

// 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) {
			obj.click();
		}
	}

	return true;
}
