﻿var lmCopySectionID = -1;
var bmCopySectionExpires = false;
var lmCopyCellID = -1;

// -----------------
// General Functions
// -----------------

function getField(zString, lIndex, zSplit)
{
    var arrFields = new String(zString).split(zSplit);
    return arrFields[--lIndex];
}


function textArea(zString)
{
    var zpString = "";

    // Replaces Less Than and Greater Than tags with codes
    // and replaces backslash and doublequotes with extra backslash
    // DOES NOT remove line break, use removeLineBreak if required.
    if (zString == null)
        zpString = "";
    else
    {
        zpString = new String(zString);
        var regLt = /\</g;
        var regGt = /\>/g;
        //var regDQ = /\"/g;

        //zpString = zpString.replace(regDQ,"\\\"");
        zpString = zpString.replace(regLt, "&lt;");
        zpString = zpString.replace(regGt, "&gt;");
    }
    return zpString;
}

function textInput(zString)
{
    // Remove double quotes and line breaks
    if (zString == null)
        var zpString = "";
    else
    {
        var zpString = new String(zString);

        var regBreak = /\r\n/g;
        var regBackSlash = /\\/g;
        var regDQ = /\"/g;
        //"
        zpString = zpString.replace(regBackSlash, "&#92;");
        zpString = zpString.replace(regBreak, " ");
        zpString = zpString.replace(regDQ, "&quot;");
    }
    return zpString;
}

// ----------------
// XP Style Toolbar
// ----------------

var bmButtonClicked = false;

function buttonHover(myButton)
{
    if (bmButtonClicked == false && myButton.selected != "true")
    {
        myButton.style.backgroundImage = "url('SystemImages/ButtonBgHover.gif')";
    }

    myButton.cells[0].style.width = "0px";
    myButton.cells[3].style.width = "0px";
    myButton.style.border = "1px solid #002D96";
}

function buttonMouseDown(myButton)
{
    myButton.style.backgroundImage = "url('SystemImages/ButtonBgClick.gif')";
    bmButtonClicked = true;
}

function buttonMouseOut(myButton)
{
    if (myButton.selected == "true")
    {
        myButton.cells[0].style.width = "0px";
        myButton.cells[3].style.width = "0px";
        myButton.style.border = "1px solid #002D96";
        myButton.style.backgroundImage = "url('SystemImages/ButtonBgClick.gif')";

    }
    else
    {
        myButton.style.backgroundImage = "url('SystemImages/ButtonBg.gif')";
        bmButtonClicked = false;

        myButton.style.border = "0px solid #002D96";
        myButton.cells[0].style.width = "1px";
        myButton.cells[3].style.width = "1px";
    }
}

// ------------------
// Listview functions
// ------------------

var zmMenuOpt = "";

function listviewClick(zID)
{
    if (zmMenuOpt != "")
    {
        document.getElementById(zmMenuOpt).style.backgroundColor = "transparent";
        document.getElementById(zmMenuOpt).style.color = "black";
        document.getElementById(zmMenuOpt + "_text").style.color = "black";
    }

    zmMenuOpt = zID;

    document.getElementById(zmMenuOpt).style.backgroundColor = "rgb(49,105,198)";
    document.getElementById(zmMenuOpt).style.color = "white";
    document.getElementById(zmMenuOpt + "_text").style.color = "white";
}

function listviewKeyDown(e)
{
    var trMenu;

    var lmMenuOpt = parseInt(getField(zmMenuOpt, 2, "_"));

    if (e.keyCode == 13)
    {
        listviewDblClick()
    }

    if (e.keyCode == 38 || e.keyCode == 40)
    {
        if (e.keyCode == 38)
        {
            // Cursor Down
            lmMenuOpt--

        }


        if (e.keyCode == 40)
        {
            // Cursor Down
            lmMenuOpt++
        }

        try
        {
            trMenu = document.getElementById("Row_" + lmMenuOpt);

            if (trMenu != null)
                listviewClick("Row_" + lmMenuOpt)
        }
        catch (e)
        {
        }
    }
}


// ------------------------
// Drag-able column headers
// ------------------------

var bmColumnMouseDown = false;

function getOffsetTop(element)
{
    var parent = element.offsetParent;
    var offset = element.offsetTop;
    try
    {
        while (1 != 2)
        {
            offset += parent.offsetTop;
            parent = parent.offsetParent;
        }
    }
    catch (e)
    {
        return offset;
    }
}

