// copyright (c) 1999  L. David Baron
// based on original by Chris Wilson

function writeout (selector ) { //debug function
	var j;

	for (j=0; j<selector.length; j++) {
		window.alert("selectedIndex is " + selector.selectedIndex);
		window.alert("Option " + j +
			" has text=" + selector.options[j].text +
			", value=" + selector.options[j].value +
			", label=" + selector.options[j].label);
	}
}

function strISSContains ( str, word ) {
// case-insensitive search for word as one of the space separated values
// within str.
// If word contains spaces, you won't get a match
	var list, i;

	list = str.toLowerCase().split(" ");
	word = word.toLowerCase();

	for ( i = 0 ; i < list.length ; i++ ) {
		// D  window.alert("comparing " + word + " with " + list[i]);   // D
		if (word == list[i])
			return 1;
	}
	return 0;
}


function selectStyleSheet( selector ) {
	var i;
 	var title = selector.options[ selector.selectedIndex ].value;

	//D title = "Oldstyle";

	linkElements = document.getElementsByTagName("LINK");

	for ( i = 0; i < linkElements.length; i++ ) {
		if ( strISSContains(linkElements[i].rel, "stylesheet")) {

			//D window.alert("Examining " + linkElements[i].title );

			if ( linkElements[i].title == title ) {
				linkElements[i].disabled = false;
			}
			else if ( linkElements[i].title != "" ) {
				linkElements[i].disabled = true;
			}
		}
	}
}


function UpdateSSList( selector ) {

	function RemoveAll () {
		var j;

		for (j=0; j < selector.length; j++ ) {

			writeout(selector);
			window.alert("Removing option " + j);

			selector.remove(j);

			writeout(selector);
		}
	}

	function AddIfUnique( ssLink ) {
		var i, opt;

		for ( i = 0; i < selector.length; i++ ) {
			if ( selector.options[i].text == ssLink.title )
				return;
		}

		//D window.alert("adding " + ssLink.title);

		opt = document.createElement("OPTION"); // must be uppercase
		// opt.text = ssLink.title;  // THIS (opt.text) IS READONLY
		opt.label = ssLink.title;
		opt.value = ssLink.title;
		// could use document.createTextNode and opt.appendChild  ??

		//D window.alert("ssLink.title=" + ssLink.title);
		//D window.alert("opt.label=" + opt.label);
		writeout(selector);

		// selector.add( opt , selector.options[ selector.length - 1] );
		selector.add( opt , selector.lastChild );
		if ( ! ssLink.disabled )  // only one unique title may be enabled 
			selector.selectedIndex = opt.index;
	}
	
	
	function AddNone() {

	    var opt;

		opt = document.createElement("OPTION"); // must be uppercase
		opt.text = "--NONE--";
		selector.add( opt , null );
		selector.selectedIndex = 0;
	}
	

	var j, linkElements;

	// XXX NEEDED selector.options.length = 0; // nonstandard
	RemoveAll();

	AddNone();
	linkElements = document.getElementsByTagName("LINK");

	for ( j = 0; j < linkElements.length; j++ ) {   // nonstandard ...
		if ( ( linkElements[ j ].title != "" ) &&
				(strISSContains(linkElements[j].rel, "stylesheet")) ) {
			AddIfUnique( linkElements[j] );
			//D window.alert("adding " + linkElements[j].title);
		}
	}
}

