var docIDloc = window.location.href.indexOf("docID=")
var documentID = window.location.href.substr(docIDloc + 6, 32)
var editWindow;
var preview = "no"
var ADSLibURL = window.location.href.toLowerCase()

function getFileSubdirectory(fileType) {
	switch (fileType.toUpperCase()) {
		case "PDF": return "pdf"
		case "GIF": return "images"
		case "JPG": return "images"
		case "PNG": return "images"
		default: return "other"
	}
}

function selectFileAndClose(filename) {
	var fName = replaceSubstring(filename, " ","+")
	var dbPath = document.forms[0].dbPath.value
	var x = dbPath.lastIndexOf("/")
	var dbName = dbPath.substring(x+1,dbPath.length-4)
	var fileType = fName.substr(fName.lastIndexOf(".")+ 1)
	openerField.value ="/dcmsSites/" + dbName + "/" +
		getFileSubdirectory(fileType)+"/"+ fName 
	self.close();
}

function getLibraryGraphic( fieldName, dbPath) { 
// this function allows the selection of graphics 
// from the specified site
//	browser = 'nn'; 
	if (!dbPath) {
		var dbPath = window.document.forms[0].dbPath.value
	}
	abURL = dbPath +"/frmImageSelector?OpenForm" ;
	filenameField = fieldName;
	opts = 'width=680,height=500,resizable=yes,scrollbars=yes'; 
	popupWin= addWindow(junctionName() + abURL, 'ImageSelector', opts)
	popupWin.focus()
}

function setVisibilityIE(command, layername) {
	var x = document.getElementById(layername)
	if (x != null) {
		x.style.visibility= (command=="show") ? "visible":"hidden"
	}
}

function checkEditWindow() {
	if (!WM_readCookie("openEditWindow")) {
	}
	else {
		setVisibilityIE("hide","ActionBox")
	}
}

function handleUnload() {
//	WM_killCookie("openEditWindow")
	if (editWindow) {
		if (preview=="no") {
			editWindow.close();
		}
	}
}


// This next little bit of code tests whether the user accepts cookies.
var WM_acceptsCookies = false;
if(document.cookie == '') {
	document.cookie = 'WM_acceptsCookies=yes'; // Try to set a cookie.
	if(document.cookie.indexOf('WM_acceptsCookies=yes') != -1) {
		WM_acceptsCookies = true; 
	}// If it succeeds, set variable
} else { // there was already a cookie
	WM_acceptsCookies = true;
}

function WM_setCookie (name, value, hours, path, domain, secure) {
	if (WM_acceptsCookies) { // Don't waste your time if the browser doesn't accept cookies.
		var not_NN2 = (navigator && navigator.appName 
			&& (navigator.appName == 'Netscape') 
			&& navigator.appVersion 
			&& (parseInt(navigator.appVersion) == 2))?false:true;
		if(hours && not_NN2) { // NN2 cannot handle Dates, so skip this part
			if ( (typeof(hours) == 'string') && Date.parse(hours) ) { // already a Date string
				var numHours = hours;
			} else if (typeof(hours) == 'number') { // calculate Date from number of hours
				var numHours = (new Date((new Date()).getTime() + hours*3600000)).toGMTString();
			}
		}

	document.cookie = name + '=' + escape(value) + ((numHours)?(';expires=' + numHours):'') + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'') + ((secure && (secure == true))?'; secure':''); // Set the cookie, adding any parameters that were specified.
	}

}

//added so that we can maintain the cookie back and forth between the 
//databases and servlets
function FAP_setCookie (name, value, hours, path, domain, secure) {
	if (WM_acceptsCookies) { // Don't waste your time if the browser doesn't accept cookies.
		var not_NN2 = (navigator && navigator.appName 
			&& (navigator.appName == 'Netscape') 
			&& navigator.appVersion 
			&& (parseInt(navigator.appVersion) == 2))?false:true;
		if(hours && not_NN2) { // NN2 cannot handle Dates, so skip this part
			if ( (typeof(hours) == 'string') && Date.parse(hours) ) { // already a Date string
				var numHours = hours;
			} else if (typeof(hours) == 'number') { // calculate Date from number of hours
				var numHours = (new Date((new Date()).getTime() + hours*3600000)).toGMTString();
			}
		}
	path = "/";
	document.cookie = name + '=' + escape(value) + ((numHours)?(';expires=' + numHours):'') + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'') + ((secure && (secure == true))?'; secure':''); // Set the cookie, adding any parameters that were specified.
	}
}
function WM_readCookie(name) {
	if(document.cookie == '') { // there's no cookie, so go no further
		return false; 
	} else { // there is a cookie
		var firstChar, lastChar;
		var theBigCookie = document.cookie;
		firstChar = theBigCookie.indexOf(name);	// find the start of 'name'
		var NN2Hack = firstChar + name.length;
		if((firstChar != -1) && (theBigCookie.charAt(NN2Hack) == '=')) { // if you found the cookie
			firstChar += name.length + 1; // skip 'name' and '='
			lastChar = theBigCookie.indexOf(';', firstChar); // Find the end of the value string (i.e. the next ';').
			if(lastChar == -1) lastChar = theBigCookie.length;
			return unescape(theBigCookie.substring(firstChar, lastChar));
		} else { // If there was no cookie of that name, return false.
			return false;
		}
	}	
}

function WM_killCookie(name, path, domain) {
	var theValue = WM_readCookie(name); // We need the value to kill the cookie
	if(theValue) {
		document.cookie = name + '=' + theValue + '; expires=Fri, 13-Apr-1970 00:00:00 GMT' + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:''); // set an already-expired cookie
	}
} 

