/************************************************************************
File:           tdbDynamicIframeLoader.js

IMPORTANT:      IF THIS SCRIPT IS CUSTOMIZED FOR A CLIENT, BE SURE TO
                PROTECT IT IN THE FORM TOOL.

Description:    JavaScript code for a client to include in their web
                site page(s) that host an iframe that loads a page
                served at thedatabank.  At this writing, the client's
                iframe may be hosting Online Forms and/or an Advocacy
                site.

                thedatabank's software setup records the URL of the
                client's iframe hosting page(s).  When the tdb software
                needs to link to an Online Form or Advocacy page, it
                creates a URL to the client's hosting page, passing the
                tdb URL in the querystring.  tdb software cannot link
                directly to the Online Form or Advocacy page, because
                it would be unbranded - the client's hosting page
                provides the branding.  By passing the URL to load into
                the iframe, and letting this script figure out what to
                load into the iframe, the client does not need to have
                a hosting page for every page that thedatabank's software
                might need to link to, AND does not need to do any
                programming.  They simply need to add the following to
                the <head> section of their hosting page(s):

                FOR ONLINE FORMS (secure):
                <script language="javascript" type="text/javascript" src="https://www.thedatabank.com/dpg/<clientid>/tdbDynamicIframeLoader.js"></script>

                FOR ADVOCACY (not secure):
                <script language="javascript" type="text/javascript" src="http://www.thedatabank.com/dpg/<clientid>/tdbDynamicIframeLoader.js"></script>

Setup:          If the client has Advocacy, set their domain in the
                stdbHostedActionDomain variable.

                This script adds window.onload code.  For sites that use
                JQuery (or other JS frameworks), there's potential to
                conflict with the JQuery .ready event, or similar
                functionality on other frameworks.  The site may also
                have its own window.onload code for its own purposes.
                
                The AddLoadEvent function is attempts to avoid these
                conflicts.  In testing this script seemed able to
                peacefully coexist with JQuery, but still, it might need
                to be tweaked in to work on some sites.
                   
Author:         Sean Gardner

Copyright:      (c) 2011 thedatabank inc
************************************************************************/
var stdbHostedActionDomain = "http://takeactionminnesota.e-actionmax.com/";  // All lower case.  E.g., http://dogooders.e-actionmax.com/

//---------------------------
function AddLoadEvent(func) {
//---------------------------  
    var oldonload = window.onload;  
    if (typeof window.onload != 'function') {  
        window.onload = func;  
    } else {  
        window.onload = function() {  
        	if (oldonload) {  
        	    oldonload();  
        	}  
        	func();  
        }  
    }  
}

//------------------------
function LoadTDBIframe() {
//------------------------
    var sLoadUrl = "";
    var sQString = document.location.search;
    var sQString = sQString.substring(1, sQString.length);  // Querystring without the initial question mark
    var raPairs = sQString.split("&");
    var raPair;
    for (var idx = 0; idx < raPairs.length; idx++) {
        raPair = raPairs[idx].split("=");
        if (raPair[0] && raPair[0].toLowerCase() == "tdburl") {
            if (raPair[1]) sLoadUrl = raPair[1];
        }
        if (sLoadUrl > "") break;
    }
    if (sLoadUrl > "") {
        var el = document.getElementById("tdbIframe");
        sLoadUrl = unescape(sLoadUrl);
        var sLCUrl = sLoadUrl.toLowerCase();
        if (sLCUrl.substring(0, 27) == "http://www.thedatabank.com/"
            || sLCUrl.substring(0, 28) == "https://www.thedatabank.com/"
            || (stdbHostedActionDomain > "" && sLCUrl.substring(0, stdbHostedActionDomain.length) == stdbHostedActionDomain)) {

            if (el) el.src = sLoadUrl;
        }
    }
}

AddLoadEvent(LoadTDBIframe);

