(function(){
function encode(v) {
    return typeof encodeURIComponent == "function" ?
        encodeURIComponent(v) : escape(v);
}

function appendArg(k, v) {
    req_url = req_url.concat("&" + k + "=" + v);
}

function appendEncodedArg(k, v) {
    appendArg(k, encode(v));
}

function getReferrer() {
    var result = document.referrer;
    if (!result) {
        try {
            result = window.opener.location.href;
        } catch(e) {
            result = null;
        }
    }
    return result;
}

function sendAdRequest() {
    appendEncodedArg("req", document.location);
    appendEncodedArg("rnd", Math.floor(Math.random() * 1000));
    if (typeof(youdao_codeid) != "undefined") {
       appendArg("codeid", youdao_codeid);
    }
    if (typeof(youdao_bgc) != "undefined") {
        appendEncodedArg("bgc", youdao_bgc);
    }
    if (typeof(youdao_tc) != "undefined") {
        appendEncodedArg("tc", youdao_tc);
    }
    if (typeof(youdao_dc) != "undefined") {
        appendEncodedArg("dc", youdao_dc);
    }
    if (typeof(youdao_uc) != "undefined") {
        appendEncodedArg("uc", youdao_uc);
    }
    if (typeof(youdao_client) != "undefined") {
        appendEncodedArg("syndid", youdao_client);
    }
    if (typeof(youdao_member) != "undefined") {
        appendEncodedArg("memberid", youdao_member);
    }
    if (typeof(youdao_position) != "undefined") {
        appendEncodedArg("posid", youdao_position);
    }
    if (typeof(youdao_template) != "undefined") {
        appendEncodedArg("tn", youdao_template);
    }

    var text = '<div style=display:none>-</div><iframe align="center,center"';
    if (typeof(youdao_width) != "undefined") {
    	appendEncodedArg("width", youdao_width);
        text += ' width=' + youdao_width;
    }
    if (typeof(youdao_height) != "undefined") {
    	appendEncodedArg("height", youdao_height);
        text += ' height=' + youdao_height;
    }
    req_url = req_url.substring(0, 2000);
    req_url = req_url.replace(/%\w?$/, "");

    var ref2 = getReferrer();
    if(ref2) {
        req_url += "&ref2=" + ref2;
    }
    
    text += ' src="http://impservice.union.youdao.com' + '/imp/request.s?' + req_url.slice(1)
        + '" marginwidth=0 marginheight=0 scrolling=no frameborder=0 '
        + 'allowtransparency></iframe>';
    document.write(text);
    req_url = null;
}

var req_url = "";
sendAdRequest();
})();

