// JavaScript Document
var SFORM = 3;
var kuko_code = new Array("HND","NRT","ITM","KIX","SPK","NGO","FUK","OKA","","SPK","OKD","WKJ","RIS","MMB","MBE","SHB","KUH","AKJ","OBO","HKD","AOJ","MSJ","HNA","AXT","ONJ","SDJ","GAJ","SYO","FKS","HND","NRT","OIM","MYE","HAC","KIJ","TOY","KMQ","NTQ","MMJ","NGO","NKM","ITM","KIX","SHM","TKG","TTJ","YGJ","IZO","OKI","IWJ","OKJ","HIJ","HIW","UBJ","TAK","TKS","MYJ","KCZ","KKJ","FUK","NGS","TSJ","FUJ","KMJ","HSG","OIT","KMI","KOJ","TNE","KUM","ASJ","KKX","OKE","RNJ","TKN","OKA","AGJ","UEO","KJP","KTD","MMD","MMY","TRA","ISG","HTR","OGN");
var kuko_ana = new Array("HND","NRT","ITM","KIX","SPK","NGO","FUK","OKA","","SPK","OKD","WKJ","RIS","MMB","MBE","SHB","KUH","AKJ","","HKD","","","","AXT","ONJ","SDJ","","SYO","FKS","HND","NRT","OIM","MYE","HAC","KIJ","TOY","KMQ","NTQ","","NGO","","ITM","KIX","","","TTJ","YGJ","","","IWJ","OKJ","HIJ","","UBJ","TAK","TKS","MYJ","KCZ","","FUK","NGS","TSJ","FUJ","KMJ","HSG","OIT","KMI","KOJ","","","","","","","","OKA","","","","","","MMY","","ISG","","");
var kuko_jal = new Array("HND","NRT","ITM","KIX","SPK","NGO","FUK","OKA","","SPK","OKD","","","MMB","","","KUH","AKJ","OBO","HKD","AOJ","MSJ","HNA","AXT","","SDJ","GAJ","","FKS","HND","TYO","","","","KIJ","TOY","KMQ","","MMJ","NGO","NKM","ITM","KIX","SHM","TJH","","","IZO","OKI","","OKJ","HIJ","HIW","UBJ","TAK","TKS","MYJ","KCZ","KKJ","FUK","NGS","","","KMJ","","OIT","KMI","KOJ","TNE","KUM","ASJ","KKX","OKE","RNJ","TKN","OKA","AGJ","UEO","KJP","KTD","MMD","MMY","TRA","ISG","HTR","OGN");
var kuko_skymark = new Array("","","","","","","","","","","","","","","","","","","","","HND","","","","","","","","","","","","","KIX","","","","","","","","","","","","","TKS","","","","FUK","","","","","","","","KOJ","","","","","","","","OKA","","","","","","","","","","");
var kuko_airdo = new Array("SPK","","","","","","","","AKJ","","HKD","","","","","","","","","","HND","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","");
var kuko_sna = new Array("","","","","","","","","","","","","","","","","","","","","HND","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","KMJ","","","KMI","","","","","","","","","","","","","","","","","","","");

