sortitems = 1;  // Automatically sort items within lists? (1 or 0)

function addTicker()	{
	si = document.forms['inputform'].exchange.selectedIndex;
	exchangeID = document.forms['inputform'].exchange.options[si].value;
	exchangeText= document.forms['inputform'].exchange.options[si].text;
	ticker = document.forms['inputform'].ticker.value.toUpperCase();

	if (ticker == "") {
		alert("You have not entered a valid ticker.");
		return;
	}	
	exchangeID_ticker = exchangeID + ':' + ticker;
	exchangeText_ticker = exchangeText + ':' + ticker;
	ticker_symbols_len = document.forms['inputform'].TickerSymbols.length;
	document.forms['inputform'].TickerSymbols.options[ticker_symbols_len] = new Option(exchangeText_ticker,exchangeID_ticker,"","");
	
	document.forms['inputform'].ticker.value = "";
	document.forms['inputform'].ticker.focus();
}


// new version of addCompany - user is only allowed add 30 companies to the list
function addCompany() { 
  company = document.forms['inputform'].company.value; 

  if (company == "") { 
    alert("You have not entered a valid company.");
    return; 
  } 

  companylist_len = document.forms['inputform'].companylist.length;

  if (companylist_len >29) {
    alert("You can only add 30 companies to the list");
  } 

  if (companylist_len <=29) { 
    document.forms['inputform'].companylist.options[companylist_len] = new Option(company,company,"",""); 
    document.forms['inputform'].company.value = ""; 
    document.forms['inputform'].company.focus();     
  }
} 




function addgeo(number)	{
	drop = 'region' + number;
	si = document.forms['inputform'][drop].selectedIndex;
	values = document.forms['inputform'][drop].options[si].value;
	
	if ( values ) {
            region_len = document.forms['inputform'].region.length;
            document.forms['inputform'].region.options[region_len] = new Option(values,values,"","");
	}
	
	document.forms['inputform'][drop].focus();
}

function deletesel(fbox) {
// Added to strip the first blank entry
	fbox.focus();
	
//	BumpUp(tbox);	
// Well it highly unlikely that 9,999,999 entries will be added to a <SELECT>
// but this should cover it :)
	for(var i=0; i<fbox.options.length; i++) {
            if(fbox.options[i].selected &&
                fbox.options[i].value != "") {
                fbox.options[i].value = "";
                fbox.options[i].text = "";
            }
	}

	BumpUp(fbox);
	if (sortitems) SortD(fbox);
	deselectList(fbox);
}

function toMyIndustries(fbox, tbox, itemlimit) {
//alert("tbox has " + tbox.options.length);
    var gotone = 0;
    if (itemlimit == null) { itemlimit = 9999999 }
    for(var i=0; i<fbox.options.length; i++) {
        if(fbox.options[i].selected && fbox.options[i].value != "" &&
            itemlimit >= tbox.options.length + 1) {
            gotone = 1;
        }
    }
    if ( gotone > 0 ) {
        if (  tbox.options.length > 0 && tbox.options[0].value == "-1" ) {
            // remove the 'Any Industry'
            tbox.options[0].value = "";
            tbox.options[0].text  = "";
        }
        move(fbox, tbox, itemlimit);
    }
}

function fromMyIndustries(fbox, tbox, itemlimit) {
//alert("fbox has " + fbox.options.length);
    if (  fbox.options.length > 0 && fbox.options[0].value != "-1" ) {
        move(fbox, tbox, itemlimit);
    }
//alert("fbox has " + fbox.options.length);
    if ( fbox.options.length == 0 ) {
        // Add the 'Any Industry'
            var no = new Option();
            no.value = "-1";
            no.text = "Any Industry";
            fbox.options[fbox.options.length] = no;
    }
//alert("value " + fbox.options[0].value);
//alert("text " + fbox.options[0].text);
}

function toMySubjects(fbox, tbox, itemlimit) {
//alert("tbox has " + tbox.options.length);
    var gotone = 0;
    if (itemlimit == null) { itemlimit = 9999999 }
    for(var i=0; i<fbox.options.length; i++) {
        if(fbox.options[i].selected && fbox.options[i].value != "" &&
            itemlimit >= tbox.options.length + 1) {
            gotone = 1;
        }
    }
    if ( gotone > 0 ) {
        if (  tbox.options.length > 0 && tbox.options[0].value == "-1" ) {
            // remove the 'Any Subject'
            tbox.options[0].value = "";
            tbox.options[0].text  = "";
        }
        move(fbox, tbox, itemlimit);
   }
}

function fromMySubjects(fbox, tbox, itemlimit) {
//alert("fbox has " + fbox.options.length);
    if (  fbox.options.length > 0 && fbox.options[0].value != "-1" ) {
        move(fbox, tbox, itemlimit);
    }
//alert("fbox has " + fbox.options.length);
    if ( fbox.options.length == 0 ) {
        // Add the 'Any Subject'
        var no = new Option();
        no.value = "-1";
        no.text = "Any Subject";
        fbox.options[fbox.options.length] = no;
    }
//alert("value " + fbox.options[0].value);
//alert("text " + fbox.options[0].text);
}

