/**-- 
	  Objects that handle the formatting of complex urls that link into various locations within 
	  the Marcole ControllerServlet.
--**/


/*----------------------------- ExternalMarcoleLinker ------------------------------*/
/**
 * Holds the basic navigation properties used in linking back to Marcole.  Contains 
 * an UpdateValues object that holds the Marcole session, state and request properties.
 * Pass-thru methods are used to access these methods.  All available properties 
 * are used to form up a 'base' URL used to link back to Marcole.  Application
 * specific URL formatters can use this class' functionality via compostion (see the 
 * GenericOnlineLinker class).
 */
function ExternalMarcoleLinker( bDebugOn )
{
	this.uvs = new UpdateValues();	
	this.langId = '0';
	this.navKey = null;
	this.transaction = null;
	this.lastTransaction =  null;
	this.viewId = null
	this.sessionId = null;
	this.serverURL = null;
	this.styleId = null;	

	this.setNavKey = ExternalMarcoleLinkerSetNavKey;
	this.setTransaction = ExternalMarcoleLinkerSetTransaction;
	this.setLastTransaction = ExternalMarcoleLinkerSetLastTransaction;
	this.setViewId = ExternalMarcoleLinkerSetViewId;
	this.setSessionId = ExternalMarcoleLinkerSetSessionId;
	this.setServerURL = ExternalMarcoleLinkerSetServerURL;
	this.setStyleId = ExternalMarcoleLinkerSetStyleId;	
	
	this.getNavKey = ExternalMarcoleLinkerGetNavKey;
	this.getTransaction = ExternalMarcoleLinkerGetTransaction;
	this.getLastTransaction = ExternalMarcoleLinkerGetLastTransaction;
	this.getViewId = ExternalMarcoleLinkerGetViewId;
	this.getSessionId = ExternalMarcoleLinkerGetSessionId;
	this.getServerURL = ExternalMarcoleLinkerGetServerURL;
	this.getStyleId = ExternalMarcoleLinkerGetStyleId;	
	
	// Pass-thru methods to the UpdateValues class
	this.addSessionVar = ExternalMarcoleLinkerAddSessionVar;
	this.addStateVar = ExternalMarcoleLinkerAddStateVar;
	this.addRequestVar = ExternalMarcoleLinkerAddRequestVar;

	this.toURLString = ExternalMarcoleLinkerToURLString;
	
	this.writeForm = ExternalMarcoleLinkerWriteForm;
	this.writeFormBody = ExternalMarcoleLinkerWriteFormBody;
	this.writeStartForm = ExternalMarcoleLinkerStartForm;
	this.writeEndForm = ExternalMarcoleLinkerEndForm;
	this.writeHtml = ExternalMarcoleLinkerWriteHtml;
	
	// For testing only
	this.bDebugOn = bDebugOn;
	this.testOutputString = '';
}

function ExternalMarcoleLinkerWriteHtml( str )
{
	if( this.bDebugOn )
		this.testOutputString += str;
	else
		document.write( str );
}

function ExternalMarcoleLinkerWriteForm()
{
	this.writeFormStart();
	this.writeFormBody();
	this.writeFormEnd();
}

function ExternalMarcoleLinkerStartForm()
{
	this.writeHtml('<form name="navform" method="post" action="' + ( this.getServerURL() == null ? '' : this.getServerURL() ) + '">');
}

function ExternalMarcoleLinkerWriteFormBody()
{
	document.write('<input type="hidden" name="DirectLink" value="true">');
	document.write('<input type="hidden" name="LANGID" value="0">');

	if ( this.navKey != null ) 
		document.write('<input type="hidden" name="navkey" value="' + this.navKey + '">');

	if ( this.transaction  != null ) 
		this.writeHtml('<input type="hidden" name="transaction" value="' + this.transaction + '">');
	if ( this.lastTransaction != null )
		this.writeHtml('<input type="hidden" name="lastTransaction" value="' + this.lastTransaction + '">');
	if ( this.viewid != null )
		this.writeHtml('<input type="hidden" name="viewid" value="' + this.viewid + '">');
	if ( this.sessionid != null )
		this.writeHtml('<input type="hidden" name="sessionid" value="' + this.sessionid + '">');
	if ( this.styleId != null )
		this.writeHtml('<input type="hidden" name="styleid" value="' + this.styleId +  '">');

	var uvsString = this.uvs.toURLString(); 
	if ( (uvsString != null) && (uvsString != '') ) 
	{
		this.writeHtml('<input type="hidden" name="UpdateValues" value="' + uvsString + '">');
	}
}

function ExternalMarcoleLinkerEndForm()
{
	this.writeHtml( "</form>" );
}

