/*
@@@BUILDINFO@@@ st05LocalURL.jsx 466 16-March-2007
*/

/*************************************************************************
*
* ADOBE CONFIDENTIAL
* ___________________
*
*  Copyright 2005 Adobe Systems Incorporated
*  All Rights Reserved.
*
* NOTICE:  All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any.  The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and may be covered by U.S. and Foreign Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
**************************************************************************/

if( ( stockphoto.timeKeeperOn != undefined )&&( stockphoto.timeKeeperOn ) )
{
	stockphoto.timeKeeper.st05LocalURL        = {};
	stockphoto.timeKeeper.st05LocalURL.StTime = new Date();
}

stockphoto.LocalURL={};

	stockphoto.LocalURL.CheckEachField=function( szK, iszV, n )	
	{
		var bNumeric=true, chFirst=' ', nNum=0, p=0.1, v=0, k=0, s=1, u, ch, j, szW, szV;
		bNumeric=true; 
		chFirst=' ';
		nNum=0;
		p=0.1;
		v=0;
		k=0; 
		s=1;
		if( ( szK=="" )||( iszV=="" ) ) 
		{
			return( "" );
		}
		ch = iszV.charAt(0);
		
		// digitally signed items must be treated as strings (not numbers) - rwe
		if( szK == "Price" || szK == "MediaID" || szK == "PurchaseID") 
		{
			bNumeric = false;
			szV = iszV;
		} else
		if( ( ch == '"' )||( ch == "'" ) ) 
		{
			szV = iszV.substring( 1, ((iszV.length)-1) );
			bNumeric = false;
		} 
		else	
		{
			szV = iszV;
			for( j=0; j<szV.length; j++ ) 
			{
				ch = szV.charAt(j);
				if( ( ch >= '0' )&&( ch <= '9' ) ) 
				{
					u = szV.charCodeAt(j) - 0x30;
					if( k != 0 ) 
					{
						v = v + ( p * u );
						p = 0.1 * p;
					} else	
						v = ( 10 * v ) + u;
					if( chFirst == ' ' )
					{
						chFirst = ch;
					}
				} 
				else 
				{
					if( ch == '-' ) 
					{
						s = -1;
					} 
					else 
					{
						if( ch != '.' ) 
						{
							bNumeric = false;
							break;
						}
						k++;
					}
				}
			}
		}
		if( ( bNumeric )&&( chFirst != '0' )&&( chFirst != ' ' ) )
		{
			szW = v * s;
		}
		else
		{
			szW = '"'+szV+'"';
		}
		if( n <= 0 ) 
		{
			return( "\n"+szK+": "+szW );
		}
		return( ",\n"+szK+": "+szW );
	}
stockphoto.LocalURL.AnalyzeURLstring=function( isrcStr, srcStr, doc_id ) 
{
	var reqStr, srcStr, nsrcStr, i, iSt, ch, szK, szV, n, x, args={};
	reqStr	= "args={";
	nsrcStr	= srcStr.length;
	iSt	= -1;
	szK	= "";
	szV	= "";
	n	= 0;
	x	= "";
	for( i=1; i<nsrcStr; i++ ) 
	{
		ch = srcStr.charAt(i);
		if( iSt < 0 ) 
		{
			if( ch == '?' )
				iSt = 0;
		} 
		else 
		{
			if( iSt == 0 ) 
			{	// parsing the key part (left of "=" sign)
				if( ch == '=' ) 
				{
					iSt = 1;
				}
				else 
				{
				  if( ch != '&' )
					szK += ch;
				}
			} 
			else 
			{	// parsing the value part (right of "=" sign)
				if( ch == '&' ) 
				{
					x = stockphoto.LocalURL.CheckEachField( szK,szV,n );
					if( x != "" ) 
					{
						reqStr += x;
						n++;
					}
					iSt = 0;
					szK = "";
					szV = "";
				} else 
					szV += ch;
			}
		}
	}
	x = stockphoto.LocalURL.CheckEachField( szK,szV,n );
	if( x != "" ) 
	{
		reqStr += x;
	}
	else
	{
		if( reqStr == "args={" )
		{
			return( "" );
		}
	}
	reqStr += ", doc_id:"+doc_id+" \n};\n";
	return( reqStr );
}

