var actcoll = "";
var m_oWnd;
var asyncReq;

String.prototype.trim  = function() { return this.replace(/^\s+|\s+$/g,""); }
String.prototype.ltrim = function() { return this.replace(/^\s+/,""); }
String.prototype.rtrim = function() { return this.replace(/\s+$/,""); }

function toggle(ObjID)
{
	var actdiv = document.getElementById( ObjID );
	var actimg = document.images["icon" + ObjID];

	if (actdiv != null)
	{
		if (actdiv.style.display == "none")
		{
			actdiv.style.display = "block";
			if (actimg != null) {
				actimg.src = "images/implode.gif";
			}
		}
		else
		{
			actdiv.style.display = "none";
			if (actimg != null) {
				actimg.src = "images/explode.gif";
			}
		}
	}
}

function getWhichPage(whichone) {
	var alle = document.getElementById("ph_next");
	if (alle!=null) {	
		var oDiv;
		var oNextDiv;
		var tmpdivID;
		for (var i=0; i<alle.childNodes.length; i++){
			oDiv = alle.childNodes[i];
			if (oDiv != null) {
				if (oDiv.className == "current") {
					var nextid = new Number(oDiv.id.substring(4));
					nextid += whichone;
					if (nextid == 0) {
						//alert("testmsg ist eh schon null - macht nix mehr");
						break;
					}
					
					oNextDiv = document.getElementById("page"+nextid);
					if (oNextDiv == null) {
						//alert("testmsg nextdiv nicht mehr da - call getpage: " + nextid);
						getPage(nextid);
						break;
					}

					eval(oNextDiv.href);
					commonMakeCurrent("ph_next", "page", nextid, "current", "");
					break;
				}
			}
		}
	}
}
	
function makeActive(DivID) {
	if (DivID==null || DivID=="null" || DivID.length==0)
		return;
	
	var ele = document.getElementById("sel" + DivID);
	if (ele!=null)
		ele.checked = "checked";
	//laschen commonMakeCurrent("ph_collections", "lic", DivID, "current", "");
}

function commonMakeCurrent(fatherid, preid, thisid, activeclass, inactiveclass) {
	var alle = document.getElementById(fatherid);
	if (alle!=null) {	
		var oDiv;
		var tmpdivID;
		var DivIDToCompare = preid + thisid;
		for (var i=0; i<alle.childNodes.length; i++){
			oDiv = alle.childNodes[i];
			if (oDiv != null) {
				tmpdivID = oDiv.id;
				if (tmpdivID!=null) {
					if (tmpdivID == DivIDToCompare){
						oDiv.className = activeclass;
					}
					else if (tmpdivID.indexOf(preid) == 0) {
						oDiv.className = inactiveclass;
					}
				}
			}
		}
	}
}	

function showCluster(ClusterID) {
	//ClusterID: hier searchtypeID als string: ruft AJAX auf
	//--- <li id='cla1'><a href=\"javascript:showCluster('0', 'cla1');\">All (10)</a>
	ajaxExistingResultList(-1, ClusterID, null, null);
	commonMakeCurrent("ph_clusters", "cla", ClusterID, "current", "");
}

function showOnlyDB(DBID) {
	//DBID: ruft AJAX auf
	//--- <li id='cladbid'><a href=\"javascript:showOnlyDB('dbid');\">Buchkatalog (Amazon)</a>
	ajaxExistingResultList(-1, null, null, DBID);
	commonMakeCurrent("ph_clusters", "cla", DBID, "current", "");
}

function showSortedList(sortid) {
	//ClusterID: hier searchtypeID als string: ruft AJAX auf
	//--- <li id='cla1'><a href=\"javascript:showCluster('0', 'cla1');\">All (10)</a>
	ajaxExistingResultList(-1, null, sortid, null);
	commonMakeCurrent("ph_sort", "sort", sortid, "current", "");
}


