var req;
var timerId = 0;

function reloadAd()
{
    // Clear timer
    //
    if( timerId )
    {
        clearTimeout( timerId );
        timerId = 0;
    }
    
    // issue the async call to retrieve sponsor xml document
    //
    if( typeof XMLHttpRequest != "undefined" )
    {
        req = new XMLHttpRequest();
    } 
    else 
    {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    var url = baseRef + "shared/SponsorAd.aspx";
    
    req.open( "POST", url, true );
    req.setRequestHeader("pragma", "no-cache");
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    req.onreadystatechange = callback;
    var Today = new Date();
    req.send("id=" + Today.getTime());

    // Reset the timer
    //
    timerId = setTimeout( "reloadAd()", 10000 );
}

function callback()
{
    if( req.readyState == 4 )
    {
        // Default Settings
        //
        var SponsorName = "CYO Donations";
        var ImageUrl = "..%2fimages%2fdonate-online-to-CYO-v2.gif";
        var AnchorUrl = "https://app.etapestry.com/hosted/CYO/OnlineGiving.html";

        if( req.status == 200 ) 
        {
            // Check return and update DOM.  Return should be in the form as follows:
            // <name>
            //  name
            // </name>
            // <imageurl>
            //  imageurl
            // </imageurl>
            // <anchorurl>
            //  anchorurl
            // </anchorurl>
            //
            var xmlDoc = req.responseXML;
            if( xmlDoc.getElementsByTagName("name").length > 0 )
                SponsorName = xmlDoc.getElementsByTagName("name")[0].firstChild.nodeValue;
            if( xmlDoc.getElementsByTagName("imageurl").length > 0 )
                ImageUrl = xmlDoc.getElementsByTagName("imageurl")[0].firstChild.nodeValue;
            if( xmlDoc.getElementsByTagName("anchorurl").length > 0 )
                AnchorUrl = xmlDoc.getElementsByTagName("anchorurl")[0].firstChild.nodeValue;
        }

        // Update the DOM Objects
        //        
        var idAnchor = document.getElementById('lnkSponsorURL');
        var idImage = document.getElementById('imgSponsorImage');
        var idSpan = document.getElementById('litSponsorName');

        if( idAnchor != null )
            idAnchor.href = AnchorUrl;
        if( idSpan != null )
            idSpan.innerHTML = SponsorName;
        if( idImage != null )
        {
            // Preload the image to avoid the broken image icon
            //
            var rslt = new Image();
		    rslt.src = baseRef + 'shared/MakeThumbnail.aspx?file=' + ImageUrl + '&size=110';

            // Now load the image and fade back in
            //
            idImage.src = rslt.src;
            idImage.style.visibility = 'visible';
        }
    }
}    

// Change the opacity of a specified object.  Used to fade logos in/out
//
function opacity(id, opacStart, opacEnd, millisec) { 
    //speed for each frame 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i--) { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i++) 
            { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } 
} 

// change the opacity for different browsers 
//
function changeOpac(opacity, id) { 
    var object = document.getElementById(id).style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
} 


