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

// Checkout namespace and zstringCache instance
stockphoto.checkout = {};
stockphoto.checkout.zc = new stockphoto.zstringCache("st02CheckOut");
stockphoto.checkout.zc.dict = "Checkout.txt";

// Should not localize the following keywords
stockphoto.checkout.DLLKeywords = 
{
	Version : "Version",
	MediaID : "MediaID",
	PartnerID : "PartnerID",
	MediaTitle : "MediaTitle",
	PurchaseID : "PurchaseID",
	Price : "Price",
	Currency : "Currency",
	LanguageID : "LanguageID",
	Signature : "Signature",
	Algorithm : "Algorithm",

	CurrencyType : "CurrencyType",
	lineItemID : "LineItemID",
	OrderID : "OrderID",
	CheckoutDate : "CheckoutDate",
	CheckoutStatus : "CheckoutStatus",
	RootDomain : "RootDomain",

	itemCount : "itemCount",
	beginItem : "beginItem",
	endItem : "endItem",

	dlWaiting : "Waiting",
	dlDone : "Done",
	dlDownloading : "Downloading",
	dlFailed : "Failed",

	itemSize : 4
}

st02_Err_eComUnavailable = stockphoto.checkout.zc.get("$$$/Search/ErrorMessage/EComUnavailable=The Adobe Stock Photos store is currently unavailable. Please try again later.");

//--------------------------------------------------------------------
// Download List Api
//--------------------------------------------------------------------

//======================
// DLLGetMediaKey
//======================
stockphoto.checkout.DLLGetMediaKey = function(index)
{
	return "bm_dllItem" + index + "_MediaXML";
}

//======================
// DLLGetSignatureKey
//======================
stockphoto.checkout.DLLGetSignatureKey = function(index)
{
	return "bm_dllItem" + index + "_Signature";
}

//======================
// DLLGetFlagKey
//======================
stockphoto.checkout.DLLGetFlagKey = function(index)
{
	return "bm_dllItem" + index + "_Flag";
}

//======================
// DLLGetItemKey
//======================
stockphoto.checkout.DLLGetItemKey = function(index)
{
	return "bm_dllItem" + index;
}

//======================
// DLLGetItemCount
//======================
stockphoto.checkout.DLLGetItemCount = function()
{
	if (app.preferences.bm_DownloadListCount == undefined)
	{
		app.preferences.bm_DownloadListCount = 0;
	}
	
	return	app.preferences.bm_DownloadListCount;
}

//======================
// DLLConvert
//======================
stockphoto.checkout.DLLConvert = function( linePart, downloadObject )
{
	try
	{
		with (stockphoto.checkout.DLLKeywords)
		{
			switch ( linePart[0] )
			{
				case MediaID:
					downloadObject.mediaXMLMediaID     = linePart[1];
					break;
				case PartnerID:
					downloadObject.mediaXMLPartnerID   = linePart[1];
					break;
				case Version:
					downloadObject.mediaXMLVersion     = linePart[1];
					break;
				case MediaTitle:
					downloadObject.mediaXMLMediaTitle  = linePart[1];
					break;
				case LanguageID:
					downloadObject.mediaXMLLanguageID  = linePart[1];
					break;
				case PurchaseID:
					downloadObject.selectedPurchaseID  = linePart[1];
					break;
				case Price:
					downloadObject.selectedPrice       = linePart[1];
					break;
				case Currency:
					downloadObject.selectedCurrency    = linePart[1];
					break;
				case CurrencyType:
					downloadObject.selectedCurrency    = linePart[1];
					break;
				case lineItemID:
					downloadObject.lineItemID          = linePart[1];
					break;
				case OrderID:
					downloadObject.orderID             = linePart[1];
					break;
				case Signature:
					downloadObject.signature           = linePart[1];
					break;
				case CheckoutDate:
					downloadObject.checkoutDate        = linePart[1];
					break;
				case CheckoutStatus:
					downloadObject.checkoutStatus      = linePart[1];
					break;
				case Algorithm:
					downloadObject.algorithm           = linePart[1];
					break;
				case RootDomain:
					downloadObject.rootDomain          = linePart[1];
					break;
			}
		}
		// LDM NOTE: Why return another copy of object from input?
		// How about a bool or int for fail/success
		
		return downloadObject;
	}
	catch (e)
	{
		return null;
	}	
}

