﻿// handles enter key for any input text
function inputOnKeyPress(sender, e, action, parms) {
	var keynum = window.event ? e.keyCode : e.which;

	if (keynum == 13) // enter key on the textbox
	{
		eval(action + "(sender,e,parms)");
		return false;
	}
}

// process enter submission. code example from ektron developer site examples
function callSearch(sender, e, parms) {
	//alert("sender: " + sender + "\ne: " + e + "\nparms: " + parms);return;
	var searchBox = document.getElementById(parms[0]);
	if (searchBox != null && searchBox != undefined && searchBox.value != 'Search Site'
		&& searchBox.value != '' && searchBox.value != null) {
		__doPostBack(parms[1], ''); // asp input button does not properly submit a form on enter of input box
	}
	return false;
}

function adjustSideBar() {
	/* Adjust the height of the sidebar appropriately. */
	if (document.getElementById('middle-content-sidebar') != null)
		document.getElementById('middle-content-sidebar').style.height = $('.middle-content')[0].clientHeight + "px";
}

// handles focus/blur for the search box
function SearchBoxFocusBlur(sender, e) {
	if (e.type == 'focus') {
		if (sender.value == 'Search Site') {
			sender.value = '';
		}
	}
	else {
		if (sender.value == '') {
			sender.value = 'Search Site';
		}
	}
}

//sets cookie for user login state
function LoginVisible(isVisible) {
	var cookieName = "LoginVisibility";
	if (isVisible) {
		$.cookie(cookieName, true);
	}
	else {
		$.cookie(cookieName, false);
	}
}


function GetPath(input) {
	var clean = input.toString().replace("http://", "").replace("https://", "");
	var First = clean.indexOf("/", 0);
	var Last = clean.lastIndexOf("/");
	return clean.substring(First, Last + 1);
}

function SwapLink() {

	//    var link = document.createElement('A');
	//    link.setAttribute('href', '#TB_inline?height=300&width=300&inlineId=business-login-check');
	//    link.setAttribute('title', 'Business Banking');
	//    link.setAttribute('style', 'display:none;');
	//    document.appendChild(link);
	//    try { link.click() } catch (err) { alert("Click sucked"); }
	//    try { link.OnClick() } catch (err) { alert("click suck"); }
}

function Set_Cookie(name, value, expires, path, domain, secure) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime(today.getTime());

	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
	if (expires) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date(today.getTime() + (expires));

	document.cookie = name + "=" + escape(value) +
	((expires) ? ";expires=" + expires_date.toGMTString() : "") +
	((path) ? ";path=" + path : "") +
	((domain) ? ";domain=" + domain : "") +
	((secure) ? ";secure" : "");
}

