﻿/* ------------------------------- */
/* CONSTANTS */
/* ------------------------------- */
var program_list_container = 'programlist';
var FormName = 'propfilter_form';
var minus_sign = '_global/images/minus.gif';
var plus_sign = '_global/images/plus.gif';
var str_selectedvalues = '';    //  A string of comma deliniated key values to VALUE records

/* ------------------------------- */
/* EVENT TRAPS */
/* ------------------------------- */
//  When the window loads, run the function 'init'
Event.observe(window, 'load', init, false);

/* ------------------------------- */
/* INITILIZE when window loads */
/* ------------------------------- */
function init() {
    /* See if there is a program list DIV on this page                  */
    var dom_element = document.getElementById(program_list_container);
    if (dom_element) {
        /* Perform AJAX refresh of the main content                  */
        ajax_is_loading();
        new Ajax.Updater(program_list_container, '_ajax/propfilter_handle.asp?action=show');
    }
}

/* ------------------------------------------- */
// USER CLICKED DEBUG
/* ------------------------------------------- */
function propdebug_clicked() {
    ajax_is_loading();
    new Ajax.Updater(program_list_container, '_ajax/propfilter_handle.asp?action=debug');
}

/* ------------------------------------------- */
/* Throw up a "I am busy"               */
/* ------------------------------------------- */
function ajax_is_loading() {
    var dom_element = document.getElementById('ajaxloading');
    if (!dom_element) {
        Insertion.Bottom(program_list_container, '<div id="ajaxloading"><img src="_global/images/ajaxloading.gif" alt="Loading..."><br />Searching...</div>');
    }
}

/* ------------------------------------------- */
/* USER CLICKED A PROPERTY VALUE CHECKBOX      */
/* ------------------------------------------- */
function propvalue_clicked(value_key) {
    /* ------------------------------------------- */
    // Change the text to match the state of the check box
    /* ------------------------------------------- */
    // Get the <span> elements with the ID
    var obj_text = document.getElementById("propvalue_text" + value_key);
    if (!obj_text)
        return;
    // Get the <input> elements with this ID
    var obj_checkbox = document.getElementById("propvalue_cb" + value_key);
    if (!obj_checkbox)
        return;
    /* Change the name style class                 */
    var setto;
    if (obj_checkbox.checked) {
        obj_text.className = "propvalue_on";
        setto = 1;
    }
    else {
        obj_text.className = "propvalue_off";
        setto = 0;
    }
    /* ------------------------------------------- */
    // Tell the server about the new value, and
    //  update the article list based on the new results
    /* ------------------------------------------- */
    /* Perform AJAX update of the main content                  */
    ajax_is_loading();
    new Ajax.Updater(program_list_container, '_ajax/propfilter_handle.asp?action=toggle&value=' + value_key + '&setto=' + setto)
}

/* ------------------------------------------- */
/* USER CLICKED A PROPERTY NAME                */
/* ------------------------------------------- */
function propname_clicked(name_key) {
    /* ------------------------------------------- */
    /* Expand or Collapse the list of values               */
    /* ------------------------------------------- */
    var divname = "propname" + name_key;  // The div to expand/collapse
    var dom_element = document.getElementById(divname);
    if (!dom_element) {
        return;
    }
    propvalues_showhide('toggle', dom_element);   //  Toggle the show/hide
}

/* ------------------------------------------- */
// Show or hide the list of values for a given property name
/* ------------------------------------------- */
function propvalues_showhide(cmd_in, dom_element_in) {
    var input_cnt = 0;
    var show_list = false;

    /* ------------------------------------------- */
    //  show/hide division
    /* ------------------------------------------- */
    //  Get a collection of DIV tags
    var dom_element = dom_element_in.getElementsByTagName("div");
    if (!dom_element)
        return;
    input_cnt = dom_element.length;
    if (!input_cnt)
        return;
    //  Loop through all the elements and set the attributes
    for (var i = 0; i < input_cnt; i++) {
        if (dom_element[i].className == 'showhide_div') {
            //  OK... found our division. Now decide what to set it to
            if (cmd_in == 'toggle') {   // Determine what the current setting is
                if (dom_element[i].style.display == "none") {
                    show_list = true;   //  Current hide, so ask for show
                }
                else {
                    show_list = false;   //  Current show, so ask for hide
                }
            }
            else {
                if (cmd_in == 'show') {
                    show_list = true;
                }
                else {
                    if (cmd_in == 'hide') {
                        show_list = false;
                    }
                    else {
                        return false;   // Command was unrecognized... get outta here
                    }
                }
            }
            //  Show or hide the div
            if (show_list) {
                dom_element[i].style.display = "block";
            }
            else {
                dom_element[i].style.display = "none";
            }
        }
    }

    /* ------------------------------------------- */
    //  Toggle the plus/minus image
    /* ------------------------------------------- */
    //  Get a collection of IMG tags
    var dom_element = dom_element_in.getElementsByTagName("img");
    if (!dom_element)
        return;
    input_cnt = dom_element.length;
    if (!input_cnt)
        return;
    //  Loop through all the elements and set the attributes
    for (var i = 0; i < input_cnt; i++) {
        if (dom_element[i].className == 'showhide_icon') {
            if (show_list) {
                dom_element[i].src = minus_sign;
            }
            else {
                dom_element[i].src = plus_sign;
            }
        }
    }
    return;
}

