//Remove the $ sign if you wish the parse number to NOT include it
var prefix="€";
var wd;
var code;

function clearDetails(field,text) {
	if(field.value==text) {
		field.value="";
	}
}

function reenterDetails(field, text) {
	if(field.value=="") {
		field.value=text;
	}
}

function changeyear(frm) {
	var currentage = frm.currentage.value;
	
	if (currentage == "") {
		alert("Current age must be entered");
		frm.currentage.focus();
		return false;
	} else if (isNaN(currentage)) {
		alert("Current age must be numeric only");
		frm.currentage.focus();
		frm.currentage.select();
		return false;
	} else {
		var noyears = 65 - currentage;
		var code = "<B>" + noyears + "</B>";
		
		if (document.layers) {
			document.layers['years'].document.open();
			document.layers['years'].document.write(code);
			document.layers['years'].document.close();		
		} else if (document.getElementById) {
			document.getElementById('years').innerHTML = code;		
		} else if (document.all) {
			document.all.years.innerHTML = code;		
		}
	}
}
	
function asset_calc(frm) {
	var currentage = frm.currentage.value;
	var annualsalary = frm.annualsalary.value;	
	
	frm.retiresalary.value = "";
	frm.totalvalue.value = "";		
	
	if (currentage == "") {
		alert("Current age must be entered");
		frm.currentage.focus();
		return false;
	} else if (isNaN(currentage)) {
		alert("Current age must be numeric only");
		frm.currentage.focus();
		frm.currentage.select();
		return false;
	} else if (annualsalary == "") {
		alert("Annual salary must be entered");
		frm.annualsalary.focus();
		return false;
	} else if (isNaN(annualsalary)) {
		alert("Annual salary must be numeric only");
		frm.annualsalary.focus();
		frm.annualsalary.select();
		return false;		
	} else {
		var noyears = 65 - currentage;	
		var inflatedsalary = annualsalary;
		var totalincome = annualsalary;
		totalincome = parseInt(totalincome);
		
		for(x=currentage;x<65;x++) {
			inflatedsalary = inflatedsalary * 1.02;	
			var temp = parseInt(inflatedsalary);
			
			totalincome = totalincome + inflatedsalary;
		}			
		inflatedsalary = round_decimals(inflatedsalary,0);
		inflatedsalary = commify(inflatedsalary)		
		totalincome =  round_decimals(totalincome,0);
		totalincome = commify(totalincome)		
		
		frm.retiresalary.value = inflatedsalary;
		frm.totalvalue.value = totalincome;	
	}
}

function commify( Num) {
	var newNum = "";
	var newNum2 = "";
	var count = 0;

	//check for decimal number
	if (Num.indexOf('.') != -1){ //number ends with a decimal point
		if (Num.indexOf('.') == Num.length-1){
			Num += "00";
		}
		if (Num.indexOf('.') == Num.length-2){ //number ends with a single digit
			Num += "0";
		}

		var a = Num.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.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;
	return newNum2;
}

function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}




function validate(frm) {

	var textName = frm.Name.value;
	var textAddress = frm.Address.value; 
	var textPhone = frm.Phone.value; 		

	var textDateofbirth = frm.Dateofbirth.value; 				
	var textOccupation = frm.Occupation.value; 		
	var radioSmoker0 = frm.Smoker[0].checked; 
	var radioSmoker1 = frm.Smoker[1].checked;	


	if (textName == "") {
		alert("Name must be entered");
		frm.Name.focus();
		return false;
	}

	if (textAddress == "") {
		alert("Address must be entered");
		frm.Address.focus();
		return false;
	}

	if (textPhone=="") {
		alert("Phone number must be entered");
		frm.Phone.focus();
		return false;
	}

	if (textDateofbirth=="") {
		alert("Date of birth must be entered");
		frm.Dateofbirth.focus();
		return false;
	}
	
	if (textOccupation=="") {
		alert("Occupation must be entered");
		frm.Occupation.focus();
		return false;
	}
		
	if ((!radioSmoker0)&&(!radioSmoker1)) {
		alert("You must select wheter you are a Smoker or Non-smoker");
		return false;
	}	
	
	return true;
}	

