var Flag=true;
//定义disable时，text输入框的背景颜色#999999
var disableColor="#C1C1C1";	
var Relation=" AND ";		//逻辑检索词
var splitword;		//分词

function showColumns(thisForm){
	
	var inputs=thisForm.all.tags("input");		//取得当前input集合

	//初始化所有的选择框
	for(var i=0;i<inputs.length;i++){
		if(inputs[i].type=="text"){
			inputs[i].disabled=false;
			inputs[i].style.backgroundColor="";
		}
	}
	
	//依次选择所有的被选择的checkbox，取其value
	//如果前面的text输入框的name没有在这个value中出现，
	//就对这个text输入框进行操作：【值设置为空，不可编辑，设置背景色】
	for(var i=0;i<inputs.length;i++){
		if(inputs[i].type=="checkbox"){			//查找复选框，取值类似: "标题,日期"
			if(inputs[i].checked){
				strColumns=inputs[i].value;
				for(var j=0;j<inputs.length;j++){
					if(inputs[j].type=="text"){		//查找输入框，name类似："标题"
						if(strColumns.indexOf(inputs[j].name)==-1){
							inputs[j].value="";
							inputs[j].disabled=true;
							inputs[j].style.backgroundColor=disableColor;
//							alert(inputs[i] + ":" + inputs[j]);
						}
					}
				}
			}
		}
	}
}

function SearchForm_onsubmit()
{
	var channelid="";
	for(var i=0;i<document.searchform.length;i++)
	{
		if(document.searchform.elements[i].checked==true)
		{
			channelid = channelid+","+document.searchform.elements[i].alt;
		}
	}
	
	if(!checknumber())
	{
		return false;
	}
	
	if(!searchworddispart()){
	return false;
	}
	
	if(channelid=="")
	{
		alert("请选择检索的数据库");
		return false;
	}
	else
	{
		var	Length=channelid.length;
		document.searchform.channelid.value=channelid.substring(1);
		//alert(document.searchform.channelid.value);
	
	}
	//alert(document.searchform.searchword.value);
return true;
}

function searchworddispart()
{
var searchwordtemp="";		//检索表达式
var searchwordfield="";		//检索字段名
var searchwordvalue="";		//检索字段值
var j=0;
//如果有值则组合，并且，在输入框中为以空格间隔的词，要做以切分(为完成)
for(var i=0;i<document.searchform.length;i++)
	{
		if(document.searchform.elements[i].disabled==false)
		{
			if(document.searchform.elements[i].type=="text"){
				if(document.searchform.elements[i].value!=""){
					searchwordfield=document.searchform.elements[i].name;
					searchwordvalue=document.searchform.elements[i].value;
					//切词
					worddispart(searchwordvalue,' ');
					searchwordvalue=splitword;
					
					searchwordtemp=searchwordtemp+searchwordfield+"="+searchwordvalue;
					j=i;
					break;
					}
			}
		}
	}
//防止多加连接词措施

for(var i=j+1;i<document.searchform.length;i++)
	{
		if(document.searchform.elements[i].disabled==false)
		{
			if(document.searchform.elements[i].type=="text"){
				if(document.searchform.elements[i].value!=""){
					searchwordfield=document.searchform.elements[i].name;
					searchwordvalue=document.searchform.elements[i].value;
					//切词
					worddispart(searchwordvalue,' ');
					searchwordvalue=splitword;
					
					searchwordtemp=searchwordtemp+Relation+searchwordfield+"="+searchwordvalue;
					}
			}
		}
	}
	document.searchform.searchword.value=searchwordtemp;

	return true;
}

