
if(!window.BF_MODULES_DOMAIN)
{
	if(window.location.hostname.indexOf("temp"))
	{
		BF_MODULES_DOMAIN = "";
	}
	else
	{
		var h = window.location.hostname;
		h = h.substring(h.indexOf(".")+1);
		

		BF_MODULES_DOMAIN = "builderfusion." + h;
	}
}

/**************************** LOGIN / MEMBERS ONLY ***************************/

if(!window.LOGIN_PAGE)
{
	LOGIN_PAGE = "/member-login.html";
}

if(!window.BF_ACCESS_DENIED)
{
	BF_ACCESS_DENIED = "/access-denied.html";
}

/*

Put a call to this function below the closing form tag for the login form

	</form>
    <script> setupLoginForm(document.forms.myForm); </script>

*/

function setupLoginForm(form)
{
	if(!form)
		form = document.forms.bflogin;
	
	if(window.BF_DEBUG)
	{
		alert("setupLoginForm():  form=" + form);
	}
	if(form)
	{
	    if(BF_MODULES_DOMAIN.indexOf(".builderfusion.com")==-1 && BF_MODULES_DOMAIN.indexOf(".memberfusion.com")==-1)
	    {
	        form.method="post";
	    }
	    else
	    {
	        form.method="get";
	        form.action=(window.location.href.indexOf("https://"+BF_MODULES_DOMAIN)==0 ? "https://" : "http://")+BF_MODULES_DOMAIN+"/bf/website/simple/login";
	    }
	        
	}
}


/*

Put a call to this function in the top/head of any page that requires that the user
is logged in:

    <script> isLoggedIntoBF(); </script>

*/

function isLoggedIntoBF()
{
    var loginURL = LOGIN_PAGE + "?forwardURL=" + escape(window.location.href);

    var bfweb = getCookie("BFWEB");
    if(!bfweb || bfweb.length==0 || bfweb.indexOf("|")<=0)
    {
    	if(window.BF_DEBUG)
    	{
    		alert("isLoggedIntoBF():  redirecting to "+loginURL + "...");
    	}
        window.location.href = loginURL;
    }
    
    if (top != window) 
    {
        top.location.replace(window.location.href);
    }
}



/*

Put a call to this function in the top/head of any page that requires that the user
who is a member of a particular council is logged in (CouncilControl must be enabled):

    <script> isCouncilMember("COUNCIL_ABBREVIATION"); </script>

*/

function isCouncilMember(councilAbbreviation)
{
    var bfweb = getCookie("BFWEB");
	if(window.BF_DEBUG)
	{
		alert("isCouncilMember(\"" + councilAbbreviation + "\"):  bfweb = "+bfweb + "...");
	}
    if(!bfweb || bfweb.length==0 || bfweb.indexOf("|")<=0)
    {
        var loginURL = LOGIN_PAGE + "?forwardURL=" + escape(window.location.href);

        window.location.href = loginURL;
    }
    else
    {
    	if(bfweb.indexOf(councilAbbreviation) != -1)
    	{
		    if (top != window) 
		    {
		        top.location.replace(window.location.href);
		    }
    	}
    	else
    	{
    		window.location.href = BF_ACCESS_DENIED;
    	}
   }
}

function isMemberOfGroup(groupAbbreviation,justDoCheck)
{
    	
	var isMem = false;
	var bf = getCookie("BF");

	if(!bf || bf.length==0 || bf == "\"\"")
    {
		if(!justDoCheck)
		{
	        var loginURL = LOGIN_PAGE + "?forwardURL=" + escape(window.location.href);
	    	if(window.BF_DEBUG)
	    	{
	    		alert("isMemberOfGroup():  redirecting to "+loginURL + "...");
	    	}
	        window.location.href = loginURL;
	        return false;
		}
    }
    else
    {
    	
    	var bfParams = getSearchParams(bf);
   	
		if(bfParams["gm"])
		{
			var groups = bfParams["gm"].split(",");
			for(var i=0; i<groups.length; i++)
			{
				if(groups[i].toLowerCase() == groupAbbreviation.toLowerCase())
				{
					isMem = true;
					break;
				}
			}
		}
    }
	
	if(isMem)
	{
	    if (top != window) 
	    {
	        top.location.replace(window.location.href);
	        return true;
	    }
	}
	else if(!justDoCheck)
	{
   		window.location.href = BF_ACCESS_DENIED;
	}

    return isMem;
}