function ExternalMarcoleLinkerSetNavKey( newValue )
{
	this.navKey = newValue;
}
function ExternalMarcoleLinkerSetTransaction( newValue ) 
{
	this.transaction = newValue;
}
function ExternalMarcoleLinkerSetLastTransaction( newValue ) 
{
	this.lastTransaction = newValue;
}
function ExternalMarcoleLinkerSetViewId( newValue )
{ 
	this.viewId = newValue;
}
function ExternalMarcoleLinkerSetSessionId( newValue )
{
	this.sessionId = newValue;
}
function ExternalMarcoleLinkerSetServerURL( newValue )
{
	this.serverURL = newValue;
}
function ExternalMarcoleLinkerSetStyleId( newValue )
{
	this.styleId = newValue;
}
function ExternalMarcoleLinkerGetStyleId( newValue )
{
	return this.styleId;
}
function ExternalMarcoleLinkerGetNavKey()
{
	return this.navKey;
}
function ExternalMarcoleLinkerGetTransaction() 
{
	return this.transaction;
}
function ExternalMarcoleLinkerGetLastTransaction() 
{
	return this.lastTransaction;
}
function ExternalMarcoleLinkerGetViewId()
{ 
	return this.viewId;
}
function ExternalMarcoleLinkerGetSessionId()
{
	return this.sessionId;
}
function ExternalMarcoleLinkerGetServerURL()
{
	return this.serverURL;
}

function ExternalMarcoleLinkerAddSessionVar( newName, newValue )
{
	this.uvs.addSessionVar( newName, newValue, 'SE' );
}
function ExternalMarcoleLinkerAddStateVar( newName, newValue )
{
	this.uvs.addStateVar( newName, newValue, 'ST' );
}
function ExternalMarcoleLinkerAddRequestVar( newName, newValue )
{
	this.uvs.addRequestVar( newName, newValue, 'RE' );
}
function ExternalMarcoleLinkerToURLString( )
{
	var uRLString = '';
	uRLString += ( this.navKey != null ? 'navKey=' + this.navKey + '&' : '' );
	uRLString += ( this.transaction  != null ? 'transaction=' + this.transaction + '&' : '' );		
 	uRLString += ( this.lastTransaction != null ? 'lastTransaction=' + this.lastTransaction + '&' : '' );
	uRLString += ( this.viewId != null ? 'viewid=' + this.viewId + '&' : '' );
	uRLString += ( this.sessionId != null ? 'sessionid=' + this.sessionId + '&' : '' );
	uRLString += 'DirectLink=true&';
	uRLString += 'LANGID=' + this.langId;
	
	var uvsString = this.uvs.toURLString(); 
	if ( (uvsString != null) && (uvsString != '') ) 
	{
		uRLString += '&UpdateValues=';
		uRLString += uvsString;
	}
	return uRLString;
}

function ExternalMarcoleLinkerWriteForm()
{
	document.write('<input type="hidden" name="DirectLink" value="true">');
	document.write('<input type="hidden" name="LANGID" value="0">');

	if ( this.navKey != null ) 
		document.write('<input type="hidden" name="navkey" value="' + this.navKey + '">');

	if ( this.transaction  != null ) 
		document.write('<input type="hidden" name="transaction" value="' + this.transaction + '">');
	if ( this.lastTransaction != null )
		document.write('<input type="hidden" name="lastTransaction" value="' + this.lastTransaction + '">');
	if ( this.viewid != null )
		document.write('<input type="hidden" name="viewid" value="' + this.viewid + '">');
	if ( this.sessionid != null )
		document.write('<input type="hidden" name="sessionid" value="' + this.sessionid + '">');

	var uvsString = this.uvs.toURLString(); 
	if ( (uvsString != null) && (uvsString != '') ) 
	{
		document.write('<input type="hidden" name="UpdateValues" value="' + uvsString + '">');
	}
}


/*--------------------------------- UpdateValues ------------------------------------*/
/**
 * Essentially an array holding any session, state, and request properties required by
 * Marcole.  The toURLString() method iterates over the array forming up the value string
 * of the 'UpdateValues' request parameter.
 */
function UpdateValues()
{
	this.updateValueObjects = new Array();
	this.index = 0;
	this.addSessionVar = UpdateValuesAddSessionVar;
	this.addStateVar = UpdateValuesAddStateVar;
	this.addRequestVar = UpdateValuesAddRequestVar;
	this.toURLString = UpdateValuesToURLString;
}
function UpdateValuesAddSessionVar( newName, newValue )
{
	this.updateValueObjects[this.index] = new UpdateValue( newName, newValue, 'SE' );
	this.index++;
}
function UpdateValuesAddStateVar( newName, newValue )
{
	this.updateValueObjects[this.index] = new UpdateValue( newName, newValue, 'ST' );
	this.index++;
}
function UpdateValuesAddRequestVar( newName, newValue )
{
	this.updateValueObjects[this.index] = new UpdateValue( newName, newValue, 'RE' );
	this.index++;
}
function UpdateValuesToURLString()
{	
	var uRLString = '';
	for ( i = 0; i < this.updateValueObjects.length; i++ )
	{
		if( i > 0 )
			uRLString += '~~';
		uRLString += this.updateValueObjects[i].toURLString();
	}

	return uRLString;
}

/*--------------------------------- UpdateValue ------------------------------------*/
/**
 * Name value pairs formatted according to their ptype.  ptype indicates how the
 * properties are used in the Marcole aplication. 
 * @param name
 * @param value
 * @param ptype : SE, ST, or RE, for session, state or request ( respectively )
 */
function UpdateValue( name, value, ptype )
{
	this.name = name;
	this.value = value;
	this.persistence_type = ptype;
	
	this.toURLString = UpdateValueToURLString;
}

function UpdateValueToURLString()
{
	return '[' + this.persistence_type + ']' + this.name + '^^' + this.value;
}

