var type163 = 0;
var typePopo = 1;
var type126 = 2;
var type188 = 3;
var typeVip =4;
var typeYeah =5;
var ckKeyFromType = "NEBLOG_LOGTP";
var ckPath = "/";
var ckDomain=".163.com";
var errInfo = ["用户名或密码不正确,请重新输入", "该网易博客用户名不存在", "超时退出或无权限访问,请重新登录"];

var $_dwrInfo;

function trim(str) {
	var trim = str.replace(/^\s*/, "");
	trim = trim.replace(/\s*$/, "");
	return trim;
}

function setFocus(id) {
	if (userId <= 0) {
		$('account_netease').focus();
	} else {
		$(id).focus();
	}
}

function dwrInfo(msg,type,id) {
	try{
		if (!$_dwrInfo) {
			$_dwrInfo = new NetEase.DwrTips({containerId:id});
		}
		$_dwrInfo.appendMsg(msg,type,id);
	}catch(ex){alert(msg);}
}

function onEnterPress(event) {
	if (!$('applyButton') || $('applyButton').disabled ) {
		return;
	}
	var evt = (event) ? event : ((window.event) ? window.event : null);
	if (evt.keyCode == 13) {
		if (location.href.indexOf('cloneone') >= 0) {
			addTransferRequest('account_netease', 'pwd_netease', 'account_type', 'url');
		}
	}

}

var Cookie = {
	set : function(name, value, expirationInDays, path, domain) {
		var cookie = escape(name) + "=" + escape(value);

		if (expirationInDays) {
			var date = new Date();
			date.setDate(date.getDate() + expirationInDays);
			cookie += "; expires=" + date.toGMTString();
		} 

		if (path) {
			cookie += ";path=" + path;
		}
		if (domain) {
			cookie += ";domain=" + domain;
		}
		
		document.cookie = cookie;

		if (value && (expirationInDays == undefined || expirationInDays > 0) && !this.get(name)) {
			return false;
		}
	},

	clear : function(name, path) {
		this.set(name, "", -1, path);
	},
	
	get : function(name) {
		var pattern = "(^|;)\\s*" + escape(name) + "=([^;]+)";
		var m = document.cookie.match(pattern);
		if (m && m[2]) {			
			return unescape(m[2]);
		}else{ 
			return null;
		}
	}
}


function clearUserNameInit(type){
	return  Cookie.clear("NEBLOG_LOG"+getLogStr(type), ckPath);
}

function getNameSuffix(type){
	if(type==type163)
		return  "";
	else if(type==typePopo)
		return  ".popo";
	else if(type==type126)
		return  "@126";
	else if(type==type188)
		return  "@188";		
	else if(type==typeVip)
		return  ".vip";
	else if(type==typeYeah)
		return  "@yeah";
}

function getLogStr(type){
	if(type==type163)
		return  "163";
	else if(type==typePopo)
		return  "POPO";
	else if(type==type126)
		return  "126";
	else if(type==type188)
		return  "188";		
	else if(type==typeVip)
		return  "VIP";	
	else if(type==typeYeah)
		return  "YEAH";		
}

function checkUsername(account) {
	return true;
}

function checkPassword(password) {
	return true;
}

