var OnLoadScripts = [];
var scriptCount = 0;
var askbecuWindow;
var becuexternalLink;

function RunOnLoadScripts()
{

	for(var i=0;i<OnLoadScripts.length;i++)
	{
		eval(OnLoadScripts[i]);
	}
}

function AddOnLoadScript(s,first)
{
	if(first)
	    OnLoadScripts.unshift(s);
	else
	    OnLoadScripts.push(s); 	
}

function domRollover() {
	if (navigator.userAgent.match(/Opera (\S+)/)) 
	{
		var operaVersion = parseInt(navigator.userAgent.match(/Opera (\S+)/)[1]);
	}
	if (!document.getElementById||operaVersion <7) return;
	
	var imgarr=document.getElementsByTagName('img');
	var imgPreload=new Array();
	var imgSrc=new Array();
	var imgClass=new Array();
	
	for (var i=0;i<imgarr.length;i++){
		if (imgarr[i].className.indexOf('domroll')!=-1)
		{
			imgSrc[i]=imgarr[i].getAttribute('src');
			imgClass[i]=imgarr[i].className;
			imgPreload[i]=new Image();
			if (imgClass[i].match(/domroll (\S+)/)) 
			{
				imgPreload[i].src = imgClass[i].match(/domroll (\S+)/)[1]
			}
			imgarr[i].setAttribute('xsrc', imgSrc[i]);
			imgarr[i].onmouseover=function(){this.setAttribute('src',this.className.match(/domroll (\S+)/)[1])}
			imgarr[i].onmouseout=function(){this.setAttribute('src',this.getAttribute('xsrc'))
			}
		}
	}
}
AddOnLoadScript("domRollover()");

function PanelToggle(panel,sender)
{
	// show requested panel and heading if we need to
	if(panel.style.display == "none")
	{
		panel.style.display = "block";
		sender.className = "head active";
	}
	else
	{
	    panel.style.display = "none";
		sender.className = "head";
	}	
}

function BioToggle(bio,sender)
{
	if(bio.style.display == "none")
	{
		bio.style.display = "block";
		sender.className = "fulllink fullactive";
	}
	else
	{
		bio.style.display = "none";
		sender.className = "fulllink";
	}
}

function SubmitForm(formID)
{
	var form = document.getElementById(formID);	
	if(form != null)
		form.submit();
}

function FilterClick(id,value)
{
	document.getElementById(id).value = value;
	SubmitForm("learnPlanForm");
}

function TopicClick(id)
{
	document.getElementById("topic").value = id;
	SubmitForm("learnPlanForm");
}

function SortSelect(select)
{	
	document.getElementById("sort").value = select.options[select.selectedIndex].value; 
	SubmitForm("learnPlanForm");
}

function RateOverlayToggle(overlay)
{
    var show = true;    
    if(overlay.style.display != "none")
        show = false;
    var table = getAncestorByClassName(overlay,"rate_table");    
    var divs = table.getElementsByTagName("div");    
    for(var i=0;i<divs.length;i++)
    {
        if(divs[i].className == "info_overlay_large")
            divs[i].style.display = "none";
    }

    if(show)
        overlay.style.display = "block";
    else
        overlay.style.display = "none";
    
    if(IEVersion() < 7)
    {
        var header = GetRateOverlayChildDiv(overlay,"info_overlay_header");
        header.className += " overlayHeaderIE6";
        header.style.background = "none";
        
        var mid = GetRateOverlayChildDiv(overlay,"info_overlay_mid");
        mid.className += " overlayMidIE6"; 
        mid.style.background = "none"; 

        var footer = GetRateOverlayChildDiv(overlay,"info_overlay_footer");
        footer.className += " overlayFooterIE6";
        footer.style.background = "none";
    }
}

function GetRateOverlayChildDiv(node,id)
{
   var divs = node.getElementsByTagName("div");
   for(var i=0;i<divs.length;i++)
   {
        if(divs[i].id == id)
            return divs[i];
   }
   
   return null;   
}

