//string wrapper
String.prototype.replaceAll = function(search, replace){
	var regex = new RegExp(search, "g");
	return this.replace(regex, replace);
};
String.prototype.trim = function() {
	return this.replace(/(^\s+)|(\s+$)/g, "");
};
var cookie = function(key, value, options){
	if(typeof value=="undefined"){
		value=null;
		if(document.cookie && document.cookie!=''){
			var arr = document.cookie.split(";");
			for(var i=0;i<arr.length;i++){
				var c = arr[i].trim();
				if (c.substring(0, key.length + 1) == (key + '=')) {
					value = decodeURIComponent(c.substring(key.length + 1));
					break;
				}
			}
		}
		return value;
	}
	options = options || {};
	if(value===null){
		value="";
		options.expires=-1;
	}
	var expires="";
	if(options.expires && (typeof options.expire=="number" || options.expires.toUTCString)){
		var date;
		if(typeof options.expire=="number"){
			date = new Date();
			date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
		}else{
			date = options.expires;
		}
		expires = "; expires=" + date.toUTCString();
	}
	var path = options.path ? '; path=' + (options.path) : '';
	var domain = options.domain ? '; domain=' + (options.domain) : '';
	var secure = options.secure ? '; secure' : '';
	document.cookie = [key, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
}
var msgMap ={
	"input.account_name":"请输入登录帐号",
	"input.email":"请输入完整的邮箱地址",
	"input.password":"请你输入密码",
	"input.noat":"请使用不包括@部分帐号登录",
	
	"input.account_name.en_US":"please enter your account",
	"input.email.en_US":"please enter your email address",
	"input.password.en_US":"please enter your password",
	"input.noat.en_US":"please enter your account without '@'",
	
	"input.account_name.zh_TW":"請輸入登錄帳號",
	"input.email.zh_TW":"請輸入完整的郵箱地址",
	"input.password.zh_TW":"請你輸入密碼",
	"input.noat.zh_TW":"請使用不包括@部分的帐号登录"
}
var oTxt = document.getElementById("divText");
	if(oTxt){
		var max = oTxt.getElementsByTagName("p").length;
		var i=1;
		setInterval("fnChangeText()",5000);
		function fnChangeText(){
			i=i<max?i+1:1;
			oTxt.className = "mod text-mod text-mod-"+i;
		}
	}
var code = "input.account_name";
var hl = getHl();
function getHl(){
	var hl = getParam("hl");
	if(!hl){
		hl = cookie("hl");
	}
	if(hl == "en")hl = "en_US";
	if(hl == "zh-TW")hl = "zh_TW";
	if(hl == "zh-cn")hl = "zh_CN";
	return hl || "zh_CN";
}
function getId(id){
	return document.getElementById(id);
}
function getParam(key){
	var str = decodeURI(location.search.substring(1));
	if(str=="")return null;
	var arr = str.split("&");
	for(var i=0;i<arr.length;i++){
		if(arr[i].indexOf(key+"=")==0){
			return arr[i].substring(key.length+1);
		}
	}
	return null;
}
function getMsg(code){
	var value = msgMap[code + "." + hl];
	if(!value){
		value = msgMap[code];
	}
	return value;
}
function showError(code, element){
	var msg = getMsg(code);
	var msgpid = getId("msgpid");
	if(msgpid){
		msgpid.innerHTML = msg;
	}else{
		alert(msg);
	}
	if(element){
		element.focus();
	}
}
function setMsg(){
	var msgpid = getId("msgpid");
	if(!msgpid || msgpid.innerHTML.trim() == ""){
		var msg = cookie("ex_msg");
		if(!msg)return;
		msg = msg.replaceAll("\\+", " ");
		if(msgpid){
			msgpid.innerHTML = msg;
		}else{
			alert(msg);
		}
		cookie("ex_msg", "");
	}
}
window.onload=function(){
	//检查是否有异常信息
	setMsg();
	var domain = getId("domain").value;
	var host=location.hostname;
	var pos = host.indexOf("mail.");
	if(pos == 0){
		domain = host.substring(5);		
	}
	pos = host.indexOf("mail.qiye.");
	if(pos == 0 && host != "mail.qiye.163.com"){
		domain = host.substring(10);
		var msgpid = getId("msgpid");
		var msg = getParam("msg");
		if(msgpid){
			msgpid.innerHTML = "";
		}
		if(msg){
			msgpid.innerHTML = "登录失败，请重新尝试";
		}	
	}
	getId("domain").value = domain;
	//设置提示信息
	if(domain==""){
		code="input.email";
	}
	account = getId("account_name");
	account.onfocus=function(){
		if(this.value==getMsg(code)){
			this.value="";
		}
	};
	account.onblur=function(){
		if(this.value==""){
			this.value=getMsg(code);
		}
	};
	//获得cookie中记住的用户名
	var remUserName=cookie("qiye_account");
	if(remUserName){
		if(domain==""){
			account.value=remUserName;
		}else{
			account.value=remUserName.replace("@" + domain, "");
		}
		getId("remUsername").checked="checked";
		getId("password").focus();
	}else{
		if(domain!=""){
			account.value="";
			account.focus();
		}else{
			account.value=getMsg(code);
		}
	}
	
};
function frmvalidator(){
	account = getId("account_name");
	p = getId("password");
	var account_name=account.value;
	var password=p.value;
	var domain = getId("domain").value;
	if(account_name=="" || account_name==getMsg(code)){
		showError(code, account);
		return false;
	}
	if(password==""){
		showError("input.password", p);
		return false;
	}
	var pos = account_name.indexOf("@");
	if(pos > 0){
		var ndomain=account_name.substring(pos + 1);
		if(domain == ""){
			getId("domain").value = ndomain;
		}else if(ndomain != domain){			
			showError("input.noat");
			return false;
		}
		account.value=account_name.substring(0,pos);
	}else if(domain == ""){
		showError("input.email", account);
		return false;
	}
	var form = getId("loginform");
	var url = form.action;
	if(!url){
		url = "https://entry.qiye.163.com/domain/domainEntLogin";
	}
	if(getId("secure").checked || (getId("all_secure") && getId("all_secure").checked)){
		url = url.replace("http:", "https:");
	}else{
		url = url.replace("https:", "http:");
	}
	form.action = url;
	
	//是否记住用户名
	var host=location.hostname;
	if(getId("remUsername").checked){		
   		var acc = account.value+"@" + domain;
   		cookie("qiye_account", acc, {domain:host,path:"/",expires:4});		
	}else{
		cookie("qiye_account", acc ,{domain:host,path:"/",expires:0});
	}
	//记住所选语言
	cookie("hl", hl, {domain:host,path:"/",expires:4});
	return true;
}
function DrawImage(ImgD,iwidth,iheight){
	if(ImgD==null||ImgD.src==null)
		return;
	var image=new Image();
	image.src=ImgD.src;
	if(image.width>0 && image.height>0){
		if(image.width/image.height>= iwidth/iheight){
			if(image.width>iwidth){ 
				ImgD.width=iwidth;
				ImgD.height=(image.height*iwidth)/image.width;
			}else{
				ImgD.width=image.width; 
				ImgD.height=image.height;
			}
		}else{
			if(image.height>iheight){ 
				ImgD.height=iheight;
				ImgD.width=(image.width*iheight)/image.height; 
			}else{
				ImgD.width=image.width; 
				ImgD.height=image.height;
			}			
		}
	}
}
