﻿// JScript 文件

//取得元素的值
function getElement(ele)
{
    if (ele && typeof ele == "string")
    {
        var elem = document.getElementById(ele);
        if ( elem == null )
        {
            elem = document.all.item( ele );
        }
        return elem;
	}	
	return ele;
}

//添加一条记录的收藏
function SaveCollection( resid )
{
    window.showModalDialog( "AddDatabase.aspx?resid=" + resid, "",  "dialogWidth=750px;dialogHeight=400px;center=yes;help=no;resizable=yes;status=no;scroll=yes;" );
}

//添加选取数据库的收藏
function Add2MyCollec()
{
    var resid = "";
    var objs = getcheckobjs();
    if ( objs == null )
    {
        return;
    }
    
    var obj = null;
	for( var i = 0; i < objs.length;  ++ i )
	{
		obj = objs[i];
		if ( obj == null )
		{
		    continue;
		}
		if ( obj.name.indexOf("check_db_") == -1 )
		{
		    continue;
		}
		if( obj.checked == true && obj.value != "" )
		{
		    if( resid.length>0 )
		    {
		        resid += ";";
		    }
		    resid += obj.value;
		}
	}
	
	if( resid.length==0 )
	{
	    alert( "请选择要收藏的目标资源库！" );
	    return false;
	}
	var url = "AddDatabase.aspx?resid=" + resid;
    window.showModalDialog( url, "", "dialogWidth=750px;dialogHeight=400px;center=yes;help=no;resizable=yes;status=no;scroll=yes;" );
    return false;
}

//选择分组情况		
function selectgroup( obj, from, to )
{
    var xobj = null;
    for( var i = from; i <= to; ++ i )
    {
        xobj = getElement( "check_db_" + i );
        if( xobj != null )
        {
            xobj.checked = false;
        }
    }
    if( obj.checked )
    {
        obj = getElement( "check_db_" + from );
        if( obj != null && (!obj.disabled ) )
        {
            obj.checked = true;
        }
    }
}

//显示分组情况
function showgroup( obj, from, to )
{
    if( obj.src.indexOf( '01.gif' ) != -1 )
    {//当前关闭
        obj.src = "Image/02.gif";
        for( var i = from; i <= to; ++ i )
        {
            obj = getElement( "tr_" + i  );
            if( obj != null )
            {
                obj.style.display = "block";
            }
        }
    }
    else
    {
        obj.src = "Image/01.gif";
        for( var i = from; i <= to; ++ i )
        {
            obj = getElement( "tr_" + i );
            if( obj != null )
            {
                obj.style.display = "none";
            }
        }
    }
}

//取得所有check对象
function getcheckobjs()
{
    var parent = getElement("dbList");
    if ( parent == null )
    {
        parent = document;
    } 
  
    var objs = parent.getElementsByTagName( "INPUT" );
    if ( objs == null )
    {
        return  null;
    }
    
    return objs;
}

//选择检查或CHECK或UNCHECK
function checkall(objs,count)
{
    var nCheck = 0;
    var obj = null;
    for( var i = 0; i < objs.length; ++ i )
    {
        obj = objs[i];
        if ( obj == null )
        {
            continue;
        }
        if ( obj.name.indexOf("check_db_") == -1 )
        {
            continue;
        } 
        if ( obj.disabled )
        {
            continue;
        }
        if ( count == 0 )
        {
            obj.checked = false;
        }
        else if ( count > 0 )
        {
            if( nCheck < count )
            {
                obj.checked = true;
            }
            else 
            {
                obj.checked = false;
            }
        }
        if ( obj.checked )
        {
            ++ nCheck;
        }
    }
    return nCheck;
}
		
//检测是否选择数据库个数据超过最大个数
function checksel(obj, count)
{
    if ( count <= 0 )
    {
        count = 20;
    }
   
    if ( obj == null )
    {
        return;
    }
    if ( ! obj.checked )
    {
        return;
    }
   
    var objs = getcheckobjs();
    if ( objs == null )
    {
        return;
    }
    
    var nCheck = checkall( objs, -1 );
    if( nCheck >= count )
    {
        obj.checked = false;
        alert( "一次最多只能添加"+count+"个数据库进行跨库检索" );
    }
    else
    {
        obj.checked = true;
    }
}

//选择所有数据库
function selectall(count)
{
    var objs = getcheckobjs();
    if ( objs == null )
    {
        return;
    }
    checkall( objs, count );
}

