/******************************************************************************
* odfForm.js
*******************************************************************************
Gestion de itl:form
*******************************************************************************
*                                                                             *
* Copyright 2006									                          *
*                                                                             *
******************************************************************************/


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 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 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.decimalRegexp = /^[-+]?(([0-9]+\.[0-9]*)|([0-9]*\.[0-9]+)|([0-9]+))([eE][-+]?[0-9]+)?$/;

DecimalControl.prototype.checkFormatValue = function()
{
	var input = document.getElementById(this._id);
	if(input == null) {
		return false;
	}
	var value = input.value;
	if(value == "") return true;
	return this.decimalRegexp.test(value);
}

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 " + this._formId);
	}
	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 inputs = this.getInputs();
	if (inputs == null) {
		return true;
	}
	if (typeof(inputs.length) == "number")
	{
		for (var i=0;i<inputs.length;i++)
		{
			var input = inputs[i];
			if (input.type != "checkbox") continue;	// skip hidden input
			if (input.checked) return true;
		}
		return false;
	}
	return false;	// the only one is the hidden input (there are no options)
}

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()
{
	return this.isChecked();
}

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 = objThesaurus.translate("odf20");
		for(var i=0;i<reasons.length;i++) {
			msg += "-\240" + reasons[i] + "\n";
		}
		alert(msg);
		if(firstFalseControl != null) firstFalseControl.focus();
		return false;
	}
}
