/******************************************************************************
* odfClientRuntime.js
*******************************************************************************
Runtime client pour les contrôles
*******************************************************************************
*                                                                             *
* Copyright 2000-2002								                          *
*                                                                             *
******************************************************************************/
function formatCurrency(currency, simple)
{
	var langPrefix = lang.substr(0, 2);
	if(simple != true && langPrefix == "en") simple = true;
	var sign ="";
	if(currency < 0) {
		currency = -currency;
		sign = "-";
	}
	var integer = Math.floor(currency);
	var full = "" + integer;
	var decimal = "" + Math.floor(100 * (currency - integer));
	if(decimal.length == 1) decimal = "0"+decimal;
	var res = null;
	var pos = full.length;
	while(pos > 3) {
		block = full.substr(pos - 3, 3);
		if(res == null) {
			res = block;
		} else {
			if(simple) {
				res = block+","+res;
			} else {
				res = block+"\240"+res;
			}
		}
		pos -= 3;
	}
	if(pos > 0) {
		block = full.substr(0, pos);
		if(res == null) {
			res = block;
		} else {
			if(simple) {
				res = block+","+res;
			} else {
				res = block+"\240"+res;
			}
		}
	}
	if(simple) {
		res += "."+decimal;
	} else {
		res += ","+decimal;
	}
	return sign+res;
}

function formatTimestamp(timestamp, format)
{
	switch(format) {
	case "hours":
		return objDateUtils.getStringDate(timestamp, "G:i:s");
	case "time":
		return objDateUtils.getStringDate(timestamp, "d/m/Y G:i:s");
	case "date":
	default:
		return objDateUtils.getStringDate(timestamp, "d/m/Y");
	}
}

function showModalWindow(url, name, width, height, arguments)
{
	var leftPos = (screen.availWidth - width) / 2;
	var topPos = (screen.availHeight - height) / 2;
	if(document.all) {
		width = parseInt(width, 10);
		height = parseInt(height, 10);
		width += 8;
		height += 29;
		var options = 'resizable=yes;scroll=no;edge=sunken;status=no;dialogWidth=' + width + 'px;dialogHeight=' + height + 'px;dialogLeft=' + leftPos + 'px;dialogTop=' + topPos + "px;";
		//consoleDump("window.showModalDialog(\"" + url + "\", arguments, \"" + options +"\")");
		window.showModalDialog(url, arguments, options);
	} else {
		var options = 'width=' + width + ',height=' + height + ',left=' + leftPos + ',top=' + topPos;
		var win = window.open(url, name, options);
		if (win==null) {
			alert(objThesaurus.translate("odfCheckPopupBlocker"));
		} else {
			win.focus();
		}
	}
}


function initSelect(id)
{
	var select = document.getElementById(id);
	if(select == null) return;
	var value = select.getAttribute("V");
	if(value == null) return;
	var options = select.options;
	var defaultValue = -1;
	for(var i=0;i<options.length;i++) {
		var option = options.item(i);
		if(option.value == value) {
			select.selectedIndex = i;
			return;
		}
		if(option.getAttribute("DEFAULT") != null) {
			defaultValue = i;
		}
	}
	select.selectedIndex = defaultValue;
}

function updateCheckBox(id, init)
{
	var hidden = document.getElementById(id);
	var checkbox = document.getElementById(id+"_c");
	if(hidden == null || checkbox == null) {
		return;
	}
	if(init) {
		checkbox.checked = hidden.value == "true";
	} else {
		hidden.value = "" + checkbox.checked;
	}
}