function getBFCookieParamValue(paramName)
{
	var bf = getCookie("BF");
	
	var paramValue = "" 

	if(bf && bf != "\"\"")
    {
    	var bfParams = getSearchParams(bf);

    	paramValue = bfParams[paramName];
    }

	return paramValue;
}

function logOut(redirectURL)
{
	var url = (window.location.href.indexOf("https://"+BF_MODULES_DOMAIN)==0 ? "https://" : "http://") + BF_MODULES_DOMAIN + "/bf/logoff" + (redirectURL ? "?redirectURL="+ escape(redirectURL) : "");
	
	window.location.href = url;
}

/*

Put a call to this function anywhere on the page that displays the BF Login 
Module in the "memberFrame" IFRAME:

    <script> loginOnLoad(); </script>

*/

function loginOnLoad(forwardURL)
{
	if(window.BF_DEBUG)
	{
		alert("loginOnLoad():  forwardURL=" + forwardURL);
	}
    if(window.attachEvent)
    {
        window.attachEvent("onload", function(i) { initializeLogin(forwardURL); });
    }
    else
    {
        window.addEventListener("load", function(){ initializeLogin(forwardURL); }, false); 
    }
}

/*

This function is called from the loginOnLoad function.

This function gets any applicable parameters from the query string
of the URL and then updates the "memberFrame" IFRAME with the 
appropriate URL.

    forwardURL : If the isLoggedInBF function determines that the 
                 user is not logged in, then it redirects to the
                 login page that contains the memberFrame.  This
                 parameter is added to the login module URL in that
                 frame.
                 
    loginError : If the username/password entered on the login module
                 is invalid, the module redirects back to this page
                 with the login module loaded in the memberFrame.
                 This parameter is added to the login module URL.

    forgotLogin: The value of this parameter will become the URL of the
                 memberFrame.
    

If there are no applicable parameters, then the memberFrame will go 
to the BF members only module.

*/

