/* Geography function */

/* geo boxes */
function setdropdown(num,all) {
	id1 = document.forms['inputform'].region1.options[document.forms['inputform'].region1.selectedIndex].value;
	id2 = document.forms['inputform'].region2.options[document.forms['inputform'].region2.selectedIndex].value;
	
	for (m=num; m<=3; m++) {
		mregion = eval("document.forms['inputform'].region" + m);
		mregion.options.length = 0;
	}
	
	region = eval("document.forms['inputform'].region" + num);
	region.options.length = 1;
	region.options[0].value = "*";
	region.options[0].selected = true;



	for (m=num; m<=3; m++) {
		marrow = "arrow" + m;
		document.images[marrow].src = "../images/spacer.gif";
	}
	
	num--;
	id = eval("id" + num);

	if(num==1){

               if (all)
               {
                 region.options[1] = new Option("---------- Select All ----------","all");
		 var count = 2;
	       }
               else
               {
               	 var count = 1;
               }
		for (i=0; i < level_2.length; i++)
		{
			var arrayOfStrings = level_2[i].split("|");
			if(arrayOfStrings[2]==id){
				switch (num) {
					case 1:
						region.options[0].value = "";
						region.options[0].text = "------ Select Country ------";
						document.images["arrow2"].src = "../images/black_arrow.gif";
						document.images["arrow3"].src = "../images/spacer.gif";

  			 	                break;
					case 2:
						region.options[0].value = "";
						region.options[0].text = "------ Select State ------";
						document.images["arrow3"].src = "../images/black_arrow.gif";
						break;
				}

                                region.options[count] = new Option(arrayOfStrings[1], arrayOfStrings[0]);
				count++;
				var pid = arrayOfStrings[0];

				for (j=0; j < level_2.length; j++ )
				{
					var arrayOfStrings2 = level_2[j].split("|");
					if (arrayOfStrings2[2]==pid)
					{
						region.options[count] = new Option(arrayOfStrings2[1], arrayOfStrings2[0]);
						count++;
					}
				}

			}

		}

	}
	
	if(num==2){
	        if (all)
               {
	        region.options[1] = new Option("-------- Select All --------","all");
		var count = 2;
               }
               else
               {
               	 var count = 1;
               }
                if (id2 == 1001) { /* the USA */
                    var level3Length = USlevel_3.length;
                }
                else {
                    var level3Length = level_3.length;
                }
		for (i=0; i < level3Length; i++)
		{
                    if (id2 == 1001) { /* the USA */
			var arrayOfStrings = USlevel_3[i].split("|");
                        region.options[0].value = "*";
                        region.options[0].text = "------ Select State ------";
			region.options[count] = new Option(arrayOfStrings[1], arrayOfStrings[0]);
                        count++;
                    }
                    else {
			var arrayOfStrings = level_3[i].split("|");
			if(arrayOfStrings[2]==id2){
				switch (num) {
					case 1:
						region.options[0].value = "*";
						region.options[0].text = "------ Select Country ------";
						document.images["arrow2"].src = "../images/black_arrow.gif";
						document.images["arrow3"].src = "../images/spacer.gif";
						break;
					case 2:
						if (arrayOfStrings[2]=="1069")
						{
							region.options[0].value = "*";
							region.options[0].text = "------ Select Country ------";
						}else {
							region.options[0].value = "*";
							region.options[0].text = "------ Select State ------";
						}
						document.images["arrow3"].src = "../images/black_arrow.gif";
						break;
				}
                            region.options[count] = new Option(arrayOfStrings[1], arrayOfStrings[0]);
                            count++;
                        }
                    }
		}
	
	}

}

function deleteGeo(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);
    if ( fbox.options.length <= 0 ) {
       document.forms['inputform'].region.options[0] = new Option("Any Geographical Relevance","-1","","");
    }
}

