//////////////////////////////////////////////////////////////
// Common functions
// v1.00 (Budd Wright)
//
// Description:
//////////////////////////////////////////////////////////////
// Contains common page functions
//////////////////////////////////////////////////////////////
//
// Usage:
//////////////////////////////////////////////////////////////
// <script language="javascript" src="js/common.js"></script>
//////////////////////////////////////////////////////////////
//
// v1.00 Notes
// -----------
// Requires pageName variable to be set elsewhere
//
// Known Issues
// ------------
// 
//////////////////////////////////////////////////////////////

/* pageName variable */
var pageName = location.href.substring(0, location.href.lastIndexOf("/") + 1);
	pageName = location.href.substring(pageName.length, location.href.length + 1);
	pageName = pageName.substring(0, pageName.indexOf("?"));


/* Constants */
var MESSAGE_CONFIRM_DELETE = "Are you sure you want to delete this item? This action cannot be reversed.";






/* ViewSelected function */
// Jumps to the view action for a given selected user in a results select box
function ViewSelected()
{
	// Check for pageName variable
	if(pageName == null)
	{
		alert("The pageName variable must be set for this function to work.");
		return;
	}

	// Reference to the select box & its selected option values
	var resultsSelection = document.getElementById("selectBox");

	if(resultsSelection.selectedIndex == -1)
		return;

	resultsSelection = resultsSelection.options[resultsSelection.selectedIndex].value;


	// Handle special option entries
	if(resultsSelection == 0)
		return;
	else if(resultsSelection == -1)
	{
		// Special add user option
		location.href = pageName + "?action=add";
		return;
	}


	// Jump to selected user view
	location.href = pageName + "?action=view&id=" + resultsSelection;
}


/* Action function */
// Jumps to a specified action for a given user
function Action(userId, action)
{
	// Check for pageName variable
	if(pageName == null)
	{
		alert("The pageName variable must be set for this function to work.");
		return;
	}


	location.href = pageName + "?action=" + action + "&id=" + userId;
}


/* ConfirmDelete function */
// Forces a user to confirm a delete action
function ConfirmDelete(userId, action)
{
	// Confirm the delete
	if(confirm("Are you sure you want to delete this item? This action cannot be reversed."))
	{
		Action(userId, action);
	}
}


/* ConfirmAction function */
// Forces a user to confirm a specified action, with a specified message
function ConfirmAction(recordId, action, message)
{
	// Confirm the action
	if(confirm(message))
	{
		location.href = pageName + "?action=" + action + "&id=" + recordId;
	}
}



/* GoToLink function */
// Jumps to a specified link url selected from a link list drop down box
function GoToLink(linkList)
{
	var url = linkList.options[linkList.selectedIndex].value;

	if(url != "")
		location.href = url;
}



/* SubframeAction function  */
// Jumps to a specified action in a subframe for a given user, carrying its parent Id in url
function SubframeAction(recordId, parentId, action)
{
	// Check for pageName variable
	if(pageName == null)
	{
		alert("The pageName variable must be set for this function to work.");
		return;
	}


	location.href = pageName + "?action=" + action + "&id=" + recordId + "&parentId=" + parentId;
}


/* SubframeConfirmDelete function */
// Forces a user to confirm a delete action in a subframe, carrying its parent Id in url
function SubframeConfirmDelete(recordId, parentId)
{
	// Confirm the delete
	if(confirm("Are you sure you want to delete this line? This action cannot be reversed."))
	{
		SubframeAction(recordId, parentId, "delete");
	}
}


/* PreviewImage function */
// Previews an uploaded image
function PreviewImage(fieldName, previewElement)
{
	// References to the upload field & preview image
	var uploadImage = document.getElementById(fieldName).value;
	var preview = document.getElementById(previewElement);

	// If there's no image in the field, return
	if(uploadImage.length < 1)
		return;

	// Change the preview image source
	preview.src = uploadImage;
	
	return true;
}


/* PreviewImageBySelectBox function */
// Previews an uploaded image
function PreviewImageBySelectBox(fieldName, imagePath, defaultImage)
{
	// References to the select box field & preview image
	var selectedImage = document.getElementById(fieldName).selectedIndex;
	var preview = document.getElementById("previewImage");


	// Image src
	if(document.getElementById(fieldName).options[selectedImage].value != 0)
	{
		selectedImage = imagePath + "/" + document.getElementById(fieldName).options[selectedImage].text;
		preview.style.display = "";
	}
	else
	{
		selectedImage = defaultImage;
		preview.style.display = "none";
	}


	// Change the preview image source
	preview.src = selectedImage;

	return true;
}


/* SetImageDimensions function */
// Sets form fields with image dimensions
// Used in image library tools
function SetImageDimensions(fieldName, previewElement)
{
	// References to the upload field & preview image
	var uploadField = document.getElementById(fieldName).value;
	var preview = document.getElementById(previewElement);

	// If there's no image in the field, return
	if(uploadField.length < 1)
		return;

	// Update the form fields with width and height settings
	document.getElementById("width").value = preview.width;
	document.getElementById("height").value = preview.height;
}




/***************************/
/* Window Sizing functions */
/***************************/



/* Resize function */
// Resizes a subframe to fit its contents on load
function Resize(frameName)
{
	var frameReference = parent.document.getElementById(frameName);
	var frameHeight = document.body.scrollHeight;

	frameReference.style.height = frameHeight + "px";
}


/* Minimize function */
// Minimizes a subframe
function Minimize(frameName)
{
	var frameReference = parent.document.getElementById(frameName);

	frameReference.style.height = "25px";
}


/* Close function */
// Closes a subframe
function Close(frameName)
{
	var frameReference = parent.document.getElementById(frameName);

	frameReference.style.display = "none";
}



/***************************/
/* Event Handler functions */
/***************************/



/* AddEvent function */
// Adds an event handler to an object
function AddEvent(object, event, fn, useCapture)
{
	// EventListener W3C standard
	if (object.addEventListener)
	{
		object.addEventListener(event, fn, useCapture);
		return true;
	}

	// IE's attachEvent (early W3C standard?)
	else if (object.attachEvent)
	{
		var isAttached = object.attachEvent("on" + event, fn);
		return isAttached;
	}
	else
	{
		alert("Handler could not be attached");
		return false;
	}
} 


/* RemoveEvent function */
// Removes an event handler from an object
function RemoveEvent(object, event, fn, useCapture)
{
	// EventListener W3C standard
	if (object.removeEventListener)
	{
		object.removeEventListener(event, fn, useCapture);
		return true;
	}

	// IE's detachEvent (early W3C standard?)
	else if (object.detachEvent)
	{
		var isRemoved = object.detachEvent("on" + event, fn);
		return isRemoved;
	}
	else
	{
		alert("Handler could not be removed");
		return false;
	}
}



/***************************/
/* Event Handler functions */
/***************************/



/* SubmitFormAction */
// Submits a form, changing its action to the supplied action
function SubmitFormAction( formId, newAction, recordId )
{
	 var formElement = document.getElementById( formId );

	 formElement.action = pageName + "?action=" + newAction;

	 // Attach a record Id to the submission
	 if( recordId.toString().length > 0 )
	 {
		if( document.getElementById("recordId") != null )
			document.getElementById("recordId").value = recordId;
		else
		 	formElement.action += "&recordId=" + recordId;
	}

	 formElement.submit();
}


/* ConfirmFormAction */
// Upon successful confirmation, submits a form, changing its action to the supplied action
function ConfirmFormAction( formId, newAction, message, recordId )
{
	 if( confirm( message ) )
	 	SubmitFormAction( formId, newAction, recordId );
}