function open_airline(type,h) {

	var i = h;
	var err = "";
	var err_port;

	var smon;
	var sday;
	var sfrom;
	var sto;

	if ( type == 'travel' ) {
		h+= SFORM + 1;
	} else {
		h+=SFORM;
	}

	if ( i == 'all') {

		smon = document.LINE.MONTH.selectedIndex + 1;
		sday = document.LINE.DAY.selectedIndex + 1;
		sfrom = kuko_code[document.LINE.FROM.selectedIndex];
		sto = kuko_code[document.LINE.TO.selectedIndex];
		h = "all";

	}	

	if (i == 0) {
		var mon_ana = new Array("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC");

/*
		smon = mon_ana[document.LINE.MONTH.selectedIndex];
*/
		smon = document.LINE.MONTH.selectedIndex + 1;
		sday = document.LINE.DAY.selectedIndex + 1;
		sfrom = kuko_ana[document.LINE.FROM.selectedIndex];
		sto = kuko_ana[document.LINE.TO.selectedIndex];

		if (sfrom == "") {err = "ANA"; err_port = 1;}
		if (sto == "") {err = "ANA"; err_port = 2;}
	}

	if (i == 1) {

		smon = document.LINE.MONTH.selectedIndex + 1;
		sday = document.LINE.DAY.selectedIndex + 1;
		sfrom = kuko_jal[document.LINE.FROM.selectedIndex];
		sto = kuko_jal[document.LINE.TO.selectedIndex];

		if (sfrom == "") {err = "JAL"; err_port = 1;}
		if (sto == "") {err = "JAL"; err_port = 2;}
	}

	if (i == 2) {

		smon = document.LINE.MONTH.selectedIndex + 1;
		if (smon < 10) smon = "0" + smon;
		sday = document.LINE.DAY.selectedIndex + 1;
		sfrom = kuko_skymark[document.LINE.FROM.selectedIndex];
		sto = kuko_skymark[document.LINE.TO.selectedIndex];

		if (sfrom == "") {err = "SKYMARK"; err_port = 1;}
		if (sto == "") {err = "SKYMARK"; err_port = 2;}
	}

	if (i == 3) {
		var airdo_mon = new Array("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC");

		smon = document.LINE.MONTH.selectedIndex + 1;
		if (smon < 10) smon = "0" + smon;
		sday = document.LINE.DAY.selectedIndex + 1;
		sfrom = kuko_airdo[document.LINE.FROM.selectedIndex];
		sto = kuko_airdo[document.LINE.TO.selectedIndex];

		if (sfrom == "") {err = "AIR DO"; err_port = 1;}
		if (sto == "") {err = "AIR DO"; err_port = 2;}
	}

	if (i == 4) {

		smon = document.LINE.MONTH.selectedIndex + 1;
		sday = document.LINE.DAY.selectedIndex + 1;
		sfrom = kuko_sna[document.LINE.FROM.selectedIndex];
		sto = kuko_sna[document.LINE.TO.selectedIndex];

		if (sfrom == "") {err = "SNA"; err_port = 1;}
		if (sto == "") {err = "SNA"; err_port = 2;}
	}

	if ((i == 5) || (i == 6)) {
		smon = "";
		sday = "";
		sfrom = document.LINE.FROM.selectedIndex;
		sto = document.LINE.TO.selectedIndex;
	}

	if (err == "") {
		document.forms[h].MONTH.value = smon;
		document.forms[h].DAY.value = sday;
		document.forms[h].FROM.value = sfrom;
		document.forms[h].TO.value = sto;
		var w = "a" + h;
		window.open(i + ".html",w);
	}
	else {
		if (err_port == 1) alert(err + "はこの[出発地]を指定できません。");
		if (err_port == 2) alert(err + "はこの[到着地]を指定できません。");
		document.AIRLINES.elements[i].checked = false;
	}
//	setCookie("FROM", document.LINE.FROM.selectedIndex);
//	setCookie("TO", document.LINE.TO.selectedIndex);
}

function sub(type) {

var mday = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var ndate = new Date();
var nyear;
var nmon;
var syear;
var smon;
var sday;
var errmes = "";

	nyear = ndate.getYear();
	if(nyear < 2000) nyear = nyear + 1900;
	nmon = ndate.getMonth() + 1;
	nday = ndate.getDate();
	smon = document.LINE.MONTH.selectedIndex + 1;
	sday = document.LINE.DAY.selectedIndex + 1;
	if(smon < nmon) syear = nyear + 1;
	else syear = nyear;

	if((syear % 4 == 0) && (syear % 100 != 0)) mday[1] = 29;
	if(syear % 400 == 0) mday[1] = 29;
	if(sday > mday[smon - 1]) {
		errmes = "　" + syear + "年" + smon + "月は" + mday[smon - 1] + "日までで、" + sday + "日はありません。\n";
	}

	smonbuf = (smon < nmon) ? (smon + nmon) : smon;
	monlimit = nmon + 2;
	i = ((monlimit > 12) ? (monlimit - 12) : monlimit) - 1;
	daylimit = (nday > mday[i] || nday == mday[nmon - 1] ) ? mday[i] : nday;
	if((monlimit < smonbuf) || ((monlimit == smonbuf) && (sday > daylimit))) errmes = errmes + ((errmes != "") ? "　また、" : "　") + "選択できるのは2ヶ月先の" + syear + "年" + (i + 1) + "月" + daylimit + "日までです。\n";

	if(errmes != "") errmes = "【搭乗日に" + smon + "月" + sday + "日は指定できません。】\n" + errmes + "　搭乗日を選択しなおしてください。\n\n";

	if(document.LINE.FROM.selectedIndex == document.LINE.TO.selectedIndex)
		errmes = errmes  + ((errmes != "") ? "\n" : "") + "【[出発地]と[到着地]に同じ空港を指定できません。】\n　違う空港を選択してください。";

	if(errmes != "") {
		alert(errmes);
		return;
	}

open_airline(type,'all');
//	if(document.AIRLINES.ANAS.checked == true) open_airline(type,0);
//	if(document.AIRLINES.JAL.checked == true) open_airline(type,1);
//	if(document.AIRLINES.SKYMARK.checked == true) open_airline(type,2);
//	if(document.AIRLINES.AIRDO.checked == true) open_airline(type,3);
//	if(document.AIRLINES.SNA.checked == true) open_airline(type,4);
//	if(document.AIRLINES.TMD.checked == true) open_airline(type,5);
//	if(document.AIRLINES.TMA.checked == true) open_airline(type,6);

//	setCookie("FROM",kuko_code[document.LINE.FROM.selectedIndex]);
//	setCookie("TO",kuko_code[document.LINE.TO.selectedIndex]);

	if ( type == 'travel' ) {
		document.pricesearch.submit();
	}
}

