function calculate_roi()
{

	var dev_no = document.getElementById('dev_number').value;
	var dev_rate = document.getElementById('dev_rate').value;
	var dev_test_time = document.getElementById('dev_test_time').value / 100;
	
	var dev_total_cost = dev_no * dev_rate * dev_test_time * 220 * 8;
	var dev_manual_cost = dev_total_cost * 0.83;
	
	var qa_no = document.getElementById('qa_number').value;
	var qa_rate = document.getElementById('qa_rate').value;
	var qa_test_time = document.getElementById('qa_test_time').value / 100;

	var qa_total_cost = qa_no * qa_rate * qa_test_time * 220 * 8;
	var qa_manual_cost = qa_total_cost * 0.69;

	var ba_no = document.getElementById('ba_number').value;
	var ba_rate = document.getElementById('ba_rate').value;
	var ba_test_time = document.getElementById('ba_test_time').value / 100;

	var ba_total_cost = ba_no * ba_rate * ba_test_time * 220 * 8;
	var ba_manual_cost = ba_total_cost * 0.69;

	var uat_no = document.getElementById('uat_number').value;
	var uat_rate = document.getElementById('uat_rate').value;
	var uat_test_time = document.getElementById('uat_test_time').value / 100;

	var uat_total_cost = uat_no * uat_rate * uat_test_time * 220 * 8;
	var uat_manual_cost = uat_total_cost * 0.69;

	var total_cost = dev_total_cost + qa_total_cost + ba_total_cost + uat_total_cost;
	var manual_cost = dev_manual_cost + qa_manual_cost + ba_manual_cost + uat_manual_cost;
	
	var perc_manual = document.getElementById('perc_manual').value / 100;
	var cycle_duration = document.getElementById('cycle_duration').value;

	var total_cost_manual = total_cost * perc_manual;
	var total_cost_assist = manual_cost * perc_manual;
	
	var new_duration = cycle_duration*0.725
	
	var temp_duration_weeks = 52 / cycle_duration;
	var temp_new_duration_weeks = 52 / new_duration;
	var extra_cycles_available = temp_new_duration_weeks - temp_duration_weeks;
	
	if ( !isNaN(total_cost) )
	{
		document.getElementById('total_cost').value = Math.round(total_cost * 100) / 100;
		document.getElementById('total_cost_display').innerHTML = formatNumber(Math.round(total_cost * 100) / 100,2,',','.','$','','-','');
	}
	else
		document.getElementById('total_cost').value = 0;

	if ( !isNaN(total_cost_manual) )
	{
		document.getElementById('total_cost_manual').value = Math.round(total_cost_manual * 100) / 100;
		document.getElementById('total_cost_manual_display').innerHTML = formatNumber(Math.round(total_cost_manual * 100) / 100,2,',','.','$','','-','');
	}
	else
		document.getElementById('total_cost_manual').value = 0;

	if ( !isNaN(total_cost_assist) )
	{
		document.getElementById('total_assist').value = Math.round(total_cost_assist * 100) / 100;
		document.getElementById('total_assist_display').innerHTML = formatNumber(Math.round(total_cost_assist * 100) / 100,2,',','.','$','','-','');
	}
	else
		document.getElementById('total_assist').value = 0;

	var total_saving = total_cost_manual-total_cost_assist;
	if ( !isNaN(total_saving) )
	{
		document.getElementById('total_saving').value = Math.round(total_saving * 100) / 100;
		document.getElementById('total_saving_display').innerHTML = formatNumber(Math.round(total_saving * 100) / 100,2,',','.','$','','-','');
	}
	else
		document.getElementById('total_saving').value = 0;
	
	if ( !isNaN(new_duration) )
	{
		document.getElementById('new_duration').value = Math.round(new_duration * 10) / 10;
		document.getElementById('new_duration_display').innerHTML = formatNumber(Math.round(new_duration * 10) / 10,1,',','.','','','-','');
	}
	else
		document.getElementById('new_duration').value = 0;

	if ( !isNaN(extra_cycles_available) )
	{
		document.getElementById('additional_duration').value = Math.round(extra_cycles_available * 10) / 10;
		document.getElementById('additional_duration_display').innerHTML = formatNumber(Math.round(extra_cycles_available * 10) / 10,1,',','.','','','-','');
	}
	else
		document.getElementById('additional_duration').value = 0;
}

