﻿String.prototype.trim = function(){
	if(!String._specialCharPattern)
		String._specialCharPattern = /^\s+|\s+$/gi;
	String._specialCharPattern.lastIndex = 0;
	return this.replace(String._specialCharPattern,"");
}
var StyleMgr = {
	hasCSSClass:function(el,cssStyle){
		var pattern = new RegExp("\\b"+cssStyle+"\\b","g");
		if(pattern.test(el.className))
			return true;
		return false;
	},
	addCSSClass:function(el,cssStyle){
		if(!this.hasCSSClass(el,cssStyle))
			el.className += " "+cssStyle;
	},
	removeCSSClass:function(el,cssStyle){
		var pattern = new RegExp("^(.*)\\b("+cssStyle+")\\b(.*)$","g");
		if(pattern.test(el.className))
			el.className = RegExp.$1.trim()+" "+RegExp.$3.trim();
	},
	replaceCSSClass:function(el,cssStyle,newCssStyle){
		var pattern = new RegExp("^(.*)\\b("+cssStyle+")\\b(.*)$","g");
		if(pattern.test(el.className))
			el.className =  RegExp.$1+newCssStyle+RegExp.$3;
	},
	getCSSStyle:function(el,property){
		if(document.documentElement.currentStyle && el.currentStyle){
			if(property == "opacity"){
				var opacity = 100;
				try{
					opacity = el.filters["DXImageTransform.Microsoft.Alpha"].opacity;
				}catch(e){
					try{
						opacity = el.filters["alpha"].opacity;
					}catch(e){}
				}
				return opacity/100;
			}else if(property == "float"){
				return el.currentStyle["styleFloat"];
			}else{
				return el.currentStyle[property];
			}
		}else if(document.defaultView && document.defaultView.getComputedStyle){
			if(property == "float")
				property = "cssFloat";
			return document.defaultView.getComputedStyle(el,"")[property];
		}else{
			return el.style[property];
		}
	},
	setCSSStyle:function(el,property,value){
		if(el.currentStyle){
			if(property == "opacity"){
				el.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+value*100+")";
				try{
					if(!el.currentStyle || !el.currentStyle.hasLayout())
						el.style.zoom = 1;
				}catch(e){}
				return;
			}
			if(property == "float")
				property = "styleFloat";
			el.style[property] = value;
		}else{
			if(property == "float")
				property = "cssFloat";
			el.style[property] = value;
		}
	}
}

var JEffect = function(target,property,from,to){
	this.target = target;
	this.property = property;
	this.from = from;
	this.to = to;
	this.unit = "";
	this.saveLength = 0;
	this.isEffecting = false;
	this.timer = {
		timer:null,
		start:0
	};
	this.constraints = {
		lpf:0,
		length:20,
		ipf:20
	};
	this.onComplete = null;
}
JEffect.CONSTRIANT_FROM = 0;
JEffect.CONSTRIANT_TO = 1;
JEffect.CONSTRIANT_PROPERTY = 2;
JEffect.CONSTRIANT_FRAMELENGTH = 3;
JEffect.CONSTRIANT_TARGET = 4;
JEffect.CONSTRIANT_UNIT = 5;
JEffect.prototype.setConstriant = function(c,v){
	if(c == JEffect.CONSTRIANT_FROM){
		this.from = v;
	}else if(c == JEffect.CONSTRIANT_TO){
		this.to = v;
	}else if(c == JEffect.CONSTRIANT_PROPERTY){
		this.property = v;
	}else if(c == JEffect.CONSTRIANT_FRAMELENGTH){
		this.constraints.length = v;
	}else if(c == JEffect.CONSTRIANT_TARGET){
		this.target = v;
	}else if(c == JEffect.CONSTRIANT_UNIT){
		this.unit = v;
	}else{
		throw new Error("Illegal Argument");	
	}
}
JEffect.prototype.analyze = function(){
	this.constraints.lpf = Math.abs(this.from-this.to)/this.constraints.length;
}
JEffect.prototype.start = function(){
	if(this.isEffecting)
		return;
	this.isEffecting = true;
	var _this = this;
	this.saveLength = this.from;
	this.timer.timer = setInterval(function(){
		_this._count();
	},this.constraints.ipf);
}
JEffect.prototype.pause = function(){
	if(this.isEffecting){
		clearInterval(this.timer.timer);
		this.timer.timer = null;
		this.timer.start = 0;
		this.isEffecting = false;			
	}
}
JEffect.prototype._count = function(){
	var f = this.from;
	var t = this.to;
	var p = this.constraints.lpf;
	if(t>f){
		if(this.saveLength+p>=t){
			this.target[this.property] = this.to+this.unit;
			clearInterval(this.timer.timer);
			this.timer.timer = null;
			this.timer.start = 0;
			this.isEffecting = false;
			if(this.onComplete != null){
				this.onComplete.call(this);;
			}			
		}else{
			this.target[this.property] = this.saveLength + p + this.unit;
			this.saveLength = this.saveLength + p;
		}
	}else{
		if(this.saveLength-p<=t){
			this.target[this.property] = this.to+this.unit;
			clearInterval(this.timer.timer);
			this.timer.timer = null;
			this.timer.start = 0;
			this.isEffecting = false;
			if(this.onComplete != null){
				this.onComplete.call(this);;
			}			
		}else{
			this.target[this.property] = this.saveLength - p + this.unit;
			this.saveLength = this.saveLength - p;
		}
	}
}

