/**
 * site_quote_life_mortgage.js - JavaScript Functions.
 * @package site_quote_life_mortgage.js
*/

$(document).ready(function() { 
    var s_path = window.location.pathname;
    var s_page_raw = s_path.substring((s_path.lastIndexOf('/') + 1), s_path.lastIndexOf('.')).toLowerCase();
    var s_page = s_page_raw.toLowerCase();

    var additional_header = "";
   	var life_amount_label = "&nbsp; Amount of Life Cover";
	var serious_amount_label = "&nbsp; Amount of Serious Illness Cover";
    //alert(s_page);

    if ((s_page == "life-assurance-serious-illness") || (s_page == "mortgage-protection")) { 
        $('#info_1_link').fancyZoom({width:400, height:200});
        if ($("#pd_l_accelerated_row").length > 0) { 
        	$("#pd_l_accelerated_row").hide();
        }
        if ((s_page == "life-assurance-serious-illness") && 
        		((($("#pd_l_p1_amount_serious").attr("value") != "") && ($("#pd_l_p1_amount_serious").attr("value") != "0")) || 
        		(($("#pd_l_p2_amount_serious").attr("value") != "") && ($("#pd_l_p2_amount_serious").attr("value") != "0"))) ) { 
        	$("#pd_l_accelerated_row").show();
        }
    	if ($("#pd_l_single_joint").length > 0) { 
    		var type = $("#pd_l_single_joint :selected").attr("value");
	        if (type == "joint") { 
	        	if (s_page == "life-assurance-serious-illness") { 
	       		//alert("joint life");
	            //$("#seperator_row_1").show();
	            //$("#seperator_row_2").show();
	            //$("#seperator_row_3").show();
	            //$("#life_2_amount_row").show();
	            //$("#life_2_serious_row").show();
	        	}
	        	$("#pd_l_p2").show();
	        }
	        else if (type == "dual") { 
        		$("#life1_life_amount_label").html(life_amount_label+" : Life 1");
        		$("#life1_serious_amount_label").html(serious_amount_label+" : Life 1");
	            $("#seperator_row_1").show();
	            $("#seperator_row_2").show();
	            $("#seperator_row_3").show();
	            $("#life_2_amount_row").show();
	            $("#life_2_serious_row").show();
	            $("#pd_l_p2").show();
	        }
	        else { 
	        	$("#pd_l_p2").hide();
	            $("#seperator_row_1").hide();
	            $("#seperator_row_2").hide();
	            $("#seperator_row_3").hide();
	            $("#life_2_amount_row").hide();
	            $("#life_2_serious_row").hide();
	        }
    	}
    	if ($("#additional_header").length > 0) { 
    		additional_header_default = $("#additional_header").text();
    	}
    	if (s_page == "mortgage-protection") { 
            if ($("#pd_l_options_table").length > 0) { 
            	$("#pd_l_options_table").hide();
            }
        }
    }
    if (s_page == "life-protection-over-50") { 
        $('#info_1_link').fancyZoom({width:400, height:200});
    }
});

function check_policy() { 
	var life_amount_label = "&nbsp; Amount of Life Cover";
	var serious_amount_label = "&nbsp; Amount of Serious Illness Cover";
    var type = $("#pd_l_single_joint :selected").attr("value");
    var amount_serious_1 = $("#pd_l_p1_amount_serious").attr("value");
    var amount_serious_2 = $("#pd_l_p2_amount_serious").attr("value");

    //alert(type);
    if (type == "single") { 
        $("#pd_l_p2").hide();
        $("#life1_life_amount_label").html(life_amount_label);
        $("#life1_serious_amount_label").html(serious_amount_label);
        $("#seperator_row_1").hide();
        $("#seperator_row_2").hide();
        $("#seperator_row_3").hide();
        $("#life_2_amount_row").hide();
        $("#life_2_serious_row").hide();
        $("#pd_l_accelerated_row").attr("value", "Y");
        $("#pd_l_accelerated_row").hide();
    }
    else if (type == "joint") { 
        $("#pd_l_p2").show();
        $("#life1_life_amount_label").html(life_amount_label);
        $("#life1_serious_amount_label").html(serious_amount_label);
        if ($("#object").val() == "life-serious") { 
        	//$("#seperator_row_1").show();
        	//$("#seperator_row_2").show();
        	//$("#seperator_row_3").show();
        	//$("#life_2_amount_row").show();
        	//$("#life_2_serious_row").show();
        }
        $("#seperator_row_1").hide();
        $("#seperator_row_2").hide();
        $("#seperator_row_3").hide();
        $("#life_2_amount_row").hide();
        $("#life_2_serious_row").hide();
    }
    else if (type == "dual") { 
        $("#life1_life_amount_label").html(life_amount_label+": Life 1");
        $("#life1_serious_amount_label").html(serious_amount_label+": Life 1");
        $("#pd_l_p2").show();
        $("#seperator_row_1").show();
        $("#seperator_row_2").show();
        $("#seperator_row_3").show();
        $("#life_2_amount_row").show();
        $("#life_2_serious_row").show();
        if (((amount_serious_1 != "") && (amount_serious_1 != "0")) || ((amount_serious_2 != "") && (amount_serious_2 != "0"))) { 
        	$("#pd_l_accelerated_row").show();
        }
        else { 
        	$("#pd_l_accelerated_row").hide();
        }
    }
}

