String.prototype.lPad = function (n,c) {var i; var a = this.split(''); for (i = 0; i < n - this.length; i++) {a.unshift (c)}; return a.join('')}
String.prototype.rPad = function (n,c) {var i; var a = this.split(''); for (i = 0; i < n - this.length; i++) {a.push (c)}; return a.join('')}

var cms_checkon = new Array("save", "apply");
var cms_i8n_hash = new cms_Hash();
var cms_submitted = false;

function buttonsubmit(button) {
	if (cms_submitted) return false;
	if (button == 'help') {
		//komt nog
	} else {
		if (document.adminform) {
			if (document.adminform.action) document.adminform.action.value = button;
			if (button == 'delete') {
				if (document.adminform.idarray) {
					if (!confirm(cms_i8n('WARN_ITEMS_DELETE', 'Delete the selected items?'))) return false;
				} else {
					if (!confirm(cms_i8n('WARN_ITEM_DELETE', 'Delete this item?'))) return false;
				}
			}
			
			if (cms_checkon && cms_in_array(button, cms_checkon)) {
				var message = cms_check_required();
				if (message) {
					var el = document.getElementById('systemmessage');
					if (el) {
						el.innerHTML = message;
						el.className = 'systemerror';
					}
					return false;
				}
				if (typeof cms_onsubmit != 'undefined') {
					for (var i=0;i<cms_onsubmit.length;i++) {
						message+=cms_onsubmit[i]();
					}
				}
				if (message) {
					var el = document.getElementById('systemmessage');
					if (el) {
						el.innerHTML = message;
						el.className = 'systemerror';
					}
					return false;
				}
			}
			cms_submitted = true;
			document.adminform.submit();
		}
	}
}

function cms_i8n(key, i8ndefault) {
	if (cms_i8n_hash.length == 0) {
		if (typeof cms_i8n_init != 'undefined') cms_i8n_init();
	}
	if (cms_i8n_hash.hasItem(key)) return cms_i8n_hash.getItem(key);
	else if (typeof i8ndefault != 'undefined') return i8ndefault;
	else return '';
}

function setTableSort(field, direction) {
	if (!document.adminform) return false;
	document.adminform.action.value = 'setfilter';
	var el = document.getElementById('filterordering');
	if (el) el.value = field;
	else return false;
	var el = document.getElementById('filterorderdir');
	if (el) el.value = direction;
	else return false;
	document.adminform.submit();
}

function cms_check_required() {
	var msg = '';
	if (typeof cms_required != 'undefined') {
		for (var i=0;i<cms_required.length;i++) {
			var el = document.getElementById(cms_required[i]);
			if (el) {
				var ellbl = document.getElementById('lbl'+cms_required[i]);
				var label = el.name;
				if (ellbl) label = ellbl.getAttribute('title');
				if (el.value == '') {
					msg+=sprintf(cms_i8n('ERR_ITEM_EMPTY', '%s is required.'), label)+'<br />';
				}
			}
		}
	}
	return msg;
}

function cms_in_array(string, array)
{
	for (var i=0;i<array.length;i++) {
		if (string == array[i]) return true;
	}
	return false;
}

function cms_move_select_options(selectFrom, selectTo) {
	for (var i=selectFrom.options.length-1; i >= 0; i--) {
		if (selectFrom.options[i].selected) {
			var elOption = document.createElement('option');
			elOption.text = selectFrom.options[i].text;
			elOption.value = selectFrom.options[i].value;
			try {
				selectTo.add(elOption, null);
			} catch(ex) {
				selectTo.add(elOption); // IE only
			}
			selectFrom.remove(i);
		}
	}
}

function setIdArray(id) {
	if (document.adminform.idarray) {
		if (id) {
			document.adminform.idarray.value = id;
		} else {
			els = document.getElementsByName('cid[]');
			var ids = '';
			for(var i=0;i<els.length;i++) {
				if (els[i].checked) ids += els[i].getAttribute('value') + ',';
			}
			ids = ids.substring(0, ids.length-1);
			document.adminform.idarray.value = ids;
		}
	}
}