function worddispart(searchwordvalueStr,splitString)
{
	var my_array=searchwordvalueStr.split(splitString);
	var tempword="";
	var maxlength=my_array.length;
	var searchwordvalueArray=new Array();
	var i=0;
	var j=0;
	var splitword1="";
	//剔除其中的空串
	for(i=0,j=0;i<maxlength;i++){
		if(my_array[i]!=""){
		searchwordvalueArray[j]=my_array[i];
		j++;
		}
	}
	maxlength=searchwordvalueArray.length;
	
	for(i=0;i<maxlength-1;i++){
	tempword=tempword+searchwordvalueArray[i]+"' and '";
	}
	if(maxlength>1){
	tempword=tempword+searchwordvalueArray[maxlength-1];
	}
	
	if(maxlength<=1){
	splitword1=searchwordvalueStr;
	}
	else{
	tempword=tempword;
	splitword1=tempword;
	}
return (splitword1);
}

function worddispartor(searchwordvalueStr,splitString)
{
	var my_array=searchwordvalueStr.split(splitString);
	var tempword="";
	var maxlength=my_array.length;
	var searchwordvalueArray=new Array();
	var i=0;
	var j=0;
	var splitword1="";
	//剔除其中的空串
	for(i=0,j=0;i<maxlength;i++){
		if(my_array[i]!=""){
		searchwordvalueArray[j]=my_array[i];
		j++;
		}
	}
	maxlength=searchwordvalueArray.length;
	
	for(i=0;i<maxlength-1;i++){
	tempword=tempword+searchwordvalueArray[i]+"' or '";
	}
	if(maxlength>1){
	tempword=tempword+searchwordvalueArray[maxlength-1];
	}
	
	if(maxlength<=1){
	splitword1=searchwordvalueStr;
	}
	else{
	tempword=tempword;
	splitword1=tempword;
	}
	
return (splitword1);
}

function checkdisplay()
{
	if(document.searchform.displaytype.selectedIndex==0)
	{
		document.all.s_prepage.style.visibility="";
		document.all.s_prepage.style.display="block";
	}
	else
	{
		if(!checknumber())
		{
			return false;
		}
		document.all.s_prepage.style.visibility="hidden";
		document.all.s_prepage.style.display="none";
	}
	return true;
}

function checknumber()
{
	var prepage = document.searchform.prepage.value;
	if(isNaN(prepage))
	{
		alert("输入的显示记录数'"+prepage+"'不是合法数字");
		return false;
	}
	return true;
}

function clearSearchForm() {
for(var i=0;i<document.searchform.length;i++)
	{
		if(document.searchform.elements[i].disabled==false)
		{
			if(document.searchform.elements[i].type=="text"){
				document.searchform.elements[i].value="";
			}
		}
	}
}

function Select() 
{
    for (var i=0;i<document.Outline.elements.length;i++)
    {
        var e = document.Outline.elements[i];
        if (e.name == 'record')
    	   e.checked=Flag;
    }
    Flag=!Flag;
}
function Browse()
{
	document.Outline.action="detail";
	document.Outline.submit();
}
function Download()
{
	document.Outline.action="download";
	document.Outline.submit();
}

function getWord()//获取鼠标选中的文本
{
	var str = document.selection.createRange().text;
	if(typeof(str)!="undefined")
	{
		if(str!="")
	        {
				if(typeof(document.all.searchw)!="undefined")
			{
				document.all.searchw.value=str;
			}
		}
	}
}

function searchwordcheck_diff(thisform)
{
var swd_check=document.searchform.searchw.value;
var swd_op_check=document.searchform.field1.value;
if (swd_op_check=="")
	{
	worddispart(swd_check,' ');
	swd_check=splitword;
	document.searchform.searchword.value=swd_check;		//使用的是默认检索
	}
	else
	{
	worddispart(swd_check,' ');
	swd_check=splitword;
	document.searchform.searchword.value=swd_op_check+"="+swd_check;
	}
	return true;
}

