// JavaScript Document

/***************************** VOLLTEXTSUCHE *************************************************************************/
// Suchfeld Wert konfigurieren und beim reinklicken leeren:
var click = 0;
function configureSearchField() {
     var searchFieldValue = "Suchbegriff";
// geändert, gesuchter Begriffe wird im Feld nicht mehr benötigt.
//     if (location.search.indexOf('query') != -1) {
//          /* cut out query value: */
//          var qo = location.search;
//          var qn = qo.substring(qo.indexOf("query=")+6,qo.length);
//          searchFieldValue = qn.substring(0,qn.indexOf("&"));
//          click++;
//     }
     document.formMeaSuche.query.value = decodeParams(searchFieldValue);
     /* empty field function: */
     document.getElementById('suchFeld').onclick = function () {
          if (click==0) document.getElementById("suchFeld").value = "";
          click++;
          return false;
     }
     return false;
}

function sendMeaSuche() {
     var test = true; 
     if (document.formMeaSuche.query.value == ""){
         alert("Bitte geben Sie einen Suchbegriff ein!");
         document.formSuche.query.focus();
         click = 0;
         test = false;
	}
     if (test) {
          var query       = document.formMeaSuche.query.value;
          var initialSize = document.formMeaSuche.initialSize.value;
          var pageSize    = document.formMeaSuche.pageSize.value;
          var maxResults  = document.formMeaSuche.maxResults.value;
          var singleton   = document.formMeaSuche.singleton.value;
          var resultsURL  = document.formMeaSuche.resultsURL.value;
          document.formMeaSuche.action = '/de/wichtiges/suche.html?query='+query+'&initialSize='+initialSize+'&pageSize='+pageSize+'&maxResults='+maxResults+'&singleton='+singleton+'&resultsURL='+resultsURL;
          document.formMeaSuche.submit();
     }
     return test;
}

function decodeParams(s) {
   var i = 0;
   var params = s;
   var output = "";
   var hxc = "0123456789ABCDEFabcdef"; 
   while (i < params.length) {
       var tmpChar = params.charAt(i);
	   if (tmpChar == "+") {
	       output += " ";
		   i++;
	   } else if (tmpChar == "%") {
			if (i < (params.length-2) && hxc.indexOf(params.charAt(i+1)) != -1 && hxc.indexOf(params.charAt(i+2)) != -1 ) {
				output += unescape( params.substr(i,3) );
				i += 3;
			} else {
				output += ""; // no char in case of error
				i++;
			}
		} else {
		   output += tmpChar;
		   i++;
		}
	}
   return output;
}

/***************************** APOTHEKENSUCHE *************************************************************************/
function sendSuche() {
     var test = true; 
     if ((document.formSuche.zip.value == "") && (document.formSuche.city.value == "")) {
         alert("Bitte geben Sie eine Postleitzahl oder eine Stadt ein!");
         document.formSuche.zip.focus();
         test = false;
	}
	if (!document.formSuche.zip.value == "") {
		if(!document.formSuche.zip.value.match(/^[\d]{3,5}$/)) {
			alert('FEHLER im Feld PLZ!\n\nEine Postleitzahl besteht aus 5 Ziffern.\nErlaubt sind 3-5 Ziffern. Bsp.: 144 oder 14478');
			document.formSuche.zip.focus();
			test = false;
		}
	}
/*
if (!document.formSuche.city.value == "") {
		if(!document.formSuche.city.value.match(/^[a-zA-ZäöüÄÖÜ]{2,}/)) {
			alert('FEHLER im Feld Stadt!\n\nErlaubt sind nur Buchstaben.');
			document.formSuche.city.focus();
			test = false;
		}
	}
	if (!document.formSuche.street.value == "") {
		if(!document.formSuche.street.value.match(/^[a-zA-ZäöüÄÖÜ\.-]{2,}/)) {
			alert('FEHLER im Feld Straße!\n\nErlaubt sind Buchstaben, Zahlen,\nder Punkt und das Minus-Zeichen.');
			document.formSuche.city.focus();
			test = false;
		}
	}
*/
if (test) {
          var zip    = document.formSuche.zip.value;
          var city   = document.formSuche.city.value;
          var street = document.formSuche.street.value;
          document.formSuche.action = '/de/wichtiges/apothekensuche.html?zip='+zip+'&city='+city+'&street='+street;
          document.formSuche.submit();
     }
     return test;
}

// get URl-parameter 
function getValue(name){
   var i=1  //Suchposition in der URL
   var suche = name+"="
   while (i<location.search.length){
      if (location.search.substring(i, i+suche.length)==suche){
         var ende = location.search.indexOf("&", i+suche.length)
         ende = (ende>-1) ? ende : location.search.length
         var loca = location.search.substring(i+suche.length, ende)
         return unescape(loca)
      }
      i++
   }
   return ""
}

// pre-fill form from URL-parameters
function fillForm () {
     document.formSuche.zip.value = getValue("zip");
     document.formSuche.city.value = getValue("city");
     document.formSuche.street.value = getValue("street");
     return true;
}