function swapa() {
	var frm = document.LINE.FROM.selectedIndex;
	var to = document.LINE.TO.selectedIndex;
	document.LINE.FROM.selectedIndex = to;
	document.LINE.TO.selectedIndex = frm;
}

function swapb() {
	var frm = document.pricesearch.from.selectedIndex;
	var to = document.pricesearch.to.selectedIndex;
	document.pricesearch.from.selectedIndex = to;
	document.pricesearch.to.selectedIndex = frm;
}

function swapc() {
	var frm = document.pricesearch.from.selectedIndex;
	var to = document.pricesearch.to.selectedIndex;
	document.pricesearch.from.selectedIndex = to;
	document.pricesearch.to.selectedIndex = frm;
	autoSetAirlines4travel();
}

function autoSetAirline() {
/*
	if(kuko_ana[document.LINE.FROM.selectedIndex] != "" && kuko_ana[document.LINE.TO.selectedIndex] != "") {
		document.AIRLINES.ANAS.checked = true;
	}
	else {
		document.AIRLINES.ANAS.checked = false;
	}

	if(kuko_jal[document.LINE.FROM.selectedIndex] != "" && kuko_jal[document.LINE.TO.selectedIndex] != "") {
		document.AIRLINES.JAL.checked = true;
	}
	else {
		document.AIRLINES.JAL.checked = false;
	}

	if(kuko_skymark[document.LINE.FROM.selectedIndex] != "" && kuko_skymark[document.LINE.TO.selectedIndex] != "") {
		document.AIRLINES.SKYMARK.checked = true;
	}
	else {
		document.AIRLINES.SKYMARK.checked = false;
	}

	if(kuko_airdo[document.LINE.FROM.selectedIndex] != "" && kuko_airdo[document.LINE.TO.selectedIndex] != "") {
		document.AIRLINES.AIRDO.checked = true;
	}
	else {
		document.AIRLINES.AIRDO.checked = false;
	}

	if(kuko_sna[document.LINE.FROM.selectedIndex] != "" && kuko_sna[document.LINE.TO.selectedIndex] != "") {
		document.AIRLINES.SNA.checked = true;
	}
	else {
		document.AIRLINES.SNA.checked = false;
	}
*/
}

function autoSetAirlines4travel() {
	var frmVal = document.pricesearch.from.value;
	var toVal = document.pricesearch.to.value;
	for( i = 0 ; i < document.LINE.FROM.length ; i ++ ) {
		if ( document.LINE.FROM.options[i].value == frmVal ) {
			document.LINE.FROM.options[i].selected = true;
		}
	}
	for( i = 0 ; i < document.LINE.TO.length ; i ++ ) {
		if ( document.LINE.TO.options[i].value == toVal ) {
			document.LINE.TO.options[i].selected = true;
		}
	}
	autoSetAirline();
}

