function isdefined(variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}

function dealSort(sort) 
{
	if(!/.+ASC$/.test(sort) && !/.+DESC$/.test(sort))
	{
		if (DWRUtil.getValue("sort").indexOf('ASC') >= 0) sort+=' DESC';
		else sort+=' ASC';
	}
	DWRUtil.setValue("sort",sort);
	getCTR2MealDeals(DWRUtil.getValue("contractLength"));
};

var getPhonePrice = function(deal) {
	var price = new Number(deal.PHONEPRICE);
	price=price.toFixed(2);
	result="<span class='regular'>" ;
	if (price == 0)
		result+="Free";
	else
		result+="&pound;"+price;
	result+="</span>";
	return result;
};

var get3MonthsFreeLineRental = function(deal) {
	if (deal.EXTRA3MONTHSFREELINERENTAL == 1)
		return "EXTRA3MONTHSFREELINERENTAL";
	else
		return "NOEXTRA3MONTHSFREELINERENTAL";
};

var getlineRental = function(deal) {
	return "<span class='price'>&pound;"+parseFloat(deal.LINERENTAL).toFixed(2)+"</span><br />per month"
};

var getinclusiveMinutes = function(deal) {
	return "<span class='regular petrol'>"+parseInt(deal.INCLUSIVEMINUTES)+"</span><br />anytime<br />minutes";
};

var getinclusiveTexts = function(deal) {
	var s;
	if (parseInt(deal.INCLUSIVETEXTS) >= 0)
		texts=parseInt(deal.INCLUSIVETEXTS);
	else
		texts=deal.INCLUSIVETEXTS;
	if(texts == "unlimited")
		s = "<span class='regular petrol'>"+texts+"</span>";
	else
		s = "<span class='regular petrol'>"+texts+"</span><br />inclusive<br />texts"; 
	return s ;
};

var getDurationMinutesTexts = function(deal)
{
	var s;
	var texts;
	if (deal.INCLUSIVETEXTS == 'unlimited')
	{
		texts = 'Unltd';
	}
		else 
	{
		texts = deal.INCLUSIVETEXTS;
	}
	s = "<ul class='plan month"+deal.CONTRACTLENGTH+"'>";
	s += "<li class='planType'>Talk</li>";
	s += "<li class='planL1'><span>"+deal.CONTRACTLENGTH+" months</span></li>";
	s += "<li class='planL2'><span>"+parseInt(deal.INCLUSIVEMINUTES)+" mins</span></li>";
	s += "<li class='planL3'><span>"+texts+" texts</span></li></ul>";

	return s;
}

var getCallsToLandlines = function(deal) {
	if(deal.EXTRAUNLIMITEDLANDCALLS == 1)
		return "<span class='regular petrol'>unlimited</span><br />landline calls";
	else
		return "<span class='regular petrol'>-</span>";
};

var getInternetEmails = function(deal) {
	if(deal.EXTRAMOBILENET == 1)
		return "EXTRAMOBILENETACTIVE";
		//return "<span class='regular petrol'>500MB</span>";
	else
		return "EXTRAMOBILENETINACTIVE";
		//return "<span class='regular petrol'>-</span>";
};

var getIsIphone = function(deal) {
	if(deal.ISIPHONETARIFF == 1)
		return "ISIPHONETRUE";
	else
		return "ISIPHONEFALSE";
};

var getSumbitButton = function(deal) {
	result="<span style='text-align:right'>";
	result+="<a href='Add-Tariff-"+deal.TARIFFID+".html' style='font-size:11px;' class='globalButton globalButtonRedBack'><span>Select</span></a>";
	result+="</span>";
	return result;
}

function getCTR2MealDeals(contractLen)
{
	document.getElementById('planDealswait').style.display = '';
	document.getElementById('planDeals').style.display = 'none';
	DWRUtil.setValue('contractLength',contractLen);
	document.getElementById('duration'+contractLen).checked="checked";
	
	var _arguments = DWRUtil.getValue("contractLength")+'|'+DWRUtil.getValue("contractType")+'|'+DWRUtil.getValue("sort")+'|'+DWRUtil.getValue("productId")+'|0';
	DWREngine._execute(_cfscriptLocation, null, 'CTR2MealDeals', _arguments, CTR2MealDealsResult);
}

function CTR2MealDealsResult(deals)
{
	var deal='planDeals';
	DWRUtil.removeAllRows(deal);
	if (DWRUtil.getValue("productId") != 0)
	{
		DWRUtil.addRows(deal, deals, [ getPhonePrice, getlineRental, getDurationMinutesTexts,getinclusiveMinutes,getinclusiveTexts, getCallsToLandlines,getSumbitButton,getInternetEmails, get3MonthsFreeLineRental, getIsIphone])
	}
	else
	{
		DWRUtil.addRows(deal, deals, [ getlineRental, getDurationMinutesTexts,getinclusiveMinutes,getinclusiveTexts, getCallsToLandlines, getSumbitButton,getInternetEmails , get3MonthsFreeLineRental, getIsIphone])
	}
	document.getElementById('planDealswait').style.display = 'none';
	document.getElementById('planDeals').style.display = '';
}

function init()
{
	DWREngine._errorHandler =  errorHandler;
	getCTR2MealDeals(24);
}

window.onload = init;