/************************************************************************************************************
(C) www.dhtmlgoodies.com, October 2005

This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.

Terms of use:
You are free to use this script as long as the copyright message is kept intact. However, you may not
redistribute, sell or repost it without our permission.

Thank you!

www.dhtmlgoodies.com
Alf Magne Kalleland

************************************************************************************************************/

/* 2006-12-13, Hong Zhang
 * Added function getUrlSubject (), to extract url parameters, and loading
 * records for corresponding subject.
 */


var ajaxSubj = new sack();
var ajaxFormat = new sack();
var ajaxRecord = new sack();


function getSubjList(subjCode)
{
        // Empty subject select box
    document.getElementById ('lib_subject').options.length = 0;

    ajaxSubj.requestFile = '/search/databases/getSubj2.php?subjCode=' + subjCode;
    ajaxSubj.onCompletion = createSubj;
    ajaxSubj.runAJAX();         // Execute AJAX function
}

function createSubj()
{
    var objSubj = document.getElementById('lib_subject');
        // Executing the response from Ajax as Javascript code
    eval(ajaxSubj.response);
}


function getFormat(subjCode, formatCode)
// formatCode could be empty.
{
        // Empty format select box
    document.getElementById ('lib_format').options.length = 0;
        // Empty content area.
    document.getElementById ('lib_records').innerHTML = '';

    if ( subjCode.length > 0 )
    {
            // Specifying which file to get
        ajaxFormat.requestFile = '/search/databases/getFormat2.php?subjCode=' + subjCode + "&formatCode=" + formatCode;
        ajaxFormat.onCompletion = createFormat;
        ajaxFormat.runAJAX();         // Execute AJAX function
    }
}

function getAllRecords (subj)
{
    ajax.requestFile = '/search/databases/getFormat2.php?subjCode=' + subjCode + '&all_records=all';
    ajax.onCompletion = showRecords;
    ajax.runAJAX();         // Execute AJAX function
}


function createFormat()
{
    var objFormat = document.getElementById('lib_format');
        // Executing the response from Ajax as Javascript code

    eval(ajaxFormat.response);
}

function getRecords(subjCode, formatCode)
{
/*
    // Reset the expandall/closeall icon.
    var objImg = document.getElementById('allIcon');
    objImg.src = expandallImg.src;
*/

    // Disable any remaining ajax running, if any.
    if ( getArr_ajax )
    {
        getArr_ajax = new Array ();
    }

    // Removed on request.
//    document.getElementById ('loading_img').style.display = 'block';

        // Empty content area.
    document.getElementById ('lib_records').innerHTML = '';


    if ( formatCode.length > 0 )
    {
            // Specifying which file to get
        ajaxRecord.requestFile = '/search/databases/getRecord2.php?subjCode=' + subjCode + '&formatCode=' + formatCode;

            // Specify function that will be executed after file has been found
        ajaxRecord.onCompletion = displayRecords;
        ajaxRecord.runAJAX();         // Execute AJAX function
    }
}

function displayRecords ()
{
    // Removed on request.
//    setTimeout("showRecords()", 50);
    showRecords ();
}

function showRecords()
{
    // Removed on request.
//    document.getElementById ('loading_img').style.display = 'none';

    var objRecord = document.getElementById('lib_records');
        // Executing the response from Ajax as Javascript code
    var result = ajaxRecord.response;
    objRecord.innerHTML = result;
}

function getSubcatList(sel)
{
    var subjCode = sel.options[sel.selectedIndex].value;

        // Empty subcategory select box
    document.getElementById ('lib_subcat').options.length = 0;
        // Empty format select box
    document.getElementById ('lib_format').options.length = 0;
        // Empty content area.
    document.getElementById ('lib_records').innerHTML = '';

    if ( subjCode.length > 0 )
    {
            // Specifying which file to get
        ajax.requestFile = '/search/databases/getSubcat.php?subjCode=' + subjCode;
            // Specify function that will be executed after file has been found
        ajax.onCompletion = createSubcat;
        ajax.runAJAX();         // Execute AJAX function
    }
}

function createSubcat()
{
    var obj = document.getElementById('lib_subcat');
        // Executing the response from Ajax as Javascript code
    eval(ajax.response);
}



/* Added 2006-12-13, Hong Zhang
 * Extract url parameters, and loading records for corresponding subject.
 */

/*
Webmonkey GET Parsing Module
Language: JavaScript 1.0
The parsing of GET queries is fundamental
to the basic functionality of HTTP/1.0.
This module parses GET with JavaScript 1.0.
Source: Webmonkey Code Library
(http://www.hotwired.com/webmonkey/javascript/code_library/)
Author: Patrick Corcoran
Author Email: patrick@taylor.org
*/


function createRequestObject() {
  FORM_DATA = new Object();
    // The Object ("Array") where our data will be stored.
  separator = ',';
    // The token used to separate data from multi-select inputs
  query = '' + this.location;
  qu = query
    // Get the current URL so we can parse out the data.
    // Adding a null-string '' forces an implicit type cast
    // from property to string, for NS2 compatibility.
  query = query.substring((query.indexOf('?')) + 1);
    // Keep everything after the question mark '?'.
  if (query.length < 1) { return false; }  // Perhaps we got some bad data?
  keypairs = new Object();
  numKP = 1;
    // Local vars used to store and keep track of name/value pairs
    // as we parse them back into a usable form.
  while (query.indexOf('&') > -1) {
    keypairs[numKP] = query.substring(0,query.indexOf('&'));
    query = query.substring((query.indexOf('&')) + 1);
    numKP++;
      // Split the query string at each '&', storing the left-hand side
      // of the split in a new keypairs[] holder, and chopping the query
      // so that it gets the value of the right-hand string.
  }
  keypairs[numKP] = query;
    // Store what's left in the query string as the final keypairs[] data.<
  for (i in keypairs) {
    keyName = keypairs[i].substring(0,keypairs[i].indexOf('='));
      // Left of '=' is name.
    keyValue = keypairs[i].substring((keypairs[i].indexOf('=')) + 1);
      // Right of '=' is value.
    while (keyValue.indexOf('+') > -1) {
      keyValue = keyValue.substring(0,keyValue.indexOf('+')) + ' ' + keyValue.substring(keyValue.indexOf('+') + 1);
        // Replace each '+' in data string with a space.
    }
    keyValue = unescape(keyValue);
      // Unescape non-alphanumerics
    if (FORM_DATA[keyName]) {
      FORM_DATA[keyName] = FORM_DATA[keyName] + separator + keyValue;
        // Object already exists, it is probably a multi-select input,
        // and we need to generate a separator-delimited string
        // by appending to what we already have stored.
    } else {
      FORM_DATA[keyName] = keyValue;
        // Normal case: name gets value.
    }
  }
  return FORM_DATA;
}
FORM_DATA = createRequestObject();
  // This is the array/object containing the GET data.
  // Retrieve information with 'FORM_DATA [ key ] = value'.


function getUrlSubject ()
{
    createRequestObject ();

    var subject = '51';
    var format = '21';

    if ( typeof(FORM_DATA['subject']) != "undefined" && FORM_DATA['subject'].length > 0 )
        // Set subject to the url parameter.
        subject = FORM_DATA['subject'];

    if ( typeof(FORM_DATA['format']) != "undefined" && FORM_DATA['format'].length > 0 )
        // Set format to the url parameter.
        format = FORM_DATA['format'];

    getSubjList(subject);
    getFormat(subject, format);
    getRecords(subject, format);
}

