var qsParm = new Array();
qs();

function qs() 
{
    var query = window.location.search.substring(1);
    var parms = query.split('&');
    for (var i=0; i<parms.length; i++) 
    {
        var pos = parms[i].indexOf('=');
        if (pos > 0) 
        {
            var key = parms[i].substring(0,pos);
            var val = parms[i].substring(pos+1);
            qsParm[key] = val;
        }
    }
} 

// Capture impressions per page (one call per page to this routine with up to 10 impressions)
function Impressions(GraphicNames)
{

    //document.write(GraphicNames+'<br>');
    var xmlhttp;
    if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

	
	
    
	xmlhttp.open("GET", 'http://www.world-grain.com/ImpressionsOAS.asp?AdGraphic='+GraphicNames+'&RURL='+PageName,true);
    xmlhttp.send(null);
	
}


// Capture Video Clicks
function Videos(VideoName)
{
    var xmlhttp;
    if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.open("GET", 'http://www.world-grain.com/Videos.asp?VideoGraphic='+VideoName+'&RURL='+PageName,true);
    xmlhttp.send(null);
}

// Capture Failures here (Failures to unpack an impression to find the ad URL)
function Failures(HTML)
{
    var xmlhttp;
    if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.open("GET", 'http://www.world-grain.com/Failures.asp?RURL='+PageName+'&HTML='+HTML,true);
    xmlhttp.send(null);
}

function StripOutGraphic(GraphicURL)
{
    //document.write(GraphicURL+'<br>');
    var arrURL = GraphicURL.split("/");
    if (arrURL.length >=0) arrImpressions[arrImpressions.length] = arrURL[arrURL.length-2];
	//document.write();
	//if (arrURL.length >=0) arrImpressions[arrImpressions.length] = arrURL(8);
}


function ModifyUrl(AdId)
{
   try
    {
        var OurAdScriptTag = document.getElementById(AdId);
        if (!OurAdScriptTag) return false;
        
        // If the ExpandAd= parameter is coded, then see if this is a match
        if (AdId == qsParm['ExpandAd'])
        {
           DisplayThisAd(OurAdScriptTag);     
        }
        
        // Review the ad and grab the creative and save it
        var AdConductorLink = OurAdScriptTag.nextSibling;
        switch (AdConductorLink.tagName.toLowerCase())
        {
            case 'a':
            	// Ad Conductor - Static ad - Fixup the URL and record impression
                StripOutGraphic(AdConductorLink.firstChild.src);
                AdConductorLink.href='http://test.world-grain.com/ClickThruOAS.asp?AdURL='+AdConductorLink.href+'&AdGraphic='+AdConductorLink.firstChild.src;
				//AdConductorLink.href='http://test.world-grain.com/ClickThruOAS.asp?AdURL='+AdConductorLink.href+'&AdGraphic='+AdConductorLink.firstChild.src+'&RURL='+PageName;
                break;
            default:
            	// Ad Conductor - Rich-media ad in an OBJECT tag with a MOVIE child
            	var bFound = false;
                for (var i=0;i<AdConductorLink.childNodes.length;i++)
                {
                    if (AdConductorLink.childNodes[i].name.toLowerCase() == 'movie')
                    {
                        var arrValue = AdConductorLink.childNodes[i].value.split("?");
                        if (arrValue.length > 0)
                        {
                            // record impression only
                            StripOutGraphic(arrValue[0]);
                            bFound = true;
                            break;
                        }                            
                    }
                }

                if (bFound == true) break;
            
                
                // See if this is a redirect where we've added an ID in the script tag
                if (AdConductorLink.id)
                {
                    if (AdConductorLink.id.length>0)
                    {
                        StripOutGraphic(AdConductorLink.id);
                        bFound = true;
                        break;
                    }
                }
                
                // DoubleClick - Redirect ad is in an IMG tag
                if (FindIMGChild(OurAdScriptTag.parentNode))
                {
                    bFound = true;
                    break;
                }

                // Unknown Company's Redirect 
                // We've missed and cannot parse the impression correctly 
                // So push it to a new DIV where we can see the HTML
                // But only if DebugAds=true
                if (!bFound && qsParm['DebugAds']=='true')
                {
                    DisplayThisAd(AdConductorLink);
                }
                break;
                
                // if this ad is not found anywhere else, then save it as a missing ad.
                if (!bFound)
                {
                    StripOutGraphic('Missing: '+AdId);
                    if (OurAdScriptTag)
                    {
                        if (OurAdScriptTag.parentNode)
                        {
                            if (OurAdScriptTag.parentNode.outerHTML.length > 0)
                            {
                                Failures(OurAdScriptTag.parentNode.outerHTML);
                            }
                            else
                            {
                                Failures('<html><body>OurAdScriptTag.parentNode.OuterHTML.length == 0</body></html>');
                            }
                        }
                        else
                        {
                            Failures('<html><body>Missing OurAdScriptTag.parentNode</body></html>');
                        }
                           
                    }
                    else
                    {
                        Failures('<html><body>Missing OurAdScriptTag</body></html>');
                    }
                }
        }
     }
     catch (err)
    {
    }

}

// Find the first IMG tag in this branch of the DOM and save it as the impression
function FindIMGChild(AdLink)
{
    var RC = false;
    for (var i=0;i<AdLink.childNodes.length;i++)
    {
        if (AdLink.tagName.toLowerCase() == 'img')
        {
            if (AdLink.childNodes[i].src)
            {
                StripOutGraphic(AdLink.childNodes[i].src);
                return true;
            }
        }
        else
        {
            if (AdLink.childNodes[i].length>0)
            {
                if (FindIMGChild(AdLink.childNodes[i]))
                {
                    RC = true;
                    break;
                }
            }
        }
    }
    return RC;
}

function DisplayThisAd(AdLink)
{
    var newDiv = document.createElement("div");
    newDiv.style.position='absolute';
    newDiv.style.top=0;
    var newTextarea = document.createElement("textarea");
    newTextarea.style.width=400;
    newTextarea.style.height=400;
    newDiv.appendChild(newTextarea);
    newDiv.childNodes[0].innerText=AdLink.parentNode.outerHTML;
    document.body.appendChild(newDiv);
}
     