function togglePresDesc(obj,desc) {
    if(obj.className == 'subject active')  {
        obj.className = 'subject';
        document.getElementById(desc).style.display = 'none';
    } else {
        obj.className = 'subject active';
        document.getElementById(desc).style.display = 'block';
    }
}

function popup(url,name,width,height,resize,centered)
{		
	var screenHeight = screen.availHeight;
	var screenWidth = screen.availWidth;
	var top = (screenHeight/2) - (height/2);
	var left = (screenWidth/2) - (width/2);
	
	if(resize)
		resize = "yes";
	else
		resize = "no";
	
	var params = "menubar=no,resizable=" + resize + ",width=" + width + ",height=" + height; 
	
	if(centered)
		params += ",top=" + top + ",left=" + left;

	window.open(url,name,params);
}


function getAncestorByClassName(node,cname)
{
    // starts with the provided node and looks up the 
    // DOM tree to get an element with specific CSS class name.
    if(node.tagName != "BODY")
    {
        if(node.className == cname)        
            return node;
        else
            return getAncestorByClassName(node.parentNode,cname);
    }
    
    return null;
}

function getChildByClassName(node,cname)
{
//    for(var i=0;i< node.childNodes.length;i++)
//    {
//        
//        
//        if(node.childNodes[i].
//        if(node.childNodes[i].className == cname)        
//            return node.childNodes[i];
//        else
//            return getDescendantByClassName(node.childNodes[i],cname);
//    }
//    
    for(var cnode=node.firstChild;cnode!=null;cnode=cnode.nextSibling)
    {
        if(cnode.nodeType==1)
        { 
            if(cnode.className == cname)        
                return cnode;
        }
    }
}


function getAncestorByTagName(node,tname)
{
    // starts with the provided node and looks up the 
    // DOM tree to get an element with specific tag name.
    if(node.tagName != "BODY")
    {
        if(node.tagName == tname)        
            return node;
        else
            return getAncestorByClassName(node.parentNode,tname);
    }
    
    return null;
}

//var nn = null;
//function initPasswordField(input)
//{
//    if(input.value == "Password")
//    {
//        input.value = "";
//        nn = input.cloneNode(true); 
//        nn.type = "password"; 
//        input.parentNode.replaceChild(nn,input); 
//        setTimeout("nn.focus()", 100);

//    }
//}

function JoinNowClick()
{
    if(document.getElementById("agree").checked)
    {
        window.location="https://umnas3.umonitor.com/becu/welcome.do";
    }
    else
    {
        alert("Please indicate your acceptance of the Electronic Communications Disclosure to begin application.");
    }
}

function setFeedbackField(textarea)
{
    if(textarea.value == "Please type your comments/questions here.")
        textarea.value = "";
}

function launchSurvey()
{
	//Opens survey overlay
	var cookie = readCookie('becusurvey');
	var url = document.location.search;
	var qs = url.substring(url.indexOf('?')+1);
	var randomnumber=Math.floor(Math.random()*101);
	
	if (cookie != null && cookie != "")
	{
	}
	else
	{
	    if (qs == 'var=olb')
	    {
	        if (randomnumber<=30)
	        {
	            SetCookie('becusurvey','yes');
	            zBox_GetBox('survey').Show();;return false;
	        }
	    }
	}
}