function initializeLogin(fURL)
{
    var x = window.location.search;
    var loginError = false;
    var forgotLogin = false;
    var forwardURL = "";
    if(x)
    {
        if(x.substring(0,1)=="?")
            x = x.substring(1);
    
        var params = x.split("&");
        if(params && params.length>0)
        {
            for(var i=0; i<params.length; i++)
            {
                if(params[i].indexOf("forwardURL=")==0)
                {
                    var vals = params[i].split("=");
                    forwardURL = unescape(vals[1]);
                }
                
                if(params[i].indexOf("loginError=")==0)
                {
                    var vals = params[i].split("=");
                    loginError = vals[1]=="true";
                }
                
                if(params[i].indexOf("forgotLogin=")==0)
                {
                    var vals = params[i].split("=");
                    forgotLogin = vals[1]=="true";
                }
            }
        }
    }
    
    if(!forwardURL)
        forwardURL = fURL;
    
    if(!forwardURL && document.referrer)
    	forwardURL = document.referrer;
        
    var bfweb = getCookie("BFWEB");
    var isLoggedIn = bfweb && bfweb.length>0 && bfweb.indexOf("|")>0;
     
    var purl = window.location.href.lastIndexOf("#")==-1 ? window.location.href : window.location.href.substring(0,window.location.href.lastIndexOf("#"));

    purl += "#bf_memberFrame";
    
    var url = "";
    
    if(isLoggedIn)
    {
    	if(fURL)
    		url = fURL;
    	else
    		url = (window.location.href.indexOf("https://"+BF_MODULES_DOMAIN)==0 ? "https://" : "http://") + BF_MODULES_DOMAIN + "/bf/website/simple/members/index.jsp";
    }

    else if(loginError)
    {
        url = (window.location.href.indexOf("https://"+BF_MODULES_DOMAIN)==0 ? "https://" : "http://") + BF_MODULES_DOMAIN + "/bf/website/simple/login.jsp?forwardURL=" + escape("/bf/website/simple/members/index.jsp") + "&loginError=true";
    }
    
    else if(forgotLogin)
    {
        url = (window.location.href.indexOf("https://"+BF_MODULES_DOMAIN)==0 ? "https://" : "http://") + BF_MODULES_DOMAIN + "/bf/website/simple/forgotPassword.jsp";
    }
    
    else if(forwardURL)
    {
        url = (window.location.href.indexOf("https://"+BF_MODULES_DOMAIN)==0 ? "https://" : "http://") + BF_MODULES_DOMAIN + "/bf/website/simple/login.jsp?forwardURL=" + escape(forwardURL);
    }
    else
    {
        forwardURL = (window.location.href.indexOf("https://"+BF_MODULES_DOMAIN)==0 ? "https://" : "http://") + BF_MODULES_DOMAIN + "/bf/website/simple/members/index.jsp?purl=" + escape(purl);
        url = (window.location.href.indexOf("https://"+BF_MODULES_DOMAIN)==0 ? "https://" : "http://") + BF_MODULES_DOMAIN + "/bf/website/simple/login.jsp?forwardURL=" + escape(forwardURL);
    }

    url += (url.indexOf("?")==-1?"?":"&") + "purl=" + escape(purl);

	if(window.BF_DEBUG)
	{
		alert("initializeLogin():  fURL=" + fURL + ", url=" + url);
	}

    var f = document.getElementById("frame_memberFrame");

    if(f)
    f.src = url;
}

/******************************** DIRECTORY **********************************/

function goToDirectory(frameName, params,advancedSearch)
{
    // "var url" and "var monthViewURL" need to be changed to the domain of the site you are working on
    var x = window.location.search;
    var url = (window.location.href.indexOf("https://"+BF_MODULES_DOMAIN)==0 ? "https://" : "http://") + BF_MODULES_DOMAIN + "/bf/website/directory/" + (advancedSearch?"search.jsp":"index.jsp");

    if(x && x.substring(0,1)=="?")
        x = x.substring(1);

    if(params && (params.substring(0,1) == "?" || params.substring(0,1) == "&"))
        params = params.substring(1);
    
    if(params)
    {
        if(x)
            x += "&" + params;
    else
        x = params;
    }
    
    if(x)
    {
    	var parts = x.split("&");
    	var params2 = "";
    	for(var i=0; i<parts.length; i++)
    	{
    		var nameval = parts[i].split("=");
    		if(nameval[0] != "purl")
    			params2 += (params2.length>0?"&":"") + nameval[0] + "=" + nameval[1];
    	}
        url += "?" + params2;
    }

    if(!frameName)
        frameName = "dirFrame";

    
    purl = window.location.href.lastIndexOf("#")==-1 ? window.location.href : window.location.href.substring(0,window.location.href.lastIndexOf("#"));

    purl += "#bf_" + frameName;
    
    url += (url.indexOf("?")==-1?"?":"&") + "purl=" + escape(purl);
    

    var f = document.getElementById("frame_"+frameName);

    if(f)
    {
        f.src = url;
    }
    else if(window[frameName])
    {
        window[frameName].location.href=url;
    }
    else
    {
        //You specified the wrong frame name
    }
}

