function urlDecode(str) {
return(unescape(str));
}

function queryStringToArray(str) {
  var hash = new Array();
  if (str == null || str.length < 2) return hash;
  if (str.charAt(0) == '?') str = str.substring(1);
  var pairs = str.split("&");
  for (i=0; i < pairs.length; i++) {
    var pair = pairs[i].split("=");
    if (pair[0].length > 0) hash[urlDecode(pair[0])] = urlDecode(pair[1]);
  }
  return hash;
}

function getQueryField(str, key) {
  if (str.charAt(0) == '?') str = str.substring(1);
  var qtemp = str.split("&");
  for (i=0; i<qtemp.length; i++) {
    var hash = qtemp[i].split("=");
    if (hash[0] == key) return urlDecode(hash[1]);
  }
  return null;
}

function getQueryList(hash, identifierKey, identifierValue) {
  var idK = 0;
  var List = new Array();
  while(hash[identifierKey+""+idK]) {
    var value = (hash[identifierValue+""+idK]) ? escape(hash[identifierValue+""+idK]) : "";
    List[List.length] = hash[identifierKey+""+idK] + "=" + value;
    idK++;
  }
  return List;
}

// transforms the querystring from the url to applet parameter html tags
// call inside of applet tags that are document.written, too.
// used for "itemlist" & "treepath"

function fillAdditionalParams(str) {
	if (str == null || str == "") return;
  str = str.replace(/\?/,"");
  var qtemp = new Array();
  qtemp = str.split("&");
  var hash = new Array();
  var ret = "";
  for (i=0; i<qtemp.length; i++) {
    hash = qtemp[i].split("=");
    if (hash[0] != "") {
      // reawake potential querystring
      var value = hash[1];
      value = value.replace(/%3f/g,"?");
      value = value.replace(/%26/g,"&");
      value = value.replace(/%3d/g,"=");
      value = value.replace(/%2e/g,".");
      value = value.replace(/%3a/g,":");
      value = value.replace(/%2f/g,"/");
      if (i > 0) ret += "\n";
      ret += '<PARAM NAME="'+hash[0]+'" VALUE="'+value+'">';
    }
  }
  return ret;
}

// puts the defaultLanguage in front of the others
function getLanguagesCurrentFirst(dataOrGUI) {
  if(dataOrGUI == "GUI") { 
    var languages = availableGUILanguages.split(',');
    var useThisLanguage = false;
    for(i in languages) {
      if(currentLanguage == languages[i]) useThisLanguage = true;
    }
    var sorted = (useThisLanguage) ? currentLanguage : "";
  } else {
    var languages = availableDataLanguages.split(',');
    var useThisLanguage = false;
    for(i in languages) {
      if(currentDataLanguage == languages[i]) useThisLanguage = true;
    }    
    var sorted = (useThisLanguage) ? currentDataLanguage : "";
  }
	
	for (i = 0; i < languages.length; i++) {
    if(dataOrGUI == "GUI") {
		  if (languages[i] != currentLanguage) sorted += ',' + languages[i];
    } else {
      if (languages[i] != currentDataLanguage ) sorted += ',' + languages[i];
    }
	}
  if(sorted.charAt(0) == ",") sorted = sorted.substring(1, sorted.length);
	return sorted;
}

function getGUIFallbackLanguage() {
  var languages = availableGUILanguages.split(',');
  return languages[0];
}

function getDataFallbackLanguage() {
  var languages = availableDataLanguages.split(',');
  return languages[0];
}

function checkValue(value) {
	var undef;
  if(value == undef) return "";
  else return value; 
}

function belSearchForSingleIDInTree() { /* this function isn't any longer in use */
  alert("belSearchForSingleIDInTree()\ntool.js\n\nthis function isn't any longer in use");
  //belenusHome.document.getElementById('InvisibleFrame').setAttribute("src","search/search_for_single_id_in_tree.html");
	}

