﻿//CourseInfo hold the current course information.
CourseInfo = function() {
    this.Code = null;
    this.Title = null;
    this.Description = null;
    this.Version = null;
    this.CreatedOn = null;
    this.LastModifiedOn = null;
    this.Copyright = null;
    this.FootNote = null;
}

var courseInfo = new CourseInfo();

function GetXmlElement(elementToSearch, elementName)
{
    var element = null;

    if (null != elementToSearch)
    {
        for (var i = 0; i < elementToSearch.childNodes.length; i++)
        {
            var node = elementToSearch.childNodes[i];
            if (node.nodeType == 1)
            {
                if (node.nodeName == elementName)
                {
                    element = node;
                    break;
                }
            }
        }
    }

    return element;
}

function LoadXML(uri)
{
    try //Internet Explorer
    {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    }
    catch (e)
    {
        try //Firefox, Mozilla, Opera, etc.
        {
            xmlDoc = document.implementation.createDocument("", "", null);
        }
        catch (e)
        {
            alert(e.message);
            return;
        }
    }

    xmlDoc.async = false;
    xmlDoc.load(uri);

    return xmlDoc;
}

function SerializeXMLToString(xmlElement)
{
    var str = null;

    if (null != xmlElement)
    {
        try
        {
            if (window.ActiveXObject)
            {
                str = xmlElement.xml;
            }
            else if (document.implementation && document.implementation.createDocument)
            {
                var s = new XMLSerializer();
                str = s.serializeToString(xmlElement);
            }
        }
        catch (e)
        {
            str = '<div style="color:red;"><em><b>Error: Failed to parse XML.</b></em></div>';
        }
    }

    return str;
}

function ClearElement(el)
{
    if (null != el)
    {
        if (el.hasChildNodes())
        {
            while (el.childNodes.length >= 1)
            {
                el.removeChild(el.firstChild);
            }
        }
    }
}

function ReplaceAll(text, strA, strB)
{
    while (text.indexOf(strA) != -1)
    {
        text = text.replace(strA, strB);
    }
    return text;
}

//Displays a JQuery Modal dialog box.
//@strTitle - the title of the dialog box.
//@strMessage - the message text to show in the dialog.
function ShowDialogModal(strTitle, strMessage, arr) {

    $('#dialog').attr('title', strTitle);

    var titles = "<p align=justify>" + strMessage + "</p>" + "<ul>";
    if (arr != null) {
        for (var i = 0; i < arr.length; i++) {
            titles += "<b> <li>";
            titles += arr[i].title;
            titles += "</li></b>";
        }
        titles += "</ul>";
    }
    //$('#dialog').children('#dialogtext').html(titles);
    $('#dialog').html(titles);
    $('#dialog').dialog(
                       {
                           autoOpen: false,
                           modal: true,
                           resizable: false,
                           zIndex: 1,
                           buttons: { "Ok": function() { $(this).dialog("destroy"); } }
                       });

    $("#dialog").dialog('open');
}

//Displays a JQuery Modal dialog box at the specified position @x,@y,
//note: currently it displays at left,top.
//@strTitle - the title of the dialog box.
//@strMessage - the message text to show in the dialog.
function ShowDialogModalXY(strTitle, strMessage, arr) {

    $('#dialog').attr('title', strTitle);
    var titles = "<p align=justify>" + strMessage + "</p>" + "<ul>";
    if (arr != null) {
        for (var i = 0; i < arr.length; i++) {
            titles += "<b> <li>";
            titles += arr[i].title;
            titles += "</li></b>";
        }
        titles += "</ul>";
    }
    //$('#dialog').children('#dialogtext').html(titles);
    $('#dialog').html(titles);
    $('#dialog').dialog(
                       {
                           autoOpen: false,
                           modal: true,
                           resizable: false,
                           zIndex: 10,
                           position: ['left', 'top'],
                           buttons: { "Ok": function() { $(this).dialog("destroy"); } }
                       });

    $("#dialog").dialog('open');
}

//Displays a JQuery dialog box.
//@strTitle - the title of the dialog box.
//@strMessage - the message text to show in the dialog.
function ShowDialog(strTitle, strMessage) {

    $('#dialog').attr('title', strTitle);
    $('#dialog').children('#dialogtext').text(strMessage);

    $('#dialog').dialog(
                       {
                           autoOpen: false,
                           modal: false,
                           resizable: false,
                           zIndex: 1,
                           buttons: { "Ok": function() { $(this).dialog("close"); } }
                       });

    $("#dialog").dialog('open');
}

//Reads the course information from the Config.ProjectInfo
//JSON object and wraps them in a CourseInfo object for later use.
function SetCourseInformation(projectInfo) {
    courseInfo.Code = projectInfo.ProjectCode;
    courseInfo.Title = projectInfo.Title;
    courseInfo.Description = projectInfo.Description;
    courseInfo.Version = projectInfo.Version;
    courseInfo.CreatedOn = projectInfo.CreatedOn;
    courseInfo.LastModifiedOn = projectInfo.LastModifiedOn;
    courseInfo.Copyright = projectInfo.Copyright;
    courseInfo.FootNote = projectInfo.FootNote;          
}

//A small utility called 'Contains' which checks if the
//given element is present in the javascript Array object.
//Uses Linear search.
Array.prototype.Contains = function(element) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == element) {
            return true;
        }
    }
    return false;
}