function move(fbox,tbox, itemlimit) {
// Added to strip the first blank entry
	fbox.focus();
	
	BumpUp(tbox);	
// Well it highly unlikely that 9,999,999 entries will be added to a <SELECT>
// but this should cover it :)
	if (itemlimit == null) { itemlimit = 9999999 }
	for(var i=0; i<fbox.options.length; i++) {
            if(fbox.options[i].selected && fbox.options[i].value != "" &&
                itemlimit >= tbox.options.length + 1) {
                var no = new Option();
                no.value = fbox.options[i].value;
                no.text = fbox.options[i].text;
                tbox.options[tbox.options.length] = no;
                fbox.options[i].value = "";
                fbox.options[i].text = "";
            }
	}

	BumpUp(fbox);
	if (sortitems) SortD(tbox);
	deselectList(fbox);
}

function BumpUp(box)  {
	for(var i=0; i<box.options.length; i++) {
		if(box.options[i].value == "")  {
			for(var j=i; j<box.options.length-1; j++)  {
				box.options[j].value = box.options[j+1].value;
				box.options[j].text = box.options[j+1].text;
			}
			var ln = i;
			break;
		}
	}

	if(ln < box.options.length)  {
		box.options.length -= 1;
		BumpUp(box);
	}
}

function SortD(box) {
	var temp_opts = new Array();
	var temp = new Object();
	for(var i=0; i<box.options.length; i++)  {
		temp_opts[i] = box.options[i];
	}
	for(var x=0; x<temp_opts.length-1; x++)  {
		for(var y=(x+1); y<temp_opts.length; y++)  {
			if(temp_opts[x].text > temp_opts[y].text)  {
				temp = temp_opts[x].text;
				temp_opts[x].text = temp_opts[y].text;
				temp_opts[y].text = temp;
				temp = temp_opts[x].value;
				temp_opts[x].value = temp_opts[y].value;
				temp_opts[y].value = temp;
			}
		}
	}
	for(var i=0; i<box.options.length; i++)  {
		box.options[i].value = temp_opts[i].value;
		box.options[i].text = temp_opts[i].text;
	}
}


// PA: Icky hack, but nessary :(
function initialiseLists()	{
	preSelect();
	move(document.forms['inputform'].OverallIndust,
	     document.forms['inputform'].OverallIndustSrc);
	move(document.forms['inputform'].OverallSubject,
	     document.forms['inputform'].OverallSubjectSrc);
}

function selectedList(listbox) {
	if (listbox.options.length > 0)	{
		for (var i=0; i<listbox.options.length; i++)	{
			listbox.options[i].selected = true;
		}
	}
}

function deselectList(listbox) {
	if (listbox.options.length > 0)	{
		for (var i=0; i<listbox.options.length; i++)	{
			listbox.options[i].selected = false;
		}
	}
}

function preSelect() {
	deselectList(document.forms['inputform'].OverallIndustSrc);
	deselectList(document.forms['inputform'].OverallSubjectSrc);
	deselectList(document.forms['inputform'].exchange);
	deselectList(document.forms['inputform'].region1);
	deselectList(document.forms['inputform'].region2);
	deselectList(document.forms['inputform'].region3);
	selectedList(document.forms['inputform'].OverallIndust);
	selectedList(document.forms['inputform'].OverallSubject);
	selectedList(document.forms['inputform'].companylist);
	selectedList(document.forms['inputform'].TickerSymbols);
	selectedList(document.forms['inputform'].region);
	return false;
}


function preSelectDirectory() {
	//deselectList(document.forms['inputform'].OverallIndustSrc);
	//deselectList(document.forms['inputform'].OverallSubjectSrc);
	//deselectList(document.forms['inputform'].exchange);
	//selectedList(document.forms['inputform'].OverallIndust);
	//selectedList(document.forms['inputform'].OverallSubject);
	selectedList(document.forms['inputform'].companylist);
	//selectedList(document.forms['inputform'].TickerSymbols);
	//selectedList(document.forms['inputform'].region);
	return false;
}



function preSelect2() {
	deselectList(document.forms['inputform'].OverallIndustSrc);
	deselectList(document.forms['inputform'].OverallSubjectSrc);
	deselectList(document.forms['inputform'].exchange);
	selectedList(document.forms['inputform'].OverallIndust);
	selectedList(document.forms['inputform'].OverallSubject);
	selectedList(document.forms['inputform'].companylist);
	selectedList(document.forms['inputform'].TickerSymbols);
	selectedList(document.forms['inputform'].region);
	return false;
}



/* reset each of the select lists for the directory of contacts screen */
function resetDCSearch() {
	preSelectDirectory2();
	deletesel(document.forms['inputform'].OverallIndust);
	deletesel(document.forms['inputform'].OverallSubject);
	deletesel(document.forms['inputform'].TickerSymbols);
	deletesel(document.forms['inputform'].companylist);
	deletesel(document.forms['inputform'].region);

	document.forms['inputform'].region1.options[0].selected = true;
	
	for (m=2; m<=3; m++) {
		mregion = eval("document.forms['inputform'].region" + m);
		mregion.options.length = 0;
	}
	document.images["arrow2"].src = "../images/spacer.gif";
	document.images["arrow3"].src = "../images/spacer.gif";
}