function belInitStandaloneSearch4(searchParams, searchTerm, startPoint) {
	if(belenusHome.interpartsVersion == "dynamic") {
		var searchRequestStr = "&resultid=1_2_3_5_7_11_13_17&restart=true&"+searchParams;
		searchRequestStr += "&jsfile=js/dyn_search.js&cssfile=custom/searchresult.css";
		searchRequestStr +="&parameter0="+searchTerm;
		searchRequestStr += "&tabCaption="+escape('shortTabCaption='+belenusHome.ld['nav_search']+' ( '+searchTerm+' )&longTabCaption='+belenusHome.ld['nav_search']+' ( '+searchTerm+' )');
		setTimeout("void(belenusHome.WM.openWindow('searchX','"+searchRequestStr+"',false));",2000);
	} else {
		var searchRequestStr = "&searchTerm="+searchTerm;
		searchRequestStr += "&searchField="+searchParams;
		searchRequestStr += "&tabCaption="+escape('shortTabCaption='+belenusHome.ld['nav_search']+' ( '+searchTerm+' )&longTabCaption='+belenusHome.ld['nav_search']+' ( '+searchTerm+' )');
		setTimeout("void(belenusHome.WM.openWindow('search','"+searchRequestStr+"',false));",2000);
	}
}

function searchForSingleIDInTree(id) {
	var searchRequestStr = "../server?&resultid=1_2_3_5&restart=true&"+search4SingleIDInTreeQuery;
	searchRequestStr += "&jsfile=js/tree_jump.js&cssfile=custom/searchresult.css";
	searchRequestStr += "&parameter0="+id;
	belenusHome.document.getElementById("InvisibleFrame").setAttribute("src",searchRequestStr);
}

function getPrintLabel(TreePath) {
  var str = "";
  if(TreePath == null) return str;
  var tmpCurrentTreePath = TreePath; //getProperty("Tree","CurrentTreePath",null);
  if(tmpCurrentTreePath.charAt(tmpCurrentTreePath.length-1) == "/") tmpCurrentTreePath = tmpCurrentTreePath.substring(0,tmpCurrentTreePath.length-2);
  for(var i = 0; i <= maxShownParentLabels; i++) {
    if(tmpCurrentTreePath == "") break;
    var tmp = getProperty("Global","ItemData","Field=Label&ItemPath="+tmpCurrentTreePath);
    if(tmp == null) break;
    else {
      tmp = belenusHome.getQueryField(tmp,"Label");
      if(tmp.length > maxLengthOfStr && i != 0) {
        tmp = tmp.substring(0, (maxLengthOfStr-shownDots));
        for(var j = 0; j < shownDots; j++) {
          tmp += ".";
        }
      }
      
      if(tmpCurrentTreePath.split("/").length == 2 && belenusHome.displayRoot == false) str +="";
      else if (i == 0) str = tmp; else str = tmp + "/" + str;
    }
    tmpCurrentTreePath = tmpCurrentTreePath.substring(0, tmpCurrentTreePath.lastIndexOf('/'));
  }
  return str;
}

function getLabelNav(TreePath) {
  if(TreePath == "") {
    return belenusHome.ld['defaultTitle'];
  }
  var str = "";
  var m = 1+belenusHome.maxShownParentLabels;
  var tmpCurrentTreePath = TreePath; //getProperty("Tree","CurrentTreePath",null);
  if(tmpCurrentTreePath.charAt(tmpCurrentTreePath.length-1) == "/") tmpCurrentTreePath = tmpCurrentTreePath.substring(0,tmpCurrentTreePath.length-2);
  var sizeOf = tmpCurrentTreePath.split("/");
  var startCut = null;
  if(sizeOf.length > m) {
    startCut = sizeOf.length-m;
  }
      

  for(var i = 0; i < sizeOf.length; i++) {
    var linkTitle = "";
    if(i >= startCut) {
      if(tmpCurrentTreePath == "" || tmpCurrentTreePath == "/") break;
      var tmp = getProperty("Global","ItemData","Field=Label&ItemPath="+tmpCurrentTreePath);
      if(tmp == null) break;
      else {
        tmp = belenusHome.getQueryField(tmp,"Label");
        linkTitle = tmp;
        //alert("LinkTitle:"+linkTitle+"\ntoken:"+i);
        if(tmp.length > maxLengthOfStr && i != startCut) {
          tmp = tmp.substring(0, (maxLengthOfStr-shownDots));
          for(var j = 0; j < shownDots; j++) {
            tmp += ".";
          }
        }
        
        if(tmpCurrentTreePath.split("/").length == 2 && belenusHome.displayRoot == false) str +="";
        else if (i == 0 || i == (startCut)) str = tmp;         	
        else str = '<a title="'+linkTitle+'" href="javascript:belenusHome.eventHandler(\'OpenLink\',\'URL=parts:'+tmpCurrentTreePath+'\')">' + tmp + '</a> / ' + str;
        /*
        if (i == 0 || i == (startCut)) str = tmp;         	
        else str = '<a title="'+linkTitle+'" href="javascript:belenusHome.eventHandler(\'OpenLink\',\'URL=parts:'+tmpCurrentTreePath+'\')">' + tmp + '</a> / ' + str;
        */        
      }
      tmpCurrentTreePath = tmpCurrentTreePath.substring(0, tmpCurrentTreePath.lastIndexOf('/'));
    }
  }
  return str;
}

