//Initialise an array of controls, and set the save button to be active or inactive
function oControlSet(button, buttonActive, path) {	
	this.controls=new Array();
	this.subsidiaryControls=new Array();
	this.controlNames=new Object();
	this.button=button;
	if (button) button.disabled=!buttonActive;
	this.path=path;
	this.classChange='';
	this.showHelp=true;
	this.hiddenArguments='';
	this.helpMinimumWidth='';
	//this.helpMinimumWidth=undefined;
	this.disabled=false;}

//Add a new control to the set
oControlSet.prototype.add=function(control,label,help) {
	//if (control==null) return;
	if (this.initialFocus==undefined) this.initialFocus=control.id;
	control.controlSet=this;
	control.index=this.controls.length;
	control.help=help;
	control.warning==undefined;
	control.labelTd=document.getElementById('csLabelTd_' + control.id);
	control.controlTd=document.getElementById('csControlTd_' + control.id);
	control.validator=new oValidator();
	control.label=label;
	if (control.label!=undefined) control.label.className='csLabel';
	control.labelTd.control=control;
	control.labelTd.innerHTML=label.replace(/ /g,'&nbsp;');
	control.labelTd.onmouseover=function() {this.control.controlSet.select(this.control,true);};	
	control.controlTd.control=control;
	control.controlTd.onmouseover=function() {this.control.controlSet.select(this.control,true);};
	control.onkeyup=function(e) {
		if (!e) e=event;
		if (e.keyCode!=9 && e.keyCode!=13) this.controlSet.activateButton(this);};	
	//control.onblur=function() {this.controlSet.validateControl(this);}
	control.onmouseover=function() {this.controlSet.select(this,true);};
	if (this.button) {
		if (control.type=='checkbox') control.onclick=function() {this.controlSet.activateButton(this);};
		else control.onchange=function() {this.controlSet.activateButton(this);}}
	control.onfocus=function() {this.controlSet.select(this,true);};	
	this.controls[this.controls.length]=control;
	this.controlNames[control.id]=control;}

oControlSet.setKeyupFunction=function(control,keyupFunction) {
	control.onkeyup=function(e) {
		if (!e) e=event;
		if (e.keyCode!=9 && e.keyCode!=13) keyupFunction();};}
				
oControlSet.prototype.activateButton=function(control) {
	this.dynamicValidation(control);
	if (this.button && this.button.disabled) this.button.disabled=false;}

oControlSet.prototype.select=function(control,byControl) {
	if (!this.showHelp || this.disabled) return;
	var isMessage=false;
	if (this.selected==control) return;
	this.selected=control;
	//if (this.selected==control.index) return;
	//this.selected=control.index;
	isMessage=this.controls[control.index].help!=undefined || this.controls[control.index].warning!=undefined;
	for (var i=0;i<this.controls.length;i++) {
		if (i==control.index) {
			control.labelTd.className='csSelectedLeft' + this.classChange + (control.warning==undefined?'':'Warning');
			control.controlTd.className=(isMessage?'csSelectedMiddle':'csSelectedRight') + this.classChange;
			if (!byControl) try {
				control.focus();
				control.focus();} catch(e) {}
			//if (!byControl) setTimeout('get(\'' + control.id + '\').focus();',10);
			}
		else {
			this.controls[i].labelTd.className='csNormalLeft' + this.classChange + (this.controls[i].warning==undefined?'':'Warning');
			this.controls[i].controlTd.className=(isMessage?'csNormalRightMessage':'csNormalRight') + this.classChange;}}
	this.setHelpImage(control.warning==undefined);
	if (control.warning==undefined) {
		//alert(this.helpMinimumWidth);
		document.getElementById('csHelpContent').innerHTML=control.help + this.helpMinimumWidth;// +
			(this.helpWidth?'<br><img src="/com/ufltek/core/gui/images/spacers/' + this.helpWidth + '.gif">':'');
		//document.getElementById('csHelpContent').innerHTML=this.getHelpMinimumWidthText(control.help);
		document.getElementById('csHelpTitle').innerHTML=control.label.replace(/ /g,'&nbsp;');}
	else {
		document.getElementById('csHelpContent').innerHTML=this.getWarning(control.warning) + this.helpMinimumWidth;
		//document.getElementById('csHelpContent').innerHTML=this.getHelpMinimumWidthText(this.getWarning(control.warning));
		document.getElementById('csHelpTitle').innerHTML=this.getWarning(control.label.replace(/ /g,'&nbsp;'));}}