function ajaxExistingResultList(page, clusterid, sortid, dbid) {
	// Request erzeugen
	if (window.XMLHttpRequest) {
		asyncReq = new XMLHttpRequest(); // Mozilla, Safari, Opera
	} 
	else if (window.ActiveXObject) {
		try {
			asyncReq = new ActiveXObject('Msxml2.XMLHTTP'); // IE 5
		} 
		catch (e) {
			try {
				asyncReq = new ActiveXObject('Microsoft.XMLHTTP'); // IE 6
			} 
			catch (e) {}
		}
	}

	// ueberpruefen, ob Request erzeugt wurde
	if (asyncReq) {
		var url = "_getResultList.jsp?todo=get";
		if (page != -1)
			url += "&page=" + page;
		if (clusterid!=null && clusterid.length>0)
			url += "&clusterid=" + clusterid;
		if (sortid!=null && sortid.length>0)
			url += "&sortid=" + sortid;
		//mehrere werden mit comma getrennt
		if (dbid!=null && dbid.length>0)
			url += "&dbids=" + dbid;
			
		// Request oeffnen
		asyncReq.open('post', url, true);
		// Request senden
		asyncReq.send(null);
		// Request auswerten
		asyncReq.onreadystatechange = writeResultList;
	}
}

function writeResultList() {
  if (asyncReq.readyState == 4) {
    if (asyncReq.status == 200) {
			// den Inhalt des Requests in das <div> schreiben
			var content = asyncReq.responseText;//responseXML ist leer
			if (content != null && content.length > 0) {
			  var assodiv = document.getElementById("ph_resultlist");
			  if (assodiv != null) {
			    assodiv.innerHTML = content;
			  }
			}				
    }
  }
}

function editSearch(ObjID, Term) {
	if (document.searchform != null)
	{
		var tmpValue = document.searchform.query.value;
		var tmpValueLow = tmpValue.toLowerCase();

		//--- replace leading " and ending "
		Term = Term.replace("!Q!", "\"");
		Term = Term.replace("!Q!", "\"");
		var TermLow = Term.toLowerCase();

		//--- existiert Term in tmpValueLow?
		var pos = tmpValueLow.indexOf(TermLow);
		//-- Term not exists => add it
		if (pos == -1) {
			tmpValueLow = tmpValue + " " + Term; //reuse tmpValueLow
			//--- trim, remove "  ", set value
			tmpValueLow = tmpValueLow.replace("  ", " ");
			tmpValueLow = tmpValueLow.replace("  ", " ");
			tmpValueLow = tmpValueLow.trim();
			document.searchform.query.value = tmpValueLow;
		}
	}
}

function tunnelOneContent(resultID, objectID, outputFormat, doHighlight, price) {
	var myForm = document.showcontentform;
	myForm.resultids.value = resultID;
	myForm.objectids.value = objectID;
	myForm.outputformats.value = outputFormat;
	myForm.dohighlights.value = doHighlight;
	myForm.price.value = price;

	//--- kostet nix => showcontent.jsp
	if (price == 0) {
		myForm.action = "showcontent.jsp";
		myForm.target = "truffels";//phuongtest:pay-im-gleichen-fenster
	}
	else {
		myForm.todo.value = "ogml"; //only guest must login
		myForm.message.value = _doccost + " " + _Currency + " " + formatPrice(price) + ".<br />" + _doclogin;
		myForm.from.value = "intern";
		myForm.urlfrom.value = "result.jsp";
		myForm.action = "login.jsp";
	}

	//phuongtest: pay-im-gleichen-fenster
	//myForm.target = "_newwnd";
	myForm.submit();//ok, because here is a click on a link
}

function submitPayment(resultID, objectID, outputFormat, doHighlight, price) {
	var myForm = document.showcontentform;
	myForm.resultids.value = resultID;
	myForm.objectids.value = objectID;
	myForm.outputformats.value = outputFormat;
	myForm.dohighlights.value = doHighlight;
	myForm.price.value = price;
	myForm.urlfrom.value = "result.jsp";
	myForm.action = "showcontent.jsp";
	myForm.target = "truffels";//phuongtest: pay-im-gleichen-fenster
	myForm.submit();//ok, because here is a click on a link
	
	//phuongtest: pay-im-gleichen-fenster
	location.replace("result.jsp?todo=888");
}

