//过滤全角/及半角空格
function trim(sString)
{
	var strTmp ;
	
	strTmp=sString.replace(/^[\s　\t]+|[\s　\t]+$/,"");

	return strTmp ;
}

//过滤sql关键字　防止注入式攻击
function filterKey(aSearchword,aRel)
{
	var result="";
	
	if(aRel==null) aRel="and";
	
	var key="(|)|,|/|@| =|>|<|!|&|*|^|-|+|ADJ|EQU|PRE|AND|XOR|NOT|OR|TO|%|?|'";
	
	var aKeys=key.split("|");
	
	aSearchword=trim(aSearchword);
	
	aSearchword=aSearchword.replace("\\","");
	
	for(i=0;i<aKeys.length;i++)
	{
		
		var reg=new RegExp("\\"+aKeys[i],"gi");

		aSearchword=aSearchword.replace(reg,"\\"+aKeys[i]);
	}
	result=aSearchword.replace(/[\s　\t]+/gi," "+aRel+" ");
	//alert(result);
	return result;
}

//更新拼装子检索词
function upSearchword(aSearchword,aField,aRel,rField,aForm)
{
	var searchword=aSearchword;
	
	var oForm=aForm==null?"document.mainform":aForm;
	
	var obj;
	var oExp="=";
	
	obj= eval(oForm+"."+(rField==null?aField:rField));
	
	//obj=document.aspnetForm.allsearch;
	//alert(obj);
	
	if(obj!=null)
	{
		var oValue=trim(obj.value);
		
		if(oValue!="")
		{
	        
			if(trim(searchword) != "") 
			searchword += " "+aRel+" ";
            
			if(aField.indexOf("_3")==aField.length-2)
			{
			    aField=aField.substring(0,aField.length-2);
			    searchword += aField+"!=(";
			    searchword += filterKey(oValue,"or");
			}else if(aField.indexOf("_2")==aField.length-2)
			{
			    aField=aField.substring(0,aField.length-2);
			    searchword += aField+"=(";
			    searchword += filterKey(oValue);
			}else
			{
			    searchword += aField+"=(";
			    searchword += filterKey(oValue);
			}
			searchword += ")";
			
		}
	}
	
	return searchword;
}

//更新拼装子检索词
function upSearchwordOpr(aSearchword,aField,aRel,rRel,rField,aForm)
{
	var searchword=aSearchword;
	
	var oForm=aForm==null?"document.mainform":aForm;
	
	var obj;
	var oRel;
	var oRelValue = "and";
	
	oRel = eval(oForm+"."+(rRel==null?aRel:rRel));
	if ( oRel != null )
	{
	    oRelValue = trim(oRel.value);
	}
	
	obj= eval(oForm+"."+(rField==null?aField:rField));
	
	//obj=document.aspnetForm.allsearch;
	//alert(obj);
	
	if(obj!=null)
	{
		var oValue=trim(obj.value);
		
		if(oValue!="")
		{
	        
			if(trim(searchword) != "") 
			searchword += " "+oRelValue+" ";
            
			if(aField.indexOf("_3")==aField.length-2)
			{
			    aaField=aField.substring(0,aField.length-2);
			    //searchword += aField+"!=(";
			    searchword += aaField+"=(";
			    searchword += filterKey(oValue,"or");
			}else if(aField.indexOf("_2")==aField.length-2)
			{
			    aaField=aField.substring(0,aField.length-2);
			    searchword += aaField+"=(";
			    searchword += filterKey(oValue);
			}else
			{
			    searchword += aField+"=(";
			    searchword += filterKey(oValue);
			}
			searchword += ")";
			
		}
	}
	
//		if(aField.indexOf("_3")==aField.length-2)
//		{
//		    if ( searchword.lastIndexOf("( ") == searchword.length-2 )
//		        searchword = searchword.substring(0, searchword.length-2); 
//		    else
//		        searchword += " )";
//		}
	
	return searchword;
}

