/*__________________________________________________________

   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.';
/* ------ 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;
		}

		this.setCookie();
		this.formNode.submit();
	}
};

function JLJS_JMBMemberLoginFormSetup () {
	window.JLJSJMBLoginForm = null;
	var form  = document.getElementById("memberLogin");
	if (form) {
		JLJSJMBLoginForm = new JLJS_JMBMemberLoginForm(form);
		JLJS.addEvent(JLJSJMBLoginForm.formNode, 'submit', function (e) {
			e.preventDefault();
			JLJSJMBLoginForm.checkInputData();
			setTimeout( 'JLJSJMBLoginForm.memberPwField.value="";', 2000 );
		} );
	}
}

JLJS.addOnload(JLJS_JMBMemberLoginFormSetup);