function addgphy()
{
    var si1 = document.inputform.region1.selectedIndex;
    var si2 = document.inputform.region2.selectedIndex;
    var si3 = document.inputform.region3.selectedIndex;
    var values = "";
    var gotVal1 = false;
    var gotVal2 = false;
    var showAll = false;
    var focuslist = "";

    var allValues = new Array();
    var j = 2;
    var max = document.forms['inputform']['region1'].length -2;

    for (var i = 0; i < max; i++)
    {
        allValues[i] = document.forms['inputform']['region1'].options[j].value;
        j++;
    }

   if (document.forms['inputform']['region3'].length > 0)
    {
      if (si3 > 0)
      {
        values = document.forms['inputform']['region3'].options[si3].value;

        if (values == "all")
        {
          values = document.forms['inputform']['region2'].options[si2].value;
        }
        
        gotVal1 = true;
        focuslist = "region3";
      }
    }

    if (!gotVal1)
    {

       if (document.forms['inputform']['region2'].length > 0)
       {
           if (si2 > 0)
           {
              values = document.forms['inputform']['region2'].options[si2].value;

              if (values == "all")
              {
              	values = document.forms['inputform']['region1'].options[si1].value;
              }

              gotVal2 = true;
              focuslist = "region2";
           }
       }

       if (!gotVal2)
       {
         if (si1 > 0)
         {
            values = document.forms['inputform']['region1'].options[si1].value;

            if (values == "all")
            {
              showAll = true;
            }

            focuslist = "region1";
         }
       }
    }

    if (showAll)
    {
      for (var i = 0; i < allValues.length; i++)
      {
         displaySelectedAreas(allValues[i]);
      }
    }
    else
    {
    	if (values != "")
        {
          displaySelectedAreas(values);
        }
    }
 if (values != "")
 {
    document.forms['inputform'][focuslist].focus();
 }
}


function displaySelectedAreas(values)
{

     if ( document.forms['inputform'].region.length > 0 &&
         document.forms['inputform'].region.options[0].value == "-1" )
     {
         document.forms['inputform'].region.options[0].value = "";
         document.forms['inputform'].region.options[0].text  = "";
         document.forms['inputform'].region.length = 0;
     }

     for (i=0; i < geography.length; i++)
    {
        var arrayOfStrings = geography[i].split("|");
        if(arrayOfStrings[0]==values){
           region_len = document.forms['inputform'].region.length;
           document.forms['inputform'].region.options[region_len] = new Option(arrayOfStrings[1],arrayOfStrings[0],"","");
         }
    }
}

function addgeo(number)	{
    drop = 'region' + number;
    si = document.forms['inputform'][drop].selectedIndex;
    if ( si <= 0 ) return;
    values = document.forms['inputform'][drop].options[si].value;
    region_len = document.forms['inputform'].region.length;

    if ( document.forms['inputform'].region.length > 0 &&
         document.forms['inputform'].region.options[0].value == "-1" ) {
        document.forms['inputform'].region.options[0].value = "";
        document.forms['inputform'].region.options[0].text  = "";
        document.forms['inputform'].region.length = 0;
    }

    for (i=0; i < geography.length; i++)
    {
        var arrayOfStrings = geography[i].split("|");
        if(arrayOfStrings[0]==values){
            region_len = document.forms['inputform'].region.length;
            document.forms['inputform'].region.options[region_len] = new Option(arrayOfStrings[1],arrayOfStrings[0],"","");
        }
    }
    document.forms['inputform'][drop].focus();
}

/* used by browse by to get the geography id */
function saveGeography(number) {
	drop = 'region' + number;
	si = document.forms['inputform'][drop].selectedIndex;
	values = document.forms['inputform'][drop].options[si].value;
	//alert("values = " + values);
	myName = document.forms['inputform'][drop].options[si].text;
	//alert("myName = " + myName);
        document.forms['inputform'].elements['geographyid'].value = values;
        //alert("set geographyid with values " + values);
        document.forms['inputform'].elements['geography'].value = myName;
        //alert("completed the run, myName = " + myName);
}

/* Language Checkbox stuff */
function alllang() {
	items = document.forms['inputform'].languages.length;
	items-= 1;
	if (document.forms['inputform'].languages[0].checked == true) {
		for (i=1; i<=items; i++) {
			document.forms['inputform'].languages[i].checked = false;
		}
	}
}

function inlang() {
	items = document.forms['inputform'].languages.length;
	items-= 1;
	for (i=1; i<=items; i++) {
		if (document.forms['inputform'].languages[i].checked == true) {
			document.forms['inputform'].languages[0].checked = false;
		}
	}
}