//更新拼装子检索词
function upSearchwordOpr1(aSearchword,aField,aRel,rRel_1,rRel_2)
{
	var searchword=aSearchword;
	
	var oForm = "document.mainform";
	
	var obj, obj_2, obj_3;
	var oRel;
	var oRelValue = "";
	var bFirst = true;
	
	obj   = eval(oForm+"."+aField);
	obj_2 = eval(oForm+"."+aField+"_2");
	obj_3 = eval(oForm+"."+aField+"_3");
	
	if ( (obj!=null && trim(obj.value)!="") || (obj_2!=null && trim(obj_2.value)!="") || (obj_3!=null && trim(obj_3.value)!=""))
	{
	    if(trim(searchword) != "")
	    {
			searchword += " "+aRel + " ";  
		}
			
		var oValue = "";		
		searchword += aField+"=(";
	    if ( obj != null && trim(obj.value)!="")
	    {
			searchword += filterKey(trim(obj.value));
			bFirst = false;
	    }
	    if ( obj_2 != null && trim(obj_2.value)!="")
	    {   
			oRel = eval(oForm+"."+rRel_1);
	        if ( oRel != null )
	        {
	            if ( !bFirst )
	                oRelValue = " " + trim(oRel.value) + " ";
			    searchword += oRelValue + filterKey(trim(obj_2.value));
			    bFirst = false;
	        }
	    }
	    if ( obj_3 != null && trim(obj_3.value)!="")
	    {   
			oRel = eval(oForm+"."+rRel_2);
	        if ( oRel != null )
	        {
	            if ( !bFirst )
	                oRelValue = " " + trim(oRel.value) + " ";
			    searchword += oRelValue + filterKey(trim(obj_3.value));
			    bFirst = false;
	        }
	    }
	    searchword += ")";
	    
	}
	
	return searchword;
}

//得到分类ID
function getClassid(aForm,identity)
{
    //alert("OK")
    var oForm=aForm==null?document.mainform:aForm;
    var result="";
    var test=false;
    //alert(form)
    for(i=0;i<oForm.length;i++)
    {
    			var oElement=oForm.elements[i];
    			
    			
    			if(identity!=null)
    			    test=oElement.type=='checkbox'&&oElement.name==identity;
    			 else
    			    test=oElement.type=='checkbox';   
    			
    			
    			if(test)
    			if(oElement.checked==true)
    			{
    			    result+=",";
    			    result+=oElement.value;
    			}
    			    
	}
	
	return result.substring(1,result.length);
	//alert(result);
}

//校验日期是否正确  "基本格式验证"
function checkDate(year,month,day)
{
	if(year!="" && month!="" && day!="")
	{
		switch(parseInt(month,10))
		{
			case 1 :
			case 3 :
			case 5 :
			case 7 :   
			case 8 :
			case 10 :
			case 12 : 
				if(parseInt(day,10)>31)return false;
			break;
			case 4 :
			case 6 :
			case 9 :
			case 11 : 
				if(parseInt(day,10)>30)return false;
				else return true;
			break;
			case 2 :  
				if((parseInt(year,10)%400)==0 || ((parseInt(year,10)%100)!=0 && (parseInt(year,10)%4)==0))
				{
					if(parseInt(day,10)>29)return false;
				}
				else
				{
					if(parseInt(day,10)>28)return false;
				}
				break;
			default:
				return false;
		}
	}
	//else return false;

	return true;
}


//设置日期 "快捷时间段"
function setDate()
{
		
		var nCount = parseInt(document.mainform.selectDate.value,10);

		if ( nCount==0 )
		{
			document.mainform.dc1.value = "";
			document.mainform.dc2.value = "";
			return true;
		}

		var aDay = new Date();
		var sDate = "";
		document.mainform.dc2.value = aDay.getYear() + "-" + (aDay.getMonth() + 1) + "-" + aDay.getDate();;
		
		if ( nCount!=30 )
		{
			var date  = aDay.getDate() - nCount + 1;
			aDay.setDate( date );
		} 
		else 
		{
			var month  = aDay.getMonth() - 1;
			aDay.setMonth( month );
		}

		sMonth = aDay.getMonth() + 1;
		if ( sMonth<10 ) sMonth = "0" + sMonth;
		sDay = aDay.getDate();
		if ( sDay<10 ) sDay = "0" + sDay;

		sDate = aDay.getYear() + "-" + sMonth + "-" + sDay;

		document.mainform.dc1.value = sDate;
}

