//-----------------------------------------------------------------------------

var continents =
[ 
  "Africa",
  "Asia",
  "Europe",
  "Latin America",
  "Northern America",
  "Oceania"
];

var regionsOfContinent =
{
  "Africa" :
  [
    "Africa, Eastern",
    "Africa, Middle",
    "Africa, Northern",
    "Africa, Southern",
    "Africa, Western"
  ],
  "Asia" :
  [
    "Asia, Eastern",
    "Asia, South-central",
    "Asia, South-eastern",
    "Asia, Western"
  ],
  "Europe" :
  [
    "Europe, Eastern",
    "Europe, Northern",
    "Europe, Southern",
    "Europe, Western"
  ],
  "Latin America" :
  [
    "Caribbean",
    "America, Central",
    "America, South"
  ],
  "Northern America" :
  [
    "America, Northern"
  ],
  "Oceania" :
  [
    "Australia and New Zealand",
    "Melanesia",
    "Micronesia",
    "Polynesia"
  ]
};

var countriesOfRegion =
{
  "Africa, Eastern" :
  [
    "Burundi",
    "Comoros",
    "Djibouti",
    "Eritrea",
    "Ethiopia",
    "Kenya",
    "Madagascar",
    "Malawi",
    "Mauritius",
    "Mayotte",
    "Mozambique",
    "Reunion",
    "Rwanda",
    "Somalia",
    "Tanzania",
    "Uganda",
    "Zambia",
    "Zimbabwe"
  ],
  "Africa, Middle" :
  [
    "Angola",
    "Cameroon",
    "Central African Republic",
    "Chad",
    "Democratic Republic of Congo (Zaire)",
    "Equatorial Guinea",
    "Gabon",
    "Republic of the Congo"
  ],
  "Africa, Northern" :
  [
    "Algeria",
    "Egypt",
    "Libya",
    "Morocco",
    "Sudan",
    "Tunisia",
    "Western Sahara"
  ],
  "Africa, Southern" :
  [
    "Botswana",
    "Lesotho",
    "Namibia",
    "South Africa",
    "Swaziland"
  ],
  "Africa, Western" :
  [
    "Benin",
    "Burkina Faso",
    "Cape Verde",
    "Cote d`Ivoire (Ivory Coast)",
    "Ghana",
    "Guinea",
    "Guinea-Bissau",
    "Liberia",
    "Mali",
    "Mauritania",
    "Niger",
    "Nigeria",
    "Senegal",
    "Sierra Leone",
    "The Gambia",
    "Togo"
  ],
  "Asia, Eastern" :
  [
    "China",
    "Hong Kong",
    "Japan",
    "Korea, North",
    "Korea, South",
    "Macau",
    "Mongolia",
    "Taiwan"
  ],
  "Asia, South-central" :
  [
    "Afghanistan",
    "Bangladesh",
    "Bhutan",
    "India",
    "Iran",
    "Kazakhstan",
    "Kyrgyzstan",
    "Maldives",
    "Nepal",
    "Pakistan",
    "Sri Lanka",
    "Tajikistan",
    "Turkmenistan",
    "Uzbekistan"
  ],
  "Asia, South-eastern" :
  [
    "Brunei",
    "Cambodia",
    "East Timor",
    "Indonesia",
    "Laos",
    "Malaysia",
    "Myanmar (Burma)",
    "Philippines",
    "Singapore",
    "Thailand",
    "Vietnam"
  ],
  "Asia, Western" :
  [
    "Armenia",
    "Azerbaijan",
    "Bahrain",
    "Cyprus",
    "Georgia",
    "Iraq",
    "Israel",
    "Jordan",
    "Kuwait",
    "Lebanon",
    "Oman",
    "Qatar",
    "Saudi Arabia",
    "Syria",
    "Turkey",
    "United Arab Emirates",
    "Yemen"
  ],
  "Europe, Eastern" :
  [
    "Belarus",
    "Bulgaria",
    "Czech Republic",
    "Hungary",
    "Poland",
    "Moldova",
    "Romania",
    "Russia",
    "Slovakia",
    "Ukraine"
  ],
  "Europe, Northern" :
  [
    "Denmark",
    "Estonia",
    "Finland",
    "Iceland",
    "Ireland",
    "Latvia",
    "Lithuania",
    "Norway",
    "Sweden",
    "United Kingdom"
  ],
  "Europe, Southern" :
  [
    "Albania",
    "Andorra",
    "Bosnia-Herzegovina",
    "Croatia",
    "Greece",
    "Italy",
    "Macedonia",
    "Malta",
    "Portugal",
    "San Marino",
    "Serbia",
    "Slovenia",
    "Spain",
    "Yugoslavia"
  ],
  "Europe, Western" :
  [
    "Austria",
    "Belgium",
    "France",
    "Germany",
    "Liechtenstein",
    "Luxembourg",
    "Monaco",
    "Netherlands",
    "Switzerland"
  ],
  "Caribbean" :
  [
    "Dominica",
    "Guadeloupe",
    "Haiti",
    "Jamaica",
    "Martinique",
    "Netherlands Antilles",
    "Puerto Rico",
    "Saint Lucia",
    "The Bahamas",
    "Trinidad and Tobago",
    "West Indies"
  ],
  "America, Central" :
  [
    "Belize",
    "Costa Rica",
    "El Salvador",
    "Guatemala",
    "Honduras",
    "Mexico",
    "Nicaragua",
    "Panama"
  ],
  "America, South" :
  [
    "Argentina",
    "Bolivia",
    "Brazil",
    "Chile",
    "Colombia",
    "Ecuador",
    "French Guiana",
    "Guyana",
    "Paraguay",
    "Peru",
    "Suriname",
    "Uruguay",
    "Venezuela"
  ],
  "America, Northern" :
  [
    "Canada",
    "Greenland",
    "United States of America"
  ],
  "Australia and New Zealand" :
  [
    "Australia",
    "New Zealand",
    "Norfolk Island"
  ],
  "Melanesia" :
  [
    "Fiji",
    "New Caledonia",
    "Papua New Guinea",
    "Solomon Islands",
    "Vanuatu"
  ],
  "Micronesia" :
  [
    "Federated States of Micronesia",
    "Kiribati",
    "Nauru",
    "Palau"
  ],
  "Polynesia" :
  [
    "Cook Islands",
    "French Polynesia",
    "Niue",
    "Pitcairn Island",
    "Tonga",
    "Tuvalu"
  ]
};

