// dynamic.js

// Change the state of a group of items
function ChangeState(group, state, cookie, def){

    // Get the parameters from the URL
    val = GetURLParam(group);
    if (val.length){
        state = val;
        cookie = 1;
    }

    Change("state", group, state, cookie, def);

}

// Select an Item from a group
function SelectItem(group, item, cookie, def){

    // Get the parameters from the URL
    val = GetURLParam(group);
    if (val.length){
        item = val;
        cookie = 1;
    }

    Change("select", group, item, cookie, def);

}

// Make an item visible from a group
function ShowItem(group, item, cookie, def){

    // Get the parameters from the URL
    val = GetURLParam(group);
    if (val.length){
        item = val;
        cookie = 1;
    }

    Change("show", group, item, cookie, def);
    
//    alert("ShowItem: "+item);

}

function SelectComboItem(elementID, group, item, cookie){

    // Get the parameters from the URL
    val = GetURLParam(group);
    if (val.length){
        item = val;
        cookie = 1;
    }

    item = useCookies(cookie, group, item);
    
    var combo = document.getElementById(elementID);
    if (combo != null){
        for (i = 0; i < combo.options.length; i++){
            var option = combo.options[i];
            if (option.value == item)
                combo.selectedIndex = i;
        }
    }
}

function GetBestValue(group, items, def){

    // compile the groupclass name root
    var classGroupRoot = "dyn_"+group+"_";
    classGroupRoot.toLowerCase;
    
    // Get all span elements
    var list = document.all;
    var itemArray = items.split(",");

    for (i = 0; i < itemArray.length; i++){
        var item = itemArray[i];
        if (item.indexOf(";") != -1){
            item = item.substr(0, item.indexOf(";"));
        }
            
        // Loop through all span elements
        for (var j = 0; j < list.length; j++){
            var obj = list[j];
            
            // Ignore those not inthis group
            var className = classGroupRoot+item;
            if (className == obj.className){
//                alert("Found: "+item);
                return item;
            }
        }
    }
//    alert("Default: "+def);
    return def;
}

function Change(action, group, item, cookie, def){
    
    item = useCookies(cookie, group, item);
    
    // compile the groupclass name root
    var classGroupRoot = "dyn_"+group+"_";
    classGroupRoot.toLowerCase;
    
    // Get all span elements
    var list = document.all;

    var found = 0;
    // Loop through all span elements, changing their style if necessary
    for (var i = 0; i < list.length; i++){
        var obj = list[i];
        
        // Ignore those not inthis group
        if (classGroupRoot == obj.className.substr(0, classGroupRoot.length)){
            
//            alert(obj.className);
            
            var objClassValue = getValueFromClassName(obj.className, classGroupRoot);
            var not = isReverse(obj.className);
            
            // If we're just changing state then doo this now
            if (action == "state"){
                obj.className = classGroupRoot+objClassValue+"_" + item;
            }
            // otherwise either show it or switch it on
            else{
                // Use first in list or default
                item = GetBestValue(group, item, def);
                // See if this element matches
                var include = obj.include;
                var exclude = obj.exclude;
                var match = isMatch(item, objClassValue, classGroupRoot, not, include, exclude);
                
                // Set the style to "on" or "off"
                if (match){
                    if (action == "select"){
                        obj.className = classGroupRoot+objClassValue+"_on";
                        found = 1;
                    }
                    else if (action == "show"){
//                        alert("SHOW:"+obj.className+"  :  "+obj.style.visibilty+"   "+obj.style.display+" +");
                        obj.style.visibilty = "visible"
                        obj.style.display = ""
                        found = 1;
                    }
                }
                else{
                    if (action == "select"){
                        obj.className = classGroupRoot+objClassValue+"_off";
                    }
                    else if (action == "show"){
//                        alert("HIDE:"+obj.className+"  :  "+obj.style.visibilty+"   "+obj.style.display+" -");
                        obj.style.visibilty = "hidden"
                        obj.style.display = "none"
                    }
                }
            }
        }            
    }
}

function SetCookieValue(name, value){
    var date = new Date();
    date.setFullYear(date.getFullYear() + 1);
    var expires="expires=" + date.toGMTString();
    document.cookie=name+"="+value + "; " + expires;
}

function GetCookieValue(name){
    var cookies = document.cookie;
    var startPos = cookies.indexOf(name);
    if (startPos == -1)
        return "";
    
    startPos += name.length + 1;
    var endPos = cookies.indexOf(";", startPos);
    if (endPos == -1)
        endPos = cookies.length;
    
    var ret = cookies.substr(startPos, endPos-startPos);
    return ret;
}

function ShowBody(){
    var msg = document.getElementById("LOAD_MESSAGE");
    var body = document.getElementById("PAGE_BODY");
    
    msg.style.visibility='hidden';
    msg.style.display='none';
    body.style.visibility='visible';
    body.style.display='';
}

function GetURLParam(paramName){
    
    // Get the parameters from the URL
    urlquery= location.href.split("?")
    
    // If there are parameters then
    if (urlquery.length > 1){
        // Split up each parameter name/value pair
        urlterms = urlquery[1].split(",")
        
        // Loop through each param
        for (i = 0; i < urlterms.length; i++){
            param = urlterms[i].split("=");
            name = param[0];
            val = param[1];
            
            if (paramName.toLowerCase() == name.toLowerCase()){
                return val;
            }
        }
    }
    return "";
}

// Determines whether this element matches our requirememnts
function isMatch(testValue, objClassName, classGroupRoot, not, include, exclude){
    var match = false;
    
//    alert("IsMatch: "+testValue+" : "+objClassName+" : "+classGroupRoot+" : "+not+" : "+include+" : "+exclude);

    // Check if we're using exclusions/inclusions
    // Check for inclusions
    if ((include != null && include.length != 0)){
        match = false;
        incArray = include.split("|")
        for (i = 0; i < incArray.length; i++){
            var value = getValueFromClassName(incArray[i], classGroupRoot);
            if (value == testValue){
                match = true;
                break;
            }
        }
    }
    else if ((exclude != null && exclude.length != 0)){
        match = true;
        excArray = exclude.split("|")
        for (i = 0; i < excArray.length; i++){
            var value = getValueFromClassName(excArray[i], classGroupRoot);
            if (value == testValue){
                match = false;
                break;
            }
        }
    }
    else{    // Otherwise just use the object class name
        match = (testValue == objClassName) ? true : false;
    }
    
    // Reverse the sense if required
    if (not){
        match = !match;
    }
    
    return match;
}

function getValueFromClassName(className, classGroupRoot){
    // Get the objects item classification
   var objClassValue = className.substr(classGroupRoot.length);
    
    // Remove revers indicator
    if (objClassValue.indexOf("!") != -1)
        objClassValue = objClassValue.substr(0, objClassValue.indexOf("!"));
    
    // If the name has an underscore then chop off whatever is beyond this
    if (objClassValue.indexOf("_") != -1){
        objClassValue = objClassValue.substr(0, objClassValue.indexOf("_"));
    }
    return objClassValue;
}

function isReverse(objClassValue){
    return (objClassValue.indexOf("!") != -1) ? true : false;
}

function useCookies(cookie, group, item){
    if (cookie == 1){
        SetCookieValue(group, item);
    }

    if (cookie == 2){
        var newItemValue = GetCookieValue(group);
        if (newItemValue.length){
            item = newItemValue;
       }
    }
    return item;
}