// ************************************************************************<br/>
// Solus S.A. Script Library<br/>
// Java Script utilities for form handling<br/>
// Copyright(c)2003 Solus S.A. All Rights Reserved.<br/>
// <B>Do not modify in design view. Switch to source view.</B><br/>
// ************************************************************************<br/>
// <br/>
// ************************************************************************<br/>
//	Author: Jerónimo Vargas ^_^ V<br/>
//	Date:	2003<br/>
// ************************************************************************<br/>
// <script>

//---------------------------------------------------------------------------------------
// Builds the redirect string.
function buildRedirect( oControls, sPage )
{
	sRedirect = sPage + "?";
	for( i = 0; i < oControls.length; i ++ )
		switch( oControls[i].type )
		{
			case "checkbox":
				if( oControls[i].checked )
					sRedirect += oControls[i].name + "=" + oControls[i].value + "&";

				break;

			default:
				sRedirect += oControls[i].name + "=" + oControls[i].value + "&";
				break;
		}			
	
	
	sRedirect = sRedirect.substr( 0, sRedirect.length - 1 )

	return escapex( sRedirect );
}

//---------------------------------------------------------------------------------------
//Creates a form in the current document.
function createForm( sAction, sMethod )
{
	if( ! sMethod ) sMethod = "post";
	
	oForm = document.createElement( "FORM" );
	oForm.action = sAction;
	oForm.method = sMethod;

	document.body.appendChild( oForm );

	return oForm;
}

//---------------------------------------------------------------------------------------
// Copys a control array into a form.

// (BOOL) setFormFields ( FORM HTML oForm, STRING oVarName, STRING oVars)
function setFormFields( oForm, oVarName, oVars )
{
	if( typeof( oVars ) == "object" )

		if( oVars.length > 0 )
		{
			for( i = 0; i < oVars.length; i ++ )
			{
				if( oVars[i].type == "checkbox" )
				{
					if( oVars[i].checked )
						createFormField( oForm, oVarName, oVars[i].value );
				}
				else
					createFormField( oForm, oVarName , oVars[i].value );
			}		
		}
		else
		{
			if( oVars.type == "checkbox" )
			{
				if( oVars.checked )
					setFormField( oForm, oVarName , oVars.value );
			}
			else
				setFormField( oForm, oVarName , oVars.value );
			
		}
	else
		if( oVars.value )
			setFormField( oForm, oVarName , oVars.value );		
		else
			setFormField( oForm, oVarName , oVars );

	return true;
}

//---------------------------------------------------------------------------------------
// Sets the value of a form element.
function setFormField( oForm, sVarName, sVarValue )
{
	//alert( "setFormField - Form: " + oForm.action );
	//alert( "setFormField - Exists: " + oForm.all[sVarName] );
	var condSi=0;
	
	if( oForm.elements[sVarName] ){
		oForm.elements[sVarName].value=sVarValue;
		condSi=1;
	}

	if( condSi==0 )
		createFormField( oForm, sVarName, sVarValue );
	
	/*if( oForm.getElementsByTagName(sVarName)  )
		oForm.getElementsByTagName(sVarName).value = sVarValue;
	else
		createFormField( oForm, sVarName, sVarValue );
	*/
	return true;
}

//---------------------------------------------------------------------------------------
// Creates an "input type="hidden" element and sets its value.
function createFormField( oForm, sVarName, sVarValue )
{
//	oForm.innerHTML += "<input type=\"hidden\" name=\"" + sVarName + "\" value=\"" + sVarValue + "\" />";
	var oField = null;
	
	if( !( sVarName == "" || sVarValue == "" ) )
	{
		/*
			oField			= document.createElement( "input" );
			oField.type		= "hidden";
			oField.name		= sVarName;
			oField.value	= sVarValue;
		*/
		oField = document.createElement( "INPUT" ); 
		oField.type="hidden";
		oField.name=sVarName;
		oField.value=sVarValue;
		oForm.appendChild( oField );
	}
	
	return oField;
}

//---------------------------------------------------------------------------------------
//Finds an attribute value in an URL-like string (delimited by '&' and '=')
function parseURLArguments( sData )
{
	var oReturn	= null;	
	var oTemp	= null;
	var oPairs	= sData.split( "&" );
	
	if( oPairs.length > 1 )
	{
		oReturn	= new Array();	
	
		for( i = 0; i < oPairs.length; i ++ )
		{
			oTemp = oPairs[i].split( "=" );
			oReturn[ oTemp[ 0 ] ]	= unescape( oTemp[ 1 ] );
		}
	}
	else
		oReturn = sData;
	
	return oReturn;
}