function columnMouseDown(event)
{
    document.getElementById("divDrag").style.top = getOffsetTop(document.getElementById("t1"));
    document.getElementById("divDrag").style.height = document.getElementById("t1").clientHeight;
    document.getElementById("divDrag").style.left = event.x;
    document.getElementById("divDrag").style.display = "block";

    bmColumnMouseDown = true;
}

function columnMouseMove(event, myPage)
{
    if (bmColumnMouseDown == true)
    {
        document.getElementById("divDrag").style.left = event.x - 3;
    }
}

function columnMouseUp(event)
{
    if (bmColumnMouseDown == true)
    {
        if (event.clientX - 4 < 20)
            document.getElementById("tdColumn1").style.width = 20;
        else
        {
            if (event.clientX - 4 > document.getElementById("t1").clientWidth - 24)
                document.getElementById("tdColumn1").style.width = event.clientX - 24;
            else
                document.getElementById("tdColumn1").style.width = event.clientX - 4;
        }

        document.getElementById("divDrag").style.display = "none";
        bmColumnMouseDown = false;
    }
}

// ------------
// Image Adding
// ------------

// Requires:

// trImage - table row for hidden iframe
// imgTest - to display image
// cboImageFile on Form Main - select element

var bmImagePage = false;

function newImage()
{
    document.getElementById("iframeImage").src = "ImageUpload.aspx?ImagePath=" + document.getElementById("hidImagePath").value + "&UserID=" + document.getElementById("hidUserID").value + "&UploadKey=" + document.getElementById("hidUploadKey").value;

    setTimeout(resizeSet, 1);
}

function addImage(zImage, bExists, bSelect, bSelectLink)
{
    document.getElementById("trImage").style.display = "none";

    if (zImage != "")
    {
        if (bExists == true)
        {
            document.frmMain.cboImageFile.value = zImage;
            imageChange();
        }
        else
        {
            var cbo = document.frmMain.cboImageFile;
            var lpLength = cbo.length++;
            cbo.options[lpLength].value = zImage;
            cbo.options[lpLength].text = zImage;

            if (bmImagePage == false)
                cbo.selectedIndex = lpLength;
            else
            {
                cbo = document.frmMain.cboImageLink;
                lpLength = cbo.length++;
                cbo.options[lpLength].value = zImage;
                cbo.options[lpLength].text = zImage;
                
                if (bSelectLink)
                    document.frmMain.cboImageLink.selectedIndex = document.frmMain.cboImageLink.options.length - 1;
            }

            if (bSelect)
                document.frmMain.cboImageFile.selectedIndex = document.frmMain.cboImageFile.options.length - 1;

            imageChange();

            cbo = document.frmMain.cboSlideShowStart;
            if (cbo)
            {
                lpLength = cbo.length++;
                cbo.options[lpLength].value = zImage;
                cbo.options[lpLength].text = zImage;
            }

            cbo = document.frmMain.cboSlideShowEnd;

            if (cbo)
            {
                lpLength = cbo.length++;
                cbo.options[lpLength].value = zImage;
                cbo.options[lpLength].text = zImage;
            }

        }
    }
}


function imageChange()
{
    try
    {
        if (document.frmMain.cboImageFile.value == "")
        {
            document.getElementById("imgTest").style.display = "none";
        }
        else
        {
            document.getElementById("imgTest").src = document.getElementById("hidImagePath").value + document.frmMain.cboImageFile.value;
            document.getElementById("divImage").title = document.frmMain.txtAltText.value;
            document.getElementById("imgTest").style.display = "block";
        }
    }
    catch (e)
    {
        // Hyperlink page does not have imgTest
        adminHyperlinkChange(2)
    }

    if (bmLoaded == true)
        bmSaved = false;

    if (bmImagePage == true)
        adminImagePageChange()
}

// -----------------
// Saved data prompt
// -----------------

var bmSaved = true;
var bmLoaded = false;
var zmPageType = "";

function dataSaved()
{
    if (bmSaved == false)
    {
        if (confirm("Are you sure you want to discard changes to this " + zmPageType + "?"))
            return true;
        else
            return false;
    }
    else
        return true;

}

function dataChange()
{
    if (bmLoaded == true)
        bmSaved = false;
}

// ---------------
// Document Adding
// ---------------

