/* Validation - MK - 01/08/2011 */
$(function(){
	openTab(1);
	var searchString = document.location.search.substring(1);
	if (document.getElementById("tabContentIHT" + searchString) != null) openTab(searchString);
	$('.IHTfields').change(function(){
		if (isNaN($(this).val())) 
			{
				document.iht_calculator.total.value = '';
				alert('Please enter a valid amount. Use numbers only.');
				return false;
			}
	});

});

/*assets*/
function startCalc(){
  interval = setInterval("calc()",1);
}
function calc(){
  one = document.iht_calculator.ValueHome.value;
  two = document.iht_calculator.Savings.value;
  three = document.iht_calculator.Investments.value;
  four = document.iht_calculator.MoneyOwed.value;
  five = document.iht_calculator.OtherAssets.value;
  totalValue = (one * 1) + (two * 1) + (three * 1) + (four * 1) + (five * 1);
  if (!isNaN(totalValue))  document.iht_calculator.total.value = totalValue;
}
function stopCalc(){
  clearInterval(interval);
}
/*Liabilities*/
function startCalc1(){
  interval = setInterval("calc1()",1);
}
function calc1(){
  one1 = document.iht_calculator.OSMort.value;
  two1 = document.iht_calculator.Loans.value;
  three1 = document.iht_calculator.creditCards.value;
  four1 = document.iht_calculator.outstanding.value;
  document.iht_calculator.total2.value = (one1 * 1) + (two1 * 1) + (three1 * 1) + (four1 * 1);
}
function stopCalc(){
  clearInterval(interval);
}

/*totals*/

function startCalc2(){
  interval = setInterval("calc2()",1);
}
function calc2(){
  one = document.iht_calculator.total.value;
  two = document.iht_calculator.total2.value;
  document.iht_calculator.netassets.value = (one * 1) - (two * 1);
}
function stopCalc(){
  clearInterval(interval);
}

// IHT Calculator

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		// The following line needs to be (remove spaces): x1 = x1.replace(rgx, ' $1 ' + ',' + ' $2 ');
		// If Collage replaces it with random links, this needs to be corrected manually
		
		// $cmignore 
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
		// $/cmignore 
}
	return x1 + x2;
}

function removeCommas(nStr) {

//remove any commas
nStr=nStr.replace(/,/g,"");

//remove any spaces
nStr=nStr.replace(/\s/g,"");

return nStr;
}

function checkNum() 
//this checks whether the number entered does not have several decimals
//or non-numeric characters
{
document.forms['iht_calculator'].elements['taxable_amount'].value ='';
document.forms['iht_calculator'].elements['tax_payable'].value ='';

var estate_value_v = document.forms['iht_calculator'].elements['netassets'].value;
estate_value_v = removeCommas(estate_value_v);

var nStr = estate_value_v;

var isOK=0;

nStr=nStr+"";

//if the number has one or none decimal points, lastIndexOf and indexOf
//will give the same answer

if (nStr.lastIndexOf(".")!=nStr.indexOf("."))

isOK=1;

else

//here a regular expression is used to check for numbers and decimals

if (nStr.match(/[^0-9.]/))

isOK=2;

if (isOK != 0) alert('Please check values entered.\nUse numbers only.');

return isOK;

}


function calculate_tax()
{

if (isNaN($('.IHTfields').val())) alert('Please enter a valid amount. Use numbers only.');

var netassets_v = document.forms['iht_calculator'].elements['netassets'].value;
	any_commas = netassets_v.split(',');
	if (any_commas[1]) result_commas = 1; else result_commas = 0;

netassets_v = removeCommas(netassets_v);

if(checkNum(netassets_v) == 0)
//if the estate_value isOK, proceed to calculating the tax
{
	var taxable_amount_v = 0;
	if (netassets_v > 325000) taxable_amount_v = netassets_v - 325000;
	
	taxable_amount_v = Math.round(taxable_amount_v);
	if (result_commas)
	document.forms['iht_calculator'].elements['taxable_amount'].value = addCommas(taxable_amount_v);
	else
	document.forms['iht_calculator'].elements['taxable_amount'].value = taxable_amount_v;
	
	var tax_payable_v = 0;
	tax_payable_v = Math.round(taxable_amount_v * 40 / 100);
	
	if (result_commas)
	document.forms['iht_calculator'].elements['tax_payable'].value = addCommas(tax_payable_v);
	else
	document.forms['iht_calculator'].elements['tax_payable'].value = tax_payable_v;

}
}


// Tabs layout 2009

// showHide collapsible link element
function showHide(layerName)
{
	if (document.getElementById(layerName) != null)
	{
		if (document.getElementById(layerName).style.display != 'block') document.getElementById(layerName).style.display = 'block';
		else document.getElementById(layerName).style.display = 'none'
	}
}

// openTab
function openTab(tabNumber)
{
	var i=1;
	while (document.getElementById("tabLinkIHT" + i) != null)
	{	
		if (i==tabNumber) 
		{
		eval('document.getElementById("tabContentIHT' + i + '").style.display = "block"');
		eval('document.getElementById("tabIHT' + i + '").className = "mainMenuCurIHT"');
		eval('document.getElementById("tabLinkIHT' + i + '").className = "mainMenuCurCIHT"');
		}
	else 
		{
		eval('document.getElementById("tabContentIHT' + i + '").style.display = "none"');
		eval('document.getElementById("tabIHT' + i + '").className = "mainMenuTabIHT"');
		eval('document.getElementById("tabLinkIHT' + i + '").className = "mainMenuTabCurIHT"');
		}
		i++;
	}

	// Close collapsible elements in the opened tab
	var k=1;
	while (document.getElementById("tab" + tabNumber + "_item" + k) != null)
	{eval('document.getElementById("tab' + tabNumber + '_item' + k + '").style.display = "none";'); k++;}

	// On 'How to apply' page (tab5) do not display 'More info' under the tabs
	if (document.getElementById("moreInfo") != null) 
	{ 
		if (tabNumber == 5) document.getElementById("moreInfo").style.display = "none";
		else document.getElementById("moreInfo").style.display = "block";
	}
}




/* -------------------- section for the tabs -------------------------- */

// onresize for ns4
var origWidth, origHeight;
if (document.layers) {
	origWidth = window.innerWidth; origHeight = window.innerHeight;
	window.onresize = function() { if (window.innerWidth != origWidth || window.innerHeight != origHeight) history.go(0); }
}
var cur_lyr;	// holds id of currently visible layer
function swapLayers(id) {
  if (cur_lyr) hideLayer(cur_lyr);
  showLayer(id);
  cur_lyr = id;
}
function showLayer(id) {
  var lyr = getElemRefs(id);
  if (lyr && lyr.css) lyr.css.visibility = "visible";
}
function hideLayer(id) {
  var lyr = getElemRefs(id);
  if (lyr && lyr.css) lyr.css.visibility = "hidden";
}
function getElemRefs(id) {
	var el = (document.getElementById)? document.getElementById(id): (document.all)? document.all[id]: (document.layers)? document.layers[id]: null;
	if (el) el.css = (el.style)? el.style: el;
	return el;
}
