
//======================================================== function printFriendly() 

function printFriendly() {

	doc = window.location.href ;
	doc = getBaseURL(doc) + "#top" ;
	
	if (isMainWindow() == true) {
		alert ("This is the printable version") ;
	} else {
		window.open(doc) ;
	}
}

//======================================================== function addStyleAndButtons() 

function addStyleAndButtons() {
	addStyle() ;
	addButtons() ;
}

//======================================================== function addStyle() 

function addStyle() {

	newWin = '' ;
	
	newWin = newWin + '<style type="text/css" media="all">' ;
	newWin = newWin + '	#breadcrumb, #navigation, #main_navigation, #my_bma, #article_info { display: none; } ' ;	
	newWin = newWin + '	#header { display: block;  } ' ;
	newWin = newWin + '	#main_content { border-bottom: 0px ; } ' ;
	newWin = newWin + '</style>' ;
	
	newWin = newWin + '<style type="text/css" media="screen">' ;
	newWin = newWin + '	#bmaright, #footer { display: block; } ' ;
	newWin = newWin + '</style>' ;

	newWin = newWin + '<style type="text/css" media="print">' ;
	newWin = newWin + '	#main_content .main_content .article { width: 90%; } ' ;
	newWin = newWin + '	#bmaright, #footer { display: none; } ' ;
	newWin = newWin + '</style>' ;
	
	
	iframe = '' ;
	iframe = iframe + '<style type="text/css" media="all">' ;
	iframe = iframe + '	#header { display: none; } ' ;
	iframe = iframe + '	#bmaright { display: none; } ' ;
	iframe = iframe + '</style>' ;
	iframe = iframe + '' ;
	
	if (isMainWindow() == true) {
		document.write (newWin) ;
	} else {
//		document.write (iframe) ;
	}
}

//======================================================== function addButtons() 

function addButtons() {

	buttons = '' ;
	buttons = buttons + '<div id="bmaright"><br />' ;
	
	//buttons = buttons + '<div class="button1">';
	buttons = buttons + '<input 	type="button" 	class="button" 	onclick="window.print();" 	value="Print" >' ;
	//buttons = buttons + '</div><!-- close div class="button1"-->';
	
	buttons = buttons + '<p></p>'	;
	
	//buttons = buttons + '<div class="button1">';
	buttons = buttons + '<input 	type="button" 	class="button" 	onclick="window.close();" 	value="Close window" >' ;
	//buttons = buttons + '</div><!-- close div class="button1"-->';
	
	buttons = buttons + '<br /></div><!-- close div id="bmaright"-->' ;

	if (isMainWindow() == true) {
		document.write (buttons) ;
	} else {
		return ;
	}
}

//======================================================== function infoWindow(newURL) 

function infoWindow(newURL) {

	var features = "" ;
	features = features + "height=500, width=820" ;
	features = features + ", " + "location=no" ;
	features = features + ", " + "menubar=no" ;
	features = features + ", " + "resizable=no" ;
	features = features + ", " + "scrollbars=yes" ;
	features = features + ", " + "status=yes" ;
	features = features + ", " + "titlebar=no" ;
	features = features + ", " + "toolbar=no" ;
	features = features + ", " + "top=50" ;
	features = features + ", " + "left=100" ;
	
	newWindow = window.open(newURL, "myWindow", features) ;
	if (newWindow) 	newWindow.focus() ;

}


//======================================================== function is_IE7() 

function is_IE7() {

//	msg = "" ;
//	for (xx in navigator) {
//		msg = msg + "\n" + xx + " = " + eval("navigator."+xx) ;
//	}
//	
//	alert (msg) ;

	if (document.documentElement && typeof document.documentElement.style.maxHeight!="undefined" 
			&& navigator.appName.toUpperCase().indexOf("MICROSOFT")>-1 ) {
		
		//alert("IE7");
		return true ;
	} else {
		
		return false ;
	}
	
}


//======================================================== functions defPopup() and popup() 


function defPopup(newURL, defType) {
	//if (defType.toLowerCase() == "") { newWin = popup (newURL,"height", "width", "loc menu size scroll status title tool direct history") ; }

	if (defType.toLowerCase() == "basic") { newWin = popup (newURL,"700", "1024", "size scroll status") ; }
	
	if (defType.toLowerCase() == "600x600") { newWin = popup (newURL,"600", "600", "size scroll status") ; }

	if (defType.toLowerCase() == "tool") { newWin = popup (newURL,"700", "1024", "size scroll tool status") ; }

	if (defType.toLowerCase() == "default1") {	newWin = popup (newURL, "700", "1024", "title size status scroll tool") ; }

	if (defType.toLowerCase() == "all") {	newWin = popup (newURL, "700", "1024", "loc menu size scroll status title tool direct history") ; }

	if (defType.toLowerCase() == "all_full") {	newWin = popup (newURL, "1024", "1280", "loc menu size scroll status title tool direct history") ; }


	return newWin ;
}