function ajaxAssociation(Term, Number) {
	// Request erzeugen
	if (window.XMLHttpRequest) {
		asyncReq = new XMLHttpRequest(); // Mozilla, Safari, Opera
	} 
	else if (window.ActiveXObject) {
		try {
			asyncReq = new ActiveXObject('Msxml2.XMLHTTP'); // IE 5
		} 
		catch (e) {
			try {
				asyncReq = new ActiveXObject('Microsoft.XMLHTTP'); // IE 6
			} 
			catch (e) {}
		}
	}

	// ueberpruefen, ob Request erzeugt wurde
	if (!asyncReq) {
	  var assodiv = document.getElementById("headerasso");
	  if (assodiv != null) {
	    assodiv.innerHTML = "Kann keine AJAX-Instanz erzeugen";//XMLHTTP
	  }
		return false;
	} 
	else {
		var url = "_getAssociation.jsp?term=" + Term + "&number=" + Number;
		// Request oeffnen
		asyncReq.open('post', url, true);
		// Request senden
		asyncReq.send(null);
		// Request auswerten
		asyncReq.onreadystatechange = writeAssociation;
	}
}

/*
onreadystatechange: Der Event-Handler onreadystatechange wird bei jeder Status Aenderung aufgerufen und kann folgende Werte enthalten:
0 = uninitiElized 
1 = loading 
2 = loaded 
3 = interactiv 
4 = complete 

responseText: enthaelt die rohen Textdaten aus dem Antwortpaket.
responseXML: Antwortet der Server mit einem XML-Dokument enthält diese Eigenschaft ein DOM Objekt.
status: enthaelt den numerischer Statuscode des Antwortpakets: 404 = Not Found oder 200 = OK.
*/
function writeAssociation() {
  if (asyncReq.readyState == 4) {
    if (asyncReq.status == 200) {
			// den Inhalt des Requests in das <div> schreiben
			var content = asyncReq.responseText;//responseXML ist leer
			if (content != null && content.length > 0) {
			  var assodiv = document.getElementById("headerasso");
			  if (assodiv != null) {
			    assodiv.innerHTML = content;
			  }
			}				
    }
  }
}

function formatPrice(price) {
	var strprice = "" + price;
	var retstr;
	var point = strprice.indexOf(".");
	// 123 => 123,00
	if (point == -1) {
		retstr = strprice + _CurrencySep + "00";
	}
	// .123 => 0,12
	else if (point == 0) {
		len = (strprice.length>=3) ? 3 : strprice.length();
		retstr = "0" + _CurrencySep + strprice.substring(1, len);
		if (retstr.length() == 2)
			retstr += "00";
		if (retstr.length() == 3)
			retstr += "0";
	}
	//
	else {
		if (strprice.length>=point+3) {
			retstr = strprice.substring(0, point) + _CurrencySep + strprice.substring(point+1, point+3);
		}
		else if (strprice.length==point+2) {
			retstr = strprice.substring(0, point) + _CurrencySep + strprice.substring(point+1, point+2) + "0";
		}
		else {
			retstr = strprice.substring(0, point) + _CurrencySep + strprice.substring(point+1) + "00";
		}
	}
	return retstr;
}

//--- click on category
function changeSelection(collid) {
	var myform = document.searchform;
	if (myform == null) {
		return false;
	}

	myform.todo.value = _DoChangeSelection;
	myform.collid.value = collid;
	myform.submit();
	return true;
}

//--- click on sebpart-laschen
function changeWebPart(thisPart) {
	//1: deep, 2: web, 3: both
	var myform = document.searchform;
	if (myform == null) {
		return false;
	}

	myform.todo.value = _DoChangeWebPart;
	myform.webpart.value = thisPart;
	myform.submit();
	return true;
}

//--- click on search-button
function setValuesForSearch() {
	var myform = document.searchform;
	if (myform == null) {
		return false;
	}
	var searchterm = myform.query.value.trim();
	if (searchterm==null || searchterm.length==0) {
		alert(_fillfields);
		myform.query.focus();
		return false;
	}

	myform.query.value = searchterm;
	myform.page.value = 1;//change only this
	myform.todo.value = _DoSearchNew;
	return true;
}

