// JScript source code
/* 
	ProcessData() Structure start 
	This structure will hold the data for the template string 
*/

function ProcessData()
{
	var i=0;
	
	this.DIV_PRICE_CSS = arguments[i++];	
	this.SPAN_PRICE_CSS = arguments[i++];
	this.SUBSCRIPTION_PRICE_PER_MONTH = arguments[i++];	
	this.SUBSCRIPTION_PRICE_PER_PERIOD = arguments[i++];
	this.SAVE_MONEY = arguments[i++];		
}
/* ProcessData() Structure end */

function PeriodFormattedPrices(lowPricePerMonth,highPricePerMonth,lowPricePerPeriod,highPricePerPeriod)
{
	var discount = IsDiscount();
			
	if ( discount )
	{
		this.m_pricePerMonth = lowPricePerMonth;
		this.m_pricePerPeriod = lowPricePerPeriod;		
	}
	else
	{
		this.m_pricePerMonth = highPricePerMonth;
		this.m_pricePerPeriod = highPricePerPeriod;		
	}	
}

function Savings(quarterSaveLow, quarterSaveHigh, yearSaveLow ,yearSaveHigh)
{
	var discount = IsDiscount();
			
	if ( discount )
	{		
		this.m_quarterSave = quarterSaveLow;
		this.m_yearSave = yearSaveLow;
	}
	else
	{		
		this.m_quarterSave = quarterSaveHigh;
		this.m_yearSave = yearSaveHigh;
	}	
}

function SkuData()
{
	var i=0;
		
	this.m_price = arguments[i++];
	this.m_allPeriodPrice = arguments[i++];
	this.m_period = arguments[i++];
}

function SubscriptionTypeSkues()
{
	var i=0;	
	this.m_skuArr = new Array(arguments[i++],arguments[i++]);
	this.m_skuType = arguments[i++]; 
}

function SkuDataManager() 
{	
	this.m_skuDataArr = new Array();
	this.m_skuDataArr["114881957"] = new SkuData("&pound;6", "&pound;72", "12" );  // discounted price yearly
	this.m_skuDataArr["114878783"] = new SkuData("&pound;6", "&pound;72", "12" );  // normal price yearly
	this.m_skuDataArr["114880673"] = new SkuData("&pound;7", "&pound;21", "3" );   // discounted price quarterly
	this.m_skuDataArr["114877830"] = new SkuData("&pound;8", "&pound;24", "3" );   // normal price quarterly
	this.m_skuDataArr["114879140"] = new SkuData("&pound;5", "&pound;5", "1" );    // discounted price monthly
	this.m_skuDataArr["114876657"] = new SkuData("&pound;10", "&pound;10", "1" );  // normal price monthly
	
	this.m_pricesArr = new Array();
	//   (period discounted price for month, period normal price per month, period discounted price, period normal price)
	this.m_pricesArr["YEAR"] = new PeriodFormattedPrices("&pound;6","&pound;6","&pound;72","&pound;72");
	this.m_pricesArr["QUARTER"] = new PeriodFormattedPrices("&pound;8","&pound;8","&pound;24","&pound;24");
	this.m_pricesArr["MONTH"] = new PeriodFormattedPrices("&pound;5","&pound;10","&pound;5","&pound;10");
}

SkuDataManager.prototype.GetAllPeriodPrice = function(strSku)
{
	return this.m_skuDataArr[strSku].m_allPeriodPrice;
}

SkuDataManager.prototype.GetOneMonthPrice = function(strSku)
{
	return this.m_skuDataArr[strSku].m_price;
}

SkuDataManager.prototype.GetPeriod = function(strSku)
{
	return this.m_skuDataArr[strSku].m_period;
}

function IsDiscount() 
{	
	var d = (Url.here.params.d == "417693F3-45AF-47d6-917D-F59A7D5D788C" ? true : false);	
	return d;
}



