/* This code below is modified to use new SMILE 2.0 Display Framework back-end so as to keep backward compatibility with existing 
   SMILE 1.0 enabled web content pages. This will ensure that existing SMILE 1.0 web content continues to be operational without any    
   impact due to SMILE 2.0 changes.
   These changes are identified as "SMILE 1.0 Backward Compatibility Changes" in the comments.
*/
var smileXMLHttpRequest;
var smileHttp = (document.location.href.indexOf('https://')==0)?"https://":"http://";
var smileBase = smileHttp+"c2r-srvcs.sun.com/smile/";
var smileConfig = 0x0; /* 0x1 = dont show, 0x2 = show border */

/**
 * Bridge XMLHTTP to XMLHttpRequest in pre-7.0 Internet Explorers
 */
if( typeof XMLHttpRequest == "undefined" ) {
    smileXMLHttpRequest = function() {
        try { 
            return new ActiveXObject("Msxml2.XMLHTTP.6.0")
        } catch(e) {}
        try { 
            return new ActiveXObject("Msxml2.XMLHTTP.3.0")
        } catch(e) {}
        try { 
            return new ActiveXObject("Msxml2.XMLHTTP")
        }     catch(e) {}
        try { 
            return new ActiveXObject("Microsoft.XMLHTTP")
        }  catch(e) {}
        throw new Error( "This browser does not support XMLHttpRequest or XMLHTTP." )
    }
} else {
    smileXMLHttpRequest = XMLHttpRequest;
}

var offers = new Array();
var offerCount = 0;
var offerIndex = 0;
var offersLoaded = 0;

/* BEGIN: SMILE 1.0 Backward Compatibility Changes */
function smileLoadOffers(cnt) {

    if (offersLoaded == 0) {
        offersLoaded = 1;
        var vis = encodeURIComponent(smileReadCookie("s_vi"));
        var url = encodeURIComponent(document.URL);
        cnt = encodeURIComponent(cnt);

    if ((url != "null") && (url != "")){
        // Below is a format of the server request call and an explanation of parameters:
        // action: 0 = reportLayout(), 1=pageVisit(), 2=requestSMILEOffer()
        // src: source URL
        // vis: visitor cookie
        // cnt: count
        var req = smileBase + "OfferServlet" + "?" + "action=2" + "&cnt=" + cnt + "&src=" + url + "&vis=" + vis;

        document.write("<" + "script language='JavaScript' type='text/javascript'");
        document.write(" src='" + req + "'>" + "<" + "/script>");
      }
    }
}

function smileMedia(slt) {
    var scripts = document.getElementsByTagName('script'); 

    var scr = scripts[scripts.length-1];
  
    var vis = encodeURIComponent(smileReadCookie("s_vi"));
    var url = encodeURIComponent(document.URL);
    slt = encodeURIComponent(slt);

    if ((url != "null") && (url != "")){
        // Below is a format of the server request call and an explanation of parameters:
        // action: 0 = reportLayout(), 1=pageVisit(), 2=requestSMILEOffer()
        // src: source URL
        // vis: visitor cookie
        // slt: slot number
        var req = smileBase + "OfferServlet" + "?" + "action=2" + "&slt=" + slt + "&src=" + url + "&vis=" + vis;
    
        document.write("<" + "script language='JavaScript' type='text/javascript' src='");
        document.write(req);
        document.write("'><" + "/script>");
    }
}

function smileOffer(zone) {

    var offer = "";
    var exclude = "";
    var hlite = "";

    if (!document.phpAds_used) document.phpAds_used = ',';

    if (offerIndex < offerCount) 
    {
        offer = offers[offerIndex++];
        document.write(offer);
    } 
    else 
    {
      if ((zone != undefined) && (zone != "") && (zone != "null"))
      {
        offer = "zone:" + zone;
        exclude = document.phpAds_used;
        hlite = "#992222";
        phpAds_random = new String (Math.random()); phpAds_random = phpAds_random.substring(2,11);

        if ((smileConfig & 0x1) != 0x1) {
            if ((smileConfig & 0x2) == 0x2) {
                document.write("<" + "div style=\"border: 2px solid " + hlite + "\">");
            }
            document.write("<" + "script language='JavaScript' type='text/javascript' src='");
            document.write(smileHttp+"ads.sun.com/ads/adjs.php?n=" + phpAds_random);
            document.write("&what=" + offer);
            document.write("&block=1&blockcampaign=1");
            document.write("&exclude=" + exclude);
            if (document.referrer)
                document.write ("&referer=" + escape(document.referrer));
            document.write("'><" + "/script>");
            if ((smileConfig & 0x2) == 0x2) {
                document.write("<" + "/div>");
            }
        } else {
            document.write("<" + "!-- offer = " + offer + "--" + ">");
        }
      }
    }
}
/* END : SMILE 1.0 Backward Compatibility Changes */

