/*__________________________________________________________

   formControl.js for JAL global site (rev001)

_____________________________________________________________*/

/* ------ 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_ReserveSidePaneForm ------ */

function JLJS_GlobalNaviRegionSelector (formNode) {
	if (!formNode) return;
	this.formNode           = formNode;
	this.formNode._super_   = this;
	this.narrowDownSelector = {};
}

JLJS_GlobalNaviRegionSelector.prototype = {
	setNarrowDownSelector : function (name1, name2, groupTable) {
		if (!name1 || !name2 || !groupTable) return;
		var ptn = /^select$/i;
		var nd  = {
			selector   : this.formNode[name1],
			population : this.formNode[name2],
			buffer     : {}
		};
		if (nd.selector && nd.selector.nodeName.match(ptn) && nd.population && nd.population.nodeName.match(ptn)) {
			if (!this.narrowDownSelector.buffer) {
				for (var i = 0; i < nd.population.options.length; i++) {
					nd.buffer[nd.population.options[i].value] = nd.population.options[i].text;
				}
			}
			this.narrowDownSelector                  = nd;
			this.narrowDownSelector.selector._super_ = this;
			this.narrowDownSelector.groupTable       = groupTable;
		}
	},

	narrowDown : function (group) {
		var sel1 = this.narrowDownSelector.selector;
		var sel2 = this.narrowDownSelector.population;
		var buff = this.narrowDownSelector.buffer;
		var gtbl = this.narrowDownSelector.groupTable;
		if (!sel1 || !sel2 || !buff || !gtbl) return;
		if (JLJS.env.isIE) {
			while (sel2.options[0]) {
				sel2.options.remove(0);
			}
		} else {
			while (sel2.firstChild) {
				sel2.removeChild(sel2.firstChild);
			}
		}
		var i = 0, ptn = (group && gtbl[group]) ? new RegExp('^(' + gtbl[group].join('|') + ')$') : /.*/;
		for (var value in buff) {
			if (value.match(ptn)) {
				var cOPT   = document.createElement('option');
				cOPT.value = value;
				cOPT.text  = buff[value];
				if (JLJS.env.isIE) {
					sel2.add(cOPT, i);
					i++;
				} else {
					sel2.appendChild(cOPT);
				}
			}
		}
		sel2.options[0].selected = true;
	},

	setTransitionBehavior : function (urlTable) {
		if (typeof urlTable != 'object') return;
		this.transitionURLs = urlTable;
		JLJS.addEvent(this.formNode, 'submit', function(e) {
			e.preventDefault();
			e.target._super_.jumpByUserSelect();
		});

		// measure for MacIE's ugry behavior..
		var btn = JLJS.getElementsByClassName('image', 'input', this.formNode)[0];
		if (btn && JLJS.env.isIE && JLJS.env.isMac) {
			JLJS.addEvent(btn, 'click', function(e) {
				e.preventDefault();
				e.stopPropagation();
				var form = (function(node) {
					return (node.nodeName.toLowerCase() == 'form') ?
						node :
						(node.parentNode) ?
							arguments.callee(node.parentNode) :
							null;
				})(e.target);
				if (form) form._super_.jumpByUserSelect();
			});
		}
	},
	
	jumpByUserSelect : function () {
		if (typeof this.transitionURLs != 'object') return;
		var sel1 = this.narrowDownSelector.selector.value;
		var sel2 = this.narrowDownSelector.population.value;
		var url  = (this.transitionURLs[sel1] && this.transitionURLs[sel1][sel2]) ? this.transitionURLs[sel1][sel2] : null;
		if (url && typeof url == 'string') location.href = url;
	}
};

function JLJS_GlobalNaviRegionSelectorSetup () {
	var targetBlockID  = 'globalHeaderGL02';
	var narrowDownPair = ['PRM_REGION','PRM_LANG'];
	var languageGroups = {
		'AME' : ['ja', 'en'],
		'BRA' : ['pt'],
		'EUR' : ['xx', 'ja', 'en', 'fr', 'de', 'it', 'es'],
		'JPN' : ['ja', 'en'],
		'ASA' : ['ja', 'en'],
		'AU'  : ['ja', 'en'],
		'ID'  : ['ja', 'en'],
		'MP'  : ['ja', 'en'],
		'CN'  : ['ja', 'en', 'zh-cn'],
		'KR'  : ['ja', 'en', 'ko'],
		'SG'  : ['ja', 'en'],
		'GU'  : ['ja', 'en'],
		'MY'  : ['ja', 'en'],
		'TW'  : ['ja', 'zh-tw'],
		'HK'  : ['ja', 'en', 'zh-hk'],
		'NZ'  : ['ja', 'en'],
		'TH'  : ['ja', 'en'],
		'IN'  : ['ja', 'en'],
		'PH'  : ['ja', 'en'],
		'VN'  : ['ja', 'en']
	};

	var block = document.getElementById(targetBlockID);
	var form  = (block) ? JLJS.getElementsByTagName('form', block)[0] : null;

	if (form) {
		var regionSelector = new JLJS_GlobalNaviRegionSelector(form);
		regionSelector.setNarrowDownSelector(narrowDownPair[0], narrowDownPair[1], languageGroups);
//		JLJS.classAttr.add(regionSelector.formNode, 'pseudo-scriptEnabled');

		if (regionSelector.narrowDownSelector.selector) {
			regionSelector.narrowDownSelector.selector.options[0].selected = true;
			regionSelector.narrowDown();
		}

		if (regionSelector.narrowDownSelector.selector) {
			JLJS.addEvent(regionSelector.narrowDownSelector.selector, 'change', function (e) {
				e.target._super_.narrowDown(e.target.value);
			});
		}

		regionSelector.setTransitionBehavior(JLJS_GLOBALNAVI_REGIONSELECTOR_URLS);
	}
}

JLJS.addOnload(JLJS_GlobalNaviRegionSelectorSetup);