function SubscriptionManager()
{	
	this.m_request = null;	
	this.YEARLY = "YEARLY";
	this.QUARTERLY = "QUARTERLY";
	this.MONTHLY = "MONTHLY";
	// (discounted price, normal price, sku type)
	var yearlySkues = new SubscriptionTypeSkues(114881957,114878783,this.YEARLY);
	var quarterlySkues = new SubscriptionTypeSkues(114880673 ,114877830,this.QUARTERLY); 
	var monthlySkues = new SubscriptionTypeSkues(114879140,114876657,this.MONTHLY); 
	this.m_subscriptionTypesArr = new Array(yearlySkues, quarterlySkues, monthlySkues);	
	this.m_skuDataManager = new SkuDataManager();
	this.m_processDataArr = null;	
	this.InitUserSubscriptionData();
	
}

// Is user logged on
SubscriptionManager.prototype.IsLoggedOn = function()
{
	return UA.User.prototype.IsLoggedOn();
}

// This function initialize all User subscription data 
SubscriptionManager.prototype.InitUserSubscriptionData = function()
{	
	
	if ( this.IsLoggedOn() )
	{ 		
		var methodsArr = new Array( UA.User.Methods.GETPACKAGEEXPIRATIONUTCDATE,
									UA.User.Methods.GETCURRENTPERMITTEDSKUS,
									UA.User.Methods.GETPLANNEDPERMITTEDSKUS);
		UA.User.prototype.GetData(methodsArr, null, GetHarpoonData_OnSuccess, GetHarpoonData_OnFail, OnTimeout);
	}
}

// We get to this function after a successful data retrieving
function GetHarpoonData_OnSuccess(request)
{	
	subscriptionManager.m_request = request;			
}

// We get to this function after an error in retrieving the data
function GetHarpoonData_OnFail(request)
{	
	window.location.href = "/error.htm?errorType=99&target=f";
}

// We get to this function after a timeout error
function OnTimeout(request)
{	
	window.location.href = "/error.htm?errorType=99&target=t";
}

// this function is used to determine if the user is subscribed
SubscriptionManager.prototype.IsUserSubscribed = function()
{	
	if ( !this.IsLoggedOn() )
		return false;
		
	data = this.m_request[UA.User.Methods.GETCURRENTPERMITTEDSKUS].Data.currentPermittedSKUs;	
	for(keySku in data)
	{
		return true;
	}
	return false;
}

SubscriptionManager.prototype.IsReady = function()
{
	if ( !this.IsLoggedOn() )
		return true;
		
	if ( this.m_request == null )
	{
		return false;
	}
	return true;
	
}

SubscriptionManager.prototype.GetTemplateString = function(oSkus)
{
	var textAreaId;
	
	switch (oSkus.m_skuType)
	{
		case this.YEARLY : 
				var discountTemplate = ( IsDiscount() ? "Discount" : "" );
				textAreaId = "txtYear" + discountTemplate ;	
				break;
		case this.QUARTERLY : 
				textAreaId = "txtQuarter"; 
				break;											 
		case this.MONTHLY : 
				var discountTemplate = ( IsDiscount() ? "Discount" : "" );
				textAreaId = "txtMonth" + discountTemplate ;			 
				break;
		default: return null;
	}
	
	var elem = document.getElementById(textAreaId);
	if ( elem == null )
		return null;
	return elem.value;				
}

function BtnJoinUsOnClick()
{	
	var generalVars = new GeneralVars();		
	window.location.href = generalVars.adapterURL + '&sender=GC&cmd=1';					
}


