var xmlHttpReq;
var xmlHttpReq2;
var url;
var status= false;
var searchType="";
var server="";
var help= true;
var seealso= false;

function init()
{
	if (window.XMLHttpRequest) 
	{
		xmlHttpReq = new XMLHttpRequest();
		status= true;
		
	} else if(window.ActiveXObject) {
       	try {
        	xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
        	status= true;
      	} catch(e) {
        	try {
          		xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
          		status= true;
        	} catch(e2) {
        		status= false;
        		}
		}
    }
    	if (!xmlHttpReq && typeof xmlHttpReq != 'undefined') {
    	  xmlHttpReq = new XMLHttpRequest();
    	  status= true;
    	}
 }
 
function init2()
{
	if (window.XMLHttpRequest) 
	{
		xmlHttpReq2 = new XMLHttpRequest();		
	} else if(window.ActiveXObject) {
       	try {
        	xmlHttpReq2 = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		xmlHttpReq2 = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e2) {}
		}
    }
    	if (!xmlHttpReq2 && typeof xmlHttpReq2 != 'undefined') {
    	  xmlHttpReq2 = new XMLHttpRequest();
    	}
}
	
function check(srchtype, servername)
{
	document.getElementById("results").style.display="none";
	if(srchtype=="Findall")
	{
			document.getElementById("results").style.display="block";
			document.getElementById("texthere").innerHTML= "<div align='center'>Help is not available with this search type.</div>";			
			help= false;

	}else{
		help=true;
		searchType= srchtype;
		server= servername.toLowerCase();
		init();	
	}
}