function goToBusinessPage(frameName, params)
{
    // "var url" and "var monthViewURL" need to be changed to the domain of the site you are working on
    var x = window.location.search;
    var url = (window.location.href.indexOf("https://"+BF_MODULES_DOMAIN)==0 ? "https://" : "http://") + BF_MODULES_DOMAIN + "/bf/website/directory/businessPage.jsp";

    if(x && x.substring(0,1)=="?")
        x = x.substring(1);

    if(params && (params.substring(0,1) == "?" || params.substring(0,1) == "&"))
        params = params.substring(1);
    
    if(params)
    {
        if(x)
            x += "&" + params;
    else
        x = params;
    }
    
    if(x)
    {
    	var parts = x.split("&");
    	var params2 = "";
    	for(var i=0; i<parts.length; i++)
    	{
    		var nameval = parts[i].split("=");
    		if(nameval[0] != "purl")
    			params2 += (params2.length>0?"&":"") + nameval[0] + "=" + nameval[1];
    	}
        url += "?" + params2;
    }

    if(!frameName)
        frameName = "dirFrame";

    
    purl = window.location.href.lastIndexOf("#")==-1 ? window.location.href : window.location.href.substring(0,window.location.href.lastIndexOf("#"));

    purl += "#bf_" + frameName;
    
    url += (url.indexOf("?")==-1?"?":"&") + "purl=" + escape(purl);
    

    var f = document.getElementById("frame_"+frameName);

    if(f)
    {
        f.src = url;
    }
    else if(window[frameName])
    {
        window[frameName].location.href=url;
    }
    else
    {
        //You specified the wrong frame name
    }
}


/********************************* CALENDAR **********************************/



/*

Put a call to this function anywhere on the page that displays the BF Calendar 
Module in the "calFrame" IFRAME:

    <script> calendarOnLoad(); </script>

*/

function calendarOnLoad(monthView,params)
{
    if(window.attachEvent)
    {
        eval("window.attachEvent(\"onload\", function(i) { goToCalendar(\"calFrame\"," + monthView + ",\"" + (params?params:"") + "\"); });");
    }
    else
    {
        eval("window.addEventListener(\"load\", function(){ goToCalendar(\"calFrame\"," + monthView + ",\"" + (params?params:"") + "\"); }, false);");
    }
}

/*

This function is called from the calendarOnLoad function.  If there is an
"event_id" parameter on the query string of the URL of the HTML page, then
that parameter is added to the BF Calendar Module URL.

*/

function goToCalendar(frameName,monthView,parameters)
{
    // "var url" and "var monthViewURL" need to be changed to the domain of the site you are working on
    var x = window.location.search;
    
    if(!window.BF_EVENT_LIST_URL)
    {
    	window.BF_EVENT_LIST_URL = (window.location.href.indexOf("https://"+BF_MODULES_DOMAIN)==0 ? "https://" : "http://") + BF_MODULES_DOMAIN + "/bf/website/simple/calendar.jsp";
    }
    if(!window.BF_MONTH_VIEW_URL)
    {
    	window.BF_MONTH_VIEW_URL = (window.location.href.indexOf("https://"+BF_MODULES_DOMAIN)==0 ? "https://" : "http://") + BF_MODULES_DOMAIN + "/bf/website/simple/monthView.jsp";
    }
    
    var url = BF_EVENT_LIST_URL;
    var monthViewURL = BF_MONTH_VIEW_URL;
    var eventId = 0;
    var categoryId = 0;
    var awardEntriesView = false;

    if(x && (x.indexOf("event_id")>-1 || x.indexOf("categoryId")>-1))
    {
        if(x.substring(0,1)=="?")
            x = x.substring(1);

        var params = x.split("&");
        if(params && params.length>0)
        {
            for(var i=0; i<params.length; i++)
            {
                if(params[i].indexOf("event_id=")==0)
                {
                    var vals = params[i].split("=");
                    eventId = parseInt(vals[1]);
                }
                else if(params[i].indexOf("categoryId=")==0)
                {
                    var vals = params[i].split("=");
                    categoryId = parseInt(vals[1]);
                }
                else if(params[i].indexOf("awardEntriesView=")==0)
                {
                	var vals = params[i].split("=");
                	awardEntriesView = vals[1]=="true";
                }
            }
        }
    }
    
    if(!frameName)
        frameName = "calFrame";

    if(awardEntriesView && eventId > 0)
    {
    	url =  (window.location.href.indexOf("https://"+BF_MODULES_DOMAIN)==0 ? "https://" : "http://") + BF_MODULES_DOMAIN + "/bf/website/simple/awardEntriesView.jsp?event_id=" + eventId;
    }
    else if(eventId > 0)
    {
    	url += (url.indexOf("?")==-1?"?":"&") + "page=eventDetails.jsp&event_id=" + eventId;
    }
    else if(monthView)
    {
        url = monthViewURL;
    }
    
    if(categoryId > 0)
    	url += (url.indexOf("?")==-1?"?":"&") + "categoryId=" + categoryId;
    
    if(parameters)
    	url += (url.indexOf("?")==-1?"?":"&") + (parameters.substring(0,1)=="&"||parameters.substring(0,1)=="?" ? parameters.substring(1) : parameters);
    
    purl = window.location.href.lastIndexOf("#")==-1 ? window.location.href : window.location.href.substring(0,window.location.href.lastIndexOf("#"));

    purl += "#bf_" + frameName;
    
    url += (url.indexOf("?")==-1?"?":"&") + "purl=" + escape(purl);
    
    var f = document.getElementById("frame_"+frameName);

    if(f)
    {
        f.src = url;
    }
    else if(window[frameName])
    {
        window[frameName].location.href=url;
    }
    else
    {
        //You specified the wrong frame name
    }
}

