﻿// JScript File


// This is the function that each control that can lose the page is set to call first
//   If the hidden text value is 'Y', then we can leave the page without a problem
//   If the hidden text value is anything else, we prompt before leaving
function promptBeforeExiting (oldLink) 
{
    varSaved = document.getElementById('ctl00_ContentPlaceHolderMain_txtChangesSaved_Hidden');
    if (varSaved.value == 'Y')
    {
        window.location = oldLink;
    }
    else
    {
        varMessage = "Leave page without saving?";
        if (location.href.indexOf("Page_Emails.aspx") != -1)
        {
            varMessage = "Leave page without sending email?";
        }
        if (location.href.indexOf("Page_PhotoOrder_PlaceOrder.aspx") != -1)
        {
            varMessage ="Leave page and lose your photo order?";
        }
        if (location.href.indexOf("Page_PhotoOrder_RequestForOriginals.aspx") != -1)
        {
            varMessage ="Leave page without uploading requested photos?";
        }
        if (location.href.indexOf("Page_SchoolAdministrator_CreateClassroomInvitations.aspx") != -1)
        {
            varMessage = "Leave page without sending invitations?";
        }
        if (location.href.indexOf("Page_CreateClassroom.aspx") != -1)
        {
            varMessage = "Leave page without finishing creating your classroom?";
        }
        if (confirm(varMessage)) 
        {
           // Go to Page_DeleteDraftFile.aspx first if we want to enable saving drafts
           // window.location = "Page_DeleteDraftFile.aspx?URL=" + oldLink;
            window.location = oldLink;
        }
        else
        {
            // reset hourglass cursor
            document.body.style.cursor = 'default';
        }
    }
}


// Finds every control with an href tag and changes the link to prompt before exiting
function switchLinks() 
{
    var oldLink="";
    for (var i=0; i < document.links.length; i++) 
    {
        oldLink = document.links[i].href;    
        // Don't change link if it points to an anchor on the page    
        if (oldLink.indexOf('promptBeforeExiting') == -1 && oldLink.indexOf('#') == -1)
        {
            // Don't warn in certain cases
            var okToLeave=false;
            
            // If we're saving a draft
            if (document.links[i].id == "ctl00_ContentPlaceHolderMain_lnkSaveDraft") okToLeave=true; 
            
            // If we returning to adjust photo settings
            if (document.links[i].id == "ctl00_ContentPlaceHolderMain_lnkBackToSetupCart") okToLeave=true; 
            
            // Warn if it's not ok to leave
            if (!okToLeave)
            {
                document.links[i].href = "javascript: promptBeforeExiting('"+ oldLink + "')";
            }
        }
    }
        
    varSaved = document.getElementById('ctl00_ContentPlaceHolderMain_txtChangesSaved_Hidden');
    varSaved.value = 'N';
}

// This restores the original link without the prompt string that was inserted
function restoreLinks() 
{
    var oldLink="";
    for (var i=0; i < document.links.length; i++) 
    {
        oldLink = document.links[i].href;
        promptStart = oldLink.indexOf('promptBeforeExiting');
        if (promptStart != -1)
        {            
            newLink = oldLink.substr(promptStart+21,oldLink.length-(promptStart+23));
            document.links[i].href = newLink;
        }
    }
}

// This is called after a postback
// If the hidden text is 'Reset', it switches the links to prompt and sets the hidden value to 'N'
function resetLinksOnPostback()
{
    varSaved = document.getElementById('ctl00_ContentPlaceHolderMain_txtChangesSaved_Hidden');
    if (varSaved.value == 'Reset')
    {
        switchLinks();
        varSaved.value='N';
    }
}

// This is specific to the manage photo pages
function okToChangePhotoDropdown()
{
    varSaved = document.getElementById('ctl00_ContentPlaceHolderMain_txtChangesSaved_Hidden');
    if (varSaved.value == 'N')
    {        
        if (confirm("Leave page without saving?"))
        {
            varSaved.value='Y';
        }
        else
        {
            txtHiddenOption = document.getElementById('ctl00_ContentPlaceHolderMain_txtHiddenOption');
            txtHiddenFolder = document.getElementById('ctl00_ContentPlaceHolderMain_txtHiddenFolder');
            ddlOptions=document.getElementById('ctl00_ContentPlaceHolderMain_ddlOptions');
            ddlPictureFolder=document.getElementById('ctl00_ContentPlaceHolderMain_ddlPictureFolder');
            ddlOptions.options.selectedIndex=txtHiddenOption.value
            ddlPictureFolder.options.selectedIndex=txtHiddenFolder.value
            return false; 
        }
    }   
}