function searchwordcheck(thisform)
{
var swd_check=document.searchform.searchw.value;
var swd_op_check=document.searchform.field1.value;
if (swd_op_check=="")
	{
	worddispart(swd_check,' ');
	swd_check=splitword;
	document.searchform.searchword.value=swd_check;		//使用的是默认检索
	}
	else
	{
	worddispart(swd_check,' ');
	swd_check=splitword;
	document.searchform.searchword.value=swd_op_check+"='"+swd_check+"'";
	}
	doTRSMultiSearch(thisform);
	return true;
}
function show(colName,colVal,colStr){
		var splitStr=colStr;
		var numArray=colVal.split(splitStr);
		var i;
		for(i=0;i<numArray.length;i++){
			document.write("<a href=\"javascript:doReSearch('");
			document.write(colName);
			document.write("','");
			document.write(numArray[i]);
			document.write("')\">");
			document.write(numArray[i]);
			if(i<numArray.length-1)	document.write(";");
			document.write("</a>　")
			
		}
	}
function doReSearch(colName,searchVal){
		var TRSForm=document.re_search;
		TRSForm.searchword.value=colName + "='" + searchVal + "'";
		TRSForm.submit();
	}
	
function creator_detail(colName,colVal,colStr){
		var splitStr=colStr;
		var numArray=colVal.split(splitStr);
		var i;
		for(i=0;i<numArray.length;i++){
			document.write("<a href=http://192.9.200.127:8081/was40/search?channelid=1989&searchword=");
			document.write(colName);
			document.write("=");
			document.write(numArray[i]);
			document.write(">");
			document.write(numArray[i]);
			if(i<numArray.length-1)	document.write(";");
			document.write("</a>　")
			
		}
	}
	


function juanqi2005(data1,data2,data3,data4,data5)
 {
 		var Journal_Year=data1;
		var Journal_Volume=data2;
		var Journal_ISSN=data3;
		var ifpdf=data4;
		var ifarticle=data5;
		if(Journal_Year==2005)
		{ 
		document.write("<option value='");
		document.write(Journal_Year);
		document.write(",");
		document.write(Journal_Volume);
		document.write(",");		
		document.write(Journal_ISSN);
		document.write(",");
		document.write(ifpdf);
		document.write(",");		
		document.write(ifarticle);					
		document.write("'>");
		document.write(Journal_Volume);
		document.write("</option>");
		}
		
 }
function juanqi2004(data1,data2,data3,data4,data5)
 {
 		var Journal_Year=data1;
		var Journal_Volume=data2;
		var Journal_ISSN=data3;
		var ifpdf=data4;
		var ifarticle=data5;
		if(Journal_Year==2004)
		{ 
		document.write("<option value='");
		document.write(Journal_Year);
		document.write(",");
		document.write(Journal_Volume);
		document.write(",");		
		document.write(Journal_ISSN);
		document.write(",");
		document.write(ifpdf);
		document.write(",");		
		document.write(ifarticle);					
		document.write("'>");
		document.write(Journal_Volume);
		document.write("</option>");
		}
		
 }

function juanqi2003(data1,data2,data3,data4,data5)
 {
 		var Journal_Year=data1;
		var Journal_Volume=data2;
		var Journal_ISSN=data3;
		var ifpdf=data4;
		var ifarticle=data5;
		if(Journal_Year==2003)
		{ 
		document.write("<option value='");
		document.write(Journal_Year);
		document.write(",");
		document.write(Journal_Volume);
		document.write(",");		
		document.write(Journal_ISSN);
		document.write(",");
		document.write(ifpdf);
		document.write(",");		
		document.write(ifarticle);					
		document.write("'>");
		document.write(Journal_Volume);
		document.write("</option>");
		}
		
 }
 
 function juanqi2002(data1,data2,data3,data4,data5)
 {
 		var Journal_Year=data1;
		var Journal_Volume=data2;
		var Journal_ISSN=data3;
		var ifpdf=data4;
		var ifarticle=data5;
		if(Journal_Year==2002)
		{ 
		document.write("<option value='");
		document.write(Journal_Year);
		document.write(",");
		document.write(Journal_Volume);
		document.write(",");		
		document.write(Journal_ISSN);
		document.write(",");
		document.write(ifpdf);
		document.write(",");		
		document.write(ifarticle);					
		document.write("'>");
		document.write(Journal_Volume);
		document.write("</option>");
		}
		
 }