// number formatting function
// copyright Stephen Chapman 24th March 2006, 10th February 2007
// 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) 
{
	var x = Math.round(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');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;
}

function check_roi_form()
{
	var return_value;
	
	// default to always ok
	return_value = true;

	var dev_no = document.getElementById('dev_number').value;
	if ( isNaN(dev_no) )
		return false;
		
	var dev_rate = document.getElementById('dev_rate').value;
	if ( isNaN(dev_rate) )
		return false;

	var dev_test_time = document.getElementById('dev_test_time').value;
	if ( isNaN(dev_test_time) )
		return false;
	
	var qa_no = document.getElementById('qa_number').value;
	if ( isNaN(qa_no) )
		return false;

	var qa_rate = document.getElementById('qa_rate').value;
	if ( isNaN(qa_rate) )
		return false;

	var qa_test_time = document.getElementById('qa_test_time').value;
	if ( isNaN(qa_test_time) )
		return false;

	var ba_no = document.getElementById('ba_number').value;
	if ( isNaN(ba_no) )
		return false;

	var ba_rate = document.getElementById('ba_rate').value;
	if ( isNaN(ba_rate) )
		return false;

	var ba_test_time = document.getElementById('ba_test_time').value;
	if ( isNaN(ba_test_time) )
		return false;

	var uat_no = document.getElementById('uat_number').value;
	if ( isNaN(uat_no) )
		return false;

	var uat_rate = document.getElementById('uat_rate').value;
	if ( isNaN(uat_rate) )
		return false;

	var uat_test_time = document.getElementById('uat_test_time').value;
	if ( isNaN(uat_test_time) )
		return false;

	var perc_manual = document.getElementById('perc_manual').value;
	if ( isNaN(perc_manual) )
		return false;
	
	var cycle_duration = document.getElementById('cycle_duration').value;
	if ( isNaN(cycle_duration) )
		return false;


	// if we get this far through then all is well
	return true;
}

function proceedToCalc()
{
	var person_name = document.getElementById('person_name').value;
	var job_title = document.getElementById('job_title').value;
	var company_name = document.getElementById('company_name').value;
	var telephone = document.getElementById('telephone').value;
	var email = document.getElementById('email').value;

	// ensure any error markers are cleared
	document.getElementById('name_validation').style.display = "none";
	document.getElementById('job_validation').style.display = "none";
	document.getElementById('company_validation').style.display = "none";
	document.getElementById('telephone_validation').style.display = "none";
	document.getElementById('email_validation').style.display = "none";
	
	if ( person_name.length > 0 && job_title.length > 0 && company_name.length > 0 && telephone.length > 0 && email.length > 0)
	{
		if ( isValidEmail(email) )
		{
			// set cookies and go
			createCookie('roi_person_name', person_name, 30); 
			createCookie('roi_job_title', job_title, 30); 
			createCookie('roi_company_name', company_name, 30); 
			createCookie('roi_telephone', telephone, 30); 
			createCookie('roi_email', email, 30); 
			
			// go to the new location
			document.location = "./roi_calculator2.htm";
		}
		else
		{
			document.getElementById('email_validation').style.display = "";
			alert('Please ensure you have entered a valid email address');
			return false;
		}
	}
	else
	{					
		if ( person_name.length == 0 ) { document.getElementById('name_validation').style.display = ""; }
		if ( job_title.length == 0 ) { document.getElementById('job_validation').style.display = ""; }
		if ( company_name.length == 0 ) { document.getElementById('company_validation').style.display = ""; }
		if ( telephone.length == 0 ) { document.getElementById('telephone_validation').style.display = ""; }
		if ( email.length == 0 ) { document.getElementById('email_validation').style.display = ""; }

		alert('Please complete the highlighted fields');
		return false;
	}
}

function isValidEmail(thisAddress)
{
	var at="@"
	var dot="."
	var lat=thisAddress.indexOf(at)
	var lstr=thisAddress.length
	var ldot=thisAddress.indexOf(dot)

	if (thisAddress.indexOf(at)==-1){
	   return false;
	}

	if (thisAddress.indexOf(at)==-1 || thisAddress.indexOf(at)==0 || thisAddress.indexOf(at)==lstr){
	   return false;
	}

	if (thisAddress.indexOf(dot)==-1 || thisAddress.indexOf(dot)==0 || thisAddress.indexOf(dot)==lstr){
	    return false;
	}

	 if (thisAddress.indexOf(at,(lat+1))!=-1){
	    return false;
	 }

	 if (thisAddress.substring(lat-1,lat)==dot || thisAddress.substring(lat+1,lat+2)==dot){
	    return false;
	 }

	 if (thisAddress.indexOf(dot,(lat+2))==-1){
	    return false;
	 }
	
	 if (thisAddress.indexOf(" ")!=-1){
	    return false;
	 }

	 return true;				
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
			
function clearForm()
{
	document.getElementById('person_name').value = '';
	document.getElementById('job_title').value = '';
	document.getElementById('company_name').value = '';
	document.getElementById('telephone').value = '';
	document.getElementById('email').value = '';
}
function loadCookieValues()
{
	var person_name = readCookie('roi_person_name');
	var job_title = readCookie('roi_job_title');
	var company_name = readCookie('roi_company_name');
	var telephone = readCookie('roi_telephone');
	var email = readCookie('roi_email');
	
	if ( person_name != null ) { document.getElementById('person_name').value = person_name; }
	if ( job_title != null ) { document.getElementById('job_title').value = job_title; }
	if ( company_name != null ) { document.getElementById('company_name').value = company_name; }
	if ( telephone != null ) { document.getElementById('telephone').value = telephone; }
	if ( email != null ) { document.getElementById('email').value = email; }
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function loadCookieValues()
{
	var person_name = readCookie('roi_person_name');
	var job_title = readCookie('roi_job_title');
	var company_name = readCookie('roi_company_name');
	var telephone = readCookie('roi_telephone');
	var email = readCookie('roi_email');
	
	document.getElementById('person_name').value = person_name;
	document.getElementById('job_title').value = job_title;
	document.getElementById('company_name').value = company_name;
	document.getElementById('telephone').value = telephone;
	document.getElementById('email').value = email;
}