//---------------------------------------------------------------------------------------
// Pops Up a calendar modal window.
function datePicker( DateObject )
{

	var sBaseURL = "/";
	
	if( typeof( sURLRoot ) != "undefined" )
		sBaseURL = sURLRoot;
	
	oControl = document.getElementById( DateObject );
	
	if( window.showModalDialog ){
		var cSearchValue = showModalDialog( sBaseURL + "/_ScriptLibrary/datepicker.htm",oControl.value,"dialogWidth:195px; dialogHeight:155px; center:yes; status:no; help:no; dialogHide:no; unadorned:yes;" );
	}else{
		var cSearchValue = window.open( sBaseURL + "/_ScriptLibrary/datepicker.htm?"+oControl.name+"&"+oControl.value,'',"width=195,height=155,modal=yes" );
	}

	if( window.showModalDialog )
		if ( ! ( cSearchValue == -1 || cSearchValue == null ) )
		   oControl.value = cSearchValue;
}

//---------------------------------------------------------------------------------------
// Finds the first form element
function findFirstElement()
{
	var oForms = document.getElementsByTagName( "form" );
	
	for( i = 0; i < oForms.length; i ++ )
	{
		var oControls = oForms[i].elements;

		for( j = 0; j < oControls.length; j ++ )

			if( oControls[j].type.toLowerCase() != "hidden" && oControls[j].tabIndex != "-1" )
				return oControls[j].focus();
	}
}

function focusFirstElement()
{
	var oElement = null;
	
	if( oElement = findFirstElement() )
		oElement.focus();
}

//---------------------------------------------------------------------------------------
// Finds the next element on a form
function findNextElement( oElement )
{
	if( oForm = oElement.form )
	{
		var oControls = oForm.elements;
		var flgOK = false;
		
		for( i = 0; i < oControls.length; i ++ )
		{
			if( flgOK && oControls[i].type.toLowerCase() != "hidden" ) 
				return oControls[i];

			if( oControls[i] == oElement )
				flgOK = true;
		}
	}
}

function focusNextElement( oElement )
{
	var oNextElement = null;
	
	if( oNextElement = findNextElement( oElement ) )
		oNextElement.focus();
}

//---------------------------------------------------------------------------------------
//Simulates a redirect command
function JSRedirect( sRedirect )
{
	sRedirect = unescape( sRedirect );

	if( sRedirect.length < 512 ){
		window.location = sRedirect;
	}
	else
	{
		var sAddress = sRedirect.split( "?" );
		var oForm = createForm( sAddress[ 0 ] );

		var oPairs = sAddress[ 1 ].split( "&" );
		var oValues = null;
		var sValue = null;
		
		for( i = 0; i < oPairs.length; i ++ )
		{	
			oValues = oPairs[ i ].split( "=" );
			
			if( oValues.length > 2 )
			{
				sValue = "";
				for( j = 1; j < oValues.length; j ++ )
				{
					sValue += oValues[ j ]
					if( j < oValues.length - 1 )
						sValue += "=";
				}
			}
			else
				sValue = oValues[ 1 ];
			createFormField(oForm, oValues[ 0 ], sValue );
		}	
		oForm.submit();
	}		
}

//---------------------------------------------------------------------------------------
// LINK NAVIGATOR
//---------------------------------------------------------------------------------------
/*
 HTMLElement historyPush( FORM source, FORM destination, STRING link )
*/
function historyGO( level, form )
{
	var i, count;
	var s = form.redirect.value;

	fields = form[ "navHistory[]" ];
	
	if( fields )
		if( fields.length )
		{
			count = fields.length - level;
			
			for( i = 1; i < count; i ++ )
				s = popRedirect( s );
		}

	JSRedirect( s );
}

function historyPush( source, destination, label )
{
	var fields = source[ 'navHistory[]' ];
	var level = 0;
	var i;
	
	if( fields )
	{
		if( fields.length > 0 )
		{
			level = fields.length;
			
			for( i = 0; i < level ; i ++ )
				createFormField( destination, "navHistory[]", fields[ i ].value );
		}
		else
		{
			level = 1;
			createFormField( destination, "navHistory[]", fields.value );
		}
		
	}

	return createFormField( destination, "navHistory[]", label + "|" + level );
}

function popRedirect( redirect )
{
	fields = parseURLArguments( unescape( redirect ).split( "?" )[ 1 ] );

	return escapex( fields[ "redirect" ] );
}
//---------------------------------------------------------------------------------------
// STANDARD LIST
//---------------------------------------------------------------------------------------
function goToPage( iPage, iPageSize, oForm, evt ) 
{
	if( evt.keyCode )
		key=evt.keyCode;
	else
		key=evt.which;

	if( key != 0 )
		evt.returnValue	= 0;
	else
		evt.preventDefault(true);

	if( iPage.toString() != "" )
		if( isFinite( iPage ) && iPage >= 0 )
		{
			oForm.target = "_self";
			oForm.pageSize.value = iPageSize;
			oForm.page.value = iPage;
			oForm.submit();
		}
		else
			alert( "ERROR: La página seleccionada no es válida!" );
}

function orderData( sOrder ) 
{
	document.main.orderBy.value = sOrder;
	document.main.target = "_self";
	document.main.submit();
}

/***************************************************************************************/
/* End of code
/***************************************************************************************/