function updatePrivateImage(id, init)
{
	var hidden = document.getElementById(id);
	var image = document.getElementById(id+"_i");
	var tempoImage = document.getElementById(id+"_ih");
	var input = document.getElementById(id+"_f");
	var labelInput = document.getElementById(id+"_l");
	var para = document.getElementById(id+"_p");
	var descriptionInput = document.getElementById(id+"_d");
	if(hidden == null || image == null || input == null || tempoImage == null) return;
	var w = parseInt(hidden.getAttribute("W"), 10);
	var oid = parseInt(hidden.value, 10);
	var h = parseInt(hidden.getAttribute("H"), 10);
	var swidth = parseInt(hidden.getAttribute("SWIDTH"), 10);
	var sheight = parseInt(hidden.getAttribute("SHEIGHT"), 10);
	var label = hidden.getAttribute("SLABEL");
	var description = hidden.getAttribute("SDESCRIPTION");
	var swidth;
	var sheight;
	var sourceUrl;
	var resizedUrl;
	if(init) {
		sourceUrl = hidden.getAttribute("SURL");
		var size = computeImageSize(w, h, swidth, sheight);
		resizedUrl = "./imageProvider.asp?private_image=" + oid + "&ew=" + size[0] + "&eh=" + size[1];
	} else {
		hidden.value = "";
		tempoImage.style.display = "inline";
		resizedUrl = sourceUrl = tempoImage.src = input.value;
		sourceUrl = sourceUrl.replace(/\\/g, "/");
	}
	if(sourceUrl != null && sourceUrl != ""&& sourceUrl.substr(sourceUrl.length -1) != "/") {
		image.src = resizedUrl;
		image.className = "previewImage";
		var pos = sourceUrl.lastIndexOf("/");
		var filename = sourceUrl.substr(pos+1);
		if(labelInput != null) labelInput.value = label;
		if(descriptionInput != null) descriptionInput.value = description;
		if(para != null) para.innerHTML = filename;
		if(!init) return;
		resizeImagePreview(image, w, h, swidth, sheight);
		var title = filename + "\n" + swidth + " x " + sheight;
		image.title = title;
	} else {
		image.src = "./iso_icons/empty.gif";
		image.style.width = "0px";
		image.style.height = "0px";
		image.className = "missingPreviewImage";
		var title = "Pas d'image";
		image.title = title;
		if(labelInput != null) labelInput.value = "";
		if(descriptionInput != null) descriptionInput.value = "";
	}
}
function computeImageSize(w, h, swidth, sheight)
{
	if(w * sheight > h * swidth) {
		if(sheight < h) {
			h = sheight;
			w = swidth;
		} else {
			w = Math.round(h * swidth/sheight);
		}
	} else {
		if(swidth < w) {
			w = swidth;
			h = sheight;
		} else {
			h = Math.round(w * sheight/swidth);
		}
	}
	return new Array(w,h);
}

function resizeImagePreview(image, w, h, swidth, sheight)
{
	var size = computeImageSize(w, h, swidth, sheight);
	var width = size[0];
	var height = size[1];
	if(!isNaN(width)) image.style.width = width + "px";
	if(!isNaN(height)) image.style.height = height + "px";
}

function onloadImagePreview(id)
{
	var hidden = document.getElementById(id);
	var image = document.getElementById(id+"_i");
	var tempoImage = document.getElementById(id+"_ih");
	var input = document.getElementById(id+"_f");
	var inputw = document.getElementById(id+"_w");
	var inputh = document.getElementById(id+"_h");
	if(hidden == null || image == null || input == null || tempoImage == null) return;
	var w = parseInt(hidden.getAttribute("W"), 10);
	var h = parseInt(hidden.getAttribute("H"), 10);
	var swidth = tempoImage.width;
	if(inputw != null) inputw.value = swidth + "";
	var sheight = tempoImage.height;
	if(inputh != null) inputh.value = sheight + "";
	image.src = input.value;
	tempoImage.style.display = "none";
	resizeImagePreview(image, w, h, swidth, sheight);
	var title = input.value + "\n" + swidth + " x " + sheight;
	image.title = title;
}

function removePrivateImage(id)
{
	var hidden = document.getElementById(id);
	var image = document.getElementById(id+"_i");
	var tempoImage = document.getElementById(id+"_ih");
	var input = document.getElementById(id+"_f");
	var inputw = document.getElementById(id+"_w");
	var inputh = document.getElementById(id+"_h");
	var para = document.getElementById(id+"_p");
	if(hidden == null || image == null || input == null || tempoImage == null) return;
	var w = parseInt(hidden.getAttribute("W"), 10);
	var h = parseInt(hidden.getAttribute("H"), 10);
	hidden.value = "";
	image.src = "./iso_icons/empty.gif";
	image.style.width = w + "px";
	image.style.height = h + "px";
	image.className = "missingPreviewImage";
	input.value = "";
	var title = "Pas d'image";
	image.title = title;
	if(inputw != null) inputw.value = "";
	if(inputh != null) inputh.value = "";	
	if(para != null) para.innerHTML = title;	
}