function choosePanel()
{
    var cookie = readCookie('becuhome');

	if (cookie != null && cookie != "")
	{
	    if (cookie == '1')
	    {
	        SetCookie('becuhome','2');
            return 1;
	    }
	    if (cookie == '2')
	    {
	        SetCookie('becuhome','3');
            return 2;
	    }
	    if (cookie == '3')
	    {
	        SetCookie('becuhome','1');
            return 0;
	    }
	}
	else
	{
	    SetCookie('becuhome','1');
	    return 0;
	}
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function SetCookie(sName, sValue)
{
  // Expires the cookie in one month
  var date = new Date();
  //var hn = window.location.hostname;
  var hn = ".becu.org";
  date.setMonth(date.getMonth()+6);
  document.cookie = sName + "=" + escape(sValue) + ";expires=" + date.toGMTString() + ";domain=" + hn;
}

function setArticleID(id)
{
    var articleId = document.getElementById('ratings_form').ArticleID;
    articleId.value = id;
    return false;
}

function isRated(id)
{
    var cookieName = "becurating"+ id;
    var cookie = readCookie(cookieName);
    if (cookie != null && cookie != "")
    {
    	var divID = document.getElementById('thankyou');
	    divID.style.display = "none";
	
        var ratedDiv = document.getElementById('rated');
        ratedDiv.style.display = "block";
        return false;
    }
    else
    {
        return true;
    }
}

function callYesNo(id, formid, type)
{
    var rating = isRated(id);
    
    if (rating == true)
    {
        if (type == 'yes')
        {
            RatingSubmit(formid);
        }
        else
        {
            setArticleID(id);
            zBox_GetBox('ratings').Show();
        }
    }
}

function launchAskBECU(from){

	//Get parent browser coordinates
	if (document.all) {
		parentX = window.screenLeft;
		parentY = window.screenTop;
	} else {
		parentX = window.screenX;
		parentY = window.screenY;
	}
	
	//Get dimension of screen
	displayWidth = screen.availWidth;
	displayHeight = screen.availHeight;
	
	//Get width of parent browser
	parentWidth = window.document.body.clientWidth;
	parentHeight = window.document.body.clientHeight;
	
	//Assign width of AskBECU client
	askWidth = 260;

	//We want to track if people are using the utility link or the body link
	if (from == 'u'){
		cookieVal = 'Ask BECU : '+s.pageName+' : Navigation : Ask BECU';
	} else if (from == 'b'){
		cookieVal = 'Ask BECU : '+s.pageName+' : content_pane : Have a question? Ask BECU';
	} else {
		cookieVal = 'Ask BECU : '+s.pageName+' : contextual : ' + from;
	}

	SetCookie('askbecu',cookieVal);
	askURL = ("https://ask.becu.org/activeagentui/agent.aspx#ORG_Context_");
	//Resize browser window if needed and pop browser
	if (parentX+parentWidth+askWidth < displayWidth){
	// Ask window will fit, just put it next to window
		askbecuWindow = window.open(askURL,'AskBECUAgentWindow','height=500,width=260,resizable=1');
		askbecuWindow.moveTo((parentX+parentWidth+25),parentY);
	} else {
	// Ask window wont fit, resize original window and put Ask window next to it
		//window.moveTo(0,0);
		//window.resizeTo((displayWidth-askWidth-10),displayHeight);
		askbecuWindow = window.open(askURL,'AskBECUAgentWindow','height=500,width=260,resizable=1');
		askbecuWindow.moveTo((displayWidth-askWidth),0);
	}
	if(typeof(askbecuWindow)!='undefined' && !askbecuWindow.closed) 
	{
		askbecuWindow.blur();
	}		    	
	askbecuWindow.focus();
}

window.onunload = closePopup;
	  
function closePopup()
{ 
  if(askbecuWindow && !askbecuWindow.closed)
  	{
  		askbecuWindow.close();
  	}

}
//handles the display of the promo from main nav
var selectedNavItemIndex = -1;
function toggleNavPromo(promoID, currentNavImage) {
    var promoDiv;
    var navImage;
    var navImageName;
    var arrNavImageName;
    
    //hide any other displayed promos
    for (var i = 1; i <= 5; i++) {
        promoDiv = document.getElementById("promoItem" + i);
        promoDiv.style.display = "none";
        
        navImage = document.getElementById("navImage" + i);

        //alert(navImage.src.lastIndexOf("_"));

        if (i == selectedNavItemIndex) {
            //alert(i);
            navImage.src = navImage.src.substring(0, navImage.src.lastIndexOf("_")) + "_on.gif";
        }
        else {
            navImage.src = navImage.src.substring(0, navImage.src.lastIndexOf("_")) + "_off.gif";
        }
    }
    
    //display current promo
    promoDiv = document.getElementById(promoID);
    promoDiv.style.display = "block";
    currentNavImage.src = currentNavImage.src.substring(0, navImage.src.lastIndexOf("_")) + "_hover.gif";


}
var currentNavItem;


function hideNavPromo(promoID,navImageID, src) {
    //handle event bubbling so that mouseout on list items does not cause mouseout for div.
    var event = hideNavPromo.caller.arguments[0] || window.event;
    var theElement = event.srcElement || event.target;
    var theElementType = theElement.tagName;
    var cssClass = theElement.className;
    var navImage = document.getElementById(navImageID);
    var blnContainsSpace = cssClass.indexOf(" ", 0);
    var arrClassName = cssClass.split(" ");
   // if (arrClassName.length > 0) {
     //   if (arrClassName[0] == "promoItemContainer" || arrClassName[0] == "promoItemContainer-single") {

        //    var promoDiv = document.getElementById(promoID);
      //      promoDiv.style.display = "none";
//
            navImage.src = src;
       // }
   // }
   

}

function openTool(toolId, fieldValue) {
    var field = document.getElementById(fieldValue);
    if (field.value == '') {
        alert('Please enter a number and click the Continue button');
        return false;
    }
    if ((isNaN(field.value)) || (field.value <= 0)) {
        alert('Please enter a valid number and click the Continue button');
        return false;
    }
    var url = '/learn-and-plan/web-tool-detail.aspx?id='+ toolId +'&type=calc&articles=0&webcasts=0&calcs=1&sort=newest&topic=-1&totalDebt='+ field.value;
    window.location=url;
}

function showTooltip(tooltip, obj, width) {
    var tooltipwrapper = document.getElementById("tooltipWrapper");
    var tooltipdiv = document.getElementById("tooltipDiv");
    tooltipdiv.innerHTML = tooltip.replace("\n", "<br/>");
    tooltipwrapper.style.display = "block";
    var topOffset = (tooltipwrapper.clientHeight + 3)
    tooltipwrapper.style.top = (findPosY(obj) - topOffset) + "px";
    tooltipwrapper.style.left = findPosX(obj) + "px";
    if (width != '')
        tooltipwrapper.style.width = width + "px";
    else
        tooltipwrapper.style.width = 'auto';
}

function hideTooltip() {
    var tooltipwrapper = document.getElementById("tooltipWrapper");
    tooltipwrapper.style.display = "none";
}
function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent)
        while (1) {
        curleft += obj.offsetLeft;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}
function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent)
        while (1) {
        curtop += obj.offsetTop;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}

function loginFocus() {
    document.getElementById('txtUsername').focus();
}
// Optimization functions

  var qd = '';//query string data
  var furl = '';//full url

  function implodeString(url,str)
  {
  if(expId){expId2 = expId;} 
  else {expId2 = 'none';}
  url.search(/\?/)>0?furl=url+'&'+str:furl=url+'?'+str+'-'+expId2;
  }

  function appendString(url,oqd)
  {
  var qd=window.location.search.substring(1);
  if(qd){oqd?qd=qd+'&'+oqd:qd=qd;implodeString(url,qd);}
  else{if(oqd){implodeString(url,oqd);}}
  return furl;
  }

  function gotoPage(url){window.location.href = url;}

  function callAction(cid){return WM.conversion('ta_HomePA',
  { conversionPoint: cid }, this);
  }

  function setExternalLink(link) {
      becuexternalLink = link;
  }

  function gotoExternalLink() {
      window.location.href = becuexternalLink;
  }