//设置日期 "快捷时间段"
function setPubDate()
{
		var nCount = parseInt(document.mainform.selectPubDate.value,10);

		if ( nCount==0 )
		{
			document.mainform.pubdc1.value = "";
			document.mainform.pubdc2.value = "";
			return true;
		}

		var aDay = new Date();
		var sDate = "";
		document.mainform.pubdc2.value = aDay.getYear() + "-" + (aDay.getMonth() + 1) + "-" + aDay.getDate();;
		
		if ( nCount!=30 )
		{
			var date  = aDay.getDate() - nCount + 1;
			aDay.setDate( date );
		} 
		else 
		{
			var month  = aDay.getMonth() - 1;
			aDay.setMonth( month );
		}

		sMonth = aDay.getMonth() + 1;
		if ( sMonth<10 ) sMonth = "0" + sMonth;
		sDay = aDay.getDate();
		if ( sDay<10 ) sDay = "0" + sDay;

		sDate = aDay.getYear() + "-" + sMonth + "-" + sDay;

		document.mainform.pubdc1.value = sDate;
}

//进行时间检索时三种检索时间方式
function changeTime( nType )
{
switch (nType){
	case 1 :
		document.getElementById('time1').style.display = 'block';
		document.getElementById('time2').style.display = 'none';
		document.getElementById('time3').style.display = 'none';
		break;
	case 2 :
		document.getElementById('time1').style.display = 'none';
		document.getElementById('time2').style.display = 'block';
		document.getElementById('time3').style.display = 'none';
		break;
	case 3 :
		document.getElementById('time1').style.display = 'none';
		document.getElementById('time2').style.display = 'none';
		document.getElementById('time3').style.display = 'block';
		break;

	default:
		document.getElementById('time1').style.display = 'block';
		document.getElementById('time2').style.display = 'none';
		document.getElementById('time3').style.display = 'none';
		break;
}
//赋值默认当天时间
if (nType==3){
	setDate();
} else if(nType==1||nType==2) {
	document.mainform.dc1.value = "";
	document.mainform.dc2.value = "";
}

}

//进行时间检索时三种检索时间方式
function changePubTime( nType )
{
    switch (nType){
	case 1 :
		document.getElementById('PubTime1').style.display = 'block';
		document.getElementById('PubTime2').style.display = 'none';
		document.getElementById('PubTime3').style.display = 'none';
		break;
	case 2 :
		document.getElementById('PubTime1').style.display = 'none';
		document.getElementById('PubTime2').style.display = 'block';
		document.getElementById('PubTime3').style.display = 'none';
		break;
	case 3 :
		document.getElementById('PubTime1').style.display = 'none';
		document.getElementById('PubTime2').style.display = 'none';
		document.getElementById('PubTime3').style.display = 'block';
		break;

	default:
		document.getElementById('PubTime1').style.display = 'block';
		document.getElementById('PubTime2').style.display = 'none';
		document.getElementById('PubTime3').style.display = 'none';
		break;
    }
    //赋值默认当天时间
    if (nType==3){
	    setPubDate();
    } else if(nType==1||nType==2) {
	    document.mainform.pubdc1.value = "";
	    document.mainform.pubdc2.value = "";
    }
}