stockphoto.LocalURL.CheckLocURL=function( isrcStr, sz1stKey, doc_id ) 
{
	var srcStr=unescape( isrcStr );
	var reqStr=stockphoto.LocalURL.AnalyzeURLstring(isrcStr,srcStr,doc_id);
	if( reqStr != "" ) 
	{
		eval( reqStr );
		if( args.sptrigger == undefined ) 
		{
			return false; // as no 'sptrigger'
		}
			
		if( stockphoto.Burp.doIt ) // in updateXofYfromExternal to register searchID
		{
/*B*/	   	try
/*B*/		{
/*B*/			stockphoto.Burp.spTrigger( doc_id, args );
/*B*/		} catch(e) { ; }
		}	

		if( args.call == undefined ) 
		{
			// We need to handle the 'begin' trigger in-line and transparently. Otherwise we will
			// lose the eCom session token and sptrigger=complete will never occur.
			
			if (args.load == undefined && args.sptrigger == "begin")
			{
				if (stockphoto.DL.demandJsxLoading( stockphoto.DL.jsx_eCom))
				{
					// if the transaction doesn't complete within 20 seconds, check the quit flag anyway and quit if required to do so.
					stockphoto.core.dontQuit = true;		// temporarily prevent the user from quitting the App.
					stockphoto.core.quitRequested = false;
					cmdStr = "stockphoto.st07_eCom_HandleQuit(true);";
					stockphoto.core.quitTaskID = app.scheduleTask(cmdStr, 20000, false);	// 20 seconds ought to do it.
					
					stockphoto.core.BoxCar.NavBar.SetInECOMFlag( true, args.doc_id );
					
					// if sptrigger doesn't happen within 10 seconds, call checkOrderStatus anyway
					cmdStr = "stockphoto.st07_eCom_CheckOrderStatus(true);"
					stockphoto.core.orderTaskID = app.scheduleTask(cmdStr, 30000, false);	// 30 seconds ought to do it.
				}
			}
			else if( ( args.load == undefined )&&(	( args.sptrigger == "complete" )
			  ||( args.sptrigger == "print" )
			  ||( args.sptrigger == "cancel" )
			  ||( args.sptrigger == "resume" )
			  ||( args.sptrigger == "download")
			  ||( args.sptrigger == "home")
			  ||( args.sptrigger == "help")
			  ||( args.sptrigger == "images")
			  ||( args.sptrigger == "update")		// Make Bridge Launch updater
			  ||( args.sptrigger == "downloaditem")
			  ||( args.sptrigger == "downloadItem") ) ) 
			{
				var Dt = srcStr + "&call=st07_eCom&load=7";
				app.scheduleTask( "var srcStr=\""+escape(Dt)+"\";\nstockphoto.LocalURL.RunLocalURL( srcStr, "+doc_id+" );", 0, false );
				return true;
			}
			return false; // as no 'call'
		}

		app.scheduleTask( "stockphoto.LocalURL.RunLocalURL( \""+isrcStr+"\", "+doc_id+" );", 0, false );
		return true;
	}
	return false;
}

stockphoto.LocalURL.RunLocalURL=function( isrcStr, doc_id ) 
{
	var args={};
	var srcStr=unescape( isrcStr );
	var reqStr=stockphoto.LocalURL.AnalyzeURLstring(isrcStr,srcStr,doc_id);
	if( reqStr == "" ) 
	{
		stockphoto.DL.generalAlertBox("Fail to analuze: "+srcStr, 2);
		return;
	}
	eval( reqStr );
	if( args.load != undefined ) 
	{
		var szLoadNo, i, ch, v;
		szLoadNo = args.load + ",**";
		v = 0;
		for( i=0; i<szLoadNo.length; i++ ) 
		{
			ch = szLoadNo.charAt(i);
			if( ( ch >= '0' )&&( ch <= '9' ) ) 
			{
				v = ( 10 * v ) + ( szLoadNo.charCodeAt(i) - 0x30 );
			} 
			else 
			{
				if( ( v > 0 )&&( v < stockphoto.DL.DemandLoadJSXTable.length ) )
				{
					stockphoto.DL.demandJsxLoading( v );
				}
				v = 0;
			}
		}
	}

	var cmdLine;
	cmdLine = "stockphoto."+args.call+"( args );\n";
	eval( cmdLine );
}

if( ( stockphoto.timeKeeperOn != undefined )&&( stockphoto.timeKeeperOn ) )
{
	stockphoto.core.util.edTime( stockphoto.DL.jsx_LocalURL );
}