SubscriptionManager.prototype.CreateProcessDataFactory = function()
{
	var isUserSubscribed = this.IsUserSubscribed();	
	var monthlyData; 
	var quarterlyData;
	var monthlyData;
	var objectIds4InnerHTML;
	var oSavings = null;
		
	if ( isUserSubscribed )
	{				
		var currentSubscriptionType = this.GetUserSubscriptionType();
		var suffixStr;		
		
		switch ( currentSubscriptionType )
		{			
			case this.YEARLY: 				
				suffixStr = "CurrentYear";
				// user has yearly package. He saves nothing when moving to another package.
				oSavings = new Savings("","","","");															
				break;
			case this.QUARTERLY:				
				suffixStr = "CurrentQuarter";
				//  ( discounted quarterly save, normal quarterly save, discounted yearly save, normal yearly save )
				//oSavings = new Savings("","","SAVE &pound;63! ","SAVE &pound;81! ");
				oSavings = new Savings("","","","");
				break;
			case this.MONTHLY: 
				suffixStr = "CurrentMonth";
				//  ( discounted quarterly save, normal quarterly save, discounted yearly save, normal yearly save )
				//oSavings = new Savings("SAVE &pound;36! ","SAVE &pound;36! ","SAVE &pound;60! ","SAVE &pound;60! ");
				oSavings = new Savings("","","SAVE &pound;60*! ","SAVE &pound;48*! ");
				break;
			default:
				//window.location.href = "/error.htm?errorType=60&target=f1";
				str = "<div class='subscriptionTextPlace10'>Thank you for subscribing to Sky Premium Games Pack!</div><br/><div class='subscriptionTextPlace8'>Unfortunately you are not able to change your subscription until the end of the period of your current subscription.</div>" +
				"<br/><div class='subscriptionTextPlace15' >If you feel none of our packages are right for you <a href='/ConfirmCancellation.aspx' class='regLink2'>click here</a> to cancel your subscription.  Please <a onclick='popSupport();' class='regLink2 cursorHand'>contact us</a> if you are having difficulties with any aspect of Sky Games, and we will do our utmost to resolve your issue.</div>";
				$("oMessage").innerHTML = str;
				return;
				
		}
		
		monthlyData = new ProcessData("subscriptionTextPlace9","subscriptionTextPlace10",this.m_skuDataManager.m_pricesArr["MONTH"].m_pricePerMonth, null);
		quarterlyData = new ProcessData("subscriptionTextPlace9","subscriptionTextPlace10",this.m_skuDataManager.m_pricesArr["QUARTER"].m_pricePerMonth,this.m_skuDataManager.m_pricesArr["QUARTER"].m_pricePerPeriod,oSavings.m_quarterSave);
		yearlyData = new ProcessData("subscriptionTextPlace4","subscriptionTextPlace8",this.m_skuDataManager.m_pricesArr["YEAR"].m_pricePerMonth,this.m_skuDataManager.m_pricesArr["YEAR"].m_pricePerPeriod, oSavings.m_yearSave);		
		
		this.m_objectIds4InnerHTMLArr = new Array();
		this.m_objectIds4InnerHTMLArr["CONTAINER"] = "container" + suffixStr;
		this.m_objectIds4InnerHTMLArr[this.YEARLY] = "tdYear" + suffixStr;
		this.m_objectIds4InnerHTMLArr[this.QUARTERLY] = "tdQuarter" + suffixStr;
		this.m_objectIds4InnerHTMLArr[this.MONTHLY] = "tdMonth" + suffixStr;
		
	}
	else
	{
		//  ( discounted quarterly save, normal quarterly save, discounted yearly save, normal yearly save )
		//oSavings = new Savings("SAVE &pound;36! ","SAVE &pound;36! ","SAVE &pound;60! ","SAVE &pound;60! ");
		oSavings = new Savings("","","SAVE &pound;60*! ","SAVE &pound;48*! ");
						
		monthlyData = new ProcessData("subscriptionTextPlace9","subscriptionTextPlace10",this.m_skuDataManager.m_pricesArr["MONTH"].m_pricePerMonth,null);
		quarterlyData = new ProcessData("subscriptionTextPlace9","subscriptionTextPlace10",this.m_skuDataManager.m_pricesArr["QUARTER"].m_pricePerMonth,this.m_skuDataManager.m_pricesArr["QUARTER"].m_pricePerPeriod, oSavings.m_quarterSave);
		yearlyData = new ProcessData("subscriptionTextPlace4","subscriptionTextPlace8",this.m_skuDataManager.m_pricesArr["YEAR"].m_pricePerMonth,this.m_skuDataManager.m_pricesArr["YEAR"].m_pricePerPeriod, oSavings.m_yearSave);
					
		this.m_objectIds4InnerHTMLArr = new Array();
		this.m_objectIds4InnerHTMLArr["CONTAINER"] = "";
		this.m_objectIds4InnerHTMLArr[this.YEARLY] = "tdYear";
		this.m_objectIds4InnerHTMLArr[this.QUARTERLY] = "tdQuarter";
		this.m_objectIds4InnerHTMLArr[this.MONTHLY] = "tdMonth";
	}
	
	this.m_processDataArr = new Array();
	this.m_processDataArr[this.YEARLY] = yearlyData;
	this.m_processDataArr[this.QUARTERLY] = quarterlyData;
	this.m_processDataArr[this.MONTHLY] = monthlyData;	
	
	var divContainer = document.getElementById("ContainerHide4Discount");
	if ( divContainer != null )
	{
		if ( IsDiscount() )
		{		
			// if user is sky member than we should not show quarterly and yearly prices
			divContainer.className = "noDisplay";
		}
		else
		{
			divContainer.className = "defaultDisplay";
		}
	}
	
		
}


