/* 
 * Template JavaScript Additions
 * 
 * These methods add the following functionality to the templates:
 * 	1. Ability to have expanding/collapsing page sections
 * 	2. Ability to enable and embed any SWF file using SWFObject
 * 	3. Abillity to enable/disable various side sections of the template
 * 		on a page-by-page basis
 * 
 * The functionality is contained within a namespace to avoid confusion.
 * While this code will be written in the most portable way possible,
 * given time constraints, it is intended for the Liberty Mutual Savvy
 * website, and therefore some additional work will be required to
 * reuse this code on other sites.
 * 
 * This code relies on the jQuery library
 */

 var liberty = {
 	headline: function(className,targetType) {
		// Get a collection of all elements matching className
		var headlines = $("." + className);
		// Define the behavior
		var showContent = function(e) {
			$(this).toggleClass("open");
			// Get the element to show/hide, as defined by targetType
			var myTarget = $(this).next(targetType);
			// Toggle display of the element
			myTarget.toggle("fast").toggleClass("headlineBody");
		}
		// Bind the behavior to the elements
		headlines.click(showContent);
	},
	// This method requires that SWFObject be loaded
	createFlash: function(helperClass,targetID) {
		var helperElem = $("."+helperClass);
		if (helperElem.length) {
			var width = 574;
			var height = 300;
			var swfTitle = "Liberty Life";
			var helperString = helperElem[0].className;
			// console.log(helperString);
			var helperArray = helperString.split(" ");
			// The name of the SWF file must be the second part of the class attribute
			var swfFileName = helperArray[1];
			if (helperArray.length > 2) {
				try {
					width =  helperArray[2].split("_")[0];
					height = helperArray[2].split("_")[1];
					if (helperArray.length == 4) {
						swfTitle = helperArray[3];
					}
				} catch(e) {
					var swfTitle = helperArray[2];
				}
			}
			so = new SWFObject("/flash/"+swfFileName+".swf", swfTitle, width, height, "9", "#FFFFFF");
			so.addParam("wmode", "transparent");
			so.useExpressInstall('/flash/playerProductInstall.swf');
			so.write(targetID);
		} else {
			// console.log("Hiding #"+targetID);
			$("#"+targetID).hide();
		}
	}
 };
 
 // Trigger the function on document ready
 $(function() {
 	// Make the zip code search more interesting
	var zipMessage = "Enter Zip Code";
	$("#zip").focus(function () {
		if ($(this).val() == zipMessage) {
			$(this).val("");
		}
	}).blur(function() {
		if ($(this).val() == "") {
			$(this).val(zipMessage);
		}
	});
 	liberty.headline("headline","p");
	// Check the query string for headline requests
	var keepVisible = $(location.hash);
	$(".headline").next("p").hide();
	try {
		keepVisible.trigger("click");
		$.scrollTo(keepVisible[0],{
			axis: 'y',
			duration: 1000
		});
	} catch(e) {
		// Do nothing
	}
	liberty.createFlash("flashHelper","universalFlashArea");
		
	// Set up ToolTip functionality
	$(".toolTip").Tooltip({
		track: true,
		showURL: false,
		left: -200
	});
 });

//this is for the convert tool
function validateRadio(element, label){
	for (var i=0; i<element.length; i++) {
		if (element[i].checked == true) {
			return "";
		}
	}
	return "-"+label+"\n";
}   

function validateForm(theForm){
	var msg = "";

var label = "Question 1";

			msg += validateRadio(document.forms['ConvertForm'].elements['q1'], label);

var label = "Question 2";

			msg += validateRadio(document.forms['ConvertForm'].elements['q2'], label);
		
var label = "Question 3";

			msg += validateRadio(document.forms['ConvertForm'].elements['q3'], label);

var label = "Question 4";

			msg += validateRadio(document.forms['ConvertForm'].elements['q4'], label);

var label = "Question 5";

			msg += validateRadio(document.forms['ConvertForm'].elements['q5'], label);

var label = "Question 6";

			msg += validateRadio(document.forms['ConvertForm'].elements['q6'], label);			

	if (msg != "") {
		alert("You didn't answer: \n"+msg);
		return false;
	}else{
		scoreIt();
		return false;
	}
}
 
function scoreIt() {

var tagLine1 = "<div class=\"tagLine\">Your Liberty Mutual agent can help you determine what kind of life insurance is right for you, and how much coverage you need. Enter your zip code to find your local agent.</div>";
var tagLine2 = "<div class=\"tagLine\">Your Liberty Mutual agent can conduct a thorough needs review to help determine if permanent insurance is right, and how much coverage you need. Enter your zip code to find your local agent.</div>";

var message = 0
var newMess = new Array()
newMess[0]= "There is an Error with the Message"
newMess[1]= "<div class=\"tagLine\"><h4>You've had some life changes since you bought life insurance. Now is a really good time to talk to your representative about whether you might need permanent coverage.</h4>" + tagLine1 + "</div>";
newMess[2]= "<div class=\"tagLine\"><h4>Your term insurance may still be a good fit for your needs, but it's important to schedule an annual review with your representative to make sure your coverage is enough.</h4>" + tagLine1 + "</div>";
newMess[3]= "<div class=\"tagLine\"><h4>Purchasing life insurance is an important step toward protecting your family's financial future if something were to happen to you. Life insurance can help pay a mortgage, provide for a college education or pay for final expenses.</h4>" + tagLine1 + "</div>"
newMess[4]= "<div class=\"tagLine\"><h4>With premiums that won't increase over time and the ability to accumulate cash value, permanent life insurance may be a good fit for your needs.</h4>" + tagLine2 + "</div>"


var resT = document.getElementById("results");
var resD = document.getElementById("resultsDiv");
var x=document.getElementById("ConvertForm");
var b=0;
var e=0;
for (var i=0;i<x.length;i++) {
if (x.elements[i].checked){
a= Math.abs(x.elements[i].value);

b=b+a
}
}
if (b>=3) {e=1}

else if (b<3) {e=2}

	if (x.elements[1].checked){
e=3
}
	if (x.elements[0].checked && x.elements[10].checked){
e=4
}
resT.innerHTML= newMess[e];
resD.style.visibility="visible";
setText();
}

function setText() {
if (document.getElementById("zip2").value > 0) {
return true;
}
else {
document.getElementById("CFForm_2").reset();
}
}

function clearText()
{
document.getElementById("zip2").value='';
}

function openNewWin(link){
	window.open(link,'popup','scrollbars=yes,width=775,height=800');
}
