﻿// JScript File
var dialogTimeoutInterval = null;
var dialogScrollInterval = null;

function dialog_create(pObject, title, content, width, classname, id, direction, timeToShow)
{
    this.parentObj = pObject;
    this.m_title = null;
    this.m_content = null;
    this.m_width = 300;
    this.m_x = 0;
    this.m_y = 0;
    this.m_xbuffer = 20;
    this.m_ybuffer = 2;
    this.m_hasCloseButton = true;
    this.m_classname = null;
    this.m_id = this.GenerateID();
    this.m_direction = null;
    this.m_timetoshow = 5000;
    
    if (timeToShow && !isNaN(timeToShow))
        this.m_timetoshow = timeToShow;
    
    if (title) this.m_title = title;
    if (content) this.m_content = content;
    if (width) this.m_width = width;
    if (classname) this.m_classname = classname;
    if (id) this.m_id = id;
    if (direction) this.m_direction = direction;
    
    if (id)
    {
        var pDialog = $get(id);
        if (pDialog)
        {
            var html_doc = document.getElementsByTagName('body').item(0);
            html_doc.removeChild(pDialog);
        }
    }
    
    return this;
}

dialog_create.prototype.GenerateID = function()
{
    var result, i, j;
    result = '';
    for(j=0; j<32; j++)
    {
        if( j == 8 || j == 12|| j == 16|| j == 20)
        result = result + '-';
        i = Math.floor(Math.random()*16).toString(16).toUpperCase();
        result = result + i;
    }
    return result
}

dialog_create.prototype.HasCloseButton = function(yesno)
{
    this.m_hasCloseButton = yesno;
}

dialog_create.prototype.SetStyle = function(classname)
{
    this.m_classname = classname;
}

dialog_create.prototype.SetTitle = function(title)
{
    this.m_title = title;
}

dialog_create.prototype.SetContent = function(content)
{
    this.m_content = content;
}

dialog_create.prototype.SetWidth = function(width)
{
    this.m_width = width;
}

dialog_create.prototype.OnClose = function(pCloseButton)
{
    alert(pCloseButton.id);
}

dialog_create.prototype.CloseFunction = function()
{
    return '';
}

dialog_create.prototype.Create = function()
{
    // Get Parents position
    var position = Sys.UI.DomElement.getBounds(this.parentObj);
    var center = (document.body.offsetWidth) / 2;
    var arrowposition = 'left';
    
    // Y Position
    var browser_y = window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop; 
    if (browser_y == undefined || isNaN(browser_y))
        browser_y = 0;
        
    this.m_y = (position.y + position.height + this.m_ybuffer);
    
    // If direction is not set then calculate
    if (!this.m_direction)
        if (center > position.x)
            this.m_direction = 'mdleft';
        else
            this.m_direction = 'mdright';
    
    if (this.m_direction == 'mdleft')
        this.m_x = (position.x + this.m_xbuffer);
    else
        this.m_x = (position.x + this.m_xbuffer - this.m_width );
    
    arrowposition = this.m_direction;
    
    
    this.innerHTML  = "<div id='mdarrow' class='" + arrowposition + "'></div>"
                    + "<div id='mdmessage'>"
                    + "<div>"
                    + "<div style='float:left' id='mdtitle'>" + this.m_title + "</div>" 
                    + "<div style='float:right;' id='mdclosebutton' onclick='javascript:dialog_create_close(\"" + this.m_id +"\",\"" + this.parentObj.id + "\");'></div>"
                    + "</div>" 
                    + "<div id='mdcontent'>"
                    + this.m_content + "<br>"
                    + "</div>"
                    + "</div>"; 

    var container = document.createElement('div');
    container.id = this.m_id;
    container.className = this.m_classname;
    container.style.width = this.m_width + "px";
    container.innerHTML = this.innerHTML;    
    container.style.position = "absolute";
    
    // IE6 doesnt understand fixed
    /*
    if (navigator.appName == "Microsoft Internet Explorer") 
        if (typeof document.body.style.maxHeight == "undefined")
        {
           container.style.position = "absolute";
           this.m_y += browser_y; 
        }
        else
            this.m_y -= browser_y; 
    else
        this.m_y -= browser_y; 
    */
    container.style.top =  this.m_y + "px";
    container.style.left = this.m_x + "px";
    container.style.display = 'none';
    container.pObj = this;
         
    //document.body.appendChild(container);
    var html_doc = document.getElementsByTagName('body').item(0);
    html_doc.appendChild(container);
        
    dialog_create_reposition(this.m_id, this.parentObj.id);
    container.style.display ='';
    setOnScreen(container);
    
    // set reposition
    var repositionjs = "dialog_create_reposition(\"" + this.m_id + "\",\"" + this.parentObj.id + "\");";
    dialogScrollInterval = setInterval(repositionjs, 100);
    // Set auto close
    var closejs = "dialog_create_close(\"" + this.m_id + "\",\"" + this.parentObj.id + "\");";
    dialogTimeoutInterval = setTimeout(closejs, this.m_timetoshow);
}