function updatePrivateResource(id, init)
{
	var hidden = document.getElementById(id);
	var image = document.getElementById(id+"_i");
	var tempoImage = document.getElementById(id+"_ih");
	var input = document.getElementById(id+"_f");
	var para = document.getElementById(id+"_p");
	var labelInput = document.getElementById(id+"_l");
	var descriptionInput = document.getElementById(id+"_d");
	var inputw = document.getElementById(id+"_w");
	var inputh = document.getElementById(id+"_h");
	if(hidden == null || image == null || input == null|| para == null);
	var w = parseInt(hidden.getAttribute("W"), 10);
	var h = parseInt(hidden.getAttribute("H"), 10);
	var oid = parseInt(hidden.value, 10);
	var swidth = parseInt(hidden.getAttribute("SWIDTH"), 10);
	var sheight = parseInt(hidden.getAttribute("SHEIGHT"), 10);
	var sourceUrl;
	if(init) {
		sourceUrl = hidden.getAttribute("SURL");
		sourceUrl = sourceUrl.replace(/\/\//g, "/").replace(/^.\//, "");
		var size = computeImageSize(w, h, swidth, sheight);
		resizedUrl = "./imageProvider.asp?private_image=" + oid + "&ew=" + size[0] + "&eh=" + size[1];
	} else {
		hidden.value = "";
		sourceUrl = input.value;
	}
	if(sourceUrl != null && sourceUrl != "") {
		var label = hidden.getAttribute("SLABEL");
		var description = hidden.getAttribute("SDESCRIPTION");
		var pos = sourceUrl.lastIndexOf("/");
		var filename = sourceUrl.substr(pos+1);
		var title = filename;
		image.title = title;
		var pos = sourceUrl.lastIndexOf("\\");
		var filename = filename.substr(pos+1);
		pos = sourceUrl.lastIndexOf(".");
		var extension = sourceUrl.substr(pos+1).toLowerCase();
		switch(extension) {
		case "png":
		case "gif":
		case "jpg":
		case "jpeg":
			image.src = resizedUrl;
			image.className = "previewImage";
			image.parentNode.style.paddingTop = "0px";
			image.parentNode.style.paddingBottom = "0px";
			if(!init) {
				tempoImage.style.display = "inline";
				tempoImage.src = sourceUrl;
			}
			resizeImagePreview(image, w, h, swidth, sheight);
			var title = filename + "\n" + swidth + " x " + sheight;
			image.title = title;
			break;
		default:
			image.src = "./iso_icons/download.png";
			image.className = "previewResource";
			image.width = 16;
			image.height = 16;
			image.parentNode.style.paddingTop = "16px";
			image.parentNode.style.paddingBottom = "16px";
			if(inputw != null) inputw.value = "";
			if(inputh != null) inputh.value = "";	
		}
		para.innerHTML = filename;
		if(labelInput != null) labelInput.value = label;
		if(descriptionInput != null) descriptionInput.value = description;
	} else {
		image.src = "./iso_icons/empty.gif";
		image.className = "missingPreviewResource";
		var title = "Pas de document";
		image.title = title;
		para.innerHTML = title;
		if(labelInput != null) labelInput.value = "";
		if(descriptionInput != null) descriptionInput.value = "";
		if(inputw != null) inputw.value = "";
		if(inputh != null) inputh.value = "";	
	}

}


function removePrivateResource(id)
{
	var hidden = document.getElementById(id);
	var image = document.getElementById(id+"_i");
	var input = document.getElementById(id+"_f");
	var para = document.getElementById(id+"_p");
	if(hidden == null || image == null || input == null) return;
	hidden.value = "";
	image.src = "./iso_icons/empty.gif";
	image.className = "missingPreviewImage";
	input.value = "";
	var title = "Pas de document";
	image.title = title;
	para.innerHTML = title;
}

function updateImage(id, isResource)
{
	var hidden = document.getElementById(id);
	var image = document.getElementById(id+"_i");
	var para = document.getElementById(id+"_p");
	if(hidden == null || image == null) return;
	var sourceUrl;
	var w = parseInt(hidden.getAttribute("W"), 10);
	var h = parseInt(hidden.getAttribute("H"), 10);
	var url = hidden.getAttribute("SURL")
	var oid = parseInt(hidden.value, 10);
	if(!isNaN(oid)) {
		sourceUrl = imageProviderUrl + "?image=" + oid + "&ew=" + w + "&eh=" + h;
	}
	var label = hidden.getAttribute("SLABEL");
	var size = hidden.getAttribute("SSIZE");
	if(size != null) {
		size = Math.floor(parseInt(size, 10) / 1000);
	}
	if(isResource) {
		if(sourceUrl != null && sourceUrl != "" && sourceUrl.substr(sourceUrl.length -1) != "/") {
			image.src ="./iso_icons/odf_button_big_document.png";
			resizeImagePreview(image, w, h, 75, 100);
			image.className = "previewImage";
			var pos = url.lastIndexOf("/");
			var filename = url.substr(pos+1);
			var title = "Fichier: " + filename + "\n";
			if(label != "") title +=  "Titre: " + label + "\n";
			if(size != 0) title +=  "Taille: " + size + " ko";
			para.innerHTML = label + " --- " + filename;
			image.title = title;
		} else {
			image.src = "./iso_icons/empty.gif";
			image.style.width = "0px";
			image.style.height = "0px";
			image.className = "missingPreviewImage";
			var title = "Pas de document";
			image.title = title;
			para.innerHTML = title;
		}
	
	} else {
		var swidth = parseInt(hidden.getAttribute("SWIDTH"), 10);
		var sheight = parseInt(hidden.getAttribute("SHEIGHT"), 10);
		if(sourceUrl != null && sourceUrl != "" && sourceUrl.substr(sourceUrl.length -1) != "/") {
			image.src = sourceUrl;
			image.className = "previewImage";
			var pos = url.lastIndexOf("/");
			var filename = url.substr(pos+1);
			resizeImagePreview(image, w, h, swidth, sheight);
			var title = "Fichier: " + filename + "\n";
			if(label != "") title +=  "Titre: " + label + "\n";
			if(size != 0) title +=  "Taille: " + size + " ko";
			image.title = title;
			para.innerHTML = label + " --- " + filename;;
		} else {
			image.src = "./iso_icons/empty.gif";
			image.style.width = "0px";
			image.style.height = "0px";
			image.className = "missingPreviewImage";
			var title = "Pas d'image";
			image.title = title;
			para.innerHTML = title;
		}
	}
	
}


function removeImage(id)
{
	var hidden = document.getElementById(id);
	var image = document.getElementById(id+"_i");
	var para = document.getElementById(id+"_p");
	if(hidden == null || image == null) return;
	var w = parseInt(hidden.getAttribute("W"), 10);
	var h = parseInt(hidden.getAttribute("H"), 10);
	hidden.value = "";
	image.src = "./iso_icons/empty.gif";
	image.style.width = "0px";
	image.style.height = "0px";
	image.className = "missingPreviewImage";
	var title = "Pas d'image";
	image.title = title;
	para.innerHTML = title;
}

var albumCurrentControlId = null;

/*
function processSelectedImageForInput(descr)
{
	var id = albumCurrentControlId;
	var hidden = document.getElementById(id);
	var image = document.getElementById(id+"_i");
	var para = document.getElementById(id+"_p");
	if(hidden == null || image == null) return;
	hidden.value = descr.oid;
	var swidth = descr.width;
	var sheight = descr.height;
	var w = parseInt(hidden.getAttribute("W"), 10);
	var h = parseInt(hidden.getAttribute("H"), 10);
	var title = "Fichier: " + descr.filename + descr.extension + "\n";
	if(descr.label != "") title +=  "Titre: " + descr.label + "\n";
	if(descr.size != 0) title +=  "Taille: " + descr.size + " ko";
	if(swidth != "") {
		var sourceUrl = imageProviderUrl + "?image=" + descr.oid + "&ew=" + w + "&eh=" + h;
		image.src = sourceUrl;
		image.className = "previewImage";
		resizeImagePreview(image, w, h, swidth, sheight);
		title += "\nTaille: " + swidth + " x " + sheight;
	} else {
		image.width = 75;
		image.height = 100;
		resizeImagePreview(image, w, h, 75, 100);
		image.src = "./iso_icons/odf_button_big_document.png";
		image.className = "previewImage";
	}
	image.title = title;
	para.innerHTML = descr.label + " --- " + descr.filename + descr.extension;
}
*/
function AlbumDialogClientForAlbumControl(id, oid)
{
	this.id = id;
	this.oid = oid;
	this.descr = null;
}

AlbumDialogClientForAlbumControl.prototype.getOid = function()
{
	return this.oid;
}

AlbumDialogClientForAlbumControl.prototype.setDescription = function(descr)
{
	this.descr = descr;
}

AlbumDialogClientForAlbumControl.prototype.commit = function()
{
	var id = this.id;
	var descr = this.descr;
	var hidden = document.getElementById(id);
	var image = document.getElementById(id+"_i");
	var para = document.getElementById(id+"_p");
	if(hidden == null || image == null) return;
	hidden.value = descr.oid;
	var swidth = descr.width;
	var sheight = descr.height;
	var w = parseInt(hidden.getAttribute("W"), 10);
	var h = parseInt(hidden.getAttribute("H"), 10);
	var title = "Fichier: " + descr.filename + descr.extension + "\n";
	if(descr.label != "") title +=  "Titre: " + descr.label + "\n";
	if(descr.size != 0) title +=  "Taille: " + descr.size + " ko";
	if(swidth != "") {
		var sourceUrl = imageProviderUrl + "?image=" + descr.oid + "&ew=" + w + "&eh=" + h;
		image.src = sourceUrl;
		image.className = "previewImage";
		resizeImagePreview(image, w, h, swidth, sheight);
		title += "\nTaille: " + swidth + " x " + sheight;
	} else {
		image.width = 75;
		image.height = 100;
		resizeImagePreview(image, w, h, 75, 100);
		image.src = "./iso_icons/odf_button_big_document.png";
		image.className = "previewImage";
	}
	image.title = title;
	para.innerHTML = descr.label + " --- " + descr.filename + descr.extension;
}

function browseImage(id, url)
{
	albumCurrentControlId = id;
	var hiddenInput = document.getElementById(id);
	var oid = hiddenInput.value;
	var width = "800";
	var height = "600";
	window.albumDialogClient = new AlbumDialogClientForAlbumControl(id, oid);
	showModalWindow(url, "albumDialog", width, height, window.albumDialogClient);
}

function ReferenceSelectionProcessor(id)
{
	this._id = id
}

ReferenceSelectionProcessor.prototype.commit = function(descr)
{
	var id = this._id;
	var div = document.getElementById(id + "_l");
	var hidden = document.getElementById(id);
	if(hidden == null) return alert("no hidden ");
	if(descr != null) {
		hidden.value = "" + descr.oid;
		div.innerHTML = descr.label;
	} else {
		hidden.value = "";
		div.innerHTML = "";
	}

}

ReferenceSelectionProcessor.prototype.select = function(descr)
{
}

function browseReference(id, url)
{
	var hidden = document.getElementById(id);
	if(hidden == null) return;
	var width = "600";
	var height = "300";
	window.referenceSelectionProcessor = new ReferenceSelectionProcessor(id);
	var oid = hidden.value;
	if(oid != "") {
		url = url + "&oid=" + oid;
	}
	showModalWindow(url, "referenceDialog", width, height, window.referenceSelectionProcessor);
}



function PrivateResourceControl(id, name)
{
	this._id = id;
	this._name = name;
}



PrivateResourceControl.prototype.getName = function()
{
	return this._name;
}

PrivateResourceControl.prototype.getStringValue = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return;
	}
	return input.value;
}

