
if (Object.isUndefined(Proto)) { var Proto = { } }

Proto.form = Class.create({
	initialize: function() {
		var e = Prototype.emptyFunction;
		this.ie = Prototype.Browser.IE;
		this.options = Object.extend({
			objectName: '',
			TabInputId: Array(),
			tabRelatedDiv: Array(),
			status: true,
			enabledClass:'InputEnabled',
			disabledClass:'InputDisabled',
			relatedAction:'_actions',
			bEnabled:false,
			originalValues:Array(),
		}, arguments[0] || { });
		return this;
	},
	toogle: function() {
		//alert('toogle'+this.options.TabInputId.length);
		for (var i=0; i<this.options.TabInputId.length; i++) {
			//espion('toogle'+this.options.TabInputId[i]);
			this.InputElmentChangeStatut($(this.options.TabInputId[i]),this.options.bEnabled);
		}
		
		for (var i=0; i<this.options.tabRelatedDivclassName.length; i++) {
			this.intervertDivDisplay(this.options.tabRelatedDivclassName[i]);
		}
		
		this.options.bEnabled = !this.options.bEnabled;
	},
	setStatus: function(bStatus){
		this.options.bEnabled = bStatus;
	},
	InputChangeStatut: function(TabInputId,objectName,tabRelatedDiv) {
		this.options.bEnabled = ($(objectName+"_this.options.bEnabled").value == "true");
		for (var i=0; i<TabInputId.length; i++) {
			try {
				var elmt=$(TabInputId[i]);
				InputElmentChangeStatut(elmt,this.options.bEnabled);
			} catch (e) {
				alert('InputChangeStatut TabInputId failed : '+TabInputId[i]+' n\'existe pas ');
			}
		}
		
		var lght=tabRelatedDiv.length;
		for (var i=0; i<lght; i++) {
			try {
				intervertDivDisplay(tabRelatedDiv[i]);
			} catch (e) {
				alert('InputChangeStatut tabRelatedDiv failed : '+tabRelatedDiv[i]+' n\'existe pas ');
			}
		}
		try {
			
			$(objectName+"_actions").style.display=(bEnabled) ? '' : 'none' ;
		} catch (e) {}
		return;
	},
	
	InputElmentChangeStatut: function(elmt,bEnabledLcl) {
		
		var sClassNameOld=(bEnabledLcl) ? this.options.disabledClass : this.options.enabledClass ;
		ibEnabledLcl=!bEnabledLcl;
		
		var sClassNameNew=(ibEnabledLcl) ? this.options.disabledClass : this.options.enabledClass ;
		var sReadOnly=(ibEnabledLcl)  ? 'readOnly' : 'false' ;
		
		var InputType=elmt.getAttribute("type");
		
		switch (InputType) {
			case 'password' :
			case 'text' :
			case 'file' : // not working yet... (and never ?)
				if(!ibEnabledLcl) {
					elmt.removeAttribute("readOnly");
				} else {
					elmt.setAttribute("readOnly","readOnly");
				}
				elmt.className=String(elmt.className).replace(sClassNameOld, sClassNameNew);
				
			break;
			
			case 'radio' :
			case 'checkbox' :
				
				relatedDivId="text_"+elmt.id;
				relatedDivIdd="textd_"+elmt.id;
				if (!$(relatedDivIdd)) {
					if(!ibEnabledLcl) {
						elmt.show();
						$(relatedDivId).show();
					} else {
						elmt.hide();
						if (!elmt.checked) {
							$(relatedDivId).hide();
						}
					}
				} else {
					if(!ibEnabledLcl) {
						elmt.show();
						$(relatedDivId).hide();
					} else {
						elmt.hide();
						if (elmt.checked) {
							$(relatedDivId).show();
						}
					}
				}
			break;
			
			case 'textarea' :   // IE Like
			case 'select-one' : // IE Like
			case 'select-multiple' : // IE Like
			case 'null' :         // Gecko Like
			case null :         // Gecko Like
				// affiche ou pas l'element
				if(!ibEnabledLcl) {
					elmt.show();
				} else {
					elmt.hide();
				}
				
				if (ibEnabledLcl) {
					try {         // select
						if (InputType=='select-one' || InputType=='select-multiple' || InputType==null) {
						   if (elmt.multiple || InputType=='select-multiple') {
								
								var text = '';
								// recopiage du contenu d'un multiple dans le text_ si class="JsToText"
								if (elmt.className.indexOf("JsToText",0)>0) {
									var lght=elmt.length;
									for(var i=0; i<lght; i++) {
										text=text+elmt.options[i].text+'<br />';
									}
									$("text_"+elmt.id).innerHTML=text;
								}
	
							} else {
								var text=elmt.options[elmt.selectedIndex].text;
							}
							$("text_"+elmt.id).innerHTML=text;
						} else {
							var text=elmt.value;
						}
						
					} catch (e) { // textarea
						var text=elmt.value;
						text=String(text).replace(/\n/g,'<br/>');
					}
					
					try {
						$("text_"+elmt.id).innerHTML=text;
					} catch (e) {
						//alert("text_"+elmt.id+' failed to innerHTML (void ?)');
					}
				} else {
					//alert(elmt.id);
					if ($("text_"+elmt.id)) {
						$("text_"+elmt.id).innerHTML='';
						
					}
				}
			break;
		}
		
		this.InputElmentSearchRelated(elmt.id,bEnabledLcl);
		return false;
	},
	intervertDivDisplay: function(div_id) {
		if ($(div_id).style.display=='none') {
			showDiv(div_id);
		} else {
			hideDiv(div_id);
		}
	},
	InputElmentSearchRelated: function(elmt_id,bEnabledLcl) {
		var tabRelated=Array("trigger_", "autoc_", "quick_search_");
		var lght=tabRelated.length;
		
		for (var i=0; i<lght; i++) {
			if ($(tabRelated[i]+''+elmt_id)) {
				try {
					var elmt=$(tabRelated[i]+''+elmt_id);
					// pour les autoc on regarde lequel afficher ...
					if (tabRelated[i] != "autoc_") {
						if (bEnabledLcl) {
							$(tabRelated[i]+''+elmt_id).show();
						} else {
							(tabRelated[i]+''+elmt_id).hide;
						}
						//intervertDivDisplay(tabRelated[i]+''+elmt_id);
					} else {
						AffSwitchSelected(elmt_id, tabRelated[i]+''+elmt_id, bEnabledLcl);
					}
				} catch(e) {}
				break;
			}
		}
		return false;
	},	
 	AffSwitchSelected: function(elmt_id,AutocName,bEnabledLcl) {
 		// Affiche le textinput ou le select suivant
		//	  l'etat du formulaire
		// et la préselection du select
		var elmtHandle = $(elmt_id);
		if (bEnabledLcl) {
			
			lclInputType = elmtHandle.getAttribute("type");
			
			switch (lclInputType) {
				case 'textarea' :   // IE Like
				case 'select-one' : // IE Like
				case 'select-multiple' : // IE Like
				case null :         // Gecko Like
				case 'null' :		// Gecko Like
					if (   isNaN(elmtHandle.selectedIndex)
						|| elmtHandle.selectedIndex == 0) {
						$(elmt_id).hide();
						$(AutocName).show();
					} else {
						$(elmt_id).show();
						$(AutocName).hide();
					}
					break;
				default :
					if (AutocHandle.value!='') {
						$(elmt_id).hide();
						$(AutocName).show();
					} else {
						$(elmt_id).show();
						$(AutocName).hide();
					}
			}
		} else {
			$(AutocName).hide();
		}
	},
	addRelatedElmnt: function (name) {
		this.options.tabRelatedDivclassName.push(name);
	},
	remRelatedElmnt: function (name) {
		var before = this.options.tabRelatedDivclassName.length;
		this.options.tabRelatedDivclassName = this.options.tabRelatedDivclassName.remove(name);
	},
	addInputId: function (name) {
		this.options.TabInputId.push(name);
	},
	remInputId: function (name) {
		var before = this.options.TabInputId.length;
		this.options.TabInputId = this.options.TabInputId.remove(name);
	},
	getStatus: function () {
		return this.options.bEnabled;
	},
	saveOptions: function () {
		this.options.originalValues['TabInputId'] = this.options.TabInputId.copy();
		this.options.originalValues['tabRelatedDivclassName'] = this.options.tabRelatedDivclassName.copy();
	},
	
	reset:function () {
		this.options.TabInputId = this.options.originalValues['TabInputId'].copy();
		this.options.tabRelatedDivclassName = this.options.originalValues['tabRelatedDivclassName'].copy();
	},
	test : function(msg) {
		if (!msg) {
			msg = '';
		}
		alert(msg+' '+this.options.TabInputId.length);
	}
	
	
})

