function is_equal(string1, string2){
  if(string1.length != string2.length){ return false; }
  for(i=0;i<string1.length;i++){
    if(string1[i] != string2[i]){ return false; }
  }
  return true;
}
  

/* check_punct takes a string, and two characters to be checked for.  These characters
  correspond to the opening and closing delimiters, such as {}, [], "", etc.
  the function increments each "open" occurrence, and decrements on a "close".
  A return value of 0 indicates the delimiters are sensible.
  A return value of >0 indicates there is one or more missing from the end.
  A return value of -999 indicates a situation like )( exists.
*/

function check_punct(s,open,close){
   
  /*
  first check for equality of delimiters.  If equal, then
  expression makes sense if there is an even number of them.
  open and close should be chars, in the sense that they are single symbols, not combinations.
  */

  if(open == close){  
    counter=0;
    for(i=0;i<s.length;i++){
      c = s.charAt(i);
      if (c == open){ counter++; } 
    }
      return (counter%2);       
  }else{    
	counter = 0;
    for(i=0,c = s.charAt(0);i<s.length;i++,c = s.charAt(i)){
      if (c == open){ counter++; }
      else if(c == close){ counter --; }
      
      if (counter < 0){ return (-999);}            
    }
    return counter;
  }
}

/*The following arrays and the writeMenu function are used for dynamic menu creation.  The orbis search
bar uses this functionality to determine based on radio button choice which elements it should display.*/
OregonCities = new Array("Select...",
"Adair Village", "Adams", "Adrian", "Albany", "Amity", "Antelope", "Arlington", "Ashland",
"Astoria", "Athena", "Aumsville", "Aurora", "Baker City", "Bandon", "Banks", "Barlow", "Bay City", "Beaverton",
"Bend", "Boardman", "Bonanza", "Brookings", "Brownsville", "Burns", "Butte Falls", "Canby", "Cannon Beach",
"Canyon City", "Canyonville", "Carlton", "Cascade Locks", "Cave Junction", "Central Point", "Chiloquin",
"Clatskanie", "Coburg", "Columbia City", "Condon", "Coos Bay", "Coquille", "Cornelius", "Corvallis",
"Cottage Grove", "Cove", "Creswell", "Culver", "Dallas", "Dayton", "Dayville", "Depoe Bay", "Detroit", "Donald",
"Drain", "Dufur", "Dundee", "Dunes City", "Durham", "Eagle Point", "Echo", "Elgin", "Elkton", "Enterprise",
"Estacada", "Eugene", "Fairview", "Falls City", "Florence", "Forest Grove", "Fossil", "Garibaldi", "Gaston",
"Gates", "Gearhart", "Gervais", "Gladstone", "Glendale", "Gold Beach", "Gold Hill", "Granite", "Grants Pass",
"Grass Valley", "Green Horn", "Gresham", "Haines", "Halfway", "Halsey", "Happy Valley", "Harrisburg", "Helix",
"Heppner", "Hermiston", "Hillsboro", "Hines", "Hood River", "Hubbard", "Huntington", "Idanha", "Imbler",
"Independence", "Ione", "Irrigon", "Island City", "Jacksonville", "Jefferson", "John Day", "Johnson City", "Jordan Valley","Joseph", "Junction City", "Keizer", "King City", "Klamath Falls", "Lafayette", "La Grande", "Lake Oswego", "Lakeside","Lakeview", "Lebanon", "Lexington", "Lincoln City", "Lonerock", "Long Creek", "Lostine", "Lowell", "Lyons",
"Madras", "Malin", "Manzanita", "Maupin", "Maywood Park", "McMinnville", "Medford", "Merrill", "Metolius",
"Mill City", "Millersburg", "Milton-Freewater", "Milwaukie", "Mitchell", "Molalla", "Monmouth", "Monroe",
"Monument", "Moro", "Mosier", "Mt. Angel", "Mt. Vernon", "Myrtle Creek", "Myrtle Point", "Nehalem", "Newberg",
"Newport", "North Bend", "North Plains", "North Powder", "Nyssa", "Oakland", "Oakridge", "Ontario", "Oregon City",
"Paisley", "Pendleton", "Philomath", "Phoenix", "Pilot Rock", "Portland", "Port Orford", "Powers",
"Prairie City", "Prescott", "Prineville", "Rainier", "Redmond", "Reedsport", "Richland", "Riddle", "Rivergrove",
"Rockaway Beach", "Rogue River", "Roseburg", "Rufus", "Salem", "Sandy", "Scappoose", "Scio", "Scotts Mills", "Seaside",
"Seneca", "Shady Cove", "Shaniko", "Sheridan", "Sherwood", "Siletz", "Silverton", "Sisters", "Sodaville", "Spray",
"Springfield", "St. Helens", "St. Paul", "Stanfield", "Stayton", "Sublimity", "Summerville", "Sumpter", "Sutherlin",
"Sweet Home", "Talent", "Tangent", "The Dalles", "Tigard", "Tillamook", "Toledo", "Troutdale", "Tualatin", "Turner",
"Ukiah", "Umatilla", "Union", "Unity", "Vale", "Veneta", "Vernonia", "Waldport", "Wallowa", "Warrenton",
"Wasco", "Waterloo", "West Linn", "Westfir", "Weston", "Wheeler", "Willamina", "Wilsonville", "Winston",
"Woodburn", "Wood Village", "Yachats", "Yamhill", "Yoncalla" );