function validate_pib5(frm) {

	var textName = frm.Name.value;
	var textAddress = frm.Address.value; 
	var textPhone = frm.Phone.value; 		

	var radio_pib5products = frm.pib5products.value; 
	var radio_otherproducts = frm.otherproducts.value; 	
				
	if (textName == "") {
		alert("Name must be entered");
		frm.Name.focus();
		return false;
	}
		
	if (textAddress == "") {
		alert("Address must be entered");
		frm.Address.focus();
		return false;
	}

	if (textPhone=="") {
		alert("Phone number must be entered");
		frm.Phone.focus();
		return false;
	}
	
	return true;
}


function validate_pensions(frm){

	var textName = frm.realname.value;
        var textAddress = frm.Address.value;
        var textPhone = frm.Phone.value;
	var textdob = frm.DOB.value;
	var textemail = frm.email.value;
	var textoccu = frm.Occupation.value;
	var textregular = frm.Regular_Contribution.value;
	var textsingle = frm.Single_Contribution.value;
	var textself = frm.Status.value;
	var texttaxrate = frm.Tax_Rate.value;
	var textretirement = frm.Retirement_Age.value;
	var textanswer = frm.Answer.value;

	if (textName == "") {
                alert("Name must be entered");
                frm.realname.focus();
                return false;
        }

        if (textAddress == "") {
                alert("Address must be entered");
                frm.Address.focus();
                return false;
        }

        if (textPhone=="") {
                alert("Phone number must be entered");
                frm.Phone.focus();
                return false;
        }

  	if (textemail=="") {
                alert("Email address must be filled in");
                frm.email.focus();
                return false;
}
	if (textdob=="") {
                alert("Date of birth must be entered");
                frm.DOB.focus();
                return false;
        }
	
	 if (textoccu=="") {
                alert("Occupation must be filled in");
                frm.Occupation.focus();
                return false;
        }
	if (textregular=="") {
                alert("Regular contribution must be filled in");
                frm.Regular_Contribution.focus();
                return false;
        }
	if (textsingle=="") {
                alert("Single contribution must be filled in");
                frm.Single_Contribution.focus();
                return false;
        }
	if (textself=="none") {
                alert("Please select either Self employed or PAYE");
                frm.Status.focus();
                return false;
        }
	if (texttaxrate=="none") {
                alert("Please select tax rate");
                frm.Tax_Rate.focus();
                return false;
        }
	if (textretirement=="") {
                alert("Please enter retirement age");
                frm.Retirement_Age.focus();
                return false;
        }
	if (textanswer=="") {
                alert("Please enter the answer to the security question");
                frm.Answer.focus();
                return false;
        }		
		





return true;
}
	
function validate_fb(frm){

	var textName = frm.realname.value;
        var textPhone = frm.Phone.value;
	var textEmail = frm.email.value;
        var textAddress = frm.Address.value;
	var textMessage = frm.Message.value;
	var textAnswer = frm.Answer.value;

	if(frm.Enquiry_From[0].checked) {
		frm.recipient.value = "brokerenquiry@friendsfirst.ie,info@friendsfirst.ie";
	} else if(frm.Enquiry_From[1].checked) {
		frm.recipient.value = "callcentre@friendsfirst.ie,info@friendsfirst.ie";
	} else {
		frm.recipient.value = "info@friendsfirst.ie";
	}
	
	if (textName == "") {
                alert("Name must be entered");
                frm.realname.focus();
                return false;
        }

        if (textPhone=="") {
                alert("Phone number must be entered");
                frm.Phone.focus();
                return false;
        }

  	if (textEmail=="") {
                alert("Email address must be filled in");
                frm.email.focus();
                return false;
	}	

        if (textAddress == "") {
                alert("Address must be entered");
                frm.Address.focus();
                return false;
        }

	if (textMessage=="") {
                alert("Please enter retirement age");
                frm.Message.focus();
                return false;
        }
	if (textAnswer=="") {
                alert("Please enter the answer to the security question");
                frm.Answer.focus();
                return false;
        }		
		
	return true;
}
	
	
	
	
	
	
	
	
	