var SlideEffect = function(UI,option){
	this.UI = UI;
	this.option = option;
	this.UIContainerWidth = 0;
	this.UITabsWidth = 0;
	this.onSlide = null;
	this.onBeforeSlide = null;
	this.onAfterSlide = null;
	this.effect = null;
	this.init();
}
SlideEffect.LEFT = 0;
SlideEffect.RIGHT = 1;
SlideEffect.SLIDE_INTERVAL = 60;
SlideEffect.prototype = {
	init:function(){
		var _this = this;
		this.validate();
		this.effect = new JEffect(this.UI.containerUI,"scrollLeft",0,0);
		this.effect.onComplete = function(){
			if(_this.onAfterSlide)
				_this.onAfterSlide.call(this);
		}
	},
	validate:function(){
		this.UIContainerWidth = this.UI.containerUI.offsetWidth;
		this.UITabsWidth = 0;
		var bothMargin = 0;
		for(var i=0,len=this.UI.tabsUI.length;i<len;i++){
			if(0 == i)
				bothMargin = parseInt(StyleMgr.getCSSStyle(this.UI.tabsUI[i],"marginLeft")) + parseInt(StyleMgr.getCSSStyle(this.UI.tabsUI[i],"marginRight"));
			this.UITabsWidth += this.UI.tabsUI[i].offsetWidth + bothMargin;
		}
	},
	hasHeadSpace:function(){
		return this.UI.containerUI.scrollLeft>0;
	},
	hasTailSpace:function(){
		return this.UITabsWidth - this.UI.containerUI.scrollLeft - this.UIContainerWidth > 0;
	},
	slideToScale:function(x){
		var _this = this;
		if(x == this.UI.containerUI.scrollLeft)
			return;
		if(this.onBeforeSlide)
			this.onBeforeSlide.call(this);
		this.effect.setConstriant(JEffect.CONSTRIANT_FROM,this.UI.containerUI.scrollLeft);
		this.effect.setConstriant(JEffect.CONSTRIANT_TO,x);
		this.effect.analyze();
		this.effect.start();
	},
	slideToHead:function(){
		this.slideToScale(0);
	},
	slideToTail:function(){
		this.slideToScale(this.UITabsWidth-this.UIContainerWidth);
	},
	getCurrentScale:function(){
		return this.UI.containerUI.scrollLeft;
	},
	setScale:function(scale){
		this.UI.containerUI.scrollLeft = scale;
	}
}