/*******Watson: 1328737*******/
stockphoto.checkout.RefreshShoppingCartPage = function(doc_id)
{
	var count;
	for( count=0; count < app.documents.length; count++ )
	{
		if( ( "undefined" != typeof app.documents[count] )&&
		    ( app.documents[count].id != doc_id )&&
		    ( "undefined" != typeof app.documents[count].thumbnail) )
		{
			if( 0 < ( stockphoto.core.util.getDisplayPath(app.documents[count].thumbnail,"").indexOf( stockphoto.core.consts.ShoppingCartName ) )&&
			    ( "HTML" == app.documents[count].presentationMode) )
			{
				app.scheduleTask("stockphoto.shoppingCart.shoppingCart_Body( false, "+app.documents[count].id+" )", 0, false);
				return;
			}
		}
	} // for
}


//======================
// DLLAddItem
//======================
stockphoto.checkout.DLLAddItem = function( DLLFile, doc_id, clearShoppingCart )
{
	try
	{
		var line;
		var linePart;
		var count = 0;
		var bHasItemsToDL = false;

		var downloadObject = new Object();

		DLLFile.open("r");

		// skip blank lines to get the first line	
		line = DLLFile.readln();
		while (!DLLFile.eof)
		{
			linePart = line.split("=");
			if (linePart[0] == stockphoto.checkout.DLLKeywords.itemCount)
			{
				count = linePart[1];
				break;
			}
			else
			{
				line = DLLFile.readln();	
			}
		}
		
		var addImageToList = true;
		var dllCount = 0;
		var itm = null;
        var obj = null;
		var itemIndex;
		var propertyName = null;
		
		for (var i = 0; i < count; i++)
		{
			line = DLLFile.readln();
			while ((!DLLFile.eof) && (line != stockphoto.checkout.DLLKeywords.endItem))
			{
				linePart = new Array(2);
				var pIndex = line.indexOf("=");
				linePart[0] = line.slice(0, pIndex);
				linePart[1] = unescape(line.slice(pIndex + 1, line.length));

				downloadObject = stockphoto.checkout.DLLConvert(linePart, downloadObject);
				if (undefined != downloadObject) // We got at least one item in the order
				{
                    bHasItemsToDL = true;
                }
				line = DLLFile.readln();
			}

			// Check to see if the item is already in the download list
			addImageToList = true;
			dllCount = stockphoto.checkout.DLLGetItemCount();
			
			for (var j = 0; j < dllCount; j++) 
			{
				itm = stockphoto.checkout.DLLGetItem(j);
                obj = stockphoto.checkout.DLLConvertItem2Object(itm);
                if ((obj.mediaXMLMediaID == downloadObject.mediaXMLMediaID) && (obj.mediaXMLPartnerID == downloadObject.mediaXMLPartnerID))
                {
                	addImageToList = false;
                	break;
                }
			}
			
			// item is not in the list, so add it
			if (!addImageToList)
			{
				continue;
			};
			
			with (stockphoto.checkout.DLLKeywords)
			{
				itemIndex = stockphoto.checkout.DLLGetItemCount();
				propertyName = stockphoto.checkout.DLLGetMediaKey(itemIndex);
				app.preferences[propertyName] = 
					Version + "="     + escape(downloadObject.mediaXMLVersion)    +
					"&" + MediaID + "="    + escape(downloadObject.mediaXMLMediaID)    +
					"&" + PartnerID + "="  + escape(downloadObject.mediaXMLPartnerID)  +
					"&" + MediaTitle + "=" + escape(downloadObject.mediaXMLMediaTitle) +
					"&" + LanguageID + "=" + escape(downloadObject.mediaXMLLanguageID);
	
				propertyName = stockphoto.checkout.DLLGetItemKey(itemIndex);
				app.preferences[propertyName] = 
					PurchaseID + "=" + escape(downloadObject.selectedPurchaseID) +
					"&" + Price + "="      + escape(downloadObject.selectedPrice)      +
					"&" + Currency + "="   + escape(downloadObject.selectedCurrency)   +
					"&" + lineItemID + "=" + escape(downloadObject.lineItemID)         +
					"&" + OrderID + "="    + escape(downloadObject.orderID)            +
					"&" + CheckoutDate + "="   + escape(downloadObject.checkoutDate)   +
					"&" + CheckoutStatus + "=" + escape(downloadObject.checkoutStatus) +
					"&" + RootDomain + "=" + escape(downloadObject.rootDomain); 
	
				propertyName = stockphoto.checkout.DLLGetSignatureKey(itemIndex);
				app.preferences[propertyName] = Signature + "="  + escape(downloadObject.signature) +
								"&" + Algorithm + "="  + escape(downloadObject.algorithm);
	
				propertyName = stockphoto.checkout.DLLGetFlagKey(itemIndex);
				app.preferences[propertyName] = dlWaiting;
	
				app.preferences.bm_DownloadListCount += 1;
			}
		}	// for 
		
		DLLFile.close();
		DLLFile.remove();

        /****** Watson: 1322547 **********/
        if (( true == clearShoppingCart) && (true == bHasItemsToDL))
        {		
			if (stockphoto.DL.demandJsxLoading( stockphoto.DL.jsx_AddToCart ))	// Make sure st01 has been loaded! - rwe
			{
				stockphoto.addToCart.LSCRemoveAllItems();
				
				// Create a empty shopping cart page in case user uses left arrow after purchase.
				if (stockphoto.DL.demandJsxLoading( stockphoto.DL.jsx_ShoppingCart ))
				{
					stockphoto.shoppingCart.createEmptyHtmlPage(doc_id);
				}
			}
			/*******Watson: 1328737*******/
			stockphoto.checkout.RefreshShoppingCartPage(doc_id);
		}
	}
	catch (e)
	{
		stockphoto.core.log.logString( 2,"Exception in stockphoto.checkout.DLLAddItem" );
	}	
	
	return bHasItemsToDL;
}

