//---------------------- Global Variables ------------------------
var oct_Area = new Array();
var oct_Country = new Array();
var oct_Dest = new Array();

//---------------------- Function Domain ------------------------
function oct_Init( area , country , dest ){
	var form = document.OctopusTravelSearchModule;
	oct_SetArea( form , area , country , dest );
	oct_setCheckInOptions(form);
}
//Initializing Options
function oct_ClearOptions( obj ){
	obj.options.length = 0;
	obj.options[0] = new Option('-----',0,true,false);
	return true;
}
//Setting Options
function oct_SetArea( FormObj , area , country , dest ){
	var i,data,splited;
	var hit = false;
	FormObj.area.options.length = 0;
	if(!oct_Area){return false;}
	for(i=0;i<oct_Area.length;i++){
		data = oct_Area[i];
		splited = data.split('::');
		FormObj.area.options[i] = new Option(splited[1],splited[0],false,false);
		if(area && (area == splited[0] || area == splited[1])){
			FormObj.area.options[i].selected = true;
			hit = true
		}
	}
	if(!hit){FormObj.area.options[0].selected = true;}
	oct_SetCountry(FormObj.area, area , country , dest );
	if(!area && !country && !dest){
		oct_ClearOptions(FormObj.destination);
	}
	return true;
}
function oct_SetCountry( obj , area , country , dest ){
	var i,data,splited;
	oct_ClearOptions(obj.form.country);
	oct_ClearOptions(obj.form.destination);
	if(!oct_Country[obj.value]){return false;}
	for(i=0;i<oct_Country[obj.value].length;i++){
		data = oct_Country[obj.value][i];
		splited = data.split('::');
		obj.form.country.options[i+1] = new Option(splited[1],splited[0],false,false);
		if(country && (country == splited[0] || country == splited[1])){
			obj.form.country.options[i+1].selected = true;
		}
	}
	oct_SetDestination(obj.form.country, area , country , dest );
	return true;
}
function oct_SetDestination( obj , area , country , dest ){
	var i,data,splited;
	oct_ClearOptions(obj.form.destination);
	if(!oct_Dest[obj.value]){return false;}
	for(i=0;i<oct_Dest[obj.value].length;i++){
		data = oct_Dest[obj.value][i];
		splited = data.split('::');
		obj.form.destination.options[i+1] = new Option(splited[1],splited[0],false,false);
		if(dest && (dest == splited[0] || dest == splited[1])){
			obj.form.destination.options[i+1].selected = true;
		}
	}
	return true;
}
//Redirecting
function oct_Submit(FormObj,target){
	var i,p_obj;
	var url = 'http://www.octopustravel.com/jp/HotelLink';
	var query_string = '?';
	var params = new Array('siteid','affiliateref','language','currency','country','destination','checkin','nights','location','starrating','location','hotelname','roomtype','rooms');
	if(FormObj.destination.value == 0){
		alert('Select Destination');
		return false;
	}
	for(i=0;i<params.length;i++){
		if(params[i] == 'checkin'){
			query_string += params[i] + '=' + FormObj.CheckInYM.value + '-' + FormObj.CheckInD.value + '&';
		}else if(params[i] == 'country'){
			query_string += params[i] + '=JP&';
		}else if((p_obj = eval('FormObj.'+params[i])) && p_obj.value && p_obj.value != 0){
			query_string += params[i] + '=' + p_obj.value + '&';
		}
	}
	query_string = query_string.substr(0,query_string.length-1);
	if(target == '_self'){
		document.location.href = url + query_string;
	}else{
		window.open(url + query_string,'_blank');
	}
	return false;
}
//setting check in options
function oct_setCheckInOptions(form){
	var i,y,m,ms,d,ds;
	var today = new Date();
	y = today.getYear();
	m = today.getMonth() + 1;
	d = today.getDate();
	if(y<1900){y += 1900;}
	form.CheckInYM.options.length=0;
	form.CheckInD.options.length=0;
	for(i=0;i<12;i++){
		ms = m;
		if(m>12){m=1;y++;}
		if(m<10){ms = '0' + m;}
		form.CheckInYM.options[i] = new Option('' + ms + ' / ' + y + ' ','' + y + '-' + ms,i==0,i==0);
		m++;
	}
	for(i=1;i<32;i++){
		ds = i;
		if(i<10){ds = '0' + i;}
		form.CheckInD.options[i-1] = new Option('' + ds + '',ds,i==d,i==d);
	}
	return true;
}