//-----------------------------------------------------------------------------

function isAjax() { return langList.length == 0; }

var countries = [];
var regions = [];
var continentOfRegion = [];
var continentOfCountry = [];
var regionOfCountry = [];
var countriesOfContinent = [];

for (var continentIndex in continents) {
  var continent = continents[continentIndex];
  countriesOfContinent[continent] = [];
  for (var regionIndex in regionsOfContinent[continent]) {
    var region = regionsOfContinent[continent][regionIndex];
    regions.push(region);
    continentOfRegion[region] = continent;
    for (var countryIndex in countriesOfRegion[region]) {
      var country = countriesOfRegion[region][countryIndex];
      countries.push(country);
      continentOfCountry[country] = continent;
      regionOfCountry   [country] = region;
      countriesOfContinent[continent].push(country);
    }
  }
  countriesOfContinent[continent].sort();
}
regions.sort();
countries.sort();

var currentContinent = "(all)";
var currentRegion    = "(all)";
var currentCountry   = "(all)";

function createMenu( menu, locations, selectedLocation ) {
  menu.length = 0;
    var location = "(all)";
    var newItem = document.createElement( "option" );
    newItem.appendChild( document.createTextNode( location ) );
    newItem.setAttribute( 'value', location );
    menu.appendChild( newItem );
  for ( var locationIndex in locations ) {
    var location = locations[locationIndex];
    var newItem = document.createElement( "option" );
    newItem.appendChild( document.createTextNode( location ) );
    newItem.setAttribute( 'value', location );
    menu.appendChild( newItem );
  }
    var locationIndex = -1;
    var location = "(all)";
    if ( location == selectedLocation ) {
      menu.selectedIndex = locationIndex*1 + 1;
    }
  for ( var locationIndex in locations ) {
    var location = locations[locationIndex];
    if ( location == selectedLocation ) {
      menu.selectedIndex = locationIndex*1 + 1;
    }
  }
}

function makeContinentMenu( selectedLocation ) {
  var menu = document.getElementById( "continentMenu" );
  var locations;
  locations = continents;
  createMenu( menu, locations, selectedLocation );
}

function makeRegionMenu( selectedLocation ) {
  var menu = document.getElementById( "regionMenu" );
  var locations;
  if ( currentContinent == '(all)' ) {
    locations = regions;
  } else {
    locations = regionsOfContinent[currentContinent];
  }
  createMenu( menu, locations, selectedLocation );
}

function makeCountryMenu( selectedLocation ) {
  var menu = document.getElementById( "countryMenu" );
  var locations;
  if ( currentRegion == "(all)" ) {
    if (currentContinent == "(all)") {
      locations = countries;
    } else {
      locations = countriesOfContinent[currentContinent];
    }
  } else {
    locations = countriesOfRegion[currentRegion];
  }
  createMenu( menu, locations, selectedLocation );
}

function makeMenus () {
  makeContinentMenu( currentContinent );
  makeRegionMenu(    currentRegion    );
  makeCountryMenu(   currentCountry   );

}

//-----------------------------------------------------------------------------

