//alert('js-library.js');
var selectCat2;
var selectCat3;

var bitteWaehlen = '<bitte wählen>';

/*
* Fills one Drop-Down-Field from another Drop-Down-Field
* and select the an entry in this Drop-Down-Field.
* @param CatIDSourceField:
* <select name="<CatIDTargetField>" 
* 		  id="<CatIDTargetField>" 
* 		  class="..." 
* 		  onChange="changeDependingSelectField('CatIDSourceField', '<CatIDTargetField>',
*                                               <subCategories>,'<formname>',
*                                               '{bitte_waehlen}', 6)">...
*                                               </select>
* @param CatIDTargetField: 
* <select name="<CatIDTargetField>" id="<CatIDTargetField>" class="...">
* @param subCategories: get this form an array like this
* <templateLoop> (e.g. itemlist ...)
* unterKategorien[{i}] = new Array('{target_id}','{name}','{id}');
* </templateLoop>
* @param formName: e. g. <form method="post" name="<formname>">...
* @param TopItemDescription: e. g. 'bitte wählen'
* @param SelectedIndexValueTargetField: -> the number of the selected value -> so that will be displayed
* @author Sebastian Güldenberg <sebastian@gueldenberg.com>
* @version 26.10.2009
 */
function changeDependingSelectField(CatIDSourceField,
                     				CatIDTargetField,
                     				subCategories,
                     				formName,
                     				TopItemDescription,
                     				SelectedIndexValueTargetField){
  var SelectSourceField = document.forms[formName].elements[CatIDSourceField];
  var SelectTargetField = document.forms[formName].elements[CatIDTargetField];
 
  if(TopItemDescription==null) TopItemDescription = 'Bitte waehlen';
  if(SelectedIndexValueTargetField==null) SelectedIndexValueTargetField = 0;
  
  var argv = SelectSourceField.options[SelectSourceField.selectedIndex].value;
  var count = 1;

    SelectTargetField.length=0;
    SelectTargetField.options[0] = new Option(TopItemDescription,"0");
    
    for(var j=0; j < subCategories.length; j++){
        if(subCategories[j][0] == argv){
          SelectTargetField.options[count] = 
               new Option(subCategories[j][1],subCategories[j][2]);
         
          if(SelectedIndexValueTargetField>0){
            if(SelectTargetField.options[count].value==
                          SelectedIndexValueTargetField){
              SelectTargetField.selectedIndex = count;
            }
          }
          count++;
        }
    }
}


/*
 * Debug Funktionen
 * TODO: Fälle für type bool,function,undefined einbauen
 */
function dump_var (variable,tab) {
  if(typeof(tab) === "undefined") tab="";
  if((typeof(variable)) === "string" || (typeof(variable)) === "number") return variable+"\n";
  if((typeof(variable)) === "object") {
    var ret = "{\n";
    for (var piece in variable) {
      ret += tab+piece+" => "+dump_var(variable[piece], tab+"  ");
    }
    ret += tab+"}\n";
    return ret;
  }
  return tab+"Variable entspricht keinem erwarteten Typ.\n";
/*
boolean (Ja/Nein-Variable),
string (Zeichenkettenvariable),
number (numerische Variable),
function (Funktion),
object (Objekt),
undefined (unbestimmter Typ)
*/
}

function intval (mixed_var, base) {
  // http://kevin.vanzonneveld.net
  // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // +   improved by: stensi
  // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // *     example 1: intval('Kevin van Zonneveld');
  // *     returns 1: 0
  // *     example 2: intval(4.2);
  // *     returns 2: 4
  // *     example 3: intval(42, 8);
  // *     returns 3: 42
  // *     example 4: intval('09');
  // *     returns 4: 9

  var tmp;

  var type = typeof( mixed_var );

  if (type == 'boolean'){
      if (mixed_var == true) {
          return 1;
      } else {
          return 0;
      }
  } else if (type == 'string'){
      tmp = parseInt(mixed_var * 1, 10);
      if (isNaN(tmp) || !isFinite(tmp)){
          return 0;
      } else{
          return tmp.toString(base || 10);
      }
  } else if (type == 'number' && isFinite(mixed_var) ){
      return Math.floor(mixed_var);
  } else{
      return 0;
  }
}

