//Utility Functions

function getURLParameter(name) {
	return decodeURI((RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || [, null])[1]);
}

var format = getURLParameter('format');
if (format == 'print') {
		document.write('<link href="/view/View/PUBLIC/theme/print.css" rel="stylesheet" type="text/css" media="all" />');
} else {
		document.write('<link href="/view/View/PUBLIC/theme/print.css" rel="stylesheet" type="text/css" media="print" />');
		document.write('<link href="/view/View/PUBLIC/theme/style.css" rel="stylesheet" type="text/css"  media="screen" />');
		document.write('<link href="/view/View/PUBLIC/theme/eWebEditPro.css" rel="stylesheet" type="text/css"  media="screen" />');	
		//Separated for use in third-party direct content
		document.write('<link href="/view/View/PUBLIC/theme/eWebEditPro-footer.css" rel="stylesheet" type="text/css"  media="screen" />');	
}

function removeSpecialCharacters(str) {
	re = /\$|,|@|#|~|`|\%|\*|\^|\&|\(|\)|\+|\=|\[|\-|\_|\]|\[|\}|\{|\;|\:|\'|\"|\<|\>|\?|\||\\|\!|\$|\./g;
	return str.replace(re, "");
}

//Account Select Box
function gotoAccount() {

	var URL_AccountList = new Array(6);
	 URL_AccountList[0] = '';
	 URL_AccountList[1] = 'http://www.prudential.com/media/managed/htmlembed/pruonlinelogin_redirect.html?quadr=topright&name=insurancemfann';
	 URL_AccountList[2] = 'http://www.prudential.com/media/managed/pru_global_header_retirement_link.html';
	 URL_AccountList[3] = 'http://www.prudential.com/commandlogin'
	 URL_AccountList[4] = URL_AccountList[0];
	 URL_AccountList[5]   = 'http://www.prudential.com/globallogin?quadr=topright&name=allloginoptions'
	 
	 var i = document.frmAccount.selAccount.selectedIndex;
	 document.frmAccount.selAccount.selectedIndex = 0;
	 
	 if ((i!=0)&&(i!=4)) {
	  top.location.href = URL_AccountList[i];
	 }
}

//Open Calc Function, used in related link content (used on related link toolbox)
function open_calc_window(strLocation, strWinName) {
	rexp = /https:/
	if (rexp.test(strLocation)) {
		msgWindow = window.open(strLocation, null, 'height=500,width=500,location=no,scrollbars=yes,status=yes,menubars=no,toolbars=no,resizable=yes');
	} else {
		msgWindow = window.open(strLocation, null, 'height=500,width=500,location=no,scrollbars=yes,menubars=no,toolbars=no,resizable=yes');
	}
	msgWindow.opener = self;
}

//PHP Pages
function addbookmark() {
	var bookmarkurl = window.location;
	var bookmarktitle = "Prudential Web Site"
	if (document.all) window.external.AddFavorite(bookmarkurl, bookmarktitle)
}	

//GI PRODUCER SEARCH FUNCTION

function getOptionsId(objOpt, val) {
	var i;
	for (i = 0; i < objOpt.options.length; i++) if (objOpt.options[i].value == val) break;
	return i = (i == objOpt.options.length) ? objOpt.options.selectedIndex : i;
}

function TrimUsingRecursion(str) {
	if (str.charAt(0) == " ") {
		str = TrimUsingRecursion(str.substring(1));
	}
	if (str.charAt(str.length - 1) == " ") {
		str = TrimUsingRecursion(str.substring(0, str.length - 1));
	}
	return str;
}

function theSearch() {
	var stateKey = "";
	if (document.getElementById("state").value != "") {
		stateKey = "-keywords:" + document.getElementById("state").value;
	} else {
		alert("Please select a state.");
		if (document.getElementById("state")) document.getElementById('state').focus();
		return false;
	}
	var query = TrimUsingRecursion(document.getElementById('query').value);
	document.getElementById('query').value = query;
	var qt = document.getElementById("qt").value;
	var qtQ = query;
	if ((query == "") || (query == "Search")) {
		alert("Please enter a keyword or phrase in the text box.");
		document.getElementById('query').focus();
		return false;
	} else {
		document.getElementById("qt").value = qtQ;
		if (document.frmSearchForm2.qtype.value == 0) {
			document.getElementById('qt').value = stateKey + "  " + "\"" + query + "\"";
		} else {
			if (document.frmSearchForm2.qtype.value == 1) {
				qtQ = qtQ.replace(" ", "||");
				qtQ = stateKey + " " + qtQ;
				document.getElementById('qt').value = qtQ;
			} else {
				document.getElementById('qt').value = stateKey + " " + document.getElementById('qt').value;
			}
			document.frmSearchForm2.submit();
		}
	}
}

