/**************************************************************************************************************
	stefano guagnini
	1/2/2002
 **************************************************************************************************************
 
	Object InputManager
	-------------------
 
 **************************************************************************************************************/

	var _InputManager_readonly=0,
		_InputManager_readonly_noclear=1,
		_InputManager_mandatory=2,
		_InputManager_optional=3,
		_InputManager_info=4;
		
	var _InputManager_mandatory_class = "mandatoryinput",
		_InputManager_optional_class = "optionalinput",
		_InputManager_readonly_class = "readonlyinput",
		_InputManager_info_class = "onlyinfoinput",
		
		_InputManager_enabledbutton_class = "navigationbutton",
		_InputManager_disabledbutton_class = "navigationbutton_disabled",

		_InputManager_enabledlink_class = "navigationlink",
		_InputManager_overlink_class = "navigationlink_hover",
		_InputManager_disabledlink_class = "navigationlink_disabled";

 /*************************************************************************************************************/
 
function _InputManager()
{
	this.readonly = _InputManager_readonly;
	this.readonly_noclear = _InputManager_readonly_noclear;
	this.optional = _InputManager_optional;
	this.mandatory = _InputManager_mandatory;
	this.info = _InputManager_info;

	this.Clear = _InputManager_Clear;
	this.MakeUpper = _InputManager_MakeUpper;
	this.MakeLower = _InputManager_MakeLower;
	this.Hilight = _InputManager_Hilight;
	this.SelectText = _InputManager_SelectText;
	this.IsReadonly = _InputManager_IsReadonly;
	this.IsMandatory = _InputManager_IsMandatory;
	this.IsOptional = _InputManager_IsOptional;
	this.IsInfo = _InputManager_IsInfo;
	this.MakeReadonly = _InputManager_MakeReadonly;
	this.MakeMandatory = _InputManager_MakeMandatory;
	this.MakeOptional = _InputManager_MakeOptional;
	this.MakeInfo = _InputManager_MakeInfo;
	this.SetStatus = _InputManager_SetStatus;	
	this.Hide = _InputManager_Hide;
	this.Display = _InputManager_Display;
	this.IsHidden = _InputManager_IsHidden;
	this.IsDisplayed = _InputManager_IsDisplayed;
}

//*************************************************************************************************************

function _InputManager_IsValidInput(obj)
{
	var tag = obj.tagName.toLowerCase()
	if( tag == "textarea" || tag == "select" || tag == "button" || tag == "link" || tag == "label" )
		return tag;
	else if ( tag == "input" )
		return obj.type.toLowerCase();

	return null;
}

//*************************************************************************************************************

function _InputManager_Clear()
{
	var k, pArray=arguments, obj, tag, tpe;
	
	if ( arguments.length == 1 )
	{
		obj = arguments[0];
		tag = obj.tagName.toLowerCase();
		
		if ( tag != "input" && tag != "textarea" && tag != "select" && tag != "button" && obj.all != null )
			pArray = obj.all;
	}
	
	for (k=0; k<pArray.length; k++)
	{
		obj = pArray[k];
		tag = obj.tagName.toLowerCase();
		
		if ( tag == "select" )
		{
			obj.__selectedIndex = obj.selectedIndex = -1;
		}
		else if ( tag == "input" || tag == "textarea" || tag == "button" )
		{
			tpe = obj.type.toLowerCase();

			if ( tpe == "radio" || tpe == "checkbox" )
			{
				obj.checked = 0;
			}
			else if ( tpe == "button" || tpe == "submit" || tpe == "reset" || tpe == "button" )
			{
				//	nothing to do...
			}
			else // password | text | textarea
			{
				obj.value = "";
			}
		}
	}
}

//*************************************************************************************************************

function _InputManager_MakeUpper()
{
	var pArray = arguments, obj, k;
	
	if ( arguments.length == 1 )
	{
		if ( _InputManager_IsValidInput(arguments[0]) == null && arguments[0].all != null )
			pArray = arguments[0].all;		
	}

	for( k=0; k<pArray.length; k++ )
	{
		var obj = pArray[k];
		
		switch( _InputManager_IsValidInput(obj) )
		{
			case "submit":
			case "reset":
			case "button":
			case "link":
			case "label":
			case "select":
			case "password":
			case "radio":
			case "checkbox":
				break;
				
			case "textarea":
			case "text":
				obj.value = obj.value.toUpperCase();
				break;
		}
	}
}

