﻿// JScript File for dictionary pages.

// -----------------------------------------------------------------------------
// |
// |	Copyright (c) 2006
// |	by James Eby ( james.eby@insightbb.com )
// |
// |	Permission is hereby granted, free of charge, to any person obtaining a copy of
// |	this software and associated documentation files (the "Software"), to deal
// |	in the Software without restriction, including without limitation
// |	the rights to use, copy, modify, merge, publish, distribute, sublicense,
// |	and/or sell copies of the Software, and to permit persons to whom the
// |	Software is furnished to do so, subject to the following conditions:
// |
// |	The above copyright notice and this permission notice shall be included in
// |	all copies or substantial portions of the Software.
// |
// |	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
// |	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
// |	OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
// |	IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
// |	CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT 
// |	OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
// |	OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// |
// |	VERSION	BY/DATE			DESCRIPTION
// |	-------	----------------	--------------------------------------
// |	JLE-000	James Eby		Original.
// |		    6/18/2006
// |
// -----------------------------------------------------------------------------

// Full image information arrays.
var bbImage = new Array(0);
var bbName = new Array(0);
var bbMail = new Array(0);

var fullBlackBeltItem = new Array("image", "name", "mail");

// Other required variables.
var currentBB = -1;

// -----------------------------------------------------------------------------
// addBlackBelt
//
//  Adds a blackbelt definiton to the array.
//
//  Parms: Array in the order given for fullBlackBeltItem with all parameters given.
//
//  Returns the index of the image information in the array or -1 on failure.
//
// -----------------------------------------------------------------------------
function addBlackBelt()
{
var inc=0;
var idx = bbName.length;

    if (arguments.length != fullBlackBeltItem.length) 
    {
        alert( "Javascript function \"addBlackBelt\" had too many or too few arguments (" + arguments.length + ").");
        return -1;
    }

    bbImage[idx] = "";
    bbName[idx] = "";
    bbMail[idx] = "";

    for (inc=0; inc < arguments.length; inc++) 
    {
        if (arguments[inc] != null) 
        {
            switch (fullBlackBeltItem[inc])
            {
            case "image":
                bbImage[idx] = arguments[inc]; 
                break;
            case "name":
                bbName[idx]= arguments[inc]; 
                break;
            case "mail":
                bbMail[idx] = arguments[inc];
                break;             
            }
        }
    }
    return(idx);
}
  
// -----------------------------------------------------------------------------
// getTermValueByAttrName
//
//  Returns the specified current term information given by the passed
//  attribute name, if idx is also passed it returns the information of the
//  image at the given array index.
//
//  Parms:  attrName = name of the attribute to retrieve.
//          idx (optional) = Index if retrieving the currently set
//          image information is not the objective.
//
// -----------------------------------------------------------------------------  
function getTermValueByAttrName(attrName)
{
var idx = currentBB;

    if (arguments.length > 1)
        idx = arguments[1];
    
    if(idx == -1)
        return("");
        
    switch (new String(attrName).toLowerCase()) 
    {
    case "image":        
        return bbImage[idx]; 
    case "name": 
        return bbName[idx]; 
    case "mail":       
        return bbMail[idx]; 
    default:
        alert( "Javascript function \"getTermValueByAttrName\" was passed an invalid attribute name (" + attrName + ").");
        break;
    }
}

/* Information Retrieval Functions */
function getBlackBeltCount()
{
    return(bbName.length)
}

// -----------------------------------------------------------------------------
// writeBlackBeltPage
//
//  Writes out the HTML for the dictionary page using the index passed in the page 
//  URL or index 0. Run this function after you have defined all the terms that 
//  will be added to the dictionary.
// -----------------------------------------------------------------------------
function writeBlackBeltPage() 
{
var idx = currentImage = privGetURLIdx();

    document.getElementById("bbName").innerHTML = bbName[idx];
    document.getElementById("bbImage").innerHTML = "<img src=\"" + bbImage[idx] + "\" />";
    document.getElementById("bbMail").innerHTML = bbMail[idx];
}

// -----------------------------------------------------------------------------
// privGetURLIdx
//
//  Looks at URL parameters, accepts either "bb=???", where ??? is 
//  index of the black belt.
// -----------------------------------------------------------------------------
function privGetURLIdx() 
{
var tempString = new String(window.location.search).toLowerCase();
var idx = 0;
  
    if (tempString.substr(1,2)=="bb")
    {
        // strip "term=" from URL
        tempString = new String( tempString.substr( 4, tempString.length-4 ));
        if (tempString.length < 1)
        {
            tempString= "0";
        }
        else if (isNaN(tempString))
        {
            alert( "Javascript function \"privGetURLIdx\" was passed an invalid term index " + tempString + ".");
        }
        else
        {
            idx = parseInt(tempString);
        }
        if (idx >= bbName.length)
        {
            idx = (bbName.length - 1);
        }
    }
    return(idx);
}