function check_accelerated_available() { 
    //var amount_life = $("#pd_l_p1_amount_life").attr("value");
    var amount_serious_1 = $("#pd_l_p1_amount_serious").attr("value");
    var amount_serious_2 = $("#pd_l_p2_amount_serious").attr("value");
    //alert("amount_life: "+amount_life+"   -  amount_serious: "+amount_serious);
    //if ((amount_life == "") || (amount_serious == "") || (amount_life == "0") || (amount_serious == "0")) { 
    //    $("#pd_l_accelerated_row").hide();
    //}
    //else { 
    //	$("#pd_l_accelerated_row").show();
    //}
    //alert("amount_serious_1: '"+amount_serious_1 +"', amount_serious_2: '"+amount_serious_2+"'");
    if (((amount_serious_1 != "") && (amount_serious_1 != "0")) || ((amount_serious_2 != "") && (amount_serious_2 != "0"))) { 
    	//alert("hkjhkj");
    	$("#pd_l_accelerated_row").show();
    }
    else { 
        $("#pd_l_accelerated_row").hide();
    }
}

function checkQuotes() { 
	var pd_l_convertible = $("#pd_l_convertible").is(':checked');
	if (pd_l_convertible) { 
		$("#pd_l_monthprice").val(conv_quote);
		$("#pd_l_monthprice_refund").val(conv_quote_refund);
		$("#pd_l_monthprice_net_cost").val(conv_quote_net);
	}
	else { 
		$("#pd_l_monthprice").val(life_quote);
		$("#pd_l_monthprice_refund").val(life_quote_refund);
		$("#pd_l_monthprice_net_cost").val(life_quote_net);
	}
}

function showLoader() { 
    document.getElementById("loading").style.display="block";
}

function check_over50_benefit() { 
	var details = 'Invalid information entered.\n\n';
	var pd_l_over_50_benefit_amount = $("#pd_l_over_50_benefit_amount").attr("value");
	if (pd_l_over_50_benefit_amount > 25000) { 
		details+= '- Please enter a Benefit Amount 25,000 Euro or less.    \n\n';
		details+= 'Please correct the above before proceeding.    \n\n';
		alert(details);
	}
}

function quoteOver50() { 
	var pd_l_p1_dob_date = $("#pd_l_p1_dob_date").attr("value");
	var pd_l_p1_dob_month = $("#pd_l_p1_dob_month").attr("value");
	var pd_l_p1_dob_year = $("#pd_l_p1_dob_year").attr("value");
	var pd_l_p1_sex = $("#pd_l_p1_sex :selected").attr("value");
	//var pd_l_over_50_premium_amount = $("#pd_l_over_50_premium_amount").attr("value");
	var pd_l_over_50_benefit_amount = $("#pd_l_over_50_benefit_amount").attr("value");
	if (pd_l_p1_sex != "") { 
    $.ajax({
        type: "POST",
        url: data_helper,
        cache: false,
        data: { 
            object: 'over_50', 
            operation: 'get',
            pd_l_p1_dob_date: pd_l_p1_dob_date,
            pd_l_p1_dob_month: pd_l_p1_dob_month,
            pd_l_p1_dob_year: pd_l_p1_dob_year,
            pd_l_p1_sex: pd_l_p1_sex,
            //pd_l_over_50_premium_amount: pd_l_over_50_premium_amount,
            pd_l_over_50_benefit_amount: pd_l_over_50_benefit_amount
        },
        success: function(return_data) { 
        	//alert("Return Data : " + return_data);
            if (check_success(return_data)) { 
                return_data_array = return_data.split('|');
                element_array = return_data_array[1].split(':');
                //$("#pd_l_over_50_benefit_by_premium").attr("value", element_array[0]);
                if (pd_l_over_50_benefit_amount != "") { 
                	$("#pd_l_over_50_premium_for_benefit").attr("value", +element_array[1]);
                }
            }
            else { 
                var status = return_data.replace(statusFailed+statusSeperator, "");
                alert("Status : "+status);
            }
        }
    }); // ajax
	}
}

