var w1 = null, is_nav = 0, is_nav3 = 0, is_nav4 = 0, is_nav4up = 0, is_ie  = 0;
var is_ie3 = 0, is_ie4 = 0, is_ie4up = 0, is_ie5 = 0, is_ie5up = 0;


function DetermineBrowserTypeVersion()
{
   var agt=navigator.userAgent.toLowerCase(); 
		   
   // Note: On IE5, these return 4, so use is_ie5up to detect IE5. 
   var is_major = parseInt(navigator.appVersion); 
   var is_minor = parseFloat(navigator.appVersion); 

   // Navigator Checks
   is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('compatible') == -1)); 
   if( is_nav )
   {
     is_nav3 = (is_nav && (is_major == 3)); 
     is_nav4 = (is_nav && (is_major == 4)); 
     is_nav4up = (is_nav && (is_major >= 4)); 
   }

   // IE Checks
   is_ie   = (agt.indexOf("msie") != -1); 				
   if( is_ie )
   {
     is_ie3  = (is_ie && (is_major < 4)); 
     is_ie4  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")==-1) ); 
     is_ie4up  = (is_ie  && (is_major >= 4)); 
     is_ie5  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")!=-1) ); 
     is_ie5up  = (is_ie  && !is_ie3 && !is_ie4); 	
   }
}

function CreateSatellite( theURL, name, width, height, center, options)
{
	DetermineBrowserTypeVersion()

	// Different Browsers have different ways to get the 
	// position and size of the browser window.
	if( is_nav4up && center )
	{
	 	var x = window.screenX;
	  	var y = window.screenY;
	  	var w = window.outerWidth;
	  	var h = window.outerHeight;
	  	var midx = x + (w - width)/2; 
	  	var midy = y + (h - height)/2; 	
		w1 = window.open( theURL, name, "left=" + midx + ",top=" + midy + ",height="+ height + ",width=" +  width + options );
	}else if( is_ie5up && center ) 
	{
		var x = window.screenLeft;
     	var y = window.screenTop;
		var w = document.body.clientWidth;
	 	var h = document.body.clientHeight;
		var midx = x + (w - width)/2; 
		var midy = y + (h - height)/2; 	

		// Next bit forces IE5 to give focus and display the window in the correct position
	  	if( w1 != null && !w1.closed ){w1.close(); w1 = null;}			    
	      
		w1 = window.open(theURL, name, "left=" + midx + ",top=" + midy + ",height="+ height + ",width=" +  width + options );
	}else{  
	    // Older browsers can't determine browser window pos/size and IE4 has probs
	    // closing and re-opening a window  	      
		w1 = window.open(theURL, name, "height="+ height + ",width=" +  width + options );
	}
	return w1
}
		


function getServer()
{
	var myurl = window.location.href;
	if (myurl.indexOf("klassiker.chadwyck.com") != -1) return "US";
	else return "UK";
}

function getLanguage()
{
	var myurl = window.location.href;
	if (myurl.indexOf("/english/") != -1) return "english";
	if (myurl.indexOf("/deutsch/") != -1) return "deutsch";
	return "";
}
	
function getArea()
{
	var myurl = window.location.href;
	if (myurl.indexOf("/all/") != -1) return "all";
	if (myurl.indexOf("/literary/") != -1) return "literary";
	if (myurl.indexOf("/nonlit/") != -1) return "nonlit";
	if (myurl.indexOf("/diaries/") != -1) return "diaries";
	if (myurl.indexOf("/letters/") != -1) return "letters";
	if (myurl.indexOf("/register/") != -1) return "register";
	return "";	
}	

function getApp()
{
	var myurl = window.location.href;
	if (myurl.indexOf("/search") != -1) return "search";
	if (myurl.indexOf("/fulltext") != -1) return "fulltext";
	if (myurl.indexOf("/htxview") != -1) return "htxview";
}	

function getID()
{
	var myurl = window.location.href;
	var id_index = myurl.indexOf("id=");
	if (id_index == -1)	// check for uppercase ID
		id_index = myurl.indexOf("ID=");
	
	if(id_index != -1)
	{
		var id = myurl.substring(id_index+3);
		var id_end = id.indexOf("&");
		if (id_end != -1) id = id.substring(0,id_end);
		return id;
	}
	else
	{
		return "";
	}
		
}

function getTocPage()
{
	var myurl = window.location.href;
	var toc_index = myurl.indexOf("content=tocs/toc_");
	if (toc_index != -1)
	{
		var toc = myurl.substring(toc_index + 17);
		toc_index = toc.indexOf(".htx");
		if (toc_index != -1)
		{
			toc = toc.substring(0,toc_index);
			return toc;
		}
		else
		{
			return "";
		}
		
	}
	else
	{
	return "";
	}	
}

function isDevOrTestPort()
{
	var myurl = window.location.href;
	if (myurl.indexOf("dkv.private.chadwyck.co.uk") != -1) return "dev";
	if (myurl.indexOf(":8086") != -1) return "test";
	return "live";
}


function createDurableURL()
{
		
	if (getLanguage() == "deutsch")
	{
		CreateSatellite("/deutsch/help/url_popup.htm","url_popup",660,480,1,'');
	}
	else
	{
		//alert(document.urlform.url.value);
		CreateSatellite("/english/help/url_popup.htm","url_popup",660,480,1,'');
	}
}