function getUpcomingEventsURL(calendarPage,target,limit,params,miniMonthView)
{
    var url = (window.location.href.indexOf("https://"+BF_MODULES_DOMAIN)==0 ? "https://" : "http://") + BF_MODULES_DOMAIN + "/bf/website/simple/" 
    	+ (miniMonthView?"miniMonthView.jsp":"upcomingEvents.jsp") + "?target=" + (target?target:"_top") + "&calendarURL=" 
        + escape((window.location.href.indexOf("https://"+BF_MODULES_DOMAIN)==0 ? "https://" : "http://") + window.location.hostname + (calendarPage.indexOf("/")==0?"":"/") 
        + calendarPage) + (limit?"&limit=" + limit : "");
    
    	if(params)
    		url += "&" + params;
        
    return url;
}


/************************************** RANDOM ADS *************************************/

var RANDOM_ADS_ID = "randomAds";

/*

Put a call to this function in the top/head of any page that displays randoms ads in an
HTML tag with an ID of RANDOM_ADS_ID:

    <script> ramdomizeAdsOnLoad(); </script>

    <script> ramdomizeAdsOnLoad(new Array("randomAds-a","randomAds-b")); </script>

*/

function ramdomizeAdsOnLoad(ids)
{
	if(ids)
	{
	    if(window.attachEvent)
	    {
	        window.attachEvent("onload", function(i) {
	        	for(var i=0; i<ids.length; i++)
	         		randomizeAds(ids[i]);
	        });
	    }
	    else
	    {
	        window.addEventListener("load", function(){
	        	for(var i=0; i<ids.length; i++)
	         		randomizeAds(ids[i]);
			}, false);
	    }
	}
	else
	{
	    if(window.attachEvent)
	    {
	        window.attachEvent("onload", function(i) { randomizeAds(); });
	    }
	    else
	    {
	        window.addEventListener("load", function(){ randomizeAds(); }, false);
	    }
	}
}

/*

This function is called from the randomizeAdsOnLoad function.

*/

function randomizeAds(divId)
{
    var div = document.getElementById(divId?divId:RANDOM_ADS_ID);
	var originalAds = getCleanedUpImages(div);
    
    if(div)
    {
        //Add the <a>, <embed>, and <object> tags in random order

        var numberOfAds = originalAds.length;
        var randomAds = new Array(numberOfAds);
        var hasIndex = new Object();
        for(var i=0; i<numberOfAds; i++)
        {
            var randomIndex = Math.floor(numberOfAds*Math.random());
            while(hasIndex[randomIndex])
                randomIndex = Math.floor(numberOfAds*Math.random());

            randomAds[randomIndex] = originalAds[i];
            hasIndex[randomIndex] = true;
        }

        for(var i=0; i<numberOfAds; i++)
        {
            var ad = randomAds[i];
            div.appendChild(ad);
        }

        div.style.display = "inline";
    }
}