//======================
// DLLAddItemBack
//======================
stockphoto.checkout.DLLAddItemBack = function(item)
{
	try
	{
		var index = stockphoto.checkout.DLLGetItemCount();

		var propertyName = "" + stockphoto.checkout.DLLGetMediaKey(index);
		app.preferences[propertyName] = item[0];
		
		propertyName = "" + stockphoto.checkout.DLLGetItemKey(index);
		app.preferences[propertyName] = item[1];
		
		propertyName = "" + stockphoto.checkout.DLLGetSignatureKey(index);
		app.preferences[propertyName] = item[2];
		
		propertyName = "" + stockphoto.checkout.DLLGetFlagKey(index);
		app.preferences[propertyName] = item[3];
		
		app.preferences.bm_DownloadListCount += 1;
	}
	catch (e)
	{
		stockphoto.core.log.logString( 2,"Exception in  stockphoto.checkout.DLLAddItemBack" );
	}
	
	return;
}

//======================
// DLLGetItem
//======================
stockphoto.checkout.DLLGetItem = function(index)
{
	try
	{
		var dllItem = new Array(stockphoto.checkout.DLLKeywords.itemSize);
		
		var propertyName = "" + stockphoto.checkout.DLLGetMediaKey(index);
		dllItem[0] = app.preferences[propertyName];
		
		propertyName = "" + stockphoto.checkout.DLLGetItemKey(index);
		dllItem[1] = app.preferences[propertyName];
		
		propertyName = "" + stockphoto.checkout.DLLGetSignatureKey(index);
		dllItem[2] = app.preferences[propertyName];
		
		propertyName = "" + stockphoto.checkout.DLLGetFlagKey(index);
		dllItem[3] = app.preferences[propertyName];

		return dllItem;
	}
	catch (e)
	{
		stockphoto.core.log.logString( 2,"Exception in stockphoto.checkout.DLLGetItem" );
		return null;
	}
}