dialog_create_findparentposition = function(parentbtn)
{
    var position = '';
    var parentobj = parentbtn;
    while (position == '' && parentobj)
    {
        if (parentobj && parentobj.style && parentobj.style.position)
            position = parentobj.style.position;
        parentobj = parentobj.parentNode;
    }
    
    return position;
}

dialog_create_reposition = function(container, parentbtn)
{
    var cDiv = $get(container);
    var pDiv = $get(parentbtn);
    if (!cDiv || !pDiv) return;
    
    var obj = cDiv.pObj;
    if (!obj) return;
    
    // Get position
    var pPosition = Sys.UI.DomElement.getBounds(pDiv);
    var cPosition = Sys.UI.DomElement.getBounds(cDiv);
    
    var top = (parseInt(pPosition.y) + parseInt(pPosition.height) + obj.m_ybuffer );
    var browser = (navigator.appName);
    if (dialog_create_findparentposition(pDiv) == "fixed" && browser != "Microsoft Internet Explorer" )
        top += getBrowserY();
    cDiv.style.top = top + 'px';
}

function getBrowserY()
{
    return window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
}

function getWindowSize()
{
    var myWidth = 0, myHeight = 0;
    
    if( typeof( window.innerWidth ) == 'number' ) 
    {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
    {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
    {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    
    return [myWidth, myHeight];
}

function setOnScreen(dialog)
{
    var bounds =  Sys.UI.DomElement.getBounds(dialog);
    var browser_y = getBrowserY();
    var browser_bounds = getWindowSize();
    var screenheight = browser_bounds[1];
    var scrollby = 0;
    
    if ((bounds.y + bounds.height) - browser_y > screenheight)
    {
        scrollby = (bounds.y + bounds.height) - browser_y - screenheight;
    }
    window.scrollTo(0, browser_y + scrollby);
}

function dialog_create_close(dialogID, parentID)
{
    var pDialog = $get(dialogID);
    if (pDialog)
        document.body.removeChild(pDialog);
        
    var pParent = $get(parentID)
    if (pParent)
        switch (pParent.type)
        {
            case "button" :
                pParent.disabled = '';
        }

    if (dialogTimeoutInterval != null)
        clearInterval(dialogTimeoutInterval);        
}

function ErrorDialog(message, parentID, timeToShow)
{
    message = JSFixMessage(message);
    
    var js = 'GenericDialogI(\'Sorry\',\'' + message + '\',\'' + parentID + '\',\''+ timeToShow + '\');';
    setTimeout(js,100);
}

function GenericDialog(title, message, parentID, timeToShow)
{
    title = JSFixMessage(title);
    message = JSFixMessage(message);
    
    var js = 'GenericDialogI(\'' + title + '\',\'' + message + '\',\'' + parentID + '\',\'' + timeToShow + '\');';
    setTimeout(js,100);
}

function JSFixMessage(msg)
{
    return msg.replace(/'/gi, "\\'");
}

function GenericDialogI(title, message, parentID, timeToShow)
{
    var pObject = $get(parentID);
    if (!pObject)
    {
        alert(message);
        return;
    }
    var dialog = new dialog_create(pObject, title, message, 400, 'messageDialog', parentID + '_error', null, timeToShow);
    dialog.Create();
}
