/************************************************************************************************
*	Programmeur :		Daniel Rioux											*
*																		*
*	Date de céation :		13 Février 2009											*
*																		*
*	Description :		Classe de construction de contrôles input (text,radio,checkbox,password,etc...	*
************************************************************************************************/

//Classe input (pour l'entrée de données dans un formulaire)
function clsInput()
{
	//----------------Les variables internes de la classe----------------------
	
	var Err = false;	//Flag pour la gestion des erreurs.
	var ErrProperty;	//Propriété causant l'erreur;
	
	//Pour la définition des attributs de l'élément.
	
	this.setAltText = alternateText;
	var AlternateText;	//Attribut alt.
	
	this.setChange = change;
	var Change;			//Événement onChange;
	
	this.setClick = click;
	var Click;			//Événement onClick. Définit ce qui doit se produire lors d'un click sur le contrôle. (Ex.: Appel d'une fonction javascript)
	
	this.setDblClick = dblClick;
	var DblClick;		//Événement ondblClick. Définit ce qui doit se produire lors d'un double click sur le contrôle. (Ex.: Appel d'une fonction javascript)
	
	this.setClass = fieldClass;
	var FieldClass;		//Classe à laquelle le champ appartient
	
	this.setId = fieldId;
	var FieldId;		//Le ID du champ
	
	this.setName = fieldName;
	var FieldName;		//Le nom du champ pour la soumission du formulaire
	
	this.setSize = fieldSize;
	var FieldSize;		//La taille du champ
	
	this.setType = fieldType;
	var FieldType;		//Le type de champ.  (text | password | checkbox | radio | submit | button | reset | file | hidden | image)
	
	this.setValue = fieldValue;
	var FieldValue;		//La valeur du champ.
	
	this.setGetFocus = getFocus;
	var GetFocus;		//Événement onSelect.
	
	this.setImagePath = imagePath;
	var ImagePath;		//Chemin vers une image si le type du input est image
	
	this.setIsChecked = isChecked;
	var IsChecked;		//Cocher une case à cocher ou un contrôle radio. Valeur booléenne.
	
	this.setIsDisabled = isDisabled;
	var IsDisabled;		//Désactiver un contrôle. Valeur booléenne.
	
	this.setIsMap = isMap;
	var IsMap;			//Définit s'il existe une carte de zone sensible (Ex.: clique sur un endroit précis dans une image, on affiche quelque chose dans une autre fenêtre). Valeur booléenne.
	
	this.setIsReadOnly = isReadOnly;
	var IsReadOnly;		//Champ en lecture seule. Valeur booléenne.
	
	this.setKeyDown = keyDown;
	var KeyDown;		//Événement onKeydown. Quand une touche est enfoncée.
	
	this.setKeyPress = keyPress;
	var KeyPress;		//Événement onKeypress. Quand une touche est enfoncée ou maintenue enfoncée.
	
	this.setKeyUp = keyUp;
	var KeyUp;			//Événement onKeyUp. Quand une touche est relâchée.
	
	this.setLostFocus = lostFocus;
	var LostFocus;		//Quand le champs perds le focus. Evénement onBlur.
	
	this.setMapPath = mapPath;
	var MapPath;		//Attribut useMap pour les inputs de type image. Désigne le chemin vers la description des zones sensibles si elle réside côté serveur.
	
	this.setMaxLength = maxLength;
	var MaxLength;		//Nombre maximal de caractères autorisé dans un champ de saisie texte.
	
	this.setMimeType = mimeType;
	var MimeType;		//Attribut accept. Type MIME des fichiers acceptés lorsque le contrôle input est de type file.
	
	this.setMouseDown = mouseDown;
	var MouseDown;		//Événement onMousedown. Quand le bouton de la souris est enfoncé.
	
	this.setMouseMove = mouseMove;
	var MouseMove;		//Événement onMousemove. Quand le curseur de la souris bouge au-dessus du contrôle.
	
	this.setMouseOver = mouseOver;
	var MouseOver;		//Événement onMouseover. Quand le curseur de la souris se trouve au-dessus du contrôle.
	
	this.setMouseOut = mouseOut;
	var MouseOut;		//Événement onMouseout. Quand la souris quitte la zone du contrôle.
	
	this.setMouseUp = mouseUp;
	var MouseUp;		//Événement onMouseup. Quand le bouton de la souris est relâché.
	
	this.setShortCutKey = shortCutKey;
	var ShortCutKey;	//Attribut accessKey. définit un raccourci clavier afin d'atteindre un champ dans un formulaire.
	
	this.setTabIndex = tabIndex;
	var TabIndex;		//Attribut tabIndex. Définit l'ordre dans lequel le contrôle doit être accédé dans le document lors de la navigation à l'aide de la touche TAB.
	
	this.setTitle = title;
	var Title;			//Pour afficher une info bulle au dessus du contrôle.
	
	//-------------------------------------
	
	//-------------------Méthodes d'assignation des valeurs données en paramètre aux propriétés-----------------------
	
	function alternateText(StrParam)
	{
		if (typeof(StrParam) == "string")
		{
			AlternateText = StrParam;
		}
		else
		{
			Err = true;
			ErrProperty = "AlternateText";
		}
	}
	
	function change(StrParam)
	{
		if (typeof(StrParam) == "string")
		{
			Change = StrParam;
		}
		else
		{
			Err = true;
			ErrProperty = "Change";
		}
	}
	
	function click (StrParam)
	{
		if (typeof(StrParam) == "string")
		{
			Click = StrParam;
		}
		else
		{
			Err = true;
			ErrProperty = "Click";
		}
	}
	
	function dblClick(StrParam)
	{
		if (typeof(StrParam) == "string")
		{
			DblClick = StrParam;
		}
		else
		{
			Err = true;
			ErrProperty = "DblClick";
		}
	}
	
	function fieldClass(StrParam)
	{
		if (typeof(StrParam) == "string")
		{
			FieldClass = StrParam;
		}
		else
		{
			Err = true;
			ErrProperty = "FieldClass";
		}
	}
	
	function fieldId(StrParam)
	{
		if (typeof(StrParam) == "string")
		{
			FieldId = StrParam;
		}
		else
		{
			Err = true;
			ErrProperty = "FieldId";
		}
	}
	
	function fieldName(StrParam)
	{
		if (typeof(StrParam) == "string")
		{
			FieldName = StrParam;
		}
		else
		{
			Err = true;
			ErrProperty = "FieldName";
		}
	}
	
	function fieldSize(StrParam)
	{
		if (typeof(StrParam) == "string")
		{
			FieldSize = StrParam;
		}
		else
		{
			Err = true;
			ErrProperty = "FieldSize";
		}
	}
	
	function fieldType(StrParam)
	{
		if (typeof(StrParam) == "string")
		{
			if ((StrParam == "button") || (StrParam == "checkbox") || (StrParam == "file") ||
				(StrParam == "hidden") || (StrParam == "image") || (StrParam == "password") ||
				(StrParam == "radio" ) ||(StrParam == "reset") || (StrParam == "submit") || 
				(StrParam == "text"))
			{
				FieldType = StrParam;
			}
			else
			{
				Err = true;
				ErrProperty = "FieldType";
			}
		}
		else
		{
			Err = true;
			ErrProperty = "FieldType";
		}
	}
	
	function fieldValue(StrParam)
	{
		if (typeof(StrParam) == "string")
		{
			FieldValue = StrParam;
		}
		else
		{
			Err = true;
			ErrProperty = "FieldValue";
		}
	}
	
	function getFocus(StrParam)
	{
		if (typeof(StrParam) == "string")
		{
			GetFocus = StrParam;
		}
		else
		{
			Err = true;
			ErrProperty = "GetFocus";
		}
	}
	
	function imagePath(StrParam)
	{
		if (typeof(StrParam) == "string")
		{
			ImagePath = StrParam;
		}
		else
		{
			Err = true;
			ErrProperty = "ImagePath";
		}
	}
	
	function isChecked(BoolParam)
	{
		if (typeof(StrParam) == "boolean")
		{
			IsChecked = BoolParam;
		}
		else
		{
			Err = true;
			ErrProperty = "IsChecked";
		}
	}
	
	function isDisabled(BoolParam)
	{
		if (typeof(StrParam) == "boolean")
		{
			IsDisabled = BoolParam;
		}
		else
		{
			Err = true;
			ErrProperty = "IsDisabled";
		}
	}
	
	function isMap(BoolParam)
	{
		if (typeof(StrParam) == "boolean")
		{
			IsMap = BoolParam;
		}
		else
		{
			Err = true;
			ErrProperty = "IsMap";
		}
	}
	
	function isReadOnly(BoolParam)
	{
		if (typeof(StrParam) == "boolean")
		{
			IsReadOnly = BoolParam;
		}
		else
		{
			Err = true;
			ErrProperty = "IsReadOnly";
		}
	}
	
	function keyDown(StrParam)
	{
		if (typeof(StrParam) == "string")
		{
			KeyDown = StrParam;
		}
		else
		{
			Err = true;
			ErrProperty = "KeyDown";
		}
	}
	
	function keyPress(StrParam)
	{
		if (typeof(StrParam) == "string")
		{
			KeyPress = StrParam;
		}
		else
		{
			Err = true;
			ErrProperty = "KeyPress";
		}
	}
	
	function keyUp(StrParam)
	{
		if (typeof(StrParam) == "string")
		{
			KeyUp = StrParam;
		}
		else
		{
			Err = true;
			ErrProperty = "KeyUp";
		}
	}
	
	function lostFocus(StrParam)
	{
		if (typeof(StrParam) == "string")
		{
			LostFocus = StrParam;
		}
		else
		{
			Err = true;
			ErrProperty = "LostFocus";
		}
	}
	
	function mapPath(StrParam)
	{
		if (typeof(StrParam) == "string")
		{
			MapPath = StrParam;
		}
		else
		{
			Err = true;
			ErrProperty = "MapPath";
		}
	}
	
	function maxLength(StrParam)
	{
		if (typeof(StrParam) == "string")
		{
			MaxLength = StrParam;
		}
		else
		{
			Err = true;
			ErrProperty = "MaxLength";
		}
	}
	
	function mimeType(StrParam)
	{
		if (typeof(StrParam) == "string")
		{
			MimeType = StrParam;
		}
		else
		{
			Err = true;
			ErrProperty = "MimeType";
		}
	}
	
	function mouseDown(StrParam)
	{
		if (typeof(StrParam) == "string")
		{
			MouseDown = StrParam;
		}
		else
		{
			Err = true;
			ErrProperty = "MouseDown";
		}
	}
	
	function mouseMove(StrParam)
	{
		if (typeof(StrParam) == "string")
		{
			MouseMove = StrParam;
		}
		else
		{
			Err = true;
			ErrProperty = "MouseMove";
		}
	}
	
	function mouseOver(StrParam)
	{
		if (typeof(StrParam) == "string")
		{
			MouseOver = StrParam;
		}
		else
		{
			Err = true;
			ErrProperty = "MouseOver";
		}
	}
	
	function mouseOut(StrParam)
	{
		if (typeof(StrParam) == "string")
		{
			MouseOut = StrParam;
		}
		else
		{
			Err = true;
			ErrProperty = "MouseOut";
		}
	}
	
	function mouseUp(StrParam)
	{
		if (typeof(StrParam) == "string")
		{
			MouseUp = StrParam;
		}
		else
		{
			Err = true;
			ErrProperty = "MouseUp";
		}
	}
	
	function shortCutKey(StrParam)
	{
		if (typeof(StrParam) == "string")
		{
			ShortCutKey = StrParam;
		}
		else
		{
			Err = true;
			ErrProperty = "ShortCutKey";
		}
	}
	
	function tabIndex(StrParam)
	{
		if (typeof(StrParam) == "string")
		{
			TabIndex = StrParam;
		}
		else
		{
			Err = true;
			ErrProperty = "TabIndex";
		}
	}
	
	function title(StrParam)
	{
		if (typeof(StrParam) == "string")
		{
			Title = StrParam;
		}
		else
		{
			Err = true;
			ErrProperty = "Title";
		}
	}
	
	
	//-------------------Les méthodes publiques------------------------
	
	this.create = createControl;
	
	//-------------------Les méthodes internes à la classe----------------
	
	//---------Méthode de création du contrôle---------
	function createControl()
	{
		
		//Vérifier si une erreur s'est glissé lors de l'affectation des valeurs aux propriétés
		if (!Err)
		{
			var InputObj = document.createElement("INPUT");
			
			//Les attributs communs à tous les types
			setAttribute(InputObj,"type",FieldType);
			
			if (Change != null)
			{
				setAttribute(InputObj,"onChange",Change);
			}
			
			if (Click != null)
			{
				setAttribute(InputObj,"onClick",Click);
			}
			
			if (DblClick != null)
			{
				setAttribute(InputObj,"onDblclick",DblClick);
			}
			
			if (FieldName != null)
			{
				setAttribute(InputObj,"name",FieldName);
			}
			
			if (FieldId != null)
			{
				setAttribute(InputObj,"id",FieldId);
			}
			
			if (FieldClass != null)
			{
				setAttribute(InputObj,"class",FieldClass);
			}
			
			if (FieldValue != null)
			{
				setAttribute(InputObj,"value",FieldValue);
			}
			
			if (GetFocus != null)
			{
				setAttribute(InputObj,"onSelect",GetFocus);
			}
			
			if (IsDisabled != null)
			{
				if (IsDisabled)
				{
					setAttribute(InputObj,"disabled","disabled");
				}
				else
				{
					setAttribute(InputObj,"disabled","");
				}
			}
			
			if (IsReadOnly != null)
			{
				if (IsReadOnly)
				{
					setAttribute(InputObj,"readonly","readonly");
				}
				else
				{
					setAttribute(InputObj,"readonly","");
				}
			}
			
			if (KeyDown != null)
			{
				setAttribute(InputObj,"onKeydown",KeyDown);
			}
			
			if (KeyPress != null)
			{
				setAttribute(InputObj,"onKeypress",KeyPress);
			}
			
			if (KeyUp != null)
			{
				setAttribute(InputObj,"onKeyup",KeyUp);
			}
			
			if (LostFocus != null)
			{
				setAttribute(InputObj,"onBlur",LostFocus);
			}
			
			if (MouseDown != null)
			{
				setAttribute(InputObj,"onMousedown",MouseDown);
			}
			
			if (MouseMove != null)
			{
				setAttribute(InputObj,"onMousemove",MouseMove);
			}
			
			if (MouseOver != null)
			{
				setAttribute(InputObj,"onMouseover",MouseOver);
			}
			
			if (MouseOut != null)
			{
				setAttribute(InputObj,"onMouseout",MouseOut);
			}
			
			if (GetFocus != null)
			{
				setAttribute(InputObj,"onMouseUp",MouseUp);
			}
			
			if (ShortCutKey != null)
			{
				setAttribute(InputObj,"acceskey",ShortCutKey);
			}
			
			if (TabIndex != null)
			{
				setAttribute(InputObj,"tabindex",TabIndex);
			}
			
			if (Title != null)
			{
				setAttribute(InputObj,"title",Title);
			}
			
			//Déterminer le type de contrôle à créer et affecter les attributs cohérents à ce type. Les autres seront tout simplement ignorés.
			switch (FieldType)
			{
				case "button":
				
				break;
				
				case "checkbox":
					
					if (IsChecked)
					{
						setAttribute(InputObj,"checked","checked");
					}
					else
					{
						setAttribute(InputObj,"checked","");
					}
					
				break;
				
				case "file":
					
					if (MimeType != null)
					{
						setAttribute(InputObj,"accept",MimeType);
					}
					
				break;
				
				case "hidden":
				
				break;
				
				case "image":
					
					if (AlternateText != null)
					{
						setAttribute(InputObj,"alt",AlternateText);
					}
					
					if (ImagePath != null)
					{
						setAttribute(InputObj,"src",ImagePath);
					}
					
					if (IsMap)
					{
						setAttribute(InputObj,"ismap","ismap");
					}
					else
					{
						setAttribute(InputObj,"ismap","");
					}
					
					if (MapPath != null)
					{
						setAttribute(InputObj,"usemap",MapPath);
					}
					
					
				break;
				
				case "password":
				
					if (MaxLength != null)
					{
						setAttribute(InputObj,"maxlength",MaxLength);
					}
				
				break;
				
				case "radio":
					
					if (IsChecked)
					{
						setAttribute(InputObj,"checked","checked");
					}
					else
					{
						setAttribute(InputObj,"checked","");
					}
					
				break;
				
				case "reset":
				
				break;
				
				case "submit":
				
				break;
				
				case "text":
					
					if (MaxLength != null)
					{
						setAttribute(InputObj,"maxlength",MaxLength);
					}
					
				break;
			}
			
			return InputObj;
		}
		else
		{
			alert ("Erreur : " + ErrProperty + " invalide.");
			return false;
		}
	}
	
	//Méthode ajoutant les attributs à l'élement
	function setAttribute(Element,StrAttrName,StrAttrValue)
	{
		var Attr = document.createAttribute(StrAttrName);
		Attr.nodeValue = StrAttrValue;
		Element.setAttributeNode(Attr);
	}
	
	
	
}