PrivateResourceControl.prototype.isChecked = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return;
	}
	return input.checked;
}

PrivateResourceControl.prototype.isDisabled = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return;
	}
	return input.disabled;
}

PrivateResourceControl.prototype.isReadOnly = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return;
	}
	return input.readOnly;
}

PrivateResourceControl.prototype.checkValue = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return true;
	}
	if(input.value != "") {
		return true;
	}
	var input = document.getElementById(this._id + "_f");
	if(input == null) {
		return true;
	}
	if(input.value != "") {
		return true;
	}
	return false;
}

PrivateResourceControl.prototype.checkTitleValue = function()
{
	var input = document.getElementById(this._id + "_l");
	if(input == null) {
		return true;
	}
	if(input.value != "") {
		return true;
	}
	return false;
}
PrivateResourceControl.prototype.checkDescriptionValue = function()
{
	var input = document.getElementById(this._id + "_d");
	if(input == null) {
		return true;
	}
	if(input.value != "") {
		return true;
	}
	return false;
}

PrivateResourceControl.prototype.onSubmit = function(formManager)
{
	var status = true;
	this._reason = "";
	var failurePara = document.getElementById(this._id + "_failure");
	var failureContent = document.getElementById(this._id + "_failure_content");
	if(!this.checkValue()) {
		if(failurePara != null) {
			var message = failureContent.innerHTML;
			this._reason = message;
			failurePara.style.display = "block";
			status = false;
		}
	} else {
		if(failurePara != null) failurePara.style.display = "none";
		var failurePara = document.getElementById(this._id + "_failure_l");
		if(!this.checkTitleValue()) {
			if(failurePara != null) {
				var message = failurePara.innerHTML;
				if(this._reason != "") this._reason += "; ";
				this._reason += message;
				failurePara.style.display = "block";
				status = false;
			}
		} else {
			if(failurePara != null) failurePara.style.display = "none";
		}
		var failurePara = document.getElementById(this._id + "_failure_d");
		if(!this.checkDescriptionValue()) {
			var failurePara = document.getElementById(this._id + "_failure_d");
			if(failurePara != null) {
				var message = failurePara.innerHTML;
				if(this._reason != "") this._reason += "; ";
				this._reason += message;
				failurePara.style.display = "block";
				status = false;
			}
		} else {
			if(failurePara != null) failurePara.style.display = "none";
		}
	}
	return status;
}

