


/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: Down Home Consulting :: http://downhomeconsulting.com */

/*
Country State Drop Downs v1.0.
(c) Copyright 2005 Down Home Consulting, Inc.
www.DownHomeConsulting.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. The software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, itness for a particular purpose and noninfringement. in no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software.

*/

// If you have PHP you can set the post values like this
//var postState = '<?= $_POST["state"] ?>';
//var postCountry = '<?= $_POST["country"] ?>';
var postState = '';
var postCountry = '';

// State table
//
// To edit the list, just delete a line or add a line. Order is important.
// The order displayed here is the order it appears on the drop down.
//
var state = '\
Business:Auctions:Auctions|\
Business:Directories:Directories|\
Business:Employment:Employment|\
Business:Finance:Finance|\
Business:Investments:Investments|\
Business:Marketing:Marketing|\
Business:MLM:MLM|\
Business:Organizations:Organizations|\
Business:Trade:Trade|\
Computing:Ecards:Ecards|\
Computing:Hardware:Hardware|\
Computing:Internet:Internet|\
Computing:IT security:IT security|\
Computing:Linux:Linux|\
Computing:MacIntosh:MacIntosh|\
Computing:Multimedia:Multimedia|\
Computing:Networking:Networking|\
Computing:Programming:Programming|\
Computing:Search Engines and Directories:Search Engines and Directories|\
Computing:Software:Software|\
Culture:Cinema:Cinema|\
Culture:Crafts:Crafts|\
Culture:Dance:Dance|\
Culture:Education:Education|\
Culture:Events:Events|\
Culture:Forums:Forums|\
Culture:Galleries:Galleries|\
Culture:Groups:Groups|\
Culture:Institutes:Institutes|\
Culture:Literature:Literature|\
Culture:Magazines:Magazines|\
Culture:Museums:Museums|\
Culture:Music:Music|\
Culture:Organizations:Organizations|\
Culture:Religion:Religion|\
Culture:Theatre:Theatre|\
Education:Career:Career|\
Education:Financial help:Financial help|\
Education:Forums:Forums|\
Education:Organizations:Organizations|\
Entertainment:Actors:Actors|\
Entertainment:Events:Events|\
Entertainment:Forums:Forums|\
Entertainment:Humor:Humor|\
Entertainment:Jokes:Jokes|\
Entertainment:Movies:Movies|\
Entertainment:Music:Music|\
Entertainment:Radio:Radio|\
Entertainment:Television:Television|\
Health:Alternative Medicine:Alternative Medicine|\
Health:Children:Children|\
Health:Clinics:Clinics|\
Health:Conditions:Conditions|\
Health:Dental Care:Dental Care|\
Health:Directories:Directories|\
Health:Disabilities:Disabilities|\
Health:Diseases:Diseases|\
Health:Education:Education|\
Health:Emergency Services:Emergency Services|\
Health:Fitness:Fitness|\
Health:Forums:Forums|\
Health:Hospitals:Hospitals|\
Health:Medicine:Medicine|\
Health:Men:Men|\
Health:Mental Health:Mental Health|\
Health:Nursing:Nursing|\
Health:Nutrition:Nutrition|\
Health:Organizations:Organizations|\
Health:Pharmacies:Pharmacies|\
Health:Reproductive Health:Reproductive Health|\
Health:Self-Help:Self-Help|\
Health:Seniors:Seniors|\
Health:Sexual Health:Sexual Health|\
Health:Teens:Teens|\
Health:Therapies:Therapies|\
Health:Women:Women|\
Leisure:Boating:Boating|\
Leisure:Climbing:Climbing|\
Leisure:Fishing:Fishing|\
Leisure:Gambling:Gambling|\
Leisure:Games:Games|\
Leisure:Hobbies:Hobbies|\
Leisure:Hunting:Hunting|\
Leisure:National Parks:National Parks|\
Leisure:Toys:Toys|\
Leisure:Travel:Travel|\
News:Directories:Directories|\
News:International:International|\
News:Journalists:Journalists|\
News:Magazines:Magazines|\
News:Newspapers:Newspapers|\
News:Radio:Radio|\
News:Regional:Regional|\
News:Television:Television|\
News:Universities:Universities|\
News:Weather:Weather|\
Science:Aeronautics:Aeronautics|\
Science:Agriculture:Agriculture|\
Science:Astronomy:Astronomy|\
Science:Biology:Biology|\
Science:Chemistry:Chemistry|\
Science:Ecology:Ecology|\
Science:Education:Education|\
Science:Energy:Energy|\
Science:Engineering:Engineering|\
Science:Forums:Forums|\
Science:Geography:Geography|\
Science:Mathematics:Mathematics|\
Science:Physics:Physics|\
Science:Space:Space|\
Shopping:Animals:Animals|\
Shopping:Antiques:Antiques|\
Shopping:Apparels:Apparels|\
Shopping:Arts:Arts|\
Shopping:Auctions:Auctions|\
Shopping:Automotive:Automotive|\
Shopping:Banking:Banking|\
Shopping:Bettings:Bettings|\
Shopping:Boating:Boating|\
Shopping:Books:Books|\
Shopping:Casinos:Casinos|\
Shopping:Children:Children|\
Shopping:Collectibles:Collectibles|\
Shopping:Computing:Computing|\
Shopping:Consumer Electronics:Consumer Electronics|\
Shopping:Crafts:Crafts|\
Shopping:Credit and Finance:Credit and Finance|\
Shopping:Cruises:Cruises|\
Shopping:Dating:Dating|\
Shopping:Disabilities:Disabilities|\
Shopping:Drinks:Drinks|\
Shopping:Education:Education|\
Shopping:Entertainment:Entertainment|\
Shopping:Financial Services:Financial Services|\
Shopping:Food:Food|\
Shopping:Gardening:Gardening|\
Shopping:Gifts:Gifts|\
Shopping:Health:Health|\
Shopping:Home:Home|\
Shopping:Hotels:Hotels|\
Shopping:Investments:Investments|\
Shopping:Jewelry:Jewelry|\
Shopping:Limousine Service:Limousine Service|\
Shopping:Lodging:Lodging|\
Shopping:Music:Music|\
Shopping:Online booking:Online booking|\
Shopping:Personal Care:Personal Care|\
Shopping:Photography:Photography|\
Shopping:Publishers:Publishers|\
Shopping:Real Estate:Real Estate|\
Shopping:Religion:Religion|\
Shopping:Restaurants:Restaurants|\
Shopping:Sex:Sex|\
Shopping:Spirituality:Spirituality|\
Shopping:Sports:Sports|\
Shopping:Tour Operators:Tour Operators|\
Shopping:Toys:Toys|\
Shopping:Travels:Travels|\
Shopping:Video:Video|\
Society:Gays and Lesbians:Gays and Lesbians|\
Society:Issues:Issues|\
Society:Military:Military|\
Society:People:People|\
Society:Philanthropy:Philanthropy|\
Society:Politics:Politics|\
Society:Relationship:Relationship|\
Society:Religion:Religion|\
Society:Sexuality:Sexuality|\
Society:Social Sciences:Social Sciences|\
Society:Spirituality:Spirituality|\
Sports:American football:American football|\
Sports:Auto Racing:Auto Racing|\
Sports:Baseball:Baseball|\
Sports:Bettings:Bettings|\
Sports:Boxing:Boxing|\
Sports:Events:Events|\
Sports:Forums:Forums|\
Sports:Martial Arts:Martial Arts|\
Sports:Rugby:Rugby|\
Sports:Running:Running|\
Sports:Skating:Skating|\
Sports:Skiing:Skiing|\
Sports:Soccer:Soccer|\
Sports:Swimming:Swimming|\
Sports:Tennis:Tennis|\
Sports:University:University|\
Sports:Volleyball:Volleyball|\
Travel:Air:Air|\
Travel:Car:Car|\
Travel:Photos:Photos|\
Travel:Sea:Sea|\
Travel:Train:Train|\
Travel:Transportation:Transportation|\
Travel:Travel Agencies:Travel Agencies|\
Travel:Travel Guides:Travel Guides|\
';

