function changeView(elementId)
{
	function hideDetail(elementId)
	{
		if(document.getElementById)
			var e = document.getElementById(elementId);

		e.style.display = 'none';
	}

	function showDetail(elementId)
	{
		if(document.getElementById)
		{
			var e = document.getElementById(elementId);
		}

		e.style.display = 'block';
	}

	if(document.getElementById)
	{
		var e = document.getElementById(elementId);
	}

	if(e.style.display == 'none' || e.style.display == false)
	{
		showDetail(elementId);
	} 
	else
	{
		hideDetail(elementId);
	}
}

/*
*
*	xtras functions
*
*/

var xtras = new Array('ftse_xtra_iframe', 'shares_xtra_iframe', 'terminal_xtra_iframe');

function get_current_total()
{
	var e;
	e = window.document.forms['sub_calculator'].subTotal;
	return e.value;
}

/*
*
*	set_base_rate
*
*/

function set_base_rate(val,days)
{
	var str;
	var e;
	var elements;

	str = "";
	for(var i = 0; i < xtras.length; i++) 
	{	
		xtra = xtras[i];
		refresh_xtra(xtra);
		set_xtra_values(xtra,days);
	}
	
	e = window.document.forms['sub_calculator'].subTotal;
	e.value = val;
	f = window.document.forms['sub_calculator'].sub_schedule;
	f.value = days;
	render_total(val);
}

/*
*
*	refresh_xtra
*
*/

function refresh_xtra(xtra)
{
	var intDays;
	var form;
	var xtraStatus;
	var prevStatus;
	var uri;
	var strXtra;
	
	// address the form
	form = document.forms['sub_calculator'];
	
	// get the current status for the xtra
	xtraStatus = window.frames[xtra].xtraStatus;
	// get the previous status for the xtra
	prevStatus = window.frames[xtra].prevStatus;
	
	// loop through the 'days' elements of the form 
	// to ascertain which is checked
	for(var i = 0; i < form.days.length; i++)
	{
		if(form.days[i].checked == true)
		{
			daySelected = parseFloat(form.days[i].value);
		}
		else
		{
			dayUnselected = parseFloat(form.days[i].value);
		}
	}
	
	// set the days value according to the results of the loop above
	intDays = (daySelected > dayUnselected) ? 365 : 30;
	
	// strip unwanted characters from the xtra string for use in the url 
	strXtra = xtra.replace(/_xtra_iframe/i,'');

	// replace the current xtra location with uri
	if(xtraStatus == 3 && intDays == 365)
	{
		uri = "/register/xtra_iframe.php?xtra=" + strXtra + "&status=" + 0 + "&prevStatus=3";
		window.frames[xtra].location.replace(uri);
	}

	if(xtraStatus == 1 && intDays == 365)
	{
		uri = "/register/xtra_iframe.php?xtra=" + strXtra + "&status=" + 0 + "&prevStatus=1";
		window.frames[xtra].location.replace(uri);
	}
	
	if(prevStatus == 3 && intDays == 30)
	{
		uri = "/register/xtra_iframe.php?xtra=" + strXtra + "&status=" + 3;
		window.frames[xtra].location.replace(uri);
	}

	if(prevStatus == 1 && intDays == 30)
	{
		uri = "/register/xtra_iframe.php?xtra=" + strXtra + "&status=" + 1;
		window.frames[xtra].location.replace(uri);
	}
	
}

function set_xtra_values(xtra,days)
{
	var form;
	var rate;
	
	form = window.frames[xtra].document.forms[0];
	
	if(!form)
	{
		return false;
	}
	
	if(form[0].checked)
		form[0].click();

	if(days > 30)
	{
		rate = form.annual_value.value;
	}
	else
	{
		if(!form.monthly_value.value > 0)
			rate = form.annual_value.value;
		else
			rate = form.monthly_value.value;
	}
	form[0].value = rate;
}

function add_extra(xtra)
{
	var e;
	var subTotal;
	var amount;
	
	subTotal = get_current_total();
	e = window.document.forms['sub_calculator'].subTotal;
	if(!e.value > 0 && arguments.caller == null)
	{
		str = "Please complete part 1 of this form before selecting your extras";
		if(xtra.checked == true)
			alert(str);
		xtra.click();
		return false;
	}
	if(xtra.checked == true)
	{
		amount = (parseFloat(subTotal) + parseFloat(xtra.value)).toFixed(2);
	}
	else
	{
		amount = (parseFloat(subTotal) - parseFloat(xtra.value)).toFixed(2);
	}
	
	e.value = amount;
	render_total(amount);
}

function render_total(amount)
{
	var div;
	var str;
	var strNode;
	
	str = parseFloat(amount);
	str = str.toString();
	str = padFloat(str);
	str = "£" + str;
	div = document.getElementById('subTotalDisplay');
	strNode = document.createTextNode(str);
	div.removeChild(div.childNodes[0]);
	div.appendChild(strNode);
	return true;
}

/*
*
*	padFloat
*	args: 		num: number to pad
*/

function padFloat(num)
{
	var pad=num;
	var n;
	var len;


	// check for the existence of a decimal point
	if(num.indexOf('.') == -1)
	{
		pad = num + ".00";
	}
	// find the position of the decimal point 
	else
	{
		len = num.length;
		n = num.indexOf('.');
		if(len - n < 3)
		{
			pad = num + '0';
		}
	}
	return pad;
}

function get_rendered_total()
{
	var div;
	var str;
	var strNode;
	div = document.getElementById('subTotalDisplay');
	return(div.childNodes[0]);
}

function display_xtra(iframe,status)
{	
	// var str;
	// str = "xtra: " + iframe + "\nstatus: " + status;
	// str += "\nURI: " + src_str;
	// alert(str);

	var src_str;
	src_str = "/register/xtra_iframe.php?xtra=" + iframe;	
	src_str += "&status=" + status;
	
	strIframe = iframe + "_xtra_iframe";
	window.frames[strIframe].location.replace(src_str);
}

function get_current_days()
{
	var form = window.document.forms['sub_calculator'];
	var days = form.days;
	var activeDays;
	
	for(var i =0; i < days.length; i++)
	{
		if(days[i].checked) 
		{
			activeDays = days[i].id;
			break;
		}
	}
	return parseInt(activeDays.replace(/[a-zA-Z]*/,''));
}

function get_current_base_rate()
{
	var form = window.document.forms['sub_calculator'];
	var days = form.days;
	var activeDays;
	
	for(var i =0; i < days.length; i++)
	{
		if(days[i].checked) 
		{
			activeDays = days[i].value;
			break;
		}
	}
	return parseFloat(activeDays);
}

function resetForm()
{
	// get the current days
	var active_days = get_current_days();

	eval('document.forms[\'sub_calculator\'].days' + active_days + '.click()');
	
	// now get and set the base rate
	var active_base_rate = get_current_base_rate();

	set_base_rate(active_base_rate,active_days);

	// run through the iframes and reload
	for(var i = 0; i < xtras.length; i++)
	{
		var xtra_name = xtras[i].replace('_xtra_iframe','');
		display_xtra(xtra_name,window.upgradeStatus[i]);
	}
	return true;
}