//组装综合查询检索词
function allSearchValue(form,text)
{
    var searchword="";
     searchword=upSearchword(searchword,"title","or",text,form);
     searchword=upSearchword(searchword,"doccontent","or",text,form);
     searchword=upSearchword(searchword,"keywords","or",text,form);
     //searchword=upSearchword(searchword,"people1","or",text,form);
     //searchword=upSearchword(searchword,"people2","or",text,form);
     searchword=upSearchword(searchword,"memo","or",text,form);
     
     searchword=upSearchword(searchword,"Sys_PicId","or",text,form);
     
     searchword=upSearchword(searchword,"ComeFromName","or",text,form);
     searchword=upSearchword(searchword,"ClassName","or",text,form);
	 searchword=upSearchword(searchword,"Author","or",text,form);
     
     return searchword;
}
//按ID检索
function SearchById(obj,num)
{
   var oValue=obj.form.gonumber.value;
   
   if(oValue=='') return false;
   
   
   if(num==0)
        top.location.href='DisplayUsage/Detail.aspx?id='+oValue;
    else if(num==1)
        top.location.href='DisplayUsage/Photos.aspx?id='+oValue;
    else   
        top.location.href='View_DetailPhoto.aspx?PicId='+oValue;
    
   
}
//按网站显示分类
function chgByType(obj)
{
    var sel = obj.selectedIndex;
    clearChecked(obj);
    revertDisplay(sel);
    
}

function revertDisplay(obj)
{
    if(obj==1)
    {
        document.getElementById("category0").style.display="block";
        document.getElementById("category1").style.display="none";
    }
    else if(obj==0)
    {
        document.getElementById("category0").style.display="none";
        document.getElementById("category1").style.display="none";
    }
    else
    {
        document.getElementById("category0").style.display="none";
        document.getElementById("category1").style.display="block";
    }
}

function clearChecked(obj)
{
    var form=obj.form;
    for(i=0;i<form.length;i++)
    {
        if(form.elements[i].type=="checkbox")
            form.elements[i].checked=false;
    }
}

function indexSearch(obj)
{
    var oForm= obj.form;
    var searchword="";
    var oldsearchword=oForm.allsearch.value;
    
        if(oForm!=null&&oForm.allsearch!=null)
        {      
            
            searchword=allSearchValue("document.aspnetForm","allsearch");
            

            var classids=getClassid(oForm,"cids");
            
                if(trim(classids)!="")
                {
                    if(trim(searchword)!="")
                        searchword="(("+searchword+") and classid="+classids+")";
                    else
                        searchword="(classid="+classids+")";
                }
//alert(searchword)
                if(trim(searchword)=="")
                    return ;
                if(isNaN(oldsearchword))
                {
                    searchword=searchword.replace("Sys_PicId","title");
                }
                    //alert(searchword);
                    top.location="TRSResults.aspx?searchword="+searchword;
        }
        
}

function upPeople(aSearchword)
{
    
    
    var searchword="";
    //searchword+="(";
    searchword=upSearchword(searchword,"people1","and","people");
	searchword=upSearchword(searchword,"people1_3","and","people");
	searchword=upSearchword(searchword,"people1_2","or","people");
	alert(searchword);
	alert(searchword);
	//searchword+=")";
	/*
	
	
	searchword=upSearchword(searchword,"people2","","people");
	searchword=upSearchword(searchword,"people2_3","and","people_3");
	searchword=upSearchword(searchword,"people2_2","or","people_2");
	
	*/
	
	
}