function rotateImagesOnLoad(divId,repeatSeconds,rotateCount)
{
    if(window.attachEvent)
    {
        window.attachEvent("onload", function(i) { initializeRotateImages(divId); rotateImages(divId,repeatSeconds,rotateCount); });
    }
    else
    {
        window.addEventListener("load", function(){ initializeRotateImages(divId); rotateImages(divId,repeatSeconds,rotateCount); }, false);
    }
}

function initializeRotateImages(divId)
{
    var div = document.getElementById(divId);
	var originalAds = getCleanedUpImages(div);

    //Clear out all child nodes

    var childNodes = div.childNodes;

    while(childNodes && childNodes.length>0)
    {
    	div.removeChild(childNodes[0]);
    }
    
    //Add cleaned up child nodes

    for(var i=0; i<originalAds.length; i++)
    {
        div.appendChild(originalAds[i]);
    }
    
    div.style.display = "inline";

    return div;
}


function getCleanedUpImages(div,originalAds)
{
	if(!originalAds)
		originalAds = new Array();
	
	if(div)
	{
		if(div.hasChildNodes())
		{
			while(div.hasChildNodes())
			{
				var child = div.childNodes[0];
				if(child.tagName)
				{
					if(child.tagName.toLowerCase() == "img" || child.tagName.toLowerCase() == "embed" || child.tagName.toLowerCase() == "object")
					{
		        		div.removeChild(child);
		        		originalAds.push(child);
					}
					else if(child.tagName.toLowerCase() == "a")
					{
			            var atag = child;
	
			            var imgTags = atag.getElementsByTagName("img");
	
			            if(imgTags && imgTags.length>0)
			            {
			            	//Only leave the <img> tags
	
			            	for(var e=0; e<imgTags.length; e++)
			            	{
			            		var imgTag = imgTags[e];
			            		var pNode = imgTag.parentNode;
			            		if(pNode && pNode != atag)
			            		{
			            			pNode.removeChild(imgTag);
			            			atag.appendChild(imgTag);
			            		}
			            	}
	
					       	var childNodes = atag.childNodes;
					        for(var e=0; e<childNodes.length; e++)
					        {
					        	var node = childNodes[e];
					        	if(node.nodeName.toLowerCase() != "img")
					        		atag.removeChild(node);
					        }
	
					        originalAds.push(atag);
			            }
	
		        		div.removeChild(atag);
					}
					else
					{
						originalAds = getCleanedUpImages(child,originalAds);
						div.removeChild(child);
					}
				}
				else
				{
					div.removeChild(child);
				}
			}
		}
	}
	
	return originalAds;
}

function rotateImages(divId,repeatSeconds,rotateCount)
{
    var div = document.getElementById(divId);
    if(div)
    {
    	if(!rotateCount)
    		rotateCount = 1;
    	
    	for(var i=0; i<rotateCount; i++)
    	{
	        var childNodes = div.childNodes;
	        if(childNodes)
	        {
	        	var firstChild = childNodes[0];
	        	div.removeChild(firstChild);
	       		div.appendChild(firstChild);
	        }
    	}
        
        div.style.display = "inline";

        if(repeatSeconds > 0)
        {
        	window.setInterval("rotateImages('" + divId + "'," + 0 + "," + rotateCount + ")",repeatSeconds*1000);
        }
    }
}