function _InputManager_MakeLower()
{
	var pArray = arguments, obj, k;
	
	if ( arguments.length == 1 )
	{
		if ( _InputManager_IsValidInput(arguments[0]) == null && arguments[0].all != null )
			pArray = arguments[0].all;		
	}

	for( k=0; k<pArray.length; k++ )
	{
		var obj = pArray[k];
		
		switch( _InputManager_IsValidInput(obj) )
		{
			case "submit":
			case "reset":
			case "button":
			case "link":
			case "label":
			case "select":
			case "password":
			case "radio":
			case "checkbox":
				break;
				
			case "textarea":
			case "text":
				obj.value = obj.value.toLowerCase();
				break;
		}
	}
}

//*************************************************************************************************************

function _InputManager_Hilight(bOn)
{
	for ( var k=1; k < arguments.length; k++ )
	{
		var pObj = arguments[k];
		
		if ( pObj.className != _InputManager_disabledlink_class )
			if ( bOn )
			{
				pObj.className = _InputManager_overlink_class;
				pObj.style.color="#000000";
			}
			else
			{
				pObj.className = _InputManager_enabledlink_class;
				pObj.style.color="#ff0000";
			}
			
	}
}


//*************************************************************************************************************

function _InputManager_SelectText(obj)
{
	if( obj.isTextEdit )
	{
		obj.select();
	}
	else
	{
		obj.selStart = 0;
		obj.selLength = obj.value.length;
	}
}

//*************************************************************************************************************
		
function _InputManager_IsReadonly(obj)
{
	switch( _InputManager_IsValidInput(obj))
	{
		case "label":
		case "link": 
			return obj.className==_InputManager_disabledlink_class;
		
		default: 
			return obj.disabled || obj.readOnly;
	}
}

//*************************************************************************************************************

function _InputManager_IsMandatory(obj)
{
	switch( _InputManager_IsValidInput(obj))
	{
		case "label":
		case "link": 
			return !_InputManager_IsReadonly(obj);
		
		default: 
			return !_InputManager_IsReadonly(obj) && obj.className == _InputManager_mandatory_class;
	}
}

//*************************************************************************************************************

function _InputManager_IsOptional(obj)
{
	switch( _InputManager_IsValidInput(obj))
	{
		case "label":
		case "link": 
			return !_InputManager_IsReadonly(obj);
		
		default: 
			return !_InputManager_IsReadonly(obj) && obj.className == _InputManager_optional_class;
	}
}

//*************************************************************************************************************

function _InputManager_IsInfo(pObj)
{
	return pObj.className == _InputManager_info_class;
}

//*************************************************************************************************************

function _InputManager_LockSection(pContainer)
{
	for ( var k=0; k < pContainer.all.length; k++ )
	{
		switch( _InputManager_IsValidInput(pContainer.all[k]) )
		{
			case "textarea":
			case "select":
			case "radio":
			case "checkbox":
			case "text":
				pContainer.all[k].prev_status = pContainer.all[k].className;
				_InputManager_MakeReadonly(false,pContainer.all[k]);
				break;
		}
	}
}

//*************************************************************************************************************

function _InputManager_UnlockSection(pContainer)
{
	for ( var k=0; k < pContainer.all.length; k++ )
	{
		switch( _InputManager_IsValidInput(pContainer.all[k]) )
		{
			case "textarea":
			case "select":
			case "radio":
			case "checkbox":
			case "text":
				if ( pContainer.all[k].prev_status == _InputManager_mandatory_class )
					_InputManager_MakeMandatory(false,pContainer.all[k]);
				else if ( pContainer.all[k].prev_status == _InputManager_optional_class )
					_InputManager_MakeOptional(pContainer.all[k]);
				else if ( pContainer.all[k].prev_status == _InputManager_info_class )
					_InputManager_MakeInfo(pContainer.all[k]);
		}
	}
}

