/*
@@@BUILDINFO@@@ st18HTMLUtil.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.st18HTMLUtil        = {};
	stockphoto.timeKeeper.st18HTMLUtil.StTime = new Date();
}

// %_function...%_ : function to be called
// %_Comp%_ : "Transaction Complete"
// %_Can%_ :  "Cancel the order"

stockphoto.HTMLUtil          ={};
stockphoto.HTMLUtil.Isw      =true;
stockphoto.HTMLUtil.LineOnOff=true;

stockphoto.HTMLUtil.ConvertToInteger=function( str ) 
{
	// strictly use only '0'-'9' characters
	var i, v=0, Bt;
	for( i=0; i<str.length; i++ ) 
	{
		Bt = str.charAt( i );
		if( ( Bt >= '0' )&&( Bt <= '9' ) ) v = ( 10*v ) + ( Bt-'0' );
	}
	return( v );
}

stockphoto.HTMLUtil.ConvertToIEAddress=function( iBf ) 
{
	var i, oBf="";
	
	oBf = "file:///";
	for( i=0; i<iBf.length; i++ ) 
	{
		Bt = iBf.charAt(i);
		if( Bt == '\\' )
		{
		  	oBf += '\\\\';
		}
		else 
		{
			oBf += File.encode(Bt);
		}
	}
	
	return( oBf );
}

stockphoto.HTMLUtil.Initialize=function() 
{
	if( stockphoto.HTMLUtil.Isw ) 
	{
		stockphoto.HTMLUtil.PredefinedFolderName=[
			{ n:"ScriptDir",f:stockphoto.core.folder.getScriptFolder,a:"",z:",\n" },
			{ n:"ResourceDir",f:stockphoto.core.folder.getResourceFolder,a:"",z:",\n" },
			{ n:"DllDir",f:stockphoto.core.folder.getDllFolder,a:"",z:",\n" },
			{ n:"SPTempDir",f:stockphoto.core.folder.getTempFolder,a:"",z:",\n" },
			{ n:"HTMLTemplateDir",f:stockphoto.core.folder.getTemplateFolder,a:"",z:",\n" } ];
		stockphoto.HTMLUtil.SetPredefinedFolder=function( i ) 
		{
			var	iBf, iVs, iDt;
			iVs	= File( stockphoto.HTMLUtil.PredefinedFolderName[i].f() ).fsName;
			iDt	= stockphoto.HTMLUtil.PredefinedFolderName[i].n;
			iBf	= "{k:\""+iDt+"\",l:"+iDt.length+",v:\""+iVs+"\",t:0}"+stockphoto.HTMLUtil.PredefinedFolderName[i].z;
			return( iBf );
		}
		stockphoto.HTMLUtil.fsOut = undefined;
		stockphoto.HTMLUtil.Isw = false;
	}
}

stockphoto.HTMLUtil.ConvLine=function( iRec, kv, doc_id ) 
{
	var i, j, kv0, kv2, kv4, oRec, nRec, nRc1, Rem, bContinue, m;
	var b, e;
	var oRec="";
	nRec = iRec.length;
	nRc1 = nRec - 1;
	Rem  = nRec;
	var index = iRec.indexOf("%_");
	if (index == -1)
		return iRec;
	else
		oRec = iRec.slice(0, index);

	for( i=index; i<nRec; ) 
	{
		if( ( i < nRc1 )&&
		    ( iRec.charAt( i ) == "%" )&&
		    ( iRec.charAt(i+1) == "_" ) ) 
		{
			bContinue = false;
			b = 0;
			e = kv.length;

			while ((e - b) > 8)
			{
				m = Math.floor((e+b)/2);
				if( iRec.substr( i+2, kv[m].l ) < kv[m].k ) 
				{
					e = m;				
				}
				else
				{
					b = m;
				}
			}

			for( j=b; j<e; j++ ) 
			{
				kv0 = kv[j].l;
				kv2 = kv0 + 2;
				kv4 = kv2 + 2;
				if( Rem >= kv4 ) 
				{
					if( iRec.substr( i+2, kv0 ) == kv[j].k ) 
					{
						if( ( iRec.charAt( i+kv2 ) == "%" )&&
						    ( iRec.charAt(i+kv2+1) == "_" ) ) 
						{
							if( kv[j].t != 0 ) 
							{
//								eval( ( kv[j].v )+"("+(doc_id)+");\n" );
								var funcOut = eval( ( kv[j].v )+"("+(doc_id)+");\n" );
								if( funcOut != undefined )
									return( funcOut );
								return( "" );
							}
							oRec += kv[j].v;
							i += kv4; Rem -= kv4;
							bContinue = true;
							break;
						}
					}
				}
			}
			if( bContinue )
			{
				continue;
			}
		}
		oRec += iRec.charAt( i ); i++; Rem--;
	}
	return( oRec );	
}

stockphoto.HTMLUtil.LocalHTMLmain=function( TemplatePath, ConvTable, OutputName, folder, bSpecial, doc_id ) 
{

	function kwSort(a, b)
	{
		a1 = a.k;
		b1 = b.k;

		if (a1 < b1) 
			return -1;
		if (a1 > b1)
			return 1;
		return 0;
	}

	var keyval, t, key, fsIn, js, ks, i, cc, cdst, csrc;
	if( stockphoto.HTMLUtil.Isw ) 
	{
		stockphoto.HTMLUtil.Initialize();
	}
	keyval = "var kv=[";
	for( key in ConvTable ) 
	{
		cdst = "";
		csrc = ( ConvTable[key] ).toString();
		for( i=0; i<csrc.length; i++ ) 
		{
			if( csrc.charAt(i) == "\"" )
			{
				cdst += "\\\"";
			}
			else 
			{
				cdst += csrc.charAt(i);
			}
		}
		if( ( key.length > 7 )&&( key.substring(0,8) == "function" ) ) 
		{
			t = 1; 
		}
		else 
		{
			t = 0;
		}
		keyval = keyval+"{k:\""+key+"\",l:"+key.length+",v:\""+cdst+"\",t:"+t+"},\n";
	}
	for( i=0; i<stockphoto.HTMLUtil.PredefinedFolderName.length; i++ )
		keyval += ( stockphoto.HTMLUtil.SetPredefinedFolder( i ) );
	keyval += '{k:"HTMLImageDir2",l:13,v:"../images",t:0},\n';
	keyval += '{k:"HTMLImageDir",l:12,v:"../images",t:0} ]\n';
	eval( keyval );

	kv.sort(kwSort);

	stockphoto.HTMLUtil.fsOut     = undefined;
	stockphoto.HTMLUtil.LineOnOff = true;
	stockphoto.HTMLUtil.skipLines = false;
	fsIn = new File( TemplatePath );
	try 
	{
		if( ! fsIn.exists )
		{
			throw( "" );
		}
		fsIn.open( "r" );
		try 
		{
			stockphoto.HTMLUtil.fsOut = new File( folder+"/"+OutputName );
			stockphoto.HTMLUtil.fsOut.encoding = "UTF-8";
			stockphoto.HTMLUtil.fsOut.open( "w", "TEXT", "????" );
			try 
			{
				for( ; ; ) 
				{
					js = fsIn.readln();
					if( fsIn.eof ) 
					{
						break;
					}
					if (js[0] == "/")
					{
						if (js.indexOf("//??DO NOT CONVERT START") == 0)
							stockphoto.HTMLUtil.skipLines = true;
						if (js.indexOf("//??DO NOT CONVERT END") == 0)
							stockphoto.HTMLUtil.skipLines = false;
					}
					if (stockphoto.HTMLUtil.skipLines)
						stockphoto.HTMLUtil.fsOut.writeln( js );
					else
					{
						ks = stockphoto.HTMLUtil.ConvLine( js, kv, doc_id );
						if( ( ks != "" )&&( stockphoto.HTMLUtil.LineOnOff ) )
						{
							stockphoto.HTMLUtil.fsOut.writeln( ks );
						}
					}
				}
			} 
			catch(e) 
			{
				stockphoto.HTMLUtil.fsOut.close();
			}
			stockphoto.HTMLUtil.fsOut.close();
		} 
		catch(e) 
		{
			stockphoto.HTMLUtil.fsOut = undefined;
		}
		fsIn.close();
	} 
	catch(e) 
	{
		;
	}
	if( ( stockphoto.HTMLUtil.fsOut != undefined )&&
	    ( stockphoto.HTMLUtil.fsOut.exists ) )
	{
		return( true );
	}
	return( false );
}

stockphoto.HTMLUtil.LocalHTMLbyDocID=function( TemplatePath, ConvTable, OutputName, doc_id ) 
{
	return( stockphoto.HTMLUtil.LocalHTMLmain( TemplatePath, ConvTable, OutputName, 
								stockphoto.core.folder.getTempFolder(), false, doc_id ) );
}

stockphoto.HTMLUtil.SubLocalHTMLbyDocID=function( TemplatePath, ConvTable, OutputName, doc_id, bIsForNavHtm ) 
{
	var Fldr;
	if( ( bIsForNavHtm == undefined )||( ! bIsForNavHtm ) )
	{
		Fldr = stockphoto.core.folder.getTempFolder()+"/"+stockphoto.core.CurrentProcessID;
	}
	else 
	{
		Fldr = stockphoto.core.folder.getSaveNavbarFolder();
	}
	if( stockphoto.core.folder.checkAndCreateFolder( Fldr ) )
	{
		return( stockphoto.HTMLUtil.LocalHTMLmain
						( TemplatePath, ConvTable, OutputName, Fldr, false, doc_id ) );
	}
	return( false );
}

stockphoto.HTMLUtil.GetKeyWordFromSearchXML=function( fldw, szKey, nKey, FileNm ) 
{
	var line, s, k1, k2, nL, key, fsIn;

	key  = ""
	fsIn = File( fldw+FileNm );
	if( ( fsIn != undefined )&&( fsIn.exists ) ) 
	{
		fsIn.open("r");
		// handle the first line - searchRequest.XML
		line = fsIn.readln(); //save time dump the 1stline
		if ( ! fsIn.eof )
		{
			line = fsIn.readln();
		}
		while( ! fsIn.eof ) 
		{
			nL = line.length;
			if( nL > nKey )
			{
				s = line.substr( 1, nKey );
			}
			else 
			{
				s = "xxx";
			}
			if( s.toUpperCase() == szKey ) 
			{
				k1 = line.indexOf('"');
				if( k1 > 0 ) 
				{
					s  = line.substring( k1+1, nL );
					k2 = s.indexOf( '"' );
					if( k2 > 0 ) 
					{
						key = s.substr( 0, k2 );
						fsIn.close();
						return( key );
					}
				}
				fsIn.close();
				return( "" );
			}
			line = fsIn.readln();
		}
		fsIn.close();
	}
	return( "" );
}

stockphoto.HTMLUtil.DecodeXMLStringTable=[ { n:"&quot;", l:6, v:'"' }, { n:"&apos;", l:6, v:"'" }, { n:"&amp;", l:5, v:"&" }, { n:"&gt;", l:4, v:">" }, { n:"&lt;", l:4, v:"<" } ];
stockphoto.HTMLUtil.DecodeXMLString=function( tIn ) {
	var bDoIt=true, i, iMax, iMax2, s, k, ky, nky, v, m, mm, Bt, t;
	t = tIn;
	while( t != "" ) 
	{
		m = 0;
		for( k=0; k<5; k++ ) 
		{
			s = t;
			t = "";
			ky    = stockphoto.HTMLUtil.DecodeXMLStringTable[k].n; 
			nky   = stockphoto.HTMLUtil.DecodeXMLStringTable[k].l;
			v     = stockphoto.HTMLUtil.DecodeXMLStringTable[k].v;
			iMax  = s.length;
			if( iMax <= 0 )
			{
				return( "" );
			}
			iMax2 = iMax - nky;
			mm    = 0;
			for( i=0; i<iMax; ) 
			{
				Bt = s.charAt( i );
				if( ( i <= iMax2 )&&( s.substr(i,nky) == ky ) ) 
				{
					t += v;
					i += nky;
					m++;
				} 
				else 
				{
					t += Bt;
					i ++;
				}
				if( Bt == '&' )
				{
					mm++; // # of '&'
				}
			} // for i
			if( ( k == 0 )&&( mm <= 0 ) )
			{
				return( t );
			}
		} // for k
		if( m <= 0 )
		{
			break;
		}
	}
	return( t );
}

stockphoto.HTMLUtil.GetKeyWord=function( fldw ) 
{
	var s, t=stockphoto.HTMLUtil.GetKeyWordFromSearchXML( fldw, "KEYWORD VALUE", 13, "/CACHE/searchRequest.xml" );
	s = stockphoto.DL.MiscDlgs.DecodeXMLString( t );
	if( s == "" )
	{
		t = stockphoto.HTMLUtil.GetKeyWordFromSearchXML( fldw, "KEYWORDFORPROMOTION VALUE", 25, "/CACHE/searchRequest.xml" );
		s = stockphoto.DL.MiscDlgs.DecodeXMLString( t );
	}
	return( s );
}

stockphoto.HTMLUtil.GetDocIDFromHTMLString=function( str4, Nm ) 
{
	var k, str, Ret={};
	Ret.doc_id = ( 1000 * ( str4.charCodeAt(0) - 0x30 ) )
	            + ( 100 * ( str4.charCodeAt(1) - 0x30 ) )	
	             + ( 10 * ( str4.charCodeAt(2) - 0x30 ) )
	              + (     ( str4.charCodeAt(3) - 0x30 ) );
	k = str4.length - 4;
	if( k > 0 )
	{
		Ret.str = str4.substr( 4, k ) 
	}
	else 
	{
		Ret.str = "";
	}
	return( Ret );
}

stockphoto.HTMLUtil.ConvertToPriceStringObj=undefined;
stockphoto.HTMLUtil.ChangeSymbol=function( str, ind )
{
	var p, s, v, n;
	if( str != undefined )
	{
		p = ( unescape( str ) ).split( "?" );
		if( ind < p.length )
		{
			s = p[ ind ];
			if( s.length > 2 )
			{
				if( ( s.charAt( 0 ) == "0" )&&
				    ( s.charAt( 1 ) == "x" ) )
				{
					v = "n = String.fromCharCode( "+s+" );\n";
					eval( v );
					return( n );
				}
			}
			return( s );
		}
	}
	return( "US$" );
}
stockphoto.HTMLUtil.ConvertToPriceString=function( priceStr, bForScript )
{
	var r=["",""], o, i, currencyType, ret={};
	// --- Internal -----
	this.initialize=function()
	{
		var currencyType, dt, Script, CurrStr
		try 
		{
			if( stockphoto.core.BoxCar.AnalyzeXmlFileObj == undefined )
			{
	 			if( stockphoto.core.init.partnersXMLfsObj == undefined ) 
				{
					stockphoto.core.init.checkPartnersXML();
				}
				if( stockphoto.core.init.partnersXMLfsObj != undefined )
				{
					stockphoto.core.BoxCar.AnalyzeXmlFileObj = new stockphoto.core.BoxCar.AnalyzeXmlFile( stockphoto.core.init.partnersXMLfsObj, stockphoto.core.BoxCar.HTMLUtil.CheckKeys );
				}
			}
			if( stockphoto.core.BoxCar.AnalyzeXmlFileObj == undefined )
			{
				throw( "1" );
			}

			currencyType = ( stockphoto.DL.getCurrencyType() ).toUpperCase(); // "USD","GBP","JPY" or "EUR"

			Script = "dt="+ "stockphoto.core.BoxCar.AnalyzeXmlFileObj.Partners[0].AdobeSupports[0].PriceList[0]."+(currencyType.toUpperCase()) +";";
			eval( Script );
			if( dt == undefined )
			{
				throw( "2" );
			}
			this.p0      = parseInt( dt.p0 );    // index of CurrStr table
			this.p1      =           dt.p1;      // '0" place sysbol before digits, "1" after
			this.p2      = parseInt( dt.p2 );    // # of spaces between symbol and digits
			this.p3      = parseInt( dt.p3 );    // # of decimal places
			this.p4      =           dt.p4;      // '0' decimal point = '.', '1'=','
			this.p5      =           dt.p5;      // 3 digits symbol: '0'='.', '1'=',' and '2'=' '
			this.justify =           dt.justify; // "right, "left", "both"
			CurrStr = stockphoto.core.BoxCar.AnalyzeXmlFileObj.Partners[0].AdobeSupports[0].PriceList[0].PriceSymbolsForScript.Value;
			this.ScriptSymbol = stockphoto.HTMLUtil.ChangeSymbol( CurrStr, this.p0 );
			CurrStr = stockphoto.core.BoxCar.AnalyzeXmlFileObj.Partners[0].AdobeSupports[0].PriceList[0].PriceSymbolsForHtml.Value;
			this.HtmlSymbol   = stockphoto.HTMLUtil.ChangeSymbol( CurrStr, this.p0 );
			this.initialized  = true;
			this.currencyType = currencyType;
		} catch(e) {
			this.initialized  = false;
		}
	}
	// --- Internal -----
	this.formatPrice=function( priceStr, nOfDecPnt, DecPntType, ThousandType )
	{
		var s, p, imax, ch, out, k, i, imax, v = parseFloat( priceStr );
		switch( nOfDecPnt )
		{
			case  0: v += 0.5;  break;
			case  1: v += 0.05; break;
			default: v += 0.005;
		}
		if( v <= 0 )
			v = 0;
		s = v.toString();
		p = s.indexOf( "." );
		if( p < 0 )
		{
			s += ".";
			p = s.indexOf( "." );
		};
		s += "000";
		switch( nOfDecPnt )
		{
			case  0: imax = p; break;
			default: imax = p + 1 + nOfDecPnt;
		}
		switch( ThousandType )
		{
			case '2': ch = ' '; break;
			case '1': ch = ','; break;
			default:  ch = '.';
		}
		out = "";
		k   = p % 3;
		for( i=0; i<imax; )
		{
			if( i == p )
			{
				if( DecPntType == '1' )
				     out += ',';
				else out += '.';
				i++;
				continue;
			}
			out += s.charAt( i );
			i++;
			if( i < p ) 
			{
				if( k == 1 )
					out += ch;
				if( ( --k ) < 0 )
					k = 2;
			}
		}
		return( out );
	}
	// --- Internal ----- end ---
	if( ( stockphoto.HTMLUtil.ConvertToPriceStringObj != undefined )&&
	    ( stockphoto.HTMLUtil.ConvertToPriceStringObj.currencyType != undefined ) )
	{
		currencyType = ( stockphoto.DL.getCurrencyType() ).toUpperCase(); // "USD","GBP","JPY" or "EUR"
		if( stockphoto.HTMLUtil.ConvertToPriceStringObj.currencyType != currencyType )
		{
			delete stockphoto.HTMLUtil.ConvertToPriceStringObj;
			stockphoto.HTMLUtil.ConvertToPriceStringObj = undefined;
		}
	}
	if( stockphoto.HTMLUtil.ConvertToPriceStringObj == undefined )
	{
		stockphoto.HTMLUtil.ConvertToPriceStringObj = new this.initialize();
	}
	o = stockphoto.HTMLUtil.ConvertToPriceStringObj;
	if( ( o == undefined )||( ! o.initialized ) )
		return( undefined )

	if( o.p1 == "0" )
	{
		if( bForScript )
		     r[0] = o.ScriptSymbol;
		else r[0] = o.HtmlSymbol;
		for( i=0; i<o.p2; i++ )
			 r[0] += ' ';
		r[1] = this.formatPrice( priceStr, o.p3, o.p4, o.p5 );
	}
	else 
	{
		r[0] = this.formatPrice( priceStr, o.p3, o.p4, o.p5 ); 
		for( i=0; i<o.p2; i++ )
			 r[1] += ' ';
		if( bForScript )
		     r[1] = o.ScriptSymbol;
		else r[1] = o.HtmlSymbol;
	}
	switch( o.justify )
	{
		case "both":  ret.v1 = r[0];      ret.v2 = r[1];      break;
		case "right": ret.v1 = " ";       ret.v2 = r[0]+r[1]; break;
		default:      ret.v1 = r[0]+r[1]; ret.v2 = " ";
	}
	return( ret );
}		

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

