function oc_onLoad( contextPath ) {

    function onSuccess( request ) {

        function displayProfileModule( xml ) {

            var nodes = xml.getElementsByTagName( "user-name" );
            if( ! nodes || nodes.length == 0 ) {
                return;
            }
            var node = nodes[0];
            if( ! node.childNodes || node.childNodes.length == 0 ) {
                return;
            }
            var name = node.childNodes[0].nodeValue;

            core_setElementText( "adsk91-oc-profup-name1", name );
            core_setElementText( "adsk91-oc-profup-name2", name );

            var div = document.getElementById( "adsk91-oc-profup" );
            if( ! div ) {
                return;
            }
            div.style.display = "block";

        }

        function displayRecentActivities( xml ) {

            function appendLink( ul, linkNode ) {

                function oc_getOfferURL( contextPath, linkNode ) {
                    var siteID = linkNode.getAttribute( "site-id" );
                    var contentID = linkNode.getAttribute( "contid" );
                    var host = linkNode.getAttribute( "host" );
                    var url = host ? "http://" + host : "";
                    url += contextPath + "/servlet/oc/offer?siteID=" + siteID + "&id=" + contentID;
                    return url;
                }

                var li = document.createElement( "li" );
                ul.appendChild( li );

                var a = document.createElement( "a" );
                a.setAttribute( "href", oc_getOfferURL( contextPath, linkNode ) );
                li.appendChild( a );

                a.innerHTML = linkNode.getAttribute( "linktext" );

            }

            var nodes = xml.getElementsByTagName( "recent-activities" );
            if( ! nodes || nodes.length == 0 ) {
                return;
            }

            nodes = nodes[0].getElementsByTagName( "wam-index" );
            if( ! nodes || nodes.length == 0 ) {
                return;
            }
            var indexNode = nodes[0];

            var links = indexNode.getElementsByTagName( "link" );
            if( ! links || links.length == 0 ) {
                // No links
                return;
            }

            // Find the list
            var div = document.getElementById( "adsk91-oc-recent" );
            if( ! div ) {
                return;
            }
            var ul = document.createElement( "ul" );
            div.appendChild( ul );

            for( var i = 0; i < links.length; i++ ) {
                appendLink( ul, links[i] );
            }

            div.style.display = "block";

            // Put the divider on the previous div
            var indDiv = document.getElementById( "adsk91-oc-ind" );
            if( indDiv ) {
                indDiv.style.borderBottom = "solid 1px #cccccc";
            }

        }

        var xml = request.responseXML;
        displayProfileModule( xml );
        displayRecentActivities( xml );

    }

    function onFailure( request, obj ) {
        window.status = request.url + ": failure - " + obj;
    }

    function onException( request, error ) {
        window.status = request.url + ": " + error.message;
    }

    // 2006-08-10 (JK): Do not send an explicit If-Modified-Since header; this should be handled automatically
    //  by the browser.
    var parameters = "";
    var siteID = cda_getSiteID();
    if( siteID ) {
        parameters = "siteID=" + siteID;
    }
    new Ajax.Request(
        oc_getNavDataURL( contextPath ),
        {
            method: 'get',
            parameters: parameters,
            onComplete: onSuccess,
            onFailure: onFailure,
            onException: onException
        }
    );
}

function oc_getNavDataURL( contextPath ) {
    return contextPath + "/servlet/oc/navdata.xml";
}

function oc_getTYPDataURL( contextPath, offerID ) {
    return contextPath + "/servlet/oc/offer/typ/status.xml?id=" + offerID;
}

function oc_SelectIndustry(e) {
    var url = e.options[e.selectedIndex].value;
    if( url ) {
        document.location = url;
    }
}

function oc_verifyProfileDelete() {
    var msg = "If you click \"OK\" all profile information will be deleted. Do you want to continue?";
    var e = document.getElementById( "adsk91-oc-trans-delprof" );
    if( e ) {
        msg = e.value;
    }
    return confirm( msg );
}

function octyp_onLoad( offerID, siteID, contextPath, isPreview ) {

    function onSuccess( request ) {

        function displayFulfillmentSection( xml ) {

            var nodes = xml.getElementsByTagName( "user_data" );
            if( ! nodes || nodes.length == 0 ) {
                return;
            }

            var node = nodes[0];
            if( node.getAttribute( "offer-status" ) != "redeemed" ) {
                var url = "http://" + location.host + contextPath
                    + "/servlet/oc/offer?"
                    + "siteID=" + siteID
                    + "&id=" + offerID
                ;
                window.top.location = url;
                return;
            }

            octyp_showFulfillment();

        }

        var xml = request.responseXML;
        displayFulfillmentSection( xml );

    }

    new Ajax.Request(
        oc_getTYPDataURL( contextPath, offerID ) + ( isPreview ? "&preview=1" : "" ),
        {
            method: 'get',
            onComplete: onSuccess,
            onFailure: function( r ) { core_ajaxOnFailure( r, url, parameters ) },
            onException: core_ajaxOnException
        }
    );
}

function octyp_showFulfillment() {
    var div = document.getElementById( "section2" );
    if( div ) {
        div.style.visibility = "visible";
    }
    div = document.getElementById( "adsk91-maincontent-footer" );
    if( div ) {
        div.style.visibility = "visible";
    }
    div = document.getElementById( "id-event-details" );
    if( div ) {
        div.style.display = "";
    }

    div = document.getElementById( "id-div-sessions-reg" )
    if( div && div.style.display == "none" ) {
        EventReg_showSessions();
    }
}

