//create a global array to handle options for lists that have not yet been created
var existingOptions = new Array(0);

function getExistingOptions(){
	return existingOptions;
}

function addToExistingOptions(optToAdd){
	existingOptions.push(optToAdd);
}

function printDropdown(){

	document.write("<form name=\"frmIntraPJ\">");
	document.write("<select name=\"intraSelect\" onchange=\"IntraPJSelectChange(this)\" class=\"formElements\">");

	for(var index = 0; index < existingOptions.length; index ++){
		document.write("<option value=\"" + existingOptions[index].value + "\">" + existingOptions[index].text +"</option>");
	}

	document.write("</select>");
	document.write("</form>");
}

function IntraPJSelectChange(theSelect) {
	if (theSelect.options[theSelect.selectedIndex].value != "" ) {
		window.location = theSelect.options[theSelect.selectedIndex].value;
	}
}

function addOption(anchor, title){
	var i=0;
	formindex = new Array();

	//get all the drop down locations
	for (var j=0; j<document.forms.length; j++) {
		if ((document.forms[j].name).indexOf('frmIntraPJ') == 0) {
			formindex[i]=j;
			i++;
		}
	}

	// Remove tags from title, but not html between tags
	var tagExp = /<[^<>]*>/g;
	title = title.replace(tagExp, "");

	//create option
	var optToAdd = new Option(title , '#'+anchor);

	//if i is > 0, we found a list and may add the option to it
	if(i > 0){
		//we have found at least one dropdown menu. create a new option and add it

		//loop through all the list founf thus far
		for (j=0; j<formindex.length; j++) {
			//get the current length
			var length = document.forms[formindex[j]].intraSelect.options.length;
			//add an option to the end
			document.forms[formindex[j]].intraSelect.options.length = length+1;
			document.forms[formindex[j]].intraSelect.options[length] = optToAdd;

		}
	}

	//finally, add the option to a global array of options in case a secound
	//drop down is created later
	addToExistingOptions(optToAdd);

}
