var TF = {
	E: function(id) {
		return document.getElementById(id);
	}
};
var m_CurrentTab="";
var m_RParent="";
var m_CurrentRowIndex="";
var currentUser=readCookie("landejiUserEmail");
if(currentUser!="")
{
    currentUser+="_";
}
else
{
    currentUser="UserEmail";
}
function CheckUpImg(list,imgsize,ischeckwidth)
{
    if(list.value!="")
    {
        var tempValue = list.value;
        
        var isimg=true;
    
        var pos=list.value.lastIndexOf(".");
		var end=list.value.length;
		var value=list.value.substring(pos+1,end);
		
		if (pos==-1){
		    isimg=false;
		}
		if (value!="jpg" && value!="gif" && value!="bmp" && value!="JPG" && value!="GIF" && value!="BMP"){
		    isimg=false;
		}
			
		if (!isimg)
		{
		    TF.E('submit').disabled = "disabled";
			alert("上传图片类型错误，只能是jpg/jpeg/gif/bmp格式。");			
		}
//		else
//		{
//	        TF.E('previewimg').innerHTML="<img src='"+tempValue+"'align='middle' id='imgsmall' onload=CheckUpImgSize(this,"+ imgsize +","+ ischeckwidth +") />";
//        }
    }
}

function CheckMatterDiscuss()
{
    var action=TF.E("action");
    var content=TF.E('MatterDiscuss');
    if(action.value=="MatterReplyDiscuss") content=TF.E('MatterReplyDiscuss');    
    if(content.value=="")
    {
        alert("内容不能为空！");
        content.focus();
        return false;
    }
    return true;
}

function ContentReply(op1,op2)
{
    if(m_CurrentRowIndex!="")
    {
        CancelReply(m_RParent,m_CurrentTab);
    }
    var rParent=TF.E("RParentID");
    rParent.value=op1;
    var obj=TF.E("tab"+op2).rows;
    var newBlankRow = TF.E("tab"+op2).insertRow();
    var point = newBlankRow.rowIndex;
    obj[point].insertCell();
    obj[point].cells(0).innerHTML = "&nbsp;";    
    obj[point].insertCell();
    obj[point].cells(1).innerHTML = "<textarea name='MatterReplyDiscuss' id='MatterReplyDiscuss' rows='3' width='600' cols='80'></textarea><input type='submit' name='SubmitReply' id='SubmitReply' value='提交' />";
    m_RParent=op1;
    m_CurrentTab=op2;
    m_CurrentRowIndex=point;
    TF.E("action").value="MatterReplyDiscuss";
    TF.E("clickReply"+op2).innerHTML="<a href=\"javascript:CancelReply('"+op1+"','"+op2+"')\">收起回复</a>";
}

function CancelReply(op1,op2)
{
TF.E("tab"+op2).deleteRow(m_CurrentRowIndex);
TF.E("clickReply"+op2).innerHTML="<a href=\"javascript:ContentReply('"+op1+"','"+op2+"')\">回复</a>";
m_CurrentRowIndex="";
TF.E("action").value="MatterDiscuss";
}

function MatterReplyFocus()
{
    if(m_CurrentRowIndex!="")
    {
        CancelReply(m_RParent,m_CurrentTab);
    }
    TF.E("action").value="MatterDiscuss";
}

function ChangeMoreList(op)
{
var areadiv=TF.E('area'+op);
var ctldiv=TF.E('ctl'+op);
var otherdiv=TF.E('other'+op);
areadiv.className="area";
ctldiv.className="ctl";
ctldiv.innerHTML="<A onclick=\" ChangeList('"+op+"')\"  href='javascript:;'>更多▼</A>";
otherdiv.className="undis";
}

function ChangeList(op)
{
var areadiv=TF.E('area'+op);
var ctldiv=TF.E('ctl'+op);
var otherdiv=TF.E('other'+op);
areadiv.className="area bg1";
ctldiv.className="ctl bg2";
ctldiv.innerHTML="<A onclick=\"ChangeMoreList('"+op+"')\" href='javascript:;'>关闭▲</A>";
otherdiv.className="dis";
}

