﻿var cacheDictionary;
var specificSeparatorStart = '<??{0}??>';
var specificSeparatorEnd = '</??{0}??>';
var selectedTabID;

// assure that the dictionary exists
function AssureDictionary()
{
    if (cacheDictionary == null)
        cacheDictionary = new CacheDictionary();
        
    return cacheDictionary;    
}

// add middle part to dictionary
function AddMiddlePartToDictionary(id,containerControlID)
{  
             
    AssureDictionary();
    alert(id);
    var container = document.getElementById(containerControlID);
    if (container == null) return;
        
    if (cacheDictionary.Lookup(id) == null)        
        cacheDictionary.Add(id,container.innerHTML);    
}

// add right part to dictionary
function AddRightPartToDictionary(id, containerControlID, tabID) {                     
return
    AssureDictionary();
    
    var container = document.getElementById(containerControlID);
    if (container == null) return;
        
    var tabIDStart = specificSeparatorStart.replace('{0}',tabID);
    var tabIDEnd = specificSeparatorEnd.replace('{0}',tabID);    
        
    if (cacheDictionary.Lookup(id) == null)        
        cacheDictionary.Add(id,tabIDStart + container.innerHTML + tabIDEnd); 
    else
    {                
        if (!IsSubstringContained(cacheDictionary[id],tabIDStart))
            cacheDictionary[id] += tabIDStart + container.innerHTML + tabIDEnd;    
    }
}

// determine that substring is contained in a string
function IsSubstringContained(input,searchString)
{                    
    return input.indexOf(searchString,0) >= 0;
}


// checks that the middle part is in cache, if so take it from cache and put it to the element
function DetermineMiddlePartFromCache(id,containerControlID)
{            
    AssureDictionary();     
    alert(id);       
    var isInCache = (cacheDictionary.Lookup(id) != null);    
    if (isInCache)            
        document.getElementById(containerControlID).innerHTML = cacheDictionary[id];    
                    
    return isInCache;
}

// checks that the middle part is in cache, if so take it from cache and put it to the element
function DetermineRightPartFromCache(id,containerControlID)
{                
    AssureDictionary();            
    var isInCache = (cacheDictionary.Lookup(id) != null);    
    if (isInCache) 
    {
        var tabIDStart = specificSeparatorStart.replace('{0}',selectedTabID);
        var tabIDEnd = specificSeparatorEnd.replace('{0}',selectedTabID);
        
        
        if (!IsSubstringContained(cacheDictionary[id],tabIDStart))
            return false;
                   
        document.getElementById(containerControlID).innerHTML = GetFragmentHtmlFromRightPart(cacheDictionary[id],tabIDStart,tabIDEnd);    
    }
                    
    return isInCache;
}

// checks that the middle part is in cache for exteriors, if so take it from cache and put it to the element
function DetermineRightPartFromCacheForExterior(id,containerControlID,containerControlIDInterior)
{       
    var newID = id;             
    var radioButtonSelected = GetSelectedRadioButton(containerControlIDInterior);
    if (radioButtonSelected != null)
    {
        var selectedButtonID;
        if (radioButtonSelected.attributes['interiorID'] != null)
            selectedButtonID = radioButtonSelected.attributes['interiorID'].nodeValue;
        else
            selectedButtonID = radioButtonSelected.parentNode.attributes['interiorID'].nodeValue;
            
        newID = newID.replace('{0}',selectedButtonID);
    }
        
    return DetermineRightPartFromCache(newID,containerControlID);   
}

// checks that the middle part is in cache for exteriors, if so take it from cache and put it to the element
function DetermineRightPartFromCacheForInterior(id,containerControlID,containerControlIDExterior)
{                    
    var newID = id;             
    var radioButtonSelected = GetSelectedRadioButton(containerControlIDExterior);
    if (radioButtonSelected != null)
    {        
        var selectedButtonID;
        if (radioButtonSelected.attributes['exteriorID'] != null)
            selectedButtonID = radioButtonSelected.attributes['exteriorID'].nodeValue;
        else
            selectedButtonID = radioButtonSelected.parentNode.attributes['exteriorID'].nodeValue;
            
        newID = newID.replace('{0}',selectedButtonID);
    }
    
    return DetermineRightPartFromCache(newID,containerControlID);   
}


// get fragment html from right part
function GetFragmentHtmlFromRightPart(input,byStringStart,byStringEnd)
{             
    var positionStart = input.indexOf(byStringStart);
    var positionEnd = input.lastIndexOf(byStringEnd) + byStringEnd.length;
    
    if (positionStart == -1)
        return null;

    var subString = input.toString().substring(positionStart, positionEnd);  
    alert(subString.replace(byStringStart,'').replace(byStringEnd,''); )
    return subString.replace(byStringStart,'').replace(byStringEnd,'');            
}

// set the right tab selected tab
function SetRightPartSelectedTab(tabID)
{            
    selectedTabID = tabID;
}

// get the selected radio button
function GetSelectedRadioButton(containerClientID)
{
    var container = document.getElementById(containerClientID);
    if (container == null) return null;
    
    var items = container.getElementsByTagName('input');
    for (var i = 0;i < items.length;i++)
    {
        if (items[i].checked == true)
            return items[i];
    }
    
    return null;
}


