
var menuItem = new Array();
var isSpanish = checkLang();

function getElement(id) {
	if (document.getElementById) {
		return document.getElementById(id);
	}
	
	if (document.all || document.layers) {
		return document.all[id];
	}
	
	return null;
}

// Add and Remove event functions
function addEventSimple(obj,evt,fn) {
	if (obj.addEventListener)
		obj.addEventListener(evt,fn,false);
	else if (obj.attachEvent)
		obj.attachEvent('on'+evt,fn);
}

function removeEventSimple(obj,evt,fn) {
	if (obj.removeEventListener)
		obj.removeEventListener(evt,fn,false);
	else if (obj.detachEvent)
		obj.detachEvent('on'+evt,fn);
}

var url;

function changePage(redirectTo) {
    url = redirectTo;
    //makeRequest("status.aspx");
    getElement("loggedOutContainer").style.display = "block";
    return false;
}

function continueOn() {
    document.location.href = url;
}

function closeWarning() {
   // getElement("loggedOutContainer").style.visibility = "hidden";
    getElement("loggedOutContainer").style.display = "none";
}


function makeRequest(ajax) {

    http_request = false;

    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/html');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!http_request) {
       alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    
    http_request.onreadystatechange = alertContents;

    http_request.open('GET', ajax, true);

    http_request.send(null);
    alert(http_request.responseXML);
}

function alertContents() 
{
    if (http_request.readyState == 4) 
    {
        if (http_request.status == 200) 
        { 
            if(http_request.responseText == "True") 
            {              
                getElement("loggedOutContainer").style.display = "block";
            } 
            else if (http_request.responseText == "False") 
            {
                continueOn();
            }
        }
        else 
        {        
            alert('There was a problem with the request.');
        }
    }
}

function checkLang() {
    var cookie = getCookie("__LANGUAGE");
    if (cookie == "spanish") {
        return true;
    }
    else {
        return false;
    }
}

function getCookie( name ) {
    var start = document.cookie.indexOf( name + "=" );
    var len = start + name.length + 1;
    if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
        return null;
    }
    if ( start == -1 ) return null;
    var end = document.cookie.indexOf( ";", len );
    if ( end == -1 ) end = document.cookie.length;
    return unescape( document.cookie.substring(len, end));
}

//direct deposit savings calc
function calcSavings(obj)
{
    var numChecks = document.getElementById(obj).value;
    var payAmt = document.getElementById('payAmt').value;
    var savings = 0;
    //strip out '$' from num
    var curAmt =   payAmt.replace(/\$/g,"");
    
    if ( payAmt == '' || isNaN(curAmt) ) 
    {
        alert("Please enter a valid amount of your paycheck.");
    } else {
        savings =   curAmt   * numChecks * .035 * 12;
    }

     document.getElementById('savingsResult').innerHTML = '';
    var amt = document.createTextNode("$" + Math.round(savings));
    document.getElementById('savingsResult').appendChild(amt);
    var br = document.createElement('br');
    document.getElementById('savingsResult').appendChild(br);
    if ( checkLang() )
    {
    var decoded = html_entity_decode('AL A&Ntilde;O');
    var per = document.createTextNode(decoded);
    
    } else {
        var per = document.createTextNode('per year');
    }
    document.getElementById('savingsResult').appendChild(per);
}

function html_entity_decode(str) {
  var ta=document.createElement("textarea");
  ta.innerHTML=str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
  return ta.value;
}

function disableBtn(btn, doing)
{
    btn.className = "inContextDisabled";
    //btn.innerHTML = doing;
    var spans = btn.getElementsByTagName('span');
    for ( var i=0; i<spans.length; i++ ) {
        if ( spans[i].className == 'btnText' ) {
            spans[i].firstChild.nodeValue = doing;
        }
    }
    btn.onclick = function() { return false; };
}

function getKeyCode(e)
{
    return window.event ? e.keyCode : e.which;
}

function onHitKey(e, keyCode, elementOrId)
{
    if(getKeyCode(e) == keyCode)
    {        
        var type = typeof elementOrId;
        var element = (type.toLowerCase() == "object") ? elementOrId : document.getElementById(elementOrId);
        if(element.click != null)

        {
            element.click();
        }
        else if (element.onclick != null)
        {
            element.onclick();
        }
        else if (element.href != null && element.href.match('javascript:'))
        {

           eval(unescape(element.href.replace('javascript:','')));
        }        
        return false;
    }
    else
    {
        return true;
    }
}

