Number.prototype.formatMoney = function(c, d, t){
    var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "." : d, t = t == undefined ? "," : t, s = n < 0 ? "-" : "",
    i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
	return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t)
    + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}


function in_pounds(float_in){
	if(float_in>=0) return "&pound;" + float_in.formatMoney(2);
	else return "-&pound;" + Math.abs(float_in).formatMoney(2);
}

function calculate(){
	
	var workers = stripAlphaChars($F("workers"));
	var overheads = stripAlphaChars($F("overheads"));
	
	if(workers == "" || overheads == "") {
		_throw_error("Please enter a figure in both boxes");
		return false;
	}
	
	// Each has a number (always positive seeing as we've stripped dashes) so update screen to reflect this...
	$("workers").value = workers = parseInt(workers);
	$("overheads").value = overheads = parseFloat(overheads);
	
	var grossinvoice = (workers * 500 * 1.175);
	var payroll = ((grossinvoice/1.175) * 0.8);
	var fee = (grossinvoice * 0.04);
	var margin = ((grossinvoice/1.175) - payroll - fee);
	
	//Overdraft-specific data
	var cashout = payroll;
	
	$("payroll").innerHTML = in_pounds(payroll)	;
	$("fee").innerHTML = in_pounds(fee);
	$("margin").innerHTML = in_pounds(margin);
	$("grossinvoice").innerHTML = in_pounds(grossinvoice);
	
	
	$("summary_div").hide();
	$("results").hide();
	Effect.Appear("results", {duration:0.3});
	
	var fields = new Array('grossinvoice', 'margin', 'overheads', 'cashout');
	
	// Updates all elements with the CSS classname as stored in the 'fields' array with the value of the JS var of the same name
	fields.each(function(field) {
		classname = "." + field;
		$$(classname).each(
			function (el) {
				el.hide();
				el.innerHTML = in_pounds(eval(field));
				Effect.Appear(el, {duration:0.3});
			}
		);
	});
				
	// Special case
	$$(".no_cashout").each(function(el) {
		
		el.hide();
		el.innerHTML = "-";
		Effect.Appear(el, {duration:0.3});
		   
	});
	
	// Calculate figures for opening/closing balance
	for(i=1; i<=6; i++){
		$("openingbalance_" + i).hide();
		$("closingbalance_" + i).hide();
		
		// NowCash
		$("openingbalance_" + i).innerHTML = in_pounds( (margin-overheads) * (i-1) );
		$("closingbalance_" + i).innerHTML = in_pounds( (margin-overheads) * i );	
		
		// Overdraft
		
		$("o_netcash_" + i).innerHTML = in_pounds(-payroll);
		$("o_openingbalance_" + i).innerHTML = in_pounds(((-cashout-overheads) * (i-1)));
		$("o_closingbalance_" + i).innerHTML = in_pounds(((-cashout-overheads) * i));
		
		if(i==6){
			$("o_closingbalance_" + i).innerHTML = in_pounds((((-cashout-overheads) * 6)+grossinvoice));
			$("o_netcash_" + i).innerHTML = in_pounds((-cashout-overheads)+grossinvoice);
		}
	
	}
	
	// Show 'em all together
	for(i=1; i<=6; i++){
		Effect.Appear($("o_netcash_" + i), {duration:0.3});
		Effect.Appear($("openingbalance_" + i), {duration:0.3});
		Effect.Appear($("closingbalance_" + i), {duration:0.3});
	}
	
	$("nowtext").show();
	
	// Return false so as not to submit the form
	return false;	
}

function show_nowcash(){
	if($("nowcash").visible()==false){
		
		$("overdraft").hide();
		$("overdrafttext").hide();	
		
		$("fee_title").show();
		$("margin_title").show();
		$("nowcash").show();
		$("nowtext").show();
		
		$('now').setStyle('background-position: 0px 0px');
		$('over').setStyle('background-position: 0px 0px');

	}
	return false;
}

function show_overdraft(){
	if($("overdraft").visible()==false){
		
		$("fee_title").hide();
		$("margin_title").hide()
		$("nowcash").hide();
		$("nowtext").hide();
	
		$("overdraft").show();
		$("overdrafttext").show();	
		
		$('over').setStyle('background-position: 0px -27px');
		$('now').setStyle('background-position: 0px -27px');
		
	}
	return false;
}


/*function show_and_hide(div_to_show, div_to_hide){
	if($(div_to_show).visible()==false){
		$(div_to_hide).hide();
		$(div_to_show).show();
	}
	return false;
}*/



function stripAlphaChars(pstrSource) { 
	var m_strOut = new String(pstrSource); 
    m_strOut = m_strOut.replace(/[^0-9]/g, ''); 

    return m_strOut; 
}

function _throw_error(msg){
	$("summary_div").hide();
	$("summary_div").innerHTML = "<p class=\"error\">" + msg + "</p>";
	Effect.Appear("summary_div", {duration:0.3});
	return false;
}

function request( name ){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return false;
  else
    return results[1];
}


function contact(){
	if( ($F("email")=="Email address") || ($F("comment")=="Comment") ) return false;

	new Ajax.Request('process_contact.php', {
		
		method:'post',
		
		parameters: {
			email: encodeURIComponent($F("email")), 
			comment: encodeURIComponent($F("comment"))
		},
		
		onLoading: function(){
			$("boxcontact").hide();
			$("loading").show();
		},

		
		onSuccess: function(transport, json){
			$("loading").hide();
			$("boxcontact").show();
			if(json.success == true) {	// OK....
				$("boxcontact").innerHTML = '<p>Thank you - you should hear back from us shortly.</p>';
			}
			else{	// There was a problem with the submitted fields...	
				$("contact_info").innerHTML = '<strong style="color:#87002D">There is a problem with the fields highlighted in red...</strong>';
				for(var i = 0; i < json['error_fields'].length; i++) {
					$(json['error_fields'][i]).style.color = '#87002D';
				}
			}
		},
		
		onFailure: function(){ alert('Sorry, we are currently experiencing technical difficulties... Please try again later.') }
	
	});
	
	return false;
}