PrivateResourceControl.prototype.getReason = function()
{
	return this._reason;
}



function DecimalControl(id, name)
{
	this._id = id;
	this._name = name;
}

DecimalControl.prototype.getName = function()
{
	return this._name;
}

DecimalControl.prototype.getStringValue = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return;
	}
	return input.value;
}

DecimalControl.prototype.isChecked = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return;
	}
	return input.checked;
}

DecimalControl.prototype.isDisabled = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return;
	}
	return input.disabled;
}

DecimalControl.prototype.isReadOnly = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return;
	}
	return input.readOnly;
}

DecimalControl.prototype.checkValue = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return true;
	}
	if(input.value != "") {
		return true;
	}
	return false;
}

DecimalControl.prototype.checkFormatValue = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return false;
	}
	var value = input.value;
	if(value == "") return true;
	if(isNaN(parseFloat(value))) {
		return false;
	}
	return true;
}

DecimalControl.prototype.onSubmit = function(formManager)
{
	var failurePara = document.getElementById(this._id + "_failure");
	var failureContent = document.getElementById(this._id + "_failure_content");
	if(failurePara != null) {
		var message = failureContent.innerHTML;
		if(!this.checkValue()) {
			this._reason = message;
			failurePara.style.display = "block";
			return false;
		}
		failurePara.style.display = "none";
	}
	var failureFormatPara = document.getElementById(this._id + "_failure_format");
	if(failureFormatPara != null) {
		var message = failureFormatPara.innerHTML;
		if(!this.checkFormatValue()) {
			this._reason = message;
			failureFormatPara.style.display = "block";
			return false;
		}
		failureFormatPara.style.display = "none";
	}
	return true;
}