function smileMediaCB(scr, text) {
    var content = document.createElement('span'); 
    content.innerHTML = text;
  
    scr.parentNode.insertBefore(content, scr); 
}

function smileMediaCBJS(scr, text) {
    var content = document.createElement('script'); 
    content.type = "text/javascript"
    content.src = text;
  
    scr.parentNode.insertBefore(content, scr); 
}

/* BEGIN: SMILE 1.0 Backward Compatibility Changes */
function smileVisit() {
    // As per SMILE Phase-2, the page visits are handled by Online Evaluator when a call is made to get the 
    // banner adId for 'E9' component type. This makes following code redundant and so it is commented out.
    // The function is purposefully kept empty as there may be older clients using this function. Such clients
    // are not impacted as this function itself is not deleted.
    /*
    var vis = encodeURIComponent(smileReadCookie("s_vi"));
    var url = encodeURIComponent(document.URL);
    var keywords = encodeURIComponent(smileKeywordsList());

    var req = smileBase + "VisitServlet";
    var params = "src=" + url + "&vis=" + vis + "&fld=keyword:" + keywords;
    params += "&cb=smileVisitCB";
    
    smileVisitObj = smileJson(req, params);    
    */
}

function smileVisitCB(node, text) {
    // As per SMILE Phase-2, the page visits are handled by Online Evaluator when a call is made to get the 
    // banner adId for 'E9' component type. This makes following code redundant and so it is commented out.
    // The function is purposefully kept empty as there may be older clients using this function. Such clients
    // are not impacted as this function itself is not deleted.
    /*
    var head =  document.getElementsByTagName("head").item(0);
    head.removeChild(smileVisitObj);
    */
}

function smileTrack(node) {
    // As per SMILE Phase-2, the clicks are handled by DF back-end and NOT by onClick() call. The DF server URL will
    // be built into the 'E9' component ad HTML content. Thus this function won't be needed.
    // The function is purposefully kept empty as there may be older clients using this function. Such clients
    // are not impacted as this function itself is not deleted.
    /*
    var tgt = encodeURIComponent(node.href);
    var vis = encodeURIComponent(smileReadCookie("s_vi"));
    var url = encodeURIComponent(document.URL);

    var req = smileBase + "TrackServlet";
    var params = "src=" + url + "&vis=" + vis + "&targetUrl=" + tgt;
    params += "&cb=smileTrackCB";

    smileTrackObj = smileJson(req, params);
    smileTrackCB(smileTrackObj);
    */
}
/*
function smileTrackCB(smileTrackObj) {
    document.getElementsByTagName("head").item(0).removeChild(smileTrackObj);
}

function smileJson(url, vars) {
    var scr = document.createElement("script");
    var time = (new Date()).getTime();
    
    scr.setAttribute("type", "text/javascript");
    scr.setAttribute("src", url + "?noCacheIE=" + time + "&" + vars);
    scr.setAttribute("id", + "SJid" + time);
    
    document.getElementsByTagName("head").item(0).appendChild(scr);

    return scr;
}
*/
/* END : SMILE 1.0 Backward Compatibility Changes */

function smileAjax(url, vars, node, CBFunction) {
    var req =  new smileXMLHttpRequest();
    req.open("POST", url, true);
    req.setRequestHeader("Content-Type", "application/x-javascript;");
  
    req.onreadystatechange = function() {
        if (req.readyState == 4 && req.status == 200) {
            if (req.responseText) {
                CBFunction(node, req.responseText);
            }
        }
    };
    req.send(vars);
}

function smileReadCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function smileKeywords (type) {
    var metas = document.all ?
    document.all.tags('META') :
    document.getElementsByTagName ?
    document.getElementsByTagName ('META') : new Array();
    var keywordsA = new Array();
    var keywordsL = "";
    var i = 0;
    for (var m = 0; m < metas.length; m++)
        if (metas[m].name == 'keywords') {
            keywordsA[i++] = metas[m].content;
            keywordsL = keywordsL + metas[m].content + ",";
        }
    if (type == 1)
        return keywordsA;
    else
        return keywordsL;
}