//显示TABLE
function showtab( str )
{
    if( str == "classdb" )
    {
        getElement( "tab_class" ).className = "main_searchnav_on";
        getElement( "tab_find" ).className = "main_searchnav_off";
        
        getElement( "class_db" ).style.display = "block";
        getElement( 'find_db').style.display = "none";
    }
    else if( str == "finddb" )
    {
        getElement( "tab_class" ).className = "main_searchnav_off";
        getElement( "tab_find" ).className = "main_searchnav_on";
        
        getElement( "class_db" ).style.display = "none";
        getElement( "find_db" ).style.display = "block";
    }
}

//查找数据库
function finddatabase()
{
    var value ="";
    var strTemp = "";
    var obj = getElement( 'byname' );
    if ( obj != null )
    {
        value = obj.value;
    }
    if( value != "" )
    {
        value = value.Trim();
        strTemp += "byname=" + encodeURI( value );
        value = "";
    }
	
    obj = getElement( 'byszm' );
    if ( obj != null )
    {
        value = obj.options[obj.options.selectedIndex].value;
    }
    if( value != "" )
    {
        if( strTemp != "" )
        {
            strTemp += "&";
        }
        strTemp += "byszm=" + encodeURI( value );
        value = "";
    }

    obj = getElement( 'bysjklx' );
    if ( obj != null )
    {
        value = obj.options[obj.options.selectedIndex].value;
    }
    if( value != "" )
    {
        if( strTemp != "" )
        {
            strTemp += "&";
        }
        strTemp += "bysjklx=" + encodeURI( value );
        value = "";
    }
	
    obj = getElement( 'bylanguage' );
    if( obj != null )
    {
        value = obj.options[obj.options.selectedIndex].value;
    }
    
    if( value != "" )
    {
        if( strTemp != "" )
        {
            strTemp += "&";
        }
        strTemp += "bylanguage=" + encodeURI( value );
        value = "";
    }
	
    obj = getElement('m');
    if ( obj != null && obj.checked )
    {
        strTemp += "&m=";
    }
    
    obj = getElement( 'bycb' );
    if ( obj != null )
    {
        value = obj.options[obj.options.selectedIndex].value;
    }
    if( value != "" )
    {
        if( strTemp != "" )
        {
            strTemp += "&";
        }
        strTemp += "bycb=" + encodeURI( value );
        value = "";
    }
	
    if( strTemp == "" )
    {
        alert( '请输入查询条件！' );
    }
    else
    {
        //window.frames.item( 'searchFrame' ).location.href = "AllQuickSearch.aspx?cz=true&" + strTemp;
        var url = "ShowResDB.aspx?action=find&" + strTemp;
        
        updatedb( url );
    }
    return false;
}

function findbyfirstletter(letter)
{
    var url = "ShowResDB.aspx?action=find&" + "byszm=" + encodeURI( letter );
    updatedb( url );
}

//清除内容
function clearcontent()
{
    var obj = getElement( 'byname' );
    if( obj != null )
    {
        obj.value = "";
    }
	
    obj = getElement( 'byszm' );
    if( obj != null )
    {
        obj.options.selectedIndex = 0;
    }
	
    obj = getElement( 'bysjklx' );
    if( obj != null )
    {
        obj.options.selectedIndex = 0;
    }
	
    obj = getElement( 'bylanguage' );
    if( obj != null )
    {
        obj.options.selectedIndex = 0;
    }
	
    obj = getElement( 'bycb' );
    if( obj != null )
    {
        obj.options.selectedIndex = 0;
    }
}

//显示分类数据库		
function selectdb( str ,dbs )
{
    if( str.length == 0 )
    {
        return;
    }
    
    var url = "ShowResDB.aspx?action=class&selid="+ encodeURI(str);
    if ( dbs != null && dbs.length > 0 )
    {
        url += "&dbs="+ dbs;
    }
	updatedb( url );
}

//更新层数据
function updatedb(url)
{
    var obj = getElement("dbList");
    if( obj == null )
    {
        return;
    }
    var sValue = "";
    var ajax = new Ajax();
    
	ajax.OnCallBack = function( xmlObj )
	{
		if ( xmlObj.readyState != 4 && xmlObj.readyState != 'complete' )
		{
		    return;
		}
		sValue = xmlObj.responseText;
		if( sValue == null )
		{
			return;
		}
		//alert( sValue );
	    if( sValue == "Error" )
        {
            obj.innerHTML = '取数据错误！';
        }
        else
        {
            obj.innerHTML = sValue;
        }
	}
	
	var objuid = getElement("uid"); 
	if ( objuid != null )
	{
	    url += "&uid=" + encodeURI(objuid.value);
	}
	
	ajax.ExecuteCall( url );
}	

