// default starting values
var selectedFrequency = "once";
var ukTaxpayer = false;
var highRateTaxpayer = false;

// Do all calculations in this function....
function calcTotals() {
  // Validate input
  var submittedValue = validateInput(document.getElementById("giftAmount").value);
  
  if(submittedValue) {
      // TheResult will be used to perform the calculations (submittedValue preserved should it be required later)
      var theResult = submittedValue;
	  
	  var giftaidResult = 0;
	  var hightaxResult = 0;
	  if(ukTaxpayer) {
		giftaidResult = submittedValue * 0.25;
		hightaxResult = giftaidResult;
	  }
	  var matchedgiftResult = (giftaidResult + submittedValue)/3;
	
		switch(selectedFrequency) {
		  case ("once"):
		 
		  break;
		  case ("quarterly"):
		    theResult *= 4;
		    giftaidResult *= 4;
			hightaxResult *= 4;
			matchedgiftResult *= 4;
			
		  break;
		  case ("monthly"):
		    theResult *=12;
		    giftaidResult *=12;
			hightaxResult *=12;
			matchedgiftResult *= 12;
		  break;
		}

	  var totalResult = theResult + giftaidResult + matchedgiftResult;

	  document.getElementById("yourGift").value = formatNumber(theResult,2,",",".","","","","");
	  document.getElementById("giftAmount").value = formatNumber(submittedValue,2,",",".","","","","");
	  
		document.getElementById("giftAid").value = formatNumber(giftaidResult,2,",",".","","","","");
		if(highRateTaxpayer) {
			document.getElementById("highTax").value = formatNumber(hightaxResult,2,",",".","","","","");
		}
		else {
			document.getElementById("highTax").value = formatNumber(0,2,",",".","","","","");
	   }
	  
	  document.getElementById("matchedGift").value = formatNumber(matchedgiftResult,2,",",".","","","","");  
	  document.getElementById("giftTotal").value = formatNumber(totalResult,2,",",".","","","","");  
  }
}


function setFrequency() {  
  if (document.getElementById("freqOnce").checked){
    selectedFrequency = "once";
	document.getElementById("giftduration").setAttribute("class", "hideme");
	document.getElementById("giftduration").setAttribute("className", "hideme");
  }
  else if (document.getElementById("freqQuart").checked){
    selectedFrequency = "quarterly";
	document.getElementById("giftduration").setAttribute("class", "showme");
	document.getElementById("giftduration").setAttribute("className", "showme");
  }
  else if (document.getElementById("freqMonth").checked){
    selectedFrequency = "monthly";
	document.getElementById("giftduration").setAttribute("class", "showme");
	document.getElementById("giftduration").setAttribute("className", "showme");
  }
  // call the calcTotals function with the new frequency
  calcTotals();
}


function setUKtax(){
  var highRateDiv = document.getElementById("highRateTaxpayer");
  var highTaxField = document.getElementById("highTaxField");
  var giftAidField = document.getElementById("giftAidField");
  
  if (document.getElementById("ukTaxNo").checked) {
    ukTaxpayer = false;
	highRateDiv.setAttribute("class", "fieldinactive");
	highRateDiv.setAttribute("className", "fieldinactive");
	
	giftAidField.setAttribute("class", "fieldinactive");
	giftAidField.setAttribute("className", "fieldinactive");
	
	document.getElementById("giftAid").disabled = true;
	document.getElementById("highrateNo").disabled = true;
	document.getElementById("highrateYes").disabled = true;

	highTaxField.setAttribute("class", "fieldinactive fieldsetbottom");
	highTaxField.setAttribute("className", "fieldinactive fieldsetbottom");
	document.getElementById("highTax").disabled = true;
	
  }
  else if (document.getElementById("ukTaxYes").checked) {
    ukTaxpayer = true;
	// Allow edit of high rate tax
	highRateDiv.setAttribute("class", "fieldactive");
	highRateDiv.setAttribute("className", "fieldactive");
	
	giftAidField.setAttribute("class", "fieldactive");
	giftAidField.setAttribute("className", "fieldactive");	
	
	document.getElementById("giftAid").disabled = false;
	document.getElementById("highrateNo").disabled = false;
	document.getElementById("highrateYes").disabled = false;
	// set high rate tax field state appropriately
	if(document.getElementById("highrateNo").checked) {
		highTaxField.setAttribute("class", "fieldinactive fieldsetbottom");
		highTaxField.setAttribute("className", "fieldinactive fieldsetbottom");
		document.getElementById("highTax").disabled = true;
	}
	else {
		highTaxField.setAttribute("class", "fieldactive fieldsetbottom");
		highTaxField.setAttribute("className", "fieldactive fieldsetbottom");
		document.getElementById("highTax").disabled = false;
	}
  }
  calcTotals();
}


