﻿
var isHistoryExecuted = false;


// --------------------------------------------------------------
//  
//   * Public function *
// --------------------------------------------------------------
function AddToHistory(location, data)
{
    //adds a new ref in the history object  (back-button in the webbrowser)        
    dhtmlHistory.add(location, data);
}




// --------------------------------------------------------------
//  
//   * Public function *
// --------------------------------------------------------------
function DoHistoryUpdate(newLocation, historyData)
{
    // if the search is open...
    try {
        SearchCriteriaCancelClick();

        CancelSearchPanel();

        HideFreeTextSearch();
    }
    catch (err)
    { }

    //-----------------------------------

    //var historyMsg = (typeof historyData == "object" && historyData != null ? historyStorage.toJSON(historyData) : historyData ); 
    
    if (newLocation.startsWith('hitlist'))
        DoHistoryOnHitlist(newLocation.replace(/hitlist\:/, ""), historyData);
        
    else if (newLocation.startsWith('objectcard'))
        DoHistoryOnObjectCard(newLocation.replace(/objectcard\:/, ""), historyData);
        
    else if (newLocation.startsWith('dashboard'))
        DoHistoryOnDashboard();
    
    else if (newLocation == 'freetextsearch')
        DoHistoryOnFreeTextSearch(historyData);
        
    else if (newLocation == 'search')
        DoHistoryOnSearch();
        
    else if (newLocation.startsWith('inbox'))
        DoHistoryOnProjectInbox(newLocation.replace(/inbox\:/, ""));
}


// --------------------------------------------------------------
//   This function will handle the history of the hitlist.
//
//   * Private function *
// --------------------------------------------------------------
function DoHistoryOnHitlist(newLocation, historyData)
{
    if (newLocation.startsWith('page')) //changes the page on the hitlist
        ChangePageOnHitList(newLocation.replace(/page\:/, ""));
        
    else if (newLocation.startsWith('sort')) //changes the sort on the hitlist
        SortHitList();
}


// --------------------------------------------------------------
//   
//   * Private function *
// --------------------------------------------------------------
function DoHistoryOnObjectCard(newLocation, historyData)
{
    if (newLocation.startsWith('project')) //changes to the project tab and loads the last project
        LoadProject(newLocation.replace(/project\:/, "", false));
    else if (newLocation.startsWith('company')) //changes to the company tab and loads the last company
        LoadCompany(newLocation.replace(/company\:/, "", false));
    else if (newLocation.startsWith('contact')) //changes to the contact tab and loads the last contact
        LoadContact(newLocation.replace(/contact\:/, "", false));
        
    isHistoryExecuted = true;
}


// --------------------------------------------------------------
//   
//   * Private function *
// --------------------------------------------------------------
function DoHistoryOnDashboard()
{
    LoadDashboard();
}


// --------------------------------------------------------------
//   
//   * Private function *
// --------------------------------------------------------------
function DoHistoryOnFreeTextSearch(historyData)
{
    DoFreeTextSearch(historyData);
}


// --------------------------------------------------------------
//   
//   * Private function *
// --------------------------------------------------------------
function DoHistoryOnSearch()
{
    LoadSearchPanel();
}


// --------------------------------------------------------------
//   
//   * Private function *
// --------------------------------------------------------------
function DoHistoryOnProjectInbox(locationParameter)
{
    LoadLargeInboxList(locationParameter.toUpperCase());
}