DecimalControl.prototype.getReason = function()
{
	return this._reason;
}

//var IntegerControl = DecimalControl;

function IntegerControl(id, name)
{
	this._id = id;
	this._name = name;
}
IntegerControl.prototype.getName = function()
{
	return this._name;
}

IntegerControl.prototype.getStringValue = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return;
	}
	return input.value;
}

IntegerControl.prototype.isChecked = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return;
	}
	return input.checked;
}

IntegerControl.prototype.isDisabled = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return;
	}
	return input.disabled;
}

IntegerControl.prototype.isReadOnly = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return;
	}
	return input.readOnly;
}

IntegerControl.prototype.checkValue = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return true;
	}
	if(input.value != "") {
		return true;
	}
	return false;
}

IntegerControl.prototype.checkFormatValue = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return false;
	}
	var value = input.value;
	if(value == "") return true;
	var v = parseInt(value, 10);
	if(isNaN(parseInt(value, 10)) || value + "" != v + "") {
		return false;
	}
	return true;
}

IntegerControl.prototype.onSubmit = function(formManager)
{
	var failurePara = document.getElementById(this._id + "_failure");
	var failureContent = document.getElementById(this._id + "_failure_content");
	if(failurePara != null) {
		var message = failureContent.innerHTML;
		if(!this.checkValue()) {
			this._reason = message;
			failurePara.style.display = "block";
			return false;
		}
		failurePara.style.display = "none";
	}
	var failureFormatPara = document.getElementById(this._id + "_failure_format");
	if(failureFormatPara != null) {
		var message = failureFormatPara.innerHTML;
		if(!this.checkFormatValue()) {
			this._reason = message;
			failureFormatPara.style.display = "block";
			return false;
		}
		failureFormatPara.style.display = "none";
	}
	return true;
}

IntegerControl.prototype.getReason = function()
{
	return this._reason;
}

function GenericControl(id, name)
{
	this._id = id;
	this._name = name;
}

GenericControl.prototype.getName = function()
{
	return this._name;
}

GenericControl.prototype.getStringValue = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return;
	}
	return input.value;
}

GenericControl.prototype.isChecked = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return;
	}
	return input.checked;
}

GenericControl.prototype.isDisabled = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return;
	}
	return input.disabled;
}

GenericControl.prototype.isReadOnly = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return;
	}
	return input.readOnly;
}

GenericControl.prototype.checkValue = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return true;
	}
	if(input.value != "") {
		return true;
	}
	return false;
}

GenericControl.prototype.onSubmit = function(formManager)
{
	var failurePara = document.getElementById(this._id + "_failure");
	var failureContent = document.getElementById(this._id + "_failure_content");
	if(failurePara == null) return true;
	var message = failureContent.innerHTML;
	if(this.checkValue()) {
		failurePara.style.display = "none";
		return true;
	}
	this._reason = message;
	failurePara.style.display = "block";
	return false;
}

GenericControl.prototype.getReason = function()
{
	return this._reason;
}

function PropertySetControl(id, formManager, name)
{
	this._id = id;
	this._formId = formManager.getHtmlId()
	this._name = name;
	var div = document.getElementById(this._id);
	var value = div.getAttribute("V");
	var form = document.getElementById(this._formId);
	if(form == null) {
		return alert("no form");
	}
	var inputs = form[name];
	if(inputs == null) return;
	var values = new Object();
	var list = value.split(" ");
	for(var i=0;i<list.length;i++) values[list[i]] = 1;
	if(typeof(inputs.length) == "number") {
		for(var i=0;i<inputs.length;i++) {
			var input = inputs[i];
			if(values[input.value] == 1) {
				input.checked = true;
			}
		}
	} else {
		if(values[inputs.value] == 1) {
			inputs.checked = true;
		}
	}
}


PropertySetControl.prototype.getInputs = function()
{
	var form = document.getElementById(this._formId);
	if(form == null) {
		return;
	}
	return form[this._name];
}

PropertySetControl.prototype.getName = function()
{
	return this._name;
}

PropertySetControl.prototype.getStringValue = function()
{
	var input = this.getInputs();
	if(input == null) {
		return;
	}
	if(typeof(input.length) == "number") {
		var res = "";
		for(var i=0;i<input.length;i++) {
			if(input[i].checked) {
				if(res != "") res += " ";
				res += input.value;
			}
		}
		return res;
	}
	return input.value;
}

PropertySetControl.prototype.isChecked = function()
{
	var input = this.getInputs();
	if(input == null) {
		return;
	}
	if(typeof(input.length) == "number") {
		for(var i=0;i<input.length;i++) {
			if(input[i].checked) return true;
		}
		return false;
	}
	return input.checked;
}

PropertySetControl.prototype.isDisabled = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return;
	}
	if(typeof(input.length) == "number") {
		return input[0].disabled;
	}
	return input.disabled;
}

