// <?php !! This fools phpdocumentor into parsing this file
// $Id: mambojavascript.js,v 1.16 2003/12/22 00:52:20 eddieajau Exp $
/**
* Content code
* @package Mambo Open Source
* @Copyright (C) 2000 - 2003 Miro International Pty Ltd
* @ All rights reserved
* @ Mambo Open Source is Free Software
* @ Released under GNU/GPL License : http://www.gnu.org/copyleft/gpl.html
* @version $Revision: 1.16 $
**/// general utility for browsing a named array or object
function xshow(o) {
s = '';
for(e in o) {s += e+'='+o+'\n';}
alert( s );
}
/**
* Writes a dynamically generated list
* @param string The parameters to insert into the <select> tag
* @param array A javascript array of list options in the form
* @param string The key to display for the initial state of the list
* @param string The original key that was selected
* @param string The original item value that was selected
*/
function writeDynaList( selectParams, source, key, orig_key, orig_val ) {
var html = '\n <select ' + selectParams + '>';
var i = 0;
for (x in source) {
if (source == key) {
var selected = '';
if ((orig_key == key && orig_val == source) || (i == 0 && orig_key != key)) {
selected = 'selected="selected"';
}
html += '\n <option value="'+source+'" '+selected+'>'+source+'</option>';
}
i++;
}
html += '\n </select>';
document.writeln( html );
}
/**
* Changes a dynamically generated list
* @param string The name of the list to change
* @param array A javascript array of list options in the form
* @param string The key to display
* @param string The original key that was selected
* @param string The original item value that was selected
*/
function changeDynaList( listname, source, key, orig_key, orig_val ) {
var list = eval( 'document.adminForm.' + listname );
// empty the list
for (i in list.options.length) {
list.options = null;
}
i = 0;
for (x in source) {
if (source == key) {
opt = new Option();
opt.value = source;
opt.text = source;
if ((orig_key == key && orig_val == opt.value) || i == 0) {
opt.selected = true;
}
list.options = opt;
}
}
list.length = i;
}
/**
* Adds a select item(s) from one list to another
*/
function addSelectedToList( frmName, srcListName, tgtListName ) {
var form = eval( 'document.' + frmName );
var srcList = eval( 'form.' + srcListName );
var tgtList = eval( 'form.' + tgtListName );
var srcLen = srcList.length;
var tgtLen = tgtList.length;
var tgt = "x";
//build array of target items
for (var i=tgtLen-1; i > -1; i--) {
tgt += "," + tgtList.options.value + ","
}
//Pull selected resources and add them to list
for (var i=srcLen-1; i > -1; i--) {
if (srcList.options.selected && tgt.indexOf( "," + srcList.options.value + "," ) == -1) {
opt = new Option( srcList.options.text, srcList.options.value );
tgtList.options = opt;
}
}
}
function delSelectedFromList( frmName, srcListName ) {
var form = eval( 'document.' + frmName );
var srcList = eval( 'form.' + srcListName );
var srcLen = srcList.length;
for (var i=srcLen-1; i > -1; i--) {
if (srcList.options.selected) {
srcList.options = null;
}
}
}
function moveInList( frmName, srcListName, index, to) {
var form = eval( 'document.' + frmName );
var srcList = eval( 'form.' + srcListName );
var total = srcList.options.length-1;
if (index == -1) {
return false;
}
if (to == +1 && index == total) {
return false;
}
if (to == -1 && index == 0) {
return false;
}
var items = new Array;
var values = new Array;
for (i=total; i >= 0; i--) {
items = srcList.options.text;
values = srcList.options.value;
}
for (i = total; i >= 0; i--) {
if (index == i) {
srcList.options = new Option(items,values, 0, 1);
srcList.options = new Option(items, values);
i--;
} else {
srcList.options = new Option(items, values);
}
}
srcList.focus();
}
function setSelectedValue( frmName, srcListName, value ) {
var form = eval( 'document.' + frmName );
var srcList = eval( 'form.' + srcListName );
var srcLen = srcList.length;
for (var i=0; i < srcLen; i++) {
srcList.options.selected = false;
if (srcList.options.value == value) {
srcList.options.selected = true;
}
}
}
function getSelectedValue( frmName, srcListName ) {
var form = eval( 'document.' + frmName );
var srcList = eval( 'form.' + srcListName );
i = srcList.selectedIndex;
if (i != null && i > -1) {
return srcList.options.value;
} else {
return null;
}
}
function getSelectedText( frmName, srcListName ) {
var form = eval( 'document.' + frmName );
var srcList = eval( 'form.' + srcListName );
i = srcList.selectedIndex;
if (i != null && i > -1) {
return srcList.options.text;
} else {
return null;
}
}
function chgSelectedValue( frmName, srcListName, value ) {
var form = eval( 'document.' + frmName );
var srcList = eval( 'form.' + srcListName );
i = srcList.selectedIndex;
if (i != null && i > -1) {
srcList.options.value = value;
return true;
} else {
return false;
}
}
// Form specific functions for editting content images
function showImageProps(base_path) {
form = document.adminForm;
value = getSelectedValue( 'adminForm', 'imagelist' );
parts = value.split( '|' );
form._source.value = parts;
setSelectedValue( 'adminForm', '_align', parts || '' );
form._alt.value = parts || '';
form._border.value = parts || '0';
previewImage( 'imagelist', 'view_imagelist', base_path );
}
function applyImageProps() {
form = document.adminForm;
if (!getSelectedValue( 'adminForm', 'imagelist' )) {
alert( "Select and image from the list" );
return;
}
value = form._source.value + '|'
+ getSelectedValue( 'adminForm', '_align' ) + '|'
+ form._alt.value + '|'
+ parseInt( form._border.value );
chgSelectedValue( 'adminForm', 'imagelist', value );
}
function previewImage( list, image, base_path ) {
form = document.adminForm;
srcList = eval( "form." + list );
srcImage = eval( "document." + image );
var fileName = srcList.options.text;
var fileName2 = srcList.options.value;
if (fileName.length == 0 || fileName2.length == 0) {
srcImage.src = 'images/blank.gif';
} else {
srcImage.src = base_path + fileName;
}
}
/**
* Toggles the check state of a group of boxes
*
* Checkboxes must have an id attribute in the form cb0, cb1...
* @param The number of box to 'check'
*/
function checkAll( n ) {
var f = document.adminForm;
var c = f.toggle.checked;
var n2 = 0;
for (i=0; i < n; i++) {
cb = eval( 'f.cb' + i );
if (cb) {
cb.checked = c;
n2++;
}
}
if (c) {
document.adminForm.boxchecked.value = n2;
} else {
document.adminForm.boxchecked.value = 0;
}
}
/**
*/
function listItemTask( id, task ) {
var f = document.adminForm;
cb = eval( 'f.' + id );
if (cb) {
cb.checked = true;
submitbutton(task);
}
return false;
}
function isChecked(isitchecked){
if (isitchecked == true){
document.adminForm.boxchecked.value++;
}
else {
document.adminForm.boxchecked.value--;
}
}
/**
* Default function. Usually would be overriden by the component
*/
function submitbutton(pressbutton) {
submitform(pressbutton);
}
/**
* Submit the admin form
*/
function submitform(pressbutton){
document.adminForm.task.value=pressbutton;
try {
document.adminForm.onsubmit();
}
catch(e){}
document.adminForm.submit();
}
/**
* Getting radio button that is selected.
*/
function getSelected(allbuttons){
for (i=0;i<allbuttons.length;i++) {
if (allbuttons.checked) {
return allbuttons.value
}
}
}
// JS Calendar
var calendar = null; // remember the calendar object so that we reuse
// it and avoid creating another
// This function gets called when an end-user clicks on some date
function selected(cal, date) {
cal.sel.value = date; // just update the value of the input field
}
// And this gets called when the end-user clicks on the _selected_ date,
// or clicks the "Close" (X) button. It just hides the calendar without
// destroying it.
function closeHandler(cal) {
cal.hide(); // hide the calendar
// don't check mousedown on document anymore (used to be able to hide the
// calendar when someone clicks outside it, see the showCalendar function).
Calendar.removeEvent(document, "mousedown", checkCalendar);
}
// This gets called when the user presses a mouse button anywhere in the
// document, if the calendar is shown. If the click was outside the open
// calendar this function closes it.
function checkCalendar(ev) {
var el = Calendar.is_ie ? Calendar.getElement(ev) : Calendar.getTargetElement(ev);
for (; el != null; el = el.parentNode)
// FIXME: allow end-user to click some link without closing the
// calendar. Good to see real-time stylesheet change 
if (el == calendar.element || el.tagName == "A") break;
if (el == null) {
// calls closeHandler which should hide the calendar.
calendar.callCloseHandler(); Calendar.stopEvent(ev);
}
}
// This function shows the calendar under the element having the given id.
// It takes care of catching "mousedown" signals on document and hiding the
// calendar if the click was outside.
function showCalendar(id) {
var el = document.getElementById(id);
if (calendar != null) {
// we already have one created, so just update it.
calendar.hide(); // hide the existing calendar
calendar.parseDate(el.value); // set it to a new date
} else {
// first-time call, create the calendar
var cal = new Calendar(true, null, selected, closeHandler);
calendar = cal; // remember the calendar in the global
cal.setRange(1900, 2070); // min/max year allowed
calendar.create(); // create a popup calendar
}
calendar.sel = el; // inform it about the input field in use
calendar.showAtElement(el); // show the calendar next to the input field
// catch mousedown on the document
Calendar.addEvent(document, "mousedown", checkCalendar);
return false;
}
/**
* Pops up a new window in the middle of the screen
*/
function popupWindow(mypage, myname, w, h, scroll) {
var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
win = window.open(mypage, myname, winprops)
if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}
// LTrim(string) : Returns a copy of a string without leading spaces.
function ltrim(str)
{
var whitespace = new String(" \t\n\r");
var s = new String(str);
if (whitespace.indexOf(s.charAt(0)) != -1) {
var j=0, i = s.length;
while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
j++;
s = s.substring(j, i);
}
return s;
}
//RTrim(string) : Returns a copy of a string without trailing spaces.
function rtrim(str)
{
var whitespace = new String(" \t\n\r");
var s = new String(str);
if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
var i = s.length - 1; // Get length of string
while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
i--;
s = s.substring(0, i+1);
}
return s;
}
// Trim(string) : Returns a copy of a string without leading or trailing spaces
function trim(str) {
return rtrim(ltrim(str));
}