OregonCounties = new Array("Select...", "Baker", "Benton", "Clackamas", "Clatsop", "Columbia", "Coos", "Crook", "Curry",
 "Deschutes", "Douglas", "Gilliam", "Grant", "Harney", "Hood River", "Jackson", "Jefferson", "Josephine", 
 "Klamath", "Lake", "Lane", "Lincoln", "Linn", "Malheur", "Marion", "Morrow", "Multnomah", "Polk", "Sherman",
 "Tillamook", "Umatilla", "Union", "Wallowa", "Wasco", "Washington", "Wheeler", "Yamhill");
 
WashCities = new Array("Select...", "Aberdeen","Airway Heights","Albion","Algona","Almira","Anacortes",
"Arlington","Asotin","Auburn","Bainbridge Island","Battle Ground","Beaux Arts Village",
"Bellevue","Bellingham","Benton City","Bingen","Black Diamond","Blaine","Bonney Lake",
"Bothell","Bremerton","Brewster","Bridgeport","Brier","Buckley","Bucoda","Burien",
"Burlington","Camas","Carbonado","Carnation","Cashmere","Castle Rock","Cathlamet","Centralia",
"Chehalis","Chelan","Cheney","Chewelah","Clarkston","Cle Elum","Clyde Hill","Colfax","College Place",
"Colton","Colville","Conconully","Concrete","Connell","Cosmopolis","Coulee City","Coulee Dam",
"Coupeville","Covington","Creston","Cusick","Darrington","Davenport","Dayton","Deer Park",
"Des Moines","DuPont","Duvall","East Wenatchee","Eatonville","Edgewood","Edmonds","Electric City",
"Ellensburg","Elma","Elmer City","Endicott","Entiat","Enumclaw","Ephrata","Everett","Everson",
"Fairfield","Farmington","Federal Way","Ferndale","Fife","Fircrest","Forks","Friday Harbor",
"Garfield","George","Gig Harbor","Gold Bar","Goldendale","Grand Coulee","Grandview",
"Granger","Granite Falls","Hamilton","Harrah","Harrington","Hartline","Hatton",
"Hoquiam","Hunts Point","Ilwaco","Index","Ione","Issaquah","Kahlotus","Kalama",
"Kelso","Kenmore","Kennewick","Kent","Kettle Falls","Kirkland","Kittitas","Krupp",
"La Center","La Conner","Lacey","LaCrosse","Lake Forest Park","Lake Stevens","Lakewood",
"Lamont","Langley","Latah","Leavenworth","Liberty Lake","Lind","Long Beach","Longview",
"Lyman","Lynden","Lynnwood","Mabton","Malden","Mansfield","Maple Valley","Marcus",
"Marysville","Mattawa","McCleary","Medical Lake","Medina","Mercer Island","Mesa",
"Metaline","Metaline Falls","Mill Creek","Millwood","Milton","Monroe","Montesano",
"Morton","Moses Lake","Mossyrock","Mount Vernon","Mountlake Terrace","Moxee","Mukilteo",
"Naches","Napavine","Nespelem","Newcastle","Newport","Nooksack","Normandy Park",
"North Bend","North Bonneville","Northport","Oak Harbor","Oakesdale","Oakville",
"Ocean Shores","Odessa","Okanogan","Olympia","Omak","Oroville","Orting","Othello",
"Pacific","Palouse","Pasco","Pateros","Pe Ell","Pomeroy","Port Angeles","Port Orchard",
"Port Townsend","Poulsbo","Prescott","Prosser","Pullman","Puyallup","Quincy","Rainier",
"Raymond","Reardan","Redmond","Renton","Republic","Richland","Ridgefield","Ritzville",
"Riverside","Rock Island","Rockford","Rosalia","Roslyn","Roy","Royal City","Ruston",
"Sammamish","SeaTac","Seattle","Sedro-Woolley","Selah","Sequim","Shelton","Shoreline",
"Skykomish","Snohomish","Snoqualmie","Soap Lake","South Bend","South Cle Elum","South Prairie",
"Spangle","Spokane","Spokane Valley","Sprague","Springdale","St. John","Stanwood",
"Starbuck","Steilacoom","Stevenson","Sultan","Sumas","Sumner","Sunnyside","Tacoma",
"Tekoa","Tenino","Tieton","Toledo","Tonasket","Toppenish","Tukwila","Tumwater",
"Twisp","Union Gap","Uniontown","University Place","Vader","Vancouver","Waitsburg",
"Walla Walla","Wapato","Warden","Washougal","Washtucna","Waterville","Waverly","Wenatchee",
"West Richland","Westport","White Salmon","Wilbur","Wilkeson","Wilson Creek","Winlock",
"Winthrop","Woodinville","Woodland","Woodway","Yacolt","Yakima","Yarrow Point","Yelm",
"Zillah");