function cookQueryLoc( str ) {
  var strNew = str.toLowerCase();
  strNew = strNew.replace(/ /g,"");
  strNew = strNew.replace(/,/g,"");
  strNew = strNew.replace(/-/g,"");
  strNew = strNew.replace(/`/g,"");
  strNew = strNew.replace(/\(/g,"");
  strNew = strNew.replace(/\)/g,"");
  return strNew;
}

function setQueryLoc () {
  var queryLocRaw
           = currentCountry   != '(all)' ? currentCountry : (
             currentRegion    != '(all)' ? currentRegion : (
             currentContinent != '(all)' ? currentContinent : 'world'));
  queryLoc = cookQueryLoc( queryLocRaw );
}

function loadNewWindow() {
  var htmlFileName = "index_" +
                     queryLoc +
                     ( queryVariants == 1 ? "_all" : "" ) +
                     ".htm";
  window.location = htmlFileName;
}

function selectNewCountry( selectedLocation ) {
  var oldLocation = currentCountry;
  currentCountry   = selectedLocation;
  if ( currentCountry != "(all)" ) {
    currentContinent = continentOfCountry[currentCountry];
    currentRegion    = regionOfCountry[currentCountry];
  }
  makeMenus();
  setQueryLoc();
  if ( ! isAjax() ) {
    if ( selectedLocation != oldLocation ) {
      loadNewWindow();
    }
  }
}

function selectNewRegion( selectedLocation ) {
  var oldLocation = currentRegion;
  currentRegion = selectedLocation;
  currentCountry = '(all)';
  if ( currentRegion != "(all)" ) {
    currentContinent = continentOfRegion[currentRegion];
  }
  makeMenus();
  setQueryLoc();
  if ( ! isAjax() ) {
    if ( selectedLocation != oldLocation ) {
      loadNewWindow();
    }
  }
}

function selectNewContinent( selectedLocation ) {
  var oldLocation = currentContinent;
  currentContinent = selectedLocation;
  currentRegion  = '(all)';
  currentCountry = '(all)';
  makeMenus();
  setQueryLoc();
  if ( ! isAjax() ) {
    if ( selectedLocation != oldLocation ) {
      loadNewWindow();
    }
  }
}

function initializeLocation () {

  for ( var countryIndex in countries ) {
    if ( currentLocation == countries[countryIndex] ) {
      currentCountry = currentLocation;
      currentRegion    = regionOfCountry[currentCountry];
      currentContinent = continentOfCountry[currentCountry];
    }
  }

  for ( var regionIndex in regions ) {
    if ( currentLocation == regions[regionIndex] ) {
      currentRegion = currentLocation;
      currentContinent = continentOfRegion[currentRegion];
    }
  }

  for ( var continentIndex in continents ) {
    if ( currentLocation == continents[continentIndex] ) {
      currentContinent = currentLocation;
    }
  }

  setQueryLoc();

  makeMenus();

}

//-----------------------------------------------------------------------------

var queryNumInc = "25";
var queryLang = "";
var queryNum = queryNumInc;
var queryLoc = "world";
var queryMatch = "beginning";
//var queryVariants = 0;

var queryLangAjaxed = "";

function setQueryMatch( val ) {
  queryMatch = val;
}


function toggleQueryVariants() {
  if ( isAjax() ) {
    queryVariants = 1 - queryVariants;
  } else {
    //---Restore to pre-changed state.
    document.getElementById( 'searchVariants' ).checked = (queryVariants == 1);
    queryVariants = 1 - queryVariants;
    loadNewWindow();
  }
}

function addCommas( nStr ) {
  nStr += '';
  x = nStr.split('.');
  x1 = x[0];
  x2 = x.length > 1 ? '.' + x[1] : '';
  var rgx = /(\d+)(\d{3})/;
  while (rgx.test(x1)) {
    x1 = x1.replace(rgx, '$1' + ',' + '$2');
  }
  return x1 + x2;
}

function writeSearchSuggest( lines ) {
  var ss = document.getElementById( 'searchSuggest' );
  ss.innerHTML = '<div><b>Top Movie Matches:</b><' + '/div>';
  ss.innerHTML += '<div style="font:8px arial,sans-serif">&nbsp;</div>';
  var isMore = false;
  var isLess = false;
  for ( i=0; i < lines.length; i++ ) {
    //---Build element string.  This is cleaner using the DOM, but
    //---IE doesn't support dynamically added attributes.
    if ( lines[i] == "(none)" ) {
      ss.innerHTML += '<div>(none)<' + '/div>';
    } else {
      if ( ! ( lines[i].substr(0,1) == '#' ||
               lines[i].substr(0,1) == '<' ||
               ( lines[i].indexOf("	") == -1 && lines[i] != "..." ) ) ) {
        if (lines[i] != "...") {
          var langName    = lines[i].split("	")[0];
          var population  = lines[i].split("	")[1];
          var program     = lines[i].split("	")[2];
          var programName = lines[i].split("	")[3];
          var link = isAjax() ?
              ( 'http://worldlanguagemovies.com/program/' + program ) :
              ( program + 't_sm.htm' );
          var linkText = langName +
                         '&nbsp;&nbsp;&nbsp;&nbsp;<i>' +
                         programName +
                         '<'+'/i>';
          ss.innerHTML += '<div><a href="' +
                          link + '"' + // ' style="font-size:12px"' +
                          '>' + linkText +
                          '</' + 'a></' + 'div>';
        }
        if (lines[i] == "...") {
          isMore = true;
        }
      }
    }
  }
  ss.innerHTML += '<div>&nbsp;</div>';
  if ( isMore ) {
    ss.innerHTML +=
      '<input type="button" ' +
      '       onclick="javascript:extendSearchSuggest();" ' +
      '       value="+">';
  }
  isLess = parseInt(queryNum) > parseInt(queryNumInc);
  if ( isLess ) {
    ss.innerHTML +=
      '<input type="button" ' +
      '       onclick="javascript:reduceSearchSuggest();" ' +
      '       value="-">';
  }
  ss.innerHTML += '<div>&nbsp;</div>';
}

function handledoSearchSuggest() {
  if (searchReq.readyState == 4) {
    var lines = searchReq.responseText.split( '\n' );
    writeSearchSuggest( lines );
  }
  queryLang = escape( document.getElementById( 'searchBox' ).value );
  if ( queryLang != queryLangAjaxed ) {

    doSearchSuggestAjax();
  }
}

function extendSearchSuggest() {
  queryNum = "" + ( parseInt(queryNum) + parseInt(queryNumInc) );
  doSearchSuggest();
}

function reduceSearchSuggest() {
  if ( parseInt(queryNum) > parseInt(queryNumInc) ) {
    queryNum = "" + ( parseInt(queryNum) - parseInt(queryNumInc) );
    doSearchSuggest();
  }
}

function langQuery( langList ) {

  var langListNew = [];
  var langListInd;
  var isMore = false;

  var regExp = new RegExp(
     ( queryMatch == "beginning" ? "^" : "" ) + queryLang, "i" );

  for ( langListInd in langList ) {
    var langListElt = langList[langListInd];
    var language = langListElt.split( "	" )[0];

    if ( regExp.test( language ) ) {
      if ( langListNew.length < queryNum ) {
        langListNew.push( langListElt );
      } else {
        isMore = true;
        break;
      }
    }
  }

  if ( isMore ) {
    langListNew.push( "..." );
    langListNew.push( "" );
  }

  if ( langListNew.length == 0 ) {
    langListNew.push( "(none)" );
    langListNew.push( "" );
  }

  return langListNew;

}

function doSearchSuggestAjax() {
  if ( searchReq.readyState == 4 || searchReq.readyState == 0 ) {
    queryLang = escape( document.getElementById( 'searchBox' ).value );
    var queryUrl = '/cgi-bin/langquery?'
      + "location="    + queryLoc      + "&"
      + "pattern="     + queryLang     + "&"
      + "maxlines="    + queryNum      + "&"
      + "matchtype="   + queryMatch    + "&"
      + "variants="    + queryVariants + "&"
      + "unavailable=" + "0";
    searchReq.open( "GET", queryUrl, true );
    searchReq.onreadystatechange = handledoSearchSuggest;
    searchReq.send( null );
    queryLangAjaxed = queryLang;
  }
}

function doSearchSuggestNoAjax() {
  queryLang = escape( document.getElementById( 'searchBox' ).value );
  writeSearchSuggest( langQuery ( langList ) );
}

function doSearchSuggest() {
  if ( isAjax() ) {
    doSearchSuggestAjax();
  } else {
    doSearchSuggestNoAjax();
  }
}

initializeLocation();

document.getElementById( 'searchVariants' ).checked = (queryVariants == 1);

document.getElementById("searchBoxForm").setAttribute("autocomplete","off");

var searchReq = null;
if ( window.XMLHttpRequest ) {
  searchReq = new XMLHttpRequest();
} else if(window.ActiveXObject) {
  try { searchReq = new ActiveXObject("Msxml2.XMLHTTP.6.0"); }
    catch (e) {}
  try { searchReq = new ActiveXObject("Msxml2.XMLHTTP.3.0"); }
    catch (e) {}
  try { searchReq = new ActiveXObject("Msxml2.XMLHTTP"); }
    catch (e) {}
  //searchReq = new ActiveXObject("Microsoft.XMLHTTP");
}

doSearchSuggest();

//-----------------------------------------------------------------------------