function setValuesForSearch2() {
	var myform2 = document.searchform2;
	if (myform2 == null) {
		return false;
	}
	var searchterm = myform2.query.value.trim();
	if (searchterm==null || searchterm.length==0) {
		alert(_fillfields);
		myform2.query.focus();
		return false;
	}

	//zum submit
	var myform = document.searchform;
	myform.query.value = searchterm;
	myform.page.value = 1;//change only this
	myform.todo.value = _DoSearchNew;
	return true;
}


//--- get a not already searched page
function getPage(thispage) {
	var myform = document.searchform;
	if (myform == null) {
		return false;
	}

	myform.page.value = thispage;//change only this
	myform.todo.value = _DoSearchNext;
	myform.submit();//ok, because here is a click on a link
}

function showPPVPage(price, fee, endprice) {//prices not as string
	var myForm = document.payingform;
	if (myForm == null) {
		alert("Page error: payingform does not exist !!!");
		return;
	}

	myForm.price.value = price;
	myForm.fee.value = fee;
	myForm.amount.value = endprice;
	myForm.paytype.value = "ppv";
	myForm.todo.value = "check";
	myForm.submit();//ok, because here is a click on a link
}

function showPrepaidPage(price, amount) {//prices not as string
	var myForm = document.payingform;
	if (myForm == null) {
		alert("Page error: payingform does not exist !!!");
		return;
	}
	var amountForm = document.amountform;
	if (amountForm == null) {
		alert("Page error: amountForm does not exist !!!");
		return;
	}

	myForm.price.value = price;
	myForm.amount.value = amountForm.amount.options[amountForm.amount.selectedIndex].value;
	myForm.fee.value = 0;
	myForm.paytype.value = "prp";
	myForm.todo.value = "check";
	myForm.submit();//ok, because here is a click on a link
}

//--- click on login-button
function setValuesForLogin() {
	var myform = document.loginform;
	if (myform == null) {
		return false;
	}
	
	var bErrors = false;

	//--- login
	var login = myform.login.value.trim();
	var logindiv = document.getElementById("logindiv");
	if (login=="" || login==null || login.length==0) {
		if (logindiv != null)
			logindiv.className = logindiv.className + " errfield";

		myform.login.focus();
		bErrors = true;
	}
	else if (logindiv != null && logindiv.className.indexOf(" ")>0)
		logindiv.className = logindiv.className.substring(0, logindiv.className.indexOf(" "));

	var pwd = myform.password.value.trim();
	var pwddiv = document.getElementById("passworddiv");
	if (pwd=="" || pwd==null || pwd.length==0) {
		if (pwddiv != null)
			pwddiv.className = pwddiv.className + " errfield";

	  if (!bErrors){
			myform.password.focus();
			bErrors = true;
	  }
	}
	else if (pwddiv != null && pwddiv.className.indexOf(" ")>0)
		pwddiv.className = pwddiv.className.substring(0, pwddiv.className.indexOf(" "));

  if (bErrors){
		var errdiv = document.getElementById("errmsgdiv");
		if (errdiv != null) {
			errdiv.innerHTML = _errors + "<hr>";
		}
		else {
			alert(errs);
		}
		return false;
	}
	else {
		myform.todo.value = "login";
		return true;
	}
}

function isLoginAGuest(Loginstr) {
	if (Loginstr.indexOf("_") == 0) {
		return true;
	}
	else {
		return false;
	}
}

function actualizeLogin(actLogin) {
	var headerdiv = document.getElementById("ph_fcttop");
	if (headerdiv != null) {
		var text;
		//user
		if (actLogin != null && actLogin != "" && actLogin != "null" && actLogin.indexOf("_") != 0) {
			text = _welcome + ", " + actLogin + "! <a href=\"javascript:showMyAccountPage();\">" + _myaccount + "</a><a href=\"javascript:showLogoutPage();\">" + 
			_logout + "</a>";
			headerdiv.innerHTML = text;
		}
		//guest
		else {
			text = _welcomeg + "! <a href=\"javascript:showLoginPage();\">" + _login + 
				"</a><a href=\"javascript:showRegistrationPage();\">" + _registration + "</a>";
			headerdiv.innerHTML = text;
		}
	}
}