//======================
// DLLGetFlag
//======================
stockphoto.checkout.DLLGetFlag = function(index)
{
	try
	{
		var flag;
		var propertyName = "" + stockphoto.checkout.DLLGetFlagKey(index);
		flag = app.preferences[propertyName];
		
		return flag;
	}
	catch (e)
	{
		stockphoto.core.log.logString( 2,"Exception in stockphoto.checkout.DLLGetFlag" );
		return "";
	}
}

//======================
// DLLSetFlag
//======================
stockphoto.checkout.DLLSetFlag = function(flag, index)
{
	try
	{
		var flag;
		var propertyName = "" + stockphoto.checkout.DLLGetFlagKey(index);
		app.preferences[propertyName] = flag;
	}
	catch (e)
	{
		stockphoto.core.log.logString( 2,"Exception in stockphoto.checkout.DLLSetFlag" );
	}
	
	return;
}

//======================
// DLLConvertItem2Object
//======================
stockphoto.checkout.DLLConvertItem2Object = function(dllItem)
{
	try
	{
		var downloadObject = new Object();
		
		var line;
		var linePart;
		var pIndex;
		
		for (var i = 0; i < stockphoto.checkout.DLLKeywords.itemSize; i++)
		{
			if (i == (stockphoto.checkout.DLLKeywords.itemSize - 1))
			{
				downloadObject.flag = dllItem[i];
				continue;
			}
			
			line =  dllItem[i].split("&");
			for (var j = 0; j < line.length; j++)
			{
				linePart = new Array(2);
				
				pIndex = line[j].indexOf("=");
				
				linePart[0] = line[j].slice(0, pIndex);
				linePart[1] = unescape(line[j].slice(pIndex + 1, line[j].length));
				
				downloadObject = stockphoto.checkout.DLLConvert(linePart, downloadObject);
			}
		}

		return downloadObject;
	}
	catch (e)
	{
		stockphoto.core.log.logString( 2,"Exception in stockphoto.checkout.DLLConvertItem2Object" );
		return null;
	}
}

//======================
// DLLIsEmpty
//======================
stockphoto.checkout.DLLIsEmpty = function()
{
	var isEmpty = true; // default unless see something or error
	
	try
	{
		var item;
		var count = stockphoto.checkout.DLLGetItemCount();
		for (var i = 0; i < count; i++)
		{
			item = stockphoto.checkout.DLLGetItem(i);
			if ((item[0] != undefined) && (item[0] != ""))
			{
				// set return and leave
				isEmpty = false;
				break;
			}
		}
	}
	catch (e)
	{
		stockphoto.core.log.logString( 2,"Exception in stockphoto.checkout.DLLIsEmpty" );
		isEmpty = false;
	}
	
	return isEmpty;
}

//======================
// DLLDoRemoveItem
//======================
stockphoto.checkout.DLLDoRemoveItem = function(index)
{
	var propertyName = stockphoto.checkout.DLLGetMediaKey(index);
	app.preferences[propertyName] = "";
	
	propertyName = stockphoto.checkout.DLLGetItemKey(index);
	app.preferences[propertyName] = "";
	
	propertyName = stockphoto.checkout.DLLGetSignatureKey(index);
	app.preferences[propertyName] = "";
	
	propertyName = stockphoto.checkout.DLLGetFlagKey(index);
	app.preferences[propertyName] = "";
	
	return;
}

//======================
// DLLRemoveItem
//======================
stockphoto.checkout.DLLRemoveItem = function(index)
{
	try
	{
		stockphoto.checkout.DLLDoRemoveItem(index);
		if (stockphoto.checkout.DLLIsEmpty())
		{
			stockphoto.checkout.DLLRemoveAll();
		}
	}
	catch (e)
	{
		stockphoto.core.log.logString( 2,"Exception in stockphoto.checkout.DLLRemoveItem" );
	}
	
	return;
}

