﻿// JScript File

function dialog_create(pObject, title, content, width, classname, id, direction)
{
    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;
    
    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)
            document.body.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
    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 = 'left';
        else
            this.m_direction = 'right';
    
    if (this.m_direction == 'left')
        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='arrow' class='" + arrowposition + "'></div>"
                    + "<div id='message'>"
                    + "<div>"
                    + "<div style='float:left' id='title'>" + this.m_title + "</div>" 
                    + "<div style='float:right;' id='closebutton' onclick='javascript:dialog_create_close(\"" + this.m_id +"\",\"" + this.parentObj.id + "\");'></div>"
                    + "</div>" 
                    + "<div id='content'>"
                    + 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";
    container.style.top =  this.m_y + "px";
    container.style.left = this.m_x + "px";
    
    document.body.appendChild(container);
}

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 = '';
        }
        
}

function ErrorDialog(message, parentID)
{
    var pObject = $get(parentID);
    var dialog = new dialog_create(pObject, 'Error', message, 300, 'messageDialog', parentID + '_error', 'left');
    dialog.Create();
}