function setHighTax(){
  var theFieldset = document.getElementById("highTaxField");
  
  if (document.getElementById("highrateNo").checked) {
    highRateTaxpayer = false;
	theFieldset.setAttribute("class", "fieldinactive fieldsetbottom");
	theFieldset.setAttribute("className", "fieldinactive fieldsetbottom");
	document.getElementById("highTax").disabled = true;
  }
  else if (document.getElementById("highrateYes").checked) {
    highRateTaxpayer = true;
	theFieldset.setAttribute("class", "fieldactive fieldsetbottom");
	theFieldset.setAttribute("className", "fieldactive fieldsetbottom");
	document.getElementById("highTax").disabled = false;
  }
  calcTotals();
}

function onLoaded() {
    document.getElementById("calcwrapper").setAttribute("class", "showme");
    document.getElementById("calcwrapper").setAttribute("className", "showme");
	document.getElementById("giftAid").disabled = true;
  	document.getElementById("highTax").disabled = true;
	document.getElementById("highrateNo").disabled = true;
	document.getElementById("highrateYes").disabled = true;	
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //

/*
function calcGiftAid() {
  var submittedValue = validateInput(document.getElementById("giftAid").value);
  
  if(submittedValue) {
	  var giftamountResult = submittedValue * 0.8;
	  var hightaxResult = giftamountResult * 0.25;
	  var matchedgiftResult = submittedValue + submittedValue/3;
		
	  document.getElementById("giftAmount").value = formatNumber(giftamountResult,2,",",".","","","","");
	  document.getElementById("giftAid").value = formatNumber(submittedValue,2,",",".","","","","");
	  document.getElementById("highTax").value = formatNumber(hightaxResult,2,",",".","","","","");
	  document.getElementById("matchedGift").value = formatNumber(matchedgiftResult,2,",",".","","","","");
  }
}

function calcHighTax() {
  var submittedValue = validateInput(document.getElementById("highTax").value);
  if(submittedValue) {
	  var giftAmountResult = submittedValue * 4;
	  var giftaidResult = giftAmountResult * 1.25;
	  var matchedgiftResult = giftaidResult + giftaidResult/3;

	  document.getElementById("giftAmount").value = formatNumber(giftAmountResult,2,",",".","","","","");
	  document.getElementById("giftAid").value = formatNumber(giftaidResult,2,",",".","","","","");
	  document.getElementById("highTax").value = formatNumber(submittedValue,2,",",".","","","","");
	  document.getElementById("matchedGift").value = formatNumber(matchedgiftResult,2,",",".","","","","");
  }
}

function calcMatchedGift() {
  var submittedValue = validateInput(document.getElementById("matchedGift").value);  
  if(submittedValue) {
	  var giftaidResult = submittedValue * 3/4;
	  var giftAmountResult = giftaidResult * 0.8;
	  var hightaxResult = giftAmountResult * 0.25;

	  document.getElementById("giftAmount").value = formatNumber(giftAmountResult,2,",",".","","","","","ceil");
	  document.getElementById("giftAid").value = formatNumber(giftaidResult,2,",",".","","","","","ceil");
	  document.getElementById("highTax").value = formatNumber(hightaxResult,2,",",".","","","","","ceil");
	  document.getElementById("matchedGift").value = formatNumber(submittedValue,2,",",".","","","","","ceil");
  }
}

*/

function validateInput(num) {
  // strip any commas
  num = num.replace(/,/g,"");
  // check a value has been entered
  if(num == "") {
    alert("You must enter a value in the field");
	return false;
  }
  // check for multiple points
  else if (num.lastIndexOf(".") != num.indexOf(".")) {
    alert("The number entered contains too many points...");
	return false;
  }
  // Check it's a valid number (note that by this point commas have been removed)
  else if(num.match(/[^0-9.]/)) {
    alert("The value entered contains non numerical characters");
	return false;
  }
  // if it passes all the tests return the value as a number
  else {   
    return Number(num);
  }
}

// http://javascript.about.com/library/blnumfmt.htm
// number formatting function
// copyright Stephen Chapman 24th March 2006, 22nd August 2008
// permission to use this function is granted provided
// that this copyright notice is retained intact
function formatNumber(num,dec,thou,pnt,curr1,curr2,n1,n2,rounding) {
  // Added another parameter to give an option of the rounding method
  // floor suits  my purposes as the default, though in most cases it should probably be round...
  switch(rounding) {
    case("ceil"):
		var x = Math.ceil(num * Math.pow(10,dec));
		break;
	case("round"):
		var x = Math.round(num * Math.pow(10,dec));
		break;
	default:
		var x = Math.floor(num * Math.pow(10,dec));
  }
  if (x >= 0) n1=n2='';
  var y = (''+Math.abs(x)).split('');
  var z = y.length - dec; 
  if (z<0) z--; 
  for(var i = z; i < 0; i++) y.unshift('0'); 
  if (z<0) z = 1; 
  y.splice(z, 0, pnt); 
  if(y[0] == pnt) y.unshift('0'); 
  while (z > 3) {z-=3; y.splice(z,0,thou);}
  var r = curr1+n1+y.join('')+n2+curr2;
  return r;
}


addLoadEvent(onLoaded);
