﻿//控制图片大小
function reSize(myImg, maxWidth, maxHeight)
{
    var iniPicWidth = myImg.width;
    var iniPicHeight = myImg.height;
    var iniRate = iniPicHeight/iniPicWidth;

    if(iniPicWidth>maxWidth || iniPicHeight>maxHeight)
    {
        var newW = maxWidth;
        var newH = Math.floor(newW*iniRate);
        if (newH>maxHeight)
        {
            var newH = maxHeight;
            var newW = Math.floor(newH/iniRate);
        }
        myImg.width = newW;
        myImg.height = newH;
    }
}

//写图片Cookie 
//IsType=true;表示添加;
//IsType=false;表示删除;
//定义Cookie是否可用
var IsCookie = true;
function WritePhotoImage(ID_Code, IsType)
{
    try
    {
        var strPhotoInfo = getCookie("PhotoImage");
        var date=new Date();
        var strDate = date.getFullYear() + "-" + date.getMonth() + "-" + date.getDay();
        var expireDays=2;
        //将date设置为10天以后的时间
        date.setTime(date.getTime()+expireDays*24*3600*1000);
        if(IsType == true)
        {
            if(strPhotoInfo == null)
            {
                strPhotoInfo = ",";
            }
            strPhotoInfo = strPhotoInfo + ID_Code + ",";
      
            gsetCookie("PhotoImage",strPhotoInfo, date, "/", "duk.cn", "");
  
            if(getCookie("PhotoImage") == null)
            {
                IsCookie = false;
                return "error";
            }
            return "取消";
        }
        else
        {
            var reg = new RegExp("(," + ID_Code + ",)","g");
            strPhotoInfo = strPhotoInfo.replace(reg, ","); 
            gsetCookie("PhotoImage",strPhotoInfo, date, "/", "duk.cn", "");
            if(getCookie("PhotoImage") == null)
            {
                IsCookie = false;
                return "error";
            }
            return "添加";
        }
    }
    catch(e)
    {        
        IsCookie = false;
        return "error";
    }
}

//2007-10-16
function PhotoSpaceCookieChk(url, obj)
{
    try
    {
        var strPhotoInfo = getCookie("PhotoSpace");
        var date=new Date();
        var strDate = date.getFullYear() + "-" + date.getMonth() + "-" + date.getDay();
        var expireDays=2;
        //将date设置为10天以后的时间
        date.setTime(date.getTime()+expireDays*24*3600*1000);
        if(obj.checked == true)
        {
            if(strPhotoInfo == null || strPhotoInfo == "")
            {
                strPhotoInfo = "[duk.cn]";
            }
            strPhotoInfo = strPhotoInfo + url + "[duk.cn]";
            gsetCookie("PhotoSpace",strPhotoInfo, date, "/", "duk.cn", "");
        }
        else
        {
            strPhotoInfo = strPhotoInfo.replace("[duk.cn]" + url + "[duk.cn]",  "[duk.cn]"); 
            
            gsetCookie("PhotoSpace",strPhotoInfo, date, "/", "duk.cn", "");
        }
    }
    catch(e)
    {        
    }
}

//2007-10-16
function PhotoSpaceCookie(allUrl)
{
    var date=new Date();
    var strDate = date.getFullYear() + "-" + date.getMonth() + "-" + date.getDay();
    var expireDays=2;
    //将date设置为10天以后的时间
    date.setTime(date.getTime()+expireDays*24*3600*1000);
    gsetCookie("PhotoSpace",allUrl, date, "/", "duk.cn", "");
}

function gsetCookie(name,value,expires,path,domain,secure)
{
   
	var curCookie=name+"="+escape(value)+
	((expires)?"; expires="+expires.toGMTString():"")+
	((path)?"; path="+path:"")+
	((domain)?"; domain="+domain:"")+
	((secure)?"; secure":"");	
	document.cookie=curCookie;
	

}

function getCookie(name)
{
	var dc=document.cookie;	
	var prefix=name+"=";	
	var begin=dc.indexOf("; "+prefix);	
	if(begin==-1)
	{
		begin=dc.indexOf(prefix);		
		if(begin!=0)return null;		
		
	}else 
	begin+=2;	
	var end=document.cookie.indexOf(";",begin);	
	if(end==-1)
	end=dc.length;	
	return unescape(dc.substring(begin+prefix.length,end));	
}