// check that the quotes come in pairs.
function checkQuotes(msg) {
    status = true;
    count = 0;
    for (i =0;i<msg.length;i++ ) {
        if ( msg.substring(i,i+1) == '"' ) {
            count = count+1;
        }
    }
    if ( count%2 > 0 ) {
        return false;
    }
    else {
        return true;
    }
}

function checkNot() {
    mustHaveMsg = document.forms['inputform'].elements['searchtext'].value;
    mustHaveMsg = checkWhitespace(mustHaveMsg);
    mustNotHaveMsg = document.forms['inputform'].elements['notText'].value;
    mustNotHaveMsg = checkWhitespace(mustNotHaveMsg);

    if ( mustHaveMsg != "" ) {
        if (checkQuotes(mustHaveMsg)==false ) {
            alert ("One of the Keywords fields seems to be missing a speech mark. Please check and try again.");
            document.forms['inputform'].elements['searchtext'].focus();
        }
    }
    if ( mustHaveMsg == "") { 
        document.forms['inputform'].elements['searchtext'].value = "";
    }
              
    if ( mustHaveMsg == "" && mustNotHaveMsg != "" ) { 
        alert("You must enter something into the 'Must have' box, when the 'Must NOT have' box is used"); 
		document.forms['inputform'].notText.value =''
        document.forms['inputform'].elements['searchtext'].focus();
    }
}

function checkNotHaveMsg() {
    mustHaveMsg = document.forms['inputform'].elements['searchtext'].value;
    mustHaveMsg = checkWhitespace(mustHaveMsg);
    mustNotHaveMsg = document.forms['inputform'].elements['notText'].value;
    mustNotHaveMsg = checkWhitespace(mustNotHaveMsg);

    if ( mustHaveMsg != "" ) {
        if (checkQuotes(mustHaveMsg)==false ) {
            alert ("One of the Keywords fields seems to be missing a speech mark. Please check and try again.");
            document.forms['inputform'].elements['searchtext'].focus();
        }
    }
    if ( mustHaveMsg == "") { 
        document.forms['inputform'].elements['searchtext'].value = "";
    }
              
    if ( mustHaveMsg == ""  && mustNotHaveMsg != "" ) { 
        alert("You must enter something into the 'Must have' box, before entering text into this box"); 
		document.forms['inputform'].notText.value =''
        document.forms['inputform'].elements['searchtext'].focus();
    }

    if ( mustNotHaveMsg == "") { 
        document.forms['inputform'].elements['notText'].value = "";
    }
    else {
        if (checkQuotes(mustNotHaveMsg)==false ) {
            alert ("One of the Keywords fields seems to be missing a speech mark. Please check and try again.");
            document.forms['inputform'].elements['notText'].focus();
        }
    }
}

function checkWhitespace(inputString) {
  // Removes leading and trailing spaces from the passed string. Also
  // removes consecutive spaces and replaces it with one space.
  var retValue = inputString;
  var ch = retValue.substring(0, 1);
  while (ch == " " || ch == "	") { // Check for spaces or tabs at the beginning of the string
     retValue = retValue.substring(1, retValue.length);
     ch = retValue.substring(0, 1);
  }
  ch = retValue.substring(retValue.length-1, retValue.length);
  while (ch == " " || ch == "	") { // Check for spaces or tabs at the end of the string
     retValue = retValue.substring(0, retValue.length-1);
     ch = retValue.substring(retValue.length-1, retValue.length);
  }

  return retValue; // Return the trimmed string
} // Ends the "trim" function


/* reset each of the select lists for the search screen */
function resetSearch() {
	selectedList(document.forms['inputform'].OverallIndust);
	move(document.forms['inputform'].OverallIndust,document.forms['inputform'].OverallIndustSrc);
	selectedList(document.forms['inputform'].OverallSubject);
	move(document.forms['inputform'].OverallSubject,document.forms['inputform'].OverallSubjectSrc);
	selectedList(document.forms['inputform'].companylist);
	deletesel(document.forms['inputform'].companylist);
	selectedList(document.forms['inputform'].TickerSymbols);
	deletesel(document.forms['inputform'].TickerSymbols);
	selectedList(document.forms['inputform'].region);
	deletesel(document.forms['inputform'].region);
}