$(document).ready(function () {
			

	//Move the banner from the floater if avail.
	$('#move-banner').appendTo($('#bannerholder'));
	
	//Hide floater div if only holding temporary banner
	if($.trim($("#floater").html()).length==0)
  	$("#floater").remove(); 	
	
	//Search Box
	$('.gsa-term').focus(function () {
		if ($(this).val() == $(this)[0].title) {
			$(this).val("");
		}
	});
	$('.gsa-term').blur(function () {
		if ($(this).val() == "") {
			$(this).val($(this)[0].title);
		}
	});
	$('.gsa-term').blur();
	$(".gsa-go").click(function () {
		var str = document.gsa.term.value;
		if ((str == null || str.length == 0 || str == 'SEARCH')) {
			alert("Please enter a keyword or phrase in the text box.");
			document.gsa.term.focus();
			return false;
		} else {
			document.gsa.term.value = removeSpecialCharacters(document.gsa.term.value);
			document.gsa.as_q.value = document.gsa.term.value;
			document.gsa.submit();
		}
	});

	// Allow hitting return/enter to submit parent form.
	$("form#gsa input, form#gsa select").live('keypress', function (e) {
		if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
			var str = document.gsa.term.value;
			if ((str == null || str.length == 0 || str == 'SEARCH')) {
				alert("Please enter a keyword or phrase in the text box.");
				document.gsa.term.focus();
				return false;
			} else {
				document.gsa.term.value = removeSpecialCharacters(document.gsa.term.value);
				document.gsa.as_q.value = document.gsa.term.value;
				document.gsa.submit();
			}
			return false;
		} else {
			return true;
		}
	});

	// related-link-box
	$('.related-link-box').first().addClass('first');

	// related-link-box
	$('.product-info-group').first().addClass('first');

	//Content Border
	function reinteriorborder() {
		if ($('#interiorcontentwrapper').size() > 0) {
			$('#interiorcontentwrapper #contentholder').css('border-top', '1px solid #b7b7b7');
			$('#interiorcontentwrapper').css('backgroundPosition', '0 0');
		}
	}
	
	//Secondary Navigation Panel Functions
	function menuclose() {
		$("div.menupanel").slideUp('fast');
		if ($.browser.msie) {
			$(this).css('background-position-y', '0px');
		} else {
			$('.products').css('background-position', getbgp($('.products'), 'x') + ' 0px')
		}
		$('.fullexposure .menusection').hide();
		$('.fullexposure .menusection:eq(0)').addClass('activems');
	}

	function getbgp(ele, position) {
		var positionvalue;
		if (position == 'x') {
			//alert(ele.css('background-position-x'));
			positionvalue = ele.css('background-position').split(' ')[0];
		} else {
			positionvalue = ele.css('background-position').split(' ')[1];
		}
		return positionvalue;
	}

    //Click event handler for any anchor that has an href that ends in top
	$("a[href='#top']").click(function() {
	  $("html, body").animate({ scrollTop: 0 }, "slow");
	  return false;
	});

	  
	$('.navitem').hover(

	function () {
		if ($.browser.msie) {
			$(this).css('background-position-y', '-27px');
		} else {
			$(this).css('background-position', getbgp($(this), 'x') + ' -27px');
		}
	}, function () {
		if (!($(this).hasClass('products') && $("div.menupanel").is(":visible"))) {
			if ($.browser.msie) {
				$(this).css('background-position-y', '0px');
			} else {
				$(this).css('background-position', getbgp($(this), 'x') + ' 0px');
			}
		}
	});
	$('.navitem').click(function () {
		if (!$(this).hasClass('products')) {
			if ($.browser.msie) {
				$(this).css('background-position-y', '0px');
			} else {
				if (!($(this).hasClass('products') && $("div.menupanel").is(":visible"))) {
					$(this).css('background-position', getbgp($(this), 'x') + ' 0px');
				}
			}
		}
	});
	$('.products').click(function () {
		if ($("div.menupanel").is(":hidden")) {
			if ($.browser.msie) {
				$(this).css('background-position-y', '-27px');
			} else {
				$(this).css('background-position', getbgp($(this), 'x') + ' -27px')
			}
			$('.activems').show();
			$('.primarycategories').removeClass('pcbusactive').addClass('pcindactive');
			$("div.menupanel").slideDown();
			//$('.topnav').addClass('opennav');
			if ($('#interiorcontentwrapper').size() > 0) {
				$('#interiorcontentwrapper #contentholder').css('border-top', '0');
				$('#interiorcontentwrapper').css('backgroundPosition', '0 -1px');
			}
		} else {
			menuclose();
			reinteriorborder();
		}
	});
	$('.closemenu').click(function () {
		menuclose();
		reinteriorborder();
	});
	$('.primarycategories a.pcindividual').click(function () {
		if ($('.primarycategories').hasClass('pcbusactive')) {
			$('.primarycategories').removeClass('pcbusactive').addClass('pcindactive');
			$('.fullexposure .menusection:eq(1)').hide().delay(800).siblings().fadeIn(800);
		}
	});
	$('.primarycategories a.pcbusiness').click(function () {
		if ($('.primarycategories').hasClass('pcindactive')) {
			$('.primarycategories').removeClass('pcindactive').addClass('pcbusactive');
			$('.fullexposure .menusection:eq(0)').hide().delay(800).siblings().fadeIn(800);
		}
	});
	$('.morespots p').hover(
	function () {
		$(this).addClass('phover');
		$('a', this).addClass('ahover');
		if ($(this).prev().size() > 0) {
			$(this).prev().addClass('phover');
		}
	}, function () {
		$(this).removeClass('phover');
		$('a', this).removeClass('ahover');
		if ($(this).prev().size() > 0) {
			$(this).prev().removeClass('phover');
		}
	});

	//PHP Associate List - Alternate Table Row Highlighting
	$(".phpAssociateList #associates tr:odd").addClass("odd"); //#C4D8E2 
	$(".phpAssociateList #associates tr:even").addClass("even"); //#FFF 	
	
	// Related Link hover arrow.
	$(".related-link-box ul li a").each(function () {
		$(this).append('<img class="h" src="/media/system/cda/public/x.gif" height="7" width="6" />');
	});
	
	$('.related-link-box ul li a').mouseover(function () {
		$(this).find('img.h').remove();
		$(this).append('<img class="h" src="/media/system/cda/public/bg-menuitemhover.png" />');
	}).mouseout(function () {
		$(this).find('img.h').remove();
		$(this).append('<img class="h" src="/media/system/cda/public/x.gif" height="7" width="6" />');
		});

	//Footer Metrics - WebTrends dcsMultiTrack
	if (document.domain.search('prudential.com') != -1) {
		$('#linksectionwrapper a').click(function() {
			if ($(this).text().length > 1) 	dcsMultiTrack("DCS.dcsuri", "Ftr Link: " + encodeURIComponent($(this).text()) + " / " + page_id, "DCSext.pruURI", "Ftr Link: " + encodeURIComponent($(this).text()) + " / " + page_id)
		}); 
		$('#footerwrapper a').click(function() {
			if ($(this).text().length > 1) 	dcsMultiTrack("DCS.dcsuri", "Ftr Link: " + encodeURIComponent($(this).text()) + " / " + page_id, "DCSext.pruURI", "Ftr Link: " + encodeURIComponent($(this).text()) + " / " + page_id)
		});
		$('a.bringchallenges').click(function() {
			dcsMultiTrack("DCS.dcsuri", "Ftr Link: Prudential Bring Your Challenges Logo / " + page_id, "DCSext.pruURI", "Ftr Link: Prudential Bring Your Challenges Logo / " + page_id)
		}); 
		$('a.nav-metrics').click(function() {
			dcsMultiTrack("DCS.dcsuri", encodeURIComponent($(this).text()) + " / " + page_id + " - Header", "DCSext.pruURI", encodeURIComponent($(this).text()) + " / " + page_id + " - Header")
		}); 				
		return true;
	}

});