/**
* CheckLifeStage1
*/
function CheckLifeStage1(stage){ 
	var inputVal = document.form.pd_l_p1_email.value;
	var pass = true;
	var amounts_note = false;
	var details = 'Invalid information entered.\n\n';
	var submit_button = $("#submit_button").position();
	var overlay_top = "+=1000px";

	if (document.form.pd_l_single_joint.value == '') { 
		details+= ((stage == "life-serious") ? "- Please select Single, Joint or Dual.\n\n" : "- Please select Single or Joint.\n\n"); 
		if (pass) document.form.pd_l_single_joint.focus();
		pass = false;
	}
	if (document.form.pd_l_term.value == '') { 
		details+= "- Please select Length of Term.\n\n"; 
		if (pass) document.form.pd_l_term.focus();
		pass = false;
	}
	if (document.form.pd_l_term.value == '') { 
		details+= "- Please select Length of Term.\n\n"; 
		if (pass) document.form.pd_l_term.focus();
		pass = false;
	}
	if (document.form.pd_l_single_joint.value == "dual") { 
		if (document.form.pd_l_p1_amount_life.value == '') { 
			details+= "- Please enter an Amount of Life Cover: Life 1. *\n\n"; 
			if (pass) document.form.pd_l_p1_amount_life.focus();
			pass = false;
			amounts_note = true;
		}
		if (document.form.pd_l_p1_amount_serious.value == '') { 
			details+= "- Please enter an Amount of Serious Illness Cover: Life 1. *\n\n"; 
			if (pass) document.form.pd_l_p1_amount_serious.focus();
			pass = false;
			amounts_note = true;
		}
		if (document.form.pd_l_p2_amount_life.value == '') { 
			details+= "- Please enter an Amount of Life Cover: Life 2. *\n\n"; 
			if (pass) document.form.pd_l_p2_amount_life.focus();
			pass = false;
			amounts_note = true;
		}
		if (document.form.pd_l_p2_amount_serious.value == '') { 
			details+= "- Please enter an Amount of Serious Illness Cover: Life 2. *\n\n"; 
			if (pass) document.form.pd_l_p2_amount_serious.focus();
			pass = false;
			amounts_note = true;
		}
	}
	else { 
		if (document.form.pd_l_p1_amount_life.value == '') { 
			details+= "- Please enter an Amount of Life Cover. *\n\n"; 
			if (pass) document.form.pd_l_p1_amount_life.focus();
			pass = false;
			amounts_note = true;
		}
		if (document.form.pd_l_p1_amount_serious.value == '') { 
			details+= "- Please enter an Amount of Serious Illness Cover. *\n\n"; 
			if (pass) document.form.pd_l_p1_amount_serious.focus();
			pass = false;
			amounts_note = true;
		}
		if ((stage == "mortgage-protection") && (document.form.pd_l_p1_amount_life.value < document.form.pd_l_p1_amount_serious.value)) { 
			details+= "- Please enter an Amount of Serious Illness Cover that is Less Than or Equal To the Amount of Live Cover. *\n\n"; 
			if (pass) document.form.pd_l_p1_amount_serious.focus();
			pass = false;
			amounts_note = true;
		}
	}
	if (stage == "life-serious") { 
		if (document.form.pd_l_indexation.value == '') { 
			details+= "- Please select whether you choose Indexation.\n\n"; 
			if (pass) document.form.pd_l_indexation.focus();
			pass = false;
		}
	}
	if (document.form.pd_l_p1_name.value == '') { 
		details+= '- Please enter Your Name.\n\n';
		if (pass) document.form.pd_l_p1_name.focus();
		pass = false;
	}
	if (inputVal == '') { 
		details+= '- Please enter Your Email Address.\n\n';
    	if (pass) document.form.pd_l_p1_email.focus();
		pass = false;
	}
	else if (inputVal.indexOf("@") == -1){ 
		details+= '- Please enter a properly formatted Email Address.\n\n';
    	if (pass) document.form.pd_l_p1_email.focus();
		pass = false;
	}
	else if (inputVal.indexOf(".") == -1){ 
		details+= '- Please enter a properly formatted Email Address.\n\n';
    	if (pass) document.form.pd_l_p1_email.focus();
		pass = false;
	}
	if (document.form.pd_l_p1_phone.value == '') { 
		details+= '- Please enter Your Phone Number.\n\n';
		if (pass) document.form.pd_l_p1_phone.focus();
		pass = false;
	}
	if (document.form.pd_l_p1_address_1.value == '') { 
		details+= '- Please enter Your Address : Line 1.\n\n';
		if (pass) document.form.pd_l_p1_address_1.focus();
		pass = false;
	}
	if (document.form.pd_l_p1_address_2.value == '') { 
		details+= '- Please enter Your Address : Line 2.\n\n';
		if (pass) document.form.pd_l_p1_address_2.focus();
		pass = false;
	}
	if (document.form.pd_l_p1_area.value == '') { 
		details+= "- Please select your County.\n\n"; 
		if (pass) document.form.pd_l_p1_area.focus();
		pass = false;
	}
	if (document.form.pd_l_p1_dob_date.value == '') { 
		details+= '- Please select Your Date of Birth : Date.\n\n';
		if (pass) document.form.pd_l_p1_dob_date.focus();
		pass = false;
	}
	if (document.form.pd_l_p1_dob_month.value == '') { 
		details+= '- Please select Your Date of Birth : Month.\n\n';
		if (pass) document.form.pd_l_p1_dob_month.focus();
		pass = false;
	}
	if (document.form.pd_l_p1_dob_year.value == '') { 
		details+= '- Please select Your Date of Birth : Year.\n\n';
		if (pass) document.form.pd_l_p1_dob_year.focus();
		pass = false;
	}
	if (document.form.pd_l_p1_sex.value == '') { 
		details+= "- Please select Male or Female.\n\n"; 
		if (pass) document.form.pd_l_p1_sex.focus();
		pass = false;
	}
	if (document.form.pd_l_p1_smoker.value == '') { 
		details+= "- Please select if you are a Smoker.\n\n"; 
		if (pass) document.form.pd_l_p1_smoker.focus();
		pass = false;
	}
	if ((document.form.pd_l_single_joint.value == 'joint') || (document.form.pd_l_single_joint.value == 'dual')) { 
		if (document.form.pd_l_p2_dob_date.value == '') { 
			details+= '- Please select Second Person on Policy : Date of Birth : Date.\n\n';
			if (pass) document.form.pd_l_p2_dob_date.focus();
			pass = false;
		}
		if (document.form.pd_l_p2_dob_month.value == '') { 
			details+= '- Please select Second Person on Policy : Date of Birth : Month.\n\n';
			if (pass) document.form.pd_l_p2_dob_month.focus();
			pass = false;
		}
		if (document.form.pd_l_p2_dob_year.value == '') { 
			details+= '- Please select Second Person on Policy : Date of Birth : Year.\n\n';
			if (pass) document.form.pd_l_p2_dob_year.focus();
			pass = false;
		}
		if (document.form.pd_l_p2_sex.value == '') { 
			details+= "- Please select Second Person on Policy : Male or Female.\n\n"; 
			if (pass) document.form.pd_l_p2_sex.focus();
			pass = false;
		}
		if (document.form.pd_l_p2_smoker.value == '') { 
			details+= "- Please select Second Person on Policy : if they are a Smoker.\n\n"; 
			if (pass) document.form.pd_l_p2_smoker.focus();
			pass = false;
		}
	}

	if (pass) { 
		//overlay_top = "+="+submit_button.top+"px";
		//$("#float_insurers").center(true).show().animate({"top": overlay_top}, "slow").focus();
		//return true;
		$("#float_insurers").center(true).css('top', (submit_button.top - 50)).show();
		setTimeout("document.forms[0].submit();", 4000);
	} 
	else { 
		details+= 'Please correct the above before proceeding.    \n\n';
		if (amounts_note) details+= '* (0 if not required)    \n\n';
		alert(details);
		//$("#float_insurers").center(true).css('top', (submit_button.top - 150)).css('left', (submit_button.left + 200)).show();
		return false;
	}
}