//======================
// DLLRemoveAllDownloadedItems
//======================
stockphoto.checkout.DLLRemoveAllDownloadedItems = function()
{
	try
	{
		var count = stockphoto.checkout.DLLGetItemCount();
		var flag;
		for (var i = 0; i < count; i++)
		{
			flag = stockphoto.checkout.DLLGetFlag(i);
			if (flag == stockphoto.checkout.DLLKeywords.dlDone)
			{
				stockphoto.checkout.DLLRemoveItem(i);
			}
		}
	}
	catch (e)
	{
		stockphoto.core.log.logString( 2,"Exception in stockphoto.checkout.DLLRemoveAllDownloadedItems" );
	}
	
	return;
}

//======================
// DLLGetGroupCount
//======================
stockphoto.checkout.DLLGetGroupCount = function(DoneGroup, WaitingGroup, FailedGroup)
{
	var groupCount = 0;
	try
	{
		var count = stockphoto.checkout.DLLGetItemCount();
		var flag;
		for (var i = 0; i < count; i++)
		{
			flag = stockphoto.checkout.DLLGetFlag(i);
			if ((flag == stockphoto.checkout.DLLKeywords.dlDone) && DoneGroup)
			{			
				groupCount ++;
			}
			
			if ((flag == stockphoto.checkout.DLLKeywords.dlWaiting) && WaitingGroup)
			{			
				groupCount ++;
			}
			
			if ((flag == stockphoto.checkout.DLLKeywords.dlFailed) && FailedGroup)
			{			
				groupCount ++;
			}
		}
	}
	catch (e)
	{
		stockphoto.core.log.logString( 2,"Exception in stockphoto.checkout.DLLGetNoFailItemCount" );
		groupCount = 0;
	}
	
	return groupCount;
}

//======================
// DLLClean
//======================
stockphoto.checkout.DLLClean = function(deleteDone, deleteFailed, deleteWaiting)
{
	try
	{
		var item;
		var count = stockphoto.checkout.DLLGetItemCount();
		
		if (count == 0)
		{
			return;
		}

		var itemArray = new Array(count);
		var arrayIndex = 0;
		for (var i = 0; i < count; i++)
		{
			item = stockphoto.checkout.DLLGetItem(i);
			if ((item[3] == stockphoto.checkout.DLLKeywords.dlWaiting) && deleteWaiting)
			{
				continue;
			}
			else if ((item[3] == stockphoto.checkout.DLLKeywords.dlFailed) && deleteFailed)
			{
				continue;
			}
			else if ((item[3] == stockphoto.checkout.DLLKeywords.dlDone) && deleteDone)
			{
				continue;
			}
			else if ((item[0] != undefined) && (item[0] != ""))
			{
				itemArray[arrayIndex] = item;
				arrayIndex ++;
			}
		}

		stockphoto.checkout.DLLRemoveAll();

		for (var i = 0; i < arrayIndex; i++)
		{
			item = itemArray[i];
			if (item[3] == stockphoto.checkout.DLLKeywords.dlFailed)
			{
				item[3] = stockphoto.checkout.DLLKeywords.dlWaiting;
			}
			
			stockphoto.checkout.DLLAddItemBack(item);	
		}		
	}	
	catch (e)
	{
		stockphoto.core.log.logString( 2,"Exception in stockphoto.checkout.DLLClean" );
	}
	
	return;
}

//======================
// DLLRemoveAll
//======================
stockphoto.checkout.DLLRemoveAll = function()
{
	try
	{
		var count = stockphoto.checkout.DLLGetItemCount();
		for (var i = 0; i < count; i++)
		{
			var propertyName = stockphoto.checkout.DLLGetMediaKey(i);
			app.preferences.clear(propertyName);
			
			propertyName = stockphoto.checkout.DLLGetItemKey(i);
			app.preferences.clear(propertyName);
			
			propertyName = stockphoto.checkout.DLLGetSignatureKey(i);
			app.preferences.clear(propertyName);
			
			propertyName = stockphoto.checkout.DLLGetFlagKey(i);
			app.preferences.clear(propertyName);
		}

		app.preferences.bm_DownloadListCount = 0;
	}
	catch (e)
	{
		stockphoto.core.log.logString( 2,"Exception in stockphoto.checkout.DLLRemoveAll" );
	}
	
	return;
}