PropertySetControl.prototype.isReadOnly = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return;
	}
	if(typeof(input.length) == "number") {
		return input[0].readOnly;
	}
	return input.readOnly;
}

PropertySetControl.prototype.checkValue = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return true;
	}
	if(input.value != "") {
		return true;
	}
	return false;
}

PropertySetControl.prototype.onSubmit = function(formManager)
{
	var failurePara = document.getElementById(this._id + "_failure");
	var failureContent = document.getElementById(this._id + "_failure_content");
	if(failurePara == null) return true;
	var message = failureContent.innerHTML;
	if(this.checkValue()) {
		failurePara.style.display = "none";
		return true;
	}
	this._reason = message;
	failurePara.style.display = "block";
	return false;
}

PropertySetControl.prototype.getReason = function()
{
	return this._reason;
}

function RadioControl(id, formManager, name)
{
	this._id = id;
	this._formId = formManager.getHtmlId();
	this._name = name;
	var div = document.getElementById(this._id);
	var value = div.getAttribute("V");
	var inputs = this.getInputs();
	if(inputs == null) return;
	if(typeof(inputs.length) == "number") {
		var done = false;
		var defaultInput = null;
		for(var i=0;i<inputs.length;i++) {
			var input = inputs[i];
			if(input.value == value) {
				input.checked = true;
				done = true;
				break;
			}
			if(defaultInput != null && input.getAttribute("DEFAULT") == "true") {
				defaultInput = input;
			}
		}
		if(!done && defaultInput != null) {
			defaultInput.checked = true;
		}	
	} else {
		if(inputs.value == value) {
			inputs.checked = true;
		} else if(inputs.getAttribute("DEFAULT") == "true") {
			inputs.checked = true;
		}
	}
}


RadioControl.prototype.getName = function()
{
	return this._name;
}

RadioControl.prototype.getInputs = function()
{
	var form = document.getElementById(this._formId);
	if(form == null) {
		return;
	}
	return form[this._name];
}

RadioControl.prototype.getStringValue = function()
{
	var input = this.getInputs();
	if(input == null) return;
	if(typeof(input.length) == "number") {
		for(var i=0;i<input.length;i++) {
			if(input[i].checked) return input[i].value;
		}
	} else {
		return input.value;
	}
}

RadioControl.prototype.isChecked = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return;
	}
	if(typeof(input.length) == "number") {
		for(var i=0;i<input.length;i++) {
			if(input[i].checked) return true;
		}
		return false;
	}
	return input.checked;
}

RadioControl.prototype.isDisabled = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return;
	}
	if(typeof(input.length) == "number") {
		return input[0].disabled;
	}
	return input.disabled;
}

RadioControl.prototype.isReadOnly = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return;
	}
	if(typeof(input.length) == "number") {
		return input[0].readOnly;
	}
	return input.readOnly;
}

RadioControl.prototype.checkValue = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return true;
	}
	if(input.value != "") {
		return true;
	}
	return false;
}

RadioControl.prototype.onSubmit = function(formManager)
{
	var failurePara = document.getElementById(this._id + "_failure");
	var failureContent = document.getElementById(this._id + "_failure_content");
	if(failurePara == null) return true;
	var message = failureContent.innerHTML;
	if(this.checkValue()) {
		failurePara.style.display = "none";
		return true;
	}
	this._reason = message;
	failurePara.style.display = "block";
	return false;
}

RadioControl.prototype.getReason = function()
{
	return this._reason;
}

function CheckboxControl(id, formManager, name)
{
	this._id = id;
	this._formId = formManager.getHtmlId();
	this._name = name;
	updateCheckBox(id, true);
}


CheckboxControl.prototype.getName = function()
{
	return this._name;
}

CheckboxControl.prototype.getInputs = function()
{
	var form = document.getElementById(this._formId);
	if(form == null) {
		return;
	}
	return form[this._name];
}

CheckboxControl.prototype.getStringValue = function()
{
	var input = this.getInputs();
	if(input == null) return;
	if(typeof(input.length) == "number") {
		var value = "";
		for(var i=0;i<input.length;i++) {
			if(input[i].checked) {
				if(value != "") value += " ";
				value += input[i].value;
			}
		}
		return value;
	} else {
		return input.value;
	}
}

CheckboxControl.prototype.isChecked = function()
{
	var input = document.getElementById(this._id+"_c");
	if(input == null) {
		return false;
	}
	if(typeof(input.length) == "number") {
		for(var i=0;i<input.length;i++) {
			if(input[i].checked) return true;
		}
		return false;
	}
	return input.checked;
}

CheckboxControl.prototype.isDisabled = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return;
	}
	if(typeof(input.length) == "number") {
		return input[0].disabled;
	}
	return input.disabled;
}

CheckboxControl.prototype.isReadOnly = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return;
	}
	if(typeof(input.length) == "number") {
		return input[0].readOnly;
	}
	return input.readOnly;
}