function selectParent(key, label) {
	window.opener.document.forms[0].ParentLabelTX.value = label
	window.opener.document.forms[0].ParentRecordkeyTX.value = key
	window.close()
}

function launchWhatsNew(title, desc) {
	var x = window.location.href.indexOf("&cmd=WhatsNew")
	sourceURL = window.location.href.substring(0,x)
	sourceSite = window.location.href.substring(x+13 )
	WhatsNewTitle = title     //window.document.forms[1].dPageTitleTX.value
	WhatsNewDesc = desc  //window.document.forms[1].dDescriptionTX.value
	launchApplication("/webserv/WhatsNew.nsf/frmWhatsNewItemPopUp?OpenForm&Site="+sourceSite)
}

function addressBox( fieldName, fL, mV ) { 
	browser = 'nn'; 
	if ((ADSLibURL.indexOf("dominodev")>-1 ) ||
		(ADSLibURL.indexOf("dominouat")>-1 ) ||
		(ADSLibURL.indexOf("portal02")>-1 ) ) {
		abURL = "/webserv/webdev.nsf/NABLookupCN?OpenForm&" + fL
	}
	else {
		abURL = "/dcms/dcmsutil.nsf/frmDALookupCN?OpenForm&" + fL
	}
//	abURL = dbPath + '/frmNABLookupCN?OpenForm&' + fL;
	addressField = fieldName; 
	multiValue = mV; 
	wheight = ( browser == "nn") ? "250" : "200"; 
	wwidth = ( browser == "nn") ? "620" : "550"; 
	opts = 'width=' + wwidth + ',height=' + wheight + ',resizable=yes'; 
	popupWin=window.open( abURL,'abWindow', opts); 
	popupWin.focus()
}
function convertFromCommonName(input) {    	
	var i = 0 ;	
	i = input.lastIndexOf(" ") ;
	lastname = input.substr(i+1) ;
	firstname = input.substring(0,i); 	
	if (lastname == "Jr") { 	
		i = firstname.lastIndexOf(" "); 	
		lastname = input.substr(i+1) 	;    
		firstname = input.substring(0,i) 
	}
	return lastname + " , " + firstname 
}
function convertToCommonName(input) {  	
	var i = 0;
	i = input.indexOf(' ,');
	if (i>0) {
		lastname = input.substring(0,i);
		firstname = input.substr(i+3); 
		return firstname + " " + lastname 
	}
	return input
}

function getWebsealJunction() {
	if(location.href.indexOf("localhost") > -1) {
		return "";
	}
	if (location.href.indexOf("https://lb04") > -1) {
		return "/eudhp";
	} 
	
	if (location.href.indexOf("desktopdev") > -1) {
		return "/eudhpdev";
	}
	
	if (location.href.indexOf("desktoptest") > -1) {
		return "/eudhptest";
	}
	if (location.href.indexOf("desktopuat") > -1) {
		return "/eudhpuat";
	}
	if (location.href.indexOf("desktopcert") > -1) {
		return "/eudhpcert";
	}
	if (location.href.indexOf("https://desktop.")  > -1){
		return "/eudhpprod";
	}
	if (location.href.indexOf("https://agentdesktop.") > -1) {
		return "/eudhpprod";
	}
}

function getSearchURL(){
	var searchURL;
	if(location.href.indexOf("dev.") > -1 || location.href.indexOf("localhost") > -1){
		searchURL = "https://lb04.firemansfund.com/wasdev05u/Autonomy/SearchResults.jsp";
	}
	else if(location.href.indexOf("test.") > -1){
		searchURL = "https://lb03.firemansfund.com/wastest05u/Autonomy/SearchResults.jsp";
	}
	else if(location.href.indexOf("uat.") > -1){
		searchURL = "https://lbuat.firemansfund.com/jct05u/Autonomy/SearchResults.jsp";
	}
	else if(location.href.indexOf("cert.") > -1){
		searchURL = "https://lb02.firemansfund.com/jct05u/Autonomy/SearchResults.jsp";
	}
	else{
		searchURL = "https://lb01.firemansfund.com/jct05u/Autonomy/SearchResults.jsp";
	}
	
	document.write("<FORM METHOD=\"POST\" NAME=\"query_form1\" action=\"" + searchURL + "\">");
}

function openFAPHelp(url){
	addWindow(getWebsealJunction() + url, "helpWin", "height=450, width=505, scrollbars=yes");
	
}
function openFAPFeedBack(){
	addWindow(getWebsealJunction() + "/webserv/sharedfeedback.nsf/FB?openform&WG=phonebook&FBTitle=phone+book", "FeedbackWin", "height=600, width=800, scrollbars=yes");
	
}
function clearSearchFields(){
document.forms[0].firstname.value = "";
document.forms[0].lastname.value = "";
document.forms[0].location.value = "";
document.forms[0].department.value = "";
}

function HideContent(d) {
	if(d.length < 1) { return; }
	document.getElementById(d).style.display = "none";
}
function ShowContent(d) {
	if(d.length < 1) { return; }
	document.getElementById(d).style.display = "block";
}
function ReverseContentDisplay(d) {
	if(d.length < 1) { return; }
	if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = "block"; }
else { document.getElementById(d).style.display = "none"; }
}

function writeMultipleGroups(IDList, nameList){
var nameListArray = nameList.split(",");
var IDListArray = IDList.split(",");

	for(i = 0; i < nameListArray.length; i++){
	document.write("<a href=\"/servlet/OrgChart?docID=" + IDListArray[i] + "\">" + nameListArray[i] + "</a><br>");
	}
}

function copyrightYears(){
	var d = new Date();
var curr_year = d.getFullYear();
document.write("2002 - " + curr_year);
}