function sourcelink(data1,data2)
 {
 		var Full_Text_Path=data1;
		var DC_Format=data2;
		if(DC_Format!='')
		{  
        document.write("<tr><td>【源文件】");
		document.write("<a href=http://10.10.12.9/sourcefile/");
		document.write(Full_Text_Path);
		document.write(".");
		document.write(DC_Format);
		document.write(" target='_blank'>");
		document.write("查看源文件(内网)");
		document.write("</a>");
		
		document.write("&nbsp;&nbsp;&nbsp;<a href=http://218.80.224.167/sourcefile/");
		document.write(Full_Text_Path);
		document.write(".");
		document.write(DC_Format);
		document.write(" target='_blank'>");
		document.write("查看源文件（外网）");
		document.write("</a>");
		document.write("</td></tr>");
		}
		
 } 
	
function doTRSMultiSearch(thisForm){
	try
	{
		var allChannels=parent.frames[1].mutil_channel.selectchannel;
		
		var multiChannel="";
		for(var i=0;i<allChannels.length;i++){
			if(allChannels[i].checked)
			multiChannel += allChannels[i].value + ",";
		}
		if(multiChannel!="")
			multiChannel = multiChannel.substring(0,multiChannel.length-1);
		else{
			return false;
		}
		thisForm.channelid.value=multiChannel;
	}
	catch(E)
	{
		;
		}
		return true;
	}
	


function parmcheck(objSearchForm)
{
	
	var SQLDate,SQL,sql1,sql2,sql3,sel2,sel3;
	SQLDate=SQL=sql1=sql2=sql3="";
        
    var BeginDate=objSearchForm.BeginYear.options[objSearchForm.BeginYear.selectedIndex].value;
	var EndDate=objSearchForm.EndYear.options[objSearchForm.EndYear.selectedIndex].value;
	
	var op2=objSearchForm.op2.options[objSearchForm.op2.selectedIndex].value;
	var op3=objSearchForm.op3.options[objSearchForm.op3.selectedIndex].value;

	//拼出日期条件	
	
	if(BeginDate!=""&&EndDate!="")
	//book_date设置为入库日期
		SQLDate="latest_date=" + BeginDate + " to " + EndDate;
	if(BeginDate!=""&&EndDate=="")
		SQLDate="latest_date>=" + BeginDate;
	if(BeginDate==""&&EndDate!="")
		SQLDate="latest_date<=" + EndDate;
	
        //拼出第一栏条件	
	if(objSearchForm.term1.value!="")
		{
		if(objSearchForm.field1.value!="")
			sql1=objSearchForm.field1.value +"="+ objSearchForm.term1.value;
	    		else
    			sql1="ABSTRACT_CN="+ objSearchForm.term1.value;
		}
	
	       
        //拼出第二栏条件	
	if(objSearchForm.term2.value!="")
		{
		if(objSearchForm.field2.value!="")
	      	sql2=objSearchForm.field2.value +"="+ objSearchForm.term2.value;
	    	else
	  	sql2="AUTHOR="+ objSearchForm.term2.value;
	  	}

        
       

        //拼出第三栏条件	
	if(objSearchForm.term3.value!="")
		{
		if(objSearchForm.field1.value!="")
			sql3=objSearchForm.field3.value +"="+ objSearchForm.term3.value;
	    	else
		sql3="TITLE_CN="+ objSearchForm.term3.value;
		}

        
       
	
	if(sql1==""||sql2=="")
	{
		op2="";
	}
	if(sql1==""&&sql2=="")
	{
		op2="";
		op1="";
		op3="";
	}
	if(sql3=="")
	{
		op3="";
	}
	
	SQL=sql1+op2+sql2+op3+sql3;
        SQL=SQL.replace(",","?");
        SQL=SQL.replace(".","?");
        SQL=SQL.replace("'","?");
        SQL=SQL.replace("-","?");
        SQL=SQL.replace(":","?");
        SQL=SQL.replace("/","?");
	if(SQLDate!="")
	{
		if(SQL!="")
		{
			SQL="("+SQL+") and ("+SQLDate+")";
		}
	  	else
	  	{
	    		SQL=SQLDate;
	  	}
	}
   
	objSearchForm.searchword.value=SQL;
	if(SQL=="")
	{
		alert("检索表达式不能为空!");
		return false;
	}
//	alert(objSearchForm.channelid.value);
//	alert(SQL);
//	return false;
	return true;
}