function checkWordlength(formField){
	 var Satz = formField.value; //document.ArticleData.uaTeaser.value;
	 var Woerter = Satz.split(" ");
	 var maxWordLength = 25;
		for (i = 0; i < Woerter.length; i++){
			if (Woerter[i].length > maxWordLength){
				//alert("{zu_langes_wort} "+Woerter[i]);
				Woerter[i] = Woerter[i].substr(0,maxWordLength);
				formField.value = Woerter.join(" ");
			}
		}
}

function letterCounterTextField(userlength,formField,msgTargetFormField, msg, allowedWordLength){
	   if(allowedWordLength == null) allowedWordLength = 0;
	  
	   
	   var  wert, maxlen;
	   var  fromLength = 0;
	   //msgTargetFormField = document.ArticleData.msgtxt.value;
	   maxlen  = userlength - (fromLength + 1);
	   wert  = maxlen - formField.value.length;
	   if (wert < 0){  
			formField.value = formField.value.substr(0,maxlen);
	   }
	   //alert(msg);
	   if(msg == null){
		   	msg = "Es stehen Ihnen noch " + (maxlen - formField.value.length) + 
		   		  " Zeichen zur Verfügung.";
		   	msgTargetFormField.value  = msg;
	   }else{
		   msgTargetFormField.value  = msg + " " + (maxlen - formField.value.length);
	   }

	   
	   if(allowedWordLength > 0) checkWordlength(formField);
}

function openHelp(url){
	helpp = open(url,"hp","width=425,height=600,status=no,toolbar=no,menubar=no,scrollbars=no");
  }

function hideHTMLBlock(htmlBlockID){
	$(htmlBlockID).html("");
	return false;
}

function redirect(redirectTarget)
{
  redirectPage(redirectTarget);
}
function redirectPage(redirectTarget)
{
  if(redirectTarget != null && redirectTarget != '')
  {
    document.location.href = redirectTarget;
  }
}
// String 
function strTrim (zeichenkette) {
  return zeichenkette.replace(/^\s+/, '').replace (/\s+$/, '');
}

function strRemoveLastSpecialSign(str,sign){
  if(sign != null){
    var newstr = '';
    var lenthOfString = str.length;
    var lastIndexOfSign = str.lastIndexOf("&") + 1;
    if(lenthOfString == lastIndexOfSign){
      newstr = str.substr(0,lenthOfString - 1);
      //alert(lastIndexOfSign + '--' + lenthOfString+'###remove00'+str);
    }
    return newstr;
  }
  

}

function replaceDivBoxElements(dataArray, 
                               from, to, 
                               targetBoxID,
                               outputtype, 
                               cssclassprefix){
  if(outputtype == null) outputtype = 'p';
  if(cssclassprefix == null) cssclassprefix = 'boxLine';
  
  if(dataArray && from >= 0 && to > 0){
    var newHTML = '';
    var newHTMLElements = '';
    if(to > dataArray.length) to = dataArray.length;
    for(var i = from; i < to; i++){
      for(var j = 0; j < dataArray[i].length; j++){
        if(outputtype == 'table'){
          newHTMLElements += '<td class="' + cssclassprefix + '_' + i + '">' + dataArray[i][j] + "</td>";
        }else{
          newHTMLElements += dataArray[i][j];
        }
      }
      if(outputtype == 'p'){
        newHTML += '<p class="'+ cssclassprefix + '_' + i + '">'+ newHTMLElements +'</p>\n';
      }else if(outputtype == 'table'){
        newHTML += '<tr class="'+ cssclassprefix + '_' + i + '">'+ newHTMLElements +'</tr>\n';
      }
      newHTMLElements = '';
    }
    //alert(newHTML);
    $('#' + targetBoxID).html(newHTML);
  }
}

