// --------------------------------------------------------------------
// Norme de packaging : 1.0.6
// Nom du package - Package name : social-transformation-management-database
// Nom du programme - Program Name : utils.js
// Auteurs - Authors :
// - THRIERR Eric eric@excellent-rameur.com
// Version : 0.0.1
// Description : javascript de fonctions utilitaires
// Historique - History :
// - 2007-11-08 / Eric Thrierr / fichier initial
// --------------------------------------------------------------------
// $Id: utils.js,v 1.2 2010/02/17 10:09:52 uid1012 Exp $
String.prototype.trim = function() {
	return this.replace( /^\s+|\s+$/, "" );
}
var utils = new Object();
utils.init = function() {
	this.form = document.mostDbForm;
}
utils.onload = (window.onload) ? window.onload : function() {};
window.onload = function() {utils.onload(); utils.init()};
utils.changeLang = function(element, lang) {
	if (this.form.elements["search"]) this.form.change.value = "lang";
	this.form.elements[element].value = lang;
	this.form.submit();
}
utils.changeSelect = function(node) {
	if (node.options[0].selected) for (var i = 1; i < node.options.length; i++) node.options[i].selected = false;
	return true;
}
utils.change = function(node, newSearch) {
	if (node.className != "inactive") {
		node.href += "&collection=" + this.form.collection.value;
		node.href += "&compare=" + this.form.compare.value;
		if (!newSearch) node.href += "&criteria=" + this.form.criteria.value;
		return true;
	} else {
		return false;
	}
}
utils.changeCriteria = function(url, node, newSearch) {
	if (node.className != "inactive") {
		var location = url + "&collection=" + this.form.collection.value + "&compare=" + this.form.compare.value;
		if (!newSearch) location += "&criteria=" + this.form.criteria.value;
		document.location = location;
		return true;
	} else {
		return false;
	}
}
utils.compare = function(node, id, width, height) {
	if (node.className != "inactive") {
		return utils.popup(node, id, "scrollbars,resizable,width=" + width + ",height=" + height);
	} else {
		return false;
	}
}
utils.changeColumns = function() {
	this.form.change.value = "columns";
	this.form.submit();
}
utils.changePageSize = function() {
	this.form.newsize.value = this.form.pagesize.options[this.form.pagesize.selectedIndex].value;
	this.form.submit();
}
utils.showPage = function(page) {
	this.form.show.value = page;
	this.form.submit();
}
utils.changeSort = function(sort) {
	this.form.sort.value = sort;
	this.form.change.value = "sort";
	this.form.submit();
	return false;
}
utils.changeImg = function(image, source) {
	document[image].src = source;
	return true;
}
utils.collectionShow = function() {
	if (!opener.closed ) {
		if (!opener.opener.closed) opener.opener.focus();
	}
}
utils.collectionCompare = function(url, message, id, width, height) {
	var collection = new Array();
	collection = this.form.collection.value.split(",");
	if (collection.length > 1) {
		return utils.popup(url, id, "scrollbars,resizable,width=" + width + ",height=" + height);
	} else {
		alert(message);
		return false;
	}
}
utils.collectionEdit = function(url, message) {
	var collection = new Array();
	collection = this.form.collection.value.split(",");
	if (collection.length > 1) {
		document.location = url + "&collection=" + this.form.collection.value + "&compare=" + this.form.compare.value + "&criteria=" + this.form.criteria.value;
	} else {
		alert(message);
		return false;
	}
}
utils.collectionChange = function(node) {
	var updatedCollection = new Array();
	if (this.form.collection.value) {
		var currentCollection = this.form.collection.value.split(",");
		for (var i = 0; i < currentCollection.length; i++) {
			if (currentCollection[i] != node.value) updatedCollection.push(currentCollection[i]);
		}
	}
	if (node.checked) updatedCollection.push(node.value);
	this.form.collection.value = updatedCollection.join(",");
	var compareLink = document.getElementById("mostDbCollectionCompare");
	if (compareLink) {
		if (updatedCollection.length > 1) compareLink.className = "";
		else compareLink.className = "inactive";
	}
}
utils.collectionAll = function() {
	var collection = this.form.collection.value.split(",");
	for (var i = 0; i < this.form.elements["items[]"].length; i++) {
		this.form.elements["items[]"][i].checked = true;
		var added = false;
		for (var j = 0; j < collection.length; j++) {
			if (collection[j] == this.form.elements["items[]"][i].value) {
				added = true;
				break;
			}
		}
		if (!added) collection.push(this.form.elements["items[]"][i].value);
	}
	this.form.collection.value = collection.join(",");
	return false;
}
utils.collectionClear = function() {
	var collection = this.form.collection.value.split(",");
	for (var i = 0; i < this.form.elements["items[]"].length; i++) {
		this.form.elements["items[]"][i].checked = false;
		for (var j = 0; j < collection.length; j++) {
			if (collection[j] == this.form.elements["items[]"][i].value) {
				collection.splice(j, 1);
				break;
			}
		}
	}
	this.form.collection.value = collection.join(",");
	return false;
}
utils.view = function(node, id, width, height) {
	return utils.popup(node, "view" + id, "scrollbars,resizable,width=" + width + ",height=" + height);
}
utils.save = function (format, width, height) {
	location.href += "&format=" + format;
}
utils.popup = function(url, name, features) {
	if (features) {
		var width = parseInt (features.replace(/.*width=/, ""));
		var height = parseInt (features.replace(/.*height=/, ""));
		var newWindow = window.open(url, name, features);
		if (width && height && window.resizeTo) newWindow.resizeTo (width, height);
		if (window.moveTo) newWindow.moveTo(0,0);
	} else var newWindow = window.open(url, name);
	newWindow.focus();
	return false;
}
utils.removeClassName = function(elem, className) {
	elem.className = elem.className.replace(className, "").trim();
}
utils.addClassName = function(elem, className) {
	utils.removeClassName(elem, className);
	elem.className = (elem.className + " " + className).trim();
}
utils.toggle = function(node, id) {
	var element = document.getElementById(id);
	if (element.className.indexOf("most-display-none") >= 0) {
		utils.removeClassName(element, "most-display-none");
		//utils.addClassName(element, "most-display-block");
		utils.addClassName(node, "most-unfolded");
	} else {
		//utils.removeClassName(element, "most-display-block");
		utils.addClassName(element, "most-display-none");
		utils.removeClassName(node, "most-unfolded");
	}
}