// Country data table
//
// To edit the list, just delete a line or add a line. Order is important.
// The order displayed here is the order it appears on the drop down.
//
var country = '\
Business:Business|\
Computing:Computing|\
Culture:Culture|\
Education:Education|\
Entertainment:Entertainment|\
Health:Health|\
Leisure:Leisure|\
News:News|\
Science:Science|\
Shopping:Shopping|\
Society:Society|\
Sports:Sports|\
Travel:Travel|\
';

function TrimString(sInString) {
  if ( sInString ) {
    sInString = sInString.replace( /^\s+/g, "" );// strip leading
    return sInString.replace( /\s+$/g, "" );// strip trailing
  }
}

// Populates the country selected with the counties from the country list
function populateCountry(defaultCountry) {
  if ( postCountry != '' ) {
    defaultCountry = postCountry;
  }
  var countryLineArray = country.split('|');  // Split into lines
  var selObj = document.getElementById('countrySelect');
  selObj.options[0] = new Option('Select Category','');
  selObj.selectedIndex = 0;
  for (var loop = 0; loop < countryLineArray.length; loop++) {
    lineArray = countryLineArray[loop].split(':');
    countryCode  = TrimString(lineArray[0]);
    countryName  = TrimString(lineArray[1]);
    if ( countryCode != '' ) {
      selObj.options[loop + 1] = new Option(countryName, countryCode);
    }
    if ( defaultCountry == countryCode ) {
      selObj.selectedIndex = loop + 1;
    }
  }
}

