﻿// Javascript for LoadSession user control.

// Defining constant strings control IDs
var LOADSESSION_MODAL_POPUP_ID = "TitleBanner1_LoadSession_ModalPopupExtender";
var LOADSESSION_FILE_INPUT_ID = "TitleBanner1_LoadSession1_filMyFile";
var LOADSESSION_FILE_REQUIRED_ID = "TitleBanner1_LoadSession1_lbRequired";
var LOADSESSION_FILE_VALID_ID = "TitleBanner1_LoadSession1_lbValid";
var LOADSESSION_USER_INFO_LABEL_ID = "TitleBanner1_LoadSession1_lbMessage";
var LOADSESSION_INDICATOR_IMAGE_ID = "TitleBanner1_LoadSession1_activity_image";
var LOADSESSION_BUTTON_USER_FEEDBACK = "Browse for desired \".ssn\" file!";
var LOADSESSION_LOADING_MESSAGE = "Loading selected map view...";

// Defines the amount of time (in milliseconds), that elapses between the user
// receiving the confirmation and the popup window automatically closing down.
var LOADSESSION_DELAY = 500;

function LOADSESSION_Close()
{      
    // CLOSE MODAL POPUP WINDOW
    var modalPopupBehavior = $find(LOADSESSION_MODAL_POPUP_ID);
    if(modalPopupBehavior != null)
    {
        modalPopupBehavior.hide();
    }  
}

function LOADSESSION_doPostBack(eventTarget, eventArgument) {
   if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
       theForm.__EVENTTARGET.value = eventTarget;
       theForm.__EVENTARGUMENT.value = eventArgument;
       theForm.submit();
   }
}

function LOADSESSION_Postback()
{
    LOADSESSION_validateTextbox()
}

// Validates textbox and redirects to search page.
function LOADSESSION_validateTextbox()
{   
    // Get fileUpload control.
    var fileUpload = document.getElementById(LOADSESSION_FILE_INPUT_ID);
    var lbRequired = document.getElementById(LOADSESSION_FILE_REQUIRED_ID);
    var lbValid = document.getElementById(LOADSESSION_FILE_VALID_ID);
    var indicatorImage = document.getElementById(LOADSESSION_INDICATOR_IMAGE_ID);
    var userInfoLabel = document.getElementById(LOADSESSION_USER_INFO_LABEL_ID);
    
    // Reset controls 
    lbRequired.style.display = 'none';
    lbValid.style.display = 'none';
    
    if(fileUpload != null)
    {
        // Get file path.
        var filePath = fileUpload.value;
        
        // If file path is not empty.
        if(filePath != '')
        {  
            // Check that file path is not all blank spaces.
            if(STRING_validateNotEmpty(filePath) != false)
            {
                // Remove eventual leading/trailing spaces.
                filePath = STRING_trimAll(filePath);
                
                // Check that file has ".ssn" extension.
                if(filePath.search('.ssn') != -1)
                {
                    // Notify user.
                    indicatorImage.style.display = "inline";
                    userInfoLabel.innerHTML  = LOADSESSION_LOADING_MESSAGE; 
                    userInfoLabel.style.color = "red";
                    
                    LOADSESSION_Close();
                    
                    window.setTimeout("CONFIRMATIONDIALOG_ShowDialog('LOADSESSION');",LOADSESSION_DELAY);
                    
                    // Postback.
                    window.setTimeout("LOADSESSION_doPostBack('__Page', '');",LOADSESSION_DELAY+100);
                }
                else
                {
                    // Display required label.
                    lbValid.style.display = 'inline';
                }
            }
            else
            {
                // Display required label.
                lbRequired.style.display = 'inline';
            }
            
        }
        else
        {   
            // Display required label.
            lbRequired.style.display = 'inline';
        }
    }
}


function LOADSESSION_Show()
{      
    // OPEN MODAL POPUP WINDOW
    var modalPopupBehavior = $find(LOADSESSION_MODAL_POPUP_ID);
    if(modalPopupBehavior != null)
    {
        modalPopupBehavior.show();
        
        var lbValid = document.getElementById(LOADSESSION_FILE_VALID_ID);
        lbValid.style.display = 'none';
        var lbRequired = document.getElementById(LOADSESSION_FILE_REQUIRED_ID);
        lbRequired.style.display = 'none';
        
        // Set focus on main panel.
        var fileUpload = document.getElementById(LOADSESSION_FILE_INPUT_ID);
        fileUpload.focus();
        fileUpload.value = "";
    }    
    
}

// ****** KEY EVENTS *******************************
// **
// **
function LOADSESSION_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 LOADSESSION_OnKeyDown(e)
{
  if (e.keyCode == 27 && NBDCTOCDF_ApplyButtonEnabled == true) 
  {
    LOADSESSION_Close();
    LOADSESSION_stopEvent(e);
  } 
}


// ************************************************
