// JavaScript Document
var $Global = new Object();
$Global.removeNonDecExp = new RegExp("[^0-9]","g");

// functionality to apply on DOM load

$(document).ready(function () {




    $(function () {
        var tabContainers = $('div.tabs > div');

        $('div.tabs ul.tabNavigation a').click(function () {
            tabContainers.hide().filter(this.hash).show();

            $('div.tabs ul.tabNavigation a').removeClass('selected');
            $(this).addClass('selected');

            return false;
        }).filter(':first').click();
    });

    //
    $('img[src$=.png]').ifixpng();

	// hides the slickbox as soon as the DOM is ready
    // (a little sooner than page load)
    $('.sp-LeftNav .moreWrapper').hide();
    // toggles the slickbox on clicking the noted link  
    $('.sp-LeftNav a.slick-toggle').click(function() {
      $('.sp-LeftNav .moreWrapper').slideToggle(800);
      return false;
    });
	// initialise the calculator
	// stage 1 button
	$('#calc-submitStage1').click(function(){
		// Use Regular Expression to remove all non numeric characters
		$Global.calcRevenue = parseFloat($('#calc-revenue').val().replace($Global.removeNonDecExp,""));
		// move on to the next step
		$('#hp-FooterTools .Calc .wrapper').delay(100).animate({marginLeft:"-229"},750,'easeInOutQuart', function() {
			// change step progress
			$('#hp-FooterTools .Calc .steps .stepDesc').html('2');
			$('#hp-FooterTools .Calc .steps .stepBullet').removeClass('stepOn').addClass('stepOff');
			$('#hp-FooterTools .Calc .steps .stepBullet:eq(1)').removeClass('stepOff').addClass('stepOn');
		});
	});
	$('#calc-submitStage2').click(function(){
		// Use Regular Expression to remove all non numeric characters
		$Global.calcAbandom = parseFloat($('#calc-abandom').val().replace($Global.removeNonDecExp,""));
		$Global.calcRecover = Math.round((($Global.calcRevenue/(100-$Global.calcAbandom))*100)-$Global.calcRevenue);
		$('#hp-FooterTools .Calc .recoverValue').html('&pound;'+thousandSeparator($Global.calcRecover,','));
		// move on to the next step
		$('#hp-FooterTools .Calc .wrapper').delay(100).animate({marginLeft:"-458"},750,'easeInOutQuart', function() {
			// change step progress
			$('#hp-FooterTools .Calc .steps .stepDesc').html('3');
			$('#hp-FooterTools .Calc .steps .stepBullet').removeClass('stepOn').addClass('stepOff');
			$('#hp-FooterTools .Calc .steps .stepBullet:eq(2)').removeClass('stepOff').addClass('stepOn');
		});
	});
	// Calculator reset
	$('#hp-FooterTools .Calc .Calc-TryAgain').click(function() {
		$('#calc-revenue').val('\u00A3'); // \u00A3 = pound symbol
		$('#calc-abandom').val('');
		// move back to step 1
		$('#hp-FooterTools .Calc .wrapper').delay(100).animate({marginLeft:"0"},750,'easeInOutQuart', function() {
			// change step progress
			$('#hp-FooterTools .Calc .steps .stepDesc').html('1');
			$('#hp-FooterTools .Calc .steps .stepBullet').removeClass('stepOn').addClass('stepOff');
			$('#hp-FooterTools .Calc .steps .stepBullet:eq(0)').removeClass('stepOff').addClass('stepOn');
		});
	});
	
    $('.comingSoonLink').click(function(){
        alert("Coming Soon...\n\nWe're still finalising this document.");
    });
    
    // deal with tabbing on calculator
    $('#calc-revenue').keydown(function(e){
        if(e.keyCode == 9) {
            if(e.preventDefault) {
                e.preventDefault();
            }
            $('#calc-submitStage1').click();
            return false;
        }
    });
    $('#calc-abandom').keydown(function(e){
        if(e.keyCode == 9) {
            if(e.preventDefault) {
                e.preventDefault();
            }
            $('#calc-submitStage2').click();
            return false;
        }
    });
});



// Function to insert "thousands" separators
function thousandSeparator(n,sep) {
	var sRegExp = new RegExp('(-?[0-9]+)([0-9]{3})'),
	sValue=n+'';
	if (sep === undefined) {sep=',';}
		while(sRegExp.test(sValue)) {
		sValue = sValue.replace(sRegExp, '$1'+sep+'$2');
	}
	return sValue;
}

function getRandomInt(min,max) {
	return Math.round(min + Math.random()*(max-min))
}


	
	
	
	