function parmcheckad(thisform)
{
	        
    var checkSQL=document.SearchForm.Content.value;
        //得到COOKIE,拆分到数组；
  	Old_cookie=document.cookie;
  	var stat = 0;
  	stat=Old_cookie.indexOf("JSESSIONID");
  	if (stat!=0)
  	{
    	  Old_cookie=Old_cookie.substring(4,stat-2);
  	}
  	else
  	{
     	  Old_cookie=Old_cookie.substring(4,stat-1);
  	} 
  	//alert(Old_cookie);  
  	var cookArray=Old_cookie.split("|");
  
  	var i=0;
  	var k=0;
  	var stat=0;
  	while(i<cookArray.length)
  	{
     	  stat=cookArray[i].indexOf(":");
     	  k=i+1;
      	  checkSQL=checkSQL.replace("#"+k,"("+cookArray[i].substring(0,stat)+")");
          //cookArray[i].substring(0,stat);
          i++;
        }
    //alert("checkSQL"+":"+checkSQL);

        checkSQL=checkSQL.replace(",","?");
        checkSQL=checkSQL.replace(".","?");
        checkSQL=checkSQL.replace("'","?");
        checkSQL=checkSQL.replace("-","?");
        checkSQL=checkSQL.replace(":","?");
        checkSQL=checkSQL.replace("/","?");
      

	//alert("sql= "+checkSQL);
	document.SearchForm.searchword.value=checkSQL;
	if(checkSQL=="")
	{
		alert("检索表达式不能为空!");
		return false;
	}
	
	doTRSMultiSearch(thisform);
	return true;
}

function add(field,num)
{
    if(document.FieldSearchForm.elements[num].value!="")
    {
      var op=document.FieldSearchForm.elements[num+1].options[document.FieldSearchForm.elements[num+1].selectedIndex].value;
      var temp=document.FieldSearchForm.elements[num].value;
      var sql="";
      if(field!="history")
      {
         if (field=="pub_number")
         {
         //alert(temp.substring(0,3));
            if (temp.substring(0,3)!="AAI")
            {
               temp="AAI"+temp
            }
         }
         if (field=="subject")
         {
            if (temp.substring(0,1)!="0")
            {
               field="subject_name"
               temp="%"+temp+"%"
            }
         }
         if (field=="school")
         {
            if (temp.substring(0,1)=="0")
            {
               field="school_code"
            }
         }
         temp=temp.replace(" AND "," AND " + field + "=");
         temp=temp.replace(" OR "," OR "+ field + "=");
         temp=temp.replace(" NOT "," NOT "+ field + "=");
         sql="(" + field + "=" + temp + ")";
      }
      else
      {
         sql="(" + temp + ")";
      }
      
      if (document.SearchForm.Content.value!="")
      {
         document.SearchForm.Content.value=document.SearchForm.Content.value+op+sql;       
      }
      else
      {
         document.SearchForm.Content.value=sql;       
      }   
      //alert("sqs= "+sql);
     }
}