function cancelLogin(urltogo) {
	location.replace("error.jsp?showpage=0&urlfrom=" + urltogo);
}

function showLogoutPage() {
	location.replace("command.jsp?todo=1&urlfrom=" + location.href);
}

function showRegistrationPage() {
	location.replace("command.jsp?todo=2&urlfrom=" + location.href);
}

function showForgetPage() {
	location.replace("command.jsp?todo=3&urlfrom=" + location.href);
}

function showMyAccountPage() {
	location.replace("command.jsp?todo=4&urlfrom=" + location.href);
}

function showEditContractPage(docID) {
	location.replace("command.jsp?todo=5&docid=" + docID + "&urlfrom=" + location.href);
}

function showListSelectionsPage() {
	location.replace("command.jsp?todo=7&urlfrom=" + location.href);
}

function showEditCollectionPage() {
	location.replace("command.jsp?todo=8&collid=" + actcoll + "&urlfrom=" + location.href);
}

function showLoginPage() {
	location.replace("command.jsp?todo=9&urlfrom=" + location.href);
}

function showAbstract(docid, e) {
	var msgdiv = document.getElementById("previewobj");//previewmsg
	//--- in result.jsp: get content via ajax
	if (msgdiv!=null){
		msgdiv.innerHTML = _getabstr;
		ajaxAbstract("showabstract.jsp?resultids="+docid);
		showpreview(null, e);
		msgdiv.style.display = "block";
	}
	//--- in other pages
	else {
	  doShowTruffelsURL("showabstract.jsp?showlinks=0&resultids="+docid);
	}
}

function doShowConditions(iLanguage) {
  doShowTruffelsURL("/about/lang_" + iLanguage + "/legal_term.htm?show=no");
}

function doShowTruffelsURL(url) {
  window.open(url, "truffels", "location=1,menubar=1,resizable=1,scrollbars=1,status=1,titlebar=1,toolbar=1");
}

function executePayment(aid) {
	var ele = document.getElementById(aid);
	if (ele !=null) {
		eval(ele.href);
	}
}

function createNewSelection() {
 	location.replace("reg/acchangecollections.jsp?acaction=10&newcollection=true&collid=null&returnto=../result.jsp?todo=888&returnerr=../result.jsp?todo=888&urlcancel=../result.jsp?todo=888");
} 

function suggestSource(langid, name, vorname, telefon, mail) {
  //escape() for especial chars
  var sURL = "/about/lang_" + langid + "/contact_suggest.htm?" +
		"Name=" + escape(name) +
		"&Vorname=" + escape(vorname) +
		"&Telefon=" + escape(telefon) +
		"&Mail=" + escape(mail);

	doShowTruffelsURL(sURL);
}

function ajaxAbstract(url) {
	// Request erzeugen
	if (window.XMLHttpRequest) {
		asyncReq = new XMLHttpRequest(); // Mozilla, Safari, Opera
	} 
	else if (window.ActiveXObject) {
		try {
			asyncReq = new ActiveXObject('Msxml2.XMLHTTP'); // IE 5
		} 
		catch (e) {
			try {
				asyncReq = new ActiveXObject('Microsoft.XMLHTTP'); // IE 6
			} 
			catch (e) {}
		}
	}

	// ueberpruefen, ob Request erzeugt wurde
	if (asyncReq) {
		// Request oeffnen
		asyncReq.open('post', url, true);
		// Request senden
		asyncReq.send(null);
		// Request auswerten
		asyncReq.onreadystatechange = writeAbstract;
	}
}

function writeAbstract() {
  if (asyncReq.readyState == 4) {
    if (asyncReq.status == 200) {
			// den Inhalt des Requests in das <div> schreiben
			var content = asyncReq.responseText;//responseXML ist leer
			if (content != null && content.length > 0) {
			  var abstractdiv = document.getElementById("previewobj");
			  if (abstractdiv != null) {
			    abstractdiv.innerHTML = content;
					/*var msgdiv = document.getElementById("previewmsg");
					if (msgdiv!=null){
						msgdiv.style.display = "none";
				  }*/
				}
			}				
    }
  }
}
