﻿function inputOnKeyPress(sender, e, action, parms) {
	var keynum = window.event ? e.keyCode : e.which;

	if (keynum == 13) 
	{
		eval(action + "(sender,e,parms)");
		return false;
	}
}

function callSearch(sender, e, parms) {
	var searchBox = document.getElementById(parms[0]);
	if (searchBox != null && searchBox != undefined && searchBox.value != 'Search Site'
		&& searchBox.value != '' && searchBox.value != null) {
		__doPostBack(parms[1], '');
	}
	return false;
}

function adjustSideBar() {
	if (document.getElementById('middle-content-sidebar') != null)
		document.getElementById('middle-content-sidebar').style.height = $('.middle-content')[0].clientHeight + "px";
}

function SearchBoxFocusBlur(sender, e) {
	if (e.type == 'focus') {
		if (sender.value == 'Search Site') {
			sender.value = '';
		}
	}
	else {
		if (sender.value == '') {
			sender.value = 'Search Site';
		}
	}
}

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() {

}

function Set_Cookie(name, value, expires, path, domain, secure) {
	var today = new Date();
	today.setTime(today.getTime());

	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) {
	var a_all_cookies = document.cookie.split(';');
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false;

	for (i = 0; i < a_all_cookies.length; i++) {
		a_temp_cookie = a_all_cookies[i].split('=');
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		if (cookie_name == check_name) {
			b_cookie_found = true;
			if (a_temp_cookie.length > 1) {
				cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
			}
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if (!b_cookie_found) {
		return null;
	}
}

var OldWindowOpen = null;

function SecureLoginTypeChanged(e) {
	var myValue = $('#SecureLoginType').val();
	var link1 = document.getElementById('main-navigation-login-box-signin1');
	var link2 = document.getElementById('main-navigation-login-box-signin');
	if (myValue == 'Personal') {
		//Set_Cookie("loginOpen", true);
		link1.style.display = "inline";
		link1.style.visibility = "visible";
		link2.style.display = "none";
		link2.style.visibility = "hidden";
	}
	else {
		//Set_Cookie("loginOpen", false);
		link2.style.display = "inline";
		link2.style.visibility = "visible";
		link1.style.display = "none";
		link1.style.visibility = "hidden";
	}
}

function SecureLogin(open) {
	if (open == true) {
		$.cookie("loginOpen", true, { path: '/' });
		$('.main-navigation-login-box').fadeOut(50, function() { $('.main-navigation-login-box-open').fadeIn(100); });
	}
	else {
		$.cookie("loginOpen", false, { path: '/' });
		$('.main-navigation-login-box-open').fadeOut(50, function() { $('.main-navigation-login-box').fadeIn(100); });
	}
}

function SignInModalClicked(e) {
	var varName = document.getElementById('SecureLoginType').value;
	if (document.getElementById('rememberme').checked) {
		$.cookie('bankingtype', varName, { path: '/' });
	}
	else {
		$.cookie('bankingtype', null);
	}
}

$(document).ready(function() {

//    if ($('DIV.main-navigation-login-box-open').css('display') != 'block') {
//        $('#middle-content-sidebar H3').css('padding-top', '30px');
//    }
    $('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();

    /* 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");
        }
    });

    $('#faqaccordion').accordion({ collapsible: true, header: 'h3', autoHeight: false });
    $('#faqaccordion').accordion('activate', 0);
    $('#faqaccordion').bind('accordionchange', function(event, ui) {
        if (typeof (ui.newHeader.context) != 'undefined') {
            $(ui.newHeader.context).find('a.faqopen')[0].innerHTML = 'close';
        }
        if (typeof (ui.oldHeader.context) != 'undefined') {
            $(ui.oldHeader.context).find('a.faqopen')[0].innerHTML = 'view all';
        }
        window.scrollTo(0, 0);
    });

    /* 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(SecureLoginTypeChanged);
    $('#main-navigation-login-box-signin').click(SignInModalClicked);
    $('#main-navigation-login-box-signin1').click(SignInModalClicked);

    var bankingtype = $.cookie('bankingtype');

    //alert(bankingtype);
    if (bankingtype != 'undefined' && bankingtype != 'Personal') {
        var dd = document.getElementById('SecureLoginType');
        if (dd) {
            for (i = 0; i < dd.options.length; i++) {
                if (dd.options[i].value == bankingtype) {
                    dd.selectedIndex = i;
                    break;
                }
            }
            SecureLoginTypeChanged();
        }
    }

    /* 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.toString().toLowerCase().indexOf(GetPath(aTagColl[j].href.toLowerCase())) > -1) {
                    $('#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();
}