//======================
// eventHandler
//======================
stockphoto.checkout.eventHandler = function(event)
{
	stockphoto.core.log.logString( 0,"stockphoto.checkout.eventHandler" );
	stockphoto.core.html.setUrlFilter( app.document );
	
	if (event.eventClass == "checkout")
	{
		if (event.eventName == "carturl")
		{
			if (event.orderID != undefined)
			{
				app.preferences.stockphotoOrderId = event.orderID;
				if (event.url != undefined)
				{
					/*B*/	if( ( stockphoto.Burp.doIt )&&( stockphoto.Burp.currSc != undefined ) )
					/*B*/	{
					/*B*/		try
					/*B*/		{
					/*B*/			stockphoto.Burp.checkSc( app.document.id ); // finalize add/record items
					/*B*/   			stockphoto.Burp.callSPSUI( app.document.id, stockphoto.Burp.code_checkOut, "Price", stockphoto.Burp.currSc_Sum, "",
					/*B*/									   "Currency="+stockphoto.Burp.doubleQuote+stockphoto.Burp.currSc_SumCurrency+stockphoto.Burp.doubleQuote
					/*B*/									   +" NumImages="+stockphoto.Burp.doubleQuote+stockphoto.Burp.currSc.iMax+stockphoto.Burp.doubleQuote
					/*B*/									   +" bc="+stockphoto.Burp.doubleQuote+stockphoto.core.BoxCar.LangCountry.getBillingCountry()+stockphoto.Burp.doubleQuote );
					/*B*/		} catch(e) { ; }
					/*B*/	}
					// create a ScriptNode and make sure the bridge knows to display the online shopping cart
					// url when the ScriptNode is requested.
					var checkout = new Thumbnail("stockphoto://checkout");
					checkout.displayMode = "web";
//					browseSchama handler used to say....
//						if (stockphoto.browseScheme.sessionUrl != undefined)		// If we've registered the online shopping cart url, return it
//						{
//							checkout.displayPath = stockphoto.browseScheme.sessionUrl;
//						}
//						else														// If no remote shopping cart is registered return url to local cart
//						{
//							checkout.displayPath = stockphoto.core.folder.getMyShoppingCartFolder() + "/" + stockphoto.core.consts.ShoppingCartName;
//						}
					checkout.displayPath = unescape( event.url ); // new
					stockphoto.browseScheme.sessionUrl = event.url;
					app.document.thumbnail = checkout;
				}
				else
				{
					stockphoto.core.log.logString( 2,"Could not access the e-commerce server." );
					stockphoto.DL.alertBox(stockphoto.alertBox.eCommerceComponentID+9);/*ERROR_ECOM_ORDERSTATUS_ERROR*/
				}
			}
			else
			{
				stockphoto.core.log.logString( 2,"Could not access the e-commerce server." );
				stockphoto.DL.alertBox(stockphoto.alertBox.eCommerceComponentID+9);/*ERROR_ECOM_ORDERSTATUS_ERROR*/
			}
		}
		else if (event.eventName == "error")
		{
			stockphoto.core.log.logString( 2,"checkout returned errorCode = " + event.errorCode);
			stockphoto.DL.alertBox(event.errorCode);
			//Watson 1172062
			// Refresh shopping cart page
			stockphoto.checkout.RefreshShoppingCartPage(-1);
			stockphoto.core.checkProxyOnFail();
		}
	}
	
	return;
}

