if (!window.HOPEDIGEST)(function() {

    var initialized = false;

    HOPEDIGEST = {

        globals: {
            network_id: false,
            campaign_id: false,
            offer_id: false,
            track_id: false,
            doLogging: false,
            widgets: new Object,
            log: ""
        },

        init: function() {
            if (!initialized) {
                initialized = true;
            }
        },

        log: function(msg) {
            if (self.globals.doLogging) {
                self.globals.log += (msg + "\n");
            }
        },

        resource: function(fname, ftype) {
            self.log("Loading resource " + fname + ", " + ftype);
            if (ftype == "js") {
                var fref = document.createElement('script');
                fref.setAttribute("type", "text/javascript");
                fref.setAttribute("src", fname);
            } else if (ftype == "css") {
                var fref = document.createElement("link");
                fref.setAttribute("rel", "stylesheet");
                fref.setAttribute("type", "text/css");
                fref.setAttribute("href", fname);
            }
            if (typeof fref != "undefined") {
                self.log("Before " + document.getElementsByTagName("head").item(0).childNodes.length);
                document.getElementsByTagName("head")[0].appendChild(fref);
                self.log("After " + document.getElementsByTagName("head").item(0).childNodes.length);
            }
        },

        identify: function(nwid, tkid, cpid, ofid) {
            self.log("Identifying " + nwid + ", " + tkid + ", " + cpid);
            if (nwid) {
                self.globals.network_id = nwid;
                self.log("Identified nwid " + self.globals.network_id);
            }
            if (tkid) {
                self.globals.track_id = tkid;
                self.log("Identified tkid " + self.globals.track_id);
            }
            if (cpid) {
                self.globals.campaign_id = cpid;
                self.log("Identified cpid " + self.globals.campaign_id);
            }
            if (ofid) {
                self.globals.offer_id = ofid;
                self.log("Identified cpid " + self.globals.offer_id);
            }
        },

        register: function(wid, code) {
            self.log("Registering " + wid + " with code " + code.length);
            self.globals.widgets[wid] = code;
        },

        doWidget: function(wid, nwid, tkid, cpid, ofid) {
            self.log("Rendering [" + wid + "] with nwid [" + nwid + "], and tkid [" + tkid + "], and cpid [" + cpid + "], and ofid [" + ofid + "]");
            self.identify(nwid, tkid, cpid, ofid);
            var code = self.globals.widgets[wid];
            var code = HOPEDIGEST.globals.widgets[wid];
            if (typeof code != "undefined") {
                self.log("Writing code [" + code + "]");
                self.resource('http://files.prefabmarkets.com/javascripts/widgets/' + wid + '.css', 'css')
                document.write('<div id="hopedigest_widget_' + wid + '" align="center">' + code + '</div>');
            } else {
                self.log("Code is undefined for [" + wid + "]");
            }
        },

        // Utility Functions
        selboxValue: function(eid) {
            var sbx = document.getElementById(eid);
            return sbx.options[sbx.selectedIndex].value;
        }

    };

    var self = HOPEDIGEST;
    HOPEDIGEST.init();

})();

// Grab data from fields
function sendHopeDigestSeniorCrunchWidget() {
    if (window.HOPEDIGEST) {
        var fields = ["forwhom", "age", "funds", "lins", "arranged"];
        var url = 'http://hopedigest.com/seniorcarefunding?';
        var params = [];
        if (HOPEDIGEST.globals.network_id) params.push('network=' + escape(HOPEDIGEST.globals.network_id));
        if (HOPEDIGEST.globals.campaign_id) params.push('campaign=' + escape(HOPEDIGEST.globals.campaign_id));
        if (HOPEDIGEST.globals.offer_id) params.push('offer=' + escape(HOPEDIGEST.globals.offer_id));
        if (HOPEDIGEST.globals.track_id) params.push('id=' + escape(HOPEDIGEST.globals.track_id));
        for (var i = 0; i < fields.length; i++) {
            params.push("hd[" + fields[i] + "]=" + escape(HOPEDIGEST.selboxValue("hopedigest_sc600_" + fields[i])));
        }
        url += params.join("&");
        window.location = url;
        // alert("URL [" + url + "]");
    } else {
        alert("ERROR! HOPEDIGEST is not defined!");
    }
    return true;
}

