// extend functionality of Array objects, from: http://www.devsource.com/article2/0,1895,2180067,00.asp
Array.prototype.indexOf = function (element)  {
    for (var i = 0; i < this.length; i++)  {
        if (this[i] == element)  {
            return i;
        }
    }
    return -1;
};


var prefectureBox = null; // initialized in setupPrefectures()
var	regionBox = null;

// prefectures listed from north to south
var prefectures = new Array("北海道", "青森", "岩手", "宮城", "秋田", "山形", "福島", "茨城", 
	"栃木", "群馬", "埼玉", "千葉", "東京", "神奈川", "新潟", "富山", "石川", "福井", "山梨", 
	"長野", "岐阜", "静岡", "愛知", "三重", "滋賀", "京都", "大阪", "兵庫", "奈良", "和歌山", 
	"鳥取", "島根", "岡山", "広島", "山口", "徳島", "香川", "愛媛", "高知", "福岡", "佐賀", 
	"長崎", "熊本", "大分", "宮崎", "鹿児島", "沖縄");

var regions = new Array("北海道", "東北", "関東", "中部", "関西", "中国", "四国", "九州", "沖縄");

var all = "すべて";
var allOption = new Option(all, "");

// where the elements of the regions array start in the prefectures array
var regionStart = new Array(0, 1, 7, 14, 23, 30, 35, 39, 46);
// where the elements of the regions array end in the prefectures array
var regionEnd = new Array(0, 6, 13, 22, 29, 34, 38, 45, 46);

var initialized = false;

function setupPrefectures() {
	prefectureBox = document.getElementById("prefectures");
	regionBox = document.getElementById("regions");
	if (prefectureBox == null) {
		alert("prefectures.js updatePrefectures() error: null prefecture box");
		return;
	}
	if (regionBox == null) {
		alert("prefectures.js updatePrefectures() error: null region box");
		return;
	}
	showAllPrefectures();
	initialized = true;
}

function updatePrefectures()
{
	if (!initialized) setupPrefectures();
	var idx = regions.indexOf(regionBox.options[regionBox.selectedIndex].text);
	
	if (idx == -1) {
		showAllPrefectures();
	}
	else {
		prefectureBox.options.length = 0;				
		var listnum = 0;
		for( var i = regionStart[idx]; i <= regionEnd[idx]; i++) {
			prefectureBox.options[listnum++] = new Option(prefectures[i],i);				
		}
		if (prefectureBox.options.length > 1) { 
			// if there is more than 1 prefecture in region (i.e., not Hokkaido or Okinawa)
			// give user the option to select any/all prefectures in region
			prefectureBox.options[prefectureBox.options.length] = allOption;
			prefectureBox.selectedIndex = prefectureBox.options.length - 1;
		}
	}
}

function showAllPrefectures() {
	prefectureBox.options.length = 0;
	prefectureBox.options[0] = allOption;	
	for(var i = 0; i < prefectures.length; i++) {
		prefectureBox.options[i + 1] = new Option(prefectures[i],i);
	}

	prefectureBox.selectedIndex = 0;
}


    //var farm = "農家";
    //var inn = "宿泊業";
    //var forestry = "林業";
    //var nature = "自然体験"; // Nature Experience
    //var welfare = "福祉施設"; // Welfare Facility
    //var education = "教育施設"; // Educational Facily
    //var family = "家族";
    //var familyNetwork = "家族同士のネットワーク"; // Network of families
    //var sharedCommunity = "生活共同体(家屋を共有)"; // Community Living (house sharing)
    //var nonSharedCommunity = "生活共同体(家屋を共有しない)"; // Community Living (no house sharing)
    //var coopHouse = "コーポラティブハウス";
    var keywordsArray = new Array("farm", "inn", "forestry",
            "nature", "welfare", "education",
            "family", "familyNetwork", "sharedCommunity",
            "nonSharedCommunity", "coopHouse");

    var allKeywordsId = "allCommunityTypes";

function clearKeywords() {
    for (var i = 0; i < keywordsArray.length; i++ ) {

        var id = keywordsArray[i];
        //alert(keywordsAray[i] + " : " + id);
        document.getElementById(id).checked = false;
        //alert(document.getElementById(id));
    }
}

function keywordSet() {
    var e = document.getElementById('allKeywordsId');
    if (e != null) e.checked = null;
}