// JavaScript Document 
        
function hide_overlay(){
	var nojs = document.getElementById("calculator_nojs");
	nojs.style.display = "none";
	}
	
function is_currency(str){
	//regex1 = /^\d+\.\d{2}$/;
	//regex2 = /^\d+\.\d{1}$/;
	//regex3 = /^\d+$/;
	
	regex = /^(\d|,)+(\.(\d{1,2})?)?$/;
	
	//if (regex1.test(str) || regex2.test(str) || regex3.test(str)){
	if (regex.test(str)){
		return true;
		}
	else{
		return false;
		}
	}
	
function round_number(num,dec){
	var result = Math.floor(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
	}
	
function commify(input){
	var Num = input;
	var newNum = "";
	var newNum2 = "";
	var count = 0;
	//check for decimal number
	if (Num.toString().indexOf('.') != -1){  //number ends with a decimal point
		if (Num.toString().indexOf('.') == Num.length-1){
			Num += "00";
			}
		if (Num.toString().indexOf('.') == Num.length-2){ //number ends with a single digit
			Num += "0";
			}
		var a = Num.toString().split("."); 
		Num = a[0];   //the part we will commify
		var end = a[1] //the decimal place we will ignore and add back later
		}
	else{
		var end = "00";
		}  
	//this loop actually adds the commas   
	for (var k = Num.length-1; k >= 0; k--){
		var oneChar = Num.toString().charAt(k);
		if (count == 3){
			newNum += ",";
			newNum += oneChar;
			count = 1;
			continue;
			}
		else{
			newNum += oneChar;
			count ++;
			}
		}  //but now the string is reversed!
	//re-reverse the string
	for (var k = newNum.length-1; k >= 0; k--){
		var oneChar = newNum.charAt(k);
		newNum2 += oneChar;
		}
	// add dollar sign and decimal ending from above
	newNum2 = newNum2 + "." + end;
	return newNum2;
	}

function calculate(){
	// set variables
	var valid = false;
	var currency = document.getElementById("ff_currency").value;
	var incometb = document.getElementById("ff_income");
	income = incometb.value.replace(/,/g,"");
	var type = document.getElementById("ff_type");
	var content = document.getElementById("calculator_content");
	var saving = 0;
	
	// get currency symbols and min/max values
	var currency_symbol = "";
	var currency_min = 0;
	var currency_max = 0;
	if (currency==1){
		currency_symbol = "£";
		currency_min = 250;
		currency_max = 2500;
		currency_min_text = "250";
		currency_max_text = "2,500";
		}
	if (currency==2){
		currency_symbol = "€";
		currency_min = 375;
		currency_max = 3750;
		currency_min_text = "375";
		currency_max_text = "3,750";
		}
	if (currency==3){
		currency_symbol = "$";
		currency_min = 500;
		currency_max = 5000;
		currency_min_text = "500";
		currency_max_text = "5,000";
		}
	
	// validate income
	if (income!=null && is_currency(income)){
		valid = true;
		}
		
	// perform calculation if income is valid
	if (valid==true){
		if (type.value==2){
			saving = (income/100)*20;
			}
		else{
			saving = ((income/12)/100)*20;
			}
		saving = (Math.floor(saving*100)/100).toFixed(2);
		saving_formatted = commify(saving);
		saving_year = saving*12;
		saving_year_formatted = commify(saving_year);
		// fix the annual saving if it has too many decimals
		if (!is_currency(saving_year)){
			saving_year = round_number(saving_year,2);
			}
		// report the savings amount or any errors that occur
		if ((Math.floor(saving*100)/100)>currency_max){
			
			content.innerHTML = "<p>The maximum you can save per month in this account is "+currency_symbol+""+currency_max_text+".</p><p>Please download our application to apply for our Monthly Offshore Saver.</p><p>If you wish to explore further saving opportunities please call our Premium Banking Team on +44 (0) 1534 885 055.</p>";
			}
		else if ((Math.floor(saving*100)/100)<currency_min){
			content.innerHTML = "<p>The minimum you can save per month in order to qualify for this account is "+currency_symbol+""+currency_min_text+".</p><p>Please call our Personal Banking Team on +44 (0) 1534 885 000 for more information on other accounts we offer.</p><br>";
			}
		else{
			content.innerHTML = "<p>The maximum amount per month you can save is <b>"+currency_symbol+""+saving_formatted+"</b>.</p><p>Please download our application to apply for our Monthly Offshore Saver.</p><br><br><br>";
			}
		}
	else{
		incometb.style.backgroundColor = "#F00";
		incometb.style.color = "#FFF";
		}
	}