var currentLocation;
function showLocation(element) 
{

    if (currentLocation != null) 
    {
        currentLocation.className = "hideLoc";
    }
    
    element.parentNode.parentNode.getElementsByTagName("div")[0].className = (currentLocation == element) ? "hideLoc" : "showLoc";
    currentLocation = element.parentNode.parentNode.getElementsByTagName("div")[0];
    //fixStyles();
}

function checkRadioButton(thisOne)
{
	if($(thisOne).parent().hasClass('onlineStatementRadio'))
	{
		//$(".onlineStatementWarning").show();
		$("#ctl00_cphContent_pnlEsign").show();
	    //$(".paperStatementWarning").hide();
	}else{
		//$(".onlineStatementWarning").hide();
	    $("#ctl00_cphContent_pnlEsign").hide();
	    //$(".paperStatementWarning").show();
	}
}

function popUp(url) {
    var newwindow = window.open(url, 'extWin', 'height=800,width=1024,scrollbars=1,resizable=1,menubar=1,status=1,toolbar=1,location=1,personalbar=1');
    if (newwindow.focus) {
        newwindow.focus();
    }
    return false;
}

function initLinks() {
    var anchors = $('a');
    anchors.each(function(i) {
        var h = $(anchors[i]).attr('href');
        var r = $(anchors[i]).attr("rel");
        if (h != null && r != null) {
			if (r == 'external') {
                //popup script
                $(anchors[i]).click(function() {
                    return popUp($(this).attr("href"))
                });
				var title = $(anchors[i]).attr('title') + $(anchors[i]).text();
                $(anchors[i]).attr("title", title);
            }
        }
    });
}


function hideHelpText(e) {
    var cont = $(e.currentTarget).find('input:hidden[name="container"]').val();
	//$('#' + cont).hide("slow");
	$('#' + cont).mouseover(function(e) {
		$('#' + cont).mouseleave(function(e) {
										
			$('#' + cont).hide("slow");
		});
									 });
						}

function forceHideHelpText(e) {
    var cont = $(e.currentTarget).find('input:hidden[name="container"]').val();
    $('#' + cont).hide("slow");
}


function showHelpText(e) {
	var cont = $(e.currentTarget).find('input:hidden[name="container"]').val();
	var topOffset = $(e.currentTarget).offsetParent().offset().top;
	var topPos = $(e.currentTarget).offset().top - topOffset + 4 + 'px';
	var leftOffset = $(e.currentTarget).offsetParent().offset().left;
	var leftPos = $(e.currentTarget).offset().left - leftOffset + $(e.currentTarget).width() + 5 + 'px';
	$('#' + cont).css('top', topPos);
	$('#' + cont).css('left', leftPos);
	$('#' + cont).show("slow");
}

function initHelpText() {
    $('.helpBtn').hover(showHelpText, hideHelpText);
    $('#helpClose').click(function(e) {
        forceHideHelpText(e);
        return false;
    });
	$('.helpBtn').click(function(e) {
		//showHelpText(e);
		return false;
	});
}


function initPrintButtons() {
	$('a.print').click(function() {
		print();
		return false;
	});
}




function init() {

    initLinks();
    initHelpText();
    initPrintButtons();



	if($('.checkFloat').length > 0)
	{
		var top = $('.checkFloat').offset().top - parseFloat($('.checkFloat').css('marginTop').replace(/auto/, 0));
		var bottom = $('#formControls').height() + 250;


		$(window).scroll(function(e) {
			// what the y position of the scroll is
			var y = $(this).scrollTop();
			// whether that's below the form
		   if (y >= top && y <= bottom) {
				// if so, ad the fixed class
				$('.checkFloat').addClass('fixed');
				$('.checkFloat').removeClass('stuck');
				//if it gets to the bottom of the form, stop
			} else if (y >= bottom) {
				$('.checkFloat').addClass('stuck');
				$('.checkFloat').removeClass('fixed');
			} else {
				// otherwise remove it
				$('.checkFloat').removeClass('fixed');
			}
		});
    }

}


function setCheckType(type)
{
    var imgsrc = "/assets/common/fedcheck-";
    var imgsuffix = ".png";
    
    var imgurl = imgsrc + type + imgsuffix;

    $('#checkImg').attr('src', imgurl);
}
$(window).load(init);