/**
* CheckOver50
*/
function CheckOver50() { 
	var inputVal = document.form.pd_l_p1_email.value;
	var pass = true;
	var details = 'Invalid information entered.\n\n';
	var submit_button = $("#submit_button").position();
	var overlay_top = "+=1000px";

	if (document.form.pd_l_p1_name.value == '') { 
		details+= '- Please enter Your Name.\n\n';
		if (pass) document.form.pd_l_p1_name.focus();
		pass = false;
	}
	if (inputVal == '') { 
		details+= '- Please enter Your Email Address.\n\n';
    	if (pass) document.form.pd_l_p1_email.focus();
		pass = false;
	}
	else if (inputVal.indexOf("@") == -1){ 
		details+= '- Please enter a properly formatted Email Address.\n\n';
    	if (pass) document.form.pd_l_p1_email.focus();
		pass = false;
	}
	else if (inputVal.indexOf(".") == -1){ 
		details+= '- Please enter a properly formatted Email Address.\n\n';
    	if (pass) document.form.pd_l_p1_email.focus();
		pass = false;
	}
	if (document.form.pd_l_p1_phone.value == '') { 
		details+= '- Please enter Your Phone Number.\n\n';
		if (pass) document.form.pd_l_p1_phone.focus();
		pass = false;
	}
	if (document.form.pd_l_p1_address_1.value == '') { 
		details+= '- Please enter Your Address : Line 1.\n\n';
		if (pass) document.form.pd_l_p1_address_1.focus();
		pass = false;
	}
	if (document.form.pd_l_p1_address_2.value == '') { 
		details+= '- Please enter Your Address : Line 2.\n\n';
		if (pass) document.form.pd_l_p1_address_2.focus();
		pass = false;
	}
	if (document.form.pd_l_p1_area.value == '') { 
		details+= "- Please select your County.\n\n"; 
		if (pass) document.form.pd_l_p1_area.focus();
		pass = false;
	}
	if (document.form.pd_l_p1_dob_date.value == '') { 
		details+= '- Please select Your Date of Birth : Date.\n\n';
		if (pass) document.form.pd_l_p1_dob_date.focus();
		pass = false;
	}
	if (document.form.pd_l_p1_dob_month.value == '') { 
		details+= '- Please select Your Date of Birth : Month.\n\n';
		if (pass) document.form.pd_l_p1_dob_month.focus();
		pass = false;
	}
	if (document.form.pd_l_p1_dob_year.value == '') { 
		details+= '- Please select Your Date of Birth : Year.\n\n';
		if (pass) document.form.pd_l_p1_dob_year.focus();
		pass = false;
	}
	if (document.form.pd_l_p1_sex.value == '') { 
		details+= "- Please select Male or Female.\n\n"; 
		if (pass) document.form.pd_l_p1_sex.focus();
		pass = false;
	}
	if (document.form.pd_l_over_50_benefit_amount.value == '') { 
		details+= "- Please enter the Benefit Amount you would like a Quote for.\n\n"; 
		if (pass) document.form.pd_l_over_50_benefit_amount.focus();
		pass = false;
	}
	if (document.form.pd_l_over_50_benefit_amount.value > 25000) { 
		details+= "- Please enter a Benefit Amount 25,000 Euro or less.\n\n"; 
		if (pass) document.form.pd_l_over_50_benefit_amount.focus();
		pass = false;
	}

	if (pass) { 
		//overlay_top = "+="+submit_button.top+"px";
		//$("#float_insurers").center(true).show().animate({"top": overlay_top}, "slow").focus();
		//return true;
		//$("#float_insurers").show().center(true);
		$("#float_insurers").center(true).css('top', (submit_button.top - 50)).show();
		setTimeout("document.forms[0].submit();", 4000);
		return true;
	} 
	else { 
		details+= 'Please correct the above before proceeding.    \n\n';
		alert(details);
		return false;
	}
}