function popup(newURL, height, width, feat) {

	var features = "" ;
	features = features + "height=" + height + ",width=" + width ;
	
//	alert (features) ;
	
	if (feat.indexOf("loc") > -1) { 	features = features + "," + "location=yes" ;
	} else {									features = features + "," + "location=no" ; }
	
	if (feat.indexOf("menu") > -1) {	features = features + "," + "menubar=yes" ;
	} else {									features = features + "," + "menubar=no" ;	}
	
	if (feat.indexOf("size") > -1) {	features = features + "," + "resizable=yes" ;
	} else {									features = features + "," + "resizable=no" ; }
	
	if (feat.indexOf("scroll") > -1) {	features = features + "," + "scrollbars=yes" ;
	} else {									features = features + "," + "scrollbars=no" ; }
	
	if (feat.indexOf("status") > -1) {features = features + "," + "status=yes" ;
	} else {									features = features + "," + "status=no" ; }
	
	if (feat.indexOf("title") > -1) {	features = features + "," + "titlebar=yes" ;
	} else {									features = features + "," + "titlebar=no" ; }
	
	//tool ---
	if (is_IE7() == true) {
		if (feat.indexOf("tool") > -1) {	features = features + "," + "location=yes" ;  	} 
	}

	if (feat.indexOf("tool") > -1) {	features = features + "," + "toolbar=yes" ;  
	} else {									features = features + "," + "toolbar=no" ;  }
	//tool ---


	if (feat.indexOf("direct") > -1) {	features = features + "," + "directories=yes" ;
	} else {									features = features + "," + "directories=no" ;	}
	
	if (feat.indexOf("history") > -1) {	features = features + "," + "copyhistory=yes" ;
	} else {										features = features + "," + "copyhistory=no" ;	}
	
	randomNum = new Date().getMilliseconds() ;
	
	randomX = Math.abs(1024 - width) * randomNum /1000 ;
	randomY = Math.abs(768 - height) * randomNum /1000 ;

	features = features + "," + "top="  + randomY + ",screenY=" + randomY ;
	features = features + "," + "left=" + randomX + ",screenX=" + randomX ;

//	alert (features) ;
//	window.status = features ;

	newWin = window.open(newURL, "myWindow"+randomNum, features) ; 
	if (newWin) 	newWin.focus() ;

	return newWin ;

}

//======================================================== function change_opener (newURL)

function change_opener (newURL) {

	if (window.opener != null) {

		
		try {
			if (window.opener.top !=null) {
				window.opener.top.location.href = newURL ;
				window.opener.top.focus();

			} else {
				window.opener.location.href = newURL ;
				window.opener.focus();
			}
		} catch (err) {
			defPopup (newURL, "all_full");
		}

	} else {
				
		defPopup (newURL, "all_full");

	}

}


//======================================================== function isMainWindow () 

function isMainWindow() {

	if (window == window.parent) {
		//alert ("they are the same") ;
		return true ;
	} else {
		//alert ("they are different") ;
		return false ;
	}

}

//======================================================== function isMainWindow2 () 

function isMainWindow2() {

	doc = window.location.href ;
	doc = getBaseURL(doc) ;
	
	par = window.parent.location.href ;
	par = getBaseURL(par) ;

	//alert ("doc = " + doc + "\npar = " + par) ;
	
	if (doc == par) {
		//alert ("they are the same") ;
		return true ;
	} else {
		//alert ("they are different") ;
		return false ;
	}
}

//======================================================== function getBaseURL (tempURL) 

function getBaseURL (tempURL) {

	var newURL = "" ;
	newURL = tempURL.replace("#toptop", "") ;
	newURL = newURL.replace("#top", "") ;

	//alert (newURL);

	return newURL ;
}

//======================================================== function returnMainURL (tempURL, anchor) 

function returnMainURL (tempURL, anchor) {
	
	var newURL = "" ;
	newURL = tempURL.replace("#"+anchor, "") ;
	newURL = newURL.replace(anchor, "") ;
	//alert (newURL);
	return newURL ;
}

//======================================================== function populate_opener (fieldsArr, valsArr) 

