/*__________________________________________________________

   form control systems  (rev002_JAL_BRANCH_INTL_1)
   Copyright (C) 2004 Business Architects Inc.

_____________________________________________________________*/

var JLJS_MSG_ENTER_MEMNO     = 'Please input your membership number in 7 or 9 digits.';
var JLJS_MSG_ENTER_PASSWD    = 'Please input your password in 6 digits.';
var JLJS_AIRPORT_AREA_GROUPS = {
	'AME' : ['ALB', 'ATL', 'AUS', 'BFL', 'BWI', 'BTR', 'BHM', 'BMI', 'BOI', 'BOS', 'BUF', 'CID', 'CRW', 'CLT', 'CHA', 'CHI', 'CVG', 'CLE', 'COS', 'CAE', 'CMH', 'DFW', 'DAY', 'DEN', 'DSM', 'DTT', 'ELP', 'EVV', 'FLL', 'FWA', 'FAT', 'GRR', 'GSO', 'GSP', 'MDT', 'HFD', 'HOU', 'HSV', 'IND', 'JAX', 'AZO', 'MKC', 'TYS', 'LAN', 'LAS', 'LEX', 'LNK', 'LIT', 'LAX', 'SDF', 'MSN', 'MEM', 'MIA', 'MKE', 'MSP', 'BNA', 'MSY', 'NYC', 'ORF', 'OAK', 'OKC', 'OMA', 'ORL', 'PSP', 'PNS', 'PHL', 'PHX', 'PIT', 'PDX', 'PVD', 'RDU', 'RNO', 'RIC', 'ROA', 'RST', 'ROC', 'SMF', 'SLC', 'SAT', 'SAN', 'SFO', 'SJC', 'SNA', 'SBA', 'SEA', 'SGF', 'STL', 'SYR', 'TPA', 'TUS', 'TUL', 'WAS', 'PBI', 'ICT', 'YUM'],
	'ASA' : ['BKK', 'PUS', 'CEB', 'CNX', 'CEI', 'DPS', 'HAN', 'HDY', 'SGN', 'HKG', 'JKT', 'KHH', 'USM', 'KBV', 'KUL', 'LGK', 'MNL', 'PEN', 'HKT', 'SEL', 'REP', 'SIN', 'THS', 'TPE', 'RGN', 'JOG'],
	'CAM' : ['ACA', 'CUN', 'GDL', 'MEX', 'MTY', 'SJD'],
	'CAN' : ['YYC', 'YEA', 'YHZ', 'YMQ', 'YOW', 'YQB', 'YTO', 'YVR', 'YYJ'],
	'CHN' : ['BJS', 'DLC', 'CAN', 'HAK', 'HGH', 'KMG', 'TAO', 'SHA', 'SHE', 'TSN', 'SIA', 'XMN'],
	'EUR' : ['AHO', 'AMS', 'ATH', 'BCN', 'BSL', 'BFS', 'BEG', 'BGO', 'BER', 'BLQ', 'BOD', 'BTS', 'BRE', 'BRU', 'BUH', 'BUD', 'CAG', 'CAS', 'CTA', 'CGN', 'CPH', 'DTM', 'DRS', 'DUB', 'DUS', 'EDI', 'FAO', 'FLR', 'FRA', 'FDH', 'GVA', 'GOA', 'GIB', 'GLA', 'GOT', 'GRZ', 'HAM', 'HAJ', 'HEL', 'HOQ', 'INN', 'IST', 'KEL', 'IEV', 'KLU', 'KRS', 'LPA', 'LEJ', 'LNZ', 'LIS', 'LJU', 'LON', 'LUG', 'LUX', 'LYS', 'MAD', 'AGP', 'MLA', 'MAN', 'MRS', 'MIL', 'MOW', 'FMO', 'MUC', 'NAP', 'NCE', 'NUE', 'OSL', 'PAD', 'PMO', 'PMI', 'PAR', 'PEG', 'PSA', 'OPO', 'PRG', 'RIX', 'ROM', 'SZG', 'SCQ', 'SJJ', 'SVQ', 'SKP', 'SOF', 'LED', 'SVG', 'STO', 'SXB', 'STR', 'TLL', 'TCI', 'TIA', 'TLS', 'TRS', 'TUN', 'TRN', 'VLC', 'VCE', 'VIE', 'VNO', 'WAW', 'WRO', 'ZAG', 'ZRH'],
	'GSP' : ['GUM', 'ROP', 'SPN'],
	'HWI' : ['ITO', 'HNL', 'MKK', 'OGG', 'KOA', 'JHM', 'LNY', 'LIH'],
	'IND' : ['BLR', 'BOM', 'DEL', 'MLE'],
	'JPN' : ['AXT', 'ASJ', 'AOJ', 'AKJ', 'FUK', 'FKS', 'HKD', 'HNA', 'HIJ', 'ISG', 'IZO', 'KOJ', 'KKJ', 'KCZ', 'KMQ', 'KMJ', 'KUH', 'MMJ', 'MYJ', 'MMB', 'MSJ', 'MMY', 'KMI', 'NGS', 'NGO', 'KIJ', 'OBO', 'OIT', 'OKJ', 'OKA', 'OSA', 'SPK', 'SDJ', 'SHM', 'TAK', 'TKS', 'TYO', 'TOY', 'GAJ', 'UBJ'],
	'MDE' : ['AMM', 'BAH', 'BEY', 'CAI', 'DAM', 'DOH', 'DXB', 'JED', 'KWI', 'RUH', 'TLV'],
	'OCE' : ['ADL', 'AKL', 'BNE', 'CNS', 'CHC', 'OOL', 'MEL', 'PER', 'SYD', 'WLG'],
	'SAM' : ['BUE', 'HAV', 'IGU', 'LIM', 'POA', 'RIO', 'SCL', 'SAO'],
	'SWP' : ['PPT']
};