/****************************** RESIZE IFRAMES *******************************/
/*
    DYNAMICALLY CHANGE IFRAME HEIGHT BASED ON THE CONTENT IN THE IFRAME
    Copyright (c) 2008 BuilderFusion, Inc.
    
    Written by Jason LuBean
    
    Installation
    
    1.  Include this JavaScript file on the page where you have at least one IFRAME tag that 
        you want the height to be resized, based on the height of the content in the IFRAME tag.
    
    2.  Invoke the "bfResizeIframe" function once on the page.  This function will check every 10
        millisecond to see if there is a hash at the end of the URL in the form 
        "#bf_frameName_frameHeight" where "frameName" is replaced with the IFRAME name and
        "frameHeight" is replaced with the height (in pixels) of the page (an integer).  If that
        hash exists and the corresponding IFRAME exists, it will set the height of the FRAME.
    
    3.  For each IFRAME you want resized, insert a DIV tag in the HTML where you want to the IFRAME
        to be inserted.  Specify an ID for that tag.  There is no need for any content to be in the
        DIV tag because the code MUST dynamically add the IFRAME tag in the DIV tag in order for 
        the browser to allow the code resize the IFRAME.  After the DIV tag (or in the window.onload),
        invoke the "bfLoadResizeIframe" function:
        
             <div id="__ID__"></div>
             <script>
                var frameName = "dirFrame";
                var frameURL = "http://demo.builderfusion.com/bf/website/directory/index.jsp";
                var frameWidthPixels = 700;  
                var frameHeightPixels = 200; 
                bfLoadResizeIframe(frameName,frameURL,frameWidthPixels,frameHeightPixels);
             </script>
    4.  For each URL that is displayed in an IFRAME tag, attach/add an "onload" event listener
        (see "bfResizeParentFrameOnLoad" function) that does the following (if the "purl" 
        parameter exists on the query/search string):
        
            1.  Get the height of the page ("document.height" for Firefox; 
                "document.body.scrollHeight" for IE)
            2.  Get the "purl" value from the query string.  It will end with a hash in the form 
                "#bf_frameName" where "frameName" is replaced with the actual name of the IFRAME.
            3.  Modify the "purl" value to include a hash at the end in the form 
                "#bf_frameName_frameHeight" where "frameName" is replaced with the IFRAME name and
                "frameHeight" is replaced with the height (in pixels) of the page (an integer)
            4.  Set "window.top.location" to the modified "purl" value.
            
    The page including this script will 

    This code has been tested in IE 7.0 and Firefox 2.0.
    

*/

var bfIframes = new Object();

function bfResizeIframe()
{

    window.setInterval(
        function()
        {
            if(window.location.hash && window.location.hash!="#" && window.frames && window.frames.length>0)
            {
                var x = window.location.hash.substring(1);
                if(x.indexOf("bf_")==0)
                {
                    var parts = x.split("_");
                    if(parts.length == 3)
                    {
                        var frameName = parts[1];
                        
                        if(!frameName)
                        {
                        	var maxHeight = "0px";
                        	for(var i in bfIframes)
                        	{
                        		if(bfIframes[i].height && bfIframes[i].height > maxHeight)
                        		{
                        			maxHeight = bfIframes[i].height;
                        			frameName = i;
                        		}
                        	}
                        	
                        	if(!frameName)
                        	{
                        		for(var i in bfIframes)
                        		{
                        			if(!frameName)
                        			{
                        				frameName = i;
                        				break;
                        			}
                        		}
                        	}
                        }
                        
                        var y = parseInt(parts[2]);
                        if(!isNaN(y) && y > 0)
                        {
                            var frameHeight = y + "px";

                            var frameRef = bfIframes[frameName];

                            if(frameRef)
                            {
                                frameRef.height = frameHeight;
                            }

                        }

                    }
                }
            }
        }
        ,10
    );
}