function populateState() {
  var selObj = document.getElementById('stateSelect');
  var foundState = false;
  // Empty options just in case new drop down is shorter
  if ( selObj.type == 'select-one' ) {
    for (var i = 0; i < selObj.options.length; i++) {
      selObj.options[i] = null;
    }
    selObj.options.length=null;
    selObj.options[0] = new Option('Select Subcategory','');
    selObj.selectedIndex = 0;
  }
  // Populate the drop down with states from the selected country
  var stateLineArray = state.split("|");  // Split into lines
  var optionCntr = 1;
  for (var loop = 0; loop < stateLineArray.length; loop++) {
    lineArray = stateLineArray[loop].split(":");
    countryCode  = TrimString(lineArray[0]);
    stateCode    = TrimString(lineArray[1]);
    stateName    = TrimString(lineArray[2]);
  if (document.getElementById('countrySelect').value == countryCode && countryCode != '' ) {
    // If it's a input element, change it to a select
      if ( selObj.type == 'text' ) {
        parentObj = document.getElementById('stateSelect').parentNode;
        parentObj.removeChild(selObj);
        var inputSel = document.createElement("SELECT");
        inputSel.setAttribute("name","state");
        inputSel.setAttribute("id","stateSelect");
        parentObj.appendChild(inputSel) ;
        selObj = document.getElementById('stateSelect');
        selObj.options[0] = new Option('Select Subcategory','');
        selObj.selectedIndex = 0;
      }
      if ( stateCode != '' ) {
        selObj.options[optionCntr] = new Option(stateName, stateCode);
      }
      // See if it's selected from a previous post
      if ( stateCode == postState && countryCode == postCountry ) {
        selObj.selectedIndex = optionCntr;
      }
      foundState = true;
      optionCntr++
    }
  }
  // If the country has no states, change the select to a text box
  if ( ! foundState ) {
    parentObj = document.getElementById('stateSelect').parentNode;
    parentObj.removeChild(selObj);
  // Create the Input Field
    var inputEl = document.createElement("INPUT");
    inputEl.setAttribute("id", "stateSelect");
    inputEl.setAttribute("type", "text");
    inputEl.setAttribute("name", "state");
    inputEl.setAttribute("size", 20);
    inputEl.setAttribute("value", postState);
    parentObj.appendChild(inputEl) ;
  }
}

function initCountry(country) {
  populateCountry(country);
  populateState();
}



