﻿if(typeof cj == "undefined") cj = {};
cj.evt = {
	guid : 1,
	add : function (element, type, handler)
	{
		if(element.addEventListener){
			element.addEventListener(type, handler, false);
		}else if (element.attachEvent)
		{
			element.attachEvent("on" + type, handler);
		}else{
			if (!handler.$$guid)
			{
				handler.$$guid = this.guid++;
			}
			if (!element.events)
			{
				element.events = {};
			}
			var handlers = element.events[type];
			if (!handlers)
			{
				handlers = element.events[type] = {};
				if(element["on" + type]){
					handlers[0] = element["on" + type];
				}
			}
			handlers[handler.$$guid] = handler;
			element["on" + type] = this.handleEvent;
		}
	},
	remove : function(element, type, handler)
	{
		if (element.removeEventListener)
		{
			element.removeEventListener(type, handler, false);
		}else
		{
			if (element.events && element.events[type])
			{
				delete element.events[type][handler.$$guid];
			}
		}
	}
}
cj.util = {
	getMousePos : function(ev){
		var ev = ev ? ev : window.event;
		return (ev.pageX && ev.x) ? {"x":ev.pageX, "y":ev.pageY} : {"x":ev.clientX+this.getScrollPos().left, "y":ev.clientY+this.getScrollPos().top}
	},
	getScrollPos : function(){
		var top = left = 0;
		top = document.documentElement.scrollTop || document.body.scrollTop;
		left = document.documentElement.scrollLeft || document.body.scrollLeft;
		return {"top" : top, "left" : left}
	},
	getWindowSize : function(){
		var width = height = 0;
		width = document.documentElement.clientWidth || document.body.clientWidth;
		height = document.documentElement.clientHeight || document.body.clientHeight;
		return {"width" : width, "height" : height}
	},
   	getElementPos : function(elm){
   		var top = left = 0;
   		while(elm != null){
   			if(elm.style.overflow == ''){
	   			top += elm.offsetTop;
	   			left += elm.offsetLeft;
	   		}else{
   				top -= elm.scrollTop;
   			}
   			elm = elm.offsetParent;
   		}
   		return {"top" : top, "left" : left}
   	},
   	getPos : function(elm){
   		var top = left = 0;
   		while(elm != null){
   			try{
   				if(elm.style.position=='absolute'){
   					break;
   				}
   			}catch(ev){}
	   		top += elm.offsetTop;
	   		left += elm.offsetLeft;
   			elm = elm.offsetParent;
   		}
   		return {"top" : top, "left" : left} ;
   	},
	loadJs : function(src){
		var js = document.createElement("script");
		js.src = src;
		js.type = "text/javascript";
		document.getElementsByTagName("head")[0].appendChild(js);
	},
	fix_ieflash : function() {
		var objects = document.getElementsByTagName("object"); 
		for (var i=0;i<objects.length;i++)
			objects[i].outerHTML = objects[i].outerHTML;
	},
	popMsg : {
		div : null,
		move : 10,
		timer : null,
		holdTime : 3 * 1000,
		init : function(msg, pos, size, holdTime){
			if (typeof(size)=="undefined"){
				this.size = {width:200, height:20};
			}else{
				this.size = size;
			}
			if (typeof(holdTime)!="undefined"){
				this.holdTime = holdTime * 1000;
			}
			this.clear();
			this.div = document.createElement("div");
			this.div.className = "popMsg";
			this.div.innerHTML = msg;
			document.body.appendChild(this.div);
			this.div.style.left = pos.left + "px";
			this.div.style.top = pos.top + "px";
			this.show(1);
		},
		show : function(step){
			if(this.div.offsetWidth < this.size.width){
				this.div.style.width = Math.abs((this.size.width/this.move)*step) + "px";
				this.div.style.height = Math.abs((this.size.height/this.move)*step) + "px";
				this.timer = setTimeout( function(){ cj.util.popMsg.show(++step) }, 1 );
			}else{
				this.timer = setTimeout( function(){cj.util.popMsg.clear()}, this.holdTime);
			}
		},
		clear : function(){
			if (this.div){
				clearTimeout(this.timer);
				this.timer = null;
				document.body.removeChild(this.div);
				this.div = null;
			}
		}
	}
	,createElm : function(tagName, set){
		var obj = document.createElement(tagName)
		if(set.cssText)		obj.style.cssText = set.cssText;
		if(set.classNeme)	obj.className = set.className;
		if(set.innerHTML)	obj.innerHTML = set.innerHTML;
		if(set.style){
			for(k in set.style){
				obj.style[k] = set.style[k];
			}
		}
		return obj;
	}
}
cj.roller = {
	holdtime : 6000,
	rolltime : 70,  /*這個數字決定速度*/
	ar : 0.99,
	gap : 7,
	init : function(boxid, itemcontainerid){
		var box = document.getElementById(boxid);
		if (!box){
			return;
		}
		var items = document.getElementById(itemcontainerid);
		if(!items)	return;
		box.items = items.getElementsByTagName('table');
		if(!box.items || box.items.length==0){
			return;
		}
		box.index = 0;
		box.timer = null;
		
		box.div1 = document.createElement("div");
		box.div1.style.cssText = "position:absolute; top:0px; left:0px;";
		box.div2 = box.div1.cloneNode(true);
		box.div3 = box.div1.cloneNode(true);
		box.appendChild(box.div1);
		box.appendChild(box.div2);
		box.appendChild(box.div3);
		
		this.roll(box);
	},
	roll : function(obj){
		while(obj.div1.childNodes[0])
			obj.div1.removeChild(obj.div1.childNodes[0]);
		obj.div1.appendChild(obj.items[obj.index].cloneNode(true));
		obj.div1.style.top = 0 + "px";
		
		if(obj.items.length <2){
			return;
		}
		while(obj.div2.childNodes[0])
			obj.div2.removeChild(obj.div2.childNodes[0]);
		obj.div2.appendChild(obj.items[(obj.index+1)%obj.items.length].cloneNode(true));
		obj.div2.style.top = obj.div1.offsetHeight + 10 + "px";
		
		if(obj.items.length>2){
			obj.index  = (obj.index == obj.items.length -1) ? 0 : obj.index +1;
			while(obj.div3.childNodes[0])
				obj.div3.removeChild(obj.div3.childNodes[0]);
			obj.div3.appendChild(obj.items[(obj.index+1)%obj.items.length].cloneNode(true));
			obj.div3.style.top = 2*obj.div1.offsetHeight + this.gap + 10 + "px";
			
			obj.timer = setTimeout(function(){cj.roller.rollNext(obj, cj.roller.ar)}, cj.roller.holdtime);
		}
	},
	rollNext : function(obj, ar){
		var top = Math.floor(Math.max(0, obj.div2.offsetTop* ar));
		obj.div1.style.top = top - obj.div1.offsetHeight - this.gap + "px";
		obj.div2.style.top = top + "px";
		obj.div3.style.top = top + obj.div1.offsetHeight + this.gap + "px";
		if(!top){
			this.roll(obj);
		}else{
			obj.timer = setTimeout(function(){cj.roller.rollNext(obj, ar*cj.roller.ar)}, this.rolltime);
		}
	}
}
cj.fade = {
	fps : 10,
	otype : false,
	start : function(obj, time, current, end){
		var steps = (end - current) / (time * this.fps) ;
		this.getotype(obj);
		if (this.otype != 'none'){
			this.setfade(obj, current);
			this.dofade(steps, obj, current, end);
		}
	},
	dofade : function (steps, obj, current, end){
		var dir = (steps > 0);
		current += steps;
		this.setfade(obj, current);
		
		if (dir ^ (current - end > 0))
		{
			setTimeout(
				function()
				{
					cj.fade.dofade(steps, obj, current, end);
				},
				1000 / this.fps
			);
		}else{
			this.sdo=0;
		}
	},
	setfade : function (obj, value){
		if (!this.otpye){
			this.getotype(obj);
		}
		switch(this.otype)
		{
			case 'ie':
				obj.filters.alpha.opacity = Math.floor(value * 100);
			break;
			
			case 'khtml':
				obj.style.KhtmlOpacity = value;
			break;
			
			case 'moz':
				obj.style.MozOpacity = (value == 1) ? 0.9999999 : value;
			break;
			
			default:
				obj.style.opacity = (value == 1) ? 0.9999999 : value;
			break;
		}
	},
	getotype : function(obj){
		if (typeof obj.filters == 'object'){
			this.otype = (obj.filters.length >0
				&& typeof obj.filters.alpha == 'object'
				&& typeof obj.filters.alpha.opacity == "number")
				? 'ie' : 'none';
		}else if(typeof obj.style.opacity != 'undefined'){
			this.otype = 'w3c';
		}else if (typeof obj.style.MozOpacity != 'undefined'){
			this.otype = 'moz';
		}else if (typeof obj.style.KhtmlOpacity != 'undefined'){
			this.otype = 'khtml';
		}else{
			this.otype = false;
		}
		return false;
	}
}
cj.slidshow = {
	index : 0,
	objId : "slidshow",
	obj : new Object(),
	imgs : new Array(),
	rolltime : 10, //每隔10秒下一張(必需大於淡出入的秒數)
	init : function(){
		if (typeof slidImgs == "undefined" || slidImgs == ""){
			return;
		}
		this.obj = document.getElementById(this.objId);
		if (!this.obj){
			return;
		}
		
		//clear
		while(this.obj.childNodes[0]){
			this.obj.removeChild(this.obj.childNodes[0]);
		}
		var a = slidImgs.split(',');
		var top = 0;
		for (var i=0; i<a.length; i++){
			this.imgs[i] = document.createElement("img");
			this.imgs[i].src = a[i];
			this.imgs[i].style.cssText = "position:absolute; top: 0px; left: 0px; filter:alpha(opacity=0); visibility:hidden";
			this.obj.appendChild(this.imgs[i]);
			cj.fade.setfade(this.imgs[i], 0);
		}
		cj.fade.setfade(this.imgs[0], 1);
		this.imgs[0].style.visibility = 'visible'
		if(a.length>1)
			setTimeout("cj.slidshow.roll()", 5* 1000); //第一次等5秒就開始
	},
	roll : function(){
		this.index  = (this.index == this.imgs.length -1) ? 0 : this.index +1;
		var pre = (this.index) ? this.index -1 : this.imgs.length -1;
		for (i=0; i<this.imgs.length; i++){
			if(i==this.index || i==pre){
				this.imgs[i].style.visibility = 'visible';
			}else{
				this.imgs[i].style.visibility = 'hidden';
			}
		}
		//原來的圖 : 1--> 總共1秒， 0.8->0透明度從0.8->0
		//cj.fade.setfade(this.imgs[pre], 0);
		cj.fade.start(this.imgs[pre], 1, 0.9, 0); //前一張淡出執行1秒
		//下一張圖 : 2--> 總共2秒， 0.2->1透明度從0.2->1
		cj.fade.start(this.imgs[this.index], 3, 0.1, 1); //下一張淡入3秒
		setTimeout("cj.slidshow.roll()", this.rolltime * 1000);
	}
}
cj.drag = {
	draging : false,
	offsetX : 0,
	offsetY : 0,
	dragObj : null,
	start : function(ev){
		var ev = ev || window.event;
		var tgt = ev.target || ev.srcElement;
		if(typeof tgt.attrib != "undefined" && tgt.attrib == "dragable"){
			cj.drag.dragObj = tgt;
			cj.drag.draging = false;
			cj.drag.offsetY = (cj.util.getMousePos(ev)).y - tgt.offsetTop;
			cj.drag.offsetX = (cj.util.getMousePos(ev)).x - tgt.offsetLeft;
			cj.evt.add(document, "mousemove", cj.drag.drag);
			cj.evt.add(document, "mouseup", cj.drag.end);
		}
		return false;
	},
	drag : function(ev){
		if (!cj.drag.dragObj){
			return false;
		}
		cj.drag.dragObj.style.top = (cj.util.getMousePos(ev)).y - cj.drag.offsetY + "px";
		cj.drag.dragObj.style.left = (cj.util.getMousePos(ev)).x - cj.drag.offsetX + "px";
		if(typeof cj.drag.dragObj.mask != "undefined" && cj.drag.dragObj.mask){
			cj.drag.dragObj.mask.style.top = cj.drag.dragObj.style.top;
			cj.drag.dragObj.mask.style.left = cj.drag.dragObj.style.left;
		}
		cj.drag.draging = true;
		return false;
	},
	end : function(ev){
		if (!cj.drag.dragObj){
			return;
		}
		cj.drag.dragObj = null;
		cj.drag.draging = false;
		cj.evt.remove(document, "mousemove", cj.drag.drag);
		cj.evt.remove(document, "mouseup", cj.drag.end);
		return false;
	}
}
cj.form = {
	chk : function(f, fields){
		if(!f)	return false;
		if(typeof f.lang == "undefined" || !f.lang){
			alert("lang not set");
			return false;
		}
		var lang = f.lang.value;
		if( typeof cj.form.msg == "undefined"){
			alert("cj.form.msg not set or lang.js not load");
			return false;
		}
		
		for (var i=0; i<fields.length; i++){
			var a = fields[i].split(',');
			var obj = f[a[0]];
			if(!obj){
				alert(a[0] + " not set");
				return false;
			}
			var type = a[1];
			var title = obj.getAttribute("title");
			if(!title){
				alert(a[0] + " title not set");
				return false;
			}
			switch (type){
				case "num":
					obj.value = cj.form.trim(obj.value);
					if( !cj.form.isNumber(obj.value)){
						obj.focus();
						var pos = cj.util.getElementPos(obj);
						pos.left +=  50;
						cj.util.popMsg.init(title + cj.form.msg.not_number[lang], pos, {"width":200, "height":20});
						return false;
					}
				case "text":
					if( obj.value == "" ){
						obj.focus();
						var pos = cj.util.getElementPos(obj);
						pos.left +=  50;
						cj.util.popMsg.init(title + cj.form.msg.not_null[lang], pos, {"width":200, "height":20});
						return false;
					}
				break;
				case "email":
					if(!cj.form.isEmail(obj.value)){
						obj.focus();
						var pos = cj.util.getElementPos(obj);
						pos.left +=  50;
						cj.util.popMsg.init(cj.form.msg.valid[lang] + title, pos, {"width":200, "height":20});
						return false;
					}
				break;
				case "account":
					if(!cj.form.isAccount(obj.value)){
						obj.focus();
						var pos = cj.util.getElementPos(obj);
						pos.left +=  50;
						cj.util.popMsg.init(cj.form.msg.valid[lang] + title, pos, {"width":200, "height":20});
						return false;
					}
				break;
				case "password":
					if(!cj.form.isPasswd(obj.value)){
						obj.focus();
						var pos = cj.util.getElementPos(obj);
						pos.left +=  50;
						cj.util.popMsg.init(cj.form.msg.valid[lang] + title, pos, {"width":200, "height":20});
						return false;
					}
					if(obj.getAttribute('confirm')){
						var passwd2 = f[obj.getAttribute("confirm")];
						if(passwd2){
							if(obj.value != passwd2.value){
								passwd2.focus();
								var pos = cj.util.getElementPos(obj);
						pos.left +=  50;
								cj.util.popMsg.init(cj.form.msg.valid[lang] + passwd2.getAttribute("title"), pos, {"width":200, "height":20});
								return false;
							}
						}
					}
				break;
				case "checkbox":
					if(!obj.checked){
						obj.focus();
						var pos = cj.util.getElementPos(obj);
						pos.left +=  50;
						cj.util.popMsg.init(obj.getAttribute("title"), pos, {"width":200, "height":20});
						return false;
					}
				break;
				case "creditCard":
					var cardname = document.getElementById(obj.getAttribute("cardName"));
					if (!cardname){
						alert('Attribute : cardName not set');
						return false;
					}
					if(!cj.form.checkCreditCard.check(cardname.value, obj.value)){
						obj.focus();
						var pos = cj.util.getElementPos(obj);
						pos.left +=  50;
						cj.util.popMsg.init(cj.form.msg.valid[lang] + title, pos, {"width":200, "height":20});
						return false;
					}
				break;
				case "sid":
					if(!cj.form.isSid(obj.value)){
						obj.focus();
						var pos = cj.util.getElementPos(obj);
						pos.left +=  50;
						cj.util.popMsg.init(cj.form.msg.valid[lang] + title, pos, {"width":200, "height":20});
						return false;
					}
				break;
			}
		}
		return true;
	},
	isEmail : function(elm){
		re = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/
		return elm.match(re)
	},
	isAccount : function(obj){
		if (obj.length <4){
			return false;
		}
		var exp = /^[a-zA-Z]/;
		if(!exp.exec(obj)){
			return false;
		}
		return true;
	},
	isPasswd : function(obj){
		if (obj.length <4){
			return false
		}
		var exp = /['"]/;
		if(exp.exec(obj)){
			return false;
		}
		return true;
	},
	isNumber : function(obj){
		var exp = /^\d+\.?\d*$/;
		return (exp.exec(obj));
	},
	isSid : function(id){
		code = [10,11,12,13,14,15,16,17,34,18,19,20,21,22,35,23,24,25,26,27,28,29, 0,30,31, 0];
		id = id.toLowerCase();
		if (id.length != 10) return false;
		re = /[a-z]/
		if( !id.charAt(0).match(re) ) return false;
		re = /[0-9]/
		for (i=1; i<10; i++){
			if ( !id.charAt(i).match(re) ){
				return false;
			}
		}
		id0 = code[id.charAt(0).charCodeAt(0)-97];
		if (!id0) return false;
		return !((10-id.charAt(9)-(Math.floor(id0/10) +(id0%10)*9 + id.charAt(1)*8 + id.charAt(2)*7 + id.charAt(3)*6 + id.charAt(4)*5 + id.charAt(5)*4 + id.charAt(6)*3 + id.charAt(7)*2 + id.charAt(8)*1)%10)%10);
	},
	trim : function(obj){
		return obj.replace(/\s/g, '');
	},
	msg : {
		not_null : {
			chi : " 必填 ",
			en : " is required! ",
			jp : "をご入力ください",
			kor : " 필수입력"
		},
		valid : {
			chi	: " 請輸入有效的 ",
			en		: " Please specify valid ",
			jp : " Please specify valid ",
			kor : " Please specify valid " 
		},
		day : {
			chi	: "日期",
			en		: "Date",
			jp		: "Date",
			kor		: "Date"
		},
		not_number : {
			chi	: " 不是數字",
			en		: " is not number",
			jp		: " is not number",
			kor		: " is not number"
		}
	}
}
cj.calendar = {
	init : function(trigger){
		var a = document.getElementById(trigger);
		if(!a)	return;
		var tgt = a.getAttribute('tgt');
		a.y = document.getElementById(tgt+'Year');
		a.m = document.getElementById(tgt+'Month');
		a.d = document.getElementById(tgt+'Day');
		if(!a.y || !a.m || !a.d)	return;
		
		//select onchange --hide
		cj.evt.add(a.y, "change", function(){a.hide()});
		cj.evt.add(a.m, "change", function(){a.hide()});
		cj.evt.add(a.d, "change", function(){
			a.d.selectedIndex = Math.min(a.d.selectedIndex, cj.calendar.solarDays(a.y.value, a.m.value)-1);
			a.hide();
		});
		
		a.closeH = 13;
		a.bolder = 2;
		a.boxW = 30;
		a.boxH = 25;
		
		a.draw = cj.calendar.draw;
		a.drawBox = cj.calendar.drawBox;
		a.show = cj.calendar.show;
		a.hide = cj.calendar.hide;
		a.drawCaption = cj.calendar.drawCaption;
		a.setDate = cj.calendar.setDate;
		a.setH = cj.calendar.setH;
		a.setTheDay = cj.calendar.setTheDay;
		
		cj.evt.add(a, "click", function(){a.show()});
		a.style.cursor = "pointer";
		
		a.win = document.createElement("div");
		a.win.id = "calendar";
		var width = 7*a.boxW + 2*a.bolder + 6;
		a.win.style.width = width + "px";
		a.win.style.top = (cj.util.getElementPos(a)).top + a.offsetHeight + 5 + "px";
		a.win.style.left = (cj.util.getElementPos(a)).left + a.offsetWidth - width +  "px";
		
		//drag
		a.win.attrib = "dragable";
		cj.evt.add(a.win, "mousedown", cj.drag.start);
		
		if(document.attachEvent){
			a.win.mask = document.createElement("iframe");
			a.win.mask.style.cssText = "position:absolute; display:none; border:0px;";
			a.win.mask.style.width = a.win.style.width;
			a.win.mask.style.top = a.win.style.top;
			a.win.mask.style.left = a.win.style.left;
			document.body.appendChild(a.win.mask);
		}
		
		//close
		a.closer = document.createElement("div");
		a.closer.parent = a;
		a.closer.id = "calCloser";
		a.closer.style.top = 1 + "px";
		a.closer.style.right = 1 + "px";
		cj.evt.add(a.closer, "click", function(evt){
			var evt = evt || window.event;
			tgt = evt.target || evt.srcElement;
			tgt.parent.hide();
			});
		a.win.appendChild(a.closer);
		
		//lastYear
		a.lastYear = a.drawBox('calLast', 0, 0, 1, 1, "&#60;&#60");
		a.lastYear.parent = a;
		a.lastYear.style.cursor = "pointer";
		a.lastYear.onclick = cj.calendar.lastYear;
		a.win.appendChild(a.lastYear);
		//lastMonth
		a.lastMonth = a.drawBox('calLast', 0, 1, 1, 1, "&#60;");
		a.lastMonth.parent = a;
		a.lastMonth.style.cursor = "pointer";
		a.lastMonth.onclick = cj.calendar.lastMonth;
		a.win.appendChild(a.lastMonth);
		
		//caption
		a.caption = a.drawBox('calCaption', 0, 2, 3, 1, "");
		a.win.appendChild(a.caption);
		document.body.appendChild(a.win);
		
		//nextMonth
		a.nextMonth = a.drawBox('calNext', 0, 5, 1, 1, "&#62;");
		a.nextMonth.parent = a;
		a.nextMonth.style.cursor = "pointer";
		a.nextMonth.onclick = cj.calendar.nextMonth;
		a.win.appendChild(a.nextMonth);
		//nextYear
		a.nextYear = a.drawBox('calNext', 0, 6, 1, 1, "&#62;&#62");
		a.nextYear.parent = a;
		a.nextYear.style.cursor = "pointer";
		a.nextYear.onclick = cj.calendar.nextYear
		a.win.appendChild(a.nextYear)
		
		//nav
		a.nav = new Array();
		var days = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
		for (var i=0; i<days.length; i++){
			a.nav[i] = a.drawBox("calNav", 1, i, 1, 1, days[i]);
			a.win.appendChild(a.nav[i]);
		}
		document.body.appendChild(a.win);
	},
	draw : function(){
		this.setTheDay();
		this.drawCaption();
		this.setH();
		
		if (typeof this.box != "undefined"){
			for(i in this.box){
				this.win.removeChild(this.box[i]);
			}
			this.box = null;
		}
		this.box = new Array();
		for (var i=0; i<7*this.rows; i++){
			id = (i%7==0 || i%7==6) ? "calweekend" : "";
			var day = (i - this.firstWeekDay) + 1;
			if (day < 1 || day > this.monthDay)
				day = "";
			this.box[i] = this.drawBox(id, Math.floor(i/7)+2, (i%7), 1, 1, day);
			this.box[i].parent = this;
			if (day){
				this.box[i].style.cursor = "pointer";
				this.box[i].onclick = this.setDate;
			}
			if (day == this.d.value)
					this.box[i].id = "caltheday";
			this.win.appendChild(this.box[i]);
		}
	},
	setTheDay : function(){
		this.monthDay = cj.calendar.solarDays(this.y.value, this.m.value);
		this.firstWeekDay = (new Date(this.y.value + "/" + this.m.value + "/1")).getDay(); 
	},
	setH : function(){
		this.rows =  Math.ceil((this.monthDay + this.firstWeekDay)/7);
		this.win.style.height = 2*this.bolder + this.closeH + (this.rows+2)*(this.boxH+1) + "px";
		if(typeof this.win.mask != "undefined"){
			this.win.mask.style.height = this.win.style.height;
		}
	},
	drawCaption : function(){
		this.caption.innerHTML = this.y.value + "-" + this.m.value;
	},
	show : function(){
		if(typeof this.win.mask != "undefined"){
			this.win.mask.style.display = "block";
		}
		this.win.style.display = "block";
		this.draw();
		return false;
	},
	hide : function(){
		if(typeof this.win.mask != "undefined")
			this.win.mask.style.display = "none";
		this.win.style.display = "none";
	},
	setDate : function(){
		this.parent.d.selectedIndex = this.innerHTML - 1;
		this.parent.hide();
		if(typeof this.parent.d.onchange == "function")
			this.parent.d.onchange();
	},
	lastYear : function(){
		if (this.parent.y.selectedIndex == 0)	return;
		this.parent.y.selectedIndex --;
		this.parent.monthDay = cj.calendar.solarDays(this.parent.y.value, this.parent.m.value);
		this.parent.firstWeekDay = (new Date(this.parent.y.value + "/" + this.parent.m.value + "/1")).getDay(); 
		this.parent.draw();
	},
	nextYear : function(){
		if (this.parent.y.selectedIndex >= this.parent.y.options.length-1){
			return false;
		}
		this.parent.y.selectedIndex++;
		this.parent.monthDay = cj.calendar.solarDays(this.parent.y.value, this.parent.m.value);
		this.parent.firstWeekDay = (new Date(this.parent.y.value + "/" + this.parent.m.value + "/1")).getDay(); 
		this.parent.draw();
	},
	lastMonth : function(ev){
		if (this.parent.y.selectedIndex == 0 && this.parent.m.selectedIndex == 0)	return;
		if (this.parent.m.selectedIndex == 0){
			this.parent.y.selectedIndex --;
			this.parent.m.selectedIndex = 11;
		}else{
			this.parent.m.selectedIndex --;
		}
		this.parent.monthDay = cj.calendar.solarDays(this.parent.y.value, this.parent.m.value);
		this.parent.firstWeekDay = (new Date(this.parent.y.value + "/" + this.parent.m.value + "/1")).getDay(); 
		this.parent.draw();
	},
	nextMonth : function(){
		if (this.parent.y.selectedIndex >= this.parent.y.options.length-1 && this.parent.m.selectedIndex >= this.parent.m.options.length-1){
			return false;
		}
		if (this.parent.m.selectedIndex == 11){
			this.parent.y.selectedIndex++;
			this.parent.m.selectedIndex = 0;
		}else{
			this.parent.m.selectedIndex++;
		}
		this.parent.monthDay = cj.calendar.solarDays(this.parent.y.value, this.parent.m.value);
		this.parent.firstWeekDay = (new Date(this.parent.y.value + "/" + this.parent.m.value + "/1")).getDay(); 
		this.parent.draw();
	},
	drawBox : function (id, t, l, w, h, htm){
		var obj = document.createElement("div");
		obj.id = id;
		obj.style.top = this.bolder + this.closeH + t*(this.boxH+1) + "px";
		
		obj.style.left = this.bolder + l*(this.boxW+1) + "px";
		obj.style.width = w * (this.boxW+1) - 1 + "px";
		obj.style.height = h * (this.boxH+1) -1 + "px";
		obj.innerHTML = htm;
		return obj;
	},
	solarDays : function (y,m){
		var solarMonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
		if( m == 2){
			return (( y%4 == 0) && (y%100 !=0) || (y%400 == 0)) ? 29 : 28;
		}else{
			return solarMonth[m-1];
		}
	}
}
cj.dropdownMenu1 = {
	timer : null,
	items : [],
	ar : 0.99,
	init: function(menu, item, dir){
		if(typeof dir == 'undefined') dir = 'y';
		var mm, sm;
		for (var i=0; i<menu.length; i++){
			mm = document.getElementById(menu[i]);
			sm = document.getElementById(item[i]);
			if (typeof mm != "undefined" && typeof sm != "undefined" &&mm&&sm){
				sm.timer = null;
				sm.opened = false;
				width = Math.max(100, sm.offsetWidth) + 25;
				mm.sm = sm;
				mm.smid = sm.id
				mm.appendChild(sm);
				sm.style.width = width + 'px';
				try{(sm.getElementsByTagName('table'))[0].style.width = width + 'px';}catch(ev){};
				
				var t = mm.getAttribute('fixed')
				if(t && t==1){
					sm.style.visibility = 'visible';
				}else{
					this.items[this.items.length] = sm;
					mm.onmouseover = function(){ cj.dropdownMenu.show(this.sm) }
					mm.onmouseout = cj.dropdownMenu.hide;
				}
				
				
				if(dir=='y'){
					mm.sm.style.top = cj.util.getElementPos(mm).top + mm.offsetHeight + "px";
					mm.sm.style.left = cj.util.getElementPos(mm).left + "px";
				}else{
					if(document.all){
						mm.sm.style.left = mm.offsetWidth + "px";
						mm.sm.style.top = mm.offsetTop + 'px';
					}else{
						mm.sm.style.top = cj.util.getElementPos(mm).top + "px";
						mm.sm.style.left = cj.util.getElementPos(mm).left + mm.offsetWidth + "px";
					}
				}
			}
		}
	},
	show : function(sm){
		for(i=0; i<this.items.length; i++){
			if(this.items[i] == sm){
				this.items[i].opened = true;
			}
		}
		for(i=0; i<this.items.length; i++){
			if(this.items[i].opened){
				this.items[i].style.visibility = 'visible';
			}else{
				clearTimeout(this.items[i].timer);
				this.items[i].style.visibility = 'hidden';
			}
		}
	},
	hide : function(ev){
		var ev = ev || event;
		var to = ev.relatedTarget || ev.toElement;
		obj = to;
		while(obj != null){
			if(obj==this){
				return;
			}
			obj = obj.parentNode;
		}
		var sm = this.sm;
		sm.opened = false;
		sm.timer = setTimeout(function(){sm.style.visibility = 'hidden'}, 1000);
	}
}
cj.dropdownMenu = {
	timer : null,
	menus : [],
	gap : 3,
	init: function(menu, item, dt){
		for (var i=0; i<menu.length; i++){
			mm = document.getElementById(menu[i]);
			sm = document.getElementById(item[i]);
			if (mm&&sm){
				sm.mm = mm
				sm.style.position = 'absolute';
				sm.style.width = Math.max(100, sm.offsetWidth) + 20;
				(sm.getElementsByTagName('table'))[0].style.width = sm.offsetWidth + 'px';
				
				mm.index = this.menus.length;
				this.menus[mm.index] = mm;
				mm.dt = dt;
				mm.timer = null;
				mm.op = false;
				mm.appendChild(sm);
				mm.sm = sm
				
				mm.onmouseover = mm.onfocus = function(){ cj.dropdownMenu.show(this.index) }
				mm.onmouseout = mm.onblur = function(ev){
					var ev = ev || window.event;
					var to = ev.relatedTarget || ev.toElement
					while(to != null){
						if(to == this){
							return false;
						}
						to = to.parentNode;
					}
					cj.dropdownMenu.hide(this.index);
						
				}
			}
		}
	},
	show : function(index){
		var obj = this.menus[index];
		do{
			obj.op = true;
			if(typeof obj.mm!="undefined"){
				obj = obj.mm;
			}else{
				obj = null;
			}
		}while(obj!=null);
		for(i=0; i<this.menus.length; i++){
			var obj = this.menus[i];
			if(obj.op){
				this.setPos(index);
				obj.sm.style.visibility = 'visible';
			}else{
				clearTimeout(obj.timer);
				obj.sm.style.visibility = 'hidden';
			}
		}
	},
	hide : function(index){
		var mm = this.menus[index];
		mm.op = false;
		mm.timer = setTimeout(function(){mm.sm.style.visibility = 'hidden'}, 800);
	},
	setPos : function(index){
		var mm = this.menus[index];
		var sm = mm.sm
		var wt = cj.util.getScrollPos().top + cj.util.getWindowSize().height;
		var wl = cj.util.getScrollPos().left + cj.util.getWindowSize().width;
			
		var pos = cj.util.getPos(mm);
		if(mm.dt == 'y'){
			var dh = cj.util.getElementPos(mm).top + mm.offsetHeight + sm.offsetHeight - wt;
			var dw = cj.util.getElementPos(mm).left + sm.offsetWidth - wl ;
			if(dh>0){
				pos.top -= sm.offsetHeight;
			}else{
				pos.top += mm.offsetHeight;
			}
			if(dw>0){
				pos.left -= sm.offsetWidth - mm.offsetWidth;
			}
		}else{
			var dh = cj.util.getElementPos(mm).top + sm.offsetHeight - wt;
			var dw = cj.util.getElementPos(mm).left + mm.offsetWidth + sm.offsetWidth - wl ;
			if(dh>0){
				pos.top -= sm.offsetHeight - mm.offsetHeight;
			}
			if(dw>0){
				pos.left -= sm.offsetWidth;
			}else{
				pos.left += mm.offsetWidth;
			}
		}
		sm.style.top = pos.top + "px";
		sm.style.left = pos.left + "px";
	}
}
cj.xmlHttp = {
	req : false,
	init : function(){
		var factories = [
			function() { return new XMLHttpRequest()}
			,function(){ return new ActiveXObject("Msxml2.XMLHTTP")}
			,function(){ return new ActiveXObject("Msxml3.XMLHTTP")}
			,function(){ return new ActiveXObject("Microsoft.XMLHTTP")}
			];
		for (var i=0; i<factories.length; i++){
			try{
				this.req = factories[i]();
			}
			catch(e){
				continue;
			}
			break;
		}
		return (this.req) ? true : false;
	},
	sendRequest : function(URL, callback, postData){
		this.init();
		if (!this.req) return;
		method = (postData) ? "POST" : "GET";
		this.req.open(method, URL, true);
		this.req.setRequestHeader('User-Agent', 'XMLHTTP');
		if (postData){
			this.req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		}
		this.req.onreadystatechange = function(){
			if (cj.xmlHttp.req.readyState != 4) return;
			if (cj.xmlHttp.req.status != 200 && cj.xmlHttp.req.status != 304){
				alert('HTTP error ' + cj.xmlHttp.req.status);
				return;
			}
			callback(cj.xmlHttp.req);
		}
		if (this.req.readyState == 4) return;
		this.req.send(postData);
	}
}
cj.colorPicker = {
	size : { w:10, h:10, gap:3}
	,init : function(trigger){
		var a = document.getElementById(trigger);
		if(!a) return;
		var tgt = a.getAttribute("tgt");
		if(!tgt) return;
		a.tgt = document.getElementById(tgt);
		if(!a.tgt) return;
		
		a.size = this.size;
		a.win = cj.util.createElm("div", {});
		a.win.id = "colorPicker";
		a.show = cj.colorPicker.show;
		a.hide = this.hide;
		a.draw = this.draw;
		a.onclick = a.show;
		document.body.appendChild(a.win);
		
		//mask
		if(document.attachEvent){
			a.win.mask = cj.util.createElm("iframe", {cssText:"position:absolute; visibility:hidden; width:100%; height:100%; top:0; left:0"});
			a.win.appendChild(a.win.mask);
		}
		//drag
		a.win.attrib = "dragable";
		cj.evt.add(a.win, "mousedown", cj.drag.start);
		
		//close
		a.closer = cj.util.createElm("div", {});
		a.closer.id = "closer";
		a.closer.parent = a;
		a.closer.style.top = this.size.gap + "px";
		a.closer.style.right = 0 + "px";
		a.closer.onclick = function(){
			this.parent.hide();
		}
		a.win.appendChild(a.closer);
		
		a.draw();
	}
	,draw : function(){
		var width = 19 * this.size.w + (19 + 2) * this.size.gap;
		var height = 13 * this.size.h + (12 + 2) * this.size.gap + 27;
		
		this.box = cj.util.createElm("div", {cssText:"position:absolute; width:60px; height:20px; border:1px solid #333"});
		this.win.appendChild(this.box);
		this.box.style.width = width - 3*this.size.gap + "px";
		this.box.style.top = height - this.box.offsetHeight - this.size.gap + "px";
		this.box.style.left = this.size.gap + "px";
		
		this.win.style.width = width + "px";
		this.win.style.height = height + "px";
		this.win.style.top =  (cj.util.getElementPos(this)).top + this.offsetHeight + 5 + "px";
		this.win.style.left = (cj.util.getElementPos(this)).left +  "px";
		
		var ar = ['00', '33', '66', '99', 'CC', 'FF']
		for (var x=0; x<19; x++){
			for(var y=0; y<12; y++){
				if(x){
					cr = ar[Math.floor((x-1)/6) + (y%2) * 3];
					cg = ar[Math.floor(y/2)];
					cb = ar[(x-1)%6];
				}else{
					cr = cg = cb = ar[Math.floor(y/2)]
				}
				bg = "#" + cr + cg + cb;
				set = {
					style : {
						 position : "absolute"
						,border : "1px solid black"
						,left : x * this.size.w + (x+1) * this.size.gap + "px"
						,top : 13 + y * this.size.h + (y+1) * this.size.gap + "px"
						,width : this.size.w + "px"
						,height : this.size.h + "px"
						,background : bg
						,cursor : "pointer"
					}
				}
				divs = cj.util.createElm("div", set);
				divs.bg = bg;
				divs.parent = this;
				this.win.appendChild(divs);
				divs.onmouseover = function(){
					this.parent.box.innerHTML = this.bg;
				}
				divs.onclick = function(){
					this.parent.tgt.value = this.bg;
					this.parent.hide();
				}
			}
		}
	}
	,show : function(){
		if(typeof this.win.mask != "undefined"){
			//this.win.mask.style.visibility = 'visible';
			//this.win.mask.style.width = this.win.style.width;
			//this.win.mask.style.top = this.win.style.top;
			//this.win.mask.style.left = this.win.style.left;
		}
		this.win.style.visibility = 'visible';
		return false;
	}
	,hide : function(){
		if(typeof this.win.mask != "undefined")
			this.win.mask.style.visibility = "hidden";
		this.win.style.visibility = "hidden";
	}
}