function show2DArrayValuesByAlert(arValues){
  var strArTmp = '';
  var strArNew = '';
  for(var i = 0; i < arValues.length; i++){
    for(var j = 0; j < arValues[i].length; j++){
      strArTmp += arValues[i][j] + '|';
    }
    strArNew += strArTmp + '[#LINEEND#]\n';
  }
  alert(strArNew);
}
function checkIfStringIsLink(strToAnalyse){
  var ergebnis = strToAnalyse.search(/http:/);
  if (ergebnis != -1){
    return true;
  }else{
    return false; 
  }
}

/**
 * Google-Maps durch Dezimal-Geokoordnaten laden.(z.B. 49.077777777778,+9.1505555555556)
 * 
 **/
function getStandardGoogleMapMapExtractbyGeoCoordinates(geoCoordinates, mapExtractWidth, mapExtractHeight, zoomfactor, maptype)
{
  if(geoCoordinates == null) geoCoordinates = "49.077777777778,+9.1505555555556";
  if(mapExtractWidth == null) mapExtractWidth = "600";
  if(mapExtractHeight == null) mapExtractHeight = "350";
  if(zoomfactor == null) zoomfactor = "15";
  if(maptype == null) maptype = "m";
  if(maptype == "satellite") 
  {
	  maptype = "h";
  }
  else
  {
	  maptype = "m";
  }
    var strHTML = '<iframe width="'+mapExtractWidth+'" height="'+mapExtractHeight+'" frameborder="0" scrolling="no" ' +
        'marginheight="0" marginwidth="0" ' +
        'src="http://maps.google.de/maps?f=q&amp;source=s_q&amp;hl=de&amp;geocode=&amp;q='+geoCoordinates+'&amp;aq=&amp;sll='+geoCoordinates+'&amp;vpsrc=0&amp;ie=UTF8&amp;t='+maptype+'&amp;z='+zoomfactor+'&amp;output=embed">' +
    '</iframe>' +
        '<br />' +
        '<small>' +
          '<a href="http://maps.google.de/maps?f=q&amp;source=embed&amp;hl=de&amp;geocode=&amp;q='+geoCoordinates+'&amp;aq=&amp;vpsrc=0&amp;ie=UTF8&amp;t=m&amp;z=14" style="color:#0000FF;text-align:left">' +
            'Größere Kartenansicht</a>' +
          '</small>';
  return strHTML;
}
/**
* Google-Maps durch Adresse laden.
**/
function getStandardGoogleMapMapExtractbyAdress(Street,postCode,City, mapExtractWidth, mapExtractHeight, zoomfactor, maptype)
{
  if(mapExtractWidth == null) mapExtractWidth = "600";
  if(mapExtractHeight == null) mapExtractHeight = "350";
  if(zoomfactor == null) zoomfactor = "15";
  if(maptype == null) maptype = "m";
  if(maptype == "satellite") 
  {
	  maptype = "h";
  }
  else
  {
	  maptype = "m";
  }
  Street = encodeURI(Street);
  postCode = encodeURI(postCode);
  City = encodeURI(City);
    var strHTML = '<iframe width="'+mapExtractWidth+'" height="'+mapExtractHeight+'" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" ' + 
    	'src="http://maps.google.de/maps?q='+Street+',+'+postCode+',+'+City+'&hl=de&ie=UTF8&vpsrc=6&z='+zoomfactor+'&amp;t='+maptype+'&amp;output=embed"></iframe>' + 
    	'<br /><small>' + 
    	'<a href="http://maps.google.de/maps?q='+Street+',+'+postCode+',+'+City+'&amp;hl=de&amp;ie=UTF8&amp;vpsrc=6&amp;hq=&amp;t=m&amp;z='+zoomfactor+'&amp;source=embed" style="color:#0000FF;text-align:left">Größere Kartenansicht</a></small>';
  return strHTML;
}