// Register HTML code for [[[ senior_crunch_600 ]]]
HOPEDIGEST.register('senior_crunch_600', '<table width="600" border="0" cellspacing="0" cellpadding="0" class="main"><tr><td colspan="5" class="ttle"><img src="http://files2.prefabmarkets.com/images/hd/seniorcrunch/title600.gif" width="598"></td></tr><tr><td colspan="5"><img src="http://files7.prefabmarkets.com/images/spacer.gif" width="10" height="10" alt=" "></td></tr><tr><td width="10" rowspan="5" valign="top" class="spcr"><img src="http://files7.prefabmarkets.com/images/spacer.gif" width="20" height="20" alt=" "></td><td align="center" valign="top" class="slbx"><select id="hopedigest_sc600_forwhom" name="hd[forwhom]"><option value=""></option><option value="Myself">Myself</option><option value="My Spouse">My Spouse</option><option value="My Parent">My Parent</option><option value="Other">Other</option></select></td><td width="10" rowspan="5" valign="top" class="spcr"><img src="http://files7.prefabmarkets.com/images/spacer.gif" width="10" height="10" alt=" "></td><td valign="top" class="qstn">For whom are you seeking a senior living community?</td><td width="10" rowspan="5" valign="top" class="spcr"><img src="http://files7.prefabmarkets.com/images/spacer.gif" width="20" height="20" alt=" "></td></tr><tr><td align="center" valign="top" class="slbx"><select id="hopedigest_sc600_age" name="hd[age]"><option value=""></option><option value="1">Yes</option><option value="0">No</option></select></td><td valign="top" class="qstn">Is the person seeking care 72 years of age or older?</td></tr><tr><td align="center" valign="top" class="slbx"><select id="hopedigest_sc600_funds" name="hd[funds]"><option value=""></option><option value="1">Yes</option><option value="0">No</option></select></td><td valign="top" class="qstn">Does the person needing care intend to pay for senior housing with the proceeds of a house sale over the next 12 months?</td></tr><tr><td align="center" valign="top" class="slbx"><select id="hopedigest_sc600_lins" name="hd[lins]"><option value=""></option><option value="1">Yes</option><option value="0">No</option></select></td><td valign="top" class="qstn">Does the person needing care have an in-force life insurance policy?</td></tr><tr><td align="center" valign="top" class="slbx"><select id="hopedigest_sc600_arranged" name="hd[arranged]"><option value=""></option><option value="1">Yes</option><option value="0">No</option></select></td><td valign="top" class="qstn">Has the person needing care already arranged to move into an assisted living or retirement home within the next 60 days?</td></tr><tr align="center"><td colspan="5" class="sbmt"><img onclick="sendHopeDigestSeniorCrunchWidget()" src="http://files3.prefabmarkets.com/images/hd/seniorcrunch/submit.gif" alt="Submit Information" align="middle" width="122" height="24" border="0"></td></tr></table>');

// Register HTML code for [[[ senior_crunch_160 ]]]
HOPEDIGEST.register('senior_crunch_160', '<table width="160" border="0" cellspacing="0" cellpadding="0"><tr><td colspan="3" class="ttle"><img src="http://files2.prefabmarkets.com/images/hd/seniorcrunch/title160.gif" width="158"></td></tr><tr><td colspan="3"><img src="http://files7.prefabmarkets.com/images/spacer.gif" width="10" height="10" alt=" "/></td></tr><tr><td width="10" rowspan="10" valign="top" class="spcr"><img src="http://files7.prefabmarkets.com/images/spacer.gif" width="10" height="10" alt=" "/></td><td valign="top" class="qstn">For whom are you seeking a senior living community?</td><td width="10" rowspan="10" valign="top" class="spcr"><img src="http://files7.prefabmarkets.com/images/spacer.gif" width="10" height="10" alt=" "/></td></tr><tr><td valign="top" class="slbx"><select id="hopedigest_sc600_forwhom" name="hd[forwhom]"><option value=""></option><option value="Myself">Myself</option><option value="My Spouse">My Spouse</option><option value="My Parent">My Parent</option><option value="Other">Other</option></select></td></tr><tr><td valign="top" class="qstn">Is the person seeking care 72 years of age or older?</td></tr><tr><td valign="top" class="slbx"><select id="hopedigest_sc600_age" name="hd[age]"><option value=""></option><option value="1">Yes</option><option value="0">No</option></select></td></tr><tr><td valign="top" class="qstn">Does the person needing care intend to pay for senior housing with the proceeds of a house sale over the next 12 months?</td></tr><tr><td valign="top" class="slbx"><select id="hopedigest_sc600_funds" name="hd[funds]"><option value=""></option><option value="1">Yes</option><option value="0">No</option></select></td></tr><tr><td valign="top" class="qstn">Does the person needing care have an in-force life insurance policy?</td></tr><tr><td valign="top" class="slbx"><select id="hopedigest_sc600_lins" name="hd[lins]"><option value=""></option><option value="1">Yes</option><option value="0">No</option></select></td></tr><tr><td valign="top" class="qstn">Has the person needing care already arranged to move into an assisted living or retirement home within the next 60 days?</td></tr><tr><td valign="top" class="slbx"><select id="hopedigest_sc600_arranged" name="hd[arranged]"><option value=""></option><option value="1">Yes</option><option value="0">No</option></select></td></tr><tr align="center"><td colspan="3" class="sbmt"><img onclick="sendHopeDigestSeniorCrunchWidget()" src="http://files3.prefabmarkets.com/images/hd/seniorcrunch/submit.gif" alt="Submit Information" align="middle" width="122" height="24" border="0"></td></tr><tr><td colspan="3" align="center" valign="bottom"><img src="http://files7.prefabmarkets.com/images/hd/seniorcrunch/logo_small.gif" alt=" "/></td></tr></table>');

// Display a 600x300 widget for Senior Crunch
// ==========================================
// Must pass an affiliate id or it will send a commented error message
function HopeDigestSeniorCrunch600Widget(hd_aff_id, hd_subaff_id, campaign_id, offer_id) {
    if (hd_aff_id == null) {
        document.write('<!-- ERROR! You need to provide HopeDigestSeniorCrunch600Widget with an affiliate_id -->');
    } else {
        HOPEDIGEST.doWidget('senior_crunch_600', hd_aff_id, hd_subaff_id, 'H', offer_id);
    }
}

// Display a 160x600 widget for Senior Crunch
// ==========================================
// Must pass an affiliate id or it will send a commented error message
function HopeDigestSeniorCrunch160Widget(hd_aff_id, hd_subaff_id, campaign_id, offer_id) {
    if (hd_aff_id == null) {
        document.write('<!-- ERROR! You need to provide HopeDigestSeniorCrunch160Widget with an affiliate_id -->');
    } else {
        HOPEDIGEST.doWidget('senior_crunch_160', hd_aff_id, hd_subaff_id, 'V', offer_id);
    }
}
