function SetCookie( name, value, expires, path){
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if( expires ){
		expires = expires * 1000 * 60 * 60 * 24;
	}
	
	var expires_date = new Date( today.getTime() + (expires) );
	var cookieStr = name + "=" +escape( value ) +
	(( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" );
	
	document.cookie = cookieStr;
}

function GetCookie(name){
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) &&	( name != document.cookie.substring( 0, name.length ) ) ){
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function DeleteCookie( name, path, domain ) {
	if ( GetCookie( name ) ) 
		document.cookie = name + "=" + ( ( path ) ? ";path=" + path : "") +( ( domain ) ? ";domain=" + domain : "" ) + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function getpage(start){
	    //checkall();
		document.form1.start.value = start;
		document.form1.submit();
}

function checkall(){
	var strTemp = "";
	var selectNone = 0;
	for(i = 0; i < form1.idParam.length; i++){
		if (form1.idParam[i].checked == true){
			strTemp = strTemp + form1.idParam[i].value + "@@";
			selectNone = 1;
		}
	}
	if(selectNone==0){
		for(i = 0; i < form1.idParam.length; i++){
			//if(form1.idParam[i].value.substring(0,1)!='0')break;
			strTemp = strTemp + form1.idParam[i].value + "@@";
		}
	}
	
	strTemp = "" + strTemp.substring(0, strTemp.length - 2);
	form1.strIndex.value = strTemp;
	
	if(document.form1.search.value=='')
		return false;
	return true;
}

function checkIt(flag){
	for(i = 0; i < form1.idParam.length; i++){
		form1.idParam[i].checked =flag;
	}
	var aaa;
	for(i=0;i<3;i++){
		if(document.getElementById("Flag" + i)&&document.getElementById("S" + i)){
			aaa = document.getElementById("S" + i);
			bbb = document.getElementById("Flag" + i);
			aaa.style.height = "0";
	    	bbb.innerHTML = "<img src=images/M.gif>";
	    	form1.openadv.value = "1";
    	}
	}
	
	checkall();
}
function checkIt2(flag){
	for(i = 0; i < form1.schIndexId.length; i++){
		form1.schIndexId[i].checked =flag;
	}
}

function sourceQuery(indexStr){
	checkIt(false);
	document.form1.indexShow.value='block';
	for (i = 0; i < form1.idParam.length; i++){
		if(form1.idParam[i].value==indexStr){
			form1.idParam[i].checked=true;
		}else{
			form1.idParam[i].checked =false;
		}
	}
	checkall();
	form1.submit();
}

function focusIndex(indexStr){
	for (i = 0; i < form1.idParam.length; i++){
		if(form1.idParam[i].value==indexStr){
			form1.idParam[i].focus();
			break;
		}
	}
}
function change(strID){
	var objDiv = document.getElementById("S" + strID);
    	var objA = document.getElementById("Flag" + strID);
    	if (objDiv.style.height != "1px"){
    		objDiv.style.height = "1px";
    		objA.innerHTML = "<img src=images/P.gif>";
    		form1.openadv.value = "0";
    	}else{
    		objDiv.style.height = "0";
    		objA.innerHTML = "<img src=images/M.gif>";
    		form1.openadv.value = "1";
    	}
}

function keyWordClick(keyword){
	document.form1.search.value=keyword;
	document.form1.submit();
}

function addKeywordOnSubmit(){
	//called by applet, do nothing
}

function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  while(s.indexOf("  ")>0){
	  s = s.replace("  "," ");
  }
  return s;
}

function singleClick(selectFromApp){
	var str;
	str = document.form1.search.value;
	str = str.replace("(","");
	str = str.replace(")","");
	document.form1.search.value = "("+str +","+ selectFromApp+ ")";
	document.form1.submit();
}

function doubleClick(selectFromApp){
	document.form1.search.value = document.form1.tempSave.value + ";" + selectFromApp;
	document.form1.submit();
}

function tempSave(selectFromApp){
	document.form1.tempSave.value = document.form1.search.value;
}


function cutNode(node){
	var i;
	i = node.split("(fixed)");
	var queryNow = document.form1.search.value.toUpperCase();

	var x;
	if(queryNow.indexOf(i[0])>0){
		x = queryNow.split(";"+i[0]);
	}else{
		x = queryNow.split(i[0]+";");
	}
	//alert(queryNow)
	var y="";
	for(i = 0;i<x.length;i++){
		y = y + x[i];
	}
	//alert("query now become--"+y)
	document.form1.search.value = y;
	document.form1.submit();
}

function handleEnter(field, event){	
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if(keyCode==13){
		var i;
		for(i = 0; i < field.form.elements.length; i++)
				if (field == field.form.elements[i])
					break;
			i = (i + 1) % field.form.elements.length;
			field.form.elements[i].focus();
			return false;
	}else
		return true;
}      

function toggleAdvance(yORn){
	if(document.form1.relatedMode.value=="1"){
		alert("sorry, advance option has not been implemented yet!");
		return false;
	}else{
		document.form1.toggleAdvance.value=yORn;
		document.form1.submit();
	}
}
function showOrder(yORn){
	document.form1.showOrder.value=yORn;
}

function debugCall(a){
	alert(a);
}

function expand(id){
   var max = 150;
   if(id.indexOf("mapBlock")>0){
   	max = Math.round(document.form1.mapHeight.value/15)*15;
   }
   var x = document.getElementById(id).style.height;
   if(x!=max+'px'){
	   document.getElementById(id).style.height=parseInt(x)+15;
	   timerID = setTimeout("expand('" +id+ "')",1);
   }
}

function shrink(id){
   var x = document.getElementById(id).style.height;
   var m = id.substring(1);
   var t;
   if(parseInt(x)>1){
   	   t = parseInt(x)-15;
   	   if(t<=0)t=1;	
	   document.getElementById(id).style.height=t;
	   timerID2 = setTimeout("shrink('" +id+ "')",1);
   }else{
   	document.getElementById(m).style.display="none";
   }
}

function showSubMenu(o){
	var a = document.getElementById(o).style.display;
	if(a=="none"){
		var sub = document.getElementById("_" + o).style;
		sub.height=0;
		document.getElementById(o).style.display="block";
		expand("_"+o);
		
		if(o=="indexBlock"){
			document.form1.indexShow.value = "block";
		}else if(o=="dtree"){
			document.form1.relatedShow.value = "block";
		}else if(o=="hotBlock"){
			document.form1.hotShow.value = "block";
		}else if(o=="mapBlock"){
			document.form1.mapShow.value = "block";			
		}else if(o=="timeBarBlock"){
			document.form1.timeShow.value = "block";
		}else if(o=="journalBlock"){
			document.form1.journalShow.value = "block";
		}else if(o=="advanceBlock"){
			document.form1.advanceShow.value = "block";
		}else if(o=="filterBlock"){
			document.form1.filterShow.value = "block";
		}else if(o=="modeBlock"){
			document.form1.modeShow.value = "block";
		}
	}else{
		shrink("_"+o);
		//document.getElementById(o).style.display="none";
		if(o=="advanceBlock"){
			document.form1.advanceShow.value = "none";
		}else if(o=="indexBlock"){
			document.form1.indexShow.value = "none";
		}else if(o=="dtree"){
			document.form1.relatedShow.value = "none";
		}else if(o=="hotBlock"){
			document.form1.hotShow.value = "none";
		}else if(o=="mapBlock"){
			document.form1.mapShow.value = "none";
		}else if(o=="timeBarBlock"){
			document.form1.timeShow.value = "none";
		}else if(o=="journalBlock"){
			document.form1.journalShow.value = "none";
		}else if(o=="filterBlock"){
			document.form1.filterShow.value = "none";
		}else if(o=="modeBlock"){
			document.form1.modeShow.value = "none";
		}
	}
}

function shownewtext(o) { 
    var thislink = o + "_text";
    newlink=document.getElementById(thislink);     
    if (document.getElementById(o).style.display=="none"){
		newlink.childNodes[0].nodeValue="＋";
	}else{
		newlink.childNodes[0].nodeValue="－";
	}
}

function showSubMenu2(o){
	var a = document.getElementById(o).style.display;
	if(a=="none"){
		document.getElementById(o).style.display="block";
		if(o=="indexBlock"){
			document.form1.indexShow.value = "block";			
		}else if(o=="dtree"){
			document.form1.relatedShow.value = "block";
		}else if(o=="hotBlock"){
			document.form1.hotShow.value = "block";
		}else if(o=="mapBlock"){
			document.form1.mapShow.value = "block";			
		}else if(o=="timeBarBlock"){
			document.form1.timeShow.value = "block";
		}else if(o=="journalBlock"){
			document.form1.journalShow.value = "block";
		}else if(o=="advanceBlock"){
			document.form1.advanceShow.value = "block";
		}else if(o=="filterBlock"){
			document.form1.filterShow.value = "block";
		}else if(o=="modeBlock"){
			document.form1.modeShow.value = "block";
		}
	}else{
		document.getElementById(o).style.display="none";
		if(o=="advanceBlock"){
			document.form1.advanceShow.value = "none";
		}else if(o=="indexBlock"){
			document.form1.indexShow.value = "none";
			shownewtext(o, "indexBlock_text");	
		}else if(o=="dtree"){
			document.form1.relatedShow.value = "none";
		}else if(o=="hotBlock"){
			document.form1.hotShow.value = "none";
		}else if(o=="mapBlock"){
			document.form1.mapShow.value = "none";
		}else if(o=="timeBarBlock"){
			document.form1.timeShow.value = "none";
		}else if(o=="journalBlock"){
			document.form1.journalShow.value = "none";
		}else if(o=="filterBlock"){
			document.form1.filterShow.value = "none";
		}else if(o=="modeBlock"){
			document.form1.modeShow.value = "none";
		}
	}
	shownewtext(o);
}

function timeSwitch(a){
	document.form1.timeInterval.value = a;
	document.form1.submit();
}

function expandingWindow(target,param,param2,scroll){
	var windowprops;
	if(scroll==1){
		windowprops='width=100,height=100,scrollbars=yes,status=no,resizable=yes';
	}else{
		windowprops='width=100,height=100,scrollbars=no,status=no,resizable=no';
	}
	var speed = 70; // scrolling speed, the larger the faster
	var leftdist = screen.availWidth/2;
	var topdist = screen.availHeight/2;

	if(window.resizeTo&&navigator.userAgent.indexOf("Opera")==-1){
		var sizer = window.open("","","left=" + leftdist + ",top=" + topdist +","+ windowprops);
		for (x = 0; x < param; x += speed){
			sizer.moveTo(leftdist-x/10,topdist-(x/param2)/10);
			sizer.resizeTo(x/5, x/param2/5);
		}
		
		sizer.location = target;
	}
	else
		window.open(target,'login');
}

function newSearch(a,b){
	document.form1.search.value=a;
	document.form1.expandKeyword.value="1";
	document.form1.focusKeyword.value=b;
  	
	document.form1.submit();
}

function refreshNodeTitle(n){
	var href = document.location.href;
	var current = href.substring(href.indexOf("nodeNameIndex=")+14,href.indexOf("nodeNameIndex=")+16);
	document.location.href = document.location.href.replace("nodeNameIndex=" + current ,"nodeNameIndex="+n);
}

function showFullTitle(id,current,e,text){
	var a = document.getElementById(id);
	a.innerHTML="<font size=2 face=tahoma color=#cc0000>" + text +"</font>";
	a.style.pixelLeft=event.clientX+document.body.scrollLeft+10;
	a.style.pixelTop=event.clientY+document.body.scrollTop+10;
	a.style.visibility="visible";
}
	
function hideTitle(id){
   	document.getElementById(id).style.visibility="hidden";
}

function startPage(){
	try{
		d.openAll();
		document.getElementById('sd'+(parseInt(document.form1.focusKeyword.value)+5)).focus();
		document.getElementById('sd'+document.form1.focusKeyword.value).focus();
		focusIndex(document.form1.strIndex.value);
		scroll(0,0);
	}catch(e){}
	try{
		document.form1.search.focus();
	}catch(e){}
}

function showBigMap(){
	var url = "bigMap.jsp";
	var a = window.open(url,"","resizable=no,scrollbars=yes,height=670,width=800,top=10", false);
	a.focus();
}

function timeSearch(range){
	document.form1.rangeQuery.value=range;
	document.form1.submit();
}

function getRangeQuery(fName,value){
	document.form1.rangeQuery.value = '$'+fName + "=(" + value + ")";
	document.form1.submit();
}

function seletSingalSource(idx){
	for(i = 0; i < form1.idParam.length; i++){
		if (idx==i){
			form1.idParam[i].checked = true;
		}else{
			form1.idParam[i].checked = false;
		}
	}
}

function querySetting(){
	document.form1.action="../querySetting.jsp"; 
	document.form1.submit();
}

function openViewer(viewer, source, id){
	document.viewer.from.value="viewer/"+viewer;
	document.viewer.action= "viewer/"+viewer;
	document.viewer.source.value = source;
	document.viewer.id.value = id;
	document.viewer.target="_self";
	document.viewer.submit();	
}

function goModeSetting(from,isNSC){
	document.form1.action="querySetting.jsp?from=" + from + "&isNSC=" + isNSC;	
	document.form1.submit();
}

function clearValues(){
	document.form1.rangeQuery.value='';
	document.form1.expandKeyword.value='';
	document.form1.extraQuerySentence.value='';
	document.form1.extraRangeCondition.value='';
	document.form1.showQuerySentence.value='';
	document.form1.showRangeCondition.value='';
}

function clearFieldSettings(){
	document.form1.extraQuerySentence.value='';
	document.form1.extraRangeCondition.value='';
	fieldSettings.style.display='none';	
	document.form1.submit();
}