function processSearch(text, url)
{
	if(help)
	{
		text= text.replace( /[\.,;!#\$\/:\?'\(\)\[\]_\-\\]/g, '');
		if(xmlHttpReq!=null)
			xmlHttpReq.abort();
			
		if(text.length>2)
		{
				init();
				xmlHttpReq.open("GET",url+"?source=SEARCH&schtype="+searchType+"&words="+text.toUpperCase(),true);
				xmlHttpReq.onreadystatechange = processResults;
				xmlHttpReq.send(null);
		}else{
				document.getElementById("results").style.display="none";
		}
	}
}

function processResults() 
{ 	
	// only if req shows "loaded" 
	var countIn=0;
	var countOut=0;
	var table="<table width='820' style='text-decoration:underline' border='0' align='center'><TR>";
	/*<tr>  <td width='203'> </td>";
	table+="<td width='202'>&nbsp;</td> <td width='201'>&nbsp;</td> <td width='194'>&nbsp;</td></tr><tr>";  	
	*/
	if (xmlHttpReq.readyState == 4) 
	{
	 	// only if "OK" 
	 	if (xmlHttpReq.status == 200) 
	 	{ 	 		
			var temp= xmlHttpReq.responseText.split("|");
			var builder="";
			
			for(i=0;i<temp.length;i++)
			{
				table+="<td>";	
				countIn++;
				table+= buildLinks(temp[i], searchType);
				table+="</td>";	
				if(countIn==4){
					table+="</tr>";
					countIn=0;
				}
			}			
			if(temp.length<=1)
				builder="<div align='center'>If you don't see any matching word. Press search button.</div>";
			table+= builder;
			
			if(countIn<4)
				table+="</tr></table>";	
			else
				table+="</table>";	
			
			document.getElementById("results").style.display="block";
			document.getElementById("texthere").innerHTML= table;			
			
	 	} /*else {
	 		 alert("There was a problem retrieving the data:\n"); 
	 		 }*/
	 }
}

function buildLinks(linktext)
{
	var result="";
	var temp= linktext.split("#");
	var w=document.getElementById("wrd").value;
	var gmd='';
	var gmdcode="";
	if(document.mainmenu.gmd!=null && document.mainmenu.gmd.checked)
		gmd='on';
	else
		gmd='';
	if(document.mainmenu.gmdcode!=null)
		gmdcode=document.mainmenu.gmdcode.value;
	
	var sid=document.mainmenu.sid.value;
	var mfsearch;
	if(document.mainmenu.as_search_mf!=null)
	{
		if(document.getElementById('mf').checked)
			mfsearch='true';
		else
			mfsearch="false";
	}else
		mfsearch="false";

	if(searchType=="Subject" && seealso)
	{
		var colr="";	var rnum="";
		if(document.mainmenu.colr!=null)
		{	
			colr= document.mainmenu.colr.value; 
			colr=colr.substring(1,colr.length);
		}	
		if(document.mainmenu.num!=null)
			rnum= document.mainmenu.num.value;

		result= unescape("<a href=DoSearch.jsp?pNum=0&sort=title&choice="+searchType+"&w="+w+"&scode="+temp[1]+"&schStr="+replace(temp[0]," ","+")+"&gmd="+gmd+"&gmdcode="+gmdcode+"&sid="+sid+"&as_search_mf="+mfsearch+"&colr="+colr+"&num="+rnum+">"+temp[0]+"</a>");
	}else
		result= unescape("<a href=ViewItem.jsp?pNum=0&sort=title&choice="+searchType+"&w="+w+"&scode="+temp[1]+"&schStr="+replace(temp[0]," ","+")+"&gmd="+gmd+"&gmdcode="+gmdcode+"&sid="+sid+"&as_search_mf="+mfsearch+">"+temp[0]+"</a>");
	
	return result;
}

function replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

function callProcessFile(url)
{
	var uploadOption;
	var sync='syncmf=true&';
	
	if(xmlHttpReq==null)
		init();
				
	if(document.getElementById('clo').value=="Back")
		document.getElementById('clo').value="Cancel";
		
	if(document.getElementById("jan"))
	{
		if(document.getElementById("jan").checked)
			uploadOption= document.getElementById("jan").value;
		else
			uploadOption= "off";
	}else
		uploadOption= "off";
	
	if(document.getElementById('updatemf'))
	{
		if(document.getElementById('updatemf').checked)
			url= url+sync;		
	}	
	
	xmlHttpReq.open("GET",url+"source=UPLOAD&opt="+uploadOption,true);
	xmlHttpReq.onreadystatechange = processFile;
	xmlHttpReq.send(null);				
	InitializeTimer(url);
}

function callProcessFileCounter(url)
{
	/*if(xmlHttpReq2==null)
		init2();		
	else{
		xmlHttpReq2.abort();
		init2()		;
	}*/				
	
	init2();
	xmlHttpReq2.open("POST",url+"source=UPLOADSTATUS",true);
	xmlHttpReq2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');	
	xmlHttpReq2.onreadystatechange = processFileCounter;
	xmlHttpReq2.send(null);
}

function processFileCounter()
{
	var temp="";
	var temparr;
	
	if (xmlHttpReq2.readyState == 4) 
	{
		/* only if "OK" */
	 	if (xmlHttpReq2.status == 200) 
	 	{ 	 		
			temp = xmlHttpReq2.responseText;
			temparr= temp.split(">"); 

			bytesRead= temparr[0];
			totRecordsProcessed= temparr[1];
			insertCount= temparr[2];
			updateCount= temparr[3];
			errorCount= temparr[4];
						
			document.getElementById("counter").innerHTML= totRecordsProcessed;
			document.getElementById("upd").innerHTML= updateCount;
			document.getElementById("ins").innerHTML= insertCount; //(bytesRead/fileSize)*100
			document.getElementById("err").innerHTML= errorCount;
			//alert(temp+">>"+count);
			incrCount();
		}
	}
}

function processFile()
{ 	
	// only if req shows "loaded" 
	if (xmlHttpReq.readyState == 4) 
	{		
	 	// only if "OK" 
	 	if (xmlHttpReq.status == 200) 
	 	{ 	 		
			var temp= xmlHttpReq.responseText;
		}
	}	
}

var timerID = null
var timerRunning = true
var delay= 1000
var count=0

function InitializeTimer(url)
{
	init2();
    timerRunning = true;
    StartTheTimer(url);
	document.getElementById("sum").style.display="block";
}

function StartTheTimer(url)
{	
    if(timerRunning)
    {
    	count= count+1;
		callProcessFileCounter(url);
		//document.getElementById("counter").innerHTML= processFileCounter();
        //timerRunning = true
        
        timerID = window.setTimeout("StartTheTimer('"+url+"')", delay);
    }
}

function hideSummery()
{
	if(document.getElementById("sum")!=null)
	{
		document.getElementById("sum").style.display="none";
	}	
}

function cancelProcessing(url)
{
	if(document.getElementById('clo').value=="Close")
	{	
		window.close();	
		return;
	}else if(document.getElementById('clo').value=="Back"){
	
		history.back();
		return;
	}
	if(timerRunning)
        clearTimeout(timerID);
        
    timerRunning = false	
    stopServerToo(url);
}

function stopServerToo(url)
{
	init2();
	xmlHttpReq2.open("POST",url+"source=CANCELPROCESS",true);
	xmlHttpReq2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');	
	xmlHttpReq2.onreadystatechange = processFileCounter;
	xmlHttpReq2.send(null);
}

function stopTimer()
{
	if(timerRunning)
        clearTimeout(timerID);
        
    timerRunning = false;	   
}
