$(document).ready(function(){
	setupPlanPromoBoxes();	
	setupViewAllPlansOnPhoneDetailsPage();
	
	$('.normalPlansContainer .section').hide();
	$('.normalPlansContainerView').show();
	
	$('.planContainer').delegate('input[name=fbfInput]', 'blur', function(ev) {
		var ev = $.Event('keyup');
		ev.which = 48;
		$(this).trigger(ev);
	});
	
	$('.planContainer').delegate('input[name=fbfInput]', 'keyup', function(ev) {
		var elem = this;
		var next = $(elem).next();
		if (!(ev.which >= 96 && ev.which <= 105) && !(ev.which >= 48 && ev.which <= 57)) {
			return true;
		}
		
		if ($(this).val().length == 8) {
			next.attr('src', '/css/images/en_GB/misc/iconLoader.gif');
			
			$.ajax({
				url: '/webservices/checkNumberAjax.cfm',
				method: 'get',
				data: {msisdn: $(this).val()},
				success: function(response) {
					var next = $(elem).next();
					
					if (response == "true ") {
						$(elem).attr('valid', "true");
						next.attr('src', '/css/images/en_GB/misc/iconTick.gif');
					} else {
						$(elem).attr('valid', "false");
						next.attr('src', '/css/images/en_GB/misc/iconRemove.gif');
					}
				}
			});
		}
	});
});

function setupPlanPromoBoxes()
{	
	$('.plansSpecialOffer').addClass('plansSpecialOfferClosed');
	$('.plansSpecialOfferViewMoreLink').show();
	
	$('.plansSpecialOfferViewMoreLink').click(function(event){
		
		event.preventDefault();
		
		var el = $(this);
		
		el.parent().parent().toggleClass('plansSpecialOfferClosed');
		
		el.find('span').text(el.find('span').text() == '+ View' ? '- Hide' : '+ View');
			
	});
}

function setupViewAllPlansOnPhoneDetailsPage()
{
	$('.normalPlansContainerView a').click(function(event)
	{
		event.preventDefault();

		if('none' == $('.normalPlansContainer .section').css('display'))
		{
			$('.normalPlansContainer .section').show();
			$('.normalPlansContainerView h3 a').text('- Hide extra plans');
		}
		else
		{
			$('.normalPlansContainer .section').hide();
			$('.normalPlansContainerView h3 a').text('+ View all plans');
		}
		
		//$('.normalPlansContainerView').hide();
	});
}