/* ------ JLJS_InteractiveTextField ------ */

function JLJS_InteractiveTextField (node) {
	if (!node || !node.nodeName || !node.nodeName.match(/^(input|textarea)$/i)) return;

	this.node         = node;
	this.searchAttr   = 'title';
	this.status       = '';
	this.prepopulated = false;
	this.focused      = false;
	this.classNames = {
		'default'  : 'pseudo-default',
		'focus'    : 'pseudo-focus',
		'disabled' : 'pseudo-disabled'
	};

	if (!JLJS.getAttr(this.node, this.searchAttr)) return null;

	this.node._ITF_instance_ = this;
	this.prepopulateInfoText();
	this.setStatus((this.prepopulated) ? 'disabled' : 'default');
}

JLJS_InteractiveTextField.prototype = {
	prepopulateInfoText : function () {
		var value0 = this.node.value;
		var value1 = JLJS.getAttr(this.node, this.searchAttr);
		var value2 = JLJS.getAttr(this.node, JLJS.prfx.bAattrs + this.searchAttr);
		if (value0 && value0 != value1) {
			this.prepopulated = false;
			return;
		}
		if (value1) {
			this.node.value = value1;
			JLJS.setAttr(this.node, this.searchAttr, '');
			JLJS.setAttr(this.node, JLJS.prfx.bAattrs + this.searchAttr, value1);
		} else if (value2) {
			this.node.value = value2;
		}
		this.prepopulated = true;
	},
	
	removeInfoText : function () {
		var value = JLJS.getAttr(this.node, JLJS.prfx.bAattrs + this.searchAttr);
		if (this.node.value == value) {
			this.node.value = '';
		}
	},
	
	setStatus : function (status) {
		if (!status || typeof status != 'string' || !this.classNames[status]) return;
		this.status = status;
		for (var i in this.classNames) {
			JLJS.classAttr.remove(this.node, this.classNames[i]);
		}
		if (this.classNames[this.status]) {
			JLJS.classAttr.add(this.node, this.classNames[this.status]);
		}
	}
};

function JLJS_InteractiveTextField_Setup () {
	var nodes1 = JLJS.getElementsByTagName('input');
	var nodes2 = JLJS.getElementsByTagName('textarea');
	var nodes  = JLJS.concatNodeList(nodes1, nodes2);
	for (var i = 0; i < nodes.length; i++) {
		var type = (nodes[i].nodeName.match(/^input$/i)) ? JLJS.getAttr(nodes[i], 'type') : 'textarea';
		if (!type || !type.match(/^(text|password|textarea)$/i)) continue;

		var ITF = new JLJS_InteractiveTextField(nodes[i]);
		if (ITF && ITF.node && ITF.node._ITF_instance_) {
			JLJS.addEvent(ITF.node, 'focus', function (e) {
				var obj = e.currentTarget._ITF_instance_;
				obj.focused = true;
				obj.removeInfoText();
				obj.setStatus('focus');
			});
			JLJS.addEvent(ITF.node, 'blur' , function (e) {
				var obj = e.currentTarget._ITF_instance_;
				obj.focused = false;
				obj.prepopulateInfoText();
				obj.setStatus((obj.prepopulated) ? 'disabled' : 'default');
			});
		}
	}
}