// Requires:

// trDocument - table row for hidden iframe
// cboDocumentFile on Form Main - select element

function newDocument()
{
    document.getElementById("iframeDocument").src = "FileUpload.aspx?DocumentPath=" + document.getElementById("hidDocumentPath").value + "&UserID=" + document.getElementById("hidUserID").value + "&UploadKey=" + document.getElementById("hidUploadKey").value;
    setTimeout(resizeSet, 1);
}

function addDocument(zDocument, bExists)
{
    document.getElementById("trDocument").style.display = "none";

    if (zDocument != "")
    {
        if (bExists == true)
        {
            document.frmMain.cboDocumentFile.value = zDocument;
            adminHyperlinkChange();
            dataChange();
        }
        else
        {
            var cbo = document.frmMain.cboDocumentFile;
            var lpLength = cbo.length++;
            cbo.options[lpLength].value = zDocument;
            cbo.options[lpLength].text = zDocument;
            cbo.selectedIndex = lpLength;

            adminHyperlinkChange();
            dataChange();
        }
    }
}


function setSessionWarning()
{
    try
    {
        window.parent.opener.setSessionInterval();
    }
    catch (e)
    {
        try
        {
            window.opener.setSessionInterval();
        }
        catch (e)
        {
            try
            {
                window.parent.parent.setSessionInterval();
            }
            catch (e)
            {
                try
                {
                    window.parent.setSessionInterval();
                }
                catch (e)
                {
                }
            }
        }
    }

    try
    {
        setTimeout(resizeSet, 1);
    }
    catch (e)
    {
    }
}

function resizeSet()
{
    try
    {
        window.parent.resizeWindow();
    }
    catch (e)
    {
    }
}

function dateCheck(zID)
{
    var lpDay = parseInt(document.getElementById("cboDay_" + zID).value);
    var lpMonth = parseInt(document.getElementById("cboMonth_" + zID).value);
    var lpYear = parseInt(document.getElementById("cboYear_" + zID).value);

    var dat = new Date(lpMonth + "/" + lpDay + "/" + lpYear);

    if (parseInt(dat.getDate()) != lpDay || parseInt(dat.getMonth()) != (lpMonth - 1) || parseInt(dat.getFullYear()) != lpYear)
    {
        alert("Please select a valid date.");
        document.getElementById("cboDay_" + zID).focus();
        return false;
    }
    else
        return true;
}


function getSelectedDate(zID)
{
    var lpDay = parseInt(document.getElementById("cboDay_" + zID).value);
    var lpMonth = parseInt(document.getElementById("cboMonth_" + zID).value);
    var lpYear = parseInt(document.getElementById("cboYear_" + zID).value);

    var dat = new Date(lpMonth + "/" + lpDay + "/" + lpYear);

    if (parseInt(dat.getDate()) != lpDay || parseInt(dat.getMonth()) != (lpMonth - 1) || parseInt(dat.getFullYear()) != lpYear)
        return null;
    else
        return dat;

}

var iSI = 0;
var oSI = new Array();
var iSSInterval = 3000;
var oImgSlideShow;
var oSSInterval;
var bSSActive = false;
var oSlideShowTitle;
var sSlideShowDescription;
var oSlideShowDescriptionDiv;

function slideshow()
{
    iSI++;
    bSSActive = true;
    slideshowDisplay(iSI, true);
}

function slideshowDisplay(iIndex, bInternal)
{
    slideshowStop();

    if (iIndex >= oSI.length)
        iSI = 0;
    else
        iSI = iIndex;

    if (sSlideShowDescription == undefined)
        sSlideShowDescription = document.getElementById("hidSlideshowDescription").value;

    if (oImgSlideShow == undefined)
        oImgSlideShow = document.getElementById("imgslideshow");

    if (oSlideShowTitle == undefined)
        oSlideShowTitle = document.getElementById("divSlideTitle");

    if (oSlideShowDescriptionDiv == undefined)
        oSlideShowDescriptionDiv = document.getElementById("divSlideshowDescription");

    var oArray = sSlideShowDescription.split("\n");

    if (iSI < oArray.length)
        oSlideShowDescriptionDiv.innerHTML = oArray[iSI];
    else
        oSlideShowDescriptionDiv.innerHTML = "";

    oSlideShowTitle.innerHTML = "Image " + (iSI + 1) + " of " + oSI.length;

    oImgSlideShow.src = oSI[iSI];

    if (bInternal)
        oSSInterval = setTimeout("slideshow()", iSSInterval);
}

