﻿// Javascript for LoadSession user control.

var SAVESESSION_SEPARATOR = 'SAVESESSION_&';
var SAVESESSION_SESSION_FILE_PATH_PARAM = 'SessionFilePath';

// Defining constant strings control IDs
var SAVESESSION_MODAL_POPUP_ID = "TitleBanner1_SaveSession_ModalPopupExtender";
var SAVESESSION_HYPER_LINK_ID = "TitleBanner1_SaveSession1_hlSaveSession";

// Defines the amount of time (in milliseconds), that elapses between the user
// receiving the confirmation and the popup window automatically closing down.
//var SAVESESSION_DELAY = 500;

var SAVESESSION_CURRENT_SITE_NAME = '';
var SAVESESSION_SESSION_FILE_PATH = '';

function SAVESESSION_Close()
{      
    // CLOSE MODAL POPUP WINDOW
    var modalPopupBehavior = $find(SAVESESSION_MODAL_POPUP_ID);
    if(modalPopupBehavior != null)
    {
        modalPopupBehavior.hide();
        
        window.location = 'Map.aspx?site='+SAVESESSION_CURRENT_SITE_NAME +
                            '&' + SAVESESSION_SESSION_FILE_PATH_PARAM + '=' + SAVESESSION_SESSION_FILE_PATH;
    }  
}

function SAVESESSION_Show()
{      
    // OPEN MODAL POPUP WINDOW
    var modalPopupBehavior = $find(SAVESESSION_MODAL_POPUP_ID);
    if(modalPopupBehavior != null)
    {
        modalPopupBehavior.show();
        
        // Set focus on main panel. 
        var hyperLink = document.getElementById(SAVESESSION_HYPER_LINK_ID);
        hyperLink.focus();
        hyperLink.href = "";
        
        // Call server to save current session.
        SAVESESSION_callServer('SAVE_SESSION');
    }    
    
}

// CALLBACK METHODS *****************************************************

function SAVESESSION_callServer(message)
{   
    var action = message;
    
    switch (action) {            
        case 'SAVE_SESSION':
            
            //Construct callback message.
            var callbackMessage =  "action="+action;
            
            // INVOKING CALLBACK
            SAVESESSION_DoCallBack(callbackMessage, "SaveSession"); 
                        
            break
        default:
            break
    }//end Switch  
    
}


function SAVESESSION_processMyResult (returnMessage, context)
{
    var splittedReturnMessage = returnMessage.split(SAVESESSION_SEPARATOR);
    var action = splittedReturnMessage[0];
    
    switch (action) {
        case 'SAVE_SESSION':
        
            var link = splittedReturnMessage[1];
            var currentSiteName = splittedReturnMessage[2];
            var sessionFilePath = splittedReturnMessage[3];
            
            // Set hyperlink's href. 
            var hyperLink = document.getElementById(SAVESESSION_HYPER_LINK_ID);
            hyperLink.href = link;
            
            // Set current site name.
            SAVESESSION_CURRENT_SITE_NAME = currentSiteName;
            
            // Set the file path of the saved session.
            SAVESESSION_SESSION_FILE_PATH = sessionFilePath;
                                   
            break           
        default:
            break
    }//end Switch
}

function SAVESESSION_postMyError(returnmessage, context)
{
    alert("Callback Error: " + returnmessage + ", " + context);
}

// ****************************************************************

// ****** KEY EVENTS *******************************
// **
// **
function SAVESESSION_stopEvent(e) {
	if(!e) var e = window.event;
	
	//e.cancelBubble is supported by IE - this will kill the bubbling process.
	e.cancelBubble = true;
	e.returnValue = false;

	//e.stopPropagation works only in Firefox.
	if (e.stopPropagation) {
		e.stopPropagation();
		e.preventDefault();
	}
	return false;
}

function SAVESESSION_OnKeyDown(e)
{
  if (e.keyCode == 27 && NBDCTOCDF_ApplyButtonEnabled == true) 
  {
    SAVESESSION_Close();
    SAVESESSION_stopEvent(e);
  } 
}


// ************************************************
