﻿//parses the querystinrg and returns the value for the key if it exists
function queryString(qstringElement) 
{
    var urlQueryString = window.location.search.substring(1);
    var queryPairArray = urlQueryString.split("&");
    for (i=0;i<queryPairArray.length;i++) 
    {
        var itemPairArray = queryPairArray[i].split("=");
        if (itemPairArray[0].toLowerCase() == qstringElement.toLowerCase()) 
        {
            return itemPairArray[1];
        }
    }
}

//true if in demo mode (demo=true), otherwise false
function IsDemoMode()
{
    var isDemoMode = false;
    
    var isDemo = queryString("demo");
    if(isDemo)
    {
        if(isDemo.toLowerCase() == "true")
        {
            isDemoMode = true;
        }
    }
    
    return isDemoMode;
}

//returns the formatted url for adding demo mode to url
function getFormattedURLForDemo(urlToFormat)
{
    if(urlToFormat != "#")
    {        
        if(IsDemoMode())
        {
            var textToAppend = "demo=true";
            var urlArray = urlToFormat.split("?");
            if(urlArray.length > 1)
                textToAppend = "&" + textToAppend;
            else
                textToAppend = "?" + textToAppend;
                
            return urlToFormat + textToAppend;
        }
    }
        
    return urlToFormat;
}

//called by anchor tags in order to append demo mode querystring items if needed
function getFormattedURLForDemo_Anchor(whichElement)
{
    //get the href element
    if(whichElement)
        whichElement.href = getFormattedURLForDemo(whichElement.href);
}

//adjust the elements visibility, whether it shoudl show in demo mode or not
//true = shows if in demo mode, otherwise hides 
//false = shows if NOT in demo mode, otherwise hides
function SetModeVisibility(whichElement, showForDemoMode)
{
    var isDemo = IsDemoMode();
    var showIt = showForDemoMode;
    
        
    if(isDemo && showForDemoMode)
        showIt = true;
    else if(isDemo && !showForDemoMode)
        showIt = false;
    else if(!isDemo && showForDemoMode)
        showIt = false;
    else if(!isDemo && !showForDemoMode)
        showIt = true;
     
     if(showIt)
        showElement(MM_findObj(whichElement));
    else
        hideElement(MM_findObj(whichElement));
}