JLJS.addOnload(JLJS_InteractiveTextField_Setup);

/* ------ JLJS_Selector ----- */

function JLJS_Selector (selectNode) {
	this.setNode(selectNode);
}

JLJS_Selector.prototype = {
	setNode : function(node) {
		if (typeof node == 'object' && node.nodeName && node.nodeName.match(/^select$/i)) {
			this.node = node;
		}
	},

	adjust : function(target, arg) {
		if (this.node && typeof target == 'string' && arg) {
			this.node.options[0].selected = true;
			for (var i = 0; i < this.node.options.length; i++) {
				var value = this.node.options[i][target];
				if (value && (typeof arg == 'number' && parseInt(value, 10) == arg || typeof arg == 'string' && value == arg)) {
					this.node.options[i].selected = true;
					break;
				}
			}
		}
	},
	
	adjustByLabel : function (arg) {
		this.adjust('text', arg);
	},

	adjustByValue : function (arg) {
		this.adjust('value', arg);
	}
}



/* ------ JLJS_DateSelector ----- */

function JLJS_DateSelector() {
	this.Date = new Date();
	if (arguments.length == 3) {
		this.year   = new JLJS_Selector(arguments[0]);
		this.month  = new JLJS_Selector(arguments[1]);
		this.day    = new JLJS_Selector(arguments[2]);
		this.format = (this.year.node && this.month.node && this.day.node) ? 3 : 0;
	} else if (arguments.length == 2) {
		this.month  = new JLJS_Selector(arguments[0]);
		this.day    = new JLJS_Selector(arguments[1]);
		this.format = (this.month.node && this.day.node) ? 2 : 0;
	}
}

JLJS_DateSelector.prototype = {
	adjust : function() {
		if (this.year)  this.year.adjustByValue(this.Date.getFullYear());
		if (this.month) this.month.adjustByValue(this.Date.getMonth() + 1);
		if (this.day)   this.day.adjustByValue(this.Date.getDate());
	},

	applyOffset : function () {
		var offset = { y : 0, m : 0, d : 0 };
		var ptn    = /^((\+|\-)\d+)(y|m|d)$/;
		for (var i = 0; i < arguments.length; i++) {
			if (typeof arguments[i] == 'string' && arguments[i].match(ptn)) {
				offset[RegExp.$3] = eval(RegExp.$1);
			}
		}
		this.Date.setFullYear(this.Date.getFullYear() + offset.y);
		this.Date.setMonth(this.Date.getMonth() + offset.m);
		this.Date.setDate(this.Date.getDate() + offset.d);
		this.adjust();
	},

	adjustToToday : function () {
		this.Date = new Date();
		this.adjust();
	},

	adjustToDate : function (arg) {
		if (arg.constructor == Date) {
			this.Date = arg
		} else if (typeof arg == 'string' && arg.match(/^[\d\-\/]*$/)) {
			var date = (arg.match(/\-/)) ? arg.split('-') : arg.split('/');
			if (date.length == 3) {
				this.Date.setFullYear(parseInt(date[0], 10));
				this.Date.setMonth(parseInt(date[1], 10) - 1);
				this.Date.setDate(parseInt(date[2], 10));
			} else if (date.length == 2) {
				this.Date.setMonth(parseInt(date[0], 10) - 1);
				this.Date.setDate(parseInt(date[1], 10));
			} else {
				return;
			}
		}
		this.adjust();
	}
};



/* ------ JLJS_NarrowDownSelector ------ */

function JLJS_NarrowDownSelector (select1, select2, groupTable) {
	this.nullLabel = '___NULL___';
	this.setNode(select1, select2);
	this.setGroupTable(groupTable);
	this.narrowDown();
}

