function SetCookie(name,value){
    var argv=SetCookie.arguments;
    var argc=SetCookie.arguments.length;
    var expires=(2<argc)?argv[2]:null;
    var path=(3<argc)?argv[3]:null;
    var domain=(4<argc)?argv[4]:null;
    var secure=(5<argc)?argv[5]:false;
    document.cookie=name+"="+escape(value)+((expires==null)?"":("; expires="+expires.toGMTString()))+((path==null)?"":("; path="+path))+((domain==null)?"":("; domain="+domain))+((secure==true)?"; secure":"");
}

function GetCookie(Name) {
    var search = Name + "=";
    var returnvalue = "";
    if (document.cookie.length > 0) {
        offset = document.cookie.lastIndexOf(search);
        if (offset != -1) {
            offset += search.length;
            end = document.cookie.indexOf(";", offset);
            if (end == -1)
            end = document.cookie.length;
            returnvalue=unescape(document.cookie.substring(offset,end));
        }
    }
    return returnvalue;
}

function $(o){
    var o = document.getElementById(o)? document.getElementById(o) : o;
    return o;
}

//检查邮箱登录输入参数
function checkEmail(vForm){
    if(vForm.MailSite.options.selectedIndex==0){
   	    alert("提示：请正确选择你使用的邮箱");
   	    return false;
    } else {
   	    var expdate=new Date();
   	    expdate.setTime(expdate.getTime()+(24*60*60*1000*365));
   	    //SetCookie("MyMail",vForm.MailSite.value,expdate,"/","diyiye.net",false);
   	    SetCookie("MyMail",vForm.MailSite.value,expdate);
    }
    if(vForm.MailUser.value=="" || vForm.MailUser.value=="请在此输入您的用户名"){
   	    alert("提示：邮箱用户名必须填写！");
   	    vForm.MailUser.focus();
   	    return false;
    }
    if(vForm.MailPwd.value=="" || vForm.MailPwd.value.length<3){
   	    alert("提示：邮箱密码必须填写完整！");
   	    vForm.MailPwd.focus();
   	    return false;
    }

    var MailList = new Array(); 
    MailList["163.com"]=new Array("http://reg.163.com/in.jsp?url=http://fm163.163.com/coremail/fcg/ntesdoor2?language=0&style=1","username","password");
    MailList["vip.163.com"]=new Array("http://vip.163.com/logon.m","username","password","enterVip,true; style,");
    MailList["126.com"]=new Array("http://entry.126.com/cgi/login","user","pass","domain,126.com; language,0; bCookie,");
    MailList["sina.com"]=new Array("http://mail.sina.com.cn/cgi-bin/login.cgi","u","psw","logintype,uid; product,mail");
    MailList["vip.sina.com"]=new Array("http://vip.sina.com.cn/cgi-bin/login.cgi","user","pass");
    MailList["sohu.com"]=new Array("http://passport.sohu.com/login.jsp","loginid","passwd","fl,1; vr,1|1; appid,1000; ru,http://login.mail.sohu.com/servlet/LoginServlet; eru,http://login.mail.sohu.com/login.jsp; ct,1173080990; sg,5082635c77272088ae7241ccdf7cf062","@sohu.com");
    MailList["vip.sohu.com"]=new Array("http://passport.sohu.com/login.jsp","loginid","passwd","fl,1; vr,1|1; appid,1013; ru,http://vip.sohu.com/login/viplogin11.jsp; eru,; ct,1173857434; sg,885ebb7884194ee547f224fc8a6a5877","@vip.sohu.com");
    MailList["21cn.com"]=new Array("http://passport.21cn.com/maillogin.jsp","LoginName","passwd","NeedMoreSecurity,on; NeedIpCheck,on");
    MailList["263.net"]=new Array("http://g2wm.263.net/xmweb","usr","pass","domain,263.net; func,login");
    MailList["tom.com"]=new Array("http://bjweb.163.net/cgi/163/login_pro.cgi","user","pass","type,0; style,10");
    MailList["china.com"]=new Array("http://mail.china.com//coremail/fcg/login","user","pass");
    MailList["188.com"]=new Array("http://reg.mail.188.com/servlet/coremail/login","user","pass","domain,188.com; language,0; style,-1");
    MailList["yahoo.com.cn"]=new Array("http://edit.bjs.yahoo.com/config/login","login","passwd",".intl,cn; .done,http://mail.yahoo.com");
    MailList["gmail.com"]=new Array("https://www.google.com/accounts/ServiceLoginAuth","Email","Passwd","continue,http://mail.google.com/mail?zy=l; service,mail; hl,zh-CN");
    MailList["265.com"]=new Array("https://edit.bjs.yahoo.com/config/login","login","passwd",".done,http://edit.bjs.yahoo.com/config/mail?.intl=cn; .intl,cn; .src,ym",".my265");
    var iMail = vForm.MailSite.value;
    var UserName = $("MailUser").value;
    if (MailList[iMail] != null) {
        var cMail = MailList[iMail];
        vForm.action = cMail[0];
        $("MailUser").name = cMail[1];
        $("MailPwd").name = cMail[2];
        if (cMail[3] != null) {
        	var aPairs = cMail[3].split("; ");
        	for (var j=0; j< aPairs.length; j++) {
        		var aPair = aPairs[j].split(",");
        		var el = document.createElement("INPUT");
        		el.type="hidden";
        		el.name=aPair[0];
        		el.value=aPair[1];
        		$("MailSite").appendChild(el);
        	}
        }
        if (cMail[4] != null)
            $("MailUser").value = UserName + cMail[4];
    } else {
        alert("请选择正确的邮箱！");
    }
    vForm.submit();
    $("MailUser").value = UserName;
    $("MailPwd").value = "";
    return false;
}