function checkCid(id) {
	cid = document.getElementById('c'+id);
	if (cid) cid.setAttribute('checked', 'checked');
	setIdArray(cid.getAttribute('value'));
	return true;
}

function cms_getSefName(name) {
//	retval = name.replace(/&|%|;|:|@|=|,|/|\(|\)|\||\\|\^|'|\?|\+|\$|\*|<|>|#|%|\"/g, '_');
	retval = name.replace(/;|\/|\?|:|@|&|=|\+|\$|,|\(|\)|\||\\|\^|\[|\]|'|<|>|#|%|"/g, '_');
	retval = retval.replace(/  /g, " ");
	retval = retval.replace(/ /g, "-");
	return retval;
}

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, dateFormat) {
	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
		
		if ( dateFormat )	// optional date format
		{
			cal.setDateFormat(dateFormat);
		}
		
		calendar.create();		// create a popup calendar
		calendar.parseDate(el.value); // set it to a new date
	}
	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;
}

function cms_Hash()
{
	this.length = 0;
	this.items = new Array();
	for (var i = 0; i < arguments.length; i += 2) {
		if (typeof(arguments[i + 1]) != 'undefined') {
			this.items[arguments[i]] = arguments[i + 1];
			this.length++;
		}
	}
   
	this.removeItem = function(in_key)
	{
		var tmp_value;
		if (typeof(this.items[in_key]) != 'undefined') {
			this.length--;
			var tmp_value = this.items[in_key];
			delete this.items[in_key];
		}
	   
		return tmp_value;
	}

	this.getItem = function(in_key) {
		return this.items[in_key];
	}

	this.setItem = function(in_key, in_value)
	{
		if (typeof(in_value) != 'undefined') {
			if (typeof(this.items[in_key]) == 'undefined') {
				this.length++;
			}

			this.items[in_key] = in_value;
		}
	   
		return in_value;
	}

	this.hasItem = function(in_key)
	{
		return typeof(this.items[in_key]) != 'undefined';
	}
}

/**
 * sprintf() for JavaScript v.0.4
 *
 * Copyright (c) 2007 Alexandru Marasteanu <http://alexei.417.ro/>
 * Thanks to David Baird (unit test and patch).
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */

function str_repeat(i, m) { for (var o = []; m > 0; o[--m] = i); return(o.join('')); }

function sprintf () {
  var i = 0, a, f = arguments[i++], o = [], m, p, c, x;
  while (f) {
    if (m = /^[^\x25]+/.exec(f)) o.push(m[0]);
    else if (m = /^\x25{2}/.exec(f)) o.push('%');
    else if (m = /^\x25(?:(\d+)\$)?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(f)) {
      if (((a = arguments[m[1] || i++]) == null) || (a == undefined)) throw("Too few arguments.");
      if (/[^s]/.test(m[7]) && (typeof(a) != 'number'))
        throw("Expecting number but found " + typeof(a));
      switch (m[7]) {
        case 'b': a = a.toString(2); break;
        case 'c': a = String.fromCharCode(a); break;
        case 'd': a = parseInt(a); break;
        case 'e': a = m[6] ? a.toExponential(m[6]) : a.toExponential(); break;
        case 'f': a = m[6] ? parseFloat(a).toFixed(m[6]) : parseFloat(a); break;
        case 'o': a = a.toString(8); break;
        case 's': a = ((a = String(a)) && m[6] ? a.substring(0, m[6]) : a); break;
        case 'u': a = Math.abs(a); break;
        case 'x': a = a.toString(16); break;
        case 'X': a = a.toString(16).toUpperCase(); break;
      }
      a = (/[def]/.test(m[7]) && m[2] && a > 0 ? '+' + a : a);
      c = m[3] ? m[3] == '0' ? '0' : m[3].charAt(1) : ' ';
      x = m[5] - String(a).length;
      p = m[5] ? str_repeat(c, x) : '';
      o.push(m[4] ? a + p : p + a);
    }
    else throw ("Huh ?!");
    f = f.substring(m[0].length);
  }
  return o.join('');
}