JLJS_NarrowDownSelector.prototype = {
	setNode : function (select1, select2) {
		if (this.isSelectNode(select1) && this.isSelectNode(select2)) {
			this.choose = select1;
			this.source = select2;
			this.buffer = {};
			for (var i = 0; i < this.source.options.length; i++) {
				var value = this.source.options[i].value || this.nullLabel;
				this.buffer[value] = this.source.options[i].text;
			}
			this.choose._JLJS_NDS_INSTANCE_ = this;
			this.choose.options[0].selected = true;
			this.source.options[0].selected = true;
		}
	},

	isSelectNode : function (node) {
		return (node && node.nodeName && node.nodeName.toLowerCase() == 'select');
	},

	setGroupTable : function (table) {
		this.groupTable = (typeof table == 'object') ? table : {}
	},

	narrowDown : function (groupLabel) {
		if (!this.choose || !this.source || !this.buffer || !this.groupTable) return;
		if (!groupLabel) groupLabel = this.nullLabel;
		if (JLJS.env.isIE) {
			while (this.source.options[0]) {
				this.source.options.remove(0);
			}
		} else {
			while (this.source.firstChild) {
				this.source.removeChild(this.source.firstChild);
			}
		}
		var i   = 0;
		var ptn = (this.groupTable[groupLabel]) ? new RegExp('^(' + this.groupTable[groupLabel].join('|') + ')$') : this.nullLabel;
		for (var value in this.buffer) {
			if (value.match(ptn)) {
				var cOPT   = document.createElement('option');
				cOPT.value = (value != this.nullLabel) ? value : '';
				cOPT.text  = this.buffer[value];
				if (JLJS.env.isIE) {
					this.source.add(cOPT, i);
					i++;
				} else {
					this.source.appendChild(cOPT);
				}
			}
		}
		this.source.options[0].selected = true;
	}
}


/* ------ JLJS_JMBMemberLoginForm ------ */

function JLJS_JMBMemberLoginForm (formNode) {
	if (!formNode) return;
	this.formNode         = formNode;
	this.formNode._super_ = this;
	this.cookieKey        = 'member_no';
	this.cookieExpireYear = 10;
	this.memberNo         = null;
	this.init();
	this.getValueFromCookie();
	this.reflectStatusToField();
}

JLJS_JMBMemberLoginForm.prototype = {
	init : function () {
		var nodes = JLJS.getElementsByTagName('input', this.formNode);
		for (var i = 0; i < nodes.length; i++) {
			var node = nodes[i];
			var type = JLJS.getAttr(node, 'type');
			if (!type) continue;
			if (!this.memberNoField && type.toLowerCase() == 'text') {
				this.memberNoField = node;
			}
			if (!this.memberPwField && type.toLowerCase() == 'password') {
				this.memberPwField = node;
			}
			if (!this.AutoCompCBox && type.toLowerCase() == 'checkbox') {
				this.AutoCompCBox = node;
			}
		}
	},

	getValueFromCookie : function () {
		var data = (document.cookie) ? document.cookie.split(';') : [];
		for (var i in data) {
			if (data[i].split('=')[0].replace(/\s/g, '') == this.cookieKey) {
				this.memberNo = unescape(data[i].split('=')[1].replace(/\s/g, ''));
				break;
			}
		}
	},

	reflectStatusToField : function() {
		if (this.memberNo > 0) {
			if (this.memberNoField) this.memberNoField.value = this.memberNo;
			if (this.memberPwField) this.memberPwField.value = '';
			if (this.AutoCompCBox) this.AutoCompCBox.checked = true;
		}
	},

	setCookie : function (){
		if (!this.memberNoField || !this.AutoCompCBox) return;
		var date = new Date();
		var value = (this.AutoCompCBox.checked) ? this.memberNoField.value : '';
		var shift = ((this.AutoCompCBox.checked) ? 1 : -1) * this.cookieExpireYear;
		date.setFullYear(date.getFullYear() + shift);
		var expires = date.toGMTString();
		value = this.cookieKey + '=' + escape(value) + ';';
//		value += 'domain=' + location.hostname + ';';
		value += 'path=/;';
		value += 'expires=' + expires;
		document.cookie = value;
	},

	checkInputData : function () {
		if (!this.memberNoField || !this.memberPwField || this.submitLock) return;
		if (this.memberNoField.value.length != 9 && this.memberNoField.value.length != 7) {
			alert(JLJS_MSG_ENTER_MEMNO);
			this.memberNoField.focus();
			return;
		}
		if (this.memberPwField.value.length != 4 && this.memberPwField.value.length != 6) {
			alert(JLJS_MSG_ENTER_PASSWD);
			this.memberPwField.focus();
			return;
		}

/* コメントアウト 松田 20050725 */
//		this.submitLock = true;

		this.setCookie();
		this.formNode.submit();

/* 追加 松田 20050725 */
		this.memberPwField.value="";

	}
};