//add by liuliang @2006/03/31
function quickSearch(obj,locUrl)
{
    var oForm= obj.form;
    var searchword="";    
    var jumpUrl="";
    var oldsearchword=oForm.quick_sw.value;
   
        if(oForm!=null&&oForm.quick_sw!=null)
        {      
            
            var obj2=document.getElementById("Select2").value;
            var classid=(oForm.classid !=null )? (document.getElementById("classid").value) : (getClassid(oForm,"cids"));
            //alert(obj2+classid);
            //var classids=getClassid(oForm,"cids");
            
            if(obj2=="GroupId")
            {
                //locUrl="View_ClassPhoto.aspx";
                if(classid!=null )
                   locUrl +="?Id=" +classid;
               searchword = oForm.quick_sw.value;
               if(searchword=="")
                  return ; 
               else
                  jumpUrl = locUrl+"&Gid="+searchword;
            } 
            else
            {
               locUrl="TRSResults.aspx";
               
               if(obj2=="allsearch")
                {
                    if(trim(searchword) != "") 
			            searchword += " and ";
                    searchword += allSearchValue("document.aspnetForm","quick_sw");
                } 
                else
                { 
                    searchword=upSearchword(searchword,obj2,"and","quick_sw","document.aspnetForm");
                }
               //searchword=upSearchword(searchword,"ClassId","and","classid","document.aspnetForm");
               
               if(classid!=null && classid !="" )
               {
                   if(trim(searchword) != "")  
                        searchword = "(" + searchword + ")" + " and ";
                   searchword += "classid="+classid;
               }
               
               //searchword=upSearchword(searchword,obj2,"and","quick_sw","document.aspnetForm");
                              
               if(searchword=="")
                   return ;
              else
                 {
                  searchword="("+searchword+")";
                  jumpUrl = locUrl+"?searchword="+searchword;
                  }
            }
        }
       
    if(isNaN(oldsearchword))
    {
        jumpUrl=jumpUrl.replace("Sys_PicId","title");
    }	

//     if(oForm.DEBUG)
    // alert(searchword);
     
     location.href=jumpUrl;
}

function quickSearchSource(obj,locUrl)
{
    var oForm= obj.form;
    var searchword="";    
    var jumpUrl="";
   
        if(oForm!=null&&oForm.quick_sw!=null)
        {      
            
            var obj2=document.getElementById("Select2").value;
            var classid=(oForm.classid !=null )? (document.getElementById("classid").value) : (getClassid(oForm,"cids"));
            //alert(obj2+classid);
            //var classids=getClassid(oForm,"cids");
            
            if(obj2=="GroupId")
            {
                //locUrl="View_ClassPhoto.aspx";
                if(classid!=null )
                   locUrl +="?Id=" +classid;
               searchword = oForm.quick_sw.value;
               if(searchword=="")
                  return ; 
               else
                  jumpUrl = locUrl+"&Gid="+searchword;
            } 
            else
            {
               locUrl="TRSResults.aspx";
               
                
                if(obj2=="allsearch")
                {
                    if(trim(searchword) != "") 
			            searchword += " and ";
                    searchword += allSearchValue("document.aspnetForm","quick_sw");
                } 
                else
                { 
                    searchword=upSearchword(searchword,obj2,"and","quick_sw","document.aspnetForm");
                }
               //searchword=upSearchword(searchword,"ClassId","and","classid","document.aspnetForm");
               
               if(classid!=null && classid !="" )
               {
                   if(trim(searchword) != "")  
                        searchword = "(" + searchword + ")" + " and ";
                   searchword += "comefromid="+classid;
               }
               
               //alert( searchword ) 
               if(searchword=="")
                   return ;
              else
                 {
                  searchword="("+searchword+")";
                  jumpUrl = locUrl+"?searchword="+searchword;
                  }
            }
        }	

//     if(oForm.DEBUG)
    // alert(searchword);
     
     location.href=jumpUrl;
}
//使iframe的高度自适应
function SetIframeSize(iframeName) 
{ 
		var iframe = document.getElementById(iframeName); 
		try 
		{ 
				var bHeight = iframe.contentWindow.document.body.scrollHeight; 
				var dHeight = iframe.contentWindow.document.documentElement.scrollHeight; 
				//据说这两个高度可能不一样 
				var height = Math.max(bHeight, dHeight); 
				iframe.height = height; 
				iframe.contentWindow.document.getElementById("ctl00_ctLogo").style.width=823;
		}catch (ex){} 
}    

//自定义Trim方法
String.prototype.Trim = function() { 
    return this.replace(/(^\s*)|(\s*$)/g, ""); 
} 