//======================
// checkoutLSCItems
//======================
stockphoto.checkout.checkoutLSCItems = function(args)
{
	var	bmLanguage = stockphoto.core.getValidLocale(true);
	var lc = bmLanguage.split("_");
	bmLanguage = lc[0] + "-" + lc[1];
	
	stockphoto.core.log.logString( 0,"stockphoto.checkout.CheckOutLSCItems" );
	stockphoto.shoppingCart.updateShoppingCart(args);
	
	var cartObjs = stockphoto.shoppingCart.getShoppingCartData();
	var checkoutList = {};
	var billingCountry = stockphoto.core.BoxCar.LangCountry.getBillingCountry();
	var items2CheckOut = 0;

	if( stockphoto.core.init.partnersXMLfsObj == undefined ) 
	{
		stockphoto.core.init.checkPartnersXML();
	}

	if( stockphoto.core.init.partnersXMLfsObj == undefined ) //ck 2nd time. If we fail to find partner XML just return.
	{
		return;
	}
	
	var xmlPath = Folder.decode(stockphoto.core.init.partnersXMLfsObj.fsName);  // 1121243 End
	// We are going to show eCom pages. Shopping cart html page will be unloaded.
	stockphoto.core.html.HtmlScriptReadyStatus = false;

	for (var i = 0; i < cartObjs.length; i++)
	{
		if (cartObjs[i].flag == stockphoto.addToCart.LSCKeywords.ItemRemoved)
		{
			continue;
		}

		var p = "SelectImage" + i;
		var priceNpurchaseID = new Array(2);

		if (args[p] == undefined)
		{
			continue;
		}
		var pIndex = args[p].indexOf("+");
		
		items2CheckOut ++;
		priceNpurchaseID[0] = unescape(args[p].slice(0, pIndex));
		priceNpurchaseID[1] = unescape(args[p].slice(pIndex + 1, args[p].length));

		for (var j = 0; j < cartObjs[i].resolutionXMLPurchaseID.length; j++)
		{
			if ((cartObjs[i].resolutionXMLPrice[j]      == priceNpurchaseID[0]) &&
			    (cartObjs[i].resolutionXMLPurchaseID[j] == priceNpurchaseID[1]))
			{
				checkoutList[i] =
				{
					Price:	cartObjs[i].resolutionXMLPrice[j],
					CurrencyType: 	cartObjs[i].selectedCurrency,
					Version:      	cartObjs[i].mediaXMLVersion,
					MediaID:      	cartObjs[i].mediaXMLMediaID,
					PurchaseID:  	cartObjs[i].resolutionXMLPurchaseID[j],
					PartnerID:   	cartObjs[i].mediaXMLPartnerID,
					MediaTitle:   	cartObjs[i].mediaXMLMediaTitle,
					Description:  	cartObjs[i].resolutionXMLDescription[j],
					LanguageID:   	cartObjs[i].mediaXMLLanguageID,
					Signature:   	cartObjs[i].signature[j],
					Algorithm:   	cartObjs[i].algorithm[j],
					ExpirationDate:	cartObjs[i].expirationDate[j],
					BillingCountry: billingCountry
				};
			}
		}	// for resolutionXMLPurchaseID.length
	} // for cartObjs.length

	if (items2CheckOut == 0)
	{
		stockphoto.DL.generalAlertBox(stockphoto.checkout.zc.get("$$$/Search/ErrorMessage/CheckoutListEmpty=Checkout list is empty."), 0);
		return;
	}

	var request = 
	{
		lineItems: checkoutList,
		uiLanguage: bmLanguage,
		partnersPath: xmlPath,
		eventHandler: stockphoto.checkout.eventHandler
	};

	// load shorter topnav bar
	stockphoto.core.NavBar.SwitchNavBar( "impl.checkout", 1, 0, app.document ); // "impl.checkout" must be all lower case
	stockphoto.core.SendProxyCredentials();	
	stockphoto.impl.checkout(request);
	
	return;
}

