/**
 * Copyright (c) 2005 Margaret Early, The Multiliteracy Project.
 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.txt.
 *
 * Common javascript site wide functionality.
 *
 * $Id: common.js,v 1.33 2006/05/17 22:04:10 aaron Exp $
 */


/**
 * Prompt user for confirmation prior to deleting something.
 */
function confirmDelete() {
	return confirm('Are you sure want to delete?');
}

/**
 * Prompt user for confirmation prior to deleting a named item.
 */
function confirmDelete(item) {
	return confirm('Are you sure want to delete that '+item+'?');
}

/**
 * Prompt user for confirmation prior to deleting some number of items.
 */
function confirmMultipleDelete(count, item) {
	if (count == 0) {
		alert('No ' + item + 's selected!');
		return false;
	} else {
		return confirm('Are you sure want to delete ' + (count > 1 ? 'those '+count+' '+item+'s?' : 'that '+item+'?'));
	}
}

function fakeButtonMouseOver(buttonId) {
	var button = document.getElementById(buttonId);
	button.className = 'button buttonHover';
}

function fakeButtonMouseOut(buttonId) {
	var button = document.getElementById(buttonId);
	button.className = 'button';
}

function toggleStatNav() {
	var nav = document.getElementById('statNav');

	if (nav.style.display == 'none') {
		nav.style.display = 'block';
	} else {
		nav.style.display = 'none';
	}
}

function setAllBoxes(containerId, makeChecked) {
	var container = document.getElementById(containerId);
	var inputs = container.getElementsByTagName('INPUT');

	for (var i = 0; i < inputs.length; i ++) {
		if (inputs[i].type.toLowerCase() == 'checkbox')
			inputs[i].checked = makeChecked;
	}
}

function getBookScreenX(isWide) {
	return (isWide ?	(screen.width >= 1024 ? '1000' : '790') :
				(screen.width >= 1024 ? '1000' : '760'));
}

function getBookScreenY(isWide) {
	return (isWide ?	(screen.height >= 768 ? '780' : '600') :
				(screen.height >= 768 ? '700' : '520'));
}

/**
 * Open window for preview.
 */
function openWindow(url) {
	window.open(url, 'OpenWindow', 'width=785,height=560,screenX=50,screenY=100,toolbar=0,resizable=1,scrollbars=1');
}

/**
 * Open a sized window for preview.
 */
function openSizedWindow(url,width,height,scrollbars) {
	window.open(url, 'OpenWindow', 'width='+width+',height='+height+',screenX=100,screenY=100,toolbar=0,resizable=1,scrollbars='+scrollbars);
}

function submitFormById(formId, action) {
	var form = document.getElementById(formId);

	if (form) {
		form.action = action;
		form.submit();
	}
	return (form != null);
}

function submitForm(frame, action) {
	if (window.frames[frame].document.forms[0]) {
		window.frames[frame].document.forms[0].action = action;
		window.frames[frame].document.forms[0].submit();
	}
}

function submitManagerForm(frame, action) {
	if (window.frames[frame].document.forms[0]) {
		window.frames[frame].document.forms[0].action = action;
		window.frames[frame].document.forms[0].submit();
	} else {
		alert('Please wait until the current view has finished loading before switching views.');
	}
}

/**
 * Update a destination field with data from the source field.
 * @param document string
 * @param field string
 * @param boolean to overwrite existing data
 */
function transferDataToParent(srcField, destField, overwrite) {
	if (window.opener.document.getElementById(destField).value == '' || (typeof(overwrite) == "boolean" && overwrite)) {
		window.opener.document.getElementById(destField).value = srcField.value;
	} else {
		window.opener.document.getElementById(destField).value += ':' + srcField.value;
	}
}

/**
 * Collects selected checkboxes, creates a string value (1:2:3)
 * and sets it to a field.
 * @param srcField string
 * @param destField string (output value to)
 */
function checkboxesToString(srcField, destField) {
	stringValue = '';
	for (i = 0; i < srcField.length; i++) {
		if (srcField[i].checked) {
			values = srcField[i].value.split(':');
			if (stringValue == '') {
				stringValue = values[0];
			} else {
				stringValue += ':' + values[0];
			}
		}
	}
	destField.value = stringValue;
}

/**
 * Checks browser
 * @return string
 */
function checkIEBrowser() {
	if(document.all)
		return true;
	else
		false;
}

/**
 * Remove an element from an associative array
 */
function removeElement(array, fileId){
	result = new Array();
	var k=0;
	for(i = 0; i<array.length; i++){
		if(fileId != array[i]){
			result[k] = array[i];
			k++;
		}
	}
	return result;
}


/**
 * Toggles a hidden field to be displayed
 * @param element form field
 */
function toggle(element) {
	element.style.display = (element.style.display == "block") ? "none" : "block";
}

/**
 * Checks if popup is closed and submits form
 * @param action string
 */
function popupCloseAndUpdate(formId, action) {
	if (popup.closed) {
		changeActionAndSubmit(formId, action);
		return
	}
	setTimeout("popupCloseAndUpdate('"+formId+"','"+action+"')",1000);
}

/**
 * Change action and submit form
 * @param form
 * @param action
 * @param refresh
 */
function changeActionAndSubmit(formId, action) {
	document.getElementById(formId).action = action;
	document.getElementById(formId).submit();
}

/**
 * Function to check if an array contains a value
 * @param array
 * @param value
 */
function contains(array, value){
	for(i=0;i<array.length;i++){
		if(array[i]==value)
			return true;
	}
	return false;
}