function validate_ip(frm){

	var textName = frm.realname.value;
        var textPhone = frm.Phone.value;
	var textemail = frm.Email_Address.value;
	var textdob = frm.Date_of_Birth.value;
	var textoccupation = frm.Occupation.value;
	var textsalary = frm.Gross_Annual_Salary.value;
	var textsick = frm.Sick_Pay.value;
	var textcall = frm.When_To_Call.value;
	var textheard = frm.Heard_From.value;	
	var textretire = frm.Retirement_Age.value;
	var textanswer = frm.Answer.value;
	frm.email.value = "";
	
	if (textName == "") {
                alert("Name must be entered");
                frm.realname.focus();
                return false;
        }
	if (textPhone=="") {
                alert("Phone number must be entered");
                frm.Phone.focus();
                return false;
        }
	if(!(frm.No_Email.checked)) {
		if (textemail=="") {
			alert("Email address must be entered");
			frm.Email_Address.focus();
			frm.email.value = "";
			return false;
		} else if (!(isEmail(textemail))) {
			alert("Invalid Email address entered");
			frm.Email_Address.focus();
			frm.Email_Address.select();
			frm.email.value = "";
			return false;
		} else {
			frm.email.value = textemail;
		}
	} else {
		frm.Email_Address.value = "";
		frm.email.value = "noreply@friendsfirst.ie";
	}
	if (textdob=="") {
                alert("Date of birth must be entered");
                frm.Date_of_Birth.focus();
                return false;
        }
	 if (textoccupation=="") {
                alert("Occupation must be entered");
                frm.Occupation.focus();
                return false;
        }
	 if (textsalary=="") {
                alert("Gross Annual Salary must be entered");
                frm.Gross_Annual_Salary.focus();
                return false;
        }	
		
/*	 if (textsick=="") {
                alert("An answer must be selected");
                frm.Sick_Pay.focus();
                return false;
        }
	 if (textcall=="") {
                alert("An answer must be selected");
                frm.When_To_Call.focus();
                return false;
        }
	 if (textheard=="") {
                alert("An answer must be selected");
                frm.Heard_From.focus();
                return false;
        }	*/
	
	if (textretire=="") {
                alert("Your retirement age must be entered");
                frm.Retirement_Age.focus();
                return false;
        }
		
	if (textanswer=="") {
                alert("Please enter the answer to the security question");
                frm.Answer.focus();
                return false;
        }		
	return true;
}