var RubriqueForm = new Proto.form({TabInputId: Array("nom", "descriptionR"),
							  	  tabRelatedDivclassName: Array("RubriqueSubmit", "descriptionMaxText"),
							 	  status: false});
var DocumentForm = new Proto.form({TabInputId: Array("Nom0"),
								   tabRelatedDivclassName: Array("document0", "copyFileName0", "DocumentSubmit"),
								   status: false});
var UserForm 	 = new Proto.form({TabInputId: Array("login", "passwd", "nom", "prenom", "adresse_comp", "tel", "adresse", "code_postal", "pays", "ville", "email", "portable", "ref_id_etat"),
								   tabRelatedDivclassName: Array("UserSubmit"),
								   status: false});
var SiteForm	 = new Proto.form({TabInputId: Array("nom", "ref_id_bandeau", "ref_id_etat", "repertoire", "size_mo", "title", "url"),
								   tabRelatedDivclassName: Array("SiteSubmit"),
								   status: false});
var SiteFormClient = new Proto.form({TabInputId: Array("free_text_title", "free_text"),
									 tabRelatedDivclassName: Array("SiteSubmit", "free_textMaxText"),
									 status: false});
var SiteFormClientWeb = new Proto.form({TabInputId: Array("key_words", "description", "title"),
										tabRelatedDivclassName: Array("SiteSubmit"),
										status: false}); 
var GroupForm = new Proto.form({TabInputId: Array("nom", "description", "UserListPool", "UserListSelected"),
								tabRelatedDivclassName: Array("groupeSubmit", "moveUsers", "PoolMembres", "headMembres", "PoolRubriques", "headRubriques"),
								status: false});
GroupForm.saveOptions();										
RubriqueForm.saveOptions();				
										
										 
