var isIE=!-[1,];
function $(o){if((typeof o=='object'))return o;if(document.getElementById&&document.getElementById(o)){return document.getElementById(o);}else if (document.all&&document.all(o)){return document.all(o);}else if (document.layers&&document.layers[o]){return document.layers[o];}else{return false;}} 
function setCls(o,c,f){if(!o)return;var oc=o.className=o.className.replace(new RegExp('( ?|^)'+c+'\\b'),'');if(!f)o.className=oc.length>0?(oc+' '+c):c}
String.prototype.trim= function(){return this.replace(/(^\s*)|(\s*$)/g,'')}
if(!Array.prototype.indexOf)Array.prototype.indexOf=function(item,i){i||(i=0);var l=this.length;if(i<0)i=l+i;for(;i<l;i++)if(this[i]===item)return i;return -1}
function onEvent(A,B,C){B=B.replace(/^on/,'').toLowerCase();if(A.attachEvent){A[B+C]=function(){C.call(A,window.event)};A.attachEvent('on'+B,A[B+C])}else A.addEventListener(B,C,false);return A}
function unEvent(A,B,C){B=B.replace(/^on/,'').toLowerCase();if(A.attachEvent){A.detachEvent('on'+B,A[B+C]);A[B+C]=null}else A.removeEventListener(B,C,false);return A}
function hideDocClickEventEl(e){if(!this.clickEl)return;var T=(window.event&&window.event.srcElement)||e.target;var D=this.clickEl.eventsElDeep||10;while(D>0){if(T==this.clickEl||(this.clickEl.EventsEls&&this.clickEl.EventsEls.indexOf(T)>=0)){return}else if(T.parentNode){T=T.parentNode}else break;D--}this.clickEl.hide()}
function insertAfter(e,t){var p=t.parentNode;p.lastChild==t?p.appendChild(e):p.insertBefore(e,t.nextSibling)}
function bytes(s){if(typeof(s)!="string"){s=s.value}var l=0;for(var i=0;i<s.length;i++){if(s.charCodeAt(i)>127){l++}l++}return l}
function show(){this.style.display='block'}
function hide(){this.style.display='none'}
function setVisibile(A,B){if(typeof A=='string')A=$(A);if(A)A.style.display=B?'block':'none'}
function fadeout(o){if(!o.opacity){o.opacity=100}o.opacity-=5;alpha(o);if(o.opacity>0){o.fadeoutTimer=window.setTimeout(function(){fadeout(o)},50)}else{o.opacity=100;alpha(o);o.style.display='none'}}
function alpha(o){if(document.all){o.style.filter="alpha(opacity="+o.opacity+")"}else{o.style.opacity=(o.opacity/100)}}
function isEmail(v){return v.match(/^[\w\.\-]+@([\w\-]+\.)+[a-z]{2,4}$/ig)}
try{document.execCommand('BackgroundImageCache',false,true);}catch(e){}
function getIEVer(){var a = 0;var b = navigator.userAgent;if(b.indexOf("MSIE")>-1){var c=b.split(";")[1];c=c.replace("MSIE","");a=parseFloat(c)}return a}
String.prototype.ellipse=function(l){if(this.length>l){return this.substr(0,l-3)+ '...';}return this};
function getRootDomain(){var boolcookie=function(D){var A ="rootdomin";document.cookie=A+"=1;domain="+D+";";var B=document.cookie.match(new RegExp("(^| )"+A+"=([^;]*)(;|$)"));if(B==null)return false;var C=new Date();C.setTime(C.getTime()-10);if(B!=null)document.cookie=A+"="+B[2]+";expires="+C.toGMTString()+";domain="+D+";";return true};var D = document.domain,i=5;var E=D;while(boolcookie(D)&&i>0){E=D;D=D.replace(/^[^\.]+\./,"");i--}return E}