CheckboxControl.prototype.checkValue = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return false;
	}
	if(typeof(input.length) == "number") {
		for(var i=0;i<input.length;i++) {
			if(input[i].value == "true") return true;
		}
		return false;
	}
	return input.value == "true";
}

CheckboxControl.prototype.onSubmit = function(formManager)
{
	var failurePara = document.getElementById(this._id + "_failure");
	var failureContent = document.getElementById(this._id + "_failure_content");
	if(failurePara == null) return true;
	var message = failureContent.innerHTML;
	if(this.checkValue()) {
		failurePara.style.display = "none";
		return true;
	}
	this._reason = message;
	failurePara.style.display = "block";
	return false;
}

CheckboxControl.prototype.getReason = function()
{
	return this._reason;
}

function ColorClient(input)
{ 
	this.input = input;
}

ColorClient.prototype.getColor = function()
{
	return this.input.value;
}

ColorClient.prototype.setColor = function(str)
{
	this.input.value = str;
	updateColor(this.input.id, false);
}

function showColorDialog(id)
{
	var input = document.getElementById(id);
	window.colorClient = new ColorClient(input);
	showModalWindow("iso_scripts/colorDialog.html", "_blank", 233, 301, null);
}

function updateColor(id, init)
{
	var input = document.getElementById(id);
	var image = document.getElementById(id + "_image");
	var color = input.value;
	if(/^#[0-9A-F]{6}$/i.test(color)) {
		image.style.backgroundColor = color;
		input.style.backgroundColor = "white";
	} else {
		image.style.backgroundColor = "transparent";
		if(color == "") {
			input.style.backgroundColor = "white";
		} else {
			input.style.backgroundColor = "#FFC0C0";
		}
	}
}

function FormManager(id, htmlId, windowOptions, target)
{
	this._id = id;
	this._htmlId = htmlId;
	this._windowOptions = windowOptions;
	this._target = target;
	this._controls = new Array();
	this._bByPassStdChecking = false;
}
FormManager.prototype.getId = function()
{
	return this._id;
}

FormManager.prototype.getHtmlId = function()
{
	return this._htmlId;
}

FormManager.prototype.registerControl = function(control)
{
	this._controls[this._controls.length] = control;
}

FormManager.prototype.getControls = function(name)
{
	var list = new Array();
	for(var i=0;i<this._controls.length;i++) {
		control = this._controls[i];
		if(control.getName() == name) list[list.length] = control;
	}
	return list;
}

FormManager.prototype.byPassStandardChecking = function( bByPass )
{
	if ( bByPass == null ) 	var bByPass = true;
	this._bByPassStdChecking = bByPass;
}

FormManager.prototype.onSubmit = function()
{
	var reasons = new Array();
	var firstFalseControl = null;
	var customStatus = true;
	if(this.onCustomCheck) {
		var status = this.onCustomCheck();
		switch(typeof(status)) {
		case "string":
			reasons[reasons.length] = status;
			customStatus = false;
			break;
		case "boolean":
			customStatus = status;
			if ( this._bByPassStdChecking == true )
				return customStatus;
			break;
		}
	}
	if (this._bByPassStdChecking == false) {
		for(var i=0;i<this._controls.length;i++) {
			var control = this._controls[i];
			if(control.onSubmit == null) continue;
			var status = control.onSubmit(this);
			if(status == false) {
				if(firstFalseControl == null && control.focus != null) firstFalseControl = control;
				var reason = control.getReason(this);
				reasons[reasons.length] =  reason;
			}
		}
	}
	switch(reasons.length) {
	case 0:
		var target = this._target;
		if(this._windowOptions != null) {
			window.open("about:blank", target, this._windowOptions);
		}
		return customStatus;	// Custom status can be false
	case 1:
		alert(reasons[0]);
		if(firstFalseControl != null) firstFalseControl.focus();
		return false;
	default:
		var msg = "Le formulaire ne peut être envoyé pour les raisons suivantes:\n";
		for(var i=0;i<reasons.length;i++) {
			msg += "-\240" + reasons[i] + "\n";
		}
		alert(msg);
		if(firstFalseControl != null) firstFalseControl.focus();
		return false;
	}
}

function opennewwindow(linkElement, options, target)
{
	var href = linkElement.href;
	if(href == null || href == "") return false;
	var win = window.open(href, target, options);
	return false;
}

function tabClick(a)
{
	var srcLi = a.parentNode;
	var ul = srcLi.parentNode;
	for(var i=0;i < ul.childNodes.length;i++) {
		var li = ul.childNodes.item(i);
		if(li.nodeName != "LI") continue;
		var id = li.id;
		var tabBody = document.getElementById("tab" + id);
		if(li.tagName != "LI" || tabBody == null) {
			alert("no li " + li.tagName + " " + id);
			continue;
		}
		if(li == srcLi) {
			tabBody.className = "tabBodySelected";
			li.className = "tabButtonSelected";
		} else {
			tabBody.className = "tabBody";
			li.className = "tabButton";
		}
	}
	return false;
}