/**
*  Generic XMLHTTP Request functions (Ajax calls).
*  initRequest(); initializes the XMLHTTP request object and returns it.
*  Different methods for different browsers, implemented with try/catch statments.
*  This function is called from within the execRequest('url','functionName','method'); function.
*
*  execRequest('url','functionName','method'); is a generic function that instanciates a
*  XMLHTTP object, performs the request using the URL and method from passed in the function arguments,
*  calls a function (defined in the function arguments) on every statechange.
*
*  i.e. Function call: execRequest('example.html','retrieveRequestResponse','GET');
*       will create a new XMLHTTP request object, retrieve the URL 'example.html' via a HTTP GET command
*       and on every statechange of the XMLHTTP request object call the function 'retrieveRequestResponse'
*       which in turn descides what to do with the response data from the XMLHTTP request.
*/
var ajaxRequest;
var repositoryPath="";
var documentPath="";
//var resultSet="";
function initRequest() {
	try {
		//Set up a XMLHTTP Request for Firefox, Safari, Opera etc.
		ajaxRequest = new XMLHttpRequest();
	} catch (e) { //If it fails
		try {
			//Set up the XMLHTTP ActiveX object for Internet explorer
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				//Set up the XMLHTTP ActiveX object for Internet explorer
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				window.alert('Failed to create HTTP connection object');
				return false;
			}
		}
	}
	return ajaxRequest;
}
function execRequest(execUrl,execFunct,execMethod){
    /**
    *  execUrl =		The URL to make the request to. (i.e. http://www.example.com/example.html)
    *  execFunct =		The function wich to call on the onreadystatechange event of the XMLHTTP Object, if no
    *                		function name specified the function will display an error and return false.
    *  execMethod =  	Specifies the HTTP method to use (GET, POST, PUT, DELETE etc)
    */
    if (execFunct == null || execFunct == "") { alert('Error, no action function specified'); return false; }
    if (execMethod == null || execMethod == "") { execMethod = 'GET'; }
	ajaxRequest = initRequest();
	ajaxRequest.onreadystatechange = eval(execFunct);
	ajaxRequest.open(execMethod, execUrl, true);
	ajaxRequest.send(null);
}
//Example function for processing the XMLHTTP response data
function retrieveRequestResponseFTSearch() {
	if(ajaxRequest.readyState == 4)	{ //If ready state 4 (e.g. it has finished the request)
	/**
	*  This are all the ready states of an Ajax call
	*  0	The request is not initialized
    	*  1	The request has been set up
    	*  2	The request has been sent
    	*  3	The request is in process
    	*  4	The request is complete
	*/
		if(ajaxRequest.status == 200) { //If status is 200 (HTTP Status 200 = success)
			document.getElementById('search_resullts').innerHTML = ajaxRequest.responseText; 			//Write the response text to a specified HTML element
		}
	}  else {
if(document.getElementById('search_resullts'))document.getElementById('search_resullts').innerHTML="<img src=\"/home.nsf/loading_bar.gif\" alt=\"Loading\" title=\"Loading\" />";
}
}
function retrieveRequestResponseCS() {
	if(ajaxRequest.readyState == 4)	{ //If ready state 4 (e.g. it has finished the request)
	/**
	*  This are all the ready states of an Ajax call
	*  0	The request is not initialized
    	*  1	The request has been set up
    	*  2	The request has been sent
    	*  3	The request is in process
    	*  4	The request is complete
	*/
		if(ajaxRequest.status == 200) { //If status is 200 (HTTP Status 200 = success)
			if (ajaxRequest.responseText.indexOf("No matching entr")>-1){
				noResultPlotter("<br>"+ajaxRequest.responseText);
			}else{
			resultSet = eval( "("+ajaxRequest.responseText+")" );
			resultPlotter();
			}
		}
	}  else {document.getElementById('result_list').innerHTML="<img src=\"/home.nsf/loading_bar.gif\" alt=\"Loading\" title=\"Loading\" />";}
}
function displayContactCard(ref) {
         var cc = document.getElementById("result_contact_card");
         var newHTML = "";
         cc.style.display="block";
         cc.style.height="100%"; //"" + window.document.height + "px";
          cc.style.width="100%"; //"" + window.document.width + "px";
          newHTML += "<div id=\"result_contact_card_pane\">&nbsp;</div>";
          newHTML += "<div id=\"result_contact_card_informationtable\">";
          newHTML += "<div id=\"result_contact_card_closer\" align=\"right\"><a href=\"javascript:closeContactCard();\">[X]</a><br>";
  newHTML += "<img src=\"http://www.exmi.se/etc/cmc09.nsf/repository/CMC extra liten/$file/CMCsmall.gif\"></div>";
          newHTML += "<table><tr><td valign=\"top\"><table border=\"0\" class=\"resulttable\">";
          newHTML += "<tr><td valign=\"top\" width=\"100\"><b>Name</b></td><td valign=\"top\" width=\"490\">" + resultSet.resultEntries[ref].reName + "</td></tr>";
         newHTML += "<tr><td valign=\"top\" width=\"100\"><b>Country</b></td><td valign=\"top\" width=\"150\">" + resultSet.resultEntries[ref].reCountry + "</td></tr>";
         newHTML += "<tr><td valign=\"top\" width=\"100\"><b>Area of expertice</b></td><td valign=\"top\" width=\"370\">" + resultSet.resultEntries[ref].reAreaOfExpertice + "</td></tr>";
if(resultSet.resultEntries[ref].reIndividualAreaOfExpertice!=""){
  newHTML += "<tr><td valign=\"top\" width=\"100\"><b>Personal expertice</b></td><td valign=\"top\" width=\"370\">" + resultSet.resultEntries[ref].reIndividualAreaOfExpertice + "</td></tr>";
}
         newHTML += "<tr><td valign=\"top\" width=\"100\"><b>Website</b></td><td valign=\"top\" width=\"150\">" + resultSet.resultEntries[ref].reWebsite+ "</td></tr>";
  newHTML += "<tr><td valign=\"top\" width=\"75\"><b>National institute</b></td><td valign=\"top\" width=\"150\">" + resultSet.resultEntries[ref].reWebsite2+ "</td></tr>";
         newHTML += "</table></td><td valign=\"top\" align=\"center\"></td></tr></table></div>";
         cc.innerHTML=newHTML;
}
function closeContactCard() {
      var cc = document.getElementById('result_contact_card');
      cc.style.display='none';
      cc.innerHTML='';
}
function noResultPlotter(txt){
    var listContainer = document.getElementById("result_list");
    listContainer.innerHTML = txt;
}
function resultPlotter(pageNumber) {
         var listContainer = document.getElementById("result_list"); //get container to put search results in
         var newHTML=""; //Initiate the html string
         if(pageNumber == null) {
                       pageNumber = 1;
         }
         //**
         //* calculate the number of entries to display/pages to display:
         //* pageStart = (pageNumber * 10) - 10 | i.e. pageNumber=1 > (1*10)-10 = 0, pageNumber=2 > (2*10)-10 = 10
         //* pageEnd = (pageNumber * 10) | i.e. pageNumber=1 > 1*10 = 10, pagenumber=2 > 2*20 = 20
         //* if the pageEnd variable is larger then the maximum numbers of entries, set it to the maximum
         //* i.e. pageEnd < total number of entries
         //**
         var pageStart = (pageNumber * 10) - 10;  //calculate pageStart (start entry in the array)
         var pageEnd = (pageNumber * 10);         //calculate pageEnd (end entry in the array)
         if(pageEnd > resultSet.resultEntries.length) {
                   pageEnd = resultSet.resultEntries.length;
         }
         //plot the result list header:
         newHTML+="<div id=\"result_header\">Displaying results <b>" + (pageStart+1) + "</b> to <b>" + pageEnd + "</b> of totally <b>" + resultSet.resultEntries.length + "</b> number of hits</div>";
	newHTML += "<table border=0 class=\"searchresulttable\">";
	newHTML += "<tr><td width=\"150\"><b>Name</b></td><td width=\"140\"><b>Country</b></td><td width=\"295\"><b>Area of expertise</b></td></tr>";
         //plot the result list
         for(i=pageStart;i<pageEnd;i++) {
	trclass="trgray";
	if (i % 2==0) trclass="trwhite"; 
newHTML += "<tr class=\""+trclass+"\"><td><a href=\"javascript:displayContactCard(" + i + ");\">" + resultSet.resultEntries[i].reName + "</a></td><td>"+ resultSet.resultEntries[i].reCountry + "</td>";
if(resultSet.resultEntries[i].reIndividualAreaOfExpertice!=""){
	newHTML += "<td onmouseout=\"return hideTooltip(event)\" onmousemove=\"return displayToolTip(event,'tt_div','"+resultSet.resultEntries[i].reIndividualAreaOfExpertice+"', 5, -1,'#000000')\">" +resultSet.resultEntries[i].reAreaOfExpertice+"</td></tr>";
}else{
	newHTML +=  "<td>"+resultSet.resultEntries[i].reAreaOfExpertice+"</td></tr>";
}
         }
         //plot the reult navigator
	newHTML += "</table>";
         newHTML+="<div id=\"result_navigator_label\">Pages:</div><div id=\"result_navigator\">";
         var pages=(resultSet.resultEntries.length / 10); //devide the total number of entries with ten
         pages = Math.ceil(pages); //Round it UP to the nearest Integer
         for(i=0;i<pages;i++) {
               if(pageNumber == (i+1)) {
                   newHTML+= "\[<b>" + (i+1) + "</b>\]";
               } else {
                   newHTML+= "\[<a href=\"javascript:resultPlotter(" + (i+1) + ");\">" + (i+1) + "</a>\]";
               }
         }
         newHTML+="</div>";
         //write the HTML code to the page
         listContainer.innerHTML = newHTML;
}
function openHomepage() {
	window.location.href="/home.nsf/pages/index.html";
}
function redirect() {
	if(window.location.href.indexOf(".nsf/pages/") == -1)   {
		openHomepage();
	} else {
		return true;
	}
}
//Used for Quicksearch form
function resetQSField(field) {field.value = "";field.style.color="rgb(0,0,0)";}
function doDominoSearch(id) {
	
}
/**
*  getQueryString(var); is a generic function to get a specifick URL parameter,
*  just pass in the name of the name of the parameter you want to get
*   and the function will retrieve the value of the parameter.
*
*  i.e. URL: www.example.com/example.html&id=123
*       Function call: getQueryString('id'); returns '123'
*/
function getQueryString(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	} 
	return false;
}
function doConsultantSearch() {
	var selInp1 =  document.getElementById("countrySelect");
	var qcountry = escape(selInp1.options[selInp1.selectedIndex].value);
	var selInp2 =  document.getElementById("areaSelect");
	var qaoe =  escape(selInp2.options[selInp2.selectedIndex].value);
	var cname =  document.getElementById("cname").value;
	var newURL = "/home.nsf/pages/consultantsearch.html?open&aoe=" + qaoe + "&country=" +qcountry+"&cname=" +cname;
	window.location=newURL;
}
//Print script
function doPrint(url) {
	var printWin = window.open('','print','height=600,width=630,location=no,menubar=no,resizable=no,scrollbars=yes,toolbar=no,top=30,left=30');
	var html = document.getElementById('body_content').innerHTML;
	var footer = document.getElementById('footer').innerHTML;
        printWin.document.write('<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">');
	printWin.document.write('<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\"><head>');
	printWin.document.write('<link rel=\"stylesheet\" href=\"../styles/mwcms.css\" type=\"text/css\" />');
	printWin.document.write('<style type=\"text/css\"> #footer .divider {width:300px;}</style>');
	printWin.document.write('<script type=\"text/javascript\" src=\"../scripts/mwcms.js\"></script>');
	printWin.document.write('<title>Print</title></head><body>');
	printWin.document.write('<div style=\"width:610px;\">');
	printWin.document.write('<div id=\"header\" style=\"width:610px;\">');
	printWin.document.write('<div id=\"header_links\" style=\"width:610px;float:right;;\">');
	printWin.document.write('<a href=\"javascript:window.print();\">Print</a>');
	printWin.document.write('</div>');
	printWin.document.write('</div>');
	printWin.document.write('<div id=\"body_content\">');
	printWin.document.write(html); //Original site_content_body contents
	printWin.document.write('</div>');
	printWin.document.write('</div>');
	printWin.document.write('</body></html>');
	//printWin.location.reload();	
}
// Used in the navigation
function navigationHighLight (onoff, elmnt ) {
	// if ( onoff == "on" ) {elmnt.style.background = "rgb(216,210,158)";}
	// if ( onoff == "off" ) {elmnt.style.background = "";}
	window.status = elmnt.firstChild.href;
	elmnt.firstChild.onclick = function() {return false;} 	//Disable onclick event of links, the onclick of the div opens
						             	//the link href, se function navigationGotoLink(elmnt)		
}
function navigationOpenLink(url) {window.location.href = url;}
function navigationGotoLink(elmnt) {
	var link = elmnt.firstChild;
	if(link.target == "" || link.target == null) {
		navigationOpenLink(link);
	} else {
		window.open(link);
	}
}
function decodeString(origemail){
	var j=0;
	var result="";
	for(j=0;j<origemail.length;j++){
		result=result+String.fromCharCode(origemail.charCodeAt(j)-3);
	}
	return result;
}
//MouseEvent gather same relevant data for multiple browswers
function MouseEvent(e)
{
  if(e) {
    this.e = e;
  }else{
    this.e = window.event;
  }
  
  if(e.pageX) {
    this.x = e.pageX;
  }else{
    this.x = e.clientX;
  }
  
  if(e.pageY) {
    this.y = e.pageY;
  }else{
    this.y = e.clientY;
  }
  
  if(e.target) {
    this.target = e.target;
  }else{
    this.target = e.srcElement;
  }
}
//Get the element that was affected(moused over)
//Get the mouse's x/y coords
function displayToolTip(e,div,msg,xpos,ypos,color)
{
  var e = new MouseEvent(e);
  
  var x = e.x + xpos;
  var y = e.y - ypos;
  
  //place the tootip div nearby where the mouse entered the element
  //and replace the text with the appropriate message
  //then make it visible
  theDiv = document.getElementById(div);
  theDiv.style.left = x + 'px';
  theDiv.style.top = y +'px';
  theDiv.style.color = color;
  theDiv.innerHTML ="Personal expertise:<br>"+ msg;
  theDiv.style.visibility = "visible";
  
  return false;
}
//hide the tooltip when the mouse leaves the element
//and reset the div's text
function hideTooltip(e)
{
  var e = new MouseEvent(e);
  
  theDiv = document.getElementById("tt_div");
  theDiv.style.visibility = "hidden";
  theDiv.innerHTML = '';
  
  return false;
}
function showInlineDisplayer(refurl, postop, posleft, refwidth, refheight) {
		 var cc = document.getElementById("InlineDisplayer");
		 if (!cc){
		 		 var _body=document.getElementsByTagName("body")[0];
		 		 cc=document.createElement("div");
		 		 cc.setAttribute("id","InlineDisplayer");
		 		 _body.appendChild(cc);
		 }
         var newHTML = "";
         cc.style.display="block";
         cc.style.height="100%"; 
          cc.style.width="100%"; 
          newHTML += "<div id=\"InlineDisplayer_pane\" onclick=\"javascript:closeInlineDisplayer()\" style=\"z-index: 999; position: absolute; LEFT: "+posleft+"px; TOP: "+postop+"px;\">&nbsp;</div>";
          newHTML += "<div id=\"InlineDisplayer_informationtable\" style=\"position: absolute; LEFT: "+posleft+"px; TOP: "+postop+"px; HEIGHT: "+refheight+"px;\">";
          newHTML += "<div id=\"InlineDisplayer_closer\" align=\"right\"><a href=\"javascript:closeInlineDisplayer();\">[X]</a><br>";
  newHTML += "<table><tr><td valign=\"top\"><table border=\"0\" class=\"InlineDisplayer_resulttable\">";
          newHTML += "<tr><td valign=\"top\" width=\"150\">";
if (refurl.toLowerCase().indexOf(".gif")>-1 || refurl.toLowerCase().indexOf(".jpg")>-1 || refurl.toLowerCase().indexOf(".jpeg")>-1 || refurl.toLowerCase().indexOf(".png")>-1){
if(refurl.split(";").length>1){
newHTML +="<img src='" + refurl+ "'>";
}else{
newHTML +="<img src='" + refurl+ "'>";
}
}else{
newHTML +="<iframe src =\""+refurl+"\" width=\""+refwidth+"\" height=\""+refheight+"\" scrolling=\"auto\"></iframe>";
}
newHTML +="</td></tr>";
         newHTML += "</table></td><td valign=\"top\" align=\"center\"></td></tr></table></div>";
         cc.innerHTML=newHTML;
}
function closeInlineDisplayer() {
      var cc = document.getElementById('InlineDisplayer');
      cc.style.display='none';
      cc.innerHTML='';
}


