function letter_head_prompt(link){

	if($('letter-head-prompt'))
		$('letter-head-prompt').remove();
	
	var d = document.createElement('div');
	d.id = 'letter-head-prompt';
	var link_pos = $(link).cumulativeOffset();
	$(d).setStyle({position:'absolute',left:($('content-area').getWidth()/2)+'px',top:(link_pos[1]-25)+'px',display:'none'});
	document.body.appendChild(d);
	$(d).insert('<p>Would you like to include your logo and address?</p><p><a href=\"#\" onclick=\"return call_invoice_url(\''+ link.getAttribute('href')+'\',0);\">Yes</a><a href=\"#\" onclick=\"return call_invoice_url(\''+ link.getAttribute('href')+'\',1);\">No</a><a href=\"#\" onclick=\"return close_letter_head_prompt();\">Cancel</a></p>');
	
	new Effect.Appear(d.id,{ duration: 0.5 });
	
	return false;
}
function call_invoice_url(url, letter_head){
	close_letter_head_prompt();
	window.location.href = url+'?letter_head='+letter_head;
	return false;
}

function close_letter_head_prompt(){
	new Effect.Fade('letter-head-prompt',{ duration: 0.5 });
	
	$('letter-head-prompt').remove();
	return false;
}

function what_if_submit(element) {
    if(element.value == '') {
        element.value = "0";
    }
    $("what-if").submit();
}
function colorPicker() {
    new Control.ColorPicker('template_setting_colour');
}
function remove_annotation() {
	Element.remove('annotation');
}

function viewJobProfit(element) {
  document.location='/profit_pack/jobs/profitability/' + element.value;
  return false;
}

function switch_to(scenario) {
  document.location='/cash_pack/edit/change_data_set?scenario=' + scenario + '&location=' + document.location.pathname;
  return false;
}

function getXAbsolute(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
		curleft += obj.x;
    }
	return curleft;
}

function getYAbsolute(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function data_points() {
	var dls = $("data_points").getElementsByTagName("DL");
	var data_points = $A(dls);
		
	data_points.each(function(node){
		node.getElementsByTagName("DT")[0].onclick = function() {
		  if (!$("annotation")) {
		  	new Insertion.Top('data_points', '<div id="annotation"></div>');
		  }
		  desc = node.getElementsByTagName("DD")[0];
		  $("annotation").style.top = this.style.top;
		  $("annotation").style.left = this.style.left;
		  $("annotation").innerHTML = '<h3>Annotation</h3>' + desc.innerHTML;
	    return false;
	  };
	});

}

/* Deliverables handling */
function selectGroup(id) {
    new Ajax.Request('/profit_pack/people/select_group', {
      parameters: { id: id },
      asynchronous:true,
      evalScripts:true});
}

/* Deliverables handling */
function selectEmployee() {
    new Ajax.Request('/profit_pack/people/select_employee', {
      parameters: Form.serialize('employee-groups'),
      asynchronous:true,
      evalScripts:true});
}

function predictorSetUp() {
   $$('form#predictor select').each(function(item) {
     item = $(item);
     item.onchange = function() {
         $('predictor').submit();
     };
   });
   $$('form#predictor input').each(function(item) {
       item = $(item);    
       item.onchange = function() {
           $('predictor').submit();
       };
   });
}

function predictFigureChange(disable_field) {
   $(disable_field).disabled = 'disabled';
   $('predictor').submit(); 
}

function employeeImpact() {
    var gross_salary = parseInt($F('gross_salary'));
    var overtime = parseInt($F('overtime'));
    var overtime_figure = Math.round(gross_salary * (overtime / 100)); 
    $('overtime_figure').innerHTML = "&pound;" + comma_formatted(overtime_figure);
    
    var bonus = parseInt($F('bonus'));
    var bonus_figure = Math.round(gross_salary * (bonus / 100)); 
    $('bonus_figure').innerHTML = "&pound;" + comma_formatted(bonus_figure);
    
    var salary_total = gross_salary + overtime_figure + bonus_figure;
    $('salary_total').innerHTML = "&pound;" + comma_formatted(salary_total);
    
    var employers_ni = Math.round((salary_total - ni_earning_threshold) * (ni_employer_rate / 100));
    $('employers_ni').innerHTML = "&pound;" + comma_formatted(employers_ni);
    
    var pension = parseInt($F('pension'));
    var pension_figure = gross_salary * (pension  / 100);
    $('pension_figure').innerHTML = "&pound;" + comma_formatted(pension_figure);
    
    var total_costs =  salary_total + employers_ni + pension_figure;    

    var contracted_hours_week = parseFloat($F('contracted_hours_week'));
    var estimated_percentage = parseInt($F('estimated_percentage'));
    
    var chargeable_hours_per_month  = ((contracted_hours_week * 52) / 12) * ( estimated_percentage / 100 );
    
    var monthly_cost_of_new_employee =  total_costs / 12;
    $('monthly_cost_of_new_employee').innerHTML = "&pound;" + comma_formatted(Math.round(monthly_cost_of_new_employee));
    
    var cost_per_chargeable_hour = monthly_cost_of_new_employee / chargeable_hours_per_month;
    
    var new_outgoings = outgoings + monthly_cost_of_new_employee;
    $('new_outgoings').innerHTML = "&pound;" + comma_formatted(Math.round(new_outgoings));
    
    var new_break_even_rate = new_outgoings / (monthly_charge_hours + chargeable_hours_per_month);
    $('new_break_even_rate').innerHTML = "&pound;" + Math.round(new_break_even_rate);
 
}


//A function that returns unique IDs
//(it's like a class with a class attribute)
getId = function() {
 if (!this.id)
   this.id = 0;
 this.id++;
 return this.id;
};
//A function that acts like a stack (registers objects by unique IDs)
registerObject = function(objType, id, o) {
 if (!this.objById)
   this.objById = new Array();
 if (!this.objById[objType])
   this.objById[objType] = new Array();
 if (this.objById[objType][id])
   alert('Object already registered with this ID!!');
 else
   this.objById[objType][id] = o;
};

//A function that unstacks the registered object with a given ID
getObjectById = function(objType, id) {
 return this.objById[objType][id];
};
//We use objType for our stack to be reusable
//in other parts of our program
//This way, the stack can hold several object in different use contexts
//A test class to be instantiate
testClass = function() {
 //Attribute
 this.phrase = 'Cool! The context is saved!';
 //Method with reference to the class object
 this.doSomething = function() {
   alert(this.phrase);
 };
};
//The function that delay code execution
delay = function(code, contexte, time) {
 //We provide a unique ID for our context object to be memorized
 var id = getId();
 //We register our context object in the stack
 registerObject('delay', id, contexte);
 //When time is out, the code will be evaluated within the stacked context
 setTimeout('eval('+code+'.call(getObjectById(\'delay\','+id+')));', time);
};
//Then we run the test!!
//Instantiate the test class

function copyAddressToRegistered() {
  $('client_registered_address').value = [$F(client_address_a),$F(client_address_b),$F(client_address_c)].join(' ').strip();
  $('client_registered_town').value = $F(client_town);
  $('client_registered_postcode').value = $F(client_postcode);
}

function comma_formatted(amount)
{
	var delimiter = ","; // replace comma if desired
	if(isNaN(amount)) { return ''; }
	var n = new String(amount);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	return n;
}
