﻿var 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 = {
	is : function(o, type){
		return (typeof o == type);
	}
	,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.body.clientWidth || document.documentElement.clientWidth;
		height = document.body.clientHeight || document.documentElement.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}
   	},
	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;
			}
		}
	},
	showAttrib : function (obj, type){
   		var html = "<ul>";
		for(var i in obj){
			html += "<li>" + i + "=" + obj[i] + "</li>";
		}
		html += "</ul>";

		switch (type){
			default :
				var c = document.createElement("div");
				c.style.cssText = "position:absolute; border:1px solid #eeeeff; width:640px; height:480px; background:whitesmoke;";
				document.body.appendChild(c);
				c.style.top = this.getScrollPos().top + 50 + "px";

				var bar = document.createElement("div");
				bar.style.cssText = "width:100%; height:20px; border:1px solid #cccccc; background:#666666; color:whitesmoke; cursor:pointer; text-align:center;";
				bar.innerHTML = "Close";
				bar.onclick = function(){
					document.body.removeChild(this.offsetParent);
					return false;
				}
				c.appendChild(bar);

				var main = document.createElement("div");
				main.style.cssText = "width:100%; height:460px; overflow:auto;";
					main.innerHTML = html;
				c.appendChild(main);
				return false;
			break;

			case "text" :
				return html;
			break;
		}
	},
	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;
	}
	,addElm : function(p, set){
		var i, o, t = this;
		if(t.is(set,'string') || !set.length){
			o = p.appendChild(t.createElm_2011(set));
		}else{
			o = p.appendChild(t.createElm_2011(set[0]));
			for(var i=1, l=set.length; i<l; i++){
				t.addElm(o, set[i]);
			}
		}
		return o;
	}
	,createElm_2011 : function(set){
		var o, a , s, d = document, t = this;
		o = t.is(set, 'string')
			? d.createTextNode(set)
			: d.createElement(set.tag);
		if(set.id){
			o.id = set.id;
			cj.elm[set.id] = o;
		}
		if(set.cssText) o.style.cssText = set.cssText;
		if(set['class']){
			o.className = set['class'];
		}
		if(set.innerHTML) o.innerHTML = set.innerHTML;
		if(set.style) t.setStyle(o, set.style);
		if(set.attrib){
			cj.util.each(set.attrib.split(';'), function(s){
				if(s.indexOf(':') != -1){
					n = s.indexOf(':');
					k = s.substr(0, n).replace(/\s+/g);
					v = s.substr(n+1, s.length);
					o.setAttribute(k, v)
				}
			});
		}
		return o;
	}
	,each : function(o, cb, s){
			if(!o) return 0;
			var s = s || o;
			if(typeof(o.length) != 'undefined'){
				for(var i=0, l=o.length; i<l; i++){
					if(cb.call(s, o[i], i, o) === false) return 0;
				}
			}else{
				for (var i in o){
					if(o.hasOwnProperty(i)){
						if(cb.call(s, o[i], i, o) === false){
							return 0;
						}
					}
				}
			}
			return 1;
		}
}
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.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;

			var p = tgt.offsetParent;
			tgt.min_top = (tgt.offsetHeight > p.offsetHeight) ? (p.offsetHeight - tgt.offsetHeight) : 0;
			tgt.min_lft = (tgt.offsetWidth > p.offsetWidth) ? (p.offsetWidth - tgt.offsetWidth) : 0;
			tgt.max_top = (tgt.offsetHeight > p.offsetHeight) ? 0 : p.offsetHeight - tgt.offsetHeight;
			tgt.max_lft = (tgt.offsetWidth > p.offsetWidth) ? 0 : p.offsetWidth - tgt.offsetWidth;

			cj.drag.offsetY = (cj.util.getMousePos(ev)).y - tgt.offsetTop;
			cj.drag.offsetX = (cj.util.getMousePos(ev)).x - tgt.offsetLeft;
			cj.evt.add(cj.drag.dragObj.offsetParent, "mousemove", cj.drag.drag);
			cj.evt.add(cj.drag.dragObj.offsetParent, "mouseup", cj.drag.end);
		}
		return false;
	},
	drag : function(ev){
		if (!cj.drag.dragObj){
			return false;
		}
		var dragObj = cj.drag.dragObj;
		if (0){
			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";
		}else{
			/***********************/
			var mp = cj.util.getMousePos(ev);
			var top = mp.y - cj.drag.offsetY;
			var left = mp.x - cj.drag.offsetX;

			top = Math.max( dragObj.min_top, Math.min(dragObj.max_top, top));
			left = Math.max( dragObj.min_lft, Math.min(dragObj.max_lft, left));

			if(0 && !window.debug){
				window.debug = cj.util.createElm('div', {cssText : "position:absolute; top:0; left:0; width:500px; height:100px; z-index:1000; border:1px solid #f3f; background:#eee"} );
				document.body.appendChild(window.debug);
				var htm = '';
				htm += "min_top: " + dragObj.min_top + "<br>";
				htm += "max_top: " + dragObj.max_top + "<br>";
				htm += "min_lft: " + dragObj.min_lft + "<br>";
				htm += "max_lft: " + dragObj.max_lft + "<br>";
				htm += "top: " + top + "<br>";
				htm += "lft: " + left + "<br>";
				window.debug.innerHTML = htm;
			}

			dragObj.style.top = top + 'px';
			dragObj.style.left = left + '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 +=  obj.offsetWidth;
						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 +=  obj.offsetWidth;
						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 +=  obj.offsetWidth;
						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 +=  obj.offsetWidth;
						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 +=  obj.offsetWidth;
						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 +=  obj.offsetWidth;
								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 +=  obj.offsetWidth;
						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 +=  obj.offsetWidth;
						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 +=  obj.offsetWidth;
						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 <6){
			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 : " 必填 ",
			chs : " 必填 ",
			en : " is required! ",
			jp : "をご入力ください",
			kor : " 필수입력"
		},
		valid : {
			chi	: " 請輸入有效的 ",
			chs	: " 請輸入有效的 ",
			en		: " Please specify valid ",
			jp : " Please specify valid ",
			kor : " Please specify valid "
		},
		day : {
			chi	: "日期",
			chs	: "日期",
			en		: "Date",
			jp		: "Date",
			kor		: "Date"
		},
		not_number : {
			chi	: " 不是數字",
			chs	: " 不是數字",
			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 = -3 + "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.popup = {
	set : {}
	,init : function(set){
		//mask
		this.mask = cj.util.createElm('div', {
			cssText : "position:absolute; top:0; left: 0; visibility:hidden; background:#333; opacity:0.9; -moz-opacity:0.9; -khtml-opacity:0.9"
			,style : {filter : 'alpha(opacity:80)'}
		});
		document.body.appendChild(this.mask);
		//mask ie
		if(document.attachEvent){
			var if1 = cj.util.createElm('iframe', { cssText : "width:100%; height:100%;"});
			if1.setAttribute('frameborder','0',0);
			this.mask.appendChild(if1);
		}
		var div = cj.util.createElm('div', { cssText : "position:absolute; top:0; left:0; width:100%; height:100%; background:#333;" });
		this.mask.appendChild(div);
		//win
		this.win = cj.util.createElm('div', {
			cssText : "position:absolute; visibility:hidden; overflow:hidden; border:2px solid #ccf; z-index:3000"
			,innerHTML : "<div style='position:relative; width:100%; height:17px; background:white' align='right'><img src='../img/x.gif' border='0' style='margin:1px; cursor:pointer'></div>"
		});
		this.win.oncontextmenu=new Function("return false");
		this.win.onselectstart=new Function ("return false");
		/***********************/
		//this.win.style.overflow = 'visible';
		/***********************/
		document.body.appendChild(this.win);

		//controller
		ctrl = cj.util.createElm('div', {
			cssText : "visibility: hidden; border:1px solid #333; position:absolute; top:18px; right:1px; width:48px; height:48px; z-index:2000; background:white; opacity:0.9; -moz-opacity:0.9; -khtml-opacity:0.9"
			,style : { filter : 'alpha(opacity:90)' }
		});
		ctrl.show = false;
		this.win.appendChild(ctrl);
		this.win.ctrl = ctrl;
		ctrl.up = cj.util.createElm('img', { cssText : "position:absolute; top:0px; left:15px; cursor:pointer; width:15px; height:15px"});
		ctrl.up.src = "../func/css/images/ar_up.png";
		ctrl.appendChild(ctrl.up);

		ctrl.lft = cj.util.createElm('img', { cssText : "position:absolute; top:15px; left:0px; cursor:pointer; width:15px; height:15px"});
		ctrl.lft.src = "../func/css/images/ar_lft.png";
		ctrl.appendChild(ctrl.lft);

		ctrl.rgt = cj.util.createElm('img', { cssText : "position:absolute; top:15px; left:30px; cursor:pointer; width:15px; height:15px"});
		ctrl.rgt.src = "../func/css/images/ar_rgt.png";
		ctrl.appendChild(ctrl.rgt);

		ctrl.dwn = cj.util.createElm('img', { cssText : "position:absolute; top:30px; left:15px; cursor:pointer; width:15px; height:15px"});
		ctrl.dwn.src = "../func/css/images/ar_dwn.png";
		ctrl.appendChild(ctrl.dwn);

		cj.evt.add(ctrl.up, 'click', function(){ cj.popup.move('up') });
		cj.evt.add(ctrl.dwn, 'click', function(){ cj.popup.move('dwn') });
		cj.evt.add(ctrl.lft, 'click', function(){ cj.popup.move('lft') });
		cj.evt.add(ctrl.rgt, 'click', function(){ cj.popup.move('rgt') });

		//win.body
		cj.evt.add(this.win.getElementsByTagName('img')[0], 'click', function(){ cj.popup.close() });
		this.win.body = cj.util.createElm('div', {
			cssText : "position:relative; width:100%; height:100%; text-align:center; vertical-align:middle; background:#ccc; overflow:hidden;"
		});
		this.win.appendChild(this.win.body);

		cj.evt.add(window, 'scroll', function(){ cj.popup.setPos(); });
		cj.evt.add(window, 'resize', function(){ cj.popup.setSize(); cj.popup.setPos(); });
		var tgt = (window.event) ? document.body : window;
		cj.evt.add(tgt, 'keypress', function(evt){ cj.popup.keypress(evt) } );
	}
	,setSize : function(){
		if(!this.mask || !this.win) return false;
		var rate = 0.9;
		var W = document.body.scrollWidth || document.documentElement.scrollWidth;
		var H = document.body.scrollHeight || document.documentElement.scrollHeight;
		this.mask.style.width = W + 'px';
		this.mask.style.height = H + 'px';

		var winSize = cj.util.getWindowSize();
		var maxW = parseInt(winSize.width * rate)
		var maxH = parseInt(winSize.height * rate)

		/************** modify by customer require *************/
		var rateW = maxW / this.win.body.img.width;
		var rateH = maxH / this.win.body.img.height;
		if(rateW<1 || rateH<1){
			if(rateW<rateH){
				this.set.winW = maxW;
				this.set.winH = parseInt(this.win.body.img.height * rateW);
			}else{
				this.set.winW = parseInt(this.win.body.img.width * rateH);
				this.set.winH = maxH;
			}
		}
		//this.set.winW = Math.min(maxW, this.set.winW || maxW);
		//this.set.winH = Math.min(maxH, this.set.winH || maxH);
		/*********************************************************/

		this.win.style.width = this.set.winW  + 'px';
		this.win.style.height = this.set.winH + 'px';

		if(this.win.body.img){
			/******** modify by customer requirement *********/
			this.win.body.img.style.width = this.set.winW + "px";
			this.win.body.img.style.height = this.set.winH + "px";
			/******** modify by customer requirement *********/
			if(this.win.body.img.offsetWidth > this.set.winW || this.win.body.img.offsetHeight > this.set.winH){
				this.win.body.img.attrib = 'dragable';
				this.win.body.img.style.cursor = "move";
				this.win.ctrl.show = true;
			}else{
				this.win.ctrl.show = false;
			}
		}
	}
	,setPos : function(){
		if(!this.mask || !this.win) return false;
		var scrollPos = cj.util.getScrollPos();
		var winSize = cj.util.getWindowSize();
		this.win.style.left = scrollPos.left + parseInt( (winSize.width-this.set.winW)/2 );
		this.win.style.top = scrollPos.top + parseInt( (winSize.height-this.set.winH)/2 );
	}
	,move : function(act){
		var c = this.win.body;
		var s = c.img
		var pos = cj.util.getElementPos(s);
		s.min_top = (s.offsetHeight > c.offsetHeight) ? (c.offsetHeight - s.offsetHeight) : 0;
		s.min_lft = (s.offsetWidth > c.offsetWidth) ? (c.offsetWidth - s.offsetWidth) : 0;
		s.max_top = (s.offsetHeight > c.offsetHeight) ? 0 : c.offsetHeight - s.offsetHeight;
		s.max_lft = (s.offsetWidth > c.offsetWidth) ? 0 : c.offsetWidth - s.offsetWidth;
		var dis = 100;
		switch(act){
			case "up":
				pos.top = Math.max(pos.top - dis, s.min_top)
			break;
			case "dwn":
				pos.top = Math.min(pos.top + dis, s.max_top)
			break;
			case "lft":
				pos.left = Math.max(pos.left - dis, s.min_lft);
			break;
			case "rgt":
				pos.left = Math.min(pos.left + dis, s.max_lft);
			break;
		}
		s.style.top = pos.top + "px";
		s.style.left = pos.left + "px";
	}
	,close : function(){
		if(!this.mask || !this.win) return false;
		this.opened = false;
		this.mask.style.visibility = 'hidden';
		this.win.style.visibility = 'hidden';
		this.win.ctrl.style.visibility = 'hidden';
	}
	,popImg : function(img){
		if(!this.mask || !this.win) this.init();
		this.opened = true;
		while( this.win.body.childNodes[0]){
			this.win.body.removeChild(this.win.body.childNodes[0]);
		}
		this.win.body.img = img;
		this.win.body.appendChild(img);

		img.style.cssText = 'position:absolute; top:0; left:0; z-index:1000';
		this.set.winW = img.offsetWidth;
		this.set.winH = img.offsetHeight;
		cj.evt.add(img, 'mousedown', cj.drag.start);
		this.mask.style.visibility = 'visible';
		this.win.style.visibility = 'visible';
		this.setSize();
		this.setPos();
		if(this.win.ctrl.show){
			this.win.ctrl.style.visibility = 'visible';
		}
	}
	,keypress : function(evt){
		var evt = evt || window.event;
		if( (evt.keyCode==27) && (this.opened==true)){
			this.close();
		}
	}
}