oControlSet.prototype.getUrlArguments=function() {
	var arguments='',name;
	for (var i=0;i<this.controls.length;i++) {
		name=this.controls[i].name==''?this.controls[i].id:this.controls[i].name;
		if (this.controls[i].type=='checkbox') {
			if (this.controls[i].checked) arguments+='&' + name + '=';}
		else arguments+='&' + name + '=' + encodeURIComponent(this.controls[i].value);}
	return arguments + this.hiddenArguments;}
	
oControlSet.prototype.validate=function() {
	var controlToSelect;
	if (this.button) this.button.disabled=true;
	for (var i=0;i<this.controls.length;i++) {
		var warning=this.controls[i].validator.validate(this.controls[i].value);
		if (warning==undefined) {
			if (this.controls[i].warning!=undefined) {
				this.controls[i].warning=undefined;}}
		else {
			this.controls[i].warning=warning;
			if (controlToSelect==undefined) {
				controlToSelect=this.controls[i];}}}
	if (controlToSelect==undefined) controlToSelect=this.additionalValidation(controlToSelect);
	if (controlToSelect!=undefined) {
		this.selected=undefined;
		this.select(controlToSelect);}
	else this.refreshSelection();
	return controlToSelect==undefined;}

oControlSet.prototype.additionalValidation=function() {
	return undefined;}

oControlSet.prototype.dynamicValidation=function() {
	return;}

/* Maybe implement in the future
oControlSet.prototype.validateControl=function(control) {
	var warning=control.validator.validate(control.value);
	if (warning==undefined) {
		if (control.warning!=undefined) control.warning=undefined;}
	else {
		control.warning=warning;
		control.labelTd.className='csSelectedLeft' + this.classChange + 'Warning';
		return true;}
	return false;}
*/

oControlSet.prototype.disable=function(disable) {
	this.disableControls(disable);
	this.disabled=disable;}

oControlSet.prototype.disableControls=function(disable) {
	for (var i=0;i<this.controls.length;i++) {
		this.controls[i].disabled=disable;}
	for (var i=0;i<this.subsidiaryControls.length;i++) {
		this.subsidiaryControls[i].disabled=disable;}}

oControlSet.prototype.refreshSelection=function() {
	var controlToSelect=this.selected;
	this.selected=undefined;
	this.select(controlToSelect);}
	
oControlSet.prototype.serverValidate=function(name,warning) {
	if (warning!=undefined) this.controlNames[name].warning=this.getWarning(warning);
	//this.selected=-1;
	this.selected=undefined;
	if (name==undefined) this.select(this.controls[0],true);
	else this.select(this.controlNames[name],false);}
	
oControlSet.prototype.getWarning=function(warning) {
	return '<span class=\"csWarning\">' + warning + '</span>';}

oControlSet.prototype.setHelpImage=function(information) {	
	if (information && document.getElementById('csHelpImage').imageType!='information') {
		document.getElementById('csHelpImage').imageType='information';
		document.getElementById('csHelpImage').innerHTML='<img src="' + this.path + '../images/information.gif">';}
	else if (!information && document.getElementById('csHelpImage').imageType!='warning') {
		document.getElementById('csHelpImage').imageType='warning';
		document.getElementById('csHelpImage').innerHTML='<img src="' + this.path + '../images/warning.gif">';}}
		
oControlSet.prototype.setHelpMinimumWidth=function(length) {
	//this.helpMinimumWidth=length;
	this.helpMinimumWidth='<br>';
	for (var i=0;i<length;i++) this.helpMinimumWidth+='&nbsp;';
	}

oControlSet.prototype.getHelpMinimumWidthText=function(text) {
	//if (text.length<=this.helpMinimumWidth) return text;
	//alert(text);
	var i=0,html=text.substring(0,this.helpMinimumWidth),remainder=text.substring(this.helpMinimumWidth);
	//alert(html + '\n\n' + remainder);
	while (remainder.length>0 && i<3) {
		i++;
		if (remainder.indexOf(' ')>-1) {
			html+=remainder.substring(0,remainder.indexOf(' ')) + '<br>';
			remainder=remainder.substring(remainder.indexOf(' ')+1);
			html+=remainder.substring(0,this.helpMinimumWidth);
			remainder=remainder.substring(this.helpMinimumWidth);
			}
		else return html+remainder;
		//alert(html + '\n\n' + remainder);
		}
	return html;}