function slideshowStop()
{
    bSSActive = false;
    oSSInterval = window.clearInterval(oSSInterval);
}

function slideshowNext()
{
    iSI++;
    slideshowDisplay(iSI, bSSActive);
}

function slideshowBack()
{
    iSI--;
    if (iSI < 0)
        iSI = oSI.length - 1;

    slideshowDisplay(iSI, bSSActive);
}

// Admin change page - update iframe to keep copy and paste data on top page
function adminChangePage(lPageID, lMenuID)
{
    var oAdmin = document.getElementById("iframeAdmin");

    if (oAdmin)
        oAdmin.src = "AdminPage.asp?PageID=" + lPageID + "&MenuID=" + lMenuID;
    else
        window.location = "Default.asp?PageID=" + lPageID;
}

function validEmail(sEmail)
{
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    return reg.test(sEmail);
}



var omTimer;
var bmFullScreen = false;

function setSessionInterval()
{
    clearInterval(omTimer);
    omTimer = setInterval("alertSessionWarning()", ((parseInt(document.getElementById("hidSessionTimeout").value) - 5) * 60 * 1000));
}

function alertSessionWarning()
{
    clearInterval(timer);
    var dat = new Date();

    var zpTime;
    var zpBeforeTime;

    var zpMins = String(dat.getMinutes());
    var zpHours = String(dat.getHours());

    if (zpHours.length == 1)
        zpHours = "0" + zpHours;

    if (zpMins.length == 1)
        zpMins = "0" + zpMins;

    zpTime = zpHours + ":" + zpMins

    var lpBeforeMins = dat.getMinutes();
    var lpBeforeHours = dat.getHours();

    lpBeforeMins += 5;

    if (lpBeforeMins >= 60)
    {
        if (lpBeforeHours == 23)
            lpBeforeHours = 0;
        else
            lpBeforeHours++;

        lpBeforeMins -= 60;
    }

    if (String(lpBeforeHours).length == 1)
        lpBeforeHours = "0" + lpBeforeHours;

    if (String(lpBeforeMins).length == 1)
        lpBeforeMins = "0" + lpBeforeMins;

    zpBeforeTime = lpBeforeHours + ":" + lpBeforeMins;

    alert("5 minute session expiry warning at " + zpTime + ".\n\n" +
			"Your session will expire if you do not load or save data before " + zpBeforeTime + " and you will lose any changes made to the current page.");
}

function resizeWindow()
{
    if (window.frames.length > 0)
    {
        var zHeight;

        // Add 30 in case of horizontal scroll bar

        if (window.frames[0].document.body.clientHeight < 300)
            zHeight = "330px"
        else
            zHeight = (window.frames[0].document.body.clientHeight + 30) + "px";

        document.getElementById("iframeAdmin").height = zHeight;
        var oDiv = document.getElementById("divContent");
        if (oDiv) 
        {
            oDiv.style.height = zHeight;
            oDiv.style.minHeight = zHeight;
        } 
    }
}

function defaultPageLoad()
{
    var oHidUserID = document.getElementById("hidUserID");
    if (oHidUserID != undefined && !isNaN(parseInt(oHidUserID.value)))
    {
        window.onresize = resizeWindow;
        resizeWindow();
    }

    // Set focus to first control on a form page
    var oFirstControl = document.getElementById("hidFirstControl");
    if (oFirstControl != undefined)
        document.frmMain[oFirstControl.value].focus();

    imageRotatesStart();
}

function searchPress(e)
{
    if (e.keyCode == 13)
    {
        search()
    }
}

function search()
{
    window.location = "Default.asp?bSearch=true&SearchText=" + document.getElementById("txtSearch").value;
}

function createCookie(name, value, days)
{
    var sExpires;

    if (days)
    {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        sExpires = "; expires=" + date.toGMTString();
    }
    else
        sExpires = "";

    document.cookie = name + "=" + value + sExpires + "; path=/";
}


/* Image rotation */

var omImageRotates = null;

function addImageRotate(sGalleryID, iWaitSeconds)
{
    if (omImageRotates == null)
        omImageRotates = new Object();

    omImageRotates[sGalleryID] = new imageRotate(sGalleryID, iWaitSeconds);
}

