__ModalDialogue_version=1;

///<Documentation>
///<Class name="ModalDialogue">
///<Constructor>
///<summary>ModalDialogue constructor</summary>
///<param name="id" type="string" required="false">
///     The id to be assigned to the HTML control generated from the Render()
///     function. If this is not provided, than a GUID is automatically assigned.
///</param>
///<param name="caption" type="string" required="false">
///     The name to be displayed as a caption for the modal dialogue
///</param>
///<param name="text" type="string" required="false">
///     The message to be displayed in the dialogue.
///</param>
///<param name="type" type="integer" required="false">
///     The type of Modal dialogue box to be displayed. Currently supported:
///     0 - Informational (OK button only)
///     1 - Confirmation (Yes and No buttons)
///</param>
function ModalDialogue(caption,text)
{
    if(__base_version==undefined && __IrthErrorVersion>0)
        new IrthError("ModalDialogue","Constructor","Object requires the inclusion of the GeneralScript.js file.").Throw();
    if(__dragAndDrop_version==undefined && __IrthErrorVersion>0)
        new IrthError("ModalDialogue","Constructor","Object requires the inclusion of the DragAndDrop.js file.").Throw();
        
    this.Title=(caption==undefined||caption==null)?'':caption;
    this.Text=(text==undefined||text==null)?'':text;
    this.ID = __getGUID();
    this.BackdropID = this.ID + 'modal';
    this.EnableClose=true;
    this.Buttons=new Array();
    this.ImagesLocation='../Images/';
    this.Draggable = true;
    this.NoButtons = false;
}
///</Constructor>

///<Method name="AddButton">
///<summary>Adds a button to the Modal Dialogue box</summary>
///<returns>void</returns>
ModalDialogue.prototype.AddButton=function(text,onclick)
{
    this.Buttons.push(new Button(text,onclick));
} 
///</Method>

///<Method name="ClearButtons">
///<summary>Clears all buttons from the Modal Dialogue box</summary>
///<returns>void</returns>
ModalDialogue.prototype.ClearButtons=function()
{
    this.Buttons.clear();
} 
///</Method>

///<Method name="ClearButtons">
///<summary>Clears all buttons from the Modal Dialogue box</summary>
///<returns>void</returns>
ModalDialogue.prototype.Dispose = function(backdropID)
{
    document.body.removeChild($(this.ID));
    document.body.removeChild($(this.BackdropID));
}
///</Method>

///<Method name="Show">
///<summary>Shows the Modal Dialogue box</summary>
///<returns>HTML object</returns>
ModalDialogue.prototype.Show = function(container)
{
    var dialogueContainer = document.createElement("DIV"),
        dialogue = document.createElement("TABLE"),
        title = dialogue.insertRow(),
        filler = title.insertCell(),
        titleText = title.insertCell(),
        close = title.insertCell(),
        text = dialogue.insertRow().insertCell(),
        buttonContainer = dialogue.insertRow().insertCell(),
        closeImage = document.createElement("IMG"),
        backdropID = this.BackdropID,
        closeOnClick = "__ModalDialogue_toggle('" + this.ID + "', '" + this.BackdropID + "')";

    if (this.Buttons.length == 0 && !this.NoButtons)
        this.Buttons.push(new Button('OK'));

    if (container == undefined || container == null)
    {
        var modalBackdrop = document.createElement("DIV");
        modalBackdrop.className = 'ModalDialogueBackground';
        modalBackdrop.id = backdropID;
        modalBackdrop.style.width = document.body.clientWidth;
        modalBackdrop.style.height = document.body.clientHeight;
        document.body.appendChild(modalBackdrop);
    }
    else
    {
        container.className = 'ModalDialogueBackground';
        backdropID = container.id;
    }

    dialogueContainer.className = 'ModalDialogueContainer';
    dialogueContainer.id = this.ID;
    if (typeof container == 'undefined' || container == null)
        document.body.appendChild(dialogueContainer);
    else
        container.appendChild(dialogueContainer);

    dialogue.className = 'ModalDialogue';

    title.className = 'ModalDialogueTitle';

    titleText.className = 'ModalDialogueTitleText';
    titleText.innerHTML = this.Title;

    if (this.EnableClose)
    {
        close.className = 'ModalDialogueClose';
        closeImage.src = this.ImagesLocation + 'close.PNG';
        closeImage.onclick = closeOnClick;
        closeImage.onmouseover = "this.src='" + this.ImagesLocation + "close_hover.PNG'";
        closeImage.onmouseout = "this.src='" + this.ImagesLocation + "close.PNG'";
        closeImage.alt = 'X';
        close.appendChild(closeImage);
    }

    text.className = 'ModalDialogueText';
    text.colSpan = 3;
    text.innerHTML = this.Text;

    buttonContainer.className = 'ModalDialogueButtonContainer';
    buttonContainer.colSpan = 3;

    for (var i = 0; i < this.Buttons.length; i++)
    {
        button = document.createElement("INPUT");
        button.type = 'button';
        button.onclick = (this.Buttons[i].onClick == null) ? closeOnClick : closeOnClick + ';' + this.Buttons[i].onClick;
        button.id = this.ID + '_close_' + i;
        button.className = 'ModalDialogueButton';
        button.value = this.Buttons[i].Text;

        if (i > 0)
            buttonContainer.innerHTML += '&nbsp;';
        buttonContainer.appendChild(button);
    }

    dialogueContainer.innerHTML = dialogue.outerHTML;

    if (this.Draggable)
        makeDraggable(dialogueContainer, 250);

    __ModalDialogue_toggle(this.ID, this.BackdropID);
    return dialogueContainer;
}
///</Method>

///<EventMethod name="Show">
///<summary>Shows the Modal Dialogue box</summary>
///<returns>HTML object</returns>
__ModalDialogue_toggle = function(id, backdropID)
{
    var md = $(id),
        background = $(backdropID);

    if (!md) return;
    var mdVis = md.style.visibility;
    md.style.visibility = (mdVis == null || mdVis == '') ? 'visible' : (mdVis == 'visible') ? 'hidden' : 'visible';

    if (background)
    {
        backVis = background.style.visibility;
        background.style.visibility = (backVis == null || backVis == '') ? 'visible' : (backVis == 'visible') ? 'hidden' : 'visible';
    }
    else
        return;

    for (var i = 0; i < document.forms.length; i++)
        document.forms[i].disabled = !document.forms[i].disabled;
    return;
}
///</EventMethod>
///</Methods>
///</Class>

///<Class name="Button">
function Button(text,onclick)
{
    this.Text=text;
    this.onClick=onclick;
}
///</Class>

///<UnitTests>
///<Test object="ModalDialogue" name="Show()" description="Tests to ensure that the modal dialogue displays properly." type="positive">
ModalDialogue_Show=function()
{
    try
    {
	    var md=new ModalDialogue('Pop-up blocker is enabled!','This site requires the use of pop ups. <br /><br />In order to use the site without a problem, you will need to disable the Pop-up blocker before entering. This can be done by: <br />1. Click on the bar directly above this box ("Pop-up blocked. To see this pop-up or additional options click here...") <br />2. Select "Always Allow Pop-ups from This Site..."');
	    __actual=md.Show().innerHTML;
        __expected='';
	    return __actual==__expected;
    }
    catch(err){__actual=__handle_error(err)}
    return false;
}
///</Test>
///</UnitTests>
///</Documentation>