function hovernav()
{
    var navdiv=TF.E('nav');
    if(navdiv.className=="dis")
    {
        navdiv.className="undis";
    }
    else
    {
        navdiv.className="dis";
    }
}

//加入收藏夹
function AddFavorite(sURL, sTitle) 
{ 
    try 
    { 
        window.external.addFavorite(sURL, sTitle); 
    } 
    catch (e) 
    { 
        try 
        { 
            window.sidebar.addPanel(sTitle, sURL, ""); 
        } 
        catch (e) 
        { 
            alert("加入收藏失败，请使用Ctrl+D进行添加"); 
        } 
    } 
}

/*写入cookie*/
function writeCookie(name, value, hours){
	var expire = "";
	if(hours != null){
		expire = new Date((new Date()).getTime() + hours * 3600000);
		expire = "; expires=" + expire.toGMTString();
	}
	document.cookie = name + "=" + escape(value) + expire + ";path=/";
}

//读取cookies
function existCookie(name)
{
var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg)) return unescape(arr[2]);
else return null;
}
//删除cookies
function delCookie(name)
{
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval=existCookie(name);
if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString()+ ";path=/";
}


/*读取cookie*/
function readCookie(name){
	var cookieValue = "";
	var search = name + "=";
	if(document.cookie.length > 0){
		offset = document.cookie.indexOf(search);
		if (offset != -1){
			offset += search.length;
			end = document.cookie.indexOf(";", offset);
			if (end == -1) end = document.cookie.length;
			cookieValue = unescape(document.cookie.substring(offset, end));
		}
	}
	return cookieValue;
}

/*读取cookie 带解码*/
function readCookieDecode(name){
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1,c.length);
                if (c.indexOf(nameEQ) == 0) return decodeURIComponent(c.substring(nameEQ.length,c.length));
        }
        return null;
}

function setCookie(name, value, hours)
{
	var expire = "";
	if(hours != null)
	{
		expire = new Date((new Date()).getTime() + hours * 3600000);
		expire = "; expires=" + expire.toGMTString();
	}
	document.cookie = name + "=" + escape(value) + expire;
}

function LinkMatter(op1,op2,op3)
{
    if(op3!="") ChangeClick(op3);
    var strMatters=readCookie(currentUser+"landejiindex_LinkMatters");//历史记录
    var cookieHistory=readCookie(currentUser+"landejiindex_QuickVisit");//快捷访问
    //判断快捷访问中是否存在此链接的记录
    if(cookieHistory=="" || (cookieHistory!="" && cookieHistory.indexOf(op1+","+op2+"|")<0))//快捷访问中不存在此链接
    {
        if(strMatters=="")//历史记录为空时
        {
            writeCookie(currentUser+"landejiindex_LinkMatters",op1+","+op2+"|","99999");
            var broseUL=TF.E("HistoryBroseUL");
            if(broseUL)
            {
                var o=document.createElement("li");
                o.className="fBlackAYello";
                broseUL.appendChild(o);
                o.innerHTML="<A title='"+op1+"' href='"+op2+"' target='_blank'>"+op1+"</A>";
            }
        }
        else//历史记录不为空时，添加到开头
        {
            
            if(strMatters.indexOf(op1)<0)//历史记录中不存在此记录
            {
                strMatters=op1+","+op2+"|"+strMatters;
                writeCookie(currentUser+"landejiindex_LinkMatters",strMatters,"99999");
                var obj=TF.E("HistoryBroseUL");
                if(obj)
                {
                    var o=document.createElement("li");
                    o.className="fBlackAYello";
                    obj.insertBefore(o,obj.childNodes[0]);
                    var tempop1=op1;
                    if(tempop1.length>5) tempop1=tempop1.substring(0,5);
                    o.innerHTML="<A title='"+op1+"' href='"+op2+"' target='_blank'>"+tempop1+"</A>";
                }                
            }
            else
            {
                strMatters=strMatters.replace(op1+","+op2+"|","");
                strMatters=op1+","+op2+"|"+strMatters;
                writeCookie(currentUser+"landejiindex_LinkMatters",strMatters,"99999");
            }
        }
        var historydiv=TF.E("LandejiHistoryBrose");
        if(historydiv)
        {
            historydiv.style.display="block";
        }
    }
    else//快捷访问中存在此记录，记录显示在第一位
    {
        cookieHistory=cookieHistory.replace(op1+","+op2+"|","");
        cookieHistory=op1+","+op2+"|"+cookieHistory;
        writeCookie(currentUser+"landejiindex_QuickVisit",cookieHistory,"99999");
    }
}