function imageRotateCrossfade(sGalleryID, iOpacity)
{
    omImageRotates[sGalleryID].crossfade(iOpacity);
}

function imageRotatesStart()
{
    if (omImageRotates != null)
    {
        for (var sKey in omImageRotates)
        {
            omImageRotates[sKey].startRotate();
        }
    }
}

function imageRotate(sGalleryID, iWaitSeconds)
{
    this.ID = sGalleryID;
    this.Gallery = document.getElementById(sGalleryID);
    this.Images = new Array();
    this.CurrentImage;
    this.PreviousImage;
    this.Wait = iWaitSeconds * 1000;
}

imageRotate.prototype.startRotate = function()
{
    var sAlign = this.Gallery.parentNode.style.textAlign;
    this.Gallery.style.height = this.Gallery.clientHeight + "px";
    this.Gallery.style.width = this.Gallery.clientWidth + "px";
    this.Gallery.style.minHeight = this.Gallery.clientHeight + "px";
    this.Link = (this.Gallery.parentNode.tagName.toLowerCase() == "a");

    var oNode = this.Gallery.firstChild;

    while (oNode)
    {
        if (oNode.nodeType == 1)
        {
            this.Images.push(oNode);
        }
        oNode = oNode.nextSibling;
    }

    if (sAlign == "center")
        var iLeft = this.Images[0].offsetLeft;

    for (i = 0; i < this.Images.length; i++)
    {
        /* loop through all these child nodes and set up their styles */
        this.Images[i].style.position = "absolute";
        this.Images[i].style.display = "block";
        this.Images[i].style.zIndex = 0;
        
        if (sAlign == "right")
            this.Images[i].style.right = "0px";

        if (sAlign == "center")
            this.Images[i].style.left = iLeft + "px";

        this.fader(i, 0);
    }

    this.CurrentImage = 0;
    this.PreviousImage = this.Images.length - 1;

    this.CurrentImage = 1;
    this.PreviousImage = 0;

    this.Opacity = 100;
    this.fader(0, 100);
    this.Gallery.title = this.Images[0].alt;
    //this.Images[0].title = this.Images[0].alt;

    if (this.Link)
        this.Gallery.parentNode.title = this.Images[0].alt;

    if (this.Images.length > 1)
        window.setTimeout("imageRotateCrossfade('" + this.ID + "',0)", this.Wait);
}

imageRotate.prototype.fader = function(iImage, iOpacity)
{
    var oImage = this.Images[iImage];
    if (oImage.style)
    {
        if (oImage.style.MozOpacity != null)
        {
            /* Mozilla's pre-CSS3 proprietary rule */
            oImage.style.MozOpacity = (iOpacity / 100);
        }
        else if (oImage.style.opacity != null)
        {
            /* CSS3 compatible */
            oImage.style.opacity = (iOpacity / 100);
        }
        else if (oImage.style.filter != null)
        {
            /* IE's proprietary filter */
            oImage.style.filter = "alpha(opacity=" + iOpacity + ")";
        }
    }
}

imageRotate.prototype.crossfade = function(iOpacity)
{
    if (iOpacity < 100)
    {
        /* current image not faded up fully yet so increase its opacity */
        this.fader(this.CurrentImage, iOpacity);
        iOpacity += 10;
        window.setTimeout("imageRotateCrossfade('" + this.ID + "'," + iOpacity + ")", 30);
    }
    else
    {
        /* make the previous image - which is now covered by the current one fully - transparent */
        this.fader(this.PreviousImage, 0);
        /* current image is now previous image, as we advance in the list of images */
        this.PreviousImage = this.CurrentImage;
        this.CurrentImage += 1;
        if (this.CurrentImage >= this.Images.length)
        {
            /* start over from first image if we cycled through all images in the list */
            this.CurrentImage = 0;
        }
        /* make sure the current image is on top of the previous one */
        this.Images[this.PreviousImage].style.zIndex = 0;
        this.Images[this.CurrentImage].style.zIndex = 100;

        this.Gallery.title = this.Images[this.PreviousImage].alt;

        if (this.Link)
            this.Gallery.parentNode.title = this.Images[this.PreviousImage].alt;

        /* and start the crossfade after a pause */
        iOpacity = 0;
        window.setTimeout("imageRotateCrossfade('" + this.ID + "'," + iOpacity + ")", this.Wait);
    }
}