SubscriptionManager.prototype.GetUserSubscriptionType = function()
{
	if ( !this.IsLoggedOn() )
		return "ERROR";
				
	data = this.m_request[UA.User.Methods.GETCURRENTPERMITTEDSKUS].Data.currentPermittedSKUs;	
	
	var packageSku;
	for(keySku in data)
	{		
		packageSku = keySku;	
	}
	
	for ( i=0; i < this.m_subscriptionTypesArr.length; i++ )
	{ 
		for ( j=0; j< this.m_subscriptionTypesArr[i].m_skuArr.length; j++ )
		{			
			// find user pacage unless the user has the 1 month discounted price
			if ( packageSku ==  this.m_subscriptionTypesArr[i].m_skuArr[j].toString() && packageSku != "114879140" )
				return this.m_subscriptionTypesArr[i].m_skuType;
		}
	}
	return "ERROR";
}

SubscriptionManager.prototype.SetUserPackages = function()
{	
	var processData;
	var strTemplate;
	var tdElem;		
			
	this.CreateProcessDataFactory();
	
	if ((this.IsUserSubscribed() && "ERROR" == this.GetUserSubscriptionType()))
	{
		return;
	}
	
	this.SetCurrentContainer();
	
	for ( i=0; i < this.m_subscriptionTypesArr.length; i++ )
	{		
		processData = this.GetMapping( this.m_subscriptionTypesArr[i] );
		strTemplate = this.GetTemplateString( this.m_subscriptionTypesArr[i] );
		if ( strTemplate != null )
		{ 
			strTemplate = strTemplate.populateTemplate( processData );			
			tdElem = this.GetObject4InnerHTML( this.m_subscriptionTypesArr[i] );
			if ( tdElem != null )
				tdElem.innerHTML = strTemplate;
		}
	}
	this.SetBottomText();
}

SubscriptionManager.prototype.SetBottomText = function()
{	
	if ( this.m_processDataArr == null || this.m_processDataArr[this.MONTHLY] == null ) 
	{
		this.CreateProcessDataFactory();
	}
	var processData = this.m_processDataArr[this.MONTHLY];	
	var bottomTextID = ( IsDiscount() ? "txtBottomTextDiscount" : "txtBottomText" );
	
	var oTextArea = document.getElementById(bottomTextID);
	if ( oTextArea == null )
		return;	
	var strTemplate = oTextArea.value;
	
	strTemplate = strTemplate.populateTemplate( processData );
	var oDiv = document.getElementById("divBottomText");
	if ( oDiv == null )
		return;	
	oDiv.innerHTML = strTemplate;	
}

