/*
* @copyright Copyright 2010 Adolf Würth GmbH & Co. KG
* @link  http://www.wuerth.de
* @version  0.1
* @lastmodified    2010-02-17
*/

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//PHP-LIKE FUNCTIONS
//more: http://phpjs.org/functions/index or download their package
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function function_exists(nameAsString){
  if(typeof nameAsString == 'string'){
      return (typeof this.window[nameAsString] == 'function');
  } else {
      return (nameAsString instanceof Function);
  }
}

function substr_count (haystack, needle, offset, length) {
  // Returns the number of times a substring occurs in the string
  var pos = 0, cnt = 0;
  haystack += '';
  needle += '';
  if (isNaN(offset)) {offset = 0;}
  if (isNaN(length)) {length = 0;}
  offset--;
  while ((offset = haystack.indexOf(needle, offset+1)) != -1){
    if (length > 0 && (offset+needle.length) > length){
      return false;
    } else {
      cnt++;
    }
  }
  return cnt;
}

function str_replace(search, replace, subject){
  var str = subject.replace(search,replace);
  return str;
}

function explode(separator, string){
	var arrayOfId = string.split(separator);
	return arrayOfId;
}

function in_array(value, array) {
  for(var i = 0; i < array.length; i++) if (value == array[i]) return true;
  return false;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//FORMS AND LINKS
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/* function: confirmLink
 * ask user if he wants to go on
 * use:
 * <a href="index.php" onclick="confirmLink('Nicht alles gespeichert...wirklich fortfahren?');">LINK</a>
 */
function confirmLink(question){
  var msg;
  msg = confirm(question);
  return msg;
}
/* function: getUrlParam
 * get value of a param in URL
 * returns '' if not found
 */
function getUrlParam(strParamName){
	var strReturn = '';
	var strHref = window.location.href;
  //if parameters are present
	if (strHref.indexOf('?') > -1){
		var strQueryString = strHref.substr(strHref.indexOf('?')).toLowerCase();
    //get all param-value pairs in an array
		var aQueryString = strQueryString.split('&');
		for (var i = 0; i < aQueryString.length; i++){
      //if paramname exists get value
			if (aQueryString[i].indexOf(strParamName + '=') > -1){
				var aParam = aQueryString[i].split('=');
				strReturn = aParam[1];
				break;
			}
		}
	}
	return strReturn;
}

/* function: sendForm
 * if you want to send a form and have the opportunity to change the action
 * attribute before sending and add an anchor
 * used:
 * - onclick and so on with links/buttons
 */
function sendForm(formId, action, buttonId, anchor){
	var docForm = document.getElementById(formId);
	if(docForm != null){
    var anc = '';
    if(anchor != '' && anchor != null) anc = "#";
    docForm.action = action + anc + anchor;
    var clickButton = document.getElementById(buttonId);
    if(clickButton != null){
      clickButton.click();
    }
	}
}
/* function: focusInputText
 * set cursor in input field and put it to the end of the string
 */
function focusInputText(elementId){
  var el = document.getElementId(elementId);
  if(el != null){
    el.focus();
    el.value = el.value;
  }
}
/* function: realoadPage
 * relaod a page
 */
function reloadPage(){
	window.location.href = window.location.href;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//HIGHLIGHTING
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/* function: highlightIt
 * if you want to highlight a row of a table (or something else...then you
 * have to adapt the function slightly)
 * use:
 * - define a global var (outside of the function)
 *   => var highlightThis = new Object();
 * - give every <tr> tag an unique ID...AND a class="XXX" has to be given
 *   (needed for restoring the old settings)
 * - use this function for onclick, onmouseover and so on => highlightIt('1');
 * - multiple rows can be selected...on 2nd call the highlighting will disappear
 */
//var highlightThis = new Object();
function highlightIt(id){
  //prefix, eg. id's of table rows always start with 'row_' and just pass the number
  //set this to '' if you want to pass the whole ID via the function
  var highlightColor = '#CC0000';
  var row = document.getElementById(id);
  if(row != null){
    if(highlighThis[id] == undefined || highlighThis[id] == null){
      highlighThis[id] = row.className;
      row.style.backgroundColor = highlightColor;
    } else {
      row.style.backgroundColor = '';
      row.className = highlighThis[id];
      highlighThis[id] = null;
    }
  }
}

function ShowSt(text){
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//GENERAL
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/* mailthis: unscramble mails using js */

function mailthis(adr){
	adrnew = adr.replace(/ \[dot\] /g, ".");
	adrnew = adrnew.replace(/ \[at\] /g, "@");
	top.location.href = "mailto:"+adrnew;
	return false;
}