﻿var yPos = 0;
var xPos = 0;
var attachToObj = document.createElement("div");
var div = document.createElement("div");
var errDiv = document.createElement("div");
var divWidth = 0;
var divHeight = 0;
var autoClose = true;

var inlineObj = function(w, h, path, file, params, obj, ac) {
    var url = "/_popups/";
    showAllSelects();
    divWidth = w;
    divHeight = h;
    autoClose = (ac != null && ac.toString().toLowerCase() == "false") ? false : true;
    if (path != '') {
        url = "/" + path + "/_popups/" + file + "?" + params;
    }
    else {
        url = "/_popups/" + file + "?" + params;
    }
    attachToObj = obj;
    $.ajax({ type: "GET", url: url, data: "{}", contentType: "application/x-www-form-urlencoded;", dataType: "html",
        success: function(e) {
            return renderInline(e);
        }
    });
}

$.inlineObjPost = function(url, data) {
    $.ajax({ type: "POST", url: url, data: data, contentType: "application/x-www-form-urlencoded;", dataType: "html",
        success: function(e) {
            return renderInline(e);
        }
    });
}

function renderInline(response) {    
    response = replaceChars(response, '&lt;', '<');
    response = replaceChars(response, '&gt;', '>');
    response = replaceChars(response, '&amp;', '&');

    if (response.length != 0) {
        yPos = getTop(document.getElementById(attachToObj)) - 150;
        xPos = getLeft(document.getElementById(attachToObj)) - 50;
        if (yPos <= 0) {
            yPos = yPos + 100;
        }
        hideAllSelects();
        div.style.cssText = "z-Index:100;left:" + xPos + "px;" + "top:" + yPos + "px" + ";position:absolute;width:" + divWidth + "px;height:" + divHeight;
        //errDiv.style.cssText = "z-Index:101; filter: alpha(opacity=50); -moz-opacity: 0.5;opacity: 0.5; display: block; background-color:#000000; left:" + xPos + "px;" + "top:" + yPos + "px" + ";position:absolute;width:" + divWidth + "px;height:" + divHeight;
        //errDiv.innerHTML = "&#160;";
        
        div.innerHTML = response;
        document.body.appendChild(div);
        //document.body.appendChild(errDiv);
    }
}


function escKey(evt) {
    var evt = (evt) ? evt : ((event) ? event : null);
    if (evt.keyCode == 27) {
        hideDiv()
    }
}
document.onkeypress = escKey;

if (IE) {
    document.onclick = function() {
        if (document.getElementById(attachToObj) != null) {
            thisX = event.clientX + document.body.scrollLeft;
            if (autoClose && (thisX < xPos || thisX > (xPos + divWidth))) {
                hideDiv();
            }
        }
    };
}
else {
    document.onclick = function(e) {
        if (document.getElementById(attachToObj) != null) {
            thisX = e.pageX;
            if (autoClose && (thisX < xPos || thisX > (xPos + divWidth))) {
                hideDiv();
            }
        }
    }
}