/**
* Steuert das Laden von Google-Maps bestimmt durch Adressangaben!
* HTML Aufrufender Link:
* <!-- GOOGLE-MAPS-LINK-->
*   <a href="javascript:showGoogleMapEmbeddedByAdress('#googleMapRow_{n}', '{uStreet}+{uStreetNo}','{uPLZ}','{uOrt}', 'map');">Karte</a></div>
* <!-- /GOOGLE-MAPS-LINK-->
*
* HTML DIV in die die Google-Maps geladen werden:
* <!-- GOOGLE-MAPS-DIV-->
*   <div class="googleMapRow">
*     <div id="googleMapRow_{n}_Control" style="width:100%;text-align:right;display:none;">
*      <a href="javascript:showGoogleMapEmbeddedByAdress('#googleMapRow_{n}', '{uStreet}+{uStreetNo}','{uPLZ}','{uOrt}',650,350,15);">schliessen</a>
*     </div>
*     <div id="googleMapRow_{n}" style="width: 100%;">
*     </div>
*   </div>
* <!-- /GOOGLE-MAPS-DIV-->
**/
function showGoogleMapEmbeddedByAdress(htmlBlockID, Street,postCode,City,mapExtractWidth,mapExtractHeight,zoomfactor, maptype)
{

	var strDivGoogleMapsRow = strTrim($(htmlBlockID).html());
  if(strDivGoogleMapsRow.length == 0)
  {
    if(mapExtractWidth == null) mapExtractWidth = "600";
    if(mapExtractHeight == null) mapExtractHeight = "350";
    if(zoomfactor == null) zoomfactor = "15";
    var strHTML2Show = getStandardGoogleMapMapExtractbyAdress(Street,postCode,City, mapExtractWidth, mapExtractHeight, zoomfactor, maptype);
    $(htmlBlockID).html(strHTML2Show);
    $(htmlBlockID+"_Control").css("display","block");
  }
  else
  {
    if($(htmlBlockID).css("display") == "block")
    {
      $(htmlBlockID).css("display","none");
      $(htmlBlockID+"_Control").css("display","none");
    }
    else
    {
      $(htmlBlockID).css("display","block");
      $(htmlBlockID+"_Control").css("display","block");
    }
  }
}

/**
 * Steuert das Laden von Google-Maps betsimmt durch Dezimal-Geokoordinaten!
 * HTML Aufrufender Link:
 * <!-- GOOGLE-MAPS-LINK-->
 *   <a href="javascript:showGoogleMapEmbeddedByAdress('#googleMapRow_{n}',  '{geoLaengenDecimal},{geoBreitenDecimal}');">Karte</a></div>
 * <!-- /GOOGLE-MAPS-LINK-->
 *
 * HTML DIV in die die Google-Maps geladen werden:
 * <!-- GOOGLE-MAPS-DIV-->
 *   <div class="googleMapRow">
 *     <div id="googleMapRow_{n}_Control" style="width:100%;text-align:right;display:none;">
 *      <a href="javascript:showGoogleMapEmbeddedByAdress('#googleMapRow_{n}', '{geoLaengenDecimal},{geoBreitenDecimal}');">schliessen</a>
 *     </div>
 *     <div id="googleMapRow_{n}" style="width: 100%;">
 *     </div>
 *   </div>
 * <!-- /GOOGLE-MAPS-DIV-->
 **/

function showGoogleMapEmbeddedByDecimalGeoCoordinates(htmlBlockID,geoCoordinates,mapExtractWidth,mapExtractHeight,zoomfactor, maptype)
{
	var strDivGoogleMapsRow = strTrim($(htmlBlockID).html());
	if(strDivGoogleMapsRow.length == 0)
	{
    if(mapExtractWidth == null) mapExtractWidth = "600";
    if(mapExtractHeight == null) mapExtractHeight = "350";
    if(zoomfactor == null) zoomfactor = "15";
    var strHTML2Show = getStandardGoogleMapMapExtractbyGeoCoordinates(geoCoordinates, mapExtractWidth, mapExtractHeight, zoomfactor, maptype);
    $(htmlBlockID+"_Control").html(strHTML2Show);
    $(htmlBlockID).css("display","block");
  }
  else
  {
    if($(htmlBlockID).css("display") == "block")
    {
      $(htmlBlockID).css("display","none");
      $(htmlBlockID+"_Control").css("display","none");
    }
    else
    {
      $(htmlBlockID).css("display","block");
      $(htmlBlockID+"_Control").css("display","block");
    }
  }
}