function write_durableurl_form()
{	
	var dev_or_test = isDevOrTestPort();
	
	if (dev_or_test == "dev")
		var url = "http://devlinklin2.umi.bhowell.com:8000/openurl?ctx_ver=Z39.88-2004&res_dat=xri:";	
	if (dev_or_test == "test")
		var url = "http://prelinklin1.umi.bhowell.com:8000/openurl?ctx_ver=Z39.88-2004&res_dat=xri:";
	if (dev_or_test == "live")
		var url = "http://gateway.proquest.com/openurl?ctx_ver=Z39.88-2004&res_dat=xri:";	
		
	var myServer = getServer();
	var myLang = getLanguage();
	var myArea = getArea();
	var myApp = getApp();
	
	if (myServer == "UK") url += "dkv";
	if (myServer == "US") url += "dkv-us";
	url += "&rft_dat=xri:dkv:";
	if (myLang == "deutsch") url += "deutsch:";
	if (myApp == "search")
	{
		url += "screen:";
		url += myArea;
	}
	else if (myApp == "fulltext")
	{
		url += "ft:";
		url += myArea;
		url += ":";
		url += getID();
	}
	else if (myApp == "htxview")
	{
	//	// either a ToC or Works by Author	
		var myurl = window.location.href;
		//alert(myurl);
		if (myurl.indexOf("works_by_author") == -1)  // Not a works by author page, must be a Toc
		{
			url += "toc:";
			url += myArea;
			url += ":";
			url += getTocPage();
		}
		else
		{
			url +=  "author:";
		}
			
	}
	
	var output = "<form name=\"urlform\"><input type=\"hidden\" name=\"url\" value=\"" + url + "\"></form>";
	
	document.write(output);
}

function get_url()
{
	document.urlform.url.value=opener.document.urlform.url.value;
	document.urlform.url.focus();
	document.urlform.url.select();
	
}

function opendatewindow(datetext)
{
	// Global for browser version branching.
	var Nav4 = ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) >= 4))
	var dialogWin = new Object();
	var docstart = "<html><head><title>Date Details</title></head>\r\n";
	var splitstring = datetext.split(":");

  	docstart += "<body> <p><p>";
  	docstart += splitstring[0];
  	docstart += ":<p>";
  	if (typeof(splitstring[1]) != "undefined")
  	{
  		docstart += splitstring[1];
  	}
  	docstart += "</body></html>";
      // Assemble window attributes and try to center the dialog.

      dialogWin.width = 200;
      dialogWin.height = 50;
      if (Nav4) {
         // Center on the main window.
         dialogWin.left = window.screenX + ((window.outerWidth - dialogWin.width) / 2)
         dialogWin.top = window.screenY + ((window.outerHeight - dialogWin.height) / 2)
         var attr = "screenX=" + dialogWin.left + ",screenY=" + dialogWin.top + ",resizable=no,width=" + dialogWin.width + ",height=" + dialogWin.height
      } else {
         // The best we can do is center in screen.
         dialogWin.left = (screen.width - dialogWin.width) / 2
         dialogWin.top = (screen.height - dialogWin.height) / 2
         var attr = "left=" + dialogWin.left + ",top=" + dialogWin.top + ",resizable=no,width=" + dialogWin.width + ",height=" + dialogWin.height
      }
      attr = attr + "scrollbars=0,resizable=1,status=no,toolbar=no,menubar=no,location=no";
  	var newWindow = window.open("", "dialog", attr);
 	newWindow.document.open();
  	newWindow.document.write(docstart);
  	newWindow.document.close();
  	newWindow.focus();
}


// writes a form contain hidden fields which hold the default values of all the search_form fields;

function write_defaults_form()
{
	//alert("Starting write_defaults_form()");
	document.write("<form name='defaultsform'>\n");
	
	for(var i=0; i<document.forms['SearchLitFrm'].length; i++)
	{
		var field_name = document.forms['SearchLitFrm'].elements[i].name;
		var field_value = document.forms['SearchLitFrm'].elements[i].value;
		var input_str = "<input type='hidden' name='" + field_name + "' value='" + field_value + "'>\n";
		document.write(input_str);
	}
	
	document.write("</form>\n");
	//alert("Finished write_defaults_form()");
}


function clear_search_form()
{
	//alert(window.location.href);
	
	window.location.href="search";
	
	
	//for(var i=0; i<document.forms['SearchLitFrm'].length; i++)
	//{
	//	document.forms['SearchLitFrm'].elements[i].value = document.forms['defaultsform'].elements[i].value;
	//}
}

// need to add tag exlusions to this!
//function clear_search_form()
//{
//	var exclusions="DK_DIA_SY DK_DIA_SD DK_DIA_EY DK_DIA_ED DK_LET_SY DK_LET_SD DK_LET_EY DK_LET_ED DK_LIT_SY DK_LIT_EY DK_NLIT_SY DK_NLIT_EY";
//	
//	//alert(document.forms['SearchLitFrm'].length);
//	for(var i=0; i<document.forms['SearchLitFrm'].length; i++)
//	{
//		//alert(document.forms['SearchLitFrm'].elements[i].name);
//		//alert(document.forms['SearchLitFrm'].elements[i].type);
//		if (document.forms['SearchLitFrm'].elements[i].type == "text" && 
//			exclusions.indexOf(document.forms['SearchLitFrm'].elements[i].name) == -1)
//			document.forms['SearchLitFrm'].elements[i].value="";
//			
//	}
//}