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

// eCom namespace and zstringCache instance
stockphoto.eCom = {};

stockphoto.st07_eCom_EventHandler = function(event)
{
    var bBringupDLDialog = false;
    
	stockphoto.core.log.logString( 0,event.eventClass + ":" + event.eventName );
	if (event.eventName == "complete")
	{
		downloadListFile = File(this.orderStatusFile);
		if( stockphoto.Burp.doIt ) // in stockphoto.core.html.doPhotoDirInDefaultBrowser
		{
		/*B*/	try
		/*B*/	{
		/*B*/		stockphoto.Burp.checkOutComplete( app.document.id, this.orderStatusFile ); 
		/*B*/	} catch(e) { ; }
		}
		
        /****** Watson: 1322547 **********/
		bBringupDLDialog = stockphoto.checkout.DLLAddItem(downloadListFile, this.doc_id, true);
		stockphoto.st07_eCom_HandleQuit(false);	// either quit, or clear the flags as appropriate

		// bring up DL dialog and if prefs say so, begin the DL - rwe
		if (true == bBringupDLDialog) // only if we had at least one item to download
        {
            stockphoto.DL.Download.DownloadStock(true, stockphoto.core.ui.getAlertDisplayPrefs("AutoDownloadImagesAfterCheckout"),this.doc_id,"dl");
        }
		downloadListFile.remove();
		if( stockphoto.Burp.doIt ) // in stockphoto.core.html.doPhotoDirInDefaultBrowser
		{
		/*B*/	try
		/*B*/	{
		/*B*/		stockphoto.Burp.checkOutClear();
		/*B*/	} catch(e) { ; }
		}

	}
}
stockphoto.st07_eCom=function( args ) { // args.doc_id is added on 11/7
	var sptriggerString;
	var cmdStr;
	var doc = stockphoto.core.findDoc( args.doc_id );

  	// args.sptrigger =
  	//	"complete"		-	the user 's credit card has been build. call checkOrderStatus, but don't navigate elsewhere
  	//	"download"		-	user hit :begin Download" link on thank-you page. Bring up the Download manager
  	//	"resume"		-	user hit the "home" button. nav to ???
  	//	"downloaditem"	-	user hit a Download linki for an individual item in history. Create a DLL item and add it to the DL queue.
  	//	"youraccountresume"	-	user hit 'Continue Browsing' from the Sign In page to 'Your Account'. nav to ???
  	//	"cancel"		-	user hit "Cancel" button on any page. Nav to ???
	//	"home"			-	To display Stock Photos Home page
	//	"help"			-	To display Kona
	//  "images"        -   To go to "Purchased images" folder
	//  "update"		-   Fire the Adobe Updater (bridge)
	
	if( args.sptrigger == undefined )
	{
	     sptriggerString = "";
	}
	else
	{
		sptriggerString = ( args.sptrigger ).toLowerCase();
	}

	stockphoto.core.log.logString( 0, "st07: sptrigger = " + sptriggerString);
	if (sptriggerString == "images")
	{
		app.scheduleTask("stockphoto.MiscDlgs.GotoFolder(3,"+args.doc_id+");", 1000, false );
	}
	else if (sptriggerString == "complete")
	{
		stockphoto.st07_eCom_CheckOrderStatus(false);
	}
	else if (sptriggerString == "download")
	{
//		stockphoto.DL.Download.DownloadStock(false, false, args.doc_id);						// bring up download image(s) dialog
		stockphoto.DL.Download.DownloadStock(false, false, args.doc_id, "dl");						// bring up download image(s) dialog
		stockphoto.core.BoxCar.NavBar.SetInECOMFlag( true, args.doc_id );
	}
	else if (sptriggerString == "home") {
		stockphoto.displayStartPage();
	}
	else if (sptriggerString == "help") {
        stockphoto.DL.MiscDlgs.HelpTopic( stockphoto.core.helpTopicBase );
	}
	else if (sptriggerString == "resume"){
		if( stockphoto.DL.demandJsxLoading( stockphoto.DL.jsx_CheckOut ) ) {
			if( stockphoto.DL.demandJsxLoading( stockphoto.DL.jsx_MiscDlgs ) )
				stockphoto.MiscDlgs.GotoFolder( 1, args.doc_id ); // nav to Recent Searches folder
			else
				stockphoto.displayStartPage();
		};
	}
	else if (sptriggerString == "cancel"){
		stockphoto.DL.ShoppingCart.ShoppingCartX(args.doc_id);
	}
	else if( sptriggerString == "downloaditem" ) {
	    /*********** Watson: 1172167 **********/
		var bResult = stockphoto.st07_AddItemToDownloadList(args);
		// if there is no item to download then do not show download images dialog box.
		if (true == bResult)
		{
			stockphoto.DL.Download.DownloadStock(false, false, args.doc_id, "re-dl");					// bring up download image(s) dialog
		}
	}
	else if(sptriggerString == "print")
	{
		doc.print();
	}
	else if (sptriggerString == "update")
	{
		// Make Bridge Launch the Updater. We got this
		// code from the Bridge Prople (October 2005)				
		app.bringToFront();
		app.document.chooseMenuItem("Updates");
	}

	// restore the screen
}
stockphoto.st07_AddItemToDownloadList = function(args)
{
	// Partner name returned from DR isn't necessarily the same as the one
	// returned from the partner.

   /******** Begin Watson: 1172167 ********/
	var partnerName = stockphoto.partnerEComIDToPartnerName(args.PartnerID);
	if (partnerName != undefined)
	{
		args.PartnerID = partnerName;

		var tf = new File(stockphoto.core.folder.getTempFolder() + "/tempitemlist.tmp");
		tf.open("w");
		tf.writeln("itemCount=1");
		tf.writeln("beginItem");
		for (i in args){
			// some items should be written as-is. Don't unescape these items
			if (i == "Price"
			||  i == "MediaID"
			||	i == "PurchaseID"
			||	i == "CurrencyType"
			||	i == "Signature")
			{
				tf.writeln(i + "=" + args[i]);
			}
			else
			{
				if (i == "MediaTitle")
				{
	                // Make sure that MediaTitle element is a string
	                // (if it's just '12345' in the Re-Download URL, ES treats it as an integer and fails on .split)
	                args[i] = '' + args[i];
	                
					var mt = "";
					var wrds = args[i].split("+");
					for (var j in wrds)
					{
						mt += wrds[j] + " ";
					}

					tf.writeln(i + "=" + unescape(mt));
				}
				else
				{
					tf.writeln(i + "=" + unescape(args[i]));
				}
			}

		}
		tf.writeln("CheckoutStatus=COMPLETE");
		tf.writeln("endItem");
		tf.close();
		stockphoto.DL.demandJsxLoading( stockphoto.DL.jsx_CheckOut );

        /****** Watson: 1322547 **********/
        // Do not clear shopping cart
		stockphoto.checkout.DLLAddItem(tf, args.doc_id, false);
		tf.remove();

		   
		return true;
	}
	
	return false;
	/******** End Watson: 1172167 ********/	
}