WashCounties = new Array("Select...", "Adams County","Asotin County","Benton County",
"Chelan County","Clallam County","Clark County","Columbia County","Cowlitz County",
"Douglas County","Ferry County","Franklin County","Garfield County","Grant County",
"Grays Harbor County","Island County","Jefferson County","King County","Kitsap County",
"Kittitas County","Klickitat County","Lewis County","Lincoln County","Mason County",
"Okanogan County","Pacific County","Pend Oreille County","Pierce County","San Juan County",
"Skagit County","Skamania County","Snohomish County","Spokane County","Stevens County",
"Thurston County","Wahkiakum County","Walla Walla County","Whatcom County","Whitman County",
"Yakima County");

// Used to hold an Array-of-Array's to index the Counties and Cities of the selected state
choices = new Array(OregonCities, OregonCounties, WashCities, WashCounties);

function writeMenu(){
  // 0 = Oregon in the choices index array where 
  // 2 = Washington in the choices index array
  state = document.stateForm.state[0].checked ? 0 : 2;
  // Once the state has been defined then find if it is a city or county
  selection = document.selectionForm.selection[0].checked ? 0 : 1;
  // Index of categories
  i = state + selection;
  output = document.dropform.elements[0];
  output.options.selectedIndex = 0;
  output.options.length = choices[i].length;
  for(j=0;j<output.options.length;j++){
    output.options[j].text = choices[i][j];                              
    output.options[j].value = choices[i][j];
  }
}