function SearchBaiduInLandeji()
{
    var word=TF.E("SearchTag");
    var seachType=TF.E('SearchInLandeji').checked==''?1:0;
    var cate=TF.E('SearchCategoryID');
    if(word.value!="" && word.value!="站内搜索" && word.value!="站外搜索")
    {
        var url="/category/"+cate.value==""?1:cate.value+"-"+encodeURI(word.value)+"-"+seachType+"--0";
        location.href=url;
    }
    else
    {
        alert("请输入关键词！");
    }   
}

function SetInputValue(opName,opValue,beforeValue)
{
    var obj=TF.E(opName);//获取对象
    if(obj.value=="" || obj.value==beforeValue)
    {
        obj.value=opValue;
    }
}
var xmlHttp;
function GetXmlHttpObject()
{
    var xmlHttp=null;
    try
    {
        /* Firefox, Opera 8.0+, Safari*/
        xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
        /* Internet Explorer*/
        try
        {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}
function CheckXmlHttp()
{
    if(xmlHttp!=null)
    {
        xmlHttp.abort();
        xmlHttp=null;
    }
    xmlHttp=GetXmlHttpObject();

    if (xmlHttp==null)
    {
        alert ("您的浏览器不支持AJAX！");
        return false;
    }
    return true;
}

function AddFavoriteLink()
{
    if (CheckXmlHttp() == false) return;
    var url="/ajaxhandler.ashx?action=AddLink";
    var title=TF.E("MyFavouriteTitle");//标题
    var link=TF.E("MyFavouriteLink");//链接地址
    var ucateg=TF.E("MyFavouriteCategory");//用户自定义分类
    var intro=TF.E("MyFavouriteIntro");//备注
    var tag=TF.E("MyFavouriteTag");//标签
    
    if(title.value=="")
    {
        alert("请输入标题！");
        return;
    }
    var rStr = new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5]+$");
    if(title.value.length>50)
    {
        alert("标题不能超过50个字！");
        return;
    } 
	if(!title.value.match(rStr)){
		alert("只能由英文、中文、数字、下划线组成！");
		return;
	}
    if(link.value=="")
    {
        alert("请输入链接地址！");
        return;
    }
    rStr = /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
    if(!link.value.match(rStr)){
		alert("您输入的网址不正确！");
		return;
	}
    var cookiefavoritelist=readCookie(currentUser+"FavoriteList");
    if(cookiefavoritelist!="" && cookiefavoritelist.indexOf(link.value+",")>=0)
    {
        alert("您以收藏过此网站！");
        return;
    }
      
    url+="&MyFavouriteTitle="+escape(title.value);    
    url+="&MyFavouriteLink="+escape(link.value);
    if(ucateg.value!="")
    {
        url+="&MyFavouriteCategory="+escape(ucateg.value);
    }
    if(intro.value!="")
    {
        url+="&MyFavouriteIntro="+escape(intro.value);
    }
    if(tag.value!="")
    {
        url+="&MyFavouriteTag="+(tag.value=="可用“，”“；”“|”隔开"?"":tag.value);
    }
    xmlHttp.onreadystatechange = stateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function ChangeClick(op)
{
    if (CheckXmlHttp() == false) return;
    var url="/ajaxhandler.ashx?action=AddMatterLink&id="+op;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function AddAdminFavoriteLink()
{
    if (CheckXmlHttp() == false) return;
    var url="/ajaxhandler.ashx?action=AddAdminLink";
    var title=TF.E("AdminFavouriteTitle");//标题
    var link=TF.E("AdminFavouriteLink");//链接地址
    var ucateg=TF.E("AdminFavouriteCategory");//用户自定义分类
    var intro=TF.E("AdminFavouriteIntro");//备注
    var tag=TF.E("AdminFavouriteTag");//标签
    
    if(title.value=="")
    {
        alert("请输入标题！");
        return;
    }
    var rStr = new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5]+$");
    if(title.value.length>25)
    {
        alert("标题不能超过50个字！");
        return;
    } 
	if(!title.value.match(rStr)){
		alert("只能由英文、中文、数字、下划线组成！");
		return;
	}
    if(link.value=="")
    {
        alert("请输入链接地址！");
        return;
    }
    rStr = /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
    if(!link.value.match(rStr)){
		alert("您输入的网址不正确！");
		return;
	}
      
    url+="&MyFavouriteTitle="+escape(title.value);    
    url+="&MyFavouriteLink="+escape(link.value);
    if(ucateg.value!="")
    {
        url+="&MyFavouriteCategory="+escape(ucateg.value);
    }
    if(intro.value!="")
    {
        url+="&MyFavouriteIntro="+escape(intro.value);
    }
    if(tag.value!="")
    {
        url+="&MyFavouriteTag="+(tag.value=="可用“，”“；”“|”隔开"?"":tag.value);        
    }
    xmlHttp.onreadystatechange = stateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function stateChanged() 
{ 
    if (xmlHttp.readyState==4 && xmlHttp.status==200)
    { 
        var message=xmlHttp.responseText;
        if(message=="ok")
        {
            closeDivWindows('FavoriteTip');
            if(TF.E('AdminFavoriteTip'))
            {
                closeDivWindows('AdminFavoriteTip');
            }
            alert("收藏成功！");
//            TF.E("FavoriteTip").innerHTML="<div style='text-align:center;'>收藏成功！<br/><a href=\"javascript:closeDivWindows('FavoriteTip')\">关闭</a></div>";
        }
        else if(message=="exist")
        {
            alert("已存在，收藏失败！");
        }
        else
        {
            alert("收藏失败！");
        }
    }    
}
function GetClicksLinkMatter()
{
    if (CheckXmlHttp() == false) return;
    var url="/ajaxhandler.ashx?action=ClicksLinkMatter";
    xmlHttp.onreadystatechange = stateClicksLinkMatter;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function stateClicksLinkMatter()
{
    if (xmlHttp.readyState==4 && xmlHttp.status==200)
    { 
        var message=xmlHttp.responseText;
        if(message!="error")
        {
            var obj=TF.E("div4");
            var oDiv=document.createElement("div");
            oDiv.id="SecondClicksCategory";
            oDiv.className="tabs"; 
            oDiv.innerHTML=message;
            obj.parentNode.insertBefore(oDiv,obj.nextSibling); 
        }
    }    
}

function CheckInput(obj,strerror)
{
    if(obj.value=="")
    {
        alert(strerror);
        return false;
    }
    return true;
}

/*设置div的显示属性为block*/
function OpenDiv(op1)
{
    if(TF.E(op1))
    {
        TF.E(op1).style.display="block";
    }
}

/*设置div的显示属性为inline*/
function InlineDiv(op1)
{
    TF.E(op1).style.display="inline";
}
/*删除div*/
function DelDiv(op1)
{
    if(TF.E(op1))
    {
        TF.E(op1).outerHTML="";
    }
}
/*设置div的显示属性为none*/
function CloseDiv(op)
{
    if(TF.E(op))
    {
        TF.E(op).style.display="none";
    }
}
function checkUserLogin(divName,linkTitle,linkAddress,tempcontent)
{
    if(readCookie("landejiUserEmail")=="")
    {
        openDivWindows('LoginTip');
    }
    else
    {
        FavouriteLink(divName,linkTitle,linkAddress,tempcontent);
    }
}

function FavouriteLink(divName,linkTitle,linkAddress,tempcontent)
{
    openDivWindows(divName);
    var title=TF.E("MyFavouriteTitle");
    var link=TF.E("MyFavouriteLink");
    var content=TF.E("MyFavouriteIntro");
    title.value=linkTitle;
    link.value=unescape(linkAddress);
    content.value=unescape(tempcontent);
}

function AdminFavouriteLink(divName,linkTitle,linkAddress,tempcontent)
{
    openDivWindows(divName);
    var title=TF.E("AdminFavouriteTitle");
    var link=TF.E("AdminFavouriteLink");
    var content=TF.E("AdminFavouriteIntro");
    title.value=linkTitle;
    link.value=linkAddress;
    content.value=unescape(tempcontent);
}
function CheckLogin(theForm)
{ 
    if(theForm.UserEmail.value.length == 0 && theForm.PassWord.value.length == 0)
    {
        location.href="/login/";
    }
    else
    {
        if (theForm.UserEmail.value.length == 0)
        { 
            alert("懒得记提醒您：请输入邮箱名称！");
            return false;
        }
        if (theForm.PassWord.value.length == 0)
        {
            alert("懒得记提醒您：请输入密码！");
            return false;
        }
        var nextLogin=TF.E("RemNextLogin");
        if(nextLogin && nextLogin.type=="checkbox" && nextLogin.checked==true)
        {
            TF.E("RemNextLogin").value=1;
        }
        return true;
    }
}
function AddCategory()
{
    if (CheckXmlHttp() == false) return;
    var url="/ajaxhandler.ashx?action=AddCategory";
    var ucategn=TF.E("AddCategoryName");//用户自定义分类
    if(ucategn.value=="")
    {
        alert("请输入分类名称！");
        return;
    }
    if(ucategn.value.length>5)
    {
        alert("分类名称在5个字以内！");
        return;
    }
    url+="&CategoryName="+ucategn.value;
    xmlHttp.onreadystatechange = AddCategoryStateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function AddCategoryStateChanged()
{
if (xmlHttp.readyState==4 && xmlHttp.status==200)
    { 
        var message=xmlHttp.responseText;
        if(message=="error")
        {
            alert("创建失败！");
        }
        else if(message=="exist")
        {
            alert("此分类已存在！");
        }
        else
        {
            var ucategn=TF.E("AddCategoryName");//用户自定义分类
            var oDiv=document.createElement("option");
            oDiv.selected="selected";
            oDiv.value=message;   
            oDiv.innerText=ucategn.value;  
            TF.E('MyFavouriteCategory').appendChild(oDiv);           
            CloseDiv('AddCategory');
            InlineDiv("MyFavouriteCategory");
            alert("创建成功！");
        }
    }    
}

function UpdateUCategory(fid,ucate)
{
    if (CheckXmlHttp() == false) return;
    var url="/ajaxhandler.ashx?action=UpdateUCate&FID="+fid+"&UCate="+ucate;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function Delete()
{ 
    if(confirm("你真的要删除吗？"))
    {
        return true; 
    }
    else
    { 
        return false;
    } 
}

function Option()
{ 
    if(confirm("你确实要进行此操作？"))
    {
        return true; 
    }
    else
    { 
        return false;
    } 
}

function UserExit()
{
    if(confirm("您真的要退出吗？"))
    {
        writeCookie("landejiUserEmail", "", 1); 
        setTimeout("javascript:location.href='/'",2000);
    }   
} 

function OpenModalWindow(url, width, height){
	window.showModalDialog(url, window, "dialogWidth:"+width+"px; dialogHeight:"+height+"px; status:0; resizable:0; scroll:0"); 
	window.location.reload();
}

//转发到腾讯微博
function postToWb(title,link,appkey,picURL){
    var _t = encodeURI(title);
    var _url = encodeURI(link);
    var _appkey = encodeURI(appkey);//你从腾讯获得的appkey
    var _pic = encodeURI(picURL);//（列如：var _pic='图片url1|图片url2|图片url3....）
    var _site = '';//你的网站地址
    var _u = 'http://v.t.qq.com/share/share.php?title='+_t+'&url='+_url+'&appkey='+_appkey+'&site='+_site+'&pic='+_pic;
    window.open( _u,'转播到腾讯微博', 'width=700, height=680, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, location=yes, resizable=no, status=no' )
}