//设置默认登录邮箱
function initMail(){
	var sMail=GetCookie("MyMail");
	if (sMail!="") {
		var oSel=$("MailSite");
		for (var i=0;i<oSel.length;i++){
			if (oSel[i].value==sMail) {
				oSel[i].selected=true;
				return(false);
			}
		}
	}
}

//设置自动登录
function initLogin(){
	var sUsername=GetCookie("USERNAME");
	if (sUsername!="") {
		var oForm=$("LoginForm");
		oForm.username.value = sUsername;
		oForm.saveuser.checked = true;
	}
}

//切换className
function ExCls(o,a,b){
    o.className=(o.className.indexOf(a)!=-1)?b:a;
}
//切换innerText
function ExTxt(o,a,b){
    o.innerText=(o.innerText.indexOf(a)!=-1)?b:a;
}

//设置select默认值
function selDefault(vSel, vValue) {
    if (vValue == "") return;
    for (var d=0; d<vSel.options.length; d++) {
        if (vSel.options[d].value == vValue ) {
            vSel.selectedIndex = d;
            break;
        }
    }
}
//设置radio/checkbox默认值
function chkDefault(vFrom, vName, vValue) {
    var items = vFrom.elements;
    for (i=0; i<items.length; i++) {
        if ( items[i].name == vName && items[i].value == vValue ) {
            items[i].checked = true;
            break;
        }
    }
}

//跟踪页面点击事件 onmousedown="tracking(event);"
function tracking(e){
    e = e ? e : window.event;
    var s = e.srcElement ? e.srcElement : e.target;
    var a = s.tagName;
    var u = s.href;
    var t = s.innerText ? s.innerText : s.textContent;
    if(a == "A" || a == "IMG"){
        if(a == "IMG"){
            t = s.href || s.src;
            u = s.parentElement || s.parentNode;
        }
        try{
            new Image().src = "/click.jsp?a="+a+"&u="+escape(u)+"&t="+t;
        }catch(ex){}
    }
    return true;
}

//出站统计 onMouseDown="outLog(event);"
function outLog(e) {
	e = e ? e : window.event;
	var o = e.srcElement ? e.srcElement : e.target;
	var t = o.tagName;
	var l = o.href;
	var s = o.innerText ? o.innerText : o.textContent;
	if (t == "IMG") {
		s = o.href || o.src;
		l = o.parentElement || o.parentNode;
	}
	try {
		new Image().src = "/outlog.jsp?t="+t+"&l="+escape(l)+"&s="+s;
	} catch(ex) {}
	return true;
}

//收藏夹统计 onMouseDown="favLog(favId);"
function favLog(favId) {
	try {
		new Image().src = "/myfav_log.jsp?fid=" + favId;
	} catch(ex) {}
	return true;
}

function isValidEmail(str) {
	var validEmailRegExp = /^\w(\.?[-\w])*@\w(\.?[-\w])*\.([a-z]{3}(\.[a-z]{2})?|[a-z]{2}(\.[a-z]{2})?)$/i;
	var isValid = validEmailRegExp.test(str);
	return isValid;
}

function isValidUsername(str)
{
	var validUserNameRegExp = /^[a-z0-9]{3,16}$/i;
	var isValid = validUserNameRegExp.test(str);
	return isValid;
}

function checkLogin(vForm) {
	if (vForm.username.value == '') {
		alert("请输入用户名！");
		vForm.username.focus();
		return false;
	}
	if ( !isValidUsername(vForm.username.value) ) {
		alert("请使用合法的用户名登录！");
		vForm.username.focus();
		return false;
	}
	if (vForm.password.value == '') {
		alert("请输入密码！");
		vForm.password.focus();
		return false;
	}
	return true;
}