function addDate()
{
	var BeginDate,EndDate,SQLDate,opDate;
	var BBeginYear=document.FieldSearchForm.BeginYear.options[document.FieldSearchForm.BeginYear.selectedIndex].value;
	//var BeginMonth=document.FieldSearchForm.BeginMonth.options[document.FieldSearchForm.BeginMonth.selectedIndex].value;
	var EEndYear=document.FieldSearchForm.EndYear.options[document.FieldSearchForm.EndYear.selectedIndex].value;
	//var EndMonth=document.FieldSearchForm.EndMonth.options[document.FieldSearchForm.EndMonth.selectedIndex].value;
	//var EndMonth=document.FieldSearchForm.EndMonth.options[document.FieldSearchForm.EndMonth.selectedIndex].value;
	BeginDate=EndDate=SQLDate=opDate="";
	//if(BeginMonth.length==1)
	//{
	//   BeginMonth="0"+BeginMonth;
	//}
	//if(EndMonth.length==1)
	//{
	//   EndMonth="0"+EndMonth;
	//}
	opDate=document.FieldSearchForm.opDate.options[document.FieldSearchForm.opDate.selectedIndex].value;
        
        //拼出日期条件	
	if(BBeginYear!="")
	{

		//if(BeginMonth!="")
		//{
		//	BeginDate=BeginYear + BeginMonth;
		//}
		//else
		BeginDate=BBeginYear; 
	 }
	if(EEndYear!="")
	{
		//if(EndMonth!="")
		//{
		//	EndDate=EndYear + EndMonth;  						
		//}
		//else
		EndDate=EEndYear; 
	}
	
	if(BeginDate!=""&&EndDate!="")
		//SQLDate="book_date>=" + BeginDate + " AND book_date<=" + EndDate;
		SQLDate="date_doc=" + BeginDate + " to " + EndDate;
	if(BeginDate==""&&EndDate!="")
		SQLDate="date_doc=" + EndDate;
	if(BeginDate!=""&&EndDate=="")
		SQLDate="date_doc=" + BeginDate;
		
        if (SQLDate!="")
        {
          if (document.SearchForm.Content.value!="")
          {
           document.SearchForm.Content.value=document.SearchForm.Content.value+opDate+"("+SQLDate+")";
          }
          else
          {
           document.SearchForm.Content.value="("+SQLDate+")";       
          } 
        }   
}

function showoutchar(charnumber){
       for(var i=0;i<charnumber;i++){
        document.write("&nbsp;&nbsp;&nbsp;&nbsp;");
        }
       }
	   
	   
function SearchForm_mati_onsubmit()
{
	var channelid=document.searchform.channelid.value;
	var searchword=document.searchform.searchword.value;
	
	if(channelid=="")
	{
		alert("请选择检索的资源");
		return false;
	}
	if(searchword=="")
	{
		alert("请填写检索词");
		return false;
	}	
return true;
}
//选择上下页，第一页，最后页
function changePage(i,j){

	if(j>0){
		document.form1.action=i;
		document.form1.submit();
	}
}
//点选go翻页

/**
 * 判断textObj是否有填写数据,及显示错误消息
*/

function isNull(textObj,message)
{	
	if (textObj.value=="")
	{
		textObj.focus();
		if (message==undefined)
		{
			alert ("This field can not be blank!")
		}else
		{
			alert(message)
		}		
		return true;
	}
	return false;
}


/**
 * 判断noObj是否是整数
*/
function isInteger(noObj)
{
	if (noObj.value=="")
	{
		return true;
	}

	if (isNull(noObj) || (noObj.value.indexOf("-")>-1))
	{
		//alert ("Please input an integer!");
		noObj.focus();
		return false;
	}
	if (isNull(noObj) || (noObj.value.indexOf(".")>-1))
	{
		//alert ("Please input an integer!");
		noObj.focus();
		return false; 
	}
	strValue=noObj.value;
	for (i=0;i<strValue.length;i++)
	{
		if (strValue.charCodeAt(i,1)!=46 && (strValue.charCodeAt(i,1)<48 || strValue.charCodeAt(i,1)>57))
		{
			//alert ("Please input an integer!");
			noObj.focus();
			return false;
		}
	}
	return true;
}