/** tabmanager frame resizing function **/

function startCloseTabAnimationNow() {
  var mFSProp =  belenusHome.document.getElementById("MainFrameSet").getAttribute("rows");
  var a = mFSProp.split(",");
  mFSProp = "";
  for(b in a) {
    if(b < a.length-1) mFSProp += a[b]+","; 
  }
  belenusHome.document.getElementById("MainFrameSet").setAttribute("rows",mFSProp+"0");
}

var lastTabManagerFramesetProp = belenusHome.tabManagerCaptionFrameHeight+",*,"+belenusHome.tabManagerTabBarFrameHeight;
function startOpenTabAnimationNow(cu) {
  if(cu == undef) cu =1;
  var mFSProp =  belenusHome.document.getElementById("MainFrameSet").getAttribute("rows");
  var a = mFSProp.split(",");
  mFSProp = "";
  for(b in a) {
    if(b < a.length-1) mFSProp += a[b]+","; 
  }
  cu = parseInt(cu);
  if(cu >= belenusHome.tabManagerFrameHeight) belenusHome.document.getElementById("MainFrameSet").setAttribute("rows",mFSProp+belenusHome.tabManagerFrameHeight);
  else {
    cu = parseInt(cu*2);
    if(cu >= belenusHome.tabManagerFrameHeight) cu = belenusHome.tabManagerFrameHeight;
//    belenusHome.document.getElementById("MainFrameSet").setAttribute("rows",mFSProp+cu);
//    setTimeout("startOpenTabAnimation("+cu+")",25);
    belenusHome.document.getElementById("MainFrameSet").setAttribute("rows",mFSProp+belenusHome.tabManagerFrameHeight);
  }  
  belenusHome.TabMainFrame.document.getElementById("tabManagerFrameSet").setAttribute("rows", belenusHome.lastTabManagerFramesetProp);
}

function minimizeTab() {
	if(!belenusHome.useTabManager) return;
	var mFSProp = belenusHome.document.getElementById("MainFrameSet").getAttribute("rows"); //get main frameset properties
	mFSProp = mFSProp.split(",");
	mFSProp.reverse();
			belenusHome.tabManagerFrameHeight = mFSProp[0]; // set last frame height
	mFSProp.shift();
	mFSProp.reverse();
	mFSProp = mFSProp.join(",")+","+eval(parseInt(belenusHome.tabManagerTabBarFrameHeight)+2);
			belenusHome.lastTabManagerFramesetProp = belenusHome.TabMainFrame.document.getElementById("tabManagerFrameSet").getAttribute("rows"); 
	belenusHome.TabMainFrame.document.getElementById("tabManagerFrameSet").setAttribute("rows","0,0,"+lastTabManagerFramesetProp.split(",")[0]);
	belenusHome.document.getElementById("MainFrameSet").setAttribute("rows",mFSProp);
}

var orgFrameSize =null;
function resizeMainFramesetForBasketConfirmation(switcher) {
  if(orgFrameSize == null) belenusHome.document.getElementById("MainFrameSet").getAttribute("rows");
  var mFSProp =  belenusHome.document.getElementById("MainFrameSet").getAttribute("rows");
  var a = mFSProp.split(",");
  mFSProp = "";
  for(b in a) {
    if(b < a.length-1) mFSProp += a[b]+","; 
  }
  var nullSized=a[0]+",";
  for(var c = 1; c < a.length-1;c++)  nullSized += "0,";
  alert(nullSized);
  if(switcher == "off")  belenusHome.document.getElementById("MainFrameSet").setAttribute("rows",orgFrameSize);
  else belenusHome.document.getElementById("MainFrameSet").setAttribute("rows",nullSized+"*");
}

function doPopupBlockerTest() {
	document.getElementById("InvisibleFrame2").setAttribute("src","popup_blocker_test/index.html");
}