function checkme(s){
  qt = /\"\"/;
  wqt = /\S\"\"\S/;
  pr = /\(\)/;
  wpr = /\(\w+\)/;
  borp = /\(or\)/;
  borb = /^or/;
  bandp = /\(and\)/;
  bandb = /^and/;

  if(qt.test(s)&&(!(wqt.test(s)))){alert("Please enter a term within your quotes.\ne.g. \"oregon\", not \"\".");return false;} 
  else if(qt.test(s)&&(wqt.test(s))){alert("Please enter spaces between quoted search words.\ne.g. \"oregon\" \"water\", not \"oregon\"\"water\".");return false;} 
  else if(pr.test(s)&&!(wpr.test(s))){alert("You have entered empty parentheses in your search.");return false;}
  else if(s == ""){alert("Please enter a search term or terms and try again.");return false;}

  if(s == "and"|| s == "or"  || s == "(or)" || s == "(and)"||bandb.test(s)){
    alert("The boolean term you have entered requires additional words.\ne.g. \"fish and oregon\", or \"fish or oregon\".");
    return false;
  }

  else if(borp.test(s)  || bandp.test(s)){
    alert("There is a boolean operator in your phrase that cannot be understood.\ne.g. \"and\" oregon, or fish \"or\".");return false;}
	else if(check_punct(s,"(",")")>0){alert("You appear to be missing an end parenthesis.\nPlease revise your search.");return false;}   
	else if(check_punct(s,"(",")")<0){alert("Your parenthesis are not matched.\nPlease revise your search.");return false;}
	else if(check_punct(s,"\"","\"")>0){alert("You appear to be missing an end quote.\nPlease revise your search.");return false;}
	else if(check_punct(s,"\"","\"")<0){alert("Your search has a set of empty quotes.\nPlease revise your search.");return false;}
	return true;
  }

function Openpage(page) {
  OpenWin = this.open(page, "geohelp", "toolbar=no,location=no,status=no,menubar=no,directories=no,scrollbars=yes,resizable=no,width=423,height=488");
}

function check(){
	document.stateForm.state[0].checked=true;
	document.stateForm.state[1].checked=false;
	document.selectionForm.selection[0].checked=true;
	document.selectionForm.selection[1].checked=false;
	writeMenu();
}

function clear_localdata(){
	document.search[0].limit.selectedIndex=0;
	document.search[0].arg.value="";
	document.search[0].glis.selectedIndex=0;
	document.search[0].geog.selectedIndex=0;
	document.search[0].state[0].checked = true;
	document.search[0].state[1].checked = false;
	document.search[0].state[2].checked = false;
}

function browse(){
	if(document.search[1].glis.selectedIndex==0 && document.search[1].geog.selectedIndex==0){
		alert("Please select a topic or geographic level and try again.");           
		return false;
	}            
	return true;
}

function clear_browse(){
    document.search[1].glis.selectedIndex=0;
	document.search[1].geog.selectedIndex=0;
	document.search[1].state[0].checked = true;
	document.search[1].state[1].checked = false;
	document.search[1].state[2].checked = false;
}    

function clear_orbis(){
	document.stateForm.state[0].checked=true;
	document.stateForm.state[1].checked=false;
	document.selectionForm.selection[0].checked=true;
	document.selectionForm.selection[1].checked=false;
	writeMenu();
}

function orbis_selection(){
	sel_obj = document.dropform.elements[0];
	state = document.stateForm.state;
	cit=document.selectionForm.selection;

	if(sel_obj.selectedIndex == 0){
		if(cit[0].checked){
			result = "city and try again.";
		}else{
			result = "county and try again.";
		}
		alert("Please select a "+ result);
		return false;
	}else{
	
	// Select the value selected in the dropdown
	result = sel_obj.options[sel_obj.selectedIndex].value;

	// Add the options for county and state searches
	result += cit[0].checked ? "" : " County";
	result += state[0].checked ? "+and+(\"Or\"+OR+\"Oregon\")" : "+and+(\"Wash\"+OR+\"Washington\")";
	document.dropform.q.value = "su:" + result;
	return true;
	}
}