function pwd_js_string(s) {
	return String(s).replace(/\\/g, "\\\\").replace(/'/g, "\\\'").replace(/"/g, "\\\"");
}

function dologin(account, password, type, callback){
	if(checkUsername() && checkPassword()) { //顺利登录
		var strPassword = password;
		
		if(type==type163 || type==typeVip)
			strPassword = pwd_js_string(strPassword);
		if(type==type163  || type==typeVip){
			strPassword = strPassword.substring(0,16);
		}else if(type==typePopo){
			strPassword = strPassword.substring(0,21);
		}			
		strPwdOld = strPassword;
		strPassword = hex_md5(strPassword);		
		
		
		var strUsername = account;	
		strUsername += getNameSuffix(type);
		
		if(type == type126 || type == type188 || type == typeYeah){				
			strPassword = strPwdOld;//.replace(/#/g,"%23");			
		}

		
		clearUserNameInit(type);
		Cookie.clear(strUsername, ckPath);			
					
		//save cookie for login type
		Cookie.clear(ckKeyFromType, ckPath);
		Cookie.set(ckKeyFromType, type, 30, ckPath, ckDomain);
		
		return UserBean.checkAndGetUID(strUsername, strPassword, callback);			
	}
	return null;
}

function normalizeUrl(url) {
	url = trim(url);
	if (url.substring(0,7) != 'http://') {
		url = 'http://' + url;
	}
	return url;
}

function checkMsnUserName(url) {
	url = normalizeUrl(url);
	var reg = /http:\/\/([A-Za-z0-9_-]+).spaces.live.com/;
	var matches = url.match(reg);
	if (matches != null) {
		return matches[1].toString();
	} else {
		return null;
	}
}

function checkSinaUrl (url) {
	url = normalizeUrl(url);
	var reg=/http:\/\/blog\.sina\.com\.cn\/m\/[A-Za-z0-9_-]+|http:\/\/blog\.sina\.com\.cn\/u\/[0-9]+|http:\/\/blog\.sina\.com\.cn\/[A-Za-z0-9_-]+$/;
	var m = url.match(reg);
	if (m != null) {
		return m.toString();
	} else {
		return null;
	}
}

function checkBaiduUrl(url) {
	url = normalizeUrl(url);
	var reg = /http:\/\/hi\.baidu\.com\/[^\/]+/;
	var m = url.match(reg);
	if (m != null) {
		return m.toString();
	} else {
		return null;
	}
}

function checkBlogcnUrl(url) {
	url = normalizeUrl(url);
	var reg=/http:\/\/[A-Za-z0-9_-]+\.blogcn\.com/;
	var m = url.match(reg);
	if (m != null) {
		return m.toString();
	} else {
		return null;
	}
}

function checkSohuUrl(url) {
	url = normalizeUrl(url);
	var reg=/http:\/\/[A-Za-z0-9_-]+\.blog\.sohu\.com|http:\/\/blog\.sohu\.com\/people\/[A-Za-z0-9-_]+/;
	var m = url.match(reg);
	if (m != null) {
		return m.toString();	
 	} else {
 		return null;
 	}
}

function checkQZoneUrl(url) {
	url = normalizeUrl(url);
	var reg = /http:\/\/user\.qzone\.qq\.com\/\w+|http:\/\/new\.qzone\.qq\.com\/\w+|http:\/\/\w+\.qzone\.qq\.com/;
	var m = url.match(reg);
	if (m != null) {
		var tmp =  m.toString();
		if (tmp != 'http://user.qzone.qq.com' && tmp != 'http://new.qzone.qq.com') {
			return m.toString();
		} else {
			return null;
		}
	} else {
		return null;
	}
}

function check51Url(url) {
	url = normalizeUrl(url);
	var reg=/http:\/\/home\.51\.com\/home\.php\?user=[A-Za-z0-9_]{3,16}|http:\/\/home\.51\.com\/[A-Za-z0-9_]{3,16}|http:\/\/[A-Za-z0-9_]{3,16}\.51\.com/;
	var m = url.match(reg);
	if (m!=null) {
		var tmp = m.toString();
		if (tmp != 'http://www.51.com' && tmp != 'http://home.51.com') {
			return m.toString();
		} else {
			return null;
		}
	} else {
		return null;
	}
}

function checkYahooUrl(url) {
	url = normalizeUrl(url);
	var reg = /http:\/\/i\.cn\.yahoo\.com\/[A-Za-z0-9_@\.-]{4,128}/;
	var m = url.match(reg);
	if (m != null) {
		return m.toString();
	} else {
		return null;
	}
}

function checkSoufunUrl(url) {  // http://blog.soufun.com/xiujie
	url = normalizeUrl(url);
	var reg = /http:\/\/blog\.soufun\.com\/[A-Za-z0-9_@\.-]{4,128}/;
	var m = url.match(reg);
	if (m != null) {
		return m.toString();
	} else {
		return null;
	}
}

var special_bokee_site = ["blogs", "tech", "news", "photo", "group", "mall", "life", "media", "finance", "book", "2008", "game", "bank", "talk", "travel", "friend", "it2007"];
function checkBokeeUrl(url) {
	url = normalizeUrl(url);
	var reg = /http:\/\/[A-Za-z0-9]{4,16}\.vip.bokee.com|http:\/\/([A-Za-z0-9]{4,16})\.bokee.com/;
	var m = url.match(reg);
	if (m != null) {
		var name = m[1];
		for (var i=0;i<special_bokee_site.length;++i) {
			if (special_bokee_site[i] == name) {
				return null;
			}
		}
		return m[0];
	} else {
		return null;
	}
}

function checkBlogbusUrl(url){
    url = normalizeUrl(url);
    var reg = /http:\/\/[A-Za-z0-9]{2,16}\.blogbus.com/;
    var m = url.match(reg);
    if (m != null){
        var tmp = m.toString();
        if (tmp != 'http://groups.blogbus.com') {
			return m.toString();
		}else {
			return null;
		}
    } else {
        return null;
    }
}

var special_hexun_site  = ["group" , "lady"];
function checkHexunUrl(url){
    url = normalizeUrl(url);
    var reg = /http:\/\/[A-Za-z0-9]{3,16}\.blog.hexun.com|http:\/\/hexun.com\/([A-Za-z0-9]{3,16})\/default.html/;
    var m = url.match(reg);
    if (m != null){
        var name = m[1];
		for (var i=0;i<special_hexun_site.length;++i) {
			if (special_hexun_site[i] == name) {
				return null;
			}
		}
		return m[0];
    } else {
        return null;
    }
}

function checkRssUrl(url) {
	url = trim(url);
	if (url.substring(0,4) != 'Rss:') {
		return null;
	}
	return url;
}