var JTabPanel = function(){
	this.onStyle = "on";
	this.tabChangedListeners = [];
	this.activeTabIndex = -1;
	this.tabCtrls = [];
	this.tabContents = [];
}
JTabPanel.prototype = {
	setActiveTab:function(index){
		if(index<0||index>this.tabCtrls.length-1)
			return;//break
		var elapsedIndex = this.activeTabIndex;
		//if(index == elapsedIndex)
		//	return;//break;
		if(elapsedIndex != -1){
			this._setActiveTabUI(elapsedIndex,false);
		}
		this._setActiveTabUI(index,true);
		this.activeTabIndex = index;
		this.fireTabChanged(elapsedIndex,index);
	},
	_setActiveTabUI:function(index,active){
		if(active){
			if(!StyleMgr.hasCSSClass(this.tabCtrls[index],this.onStyle))
				StyleMgr.addCSSClass(this.tabCtrls[index],this.onStyle);
			this.tabContents[index].style.display = "block";
		}else{
			if(StyleMgr.hasCSSClass(this.tabCtrls[index],this.onStyle))
				StyleMgr.removeCSSClass(this.tabCtrls[index],this.onStyle);
			this.tabContents[index].style.display = "none";
		}
	},
	addTab:function(tabCtrl,tabContent){
		var _this=this;
		tabCtrl.setAttribute("jtabpanelindex",this.tabCtrls.length);
		tabCtrl.onclick = function(){
			var index = parseInt(this.getAttribute("jtabpanelindex"));
			_this.setActiveTab(index);
		}
		this.tabCtrls.push(tabCtrl);
		this.tabContents.push(tabContent);
	},
	addTabChangeListener:function(l){
		if(typeof l == "function"){
			this.tabChangedListeners.push(l);
		}
	},
	fireTabChanged:function(elapsedIndex,activeIndex){
		for(var i=0,len=this.tabChangedListeners.length;i<len;i++){
			this.tabChangedListeners[i].call(this,elapsedIndex,activeIndex);
		}
	},
	getActiveTabIndex:function(){
		return this.activeTabIndex;
	}
}

var Cookie = {
	setCookie:function(name,value,expires,path,domain,secure){
		if(!name || !value)
			return;
		var candidate = [];
		candidate.push(name+"="+encodeURIComponent(value));
		if(expires)
			candidate.push("expires="+expires.toGMTString());	
		if(path)
			candidate.push("path="+path);	
		if(domain)
			candidate.push("domain="+domain);
		if(secure)
			candidate.push("secure");
		document.cookie = candidate.join(";");	
	},
	getCookie:function(name){
		var res = "(?:; )?"+name+"=([^;]*);?";
		var re = new RegExp(res);
		if(re.test(document.cookie))
			return decodeURIComponent(RegExp["$1"]);
		else
			return null;	
	},
	deleteCookie:function(name,path,domain){
		this.setCookie(name,"",new Date(0),path,domain);
	}
}

function setTheme(tid){
	document.body.className = tid;
	Cookie.setCookie("theme_id",tid,new Date(new Date().getTime() + 31*24*60*60*1000));
}

function addBookmark(url,title){
    if (window.sidebar) {
        window.sidebar.addPanel(title, url,"");
    } else if( document.all ) {
        window.external.AddFavorite( url, title);
    } else if( window.opera && window.print ) {
        return true;
    }
}

function setHomePage(url, o){
	if(document.all){
		o.style.behavior='url(#default#homepage)';
		o.setHomePage(url);
	}else if (window.sidebar){
		if(window.netscape){
			try{ 
				netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 
			}catch (e){}
		}
		var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
		prefs.setCharPref('browser.startup.homepage',url);
	}else{
	}
	return false;
}

