/* Copyright 2009 Palm, Inc.  All rights reserved. */

var PaymentSetupAssistant = Class.create(
{	
    initialize: function(appid, onComplete)
	{
		this._appid = appid;
		this._onComplete = onComplete;
	},
	
    setup: function()
	{
		this._addedPrefs = this.controller.document.body.hasClassName("prefs") || this.controller.document.body.addClassName("prefs");
		this.controller.setupWidget("setupButton", {disabled:false,type:"default"},{buttonLabel:$L("Continue"), buttonClass:"primary", disabled:false});
        this.controller.listen("setupButton", Mojo.Event.tap, this._getPassword.bind(this));
		
		this.createAccount = this.createAccount.bind(this);
	},
	
	cleanup: function()
	{
		this._addedPrefs !== true && this.controller.document.body.removeClassName("prefs");
	},
	
	_getPassword: function() 
	{
		if (Weave.Services.ConnectionManager.isOnline() === false) 
		{
			//Weave.Services.ConnectionManager.showConnectionError();
		} 
		else 
		{
			var self = this;
			this.controller.showDialog(
			{
				template: 'payment-setup/password-dialog',
				assistant: new PasswordAssistant(this, 
				{
					appid: this._appid, 
					onComplete: function(ret)
					{
						if (ret.passwordValid) 
						{
							self.createAccount();
							//self.controller.stageController.swapScene("create-account", { appid: self._appid, onComplete: self._onComplete });
						}
					}
				})
			});
		}	
	},
	createAccount: function(){
		var self = this;
		this.controller.showAlertDialog({
			onChoose: function(value){
				switch (value) 
				{
					case "ok":
						this.controller.stageController.swapScene('create-account', { appid: self._appid, onComplete: self._onComplete });
						break;
						
					case "change":
						this.controller.stageController.swapScene('change-email-address', { appid: self._appid, onComplete: self._onComplete });
						break;
						
					default:
						break;
				}
			},
			title: $L("Verify Email Address"),
			message: $L("When you purchase items, we'll send receipts to #{email}.").interpolate(myProfile),
			choices: [{
				label: $L("OK"),
				value: "ok",
				type: "primary"
			}, {
				label: $L('Change Email Address'),
				value: "change",
				type: "secondary"
			}, ],
		});
	},
});