function in_array(needle, haystack){
	for (var i in haystack){
		if(needle == haystack[i]){
			return true;
		}
	}
	return false;
}

function checkvalue(id, val) {
	if(val=='') {
		document.getElementById(id).value=0;
	}
}

function isNumeric(input){
	var allowed_chars = new Array("1","2","3","4","5","6","7","8","9","0",".");
	var num_decimals = 0;
	for(var i = 0; i < input.length; i++){
		if(!in_array(input.charAt(i), allowed_chars)){
			return false;
		}
		if(input.charAt(i) == '.'){
			num_decimals++;
		}
	}
	if(num_decimals > 1){
		return false;
	}
	return true;
}

function roundNumber(newnumber){
//	return newnumber;
	//return newnumber.toFixed(2);
	newnumber = parseFloat(newnumber);

	var s = "" + Math.round(newnumber * 100)/100;
	var i = s.indexOf('.');
	if(i < 0){
		return s + ".00";
	}
	
	var t = s.substring(0, i + 1) + s.substring(i + 1, i + 3);
	if(i + 2 == s.length){ 
		t += "0";
	}
	
	return t;

}

function calculate(){
	
	document.getElementById("calculation_results").style.position = 'relative';
	document.getElementById("calculation_results").style.left = '0';
	document.getElementById("calculation_results").style.top = '0';

	var rdc_local_cost = 0.011; // 1.1p per minute
	var rdc_national_cost  =  0.011; // 1.1p per minute
	var rdc_mobile_cost  = 0.085; // 8.5p per minute
	
	var local_hours = 0;
	var local_minutes = 0;
	var local_total = 0;
	var national_hours = 0;
	var national_minutes = 0;
	var national_total = 0;
	var mobile_hours = 0;
	var mobile_minutes = 0;
	var mobile_total = 0;
	
	//validate input as number	
	if(!isNumeric(document.getElementById("local_hours").value)){
		document.getElementById("local_hours").value = 0;
	}
	if(!isNumeric(document.getElementById("local_mins").value)){
		document.getElementById("local_mins").value = 0;
	}
	if(!isNumeric(document.getElementById("local_total").value)){
		document.getElementById("local_total").value = 0;
	}

	local_hours = document.getElementById("local_hours").value*60;
	local_minutes = document.getElementById("local_mins").value;
	local_total = document.getElementById("local_total").value;
	
	
	if(!isNumeric(document.getElementById("national_hours").value)){
		document.getElementById("national_hours").value = 0;
	}
	if(!isNumeric(document.getElementById("national_mins").value)){
		document.getElementById("national_mins").value = 0;
	}
	if(!isNumeric(document.getElementById("national_total").value)){
		document.getElementById("national_total").value = 0.00;
	}

	national_hours = document.getElementById("national_hours").value*60;
	national_minutes = document.getElementById("national_mins").value;
	national_total = document.getElementById("national_total").value;
	
	if(!isNumeric(document.getElementById("mobile_hours").value)){
		document.getElementById("mobile_hours").value = 0;
	}
	if(!isNumeric(document.getElementById("mobile_mins").value)){
		document.getElementById("mobile_mins").value = 0;
	}
	if(!isNumeric(document.getElementById("mobile_total").value)){
		document.getElementById("mobile_total").value = 0.00;
	}

	mobile_hours = document.getElementById("mobile_hours").value*60;
	mobile_minutes = document.getElementById("mobile_mins").value;
	mobile_total = document.getElementById("mobile_total").value;
	
	local_total_show = roundNumber(document.getElementById("local_total").value);
	national_total_show = roundNumber(document.getElementById("national_total").value);
	mobile_total_show = roundNumber(document.getElementById("mobile_total").value);
	

	var customer_local_price = local_total;
	var customer_national_price = national_total;
	var customer_mobile_price = mobile_total;
	
	document.getElementById("customer_local_price").innerHTML = '£' + local_total_show;	
	document.getElementById("customer_national_price").innerHTML = '£' + national_total_show;
	document.getElementById("customer_mobile_price").innerHTML = '£' + mobile_total_show;
	
	
	//CUSTOMERS RDC PRICE
	var rdc_local_price = roundNumber((parseInt(local_hours,10) + parseInt(local_minutes,10)) * rdc_local_cost);
	var rdc_national_price = roundNumber((parseInt(national_hours,10) + parseInt(national_minutes,10)) * rdc_national_cost);
	var rdc_mobile_price = roundNumber((parseInt(mobile_hours,10) + parseInt(mobile_minutes,10)) * rdc_mobile_cost);
	
		
	document.getElementById("rdc_local_price").innerHTML = '£' + rdc_local_price;
	document.getElementById("rdc_national_price").innerHTML = '£' + rdc_national_price;
	document.getElementById("rdc_mobile_price").innerHTML = '£' + rdc_mobile_price;
	
		
	//CUSTOMERS TOTAL
	var customer_total = roundNumber(parseFloat(customer_local_price) + parseFloat(customer_national_price) + parseFloat(customer_mobile_price));
	document.getElementById("customer_total").innerHTML = '£' + customer_total;
	
	
	//RDC TOTAL
	var rdc_total = roundNumber(parseFloat(rdc_local_price) + parseFloat(rdc_national_price) + parseFloat(rdc_mobile_price));
	document.getElementById("rdc_total").innerHTML = '£' + rdc_total;
	
	
	//SAVING
	var local_saving = roundNumber(local_total_show - rdc_local_price);
	var national_saving = roundNumber(national_total_show - rdc_national_price);
	var mobile_saving = roundNumber(mobile_total_show - rdc_mobile_price);
	var customer_saving = roundNumber(customer_total - rdc_total);
	
	if (local_saving > 0) {
		document.getElementById("local_saving").innerHTML = '£' + local_saving;
	}
	else if (local_saving < 0) {
		document.getElementById("local_saving").innerHTML = '-£' + (local_saving * -1);
	}
	else {
		document.getElementById("local_saving").innerHTML = '£0.00';
	}
	
	if (national_saving > 0) {
		document.getElementById("national_saving").innerHTML = '£' + national_saving;
	}
	else if (national_saving < 0) {
		document.getElementById("national_saving").innerHTML = '-£' + (national_saving * -1);
	}
	else {
		document.getElementById("national_saving").innerHTML = '£0.00';
	}
	
	if (mobile_saving > 0) {
		document.getElementById("mobile_saving").innerHTML = '£' + mobile_saving;
	}
	else if (mobile_saving < 0) {
		document.getElementById("mobile_saving").innerHTML = '-£' + (mobile_saving * -1);
	}
	else {
		document.getElementById("mobile_saving").innerHTML = '£0.00';
	}
	
	if (customer_saving > 0) {
		document.getElementById("customer_saving").innerHTML = '£' + customer_saving;
	}
	else if (customer_saving < 0) {
		document.getElementById("customer_saving").innerHTML = '-£' + (customer_saving * -1);
	}
	else {
		document.getElementById("customer_saving").innerHTML = '£0.00';
	}
	
	var line_rental = 164.60;
	var three_year_saving = 16.46;
	var five_year_saving = 32.92;
	
	//TOTAL 5 YEAR SAVING	
	var total_saving = roundNumber(customer_saving*4*5);
	
	if (total_saving > 0) {
		document.getElementById("total_saving").innerHTML = '£' + total_saving;
	}
	else if (total_saving < 0) {
		document.getElementById("total_saving").innerHTML = '-£' + (total_saving * -1);
	}
	else {
		document.getElementById("total_saving").innerHTML = '£0.00';
	}
	
	if (local_saving*4 >= 0) {
		document.getElementById('yearly_local_saving').innerHTML = '£' + roundNumber(Math.abs(local_saving*4));
	} else {
		document.getElementById('yearly_local_saving').innerHTML = '-£' + roundNumber(Math.abs(local_saving*4));
	}
	
	if (national_saving*4 >= 0) {
		document.getElementById('yearly_national_saving').innerHTML = '£' + roundNumber(Math.abs(national_saving*4));
	} else {
		document.getElementById('yearly_national_saving').innerHTML = '-£' + roundNumber(Math.abs(national_saving*4));
	}
	if (mobile_saving*4 >= 0) {
		document.getElementById('yearly_mobile_saving').innerHTML = '£' + roundNumber(Math.abs(mobile_saving*4));
	} else {
		document.getElementById('yearly_mobile_saving').innerHTML = '-£' + roundNumber(Math.abs(mobile_saving*4));
	}
	if (customer_saving*4 >= 0) {
		document.getElementById('yearly_customer_saving').innerHTML = '£' + roundNumber(Math.abs(customer_saving*4));
	} else {
		document.getElementById('yearly_customer_saving').innerHTML = '-£' + roundNumber(Math.abs(customer_saving*4));
	}
}

function hideResults() {
	document.getElementById("calculation_results").style.position = 'absolute';
	document.getElementById("calculation_results").style.left = '-10000px';
	document.getElementById("calculation_results").style.top = '-10000px';
}


