/* Returns the URL path ending with the database name, i.e. removes
all view, form etc. references. */
function getWebPath() {

	//get the URL without any parameters...
	originalWebPath = window.location.pathname;
	
	//if the original path refers to the pages or previewpages view, remove the view reference
	//and anything after it. If it doesn't (i.e. the current URL refers to a form or a view, not a page),
	//just remove the last entry in the path...
	
	if ( originalWebPath.indexOf( "/previewPages" ) != -1 ) {
		webPath = originalWebPath.substr( 0, originalWebPath.indexOf( "/previewPages" ) );
	} else if ( originalWebPath.indexOf( "/pages" ) != -1 ) {
		webPath = originalWebPath.substr( 0, originalWebPath.indexOf( "/pages" ) );
	} else if ( originalWebPath.indexOf( "/Pages" ) != -1 ) {
		webPath = originalWebPath.substr( 0, originalWebPath.indexOf( "/Pages" ) );
	} else if ( originalWebPath.indexOf( "/PAGES" ) != -1 ) {
		webPath = originalWebPath.substr( 0, originalWebPath.indexOf( "/PAGES" ) );
	} else {
		webPath = originalWebPath.substr( 0, originalWebPath.lastIndexOf( "/" ) );
	}
	//alert( "DEBUG: " + webPath );
	return webPath;
	
}


/* When the user clicks enter in the quick search field, submits the search */
function searchOnEnter(e) {
	
	//alert (e.target);
	if (e.target == document.forms[0].search) {
		//var keyChar = String.fromCharCode( e.which );
		//alert(keyChar);
		//alert(e.which);
		if ( e.which == 13) {
			commonSearch();
			return false;
		}
	} else if (e.target == document.forms[1].search2) {
		//var keyChar = String.fromCharCode( e.which );
		//alert(keyChar);
		//alert(e.which);
		if ( e.which == 13) {
			extendedSearch();
			return false;
		}
	}
	
}

function searchOnEnterIE() {
	
	//alert( 'Search on enter IE hakkas pihta..' );
	//alert( new String( window.event.keyCode ) );
	var ev = window.event;
	if ( ev.keyCode == 13 ) {
		//alert( "Enter oli..." );
		commonSearch();
		return false;
	}
	
	return true;
	
}

/* Returns the style of the current area to highlight it */
function highlightCurrentArea( areaName ) {

	//alert( "Valdkonna highlightimine algas..." );
	//alert( "Etteantud valdkond on " + areaName );
	
	//if the current area name is similar to the given
	//area name, return the highlighting style name.  
	//Otherwize return the non-highlighting style name..
	if ( document.forms[0].CurrentAreaName ) {
		if ( document.forms[0].CurrentAreaName.value == areaName ) {
			return "whitemenu";
		} else {
			return "menu";
		}
	} else {
		return "menu";
	}

}


function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

/* Opens a page specified in the given dropdown's selected option. */
function go(there) {
   if (there.selectedIndex >= 0) {
   	newUrl = there.options[there.selectedIndex].value;
   	if (newUrl.length>0) {
        window.location.href = newUrl;
      }
   }
}

//Performs the extended search...
function extendedSearch() {
	//alert( 'Extended search läks...' );
	
	//get the extended search form and all the parameters that are specified in that form...
	var frm = document.forms[1];
	queryWords = frm.search2.value;

	//if the query is empty, no point to continue.  Give a laguage-specific
	//error message...
	if (queryWords == "") {
		alert( document.forms[0].term1.value );
		return;
	}
	
	//get the search page code...
	searchPage = document.forms[0].SearchResultsPage.value;

	searchQuery = queryWords.replace( / /g, " AND " );
	
	//compose the search URL...
	searchURL = searchPage + "?OpenDocument&Query=" + escape(searchQuery) + "&Start=1&Count=10&SearchMax=1000&Format=long";
	//alert( "Search URL = " + searchURL );

	//and open the search URL...
	window.location.href = searchURL;
	//return false;
	
}

/* Get the number of selected radiobutton in poll. */
function getSelectedOption() {

	//get the radiobuttons objects...
	var gallupOptions = document.forms[1].poll;
	
	//check if at least one is checked...
	for ( var i = 0; i < gallupOptions.length; i++ ) {
		if ( gallupOptions[i].checked ) {
			return i + 1;
		}
	}
	return null;
}

// ------------------------------------------------------------ DWMX Swap Image onMouseOver
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_showHideLayers() { //v3.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }
}

function hideAllSubnavItems() {
    layers = new Array('bottom', 'claims', 'ergo', 'business', 'private');
    for(i=0; i<layers.length; i++) {
        MM_showHideLayers(layers[i],'','hide');
    }
}

function showHomesubnavPrivate() {
    hideAllSubnavItems();
    MM_showHideLayers('bottom','','show');
    MM_showHideLayers('private','','show');
}

function showHomesubnavBusiness() {
    hideAllSubnavItems();
    MM_showHideLayers('bottom','','show');
    MM_showHideLayers('business','','show');
}

function showHomesubnavClaims() {
    hideAllSubnavItems();
    MM_showHideLayers('bottom','','show');
    MM_showHideLayers('claims','','show');
}

function showHomesubnavERGO() {
    hideAllSubnavItems();
    MM_showHideLayers('bottom','','show');
    MM_showHideLayers('ergo','','show');
}

/* leiab url-i pealt ühe query väärtuse */
function urlquery(queryString)
	{
	var query = location.search.substring(1); var pairs = query.split("&");
	for(var i = 0; i < pairs.length; i++) {
		var pos = pairs[i].indexOf('='); if (pos == -1) continue;
		var argname = pairs[i].substring(0,pos);var value = pairs[i].substring(pos+1);
		if(argname == queryString) return value;}
	return ''}

// ------------------------------------------------------------ Open Browser Window
function anfahrt() 
{window.open("anfahrt.htm","Anfahrt","width=510,height=550,scrollbars=yes,resizable=no,menubar=yes,screenX=0,screenY=0,xpos=0,ypos=0");}

function navhelp(page1)
{window.open(page1,'NaviHelp','width=480,height=320,screenX=0,screenY=0,left=0,top=0,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,');}

function recomm(page2) 
{window.open(page2,'SendPage','width=660,height=530,screenX=0,screenY=0,left=0,top=0,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,');}

function printp(page3) 
{window.open(page3+'?OpenDocument&print=1','PrintPage','width=660,height=530,screenX=0,screenY=0,left=0,top=0,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,');}

function aktie(page4) 
{window.open(page4,'AktieW','width=620,height=450,screenX=0,screenY=0,left=0,top=0,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,');}