function deleteCookie(name,path,domain)
{
	if(getCookie(name))
	{
		document.cookie=name+"="+
		((path)?"; path="+path:"")+
		((domain)?"; domain="+domain:"")+
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";		
		
	}
}

function checkPhoto(ID, objText)
{
    var return_str;
    if(objText.innerHTML.indexOf("添加") >= 0)
    {
        if(IsCookie == true)
        {
            return_str = WritePhotoImage(ID, true);
            if(return_str == "error")
            {
                alert("您关闭Cookie");
                return_str = "添加";
            }
        }
        else
        {
            alert("您关闭Cookie");
            return_str = "添加";
        }
    }
    else
    {
        if(IsCookie == true)
        {
            return_str = WritePhotoImage(ID, false);
            if(return_str == "error")
            {
                alert("您关闭Cookie");
                return_str = "取消";
            }
        }
        else
        {
            alert("您关闭Cookie");
            return_str = "取消";
        }
    }
    objText.innerHTML = return_str;
}



//首页头部搜索
 function search(ele,inputId)
    {
        var input = document.getElementById(inputId);
        if(input && ele.url)
        {
            if(input.value.length <= 1)
            {
                alert('“关键字”必须是一个字以上');
                input.focus();
            }
            else
            {
                if(ValidatorKeyWord(input)==false)
                {
                    var divs = document.getElementsByTagName("div");
                    var errwords = '';
                    if(divs)
                    errwords = divs.innerText;
                    alert('不能搜索以下常用字：\r\n'+ errwords);
                }
                else
                {
                    window.open(ele.url + escape(input.value));
                }
            }
        }
    }

function ValidatorKeyWord(textbox)
{
    var divs = document.getElementsByTagName("div");
    var keywords = null;
    if(divs)
    {
        for(var i=0; i<divs.length; i++)
        {
            if(divs[i].id && divs[i].id.indexOf("keyWords") > 0)
            {
                keywords = divs[i].innerText
                break;
            } 
        }
    }
    if(keywords.indexOf(textbox.value)>=0)
    {
        return false;
    }
    
    return true;
}    
    
var info_str = '请输入搜索关键字';
function _focus (obj) {
    if (obj.value == info_str) {
        obj.value = '';
    }
}
function _blur (obj) {
    if (obj.value.replace(/^\s*$/, '') == '') {
        obj.value = info_str;
    }
}

function bookmark(){
window.external.AddFavorite('http://www.doker.cn', '读客！')
}


//////NEW20061212
function Ajax (callback) {
    callback.req = null;
    // set callback
    callback.callback = function (str) {alert(str)};
    // listener
    callback.listener = function () {
        if (callback.req.readyState == 4) {
            if (callback.req.status == 200) {
                // right
                callback(callback.req.responseText);
            }
            else {
                // error
                callback('错误！');
            }
        }
    }
    callback.init = function () {
        if (window.XMLHttpRequest) {
            callback.req = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            // isIE = true;
            try {
                callback.req = new ActiveXObject("Msxml2.XMLHTTP");
            } catch(e) {
                try {
                    callback.req = new ActiveXObject("Microsoft.XMLHTTP");
                } catch(e) {
                    callback.req = false;
                }
            }
        }
        if (callback.req) {
            callback.req.onreadystatechange = function () {callback.listener()};
        }
    }
    // send 
    callback.send = function (url, content) {
        callback.req.open("post", url, true);
        callback.req.setRequestHeader('Connection', 'close');
        callback.req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        callback.req.send(content);
    }
};


function _submit (code,type) {
	Ajax(callback);
    callback.init();
    callback.send('/AddCount.aspx?Code='+code+'&Type='+type);
}
function _updatecount(dtCode){
    Ajax(callback);
    callback.init();
    callback.send('/Dissertation/aa.aspx?dtCode=' + dtCode);
    }
function callback (content) {
    //alert(content);
    //document.getElementById('returnvalue').innerHTML += content;
}
//////NEW