/* ------------------------------------------- */
// User clicked EXPAND/COLLAPSE ALL
/* ------------------------------------------- */
function expand(cmd_in) {
    /* ------------------------------------------- */
    //  Determine what to do
    /* ------------------------------------------- */
    if (cmd_in == 'show') {
    }
    else {
        if (cmd_in == 'hide') {
        }
        else {
            return false;   // Unrecognized command... get outta here
        }
    }
    /* ------------------------------------------- */
    //  Show all of the value lists
    /* ------------------------------------------- */
    //  Get a collection of div tags
    var dom_element = document.getElementById("propfilter_form").getElementsByTagName("div");
    if (!dom_element)
        return;
    input_cnt = dom_element.length;
    if (!input_cnt)
        return;
    //  Loop through all the elements and set the attributes
    for (var i = 0; i < input_cnt; i++) {
        if (dom_element[i].className == 'propfilter_prop') {
            propvalues_showhide(cmd_in, dom_element[i]);   //  Set the show/hide
        }
    }
}

/* ------------------------------------------- */
// User clicked "SELECT ALL/NONE" for a NAME
/* ------------------------------------------- */
function checkall(cmd_in, name_key) {
    var setto;
    var value_key;
    var allchecked;
    var text_style;
    var hash_valuekeys = new Hash({});  // prototype.js function

    /* ------------------------------------------- */
    //  Determine if I need to check or uncheck
    /* ------------------------------------------- */
    //  Is the form there?
    if (!document.forms[FormName])
        return;

    //  Am I checking or unchecking them all?
    if (cmd_in == 'all') {
        allchecked = true;
        setto = 1;
        text_color_style = 'Black';
    }
    else {
        if (cmd_in == 'none') {
            allchecked = false;
            setto = 0;
            text_color_style = 'Gray';
        }
        else {
            return; // bad 'cmd_in'.... get outta here
        }
    }

    /* ------------------------------------------- */
    //  Toggle the check boxes
    /* ------------------------------------------- */
    //  Get a collection of check boxes
    if (name_key == 0) {    // Choose the DIV that contains ALL of the properties
        var objInput = document.getElementById("propfilter_form").getElementsByTagName("input");
    }
    else {  // Choose the DIV that contains only the values for one property name
        var objInput = document.getElementById("propval_list" + name_key).getElementsByTagName("input");
    }
    if (!objInput)
        return;
    var input_cnt = objInput.length;
    if (!input_cnt)
        return;
    //  Loop through all the elements and set the checked attribute
    for (var i = 0; i < input_cnt; i++) {
        if (objInput[i].type == "checkbox") {
            objInput[i].checked = allchecked;  //  Set the check box
            /* add the value key and setting to the value string */
            hash_valuekeys.set(objInput[i].value, setto);  // prototype.js function
        }
    }
    /* ------------------------------------------- */
    // Perform AJAX refresh of the main content
    /* ------------------------------------------- */
    ajax_is_loading();  // Throw up a busy message
    new Ajax.Updater(program_list_container, '_ajax/propfilter_handle.asp?action=batch&selected=' + hash_valuekeys.toJSON());

    /* ------------------------------------------- */
    //  Change the value text attributes
    /* ------------------------------------------- */
    //  Get a collection of span tags
    if (name_key == 0) {    // Choose the DIV that contains ALL of the properties
        var objSpan = document.getElementById("propfilter_form").getElementsByTagName("span");
    }
    else {  // Choose the DIV that contains only the values for one property name
        var objSpan = document.getElementById("propval_list" + name_key).getElementsByTagName("span");
    }
    if (!objSpan)
        return;
    input_cnt = objSpan.length;
    if (!input_cnt)
        return;
    //  Loop through all the elements and set the text attributes
    for (var i = 0; i < input_cnt; i++) {
        if (objSpan[i].className == 'propvalue_text') {
            objSpan[i].style.color = text_color_style;  //  style
        }
    }

    return;
}

/* ------------------------------------------- */
/* Show an article in a new window             */
/* ------------------------------------------- */
function show_article(art_no) {
    var winurl = "show_article.asp?art=" + art_no;
    openwin = window.open(winurl, 'article', 'scrollbars=yes,status=yes,height=500,width=620');
    openwin.focus();
}

/* ------------------------------------------- */
//  User changed a select box
//  Return false if zero, redirect if not.
/* ------------------------------------------- */
function property_sel(select_id) {
    //  Get the value that was choses from the pull down select box
    var form_obj = $('propertyvalue_form');
    if (!form_obj) {
        return false;
    }

    var select_obj = form_obj[select_id];
    if (!select_obj) {
        return false;
    }

    var parm_value_key = $F(select_obj);
    //  It is is zero, then ignore it
    if (parm_value_key == 0) {
        return false;
    }
    else {
        var newurl = 'programs_list.asp?propvalue=' + parm_value_key;
        window.location = newurl;
    }
}