SubscriptionManager.prototype.GetMapping = function(oSkus)
{
	return this.m_processDataArr[oSkus.m_skuType]; 
}

SubscriptionManager.prototype.SetCurrentContainer = function()
{	
	var currentContainerId = this.m_objectIds4InnerHTMLArr["CONTAINER"];
	if ( currentContainerId == null || currentContainerId == "" )
		return;
	
	var containerId1;
	var containerId2;
	
	switch ( currentContainerId )
	{
		case "containerCurrentYear":
			containerId1 = "containerCurrentQuarter";
			containerId2 = "containerCurrentMonth";
			break;
		case "containerCurrentQuarter":
			containerId1 = "containerCurrentYear";
			containerId2 = "containerCurrentMonth";
			break;
		case "containerCurrentMonth":
			containerId1 = "containerCurrentYear";
			containerId2 = "containerCurrentQuarter";
			break;
		default:
		break;
	}
	
	var container1 = document.getElementById(containerId1);
	if ( container1 == null )
	{
		window.location.href = "/error.htm?errorType=99&target=f2";
		return;
	}
	container1.outerHTML = "";
	var container2 = document.getElementById(containerId2);
	if ( container2 == null )
	{
		window.location.href = "/error.htm?errorType=99&target=f2";
		return;
	}
	
	container2.outerHTML = "";
	var currentContainer = document.getElementById(currentContainerId);
	if ( currentContainer == null )
	{
		window.location.href = "/error.htm?errorType=99&target=f2";
		return;
	}
	currentContainer.className = "defaultDisplay";
	
}

SubscriptionManager.prototype.GetObject4InnerHTML = function(oSkus)
{
	var elem = document.getElementById(this.m_objectIds4InnerHTMLArr[oSkus.m_skuType]);
	return elem;		
}

function SubmitSelection( cmd )
{
	var generalVars = new GeneralVars();	
	var selection = GetUserSelection();	
	var add2URL;	
	
	switch ( selection )
	{
		case "12":
		add2URL = "&code=114878783&purchaseType=B5B4D10B-1761-43AB-B76D-8468FFED2C01";
		break;			
		case "3":
		add2URL = "&code=114877830&purchaseType=6A9B844A-4DC8-4E08-884F-2D67E533A902";
		break;
		case "1":
		add2URL = "&code=114876657&purchaseType=86A17DA8-434B-4683-940E-5DAC12D2553E";
		break;
		case "current":
		return false;				
		default:
		window.location.href = "/error.htm?errorType=99&target=f3";
		return false;
	}
			
	window.location.href = generalVars.adapterURL + "&sender=GC&cmd=" + cmd + add2URL;
	return false;
}

function GetUserSelection()
{		
	var frm = document.getElementById("Form1");
	if ( frm == null )
		return "-1";
		
	for (counter = 0; counter < frm.yourOption.length; counter++)
	{
		if (frm.yourOption[counter].checked)
		{
			if ( frm.yourOption[counter].disabled )
				return "current";	
			return frm.yourOption[counter].value;
		}
	}
	return "-1";	
}

function SetRow(selection)
{
	// m - monthly
	// q - quarterly
	// y - yearly
	// d - div
	// s - span
	var md = document.getElementById("md");
	var ms = document.getElementById("ms");
	var qd = document.getElementById("qd");
	var qs = document.getElementById("qs");
	var yd = document.getElementById("yd");
	var ys = document.getElementById("ys");
	
	md.className = "subscriptionTextPlace9";
	qd.className = "subscriptionTextPlace9";
	yd.className = "subscriptionTextPlace9";
	ms.className = "subscriptionTextPlace10";
	qs.className = "subscriptionTextPlace10";
	ys.className = "subscriptionTextPlace10";
	
	var dSelection = document.getElementById(selection + "d");
	var sSelection = document.getElementById(selection + "s");
	dSelection.className = "subscriptionTextPlace4";
	sSelection.className = "subscriptionTextPlace8";
	
}


// The one and only SubscriptionManager object
var subscriptionManager = null;