/* 用户 */
Account = {
	windows : new Array,
	showedWindows : [],
	currentWin : null,
	callback : null,
	login : function(config){
		if(!this.loginWin){
			this.loginWin = this.window({id:'login', title:'登录'});
			var tthis = this;
			ajax.update(this.loginWin.body, 'get', 'http://'+window.location.hostname+'/account/common.inc.php', {ajaxlogin:100}, function(){var f=$('ajaxloginform');FormPanel.renderInput(f);f.username.focus();tthis.loginWin.resize()});
		}else{
			this.hiddenAllError($('ajaxloginform'));
		}
		this.callback = config && config.callback ? config.callback : null;
		this.loginWin.show();
	},
	ajaxLogin : function(f){
		var msg = '';
		var flag = true;
		var isfocus = false;
		if(f.username.value.trim() == ''){
			msg = '请输入您的用户名';
			flag = false;
			isfocus = true;
			f.username.focus();
		}
		this.formError(f.username,msg);
		msg = '';
		if(f.password.value.trim() == ''){
			msg = '请输入您的密码';
			flag = false;
			if(!isfocus){
				f.password.focus();
				isfocus = true;
			}
		}
		this.formError(f.password,msg);
		if(f.verifycode){
			msg = '';
			if(f.verifycode.value.trim() == ''){
				msg = '请输入您的右边图片中的验证字符';
				flag = false;
				if(!isfocus) f.verifycode.focus();
			}
			this.formError(f.verifycode,msg);
		}
		if(flag) this.doLogin(f);
		return false;
	},
	doLogin:function(f){
		var tthis = this;
		if(!f.showError){
			f.showError = function(m){
				if(!this.errorEl){
					this.errorEl = document.createElement('div');
					this.errorEl.className = 'error';
					this.errorEl.body = document.createElement('span');
					this.errorEl.appendChild(this.errorEl.body);
					this.insertBefore(this.errorEl, this.firstChild);
				} 
				this.errorEl.body.innerHTML = m;
				this.errorEl.style.display = 'block';
			}
			f.hideError = function(){if(this.errorEl)this.errorEl.style.display='none';}
		}
		f.hideError();
		ajax.request('post',f.attributes['action'].value,{
			success:function(r){
				var v=ajax.jsondecode(r.responseText);
				if(v.success){
					tthis.isLogin = true;
					if(Login) Login.login = true;
					tthis.loginWin.hide();
					if(v.logininfo){
						var js = document.createElement('div');
						document.body.appendChild(js);
						ajax.setInnerHTML(js, v.logininfo);
					}
					if(tthis.callback && typeof(tthis.callback) == 'function'){
						tthis.callback(); 
					}
				}else{
					f.showError(v.message);
				}
			}
		},ajax.serializeForm(f));
	},
	formError:function(o,v){
		if(!o)return;
		if(v && o && !o.errorEl){
			o.errorEl = document.createElement('span');
			o.errorEl.className = 'errormsg';
			insertAfter(o.errorEl,o.parentNode);
			o.errorEl.show = this.show;
			o.errorEl.hide = this.hide;
		}
		if(!o.errorEl)return false;
		if(v){
			o.errorEl.innerHTML = v;
			o.errorEl.show();
		}else{
			o.errorEl.hide();
		}
		return false;
	},
	hiddenAllError:function(f){
		for(var i=0; i < f.elements.length; i++){
			if(f.elements[i].errorEl) f.elements[i].errorEl.hide();
		}
	},
	window : function(config){
		config.id += '-win';
		var win = this.windows[config.id];
		if(win){win.show();return}
		win = document.createElement('div');
		this.windows[config.id] = win;
		win.className = 'win-dialog';
		if(config.width > 420){win.style.width = config.width+'px';}
		var hd = document.createElement('div');
		hd.className = 'hd';
		var wrap = document.createElement('div');
		wrap.className = 'wrap';
		hd.appendChild(wrap);
		win.titleCt = document.createElement('h3');
		win.titleCt.innerHTML = config.title;
		var closebtn = document.createElement('span');
		closebtn.className = 'closebtn';
		closebtn.title = '关闭';
		closebtn.onmouseover = function(){setCls(this,'closebtn-hover')}
		closebtn.onmouseout = function(){setCls(this,'closebtn-hover',true);setCls(this,'closebtn-click',true)}
		closebtn.onmousedown = function(){setCls(this,'closebtn-click')}
		closebtn.onmouseup = function(){setCls(this,'closebtn-click',true)}
		closebtn.onclick = function(){win.hide()}
		wrap.appendChild(win.titleCt);
		wrap.appendChild(closebtn);
		win.appendChild(hd);
		win.body = document.createElement('div');
		if(config.height > 0) win.body.style.height = config.height + 'px';
		win.body.className = 'bd';
		if(config.html){win.body.innerHTML = config.html}
		win.appendChild(win.body);
		// container
		win.ct = document.createElement('div');
		win.ct.className = 'win-ct';
		// mask
		win.mask = document.createElement('div');
		win.mask.className = 'win-mask';
		win.ct.appendChild(win.mask);
		// shadow
		win.shadow = document.createElement('div');
		win.shadow.className = 'win-shadow';
		win.ct.appendChild(win.shadow);
		win.ct.appendChild(win);
		document.body.appendChild(win.ct);
		win.ct.show = this.show;
		win.ct.hide = this.hide;
		win.resize = this.resize;
		var tthis = this;
		win.show = function(){tthis.showedWindows.push(this);onEvent(window,'resize',this.resize);this.ct.show();this.resize();}
		win.hide = function(){tthis.showedWindows.pop(this);unEvent(window,'resize',this.resize);this.ct.hide()}
		win.setTitle = function(t){this.titleCt.innerHTML=t}
		win.setWidth = function(w){this.style.width=w+'px'}
		win.setHeight = function(h){this.style.height=h+'px'}
		hd.onmousedown = function(){}
		hd.onmouseup = function(){}
		// set drag event
		if(!this._mM){
			this._mM = function(e){tthis.onMove(window.event || e)}
			this._mE = function(e){tthis.endMove(window.event || e)}
		}
		onEvent(hd, 'mousedown', function(e){tthis.beforMove(window.event||e, hd)});
		win.show();
		FormPanel.renderInput(win.body);
		return win;
	},
	resize : function(){
		var win = Account.showedWindows[Account.showedWindows.length - 1];
		win.mask.style.width = '0px';
		win.mask.style.height = '0px';
		win.mask.style.left = -Math.floor((document.documentElement.scrollWidth - document.body.offsetWidth) / 2) + 'px';
		win.mask.style.width = document.documentElement.scrollWidth - 1 + 'px';
		win.mask.style.height = Math.max(document.documentElement.clientHeight, document.documentElement.scrollHeight, document.body.scrollHeight) + 'px';
		win.style.left = Math.floor((document.documentElement.scrollWidth - win.offsetWidth) / 2) + 'px';
		var t = Math.floor((document.documentElement.clientHeight - win.offsetHeight) / 2);
		if(t < 0){t=0}
		if(getIEVer() == 6){
			win.style.left = Math.floor((document.body.scrollWidth - win.offsetWidth) / 2) + 'px';
			win.style.top = document.documentElement.scrollTop + t + 'px' ;
		}else{
			win.style.left = Math.floor((document.documentElement.scrollWidth - win.offsetWidth) / 2) + 'px';
			win.style.top = t + 'px';
		}
	},
	beforMove:function(e, o){
		this.isMoving = true;
		this.moveEl = o;
		this.moveEvtStart = [e.clientX, e.clientY, this.moveEl.parentNode.offsetLeft, this.moveEl.parentNode.offsetTop];
		onEvent(document, 'mousemove', this._mM);
		onEvent(document, 'mouseup', this._mE);	
		if(isIE){
			onEvent(this.moveEl, 'losecapture', this._mE);
			this.moveEl.setCapture();
		}else{
			onEvent(window, 'blur', this._mE);
		}
	},
	endMove:function(e){
		if(!this.isMoving)return ;
		this.isMoving = false;
		unEvent(document, 'mousemove', this._mM);
		unEvent(document, 'mouseup', this._mE);
		if(isIE){
			unEvent(this.moveEl, 'losecapture', this._mE);
			this.moveEl.releaseCapture();
		}else{
			unEvent(window, 'blur', this._mE);
		}
		this.onMove(e, true);
	},
	onMove:function(e, flag){
		window.getSelection && window.getSelection().removeAllRanges();
		if(!this.isMoving && !flag)return ;
		var cx = e.clientX - this.moveEvtStart[0];
		var cy = e.clientY - this.moveEvtStart[1];
		this.moveTo(cx, cy, flag);
	},
	moveTo:function(x,y, flag){
		x += this.moveEvtStart[2];
		y += this.moveEvtStart[3];
		this.moveEl.parentNode.style.left= x + 'px';
		this.moveEl.parentNode.style.top= y + 'px';
	},
	show:function(){this.style.display='block'},
	hide:function(){this.style.display='none'},
    refreshVcode:function(A){if(!A.I){var B=A.parentNode.getElementsByTagName('img');if(!B)return;for(var C=0,L=B.length;C<L;C++)if(B[C].className='vimage'){A.I=B[C];break;}}if(A.I){if(!A.I.OS){A.I.OS=A.I.src+(A.I.src.indexOf('?')>=0?'&':'?')}A.I.src=A.I.OS+(new Date().getTime())}}
};