stockphoto.st07_eCom_HandleQuit = function(async)
{
	stockphoto.core.log.logString( 0, "st07_eCom_HandleQuit() called" );

	if (!async)
	{
		if (stockphoto.core.quitTaskID != undefined)				// if we are already scheduled,
		{
			app.cancelTask(stockphoto.core.quitTaskID, false);		// remove the existing task from the queue
			stockphoto.core.quitTaskID = undefined;
		}
	}

	stockphoto.core.dontQuit = false;	// quit happens!

	// if someone tried quitting during the critical section, do so now.
	if (stockphoto.core.quitRequested)
	{
		app.quit();
	}
}
	
stockphoto.st07_eCom_CheckOrderStatus = function(async)
{
	var billingCntry;

	if (!async)
	{
		if (stockphoto.core.orderTaskID != undefined)				// if we are already scheduled,
		{
			app.cancelTask(stockphoto.core.orderTaskID, false);		// remove the existing task from the queue
			stockphoto.core.orderTaskID = undefined;
		}
	}

	if (app.preferences.stockphotoOrderId != "done") 				// if this is the first pass on this orderID
	{
		var f = new File(stockphoto.core.folder.getMyShoppingCartFolder() + "/olderID.txt");
		var xmlPath;

		if( stockphoto.core.init.partnersXMLfsObj == undefined )
		{
			stockphoto.core.init.checkPartnersXML();
		}
		xmlPath = Folder.decode(stockphoto.core.init.partnersXMLfsObj.fsName);

		billingCntry = stockphoto.core.BoxCar.LangCountry.getBillingCountry();
		
		var request =
		{
			orderStatusFile: f.fsName,
			orderID: app.preferences.stockphotoOrderId,
			partnersPath: xmlPath,
			billingCountry: billingCntry,
			doc_id: args.doc_id,
			eventHandler: stockphoto.st07_eCom_EventHandler
		};

		stockphoto.core.SendProxyCredentials();
		stockphoto.impl.orderstatus(request);
		stockphoto.core.BoxCar.NavBar.SetInECOMFlag( false, args.doc_id );

		// "de-bounce" this trigger. Hitting the back button from view receipt page sends us this
		// trigger even though it has already been processed for the given orderID. - rwe
		app.preferences.stockphotoOrderId = "done";
	}
}

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

