var speccats, names, gcchecked, gencats, dscr, hypervar,
speccats_l, names_l, gcchecked_l, gencats_l, dscr_l;


function init (event) {
    var i, len, gensel, gensel_l, len_l;
    
    speccats = new Array;
    names = new Array;
    dscr = new Array;

    fill();

    // if the user performs a search and then goes back to this page (hosp_search.php), the third select (specific cats.) won't automatically fill up with items even though an appropriate row might be selected in the second select (general cats.)
    // this is why we'll fill it ourselves every time the page is loaded:
    gensel = document.getElementById("gencatsel");

    for (i = 0; i < gensel.options.length; i++)
	if (gensel.options[i].selected) {
	    add_items(parseInt(gensel.options[i].value));
	    break;
	}
}

function init_l (event) {
    var i, len, gensel, gensel_l, len_l;
    // the same code for laboratories

    speccats_l = new Array;
    names_l = new Array;
    dscr_l = new Array;

    fill_l();

    // if the user performs a search and then goes back to this page (hosp_search.php), the third select (specific cats.) won't automatically fill up with items even though an appropriate row might be selected in the second select (general cats.)
    // this is why we'll fill it ourselves every time the page is loaded:
    gensel_l = document.getElementById("gencatsel_l");

    for (i = 0; i < gensel_l.options.length; i++)
	if (gensel_l.options[i].selected) {
	    add_items_l(parseInt(gensel_l.options[i].value));
	    break;
	}
    
}

function add_secondary_items(event) {
   // get the id of the general cat that called the function and, if it's non-zero, fill the next select with its spec. cats.
    add_items(parseInt(event.srcElement.value));
}

function add_items (gencatnum) {
    var i, j, scsel, tlen, tmp, opts;
    
    // delete everything but the default item from the spec. cats. select (the default item is first)
    scsel = document.getElementById("speccatsel");
    opts = scsel.getElementsByTagName("option");
    while ((j = opts.length) > 1) {
	scsel.removeChild(opts[j - 1]);
    }
    opts[0].selected = true;

 
    if (gencatnum == 0 || speccats[gencatnum] == null) {
	return;
    }

    // append options for all specific cats, putting their ids as values and their names as text nodes within
    tlen = speccats[gencatnum].length;
    for (i = 0; i < tlen; i++) {
        tmp = document.createElement("option");
        tmp.value = speccats[gencatnum][i];
        tmp.appendChild(document.createTextNode(names[speccats[gencatnum][i]].toString(10)));
        scsel.appendChild(tmp);
    }
    
}


// the last two functions repeated for laboratories

function add_secondary_items_l(event) {
   // get the id of the general cat that called the function and, if it's non-zero, fill the next select with its spec. cats.
    add_items_l(parseInt(event.srcElement.value));
}

function add_items_l (gencatnum) {
    var i, j, scsel, tlen, tmp, opts;
    
    // delete everything but the default item from the spec. cats. select (the default item is first)
    scsel = document.getElementById("speccatsel_l");
    opts = scsel.getElementsByTagName("option");
    while ((j = opts.length) > 1) {
	scsel.removeChild(opts[j - 1]);
    }
    opts[0].selected = true;
 
    if (gencatnum == 0 || speccats_l[gencatnum] == null) {
	return;
    }


    // append options for all specific cats, putting their ids as values and their names as text nodes within
    tlen = speccats_l[gencatnum].length;
    for (i = 0; i < tlen; i++) {
        tmp = document.createElement("option");
        tmp.value = speccats_l[gencatnum][i];
        tmp.appendChild(document.createTextNode(names_l[speccats_l[gencatnum][i]].toString(10)));
        scsel.appendChild(tmp);
    }
    
}

function add_description (event) {
    var ix, fullname, hta;

    hta = document.getElementById("HospTextArea");
    if (hta.childNodes.length)
	hta.removeChild(hta.childNodes[hta.childNodes.length - 1]);

    ix = parseInt(event.srcElement.value);    
    if (ix == 0) return;

    fullname = document.createTextNode(names[ix] + "\n\r\n\r" + dscr[ix]);
    document.getElementById("HospTextArea").appendChild(fullname);
}

function add_description_l (event) {
    var ix, fullname, hta;
    
    hta = document.getElementById("LabTextArea");
    if (hta.childNodes.length)
        hta.removeChild(hta.childNodes[hta.childNodes.length - 1]);

    ix = parseInt(event.srcElement.value);    
    if (ix == 0) return;

    fullname = document.createTextNode(names_l[ix] + "\n\r\n\r" + dscr_l[ix]);
    document.getElementById("LabTextArea").appendChild(fullname);
}
