var keywords = new Array();
var shapeName = null;

var geoType = null;
var geoValue = null;

function GetQueryStringValue(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}

function SetKeyword(name
 , value) {
    if (!keywords[name])
        keywords[name] = new Array();

    keywords[name].push(value);
}

function CreateADV(shapeName) {
    var debug = GetQueryStringValue('DEBUG', 0);
    var fakeip = GetQueryStringValue('FAKEIP', null);

    var host = (debug && debug == 1) ? 'localhost:63331' : 'ad.dibaio.com';

    var jscript = null;
    if (keywords && shapeName) {
        var keywordParam = JoinKeywords();
        jscript = '<script type="text/javascript" src="http://' + host + '/manager.adv?ACT=I&FTYPE=K&KEYWORD=' + keywordParam + '&SHAPE=' + shapeName + '"><\/script>';        
    }
    else if (shapeName) {
        jscript = '<script type="text/javascript" src="http://' + host +'/manager.adv?ACT=I&SHAPE=' + shapeName + '"><\/script>';
    }

    if (fakeip && fakeip != '')
        jscript = jscript.replace('?ACT', '?FAKEIP=' + fakeip + '&ACT');

    //window.onload = document.write(jscript);
    document.write(jscript);
}

//function ReplaceImgSrc() {
//    alert('replace');
//}

function JoinKeywords() {
    var joined = '';
    for (key in keywords) {
        joined += key + ":" + keywords[key].join(',') + "|";
    }

    return joined.substring(0, joined.length - 1);
}

function CreateUID() {
    var s = [];
    var hexDigits = "0123456789ABCDEF";
    for (var i = 0; i < 32; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[12] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
    s[16] = hexDigits.substr((s[16] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01

    var uuid = s.join("");
    return uuid;
}

function CreateIframe(width, height, content) {
    var identifier = CreateUID();
    document.write('<div id=' + identifier + '></div>');
    var container = document.getElementById(identifier);
    var iframe = document.createElement('iframe');

    if (iframe && container) {
        //iframe.name = iframe.id = name;
        iframe.setAttribute('frameborder', '0');
        iframe.setAttribute('marginheight', '0');
        iframe.setAttribute('marginwidth', '0');
        iframe.scrolling = 'no';
        iframe.width = width;
        iframe.height = height;
        //iframe.src = 'about:blank';
        container.appendChild(iframe);

        var iframeDoc;
        if (iframe.contentDocument) {
            iframeDoc = iframe.contentDocument;
        }
        else if (iframe.contentWindow) {
            iframeDoc = iframe.contentWindow.document;
        }
        else if (window.frames[iframe.name]) {
            iframeDoc = window.frames[iframe.name].document;
        }
        if (iframeDoc) {
            iframeDoc.open();
            iframeDoc.write('<style type="text/css">' +
                           'body' +
                           '{' +
                           ' margin:0px;' +
                           ' border:0px;' +                           
                           '}' +
                           '</style>')
            iframeDoc.write(content);
            iframeDoc.close();
        }
    }
}        