var MailAssistant = {
	varifyBeforeSign:function(){
		var gm = document.mailsForm;
		var vDomain = gm.mailList;
		var vName = gm.mailUsername;
		var vPw = gm.mailPassword;
		if(!vName){
			alert('用户名不能为空');
			vName.focus();
			vName.select();
			return false;
		}
		if(vPw.value == ""){
			alert("密码不能为空");
			vPw.focus();
			vPw.select();
			return false;
		}
		switch(vDomain.value){
			case "163":
				gm.action="http://reg.163.com/CheckUser.jsp"
				gm.url.value="http://fm163.163.com/coremail/fcg/ntesdoor2?lightweight=1&verifycookie=1&language=-1&style=15";
				gm.username.value=vName.value;
				gm.password.value=vPw.value;
				gm.enterVip.value='';
				break;
			case "126":
				gm.action="https://reg.163.com/logins.jsp?type=1&product=mail126&url=http://entry.mail.126.com/cgi/ntesdoor?lightweight%3D1%26verifycookie%3D1%26language%3D0%26style%3D-1";
				gm.domain.value="126.com";
				gm.username.value=vName.value+"@126.com";
				gm.password.value=vPw.value;
				break;
			case "188":
				gm.action="http://reg.mail.188.com/servlet/coremail/login?language=0&style=1";
				gm.user.value=vName.value;
				gm.pass.value=vPw.value;
				break;
			case "vip163":
				gm.action="https://ssl1.vip.163.com/logon.m?language=-1&style=-1";
				gm.username.value=vName.value;
				gm.password.value=vPw.value;
				break;
			case "netease":
				gm.action="http://web.netease.com/cgi/login?verifycookie=1&language=0";
				gm.username.value=vName.value;
				gm.password.value=vPw.value;
				break;
			case "sohu":
				gm.action="http://passport.sohu.com/login.jsp";
				gm.url.value="";
				gm.UserName.value=vName.value;
				gm.Password.value=vPw.value;
				gm.id.value=vName.value;
				gm.username.value=vName.value;
				gm.password.value=vPw.value;
				gm.m.value=vName.value;
				gm.passwd.value=vPw.value;
				gm.mpass.value=vPw.value;
				gm.loginid.value=vName.value+"@sohu.com";
				gm.fl.value="1";
				gm.vr.value="1|1";
				gm.appid.value="1000";
				gm.ru.value="http://login.mail.sohu.com/servlet/LoginServlet";
				gm.eru.value="http://login.mail.sohu.com/login.jsp";
				gm.ct.value="1173080990";
				gm.sg.value="5082635c77272088ae7241ccdf7cf062";
				break;
			case "yahoo":
				gm.action="https://edit.bjs.yahoo.com/config/login";
				gm.login.value=vName.value;
				gm.passwd.value=vPw.value;
				break;
			case "yahoocn":
				gm.action="https://edit.bjs.yahoo.com/config/login";
				gm.login.value=vName.value+"@yahoo.cn";
				gm.passwd.value=vPw.value;
				break;
			case "tom":
				gm.action="http://login.mail.tom.com/cgi/login";
				gm.user.value=vName.value;
				gm.pass.value=vPw.value;
				break
			case "21cn":
				gm.action="http://passport.21cn.com/maillogin.jsp";
				gm.LoginName.value=vName.value;
				gm.passwd.value=vPw.value;
				gm.domainname.value="21cn.com";
				gm.UserName.value=vName.value+'@21cn.com';
				break;
			case "china":
				gm.action="http://mail.china.com/coremail/fcg/login";
				gm.user.value=vName.value;
				gm.pass.value=vPw.value;
				gm.domainname.value="china.com";
				break;
			case "mailchina":
				gm.action="http://freemail.china.com/extend/gb/NULL/NULL/NULL/SignIn.gen";
				gm.LoginName.value=vName.value;
				gm.passwd.value=vPw.value;
				break;
			case "citiz":
				gm.action="http://www.citiz.net/icp/login.jsp";
				gm.username.value=vName.value+"@citiz.net";
				gm.password.value=vPw.value;
				break;
			case "56":
				var gUrsHost=["m113","m48","m49"];
				gm.action="http://"+gUrsHost[((new Date()).getSeconds())%3]+".56.com/mail/mail.56";
				gm.username.value=vName.value;
				gm.password.value=vPw.value;
				break;
			case "Xinhuanet":
				gm.action="http://mail.xinhuanet.com/login.jsp";
				gm.username.value=vName.value;
				gm.password.value=vPw.value;
				gm.domainname.value='xinhuanet.com';
				break;
			case "sina":
				gm.action="http://mail.sina.com.cn/cgi-bin/login.cgi";
				gm.u.value=vName.value;
				gm.psw.value=vPw.value;
				break;
			case "eyou":
				gm.action="http://ssl.eyou.com/mail_login.php";
				gm.LoginName.value=vName.value;
				gm.Password.value=vPw.value;
				break;
			case "yeah":
				gm.action="https://reg.163.com/logins.jsp?type=1&product=mailyeah&url=http://entry.mail.yeah.net/cgi/ntesdoor?lightweight%3D1%26verifycookie%3D1%26style%3D-1";
				gm.domain.value="yeah.net";
				gm.username.value=vName.value+"@yeah.net";
				gm.password.value=vPw.value;
				break;
			case "263":
				gm.action="http://mailbeta.263.net/xmweb";
				gm.usr.value=vName.value;
				gm.pass.value=vPw.value;
				gm.domain.value="263.net";
				gm.func.value="login";
				break;
			case "qq":
				gm.action="http://mail.qq.com/cgi-bin/login";
				gm.u.value=vName.value;
				gm.p.value=vPw.value;
				gm.starttime.value=(new Date()).valueOf();
				break;
			case "vipsina":
				gm.action="http://vip.sina.com.cn/cgi-bin/login.php";
				gm.domain.value='vip.sina.com';
				gm.u.value=vName.value;
				gm.psw.value=vPw.value;
				break;
			case "Gmail":
				gm.action="https://www.google.com/accounts/ServiceLoginAuth";
				gm.Email.value=vName.value;
				gm.Passwd.value=vPw.value;
				break;
			case "cren":
				gm.action="http://passport.sohu.com/login.jsp";
				gm.loginid.value=vName.value+"@chinaren.com";
				gm.passwd.value=vPw.value;
				gm.fl.value="1";
				gm.vr.value="1|1";
				gm.appid.value="1005";
				gm.ru.value="http://profile.chinaren.com/urs/setcookie.jsp?burl=http://alumni.chinaren.com/";
				gm.ct.value="1174378209";
				gm.sg.value="84ff7b2e1d8f3dc46c6d17bb83fe72bd";
				break;
			case "tianya":
				gm.action="http://www.tianya.cn/user/loginsubmit.asp";
				gm.vwriter.value=vName.value;
				gm.vpassword.value=vPw.value;
				break;
			case "baidu":
				gm.action="http://passport.baidu.com/?login";
				gm.username.value=vName.value;
				gm.password.value=vPw.value;
				break;
			case "51":
				gm.action="http://passport.51.com/login.5p";
				gm.passport_51_user.value=vName.value;
				gm.passport_51_password.value=vPw.value;
				gm.gourl.value="http%3A%2F%2Fmy.51.com%2Fwebim%2Findex.php";
				break;
			case "xn":
				gm.action="http://login.xiaonei.com/Login.do";
				gm.email.value=vName.value;
				gm.password.value=vPw.value;
				break;
		}
		vPw.value="";
		return true;
	},
	init:function(){
		var _this = this;
		document.mailsForm.onsubmit = function(){
			return _this.varifyBeforeSign();
		}
		document.mailsForm.mailList.onchange = function(){
			Cookie.setCookie("_mail_assistant_vendor",this.selectedIndex,new Date(new Date().getTime() + 31*24*60*60*1000));
		}
		var savedVendor = Cookie.getCookie("_mail_assistant_vendor");
		if(savedVendor)
			document.mailsForm.mailList.selectedIndex = parseInt(savedVendor);
	}
}