function JLJS_JMBMemberLoginFormSetup () {
	window.JLJSJMBLoginForm = null;
	var block = JLJS.getElementsByClassName('memberLoginBlockA02', 'div')[0];
	var form  = (block) ? JLJS.getElementsByTagName('form', block)[0] : null;
	if (form) {
		JLJSJMBLoginForm = new JLJS_JMBMemberLoginForm(form);
		JLJS.addEvent(JLJSJMBLoginForm.formNode, 'submit', function (e) {
			e.preventDefault();
			JLJSJMBLoginForm.checkInputData();
		} );
	}
}

JLJS.addOnload(JLJS_JMBMemberLoginFormSetup);



/* ------ JLJS_ReserveSidePaneForm ------ */

function JLJS_ReserveSidePaneForm (formNode) {
	if (!formNode) return;
	this.formNode             = formNode;
	this.formNode._JLJS_RSPF_ = this;
	this.dateSelectors        = [];
	this.narrowDownSelectors  = [];
}

JLJS_ReserveSidePaneForm.prototype = {
	setDateSelector : function (name1, name2) {
		if (!name1 || !name2) return;
		var ptn  = /^select$/i;
		var sel1 = this.formNode[name1];
		var sel2 = this.formNode[name2];
		if (sel1 && sel1.nodeName.match(ptn) && sel2 && sel2.nodeName.match(ptn)) {
			this.dateSelectors[this.dateSelectors.length] = new JLJS_DateSelector(sel1, sel2);
		}
	},

	adjustDateSelectors : function () {
		for(var i = 0; i < this.dateSelectors.length; i++) {
			this.dateSelectors[0].adjustToToday();
		}
		if (this.dateSelectors[1]) {
			this.dateSelectors[1].applyOffset('+7d');
		}
	},

	setNarrowDownSelector : function (name1, name2, groupTable) {
		if (!name1 || !name2) return;
		var sel1 = this.formNode[name1];
		var sel2 = this.formNode[name2];
		var nsel = new JLJS_NarrowDownSelector(sel1, sel2, groupTable);
		JLJS.addEvent(nsel.choose, 'change', function (e) {
			e.target._JLJS_NDS_INSTANCE_.narrowDown(e.target.value);
		} );
		this.narrowDownSelectors[this.narrowDownSelectors.length] = nsel;
	}
};

function JLJS_ReserveSidePaneFormSetup () {
	var targetBlockIDs    = ['sidePane-dom', 'sidePane-int', 'sidePane-pyf'];
	var monthDatePairs    = [['PRM_MEETINGMONTH', 'PRM_MEETINGDATE'], ['PRM_SENDINGMONTH', 'PRM_SENDINGDATE']];
	var narrowDownPairs   = [['PRM_DEPAREA','PRM_DEPARTURE'], ['PRM_ARRAREA','PRM_ARRIVAL']];

	for (var i = 0; i < targetBlockIDs.length; i++) {
		var id    = targetBlockIDs[i];
		var block = document.getElementById(id);
		var form  = (block) ? JLJS.getElementsByTagName('form', block)[0] : null;
		if (form) {
			var resSidePaneForm = new JLJS_ReserveSidePaneForm(form);

			for (var j = 0; j < monthDatePairs.length; j++) {
				resSidePaneForm.setDateSelector(monthDatePairs[j][0], monthDatePairs[j][1]);
			}
			resSidePaneForm.adjustDateSelectors();

			for (var j = 0; j < narrowDownPairs.length; j++) {
				resSidePaneForm.setNarrowDownSelector(narrowDownPairs[j][0], narrowDownPairs[j][1], JLJS_AIRPORT_AREA_GROUPS);
			}
		}
	}
}

//JLJS.addOnload(JLJS_ReserveSidePaneFormSetup);