/* reset each of the select lists for the directory of contacts screen */
function resetDCSearch() {
	preSelectDirectory();
	deletesel(document.forms['inputform'].OverallIndust);
	preSelectDirectory();
	deletesel(document.forms['inputform'].OverallSubject);
	preSelectDirectory();
	deletesel(document.forms['inputform'].TickerSymbols);
	preSelectDirectory();
	deletesel(document.forms['inputform'].companylist);
	//alert("delete the Region");
	preSelectDirectory();
	deletesel(document.forms['inputform'].region);
}

/* function to get value of last selected Geography list */

function getList()
{
    var si1 = document.forms['inputform']['region1'].selectedIndex;
    var si2 = document.forms['inputform']['region2'].selectedIndex;
    var si3 = document.forms['inputform']['region3'].selectedIndex;
    var row = 1;
    var listOk = false;

   if (si1 > 0)
   {
     if (si3 > 0)
     {
       row = 3;
       listOk = true;
     }

     if (!listOk)
     {
      if (si2 > 0)
      {
       row = 2;
      }
     }
     saveAndSubmit(row);
   }
}

/* function to save geography details and submit the form */
function saveAndSubmit(row){
	saveGeography(row);
	document.forms['inputform'].submit();
}

/* now check out if the search criteria has been set */

function checkKeywords(){
    itm = eval("document.forms['inputform'].searchtext.value");
    if ( itm == "" || itm == null ) { 
         mustHave = 1; 
    }
    else { mustHave = 0; }

    itm = eval("document.forms['inputform'].notText.value");
    if ( itm == "" || itm == null ) { 
         notHave = 1; 
    }
    else { notHave = 0; }
    if ( notHave == 1 && mustHave == 1) { return false; } else { return true; }
}

function checkIndust(){
    itm = eval("document.forms['inputform'].OverallIndust.value");
    if ( itm == "" || itm == null ) { 
         return false; 
    }
    else { return true; }
}

function checkSubject(){
    itm = eval("document.forms['inputform'].OverallSubject.value");
    if ( itm == "" || itm == null ) { 
         return false; 
    }
    else { return true; }
}

function checkcompanylist(){
    itm = eval("document.forms['inputform'].companylist.value");
    if ( itm == "" || itm == null ) { 
         return false; 
    }
    else { return true; }
}

function checkTickerSymbols(){
    itm = eval("document.forms['inputform'].TickerSymbols.value");
    if ( itm == "" || itm == null ) { 
         return false; 
    }
    else { return true; }
}

function checkregion(){
    itm = eval("document.forms['inputform'].region.value");
    if ( itm == "" || itm == null ) { 
         return false; 
    }
    else { return true; }
}

function checkReleases(){
    releases = 0;
    items = document.forms['inputform'].releases.length;
    items--;
    releases = 0;
    for ( i=0; i<=items; i++ ) {
        if (document.forms['inputform'].releases[i].checked == true) {	releases = 1; } 
    }
    if ( releases == 0 ) {
        return false;
    } else {
        return true;
    }
}


function checkLanguages(){
    languages = 0;
    items = document.forms['inputform'].languages.length;
    for ( i=0; i<items; i++ ) {
    	if (document.forms['inputform'].languages[i].value != "alllangs") {
            if (document.forms['inputform'].languages[i].checked == true) { languages = 1; } 
        }
    }
    if ( languages == 0 ) {
        return false;
    } else {
        return true;
    }
}


/* check mustHave, notHave, OverallIndust, OverallSubject, companylist,
TickerSymbols, region, languages or alllangs, releases
*/
function checkSearchCriteria(){
    searchflag = 0;

    if ( checkKeywords() || checkIndust() || checkSubject() || checkcompanylist()
      || checkTickerSymbols() || checkregion() || checkReleases() || checkLanguages() ) {
        searchflag = 0;
    }
    else {
        searchflag = 1;
    }

    if ( searchflag == 1 ) {
         alert("You must enter a search criteria");
    }

    if ( searchflag == 1 ) {
         return false; 
    }
    else { return true; }
}