// Define the popType and feed in an ARRAY of values
function populate_opener (popType, valsArr) {

	if (popType == 1)
	{
		fieldsArr = ["University_Name", "University_NameDisp", "Campus", "CampusDisp", "Course", "CourseDisp"] ;
		newValsArr = [ valsArr[0], valsArr[0], valsArr[1], valsArr[1], valsArr[2], valsArr[2] ] ;
		populateArr_opener (fieldsArr, newValsArr);
	}

	if (popType == 2)
	{
		fieldsArr = ["QUniversity_Name", "QUniversity_NameDisp"] ;
		newValsArr = [ valsArr[0], valsArr[0] ] ;
		populateArr_opener (fieldsArr, newValsArr);
	}

	if (popType == 3)
	{
		fieldsArr = ["BranchOfPractice", "BranchOfPracticeDisp", "MainAppointmentCat", "MainAppointmentCatDisp", "MainAppointment"] ;
		newValsArr = [ valsArr[0], valsArr[0], valsArr[1], valsArr[1], valsArr[2] ] ;
		populateArr_opener (fieldsArr, newValsArr);
	}

	if (popType == 4)
	{
		fieldsArr = ["BranchOfPractice2", "BranchOfPracticeDisp2", "MainAppointmentCat2", "MainAppointmentCatDisp2", "MainAppointment2"] ;
		newValsArr = [ valsArr[0], valsArr[0], valsArr[1], valsArr[1], valsArr[2] ] ;
		populateArr_opener (fieldsArr, newValsArr);
	}
	
	if (popType == 5)
	{
		fieldsArr = ["BranchOfPractice3", "BranchOfPracticeDisp3", "MainAppointmentCat3", "MainAppointmentCatDisp3", "MainAppointment3"] ;
		newValsArr = [ valsArr[0], valsArr[0], valsArr[1], valsArr[1], valsArr[2] ] ;
		populateArr_opener (fieldsArr, newValsArr);
	}
	
	if (popType == 6)
	{
		fieldsArr = ["Employer", "EmployerDisp", "Employer_PostCode", "Employer_PostCodeDisp", "PrincipalWorkPlace", "PrincipalWorkPlaceDisp", "WorkPlace_PostCode", "WorkPlace_PostCodeDisp"] ;
		newValsArr = [ valsArr[0], valsArr[0], valsArr[1], valsArr[1], valsArr[2], valsArr[2], valsArr[3], valsArr[3] ] ;
		populateArr_opener (fieldsArr, newValsArr);
	}

}

//this function is generic - feed in an ARRAY of field names and an ARRAY of values
function populateArr_opener (fArr, vArr) {
	
	var thisform	= self.document.forms[0]
	var opener	= self.opener.document.forms[0]
	
	for (var x = 0 ; x < fArr.length ; x++ ) {
		
		lhs = ("opener." + fArr[x] + ".value ") ;
		rhs = "'" + vArr[x] + "'" ;
		final = lhs + " = " + rhs ;
		//alert (final) ;

		eval (final) ;
		
	}

}

//======================================================== function trim(value, remType)

function trim(value, remType) {
	
//if remType = 1	- Replaces whitespaces with a single space but deletes all tabs
// else				- Replaces both multiple whitespaces and tabs with a single space
	
	newText = value.replace(/(\n\r|\n|\r)/gm,"<1br />") ;
	
	if (remType = 1) {
		newText = newText.replace(/\t/g,"");
	} else {
		newText = newText.replace(/\t/g," ");
	}
	
	regexp1 = /\s+/g;
	newText = newText.replace(regexp1," ");
	
	regexp2 = /\<1br \/>/gi;
	newText = newText.replace(regexp2, "\n");
	
	// Removes leading whitespaces 
	regexp3 = /\s*((\S+\s*)*)/;
	newText = newText.replace(regexp3, "$1");
	
	// Removes ending whitespaces
	regexp4 = /((\s*\S+)*)\s*/;
	newText = newText.replace(regexp4, "$1");
	
	return newText ;
	
}	


//======================================================== Get and Set Cookies

function getCookieVal (offset) {
 var endstr = document.cookie.indexOf (";", offset);
 if (endstr == -1) { endstr = document.cookie.length; }
 return unescape(document.cookie.substring(offset, endstr));
 } ;

function GetCookie (name) {
 var arg = name + "=";
 var alen = arg.length;
 var clen = document.cookie.length;
 var i = 0;
 while (i < clen) {
  var j = i + alen;
  if (document.cookie.substring(i, j) == arg) {
   return getCookieVal (j);
   }
  i = document.cookie.indexOf(" ", i) + 1;
  if (i == 0) break; 
  }
 return null;
 } ;
 