//*************************************************************************************************************

function _InputManager_MakeReadonly()
{
	if ( arguments.length )
	{
		var pArray = arguments, obj, k;
		var bClear = true, k = 0;

		if ( typeof(arguments[0]) == "number" || typeof(arguments[0]) == "boolean" )
		{
			bClear = Number(arguments[0]);
			k = 1;
		}
		
		if ( arguments.length == k+1 )
		{
			if ( _InputManager_IsValidInput(arguments[k]) == null && arguments[k].all != null )
			{
				pArray = arguments[k].all;		
				k = 0;
			}
		}

		for( ; k<pArray.length; k++ )
		{
			var obj = pArray[k];

			switch( _InputManager_IsValidInput(obj) )
			{
				case "select":
					_InputManager_unlockDDL(obj);
					if ( bClear ) 
						obj.selectedIndex=-1;
					obj.className = _InputManager_readonly_class;
					obj.readOnly = obj.disabled = true;
					break;
						
				case "submit":
				case "reset":
				case "button":
					obj.className = _InputManager_disabledbutton_class;
					obj.readOnly = obj.disabled = true;
					break;

				case "link":
				case "label":
					obj.className = _InputManager_disabledlink_class;
					obj.style.color="#808080";	//	per IE4
					break;

				case "radio":
				case "checkbox":
					if ( bClear ) 
						obj.checked=0;
					obj.readOnly = obj.disabled = true;
					break;

				case "textarea":
				case "password":
				case "text":
					if ( bClear ) 
						obj.value="";
					obj.className = _InputManager_readonly_class;
					obj.readOnly = obj.disabled = true;
					break;
			}
		}
	}
}		

//*************************************************************************************************************
		
function _InputManager_MakeMandatory()
{
	var pArray = arguments, obj, k;
	
	if ( arguments.length == 1 )
	{
		if ( _InputManager_IsValidInput(arguments[0]) == null && arguments[0].all != null )
			pArray = arguments[0].all;		
	}

	for( k=0; k<pArray.length; k++ )
	{
		var obj = pArray[k];
		
		switch( _InputManager_IsValidInput(obj) )
		{
			case "submit":
			case "reset":
			case "button":
				obj.className=_InputManager_enabledbutton_class
				obj.disabled=obj.readOnly=0;
				break;
				
			case "link":
			case "label":
				obj.className = _InputManager_enabledlink_class;
				obj.style.color="#ff0000";	//	per IE4 (#000000 in rosso)
				break;

			case "select":
				_InputManager_unlockDDL(obj);

			case "textarea":
			case "password":
			case "text":
				obj.className=_InputManager_mandatory_class;

			case "radio":
			case "checkbox":
				obj.disabled=obj.readOnly=0;
		}
	}		
}

//*************************************************************************************************************

function _InputManager_MakeOptional()
{
	var pArray = arguments, obj, k;
	
	if ( arguments.length == 1 )
	{
		if ( _InputManager_IsValidInput(arguments[0]) == null && arguments[0].all != null )
			pArray = arguments[0].all;		
	}

	for( k=0; k<pArray.length; k++ )
	{
		var obj = pArray[k];

		switch( _InputManager_IsValidInput(obj) )
		{
			case "submit":
			case "reset":
			case "button":
				obj.className=_InputManager_enabledbutton_class
				obj.disabled=obj.readOnly=0;
				break;

			case "link":
			case "label":
				obj.className = _InputManager_enabledlink_class;
				obj.style.color="#ff0000";	//	per IE4	(#000000 in rosso)
				break;

			case "select":
				_InputManager_unlockDDL(obj);
				
			case "textarea":
			case "password":
			case "text":
				obj.className=_InputManager_optional_class;
				
			case "radio":
			case "checkbox":
				obj.disabled=obj.readOnly=0;
		}
	}		
}

//*************************************************************************************************************