function autoSetAirlines4travel2() {
	var frmVal = document.LINE.FROM.value;
	var toVal = document.LINE.TO.value;
	for( i = 0 ; i < document.pricesearch.from.length ; i ++ ) {
		if ( document.pricesearch.from.options[i].value == frmVal ) {
			document.pricesearch.from.options[i].selected = true;
		}
	}
	for( i = 0 ; i < document.pricesearch.to.length ; i ++ ) {
		if ( document.pricesearch.to.options[i].value == toVal ) {
			document.pricesearch.to.options[i].selected = true;
		}
	}
	autoSetAirline();
}

function setTojoYmd(){
	document.pricesearch.MONTH.value = document.LINE.MONTH.value;
	document.pricesearch.DAY.value = document.LINE.DAY.value;
}

function res() {
//	document.LINE.reset();
	for(i = 0;i < document.LINE.MONTH.length;i++) {
		if (document.LINE.MONTH.options[i].defaultSelected == true) {
			document.LINE.MONTH.selectedIndex = i;
			break;
		}
	}
	for(i = 0;i < document.LINE.DAY.length;i++) {
		if (document.LINE.DAY.options[i].defaultSelected == true) {
			document.LINE.DAY.selectedIndex = i;
			break;
		}
	}
	for(i = 0;i < document.LINE.FROM.length;i++) {
		if (document.LINE.FROM.options[i].defaultSelected == true) {
			document.LINE.FROM.selectedIndex = i;
			break;
		}
	}
	for(i = 0;i < document.LINE.TO.length;i++) {
		if (document.LINE.TO.options[i].defaultSelected == true) {
			document.LINE.TO.selectedIndex = i;
			break;
		}
	}
	fs = getCookie("FROMS");
	if (fs != "") {
		if (fs.length == 3) {
			for(i = 0;i < kuko_code.length;i++) {
				if (kuko_code[i] == fs) document.LINE.FROM.selectedIndex = i;
			}
		}
		else {
			j = eval(fs);
			if (j < 3) document.LINE.FROM.selectedIndex = j;
			else document.LINE.FROM.selectedIndex = j + 1;
		}
	}
	ts = getCookie("TOS");
	if (ts != "") {
		if (ts.length == 3) {
			for(i = 0;i < kuko_code.length;i++) {
				if (kuko_code[i] == ts) document.LINE.TO.selectedIndex = i;
			}
		}
		else {
			j = eval(ts);
			if (j < 3) document.LINE.TO.selectedIndex = j;
			else document.LINE.TO.selectedIndex = j + 1;
		}
	}
//	document.AIRLINES.reset();

	autoSetAirline();
//	document.AIRLINES.ANAS.checked = false;
//	document.AIRLINES.JAL.checked = false;
//	document.AIRLINES.SKYMARK.checked = false;
//	document.AIRLINES.AIRDO.checked = false;
//	document.AIRLINES.SNA.checked = false;

}

function getCookie(key, tmp1, tmp2, xx1, xx2, xx3) {
	tmp1 = " " + document.cookie + ";";
	xx1 = xx2 = 0;
	len = tmp1.length;
	while (xx1 < len) {
		xx2 = tmp1.indexOf(";", xx1);
		tmp2 = tmp1.substring(xx1 + 1, xx2);
		xx3 = tmp2.indexOf("=");
		if (tmp2.substring(0, xx3) == key) return(unescape(tmp2.substring(xx3 + 1, xx2 - xx1 - 1)));
		xx1 = xx2 + 1;
	}
	return("");
}

function setCookie(key, val, tmp) {
	tmp = key + "=" + escape(val) + "; ";
	tmp += "expires=Fri, 31-Dec-2030 23:59:59; ";
	document.cookie = tmp;
}

function clearCookie(key) {
	document.cookie = key + "=" + "xx; expires=1-Jan-1997 00:00:00;";
}

var syuku = new Array();
var SUN = "\"#ff7777\"";
var SAT = "\"#aaaaff\"";
var furikae;

function dat(mm, dd) {
	this.mm = mm;
	this.dd = dd;
}