function SetCookie (name,value,expires,path,domain,secure) {
path="/" ;
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
 }
 
//======================================================== NedStat Helper Code
//======================================================== NedStat Helper Code
//======================================================== NedStat Helper Code

function getDbPath() { //this gets the element inside the Notes Db - if value set by Notes DB, otherwise the URL

	//trackSubject should be set by the Notes db - it is the Form or View or Description, etc.
	var trackSubject2 = "" ;
	if (typeof(trackSubject) != "undefined") {
		trackSubject2 = trackSubject ;
	}

	//if it is not set by Notes db, then use the URL
	var dbPath = "" ;
	if (trackSubject2 == "") {
		dbPath = window.location.pathname + window.location.search ;
		if (dbPath.substr(0,1)=="/") {dbPath = dbPath.substr(1) }
		dbPath = dbPath.replace("&login","") ;  //remove &login
		dbPath = dbPath.replace("&Login","") ;  //remove &Login
		dbPath = dbPath.replace(/&seq=[0-9]?[0-9]/, "") ;  //remove &seq=x
		dbPath = dbPath.replace(/&Seq=[0-9]?[0-9]/, "") ;  //remove &Seq=x
		dbPath = dbPath.replace(/&/g, "_") ;  //replce "&" with "_", so as not to confuse NedStat
		dbPath = dbPath.replace(/%20/g, "") ; //remove %20 from URL
	} else {
		 dbPath = trackSubject2 ;
	}

	return dbPath ;

} //======


function getSiteAreaPath() { //this gets the Notes Db Title (if set by the Notes Db) and adds the dbPath from above

	var siteAreaPath2 = "" ;
	if (typeof(siteAreaPath) != "undefined") {
		siteAreaPath2 = siteAreaPath ;
	}

	var dbPath = "" ;
	dbPath = getDbPath() ;

	if (siteAreaPath2 != "") {
		var token = ".nsf/" ;
		var pos = dbPath.toLowerCase().indexOf(token) ;
		if (pos > -1) { dbPath = dbPath.substr( pos + token.length, dbPath.length ) ; }

		token = ".nsf" ;
		pos = dbPath.toLowerCase().indexOf(token) ;
 		if (pos > -1) { dbPath = dbPath.substr( pos + token.length, dbPath.length ) ; }

		 siteAreaPath2 = siteAreaPath2 + "." + dbPath ;
	} else {
		 dbPath = dbPath.replace(".nsf","_nsf");
		 siteAreaPath2 = dbPath ;
	}

	siteAreaPath2 = siteAreaPath2.replace(/\//g,".");  //replace "/" with "."

	return siteAreaPath2 ;

} //======


function getUserType() { //this gets the userType (variable set by the Notes Db) - can be Member, NonMmember, Press

	var userType2 = "Anonymous"; //can be Member, NonMmember
	if (typeof(userType) != "undefined") {
		if (userType != "") { userType2 = userType ; } ;
	}
	userType2 = "&UserType=" + userType2 ;
	return userType2 ;

} //======


function getUserCraft() { //this gets the userCraft (variable set by the Notes Db) - eg. General prationtioners or Consultants, with any spaces removed

	var userCraft2 = "" ;
	if (typeof(userCraft) != "undefined") {
		if (userCraft.toLowerCase() != "none"  && userCraft.toLowerCase() != "") { userCraft2 = "&craft=" + userCraft } 
	}
	return userCraft2 ;

} //======


function getOffice() { //this gets the user's National Office variable set by the Notes Db

	var userNational2 = "&office=none";
	if (typeof(userNational) != "undefined") {
		if (userNational != "") { userNational2 = "&office=" + userNational ; }
	} ;
	return userNational2 ;

} //======


function getLoggedin() { //this gets loggedin variable set by the Notes Db

	var loggedin2 = "";
	if (typeof(loggedin) != "undefined") {
		if (loggedin==1) { loggedin2 = "&Loggedin=yes"; } 
	} ;
	return loggedin2 ;

} //======


function getRecCode() { //this gets the recCode - relevant to Online Mebmership Apps form

	var recCode2 = "";
	if (typeof(recCode) != "undefined") {
		if (recCode!="") { recCode2 = "&RecCode="+recCode; } 
	} ;
	return recCode2 ;

} //======


function getDoTrack() { //this gets the doTrack (variable set by the Notes Db) - it must be set if stats tracking is required

	var doTrack2 = 0;
	if (typeof(doTrack) != "undefined") {
		if (doTrack==1) { doTrack2 = 1; } 
	} ;
	return doTrack2 ;

} //======


//======================================================== 
