// Resize text for accessibility
function resize_text(multiplier) {
	var body = $('body');

	if (body.css('fontSize') == "" || multiplier == 0) {
		body.css('fontSize', '1.0em');
	}
	var size = parseFloat(document.body.style.fontSize) + (multiplier * 0.2) + "em";
	body.css('fontSize', size);
	return size;
}

$(function(){

	var body = $('body');
	if(getCookie('apex.fontsize')){
		body.css('fontSize', getCookie('apex.fontsize'));
	}

	/* If we've come back from somewhere else to avoid any problems on the payment form clear the details out */
	$(':input','#payment_form').not(':button, :submit, select, :reset, :hidden')
		 .val('')
		 .removeAttr('checked')
		 .removeAttr('selected');
	
	/*
	* Check reference number from payment form
	* Adapted 07th November 2011 by SJB to incorporate the HRMC customer requests
	* Any customer with an HRMC flag is now shown the credit/debit card field before
	* they go to Sage Pay.  A transaction fee is placed on the transaction if credit
	* card is supplied.
	*/
	$('#ref_no').change(function(){
		var input = $(this);
		if(input.val().length > 3) {
			$.get('/pay-by-credit-debit-card/ref_lookup/'+input.val(), function(data) {
				data = jQuery.parseJSON(data);
				if(data){
					input.next('span').removeClass('error_text').addClass('valid_text').text('Apex Reference Number found!')
					if (data.hmrc == 1) {
						$('#payment_type_row').fadeIn();
						$('#hmrc').val(1);
					}
				}else{
					input.next('span').removeClass('valid_text').addClass('error_text').text('Apex Reference Number not found!')
				}
			});
		}else{
			input.next('span').removeClass('valid_text').addClass('error_text').text('Apex Reference Number not found!');
		}
	});

	/*
	* Calculate and show what the transaction charge is going to be.
	* Have to grab the transaction charge for an on page variable
	*
	*/
	$('#amt').change(function(){
		calculateTransactionCharge();
	});

	$('#payment_type').change(function(){
		calculateTransactionCharge();
	});
	
	// Font-size controls
	$('.text-dec').click(function(e){
		e.preventDefault();

		if(parseFloat(getCookie('apex.fontsize')) < 0.7) return false;
		setCookie('apex.fontsize', resize_text(-1), 1);
	})
	$('.text-def').click(function(e){
		e.preventDefault();

		setCookie('apex.fontsize', resize_text(0), 1);
	})
	$('.text-inc').click(function(e){
		e.preventDefault();

		if(parseFloat(getCookie('apex.fontsize')) > 1.3) return false;
		setCookie('apex.fontsize', resize_text(1), 1);
	})

	/*Hack for the minor presentational issue on the home rollover */
	$('ol li:first-child').mouseover(function(){
		/* Find the nested ol */
		$(this).parent().find('li.active ol').addClass('highlight-patch');
	}).mouseleave(function() {
		/* Find the nested ol */
		$(this).parent().find('li.active ol').removeClass('highlight-patch');
	});

	/* Tooltip for credit/debit cards */
	$('#credit_charge_message').hide();
	$('.credit_charge_explained').qtip({
		content: {
	      text: $('#credit_charge_message').html()
		},
		style: {
		  classes: 'ui-tooltip-green ui-tooltip-shadow'
		},
		position: {
			my: 'left center',  // Position my top left...
			at: 'right center', // at the bottom right of...
			target: $('select#payment_type') // my target
		}
	});
	
})

// Deal with cookies
function setCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function calculateTransactionCharge() {
	//Only add the charge if this is an HMRC customer
	if ($('#hmrc').val() == 1 && $('#payment_type').val() == 'credit_card') {
		transactionCharge = $('#amt').val() * (transactionRate/100);
		$('#amt').next('span.valid_text').remove();
		$('#amt').after('<span class="valid_text">Note: a transaction fee of &pound;' + transactionCharge.toFixed(2) + ' will be added.</span>');
	} else {
		$('#amt').next('span.valid_text').remove();
	}
}
