function process_select(el){

    if (!document.getElementById || !el.getElementsByTagName)
        /* browser doesn't support getting child nodes by tag name */
        return;

	if (el.orig){
		/* restore original <select> */
		el.parentNode.replaceChild(el.orig, el);
		el = el.orig;
	}

	/* we need to keep track of original options. To do that, just make a copy
 	 * of the whole <select> element */
	el.orig = el.cloneNode(true);

    var sel_proj = get_selected_project();

    var opts = el.getElementsByTagName('option');

    for (var i = 0; i < opts.length; i++){
        var val, item, proj;

        /* the first child of the <option> should be a text node containing the
         * display value of the <option> */
        var optionTextNode = opts[i].childNodes[0];
        if (!optionTextNode || !optionTextNode.nodeValue){
            /* <option></option> is empty, or something else is wrong */
            continue;
		}

        val = optionTextNode.nodeValue;

        if (val.indexOf(':') == -1){
			continue;
		}

    	proj = val.substring(0, val.indexOf(':'));

        item = val.substring(val.indexOf(':') + 1);

        if (proj == sel_proj){
            optionTextNode.nodeValue = item;
        } else {
            if (opts[i].selected)
                el.selectedIndex = 0;
			el.removeChild(opts[i]);
			/* the nodeList returned by getElementsByTagName is live */
			i--;
        }
    }
	el.style.display = '';
}

function get_selected_project(){

    if (!document.getElementById)
        return false;

    var el = document.getElementById("book_form").pr;

    if (!el.getElementsByTagName)
        /* I think I am being overly cautious, but that's OK */
        return false;

    var opts = el.getElementsByTagName('option');
    if (!opts[el.selectedIndex] || !opts[el.selectedIndex].childNodes[0])
        return false;
    return opts[el.selectedIndex].childNodes[0].nodeValue;
}

function process_all_selects(){
    if (!document.getElementById)
        return;
    process_select(document.getElementById("book_form").tp);
    process_select(document.getElementById("book_form").xp);
}

window.onload = process_all_selects;