/* 表单处理 */
FormPanel = {
	init:function(){
		this.renderInput();	
	},
	renderInput:function(f){
		var ipt = f ? f.getElementsByTagName('input') : document.getElementsByTagName('input');
		for(var i=0;i<ipt.length;i++){
			var cls = ipt[i].className.split(' ');
			if(cls.indexOf('txt') >= 0){
				ipt[i].focus_cls = 'focus';
				if(ipt[i].parentNode.className.split(' ').indexOf('rinput') >= 0){
					ipt[i].focus_cls = 'rfocus';
				}
				onEvent(ipt[i], 'focus', function(){if(this.readOnly)return;setCls(this.parentNode, this.focus_cls)});
				onEvent(ipt[i], 'blur', function(){setCls(this.parentNode, this.focus_cls, true)});
				if(cls.indexOf('resourceinput') >= 0){
					var oW = ipt[i].offsetWidth;
					if(oW > 0){
						ipt[i].style.width =  oW - 57 + 'px';
						ipt[i].style.paddingRight = '53px';
					}
				}else if(cls.indexOf('resourcepreviewinput') >= 0){
					var oW = ipt[i].offsetWidth;
					if(oW > 0){
						ipt[i].style.width = oW - 79 + 'px';
						ipt[i].style.paddingRight = '75px';
					}
				}
			}else if(ipt[i].className == 'button'){
				ipt[i].onmouseover = function(){setCls(this,'b-over')}
				ipt[i].onmouseout = function(){setCls(this,'b-over',true);setCls(this,'b-click',true)}
				ipt[i].onmousedown = function(){setCls(this,'b-click')}
				ipt[i].onmouseup = function(){setCls(this,'b-click',true)}
			}
		}
		var ipt = document.getElementsByTagName('textarea');
		for(var i=0;i<ipt.length;i++){
			if(ipt[i].className == 'txt'){
				ipt[i].onfocus = function(){setCls(this,'focus')}
				ipt[i].onblur = function(){setCls(this,'focus',true)}
			}
		}
	},
	trigger:function(el, type, obj){
		if(!el)return;
		if(!type)return;
		if(typeof(el) == 'string'){el=$(el)}
		if(typeof(el) != 'object')return;
		if(!el.render){
			el.render = true;
			el.overCls = type + 'trigger-over';
			el.clickCls = type + 'trigger-click';
			//setCls(el,el.overCls);
			el.onmouseover = function(){setCls(this,this.overCls)}
			el.onmouseout = function(){setCls(this,this.overCls,true);}
			el.onmousedown = function(){setCls(this,this.clickCls)}
			el.onmouseup = function(){setCls(this,this.clickCls,true)}
			arguments[0] = el;
			switch(type){
				case 'combo' : 	this.renderCombo(arguments);break;
				case 'combotree' : this.renderComboTree(arguments);break;
			}
		}
	},
	
	// render combox element
	renderCombo:function(){
		var tthis = this;
		var arg = arguments[0];
		var el = arg[0];
		var list = $(arg[3]);
		var hidetarget = $(arg[4]);
		var target = $(arg[2]);
		var callback = arg[5];
		if(!list || !target)return ;
		list.target = target;
		if(hidetarget) list.hidetarget = hidetarget;
		var w = target.clientWidth;
		list.style.width = w + 'px';
		list.el = el;
		var tthis = this;
		
		list.EventsEls = [list,el,target];
		list.show = function(){if(document.clickEl){document.clickEl.hide();}document.clickEl=this;onEvent(document,'click',hideDocClickEventEl);setCls(this.el,this.el.clickCls);this.style.display='block'}
		list.hide = function(){document.clickEl=null;unEvent(document,'click',hideDocClickEventEl);setCls(this.el,this.el.clickCls,true);this.style.display='none'}

		var lis = list.getElementsByTagName('li');
		for(var i=0;i<lis.length;i++){
			lis[i].onmouseover = function(){setCls(this,'over')}
			lis[i].onmouseout = function(){setCls(this,'over',true)}
			lis[i].onmousedown = function(){ var v=this.parentNode.target.value=this.innerHTML;if(this.parentNode.hidetarget){v=this.parentNode.hidetarget.value = this.attributes['value'].value}this.parentNode.hide();try{if(typeof(callback)=='function'){callback(this,v)}}catch(e){}}
		}
		var h = lis.length > 6 ? 132 : lis.length * 22;
		if(h == 0) h = 22;
		list.style.height = h + 'px';
		target.list = list;
		onEvent(target, 'focus', function(){if(this.readOnly)this.list.show()});
		onEvent(target, 'blur', function(){this.list.hide()});
		el.list = list;
		onEvent(el, 'click', function(){this.list.show()});
		
	},
	
	// render comboxTree element
	renderComboTree:function(){
		var tthis = this;
		var arg = arguments[0];
		var el = arg[0];
		var list = $(arg[3]);
		var hidetarget = $(arg[4]);
		var target = $(arg[2]);
		if(!list || !target)return ;
		list.target = target;
		list.modelid = arg[5] ? arg[5] : 1;
		if(hidetarget) list.hidetarget = hidetarget;
		var w = target.clientWidth;
		list.style.width = w + 'px';
		list.el = el;
		var tthis = this;
		list.show = function(){if(document.showTreeList){document.showTreeList.hide();}document.showTreeList=this;onEvent(document,'click',this.hideTreeList);setCls(this.el,this.el.clickCls);this.style.display='block'}
		list.hide = function(){document.showTreeList=null;unEvent(document,'click',this.hideTreeList);setCls(this.el,this.el.clickCls,true);this.style.display='none'}
		list.hideTreeList = tthis.hideTreeList;
		list.style.height = '200px';
		target.list = list;
		onEvent(target, 'onclick', function(){this.list.show()});
		el.list = list;
		onEvent(el, 'click', function(){this.list.show()});
	},
	
	hideTreeList : function(e){
		if(!this.showTreeList) return ;
		var target = window.event ? window.event.srcElement : e.target;
		if(target.isTreeNode){target.clickFn(target,this.showTreeList);return;}
		if(target.parentNode){
			if(target.parentNode.isTreeNode){
				target.parentNode.clickFn(target.parentNode,this.showTreeList);return;
			}else if(target.parentNode.parentNode&&target.parentNode.parentNode.isTreeNode){
				if(target.nextSibling&&target.nextSibling.nodeName=="IMG"){}else{target.parentNode.parentNode.clickFn(target.parentNode.parentNode,this.showTreeList)}return;
			}
		}
		var deep = 5;
		while(deep > 0){
			if(target.isTreeNode||[this.showTreeList, this.showTreeList.el, this.showTreeList.target].indexOf(target) >= 0){return}
			else if(target.parentNode){target=target.parentNode;}
			else break;
			deep--;
		}
		this.showTreeList.hide();
	},
	
	//hide combo list
	hideComboList:function(e){
		if(!this.showComboList) return ;
		var target = window.event ? window.event.srcElement : e.target;
		var deep = 5;
		while(deep > 0){
			if([this.showComboList, this.showComboList.el, this.showComboList.target].indexOf(target) >= 0){return}
			else if(target.parentNode){target=target.parentNode;}
			else break;
			deep--;
		}
		this.showComboList.hide();
	},
	// password strength
	getPasswordStrength:function(o){
		if(!o.list){
			o.list = $('password-strength').getElementsByTagName('em');
		}
		var v = o.value;
		var strength = 0;
		strength += (v.search(/[0-9]/)!=-1) ? 1 : 0;
		strength += (v.search(/[a-zA-Z]/)!=-1) ? 1 : 0;
		if(strength == 2 && bytes(v) < 7) strength = 1;
		if((v.search(/[^A-Za-z0-9_]/)!=-1) && bytes(v) > 10) strength = 3;
		if(o.strength){
			setCls(o.strength, 's0', true);
			setCls(o.strength, 's1', true);
			setCls(o.strength, 's2', true);
		}
		if(v){
			strength = strength > 0 ? strength - 1 : 0 
			o.strength = o.list[strength]
			setCls(o.strength, 's' + strength);
		}
	},
	// message
	message:function(o, msg, cls, autohide){
		if(!o.msgEl){
			o.msgEl = document.createElement('div');
			o.insertBefore(o.msgEl, o.firstChild);
		}
		o.msgEl.className = cls;
		o.msgEl.innerHTML = msg;
		o.msgEl.style.display = 'block';
		if(o.msgEl.fadeoutTimer){
			window.clearTimeout(o.msgEl.fadeoutTimer);
			o.msgEl.opacity = 100;
			alpha(o.msgEl);
		}
		if(autohide){window.setTimeout(function(){fadeout(o.msgEl)},3000)}
	},
	// input error
	inputError:function(o,v){
		if(!o)return;
		if(v && o && !o.errorEl){
			o.errorEl = document.createElement('div');
			o.errorEl.className = 'errormsg';
			insertAfter(o.errorEl,o.parentNode);
			o.errorEl.show = show;
			o.errorEl.hide = hide;
		}
		if(!o.errorEl)return false;
		if(v){
			o.errorEl.innerHTML = v;
			o.errorEl.show();
		}else{
			o.errorEl.hide();
		}
		return false;
	},
	// input side error
	inputSideError:function(o,v,type){
		if(!o)return;
		if(v && o && !o.errorSpan){
			o.errorSpan = document.createElement('span');
			o.errorSpan.className = 'sideerror';
			insertAfter(o.errorSpan, o);
			o.errorSpan.show = function(){this.style.display='inline-block'}
			o.errorSpan.hide = hide;
		}
		if(!o.errorSpan)return false;
		if(typeof v == 'string'){
			type = type ? type : 'error';
			o.errorSpan.className = 'sideerror ' + type
			o.errorSpan.innerHTML = v;
			o.errorSpan.show();
		}else{
			o.errorSpan.hide();
		}
	},
	hideMessage:function(o){if(o.msgEl)o.msgEl.style.display='none'}
};

window.onload = function(){FormPanel.init();}

// Grid button
function buttonRender(o){
	if(typeof(o) != 'object')return;
	o.overCls= 'over';
	o.clickCls= 'click';
	setCls(o,o.overCls);
	o.onmouseover = function(){setCls(this,this.overCls)}
	o.onmouseout = function(){setCls(this,this.overCls,true);setCls(this,this.clickCls,true)}
	o.onmousedown = function(){setCls(this,this.clickCls)}
	o.onmouseup = function(){setCls(this,this.clickCls,true)}
}
