﻿

var odocuAffiliate = new function odocuAffiliate() {
    var affiliateCode = null;
    var positionCode = null;
    var testMode = -1;
    var testWidth = 190;
    var testHeight = 390;

    function Init(ac) {
        affiliateCode = ac;

        if (window.addEventListener) // W3C standard 
        {
            window.addEventListener('load', RegisterDynamicScript, false); // NB **not** 'onload' 
        }
        else if (window.attachEvent) // Microsoft 
        {
            window.attachEvent('onload', RegisterDynamicScript);
        }
    }

    function RegisterDynamicScript() {
        var headID = document.getElementsByTagName("head")[0];
        if (headID != null) {
            var newScript = document.createElement('script');
            newScript.type = 'text/javascript';
            newScript.src = 'http://www.odocu.com/site/affiliate/psjavascript.aspx?AC=' + affiliateCode + '&CB=' + (new Date().getTime().toString());
            headID.appendChild(newScript);
        }
    }

    function ReplaceOdocuAffiliate(posDataStr) {
        var positionData = eval('(' + posDataStr + ')');
        var promo = document.getElementsByTagName("odocu:promospace");
        if (promo != null && promo.length > 0) {
            while (promo.length > 0) {
                //                affiliateCode = promo[0].getAttribute("ac");
                positionCode = promo[0].getAttribute("pos");
                var ok = false;

                if (affiliateCode != null && positionCode != null && posDataStr != '{}') {
                    //
                    // Search for position information
                    // 
                    for (i = 0; i < positionData.positions.length; i++) {
                        if (positionData.positions[i].code == positionCode) {
                            if ((testMode == 1) || (positionData.positions[i].content == "1" && testMode < 0)) {
                                if (promo[0].outerHTML) {
                                    promo[0].outerHTML = RenderTag(affiliateCode, positionCode, positionData.positions[i]);
                                }
                                else {
                                    ReplaceTag(promo[0], RenderTag(affiliateCode, positionCode, positionData.positions[i]));
                                }

                                ok = true;
                            }
                            break;
                        }
                    }
                }

                if (!ok) {
                    if (promo[0].outerHTML) {
                        promo[0].outerHTML = '';
                    }
                    else {
                        ReplaceTag(promo[0], '');
                    }
                }
            }
        }
    }

    function ReplaceTag(htmlElement, htmlCode) {
        var r = htmlElement.ownerDocument.createRange();
        r.setStartBefore(htmlElement);
        var df = r.createContextualFragment(htmlCode);
        htmlElement.parentNode.replaceChild(df, htmlElement);
    }

    function RenderTag(affiliateCode, positionCode, positionData) {
        var infoText = '';

        if (testMode < 0) {
            infoText += '<iframe frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://www.odocu.com/site/affiliate/renderpromo.aspx?AC=' + affiliateCode + '&PC=' + positionCode + '\" width=\"' + positionData.w + '\" height=\"' + positionData.h + '\" />';
        }
        else {
            infoText += '<iframe frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://www.odocu.com/site/affiliate/renderpromo.aspx" width=\"' + testWidth + '\" height=\"' + testHeight + '\" />';
        }

        return (infoText);
    }

    function SetTestMode(mode, width, height) {
        if (mode == 0) {
            testMode = 0;
        }
        else if (mode == 1) {
            testMode = 1;
            testWidth = width;
            testHeight = height;
        }
        else {
            testMode = -1;
        }
    }

    this.Init = Init;
    this.SetTestMode = SetTestMode;
    this.ReplaceOdocuAffiliate = ReplaceOdocuAffiliate;
}