//Data operation
function oct_newArea(code,name){
	oct_Area[oct_Area.length] = code + '::' + name;
	oct_Country[code] = new Array();
}
function oct_newCountry(area_code,code,name){
	oct_Country[area_code][oct_Country[area_code].length] = code + '::' + name;
	oct_Dest[code] = new Array();
}
function oct_newDest(country_code,code,name){
	oct_Dest[country_code][oct_Dest[country_code].length] = code + '::' + name;
}
//---------------------- Data Domain ------------------------
//Area Definitions
oct_newArea('JAPAN','Japan');

//Country Definitions
oct_newCountry('JAPAN','HOKK','Hokkaido Island');
oct_newCountry('JAPAN','TOH','Tohoku Area');
oct_newCountry('JAPAN','KANT','Kanto(Tokyo) Area');
oct_newCountry('JAPAN','HOKU','Hokuriku Area');
oct_newCountry('JAPAN','TOK','Tokai Area');
oct_newCountry('JAPAN','KANS','Kansai(Osaka) Area');
oct_newCountry('JAPAN','CHU','Chugoku Area');
oct_newCountry('JAPAN','SHI','Shikoku Island');
oct_newCountry('JAPAN','KYU','Kyushu Island');
oct_newCountry('JAPAN','OKI','Okinawa Islands');