var guiLanguageCollector = new Array();
var guiLanguageCollectorKeys = new Array();
/*
 * comma separated
 * attrName:
 * objId | "" (by object id -> first node value)
 * title|alt (of html elements)
 * winTitle (title of a window)
 * inputButton ( sets the value attribute of <input type="button"...>
*/
function addLKey(hashKey,winRef,attrName,lKey) {
	if(softGUILanguageSwitch==false) return hashKey;
	var overwrite = false
	for(glcks in guiLanguageCollectorKeys) if(guiLanguageCollectorKeys[glcks] == hashKey) overwrite = true;
	if(!overwrite) guiLanguageCollectorKeys[guiLanguageCollectorKeys.length] = hashKey;
	guiLanguageCollector[hashKey] = new Array(attrName,lKey,winRef);
	return hashKey;
}
function getLKey(hashKey) {
	if(guiLanguageCollector[hashKey]) return guiLanguageCollector[hashKey];
}
function guiLChange() { 
//alert(guiLanguageCollectorKeys.length+"\n\n"+guiLanguageCollectorKeys);
	for(glck in guiLanguageCollectorKeys) { // This means the Attributes title and alt of html-elements
		var typesOfSetting = guiLanguageCollector[guiLanguageCollectorKeys[glck]][0].split(",");
		for(tos in typesOfSetting) {
			if(typesOfSetting[tos] == "objId" || typesOfSetting[tos] == "") {
				try {
					guiLanguageCollector[guiLanguageCollectorKeys[glck]][2].document.getElementById(guiLanguageCollectorKeys[glck]).firstChild.nodeValue = belenusHome.ld[guiLanguageCollector[guiLanguageCollectorKeys[glck]][1]];
				} catch(e) {/*alert(e+"\n\ntypesOfSetting[tos]: "+typesOfSetting[tos]+"\nkey: "+guiLanguageCollectorKeys[glck]);*/}
			}		
			if(typesOfSetting[tos] == "title" || typesOfSetting[tos] == "alt") {
				try {
					guiLanguageCollector[guiLanguageCollectorKeys[glck]][2].document.getElementById(guiLanguageCollectorKeys[glck]).setAttribute(typesOfSetting[tos],belenusHome.ld[guiLanguageCollector[guiLanguageCollectorKeys[glck]][1]]);
				} catch(e) {}
			}
			if(typesOfSetting[tos] == "opts") {
				try {
					guiLanguageCollector[guiLanguageCollectorKeys[glck]][2].document.getElementById(guiLanguageCollectorKeys[glck]).text = 
					belenusHome.ld[guiLanguageCollector[guiLanguageCollectorKeys[glck]][1]];
				} catch(e) {}
			}			
			if(typesOfSetting[tos] == "inputButton") {
				try {
					guiLanguageCollector[guiLanguageCollectorKeys[glck]][2].document.getElementById(guiLanguageCollectorKeys[glck]).setAttribute("value",belenusHome.ld[guiLanguageCollector[guiLanguageCollectorKeys[glck]][1]]);
				} catch(e) {}
			}
			if(typesOfSetting[tos] == "winTitle") { // This means the Title of a window
				try {
					guiLanguageCollector[guiLanguageCollectorKeys[glck]][2].document.title = belenusHome.ld[guiLanguageCollector[guiLanguageCollectorKeys[glck]][1]];
				} catch(e) {}				
			}
		}
	}
	if(belenusHome.useTabManager)
		belenusHome.TAM.updateCaptions(belenusHome.wObjOfTab);
}
function setGUILanguage(lang) {
	belenusHome.WM.resetLanguageDependantsLinks(belenusHome.currentLanguage,lang);
	belenusHome.setXCookie("Language",lang.concat("|").concat(currentDataLanguage));
	try {
		if (belenusHome.interpartsVersion == "dynamic") {
			belenusHome.frames.mrhidden2.location.href = "../server?command=change_gui_language";
		}
	} catch(e) {};
	var d = belenusHome.frames.mrhidden.document;
	belenusHome.currentLanguage = lang;
	var custHREF = customHREF("lang/"+lang+"/lang.js");
	d.open();
	if(custHREF == null)
		d.write('<html><head><sc'+'ript src="lang/'+lang+'/lang.js"></sc'+'ript></head><body onload="parent.ld=ld;parent.guiLChange();location.href=\'about:blank\'"></body></html>');
	else
		d.write('<html><head><sc'+'ript src="lang/'+lang+'/lang.js"></sc'+'ript><sc'+'ript src="'+custHREF+'"></sc'+'ript></head><body onload="parent.ld=ld;parent.guiLChange();location.href=\'about:blank\'"></body></html>');
	d.close();
}