//选定select的值
function seloption(obj,value)
{
    if ( obj == null || obj.options == null )
    {
        return;
    }
    for(var i = 0; i< obj.options.length; ++ i )
    {
        if ( obj.options[i].value == value )
        {
            obj.options[i].selected = true; 
            return;
        }
    }
}

//加载检索表达式值
function loadquery(resid)
{
    //题名='中国' AND 主题='社会' OR 刊名='社会学'
    var obj = getElement("query_" + resid);
	if ( obj == null || obj.value.length == 0 || obj.value.indexOf("='") == -1 )
	{
	    return;
	}
	var arr = obj.value.split("' ");
	if ( arr.length == 0 )
	{
	    return;
	}
	
	var pos = 0, i = 0;
	var fldval = "", relval = "", wordval = "";
	
	for( i = 0; i < arr.length; ++ i )
	{
	    pos = arr[i].indexOf("='");
	    if ( pos == -1 )
	    {
	        continue;
	    }
	    
	    fldval = arr[i].substr(0, pos);
	    wordval = arr[i].substr( pos+2 );
	    wordval = wordval.replace("'","");
	    
	    if ( i > 0 )
	    {
	        pos = fldval.indexOf(" ");
	        if ( pos == -1 )
	        {
	            continue;
	        }
	        relval = fldval.substr(0, pos);
	        fldval = fldval.substr(pos+1);
	        seloption( getElement("relation_" + resid + "_" + i ), relval );
	    }
	    seloption( getElement("field_" + resid + "_" + i), fldval );
	    getElement("word_" + resid + "_" + i ).value = wordval;
	}
}

//添加检索条件
function addquery(obj, resid)
{
    if ( resid == null )
    {
        return;
    }
    if ( resid.length == 0 )
    {
        return;
    }
     
    var sValue = "";
    var ajax = new Ajax();
    
    var elem = getElement("pop"); 
    if ( elem == null )
    {
        return;
    }
	
    var pos = getAbsoluteLocation( obj );
    if ( pos == null )
    {
        return;
    }
    
	ajax.OnCallBack = function( xmlObj )
	{
		if ( xmlObj.readyState != 4 && xmlObj.readyState != 'complete' )
		{
		    return ;
		}
		sValue = xmlObj.responseText;
		if( sValue == null )
		{
			return;
		}
		
	    if( sValue == "Error" )
        {
            elem.innerHTML = '取数据错误！';
        }
        else
        {
            elem.innerHTML = sValue;
        }
        
        loadquery(resid);
        
        elem.style.left = pos.left-200;
        elem.style.top = pos.top;
        elem.style.display = "block";
	}
 
	var url = "ShowResDB.aspx?action=field&resid="+ encodeURI(resid);
	ajax.ExecuteCall( url );
}

//取得该资源是否被勾选
function getcheckbox(resid)
{
    var obj = null;
    var objs = getcheckobjs();
    for( var i=0; i<objs.length; i++ )
    {
        obj = objs[i];
        if( obj==null || obj=="undefined" || obj.disabled )
        {
            continue;
        }
        if( obj.value==resid )
        {
            return obj;
        }
    }
    return null;
}

//保存查询
function savequery(resid)
{
    var obj = getElement("query_" + resid);
    if ( obj == null )
    {
        return;
    }
    var cobj = getElement("fieldcount_" + resid);
    if ( cobj == null )
    {
        return;
    }
    if ( cobj.value.length == 0 )
    {
        return;
    }
    var nCount = parseInt(cobj.value);
    if ( nCount <= 0 )
    {
        return;
    } 
    
    var query = "";
    var fldobj = null, relobj = null, wordobj = null;
    for ( var i = 0; i< nCount ; ++ i )
    {
        fldobj = getElement("field_" + resid + "_" + i);
        if ( fldobj == null )
        {
            return;
        }
        wordobj = getElement("word_" + resid + "_" + i);
        if ( wordobj == null )
        {
            return;
        }
        if ( wordobj.value == "" || wordobj.value.length == 0 )
        {
            continue;
        }
    
        if ( query != "" )
        {
            relobj = getElement("relation_" + resid + "_" + i );
            if ( relobj != null )
            {
                query +=" ";
                query += relobj.options[relobj.options.selectedIndex].value;
                query +=" ";
            }
            else 
            {
                query += " ";
            }
        }
        query += fldobj.options[fldobj.options.selectedIndex].value;
        query += "='";
        query += wordobj.value;
        query += "'";
    }
    
    obj.value = query;
    if( query.length>0 )
    {
        obj = getcheckbox( resid );
        if( obj!=null )
        {
            obj.checked = true;
        }
    }
    //alert( query );
    closequery();
}