//Destination Definitions
oct_newDest('HOKK','CAKJ','Asahikawa');
oct_newDest('HOKK','CHKD','Hakodate');
oct_newDest('HOKK','CKTAH','Kitahiroshima');
oct_newDest('HOKK','CKTAM','Kitami');
oct_newDest('HOKK','CKUH','Kushiro');
oct_newDest('HOKK','CKSSH','Lake Kussharo');
oct_newDest('HOKK','CLTOY','Lake Toya');
oct_newDest('HOKK','CNOBO','Noboribetsu');
oct_newDest('HOKK','CSPK','Sapporo');
oct_newDest('HOKK','CSHIC','Shikabe');
oct_newDest('TOH','CAXT','Akita');
oct_newDest('TOH','CAOJ','Aomori');
oct_newDest('TOH','CMATO','Hachimantai');
oct_newDest('TOH','CHHE','Hachinohe');
oct_newDest('TOH','CHROS','hirosaki');
oct_newDest('TOH','CIWKI','Iwaki');
oct_newDest('TOH','CKAKU','Kakunodate');
oct_newDest('TOH','CHNA','Morioka');
oct_newDest('TOH','CSAKT','Sakata');
oct_newDest('TOH','CSDJ','Sendai');
oct_newDest('TOH','CSHZU','Shizukuishi');
oct_newDest('TOH','CGAJ','Yamagata');
oct_newDest('TOH','CZAOA','Zao');
oct_newDest('KANT','CTYO','Tokyo');
oct_newDest('KANT','CNRT','Tokyo(Narita AP）');
oct_newDest('KANT','','----');
oct_newDest('KANT','CCIBA','Chiba');
oct_newDest('KANT','CFKS','Fukushima');
oct_newDest('KANT','CFUNA','Funabashi');
oct_newDest('KANT','CHAKO','Hakone');
oct_newDest('KANT','CKAWS','Kawasaki');
oct_newDest('KANT','CKITA','Kitashiohara');
oct_newDest('KANT','CKORY','Koriyama');
oct_newDest('KANT','CMAKU','Makuhari');
oct_newDest('KANT','CMNKA','Minakami');
oct_newDest('KANT','CQIS','Mito');
oct_newDest('KANT','CNASU','Nasu');
oct_newDest('KANT','ANIKK','Nikko');
oct_newDest('KANT','CSHIO','Shimoda');
oct_newDest('KANT','CTOYU','Tomiura');
oct_newDest('KANT','CURAY','Urayasu');
oct_newDest('KANT','CYOKO','Yokohama');
oct_newDest('HOKU','CFUK','Fukui');
oct_newDest('HOKU','CKAGB','Kaga');
oct_newDest('HOKU','CKANZ','Kanazawa');
oct_newDest('HOKU','CKIJ','Niigata');
oct_newDest('HOKU','CSHID','Shiga (Ishikawa)');
oct_newDest('HOKU','CTOMN','Tonami');
oct_newDest('HOKU','CTOY','Toyama');
oct_newDest('HOKU','CWAKU','Wakura');
oct_newDest('TOK','CHAKU','Hakuba');
oct_newDest('TOK','CHAMS','Hamamatsu');
oct_newDest('TOK','COIZU','Hokuto');
oct_newDest('TOK','AISEZ','Ise, Shima & Toba');
oct_newDest('TOK','CLKAW','Lake Kawaguchi');
oct_newDest('TOK','CMMJ','Matsumoto');
oct_newDest('TOK','CNAGN','Nagano');
oct_newDest('TOK','CNGO','Nagoya');
oct_newDest('TOK','CNUMZ','Numazu');
oct_newDest('TOK','CSHIZ','Shizuoka');
oct_newDest('TOK','CTAKA','Takayama');
oct_newDest('TOK','CUEDA','Ueda');
oct_newDest('TOK','CQGZ','Yokkaichi');
oct_newDest('KANS','COSA','Osaka');
oct_newDest('KANS','CKIX','Osaka（Kansai AP）');
oct_newDest('KANS','CKYOT','Kyoto');
oct_newDest('KANS','','----');
oct_newDest('KANS','CAWAJ','Awaji');
oct_newDest('KANS','CHIME','Himeji');
oct_newDest('KANS','CIZUM','Izumiotsu');
oct_newDest('KANS','CKASJ','Kashihara');
oct_newDest('KANS','CKISH','Kishiwada');
oct_newDest('KANS','CKOBE','Kobe');
oct_newDest('KANS','CKUST','Kusatsu (Shiga)');
oct_newDest('KANS','CKUJ','Kushimoto');
oct_newDest('KANS','CKYOT','Kyoto');
oct_newDest('KANS','CMINJ','Minabe');
oct_newDest('KANS','CMINH','Minamiawaji');
oct_newDest('KANS','CMIYZ','Miyazu');
oct_newDest('KANS','CNAGC','Nagahama');
oct_newDest('KANS','CNARA','Nara');
oct_newDest('KANS','CNSHI','Nishinomiya');
oct_newDest('KANS','CNISH','Nishiwaki');
oct_newDest('KANS','CSAKI','Sakai');
oct_newDest('KANS','CSNDA','Sanda');
oct_newDest('KANS','CSHM','Shirahama');
oct_newDest('KANS','CTOYN','Toyonaka');
oct_newDest('KANS','CWAKA','Wakayama');
oct_newDest('CHU','CHIJ','Hiroshima');
oct_newDest('CHU','CIWAK','Iwakuni');
oct_newDest('CHU','CKURS','Kurashiki');
oct_newDest('CHU','CMATV','Matsue');
oct_newDest('CHU','CHMIY','Miyajima Island');
oct_newDest('CHU','COKJ','Okayama');
oct_newDest('CHU','CSHNO','Shimonoseki');
oct_newDest('CHU','CUBJ','Ube');
oct_newDest('CHU','CYAMH','Yamaguchi');
oct_newDest('SHI','CGEIE','Geisei');
oct_newDest('SHI','CKCZ','Kochi');
oct_newDest('SHI','CTAK','Takamatsu');
oct_newDest('SHI','CTKS','Tokushima');
oct_newDest('KYU','CBEPP','beppu');
oct_newDest('KYU','CFUK','Fukuoka');
oct_newDest('KYU','CHIJI','Hiji');
oct_newDest('KYU','CKOJ','Kagoshima');
oct_newDest('KYU','CKIRA','Kirishima');
oct_newDest('KYU','CKKJ','Kitakyushu');
oct_newDest('KYU','CKMJ','Kumamoto');
oct_newDest('KYU','CKMI','Miyazaki');
oct_newDest('KYU','CMUNB','Munakata');
oct_newDest('KYU','CNGS','Nagasaki');
oct_newDest('KYU','COIT','Oita');
oct_newDest('OKI','AOKA','Okinawa');