function _InputManager_lockDDL(obj)
{
	if ( obj.locked == null || obj.locked == false )
	{
		obj.__selectedIndex = obj.selectedIndex;
		obj.__onclick = obj.onclick;
		obj.__onchange = obj.onchange;
		obj.__onkeydown = obj.onkeydown;
	
		obj.onchange= _InputManager_handlelockedDDL;
		obj.onkeydown = _InputManager_handlelockedDDL;
		obj.onclick = _InputManager_handlelockedDDL;

		obj.locked = true;
	}
}
function _InputManager_unlockDDL(obj)
{	
	if ( obj.locked != null && obj.locked == true )
	{
		if ( obj.__selectedIndex != null ) obj.selectedIndex = obj.__selectedIndex;
		if ( obj.__onchange != null ) obj.onchange = obj.__onchange; else obj.onchange = "";
		if ( obj.__onclick != null ) obj.onclick = obj.__onclick; else obj.onclick = "";
		if ( obj.__onkeydown != null ) obj.onkeydown = obj.__onkeydown; else obj.onkeydown = "";
		obj.__onchange = null;
		obj.__onkeydown = null;
		obj.__onclick = null;
		obj.locked = false;
	}
}
function _InputManager_handlelockedDDL()
{
	this.selectedIndex = this.__selectedIndex;
	window.focus();
}

function _InputManager_MakeInfo()
{
	var pArray = arguments, obj, k;
	
	if ( arguments.length == 1 )
	{
		if ( _InputManager_IsValidInput(arguments[0]) == null && arguments[0].all != null )
			pArray = arguments[0].all;		
	}

	for( k=0; k<pArray.length; k++ ) if ( !_InputManager_IsReadonly(pArray[k]) )
	{
		var obj = pArray[k];
			
		obj.style.visibility = "hidden";
		switch( _InputManager_IsValidInput(obj) )
		{
			case "select":
				obj.className = _InputManager_info_class;
				obj.readOnly = true;
				obj.disabled = false;
				_InputManager_lockDDL(obj);
				break;

			case "link":
			case "label":
				obj.className = _InputManager_disabledlink_class;
				obj.style.color="#808080";	//	per IE4
				break;

			case "radio":
			case "checkbox":
				obj.readOnly = obj.disabled = true;
				break;

			case "textarea":
			case "password":
			case "text":
				obj.className = _InputManager_info_class;
				obj.readOnly = true;
				obj.disabled = false;
				break;					
		}
		obj.style.visibility = "visible";
	}
}		

//*************************************************************************************************************

function _InputManager_SetStatus(mode)
{
	switch( mode )
	{
		case _InputManager_readonly:
			for(var k=1;k<arguments.length;k++) _InputManager_MakeReadonly( true, arguments[k] );
			break;
			
		case _InputManager_readonly_noclear:
			for(var k=1;k<arguments.length;k++) _InputManager_MakeReadonly( false, arguments[k] );
			break;
			
		case _InputManager_mandatory:
			for(var k=1;k<arguments.length;k++) _InputManager_MakeMandatory( arguments[k] );
			break;
			
		case _InputManager_optional:
			for(var k=1;k<arguments.length;k++) _InputManager_MakeOptional( arguments[k] );
			break;
	}
}

//*************************************************************************************************************

function _InputManager_Hide()
{
	if ( arguments.length )
	{
		var bKeepSpace = false, k = 0;

		if ( typeof(arguments[0]) == "number" || typeof(arguments[0]) == "boolean" )
		{
			bKeepSpace = Number(arguments[0]);
			k = 1;
		}

		for( ; k<arguments.length; k++ )
		{
			var obj = arguments[k];

			if ( bKeepSpace )
				obj.style.visibility = "hidden";
			else
				obj.style.display = "none";
		}
	}		
}

function _InputManager_Display()
{
	for( var k=0; k<arguments.length; k++ )
	{
		arguments[k].style.visibility = "visible";
		arguments[k].style.display = "";
	}
}
			
function _InputManager_IsHidden(obj)
{
	if ( obj.style.visibility == "hidden" || obj.style.display == "none" )
		return true;
	else if ( obj.parentElement == null )
		return false;
	else
		return _InputManager_IsHidden(obj.parentElement);
}

function _InputManager_IsDisplayed(obj)
{
	return !_InputManager_IsHidden(obj);
}

//*************************************************************************************************************

var InputManager = new _InputManager();

//*************************************************************************************************************