//关闭查询
function closequery()
{
    var obj = getElement("pop"); 
    if ( obj == null )
    {
        return;
    }
    obj.style.display = "none";
}

function submitquery(resid)
{
    savequery(resid);
    
    var obj = getElement("query_" + resid);
    if ( obj == null )
    {
        return;
    }
    if( obj.value.length==0 )
    {
        return;
    }
    
    var query = resid + ":" + obj.value;
    var objchk = getElement("checkword");
    var url = "PreDeal.aspx?querystr=" + encodeURI(query);
    if ( objchk != null )
    {
        if ( ! objchk.checked )
        {
            url += "&autotrans=false";
        } 
    }
    window.location.href = url;
}

//取得检索表达式
function getquery(resid,word)
{
    var query = "";
    var obj = getElement("query_" + resid);
    if ( obj == null )
    {
        return null;
    }
    if ( obj.value.length == 0 )
    {
        return null;
    }
    
    var value = obj.value;
    var pos = value.indexOf(":");
    if ( pos > 0 )
    {
        value = value.substring( pos + 1 );
    }
    
    if ( value.indexOf("='") != -1 )
    {
        query = resid + ":" + value;
    }
    else if ( word != "" )
    {
        query = resid + ":" + value +"='" + word + "'";
    }
    return query;
}

//构建检索表达式
function buildquery(word)
{
    var obj = null;
    var query = "",temp = "";
    var objs = getcheckobjs();
    for( var i = 0; i < objs.length; ++ i )
    {
        obj = objs[i];
        if ( obj == null )
        {
            continue;
        }
        if ( obj.name.indexOf("check_db_") == -1 )
        {
            continue;
        } 
        if ( obj.disabled )
        {
            continue;
        }
        if ( obj.checked )
        {
            if ( obj.value.length == 0 )
            {
                continue;
            }
            temp = getquery(obj.value, word);
            if ( query != "" )
            {
                query += "^";
            }
            query += temp;
        }
    }
    return query;
}

function   trim(str)   
{   
    var   par   =   /^\s+/;   
    var   strRes   =   str.replace(par,'');     //Trim   the   font   blank
    
    par   =   /\s+$/;   
    strRes   =   strRes.replace(par,'');       //   Trim   the   end   blank
    return   strRes;       
}

//检索
function search(flag)
{
    var obj = getElement("wordtext");
    if ( obj == null )
    {
        return;
    }
    
    var word = obj.value;
    word = word.replace('#', ' ');
    word = word.replace('&', ' ');
	word = trim(word);
	
    var objchk = getElement("checkword");
    
    var query = buildquery(word);
    if ( query == "" )
    {
        if ( obj.value.length == 0 )
        {
            alert( "检索词不能为空! " );
        }
        else
        {
            alert( "至少需要选择一个数据库才能检索！" );
        }
        return false;
    }
    
    var url = "PreDeal.aspx?querystr=" + encodeURI(query);
    if ( objchk != null )
    {
        if ( ! objchk.checked )
        {
            url += "&autotrans=false";
        } 
    }
    if( word.length>0 )
    {
        url += "&keyword=" + encodeURI(word);
    }
    
    if ( flag != null )
    {
        window.open(url);
    }
    else 
    {
        window.location.href = url;
    }
    return false;
}

//表达式检索
function querysearch()
{
    var query = buildquery("");
    if ( query == "" )
    {
        alert("没有设置检索表达式！");
        return false;
    }
    var url = "PreDeal.aspx?querystr="+encodeURI(query)+"&autotrans=false";
    window.open(url);
    return false;
}

function dictOnline()
{
    var obj = getElement( 'wordtext' );
    if( obj==null )
    {
        return;
    }
    if( obj.value.length==0 )
    {
        return;
    }
    window.open( "http://dict.cn/" + obj.value + ".htm" );
}