function is_equal(string1, string2){
    if(string1.length != string2.length){ return false; }
    for(i=0;i<string1.length;i++){
        if(string1[i] != string2[i]){ return false; }
    }
    return true;
}
    
    //check_punct takes a string, and two characters to be checked for.  These characters
    //correspond to the opening and closing delimiters, such as {}, [], "", etc.
    //the function increments each "open" occurrence, and decrements on a "close".
    //A return value of 0 indicates the delimiters are sensible.
    //A return value of >0 indicates there is one or more missing from the end.
    //A return value of -999 indicates a situation like )( exists.
function check_punct(s,open,close){
    //alert("checking "+s+" for "+open+" and "+close);
    //first check for equality of delimiters.  If equal, then
    //expression makes sense if there is an even number of them.
    //open and close should be chars, in the sense that they are single symbols, not combinations.

    if(open == close){  
       counter=0;
       for(i=0;i<s.length;i++){
            c = s.charAt(i);
            
            if (c == open){ 
                
                counter++; 
                if(i != 0){
                    if(s.charAt(i-1) == c){
                        return -999;
                    }
                } 
            }
       }
       return (counter%2);       
    }
    
    else{    
        counter = 0;
        for(i=0,c = s.charAt(0);i<s.length;i++,c = s.charAt(i)){
            if (c == open){
                counter++;
            }else if(c == close){
                counter --;
            }
            if (counter < 0){ return (-999);}            
        }
        return counter;
    }
}
function checkme(s){
    result = false;
    qt = /\"\"/;
    wqt = /\S\"\"\S/;
    pr = /\(\)/;
    wpr = /\(\w+\)/;
    borp = /\(or\)/;
    //borb = /^or/;
    bandp = /\(and\)/;
   // bandb = /^and/;

    if(qt.test(s)&&(!(wqt.test(s)))){alert("Please enter a term within your quotes.\ne.g. \"oregon\", not \"\".");return false;} 
    else if(qt.test(s)&&(wqt.test(s))){alert("Please enter spaces between quoted search words.\ne.g. \"oregon\" \"water\", not \"oregon\"\"water\".");return false;}    
    else if(pr.test(s)&&!(wpr.test(s))){alert("You have entered empty parentheses in your search.");return false;}
    else if(s == ""){alert("Please enter a search term or terms and try again.");}
        if(s == "and"|| s == "or" || s == "(or)" || s == "(and)"){
        alert("The boolean term you have entered requires additional words.\ne.g. \"fish and oregon\", or \"fish or oregon\".");return false;
    }
    else if(borp.test(s) || bandp.test(s)){
        alert("There is a boolean operator in your phrase that cannot be understood.\ne.g. \"and\" oregon, or fish \"or\".");return false;}
    else if(check_punct(s,"(",")")>0){alert("You appear to be missing an end parenthesis.\nPlease revise your search.");}   
    else if(check_punct(s,"(",")")<0){alert("Your parenthesis are not matched.\nPlease revise your search.");}
    else if(check_punct(s,"\"","\"")>0){alert("You appear to be missing an end quote.\nPlease revise your search.");}
    else if(check_punct(s,"\"","\"")<0){alert("Your search has a set of empty quotes.\nPlease revise your search.");}
    else{result = true;}
    //alert(result);
    return result;
}
  function addOldWord(){
    if (checkme(document.search.arg.value)){
        document.search.arg.value = "<?php print $arg ?> "+document.search.arg.value;
        //alert(document.search.arg.value);
    }
    else{return false;}
    return true;
        
 }