function cal_init(d) {
	y = d.getYear();
	if(y < 1900) y += 1900;
	y -= 1980;
	m = d.getMonth();
	w = new Array(6,0,1,2,3,4,5);

	/* 曜日に基づく祝日はZellerの公式を使う */
	furikae = 0;
	syuku[0] = new dat(1,1);
	if (m>9) yy = y + 1981;
	else yy = y + 1980;
	datetemp = new Date(yy,0,14);
	syuku[1] = new dat(1,14-w[datetemp.getDay()]);
	syuku[2] = new dat(2,11);
	dd = parseInt(20.8431 + 0.242194 * y - parseInt(y / 4));
	syuku[3] = new dat(3,dd);
	syuku[4] = new dat(4,29);
	syuku[5] = new dat(5,3);
	syuku[6] = new dat(5,4);
	syuku[7] = new dat(5,5);
	yy = y + 1980;
	datetemp = new Date(yy,6,21);
	syuku[8] = new dat(7,21-w[datetemp.getDay()]);
	datetemp = new Date(yy,8,21);
	syuku[9] = new dat(9,21-w[datetemp.getDay()]);
	dd = parseInt(23.2488 + 0.242194 * y - parseInt(y / 4));
	syuku[10] = new dat(9,dd);
	datetemp = new Date(yy,9,14);
	syuku[11] = new dat(10,14-w[datetemp.getDay()]);
	syuku[12] = new dat(11,3);
	syuku[13] = new dat(11,23);
	syuku[14] = new dat(12,23);
	if(syuku[9].dd + 2 == syuku[10].dd) syuku[15] = new dat(9,syuku[9].dd+1);
}

function Calender() {
	var nowdate = new Date();
	var nowd = nowdate.getDate();
	var nowm = nowdate.getMonth();
	var nowy = nowdate.getYear();
	if (nowy<1900) nowy += 1900;
    	var mm1 = -1;
	var moncount = 0;

	cal_init(nowdate);
    	document.write("<TR valign=top>");
	bd = new Date(nowy,nowm,1);
	t = bd.getTime();
	while(true) {
		d = new Date(t);
		yy = d.getYear();
		if (yy<1900) yy += 1900;
		mm = d.getMonth();
		if(mm != mm1) {
			if (moncount == 1) lastd = dd;
			moncount++;
			if (moncount > 1) document.write("</TR></TABLE></TD>");
			if (moncount > 3) break;
    			document.write("<TD bgcolor=\"#ffffff\"><TABLE border=0><TR class=cal><TH colspan=7 align=center>" + yy + "年" + (mm + 1) + "月</TH></TR><TR class=cal align=center><TD><FONT color=" + SUN + ">日</FONT></TD><TD>月</TD><TD>火</TD><TD>水</TD><TD>木</TD><TD>金</TD><TD><FONT color=" + SAT + ">土</FONT></TD></TR><TR class=cal>");
		    	youbi = d.getDay();
			for (i=0; i<youbi; i++) {
				document.write("<TD></TD>");
			}
		    	mm1 = mm;
		}
		if(d.getDay() == 0) flg = 0;
		if(d.getDay() == 6) flg = 1;
		if(d.getDay() > 0 && d.getDay() < 6) flg = 2;
		mm2 = d.getMonth() + 1;
		dd2 = d.getDate();
		if(furikae == 1) {
			flg = 0;
			furikae = 0;
		}
		for (i=0; syuku.length > i; i++) {
			if(syuku[i].mm == mm2 && syuku[i].dd == dd2) {
				if(flg == 0 && !(mm2 == 5 && dd2 == 4)) furikae = 1;
				flg = 0;
			}
		}
		document.write("<TD align=right bgcolor=");
		if(flg == 0) document.write(SUN + ">");
		if(flg == 1) document.write(SAT + ">");
		if(flg == 2) document.write("\"#ffffff\">");
		dd = d.getDate();
		if(dd < 10) ddd = "&nbsp;" + dd;
		else ddd = dd;
		if(moncount == 2 || (moncount == 1 && dd >= nowd) || (moncount == 3 && (dd <= nowd || lastd == nowd))) document.write("<A href=\"JavaScript:cal_click(" + mm + "," + dd + ")\">" + ddd + "</A></TD>");
		else document.write("<FONT color=\"#808080\">" + ddd + "</FONT></TD>");
		if(d.getDay() == 6) document.write("</TR><TR class=cal>");
		t = t + 86400000;
    	}
	document.write("</TR>");
}

function cal_click(m,d) {
	document.LINE.MONTH.selectedIndex = m;
	document.LINE.DAY.selectedIndex = d - 1;
}

function cbx_click(ename) {
	with(document.AIRLINES) {
		if(elements[ename].checked == false) {
			elements[ename].checked = true;
		}
		else {
			elements[ename].checked = false;
		}
	}
}