//======================
// ShowLicenseAgreement
//======================
stockphoto.checkout.ShowLicenseAgreement = function( imageIdList )
{
	var countryCode = stockphoto.prefs.default_countryCode;
	var languageCode = stockphoto.prefs.default_languageCode;
	var currLocale = stockphoto.core.getValidLocale(true);
	/*****Watson : 1321791*****/
	var bMissingURL = false;
	
	if (currLocale.length == 5)
	{
		languageCode = currLocale.substr(0,2);
		countryCode = currLocale.substr(3,2);
	}
	var urlParams = 'language=' + languageCode.toLowerCase() + '&country=' + countryCode.toLowerCase();
	
	var arrUniqueLicenseUrl = new Array;
	var uDupArrCount = 0;
	var uUniqueArrayCount = 0;
	var oThumbObj;
	var sLicenseURL;
	var oMetadata;
	var uSelectedObjCount;
	var bUseSelectedThumbs = (imageIdList == undefined);
	
	if (bUseSelectedThumbs)
	{
		uSelectedObjCount = app.document.selections.length;
	}
	else
	{
		uSelectedObjCount = imageIdList.length;
	}
	for(uDupArrCount=0; uDupArrCount < uSelectedObjCount; uDupArrCount++)
	{
		if (bUseSelectedThumbs)
		{
			oThumbObj = app.document.selections[uDupArrCount];	
		}
		else
		{
			if ("undefined" != typeof imageIdList[uDupArrCount])
			{
                var sDecodedFilename = File.decode(imageIdList[uDupArrCount]);
                if ("undefined" != typeof sDecodedFilename)
                {
                    var oFile = new File(sDecodedFilename);
                    if ("undefined" != typeof oFile)
                    {
                        if (oFile.exists)
                        {
                            oThumbObj = new Thumbnail(oFile);
                        }
                        
                        delete oFile;
                    }
                }
			}
		}

		if ("undefined" != typeof oThumbObj)
		{
			oThumbObj.verifyExternalChanges();
			oThumbObj.refresh("metadata"); // Request metadata refresh before we use it
			oMetadata = oThumbObj.metadata;
			
			if ("undefined" == typeof oMetadata)  // If we didn't get direct metadata, use sync
			{
                oMetadata = oThumbObj.synchronousMetadata;
            }
		}
		
		if (("undefined" != typeof oThumbObj) && stockphoto.core.isImageFile(oThumbObj)) 
        { 
			if (oMetadata != undefined)
			{
				oMetadata.namespace = stockphoto.core.stockphotoNS;
				
				sLicenseURL = oMetadata["Attribute/bmsp:License"];
				
				// <TODO> We should decide upon and display an error message if the image does not contain valid License URL Info..
				if ("undefined" != typeof sLicenseURL)
				{
				    sLicenseURL =  stockphoto.core.trimString(sLicenseURL); // trimString never returns undefined object
				    
				    if ("" != sLicenseURL) // skip blank URLs
				    {
        				if (sLicenseURL.indexOf("CheckoutDate") >=0)
        				{	// Purchased images
        					sLicenseURL = sLicenseURL + "&" + urlParams;
        				}
        				else
        				{	// Comp images
        					sLicenseURL = sLicenseURL + "?" + urlParams;
        				}
        				
        				for (uUniqueArrayCount=0;(uUniqueArrayCount < arrUniqueLicenseUrl.length) && (sLicenseURL != arrUniqueLicenseUrl[uUniqueArrayCount]) ; )
        				{
                            uUniqueArrayCount++;
                        }
        				
        				if (uUniqueArrayCount == arrUniqueLicenseUrl.length)
        				{
        					arrUniqueLicenseUrl[arrUniqueLicenseUrl.length] = sLicenseURL;
        					stockphoto.core.html.openInDeafaultBrowser(arrUniqueLicenseUrl[uUniqueArrayCount])
        				}
        			} // end - if (sLicenseURL != "")
        			else
        			{
        			   /*****Watson : 1321791*****/	
        			   bMissingURL = true;
        			}
    			} // end - if (sLicenseURL != undefined)
    			else
    			{
    				/*****Watson : 1321791*****/
    				bMissingURL = true;
    			}
			} // end - if (oMetadata != undefined)
		}
		if (!bUseSelectedThumbs)
		{
			delete oThumbObj;
		}
		
	}
	
	/*****Watson : 1321791*****/
	if ( true == bMissingURL)
	{
		var msg = stockphoto.checkout.zc.get("$$$/Search/ErrorMessage/NoLicenseURL=One or more images do not contain Adobe Stock Photos licensing information.");
		
		stockphoto.DL.generalAlertBox(msg, 1); 
	}
	return;
}

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