function isEmail(strValue) {
	var objRE = /^[\w-\.\']{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,}$/;
	return (strValue != '' && objRE.test(strValue));
}

function lifestyle_calc (frm) {
	frm.totaloutgoings.value = "";
	
	if ((frm.mortage.value == "")||(frm.mortage.value == " ")) {
		frm.mortage.value = "0";
	} 
	if ((frm.insurance.value == "")||(frm.insurance.value == " ")) {
		frm.insurance.value = "0";
	} 
	if ((frm.telephone.value == "")||(frm.telephone.value == " ")) {
		frm.telephone.value = "0";
	} 	
	if ((frm.gasesb.value == "")||(frm.gasesb.value == " ")) {
		frm.gasesb.value = "0";
	}
	if ((frm.tv.value == "")||(frm.tv.value == " ")) {
		frm.tv.value = "0";
	}
	if ((frm.mobile.value == "")||(frm.mobile.value == " ")) {
		frm.mobile.value = "0";
	}
	if ((frm.carloan.value == "")||(frm.carloan.value == " ")) {
		frm.carloan.value = "0";
	}
	if ((frm.carinsurance.value == "")||(frm.carinsurance.value == " ")) {
		frm.carinsurance.value = "0";
	}
	if ((frm.healthinsurance.value == "")||(frm.healthinsurance.value == " ")) {
		frm.healthinsurance.value = "0";
	}
	if ((frm.regularsavings.value == "")||(frm.regularsavings.value == " ")) {
		frm.regularsavings.value = "0";
	}
	if ((frm.pensioncontributions.value == "")||(frm.pensioncontributions.value == " ")) {
		frm.pensioncontributions.value = "0";
	}
	 if ((frm.groceries.value == "")||(frm.groceries.value == " ")) {
		frm.groceries.value = "0";
	}
	if ((frm.solcialising.value == "")||(frm.solcialising.value == " ")) {
		frm.solcialising.value = "0";
	}
	if ((frm.otheroutgoings.value == "")||(frm.otheroutgoings.value == " ")) {
		frm.otheroutgoings.value = "0";
	}
	
	
	if (isNaN(frm.mortage.value)) {
		alert("Mortgage amount must be numeric only");
		frm.mortage.focus();
		frm.mortage.select();
		return false;
	} else if (isNaN(frm.insurance.value)) {
		alert("Home insurance amount must be numeric only");
		frm.insurance.focus();
		frm.insurance.select();
		return false;
	} else if (isNaN(frm.telephone.value)) {
		alert("Telephone amount must be numeric only");
		frm.telephone.focus();
		frm.telephone.select();
		return false;
	} else if (isNaN(frm.gasesb.value)) {
		alert("GAS/ESB amount must be numeric only");
		frm.gasesb.focus();
		frm.gasesb.select();
		return false;
	} else if (isNaN(frm.tv.value)) {
		alert("TV amount must be numeric only");
		frm.tv.focus();
		frm.tv.select();
		return false;
	} else if (isNaN(frm.mobile.value)) {
		alert("Mobile phone amount must be numeric only");
		frm.mobile.focus();
		frm.mobile.select();
		return false;
	} else if (isNaN(frm.carloan.value)) {
		alert("Car loan amount must be numeric only");
		frm.carloan.focus();
		frm.carloan.select();
		return false;
	} else if (isNaN(frm.carinsurance.value)) {
		alert("Car insurance amount must be numeric only");
		frm.carinsurance.focus();
		frm.carinsurance.select();
		return false;
	} else if (isNaN(frm.healthinsurance.value)) {
		alert("Health insurance amount must be numeric only");
		frm.healthinsurance.focus();
		frm.healthinsurance.select();
		return false;
	} else if (isNaN(frm.regularsavings.value)) {
		alert("Regular savings amount must be numeric only");
		frm.regularsavings.focus();
		frm.regularsavings.select();
		return false;
	} else if (isNaN(frm.pensioncontributions.value)) {
		alert("Pension Contribution amount must be numeric only");
		frm.pensioncontributions.focus();
		frm.pensioncontributions.select();
		return false;
	} else if (isNaN(frm.groceries.value)) {
		alert("Groceries amount must be numeric only");
		frm.groceries.focus();
		frm.groceries.select();
		return false;
	} else if (isNaN(frm.solcialising.value)) {
		alert("Socialising amount must be numeric only");
		frm.solcialising.focus();
		frm.solcialising.select();
		return false;
	} else if (isNaN(frm.otheroutgoings.value)) {
		alert("Other outgoings amount must be numeric only");
		frm.otheroutgoings.focus();
		frm.otheroutgoings.select();
		return false;	
	} else {
		var result = parseInt(frm.mortage.value) + parseInt(frm.insurance.value) + parseInt(frm.telephone.value) + parseInt(frm.gasesb.value) + parseInt(frm.tv.value) + parseInt(frm.mobile.value) + parseInt(frm.carloan.value) + parseInt(frm.carinsurance.value) + parseInt(frm.healthinsurance.value) + parseInt(frm.regularsavings.value) + parseInt(frm.pensioncontributions.value) + parseInt(frm.groceries.value) + parseInt(frm.solcialising.value) + parseInt(frm.otheroutgoings.value);
		frm.totaloutgoings.value = "€" + result;	
	}
	
}

function asset_calc_new (frm) {
	var currentage = frm.currentage.value;
	var annualsalary = frm.annualsalary.value;	
	
	//frm.retiresalary.value = "";
	frm.totalvalue.value = "";		
	
	if (currentage == "") {
		alert("Current age must be entered");
		frm.currentage.focus();
		return false;
	} else if (isNaN(currentage)) {
		alert("Current age must be numeric only");
		frm.currentage.focus();
		frm.currentage.select();
		return false;
	} else if (annualsalary == "") {
		alert("Annual salary must be entered");
		frm.annualsalary.focus();
		return false;
	} else if (isNaN(annualsalary)) {
		alert("Annual salary must be numeric only");
		frm.annualsalary.focus();
		frm.annualsalary.select();
		return false;		
	} else {
		var noyears = 65 - currentage;	
		var inflatedsalary = annualsalary;
		var totalincome = annualsalary;
		totalincome = parseInt(totalincome);
		
		for(x=currentage;x<65;x++) {
			inflatedsalary = inflatedsalary * 1.02;	
			var temp = parseInt(inflatedsalary);
			
			totalincome = totalincome + inflatedsalary;
		}			
		//inflatedsalary = round_decimals(inflatedsalary,0);
		//inflatedsalary = commify(inflatedsalary)		
		totalincome =  round_decimals(totalincome,0);
		totalincome = commify(totalincome)		
		
		//frm.retiresalary.value = inflatedsalary;
		frm.totalvalue.value = totalincome;	
	}
}

	