﻿function elEncodeToHex(str) {
    var r = "";
    var e = str.length;
    var c = 0;
    var h;
    while (c < e) {
        h = str.charCodeAt(c++).toString(16);
        while (h.length < 4) h = "0" + h;
        r += h;
    }
    return r;
}
function elGetDefaultType() { return 1; }
function elGetDefaultLang() { return 0; }
function elIsAdTypeExists(type) {
    if (type >= 1 && type <= 14) return true;
    else return false;
}
function elIsLangExists(lang) {
    if (lang >= 0 && lang <= 2) return true;
    else return false;
}

function elGetAdWidth(type) {
    switch (type) {
        case 1: return 200;
        case 2: return 468;
        case 3: return 728;
        case 4: return 468;
        case 5: return 234;
        case 6: return 160;
        case 7: return 120;
        case 8: return 125;
        case 9: return 180;
        case 10: return 336;
        case 11: return 250;
        case 12: return 160;
        case 13: return 160;
        case 14: return 728;
        default: return 200;
    }
}

function elGetAdHeight(type) {
    switch (type) {
        case 1: return 370;
        case 2: return 210;
        case 3: return 90;
        case 4: return 60;
        case 5: return 60;
        case 6: return 600;
        case 7: return 600;
        case 8: return 125;
        case 9: return 150;
        case 10: return 280;
        case 11: return 250;
        case 12: return 370;
        case 13: return 600;
        case 14: return 90;
        default: return 370;
    }
}

function elGetLanguageShortStr(lang) {
    switch (lang) {
        case 0: return "";
        case 1: return "_ru";
        case 2: return "_en";
        default: return "";
    }
}

function elCreateAd(aff, affP, url, width, height, type, lang) {
    //var elUrl = "http://localhost:5319/ExtraLeads/Ad/ad";
    var elUrl = "http://www.lead-machine.co.il/Ad/ad";

    if (type == 1 || type == 2 || type == 12 || type == 13 || type == 14)
        elUrl += type + "" + elGetLanguageShortStr(lang) + ".aspx";
    else
        elUrl += "_banner.aspx";
    
    elUrl += "?aff=" + aff.toString();
    elUrl += "&aff_p=" + affP.toString();
    elUrl += "&type=" + type.toString();
    elUrl += "&lang=" + lang.toString();
    elUrl += "&url=" + url.toString();

    //document.write("<div id=\"ttt\"><a href=\"http://www.test.com\">test</a></div>");
    document.write("<iframe src=\"" + elUrl + "\" scrolling=\"no\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" style=\"width: " + width + "px; height: " + height + "px; border-style: none;\"  id=\"ExtraLeadsAd" + Math.floor(Math.random() * 10000000) + "\"></iframe>");

    //promo = document.getElementById("ttt");
    //promo.style.visibility = "hidden";

    /*ifrm = document.createElement("IFRAME");
    ifrm.setAttribute("src", el_url);
    ifrm.setAttribute("scrolling", "no");
    ifrm.setAttribute("frameborder", "0");
    ifrm.setAttribute("marginheight", "0");
    ifrm.setAttribute("marginwidth", "0");

    ifrm.style.width = width + "px";
    ifrm.style.height = height + "px";
    //ifrm.style.border = "0px solid #C0C0C0";
    //ifrm.style.overflow = "hidden";

    adDiv = document.getElementById("ExtraLeadsAd");
    adDivNewID = "ExtraLeadsAd" + Math.floor(Math.random() * 10000000);
    adDiv.id = adDivNewID;
    adDiv.appendChild(ifrm);*/
}

function elShowAd() {
    var tAff = 0;
    var tAffP = 0;
    var tType = 1;
    var tLang = 0;
    var tWidth = 0;
    var tHeight = 0;
    var tUrl = elEncodeToHex(window.location.href);    

    if (el_aff != null && el_aff != undefined)
        tAff = parseInt(el_aff);

    if (el_aff_p != null && el_aff_p != undefined)
        tAffP = parseInt(el_aff_p);

    if (el_type != null && el_type != undefined)
        tType = parseInt(el_type);

    if (elIsAdTypeExists(tType) == false)
        tType = elGetDefaultType();

    if (el_lang != null && el_lang != undefined)
        tLang = parseInt(el_lang);

    if (elIsLangExists(tLang) == false)
        tLang = elGetDefaultLang();

    tWidth = elGetAdWidth(tType);
    tHeight = elGetAdHeight(tType);

    elCreateAd(tAff, tAffP, tUrl, tWidth, tHeight, tType, tLang);
}


// Init vars:
el_aff = null;
el_aff_p = null;
el_type = null;
el_lang = null;
