var win;
var systemMessage  = new SystemMessage();

Ext.onReady(function(){
	systemMessage.setMessageContainer("plansValidation");
	initFeatureWin();
});
//credit card exist status will be initialize by controller and allocated in the view page plans
var selectedPlanId = -1;
function preRequestProfileChange(id, name, currentProfileId, oldName){
	selectedPlanId = id;
	if(currentProfileId != "" && selectedPlanId > currentProfileId){		
		Ext.MessageBox.confirm('Upgrade Plan', 'By upgrading your plan, you accept the higher subscription charge to be applied, to your account, at the end of the calendar month. Do you wish to upgrade your account to ' + name + '?', function(btn){
	        if (btn == 'yes') {
				//if(creditcard=='false') { //if 
				//	window.location = ADDRESS_SSL + 'app/billing/payment.html?id=' + selectedPlanId;
	        	//} else { //else we got credit card lets change the plan
					requestProfileChange(selectedPlanId, true, name, oldName, function(result){
						if (result.success == true) {
							//systemMessage.displayInfoMessage(result.msg);
							window.location = ADDRESS_SSL + 'app/account/plans.html'; //lets reload the page so we can see changes
							//window.location = APP + '/campaign/index.html'
						} else {
							systemMessage.displayErrorMessage(result.msg);
						}
					});		
				//}
	        }
	    });
	}else if(currentProfileId != "" && selectedPlanId < currentProfileId){		
		Ext.MessageBox.confirm('Downgrade Plan', 'By downgrading your plan, you accept the higher subscription charge to be applied, to your account, at the end of the calendar month. Do you wish to downgrade your account to ' + name + '?', function(btn){
	        if (btn == 'yes') {
				requestProfileChange(selectedPlanId, false, name, oldName, function(result){
					if (result.success == true) {
						window.location = ADDRESS_SSL + 'app/account/plans.html'; //lets reload the page so we can see changes
					} else {
						systemMessage.displayErrorMessage(result.msg);
					}
				});		
	        }
	    });
	}else if(currentProfileId == ""){
		window.location = ADDRESS_SSL + 'app/billing/payment.html?id=' + selectedPlanId;
	}
	
    /*Ext.MessageBox.confirm('Update Account', 'Are you sure you wish to update your account to ' + name + '?', function(btn){
        if (btn == 'yes') {
        	//if(acst == 't' || acst == 'TESTING') { //if we don't have credit card redirect to the credit card page
			if(creditcard=='false') { //if 
				window.location = ADDRESS_SSL + 'app/billing/payment.html?id=' + selectedPlanId;
        	} else { //else we got credit card lets change the plan
				requestProfileChange(selectedPlanId, function(result){
					if (result.success == true) {
						//systemMessage.displayInfoMessage(result.msg);
						window.location = ADDRESS_SSL + 'app/account/plans.html'; //lets reload the page so we can see changes
						//window.location = APP + '/campaign/index.html'
					} else {
						systemMessage.displayErrorMessage(result.msg);
					}
				});		
			}
        }
    });*/
}

// ### Feature Help Panel ###

function processResult(btn){
	if (btn == 'yes') {
    	//if(acst == 't' || acst == 'TESTING') { //if we don't have credit card redirect to the credit card page
		if(creditcard=='false') { //if 
			window.location = ADDRESS_SSL + 'app/billing/payment.html?id=' + selectedPlanId;
    	} else { //else we got credit card lets change the plan
			requestProfileChange(selectedPlanId, function(result){
				if (result.success == true) {
					//systemMessage.displayInfoMessage(result.msg);
					window.location = ADDRESS_SSL + 'app/account/plans.html'; //lets reload the page so we can see changes
					//window.location = APP + '/campaign/index.html'
				} else {
					systemMessage.displayErrorMessage(result.msg);
				}
			});		
		}
    }
}

var featureWin;
var featureContent;
var openedPanel;
var panels = new Object
({
	//'monthly-free': null, 
	'sms-free': null, 
	'sms-cost': null, 
	'monthly-cost': null, 
	'w2m-included': null, 
	'w2m-extra': null, 
	'broadcast': null, 
	'w2m': null, 
	'reporting-basic': null, 
	'mobile-site': null,
	'free-trial-subscription': null,
	'w2m-branding': null,
	'broadcast-fee': null
	//'reporting-advanced': null,
	//'data-charges': null
});
var itemsPanels;

function initFeatureWin()
{
	var button = Ext.get('show-btn');

	var createPanel;
	itemsPanels = [];
	for (var panel in panels)
	{
		createPanel = new Ext.Panel({contentEl: panel, layout: "fit", hidden: true, height: "100%" });
		itemsPanels.push(createPanel);
		panels[panel] = createPanel;
	}	
}

function createFeatureWin()
{
	var nav = new Ext.Panel
	({
        title: 'Features',
		contentEl: "features-nav",
        region: 'west',
        split: true,
        width: 200,
        collapsible: true,
        margins:'3 0 3 3',
        cmargins:'3 3 3 3'
    });
    featureContent = new Ext.Panel
	({
        region: 'center',
        split: true,
		layout: "fit",
		width: "100%",
		bodyStyle: 'padding: 15px',
		extraCls: "featuresBody",
		defaults: { border: false },			
        collapsible: false,
        margins:'3 0 3 3',
        cmargins:'3 3 3 3',
		items: itemsPanels
    });
    featureWin = new Ext.Window
	({
        title: 'Feature Information',
        closable: true,
        width:600,
        height:360,
        plain:true,
        layout: 'border',
		closeAction: "hide",

        items: [nav, featureContent]
    });
	
	itemsPanels = null;
    
}

function showFeatureWin(sourceEl)
{
	if (featureWin == null) { createFeatureWin(); }
	
	if (sourceEl != null)
	{
		var sourceId = Ext.get(sourceEl);
		featureWin.setAnimateTarget(sourceId);
	}
	
	if (!featureWin.isVisible()) { featureWin.show(); }
}

function openPanel(newPanel, sourceEl)
{
	if (newPanel != openedPanel)
	{
		if (openedPanel != null) { panels[openedPanel].hide(); }
		panels[newPanel].show();
		openedPanel = newPanel;
	}
	showFeatureWin(sourceEl);	
}