function bfLoadResizeIframe(frameName,frameURL,frameWidth,frameHeight)
{
    var frameDiv=document.getElementById(frameName);

    if(frameDiv)
    {
        var purl = window.location.href.lastIndexOf("#")==-1 ? window.location.href : window.location.href.substring(0,window.location.href.lastIndexOf("#"));

        purl += "#bf_" + frameName;

        var url = frameURL ? (frameURL + (frameURL.indexOf("?")==-1?"?":"&") + "purl=" + escape(purl)) : "";

        var frameRef=document.createElement("iframe");
        var h={
            name:frameName,
            src:url,
            frameBorder:0,
            width:frameWidth,
            height:frameHeight,
            marginWidth:"0",
            marginHeight:"0",
            hspace:"0",
            vspace:"0",
            allowTransparency:"true",
            scrolling:"no"
        };
        for(var o in h)
        {
            frameRef.setAttribute(o,h[o])
        }

        if(frameRef.attachEvent)
        {
            frameRef.attachEvent("onload",
                function(i)
                {
                    window.scrollTo(0,0)
                }
            )
        }
        else
        {
            frameRef.addEventListener("load",
            function(){
            window.scrollTo(0,0)},
            false)
        }
        
        frameRef.id = "frame_" + frameName;

        frameDiv.appendChild(frameRef);

        bfIframes[frameName] = frameRef;
    }
}

/* FUNCTIONS FOR PAGES IN IFRAME */

function bfGetURLParams()
{
    var params = new Object();
    var search = location.search;
    if(search.indexOf("?") == 0)
    {
        var parts = search.substring(1).split("&");
        for(var i=0; i<parts.length; i++)
        {
            var part = parts[i];
            var pieces = parts[i].split("=");
            if(pieces.length==2)
            {
                params[pieces[0]] = unescape(pieces[1]);
            }
        }
    }
    
    return params;
}


function bfResizeParentFrame()
{
    var params = bfGetURLParams();
    var purl = params["purl"];

    if(purl)
    {
    	if(window.BF_DEBUG)
    		alert("bfResizeParentFrame:  purl=" + purl);
        var hashIndex = purl.lastIndexOf("#");
        var hash = hashIndex == -1 ? "" : purl.substring(hashIndex+1);
        purl = hashIndex == -1 ? purl : purl.substring(0,hashIndex);
        var parts = hash.split("_");
        var frameName = "";
        if(parts[0] == "bf" && parts.length>1)
            frameName = parts[1];

    	if(window.BF_DEBUG)
    		alert("bfResizeParentFrame:  frameName=" + frameName);
            
        if(frameName && parent != window && parent == top)
        {
            var frameHeight = document.height;
        	if(window.BF_DEBUG)
        		alert("bfResizeParentFrame:  document.height=" + frameHeight);
            if(!frameHeight)
            {
                frameHeight = document.body.scrollHeight;
            	if(window.BF_DEBUG)
            		alert("bfResizeParentFrame:  document.body.scrollHeight=" + frameHeight);
            }
            window.top.location = purl + "#bf_" + frameName + "_" + frameHeight;
        }
    }
}

function bfResizeParentFrameOnLoad()
{
    if(window.attachEvent)
    {
        window.attachEvent("onload", function(i) { if(bfResizeParentFrame) bfResizeParentFrame(); });
    }
    else
    {
        window.addEventListener("load", function(){ if(bfResizeParentFrame) bfResizeParentFrame(); }, false); 
    }
}

/****************************** UTILITY FUNCTIONS ****************************/

function getCookie(c_name)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
	    { 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) 
				c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
	    } 
	}
	return "";
}

function setCookie(label, data, expires, path, domain, secure)
{
	var cookie = label + "=" + data;

	if(expires)
		cookie += "; expires=" + expires.toGMTString();
	if(path)
		cookie += "; path=" + path;
	if(domain)
		cookie += "; domain=" + domain;
	if(secure)
		cookie += "; secure";

	document.cookie = cookie;
}

function getSearchParams(searchString)
{
	var params = new Object();
    if(searchString)
    {
        if(searchString.substring(0,1)=="?")
        	params = searchString.substring(1);
    
        var parts = searchString.split("&");
        if(parts && parts.length>0)
        {
            for(var i=0; i<parts.length; i++)
            {
            	var parts2 = parts[i].split("=");
            	if(parts2 && parts2.length==2)
            		params[parts2[0]] = unescape(parts2[1]);
            }
        }
    }

    return params;
}