function Get_Cookie(check_name) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split(';');
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for (i = 0; i < a_all_cookies.length; i++) {
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split('=');


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if (cookie_name == check_name) {
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if (a_temp_cookie.length > 1) {
				cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if (!b_cookie_found) {
		return null;
	}
}

var OldWindowOpen = null;

$(document).ready(function() {
	if ($('DIV.main-navigation-login-box-open').css('display') != 'block') {
		$('#middle-content-sidebar H3').css('padding-top', '28px');
	}
	$('DIV.main-navigation-login-box').find('A.open-button').click(function(e) {
		e.preventDefault();
		$('#middle-content-sidebar H3').animate({
			paddingTop: "230px"
		}, 400);
	});
	$('DIV.main-navigation-login-box-open').find('A.close-button').click(function(e) {
		e.preventDefault();
		$('#middle-content-sidebar H3').animate({
			paddingTop: "30px"
		}, 400);
	});

	try {
		/* Adjust tabs */
		if ($('div.tab-container').length > 0) {
			var OurIndex = $('div.tab-container > ul > li').index($('div.tab-container > ul > li.tab-container-selected'));
			var Length = $('div.tab-container > ul > li').length;

			if (OurIndex == 0) {
				$('div.tab-container > ul > li.tab-container-selected').addClass('tab-container-selected-first');
				$('div.tab-container > ul')[0].style.backgroundImage = 'url(../../images/tab-bg.png)';
			} else if (OurIndex == (Length - 1)) {
				$('div.tab-container > ul > li.tab-container-selected').addClass('tab-container-selected-last');
				$('div.tab-container > ul')[0].style.backgroundImage = 'url(../../images/tab-bg.png)';
			} else {
				$('div.tab-container > ul > li.tab-container-selected').addClass('tab-container-selected');
				$('div.tab-container > ul')[0].style.backgroundImage = 'url(../../images/tab-bg.png)';
			}
			$('div.tab-container > ul > li:first').addClass('tab-container-first-element');
			$('div.tab-container > ul > li:last').addClass('tab-container-last-element')
		}
	}
	catch (err) { }
	try {
		if ($('div.tab-container-small').length > 0) {
			var OurIndex = $('div.tab-container-small > ul > li').index($('div.tab-container-small > ul > li.tab-container-selected'));
			var Length = $('div.tab-container-small > ul > li').length;

			if (OurIndex == 0) {
				$('div.tab-container-small > ul > li.tab-container-selected').addClass('tab-container-small-selected-first');
				$('div.tab-container-small > ul')[0].style.backgroundImage = 'url(../../images/tab-bg.png)';
			} else if (OurIndex == (Length - 1)) {
				$('div.tab-container-small > ul > li.tab-container-selected').addClass('tab-container-small-selected-last');
				$('div.tab-container-small > ul')[0].style.backgroundImage = 'url(../../images/tab-bg.png)';
			} else {
				$('div.tab-container-small > ul > li.tab-container-selected').addClass('tab-container-small-selected');
				$('div.tab-container-small > ul')[0].style.backgroundImage = 'url(../../images/tab-bg.png)';
			}
			$('div.tab-container-small > ul > li:first').addClass('tab-container-small-first-element');
			$('div.tab-container-small > ul > li:last').addClass('tab-container-small-last-element');
		}
	} catch (err) { }

	adjustSideBar();


	$('#leftnav-locations a').click(function(event) {
		window.location = 'https://www.fnfg.com/Locations/Default.aspx';
	});

	/* Bind accordion left hand side events. */
	$('#accordion').accordion({ collapsible: true, header: 'h3' });

	// jQuery doesn't behave without these two lines
	$('#accordion').accordion('activate', 0);
	$('div.accordian-resource> div').hide();


	$('#accordion').bind('accordionchange', function(event, ui) {
		if (typeof (ui.newHeader[0]) != 'undefined') {
			if (typeof (ui.newHeader.parent()) != 'undefined')
				ui.newHeader.parent().addClass('selected');
			ui.newHeader.addClass("selected");
		}
		if (typeof (ui.oldHeader[0]) != 'undefined') {
			if (typeof (ui.oldHeader.parent()) != 'undefined')
				ui.oldHeader.parent().removeClass("selected");
			ui.oldHeader.removeClass("selected");
		}
	});

	/* Round all corners */
	$('.rounded').corners();

	/* Assign offsets for dropdown menus */
	var FullWidth = parseInt($('#top-content')[0].style.width);
	var Elements = $('.main-navigation-element');
	var Windows = $('.main-navigation-tab-window');
	for (i = 0; i < Elements.length; i++) {
		if (FullWidth < (parseInt($(Elements[i]).position().left) + parseInt($(Windows[i]).width()))) {
			Offset = (parseInt($(Elements[i]).position().left) + parseInt($(Windows[i]).width())) - FullWidth;
			if (typeof ($(Windows[i])[0].style) != 'undefined')
				$(Windows[i])[0].style.marginLeft = "-" + (Offset + 16) + 'px';
		}
		if (i == Elements.length - 1) {
			$(Elements[i]).find('DIV.main-navigation-tab-window').css({
				'position': 'absolute',
				'right': 0,
				'left': 'auto'
			});
		}
	}


	$('#SecureLoginType').change(function(e) {
		if ($(this).val() == 'Personal') {
			$('#main-navigation-login-box-signin').attr({
				href: "https://enterprise2.openbank.com/fi1630_auth/user/slogon",
				title: null
			}).removeClass('thickbox').unbind('click');
		} else {
			$('#main-navigation-login-box-signin').attr({
				href: "/BusinessForm.aspx?TB_iframe=true&modal=true&height=175&width=550",
				title: 'Business Banking'
			}).addClass('thickbox');
		}
		//
		$('#main-navigation-login-box-signin').click(function(e) {
			if ($('#SecureLoginType').val() == 'Business') {
				e.preventDefault();
				tb_show($(this).attr('title'), $(this).attr('href'), false);
			}
		});
	});

	/* See if our current path is a path with-in an accordion resource, if so, expand it. */
	var ResourceColl = $('.accordian-resource');
	var Path = window.location;
	matchFound = false;
	for (var ind = 0; ind < ResourceColl.length; ind++) {
		if (!matchFound) {
			var aTagColl = $('a', ResourceColl[ind]);				
			for (var j = 0; j < aTagColl.length; j++) {							
				if (Path.href.toLowerCase() == aTagColl[j].href.toLowerCase()) {
					$('#accordion').accordion('activate', ind);
					/* No jquery, i'm seriuos.. */					
					if (ind == 0)
						$('#accordion').accordion('activate', ind);
					matchFound = true;
				}				
				if (Path.href.toLowerCase() == aTagColl[j].href.toLowerCase()) {
					aTagColl[j].setAttribute("class", 'selected');
				}
			}
		}
	}
});

function popUpWindow(url, width, height) {
	window.open(url, '', 'menubar=no, width=' + width + ', height=' + height + ', toolbar=no');
}

function OnEndRequest(sender,args)
{                            
	adjustSideBar();   
}

