function $() {
  var elements = new Array();
  for (var i = 0; i < arguments.length; i++) {
	var element = arguments[i];
	if (typeof element == 'string'){
	  element = document.getElementById(element);
	}
	if (arguments.length == 1)
	  return element;
	elements.push(element);
  }
  return elements;
}

function Get_Cookie(name)
{
	var start = document.cookie.indexOf(name+"=");
	var len = start+name.length+1;
	if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
	if (start == -1) return null;
	var end = document.cookie.indexOf(";",len);
	if (end == -1) end = document.cookie.length;
	return unescape(document.cookie.substring(len,end));
}
function Set_Cookie(name,value,expires,path,domain,secure)
{
	var cookieString = name + "=" +escape(value) +
		( (expires) ? ";expires=" + expires.toGMTString() : "") +
		( (path) ? ";path=" + path : "") +
		( (domain) ? ";domain=" + domain : "") +
		( (secure) ? ";secure" : "");
	document.cookie = cookieString;
}
function not_shown_yet(cookiename)
{
	if (Get_Cookie(cookiename) == 'already_shown') {
		return false;
	} else {
		Set_Cookie(cookiename,'already_shown');
		return true;
	}
}


function framebuster(){
	(top != self)?top.location=self.location:false;
}

function MM_openBrWindow(theURL,winName,features){
	window.open(theURL,winName,features);
}

function switchImage(imgName, imgSrc){
	if(document.images){
		if(imgSrc != "none")
		document.images[imgName].src = imgSrc;
	}
}
function validMail(formID, elementID){
	if(!document.forms[formID].elements[elementID].value.match(/^[a-zA-Z0-9_-]+([\.|\+][a-zA-Z0-9_-]+)*@([0-9a-zA-Z][0-9a-zA-Z-]*[0-9A-Za-z]\.)+([A-Za-z]{2,4})$/)){
		document.forms[formID].elements[elementID].value = "";
		return false;
	}
}

function checkDisplay(elementID){
	$(elementID).style.display = $(elementID).style.display == 'none' ? '' : 'none';
}

function addClassName(elementName, KlassenName) {
    var element = $(elementName);
	if (!(element)) return;

    if (!hasClassName(elementName, KlassenName)) {
		element.className += (element.className ? ' ' : '') + KlassenName;
	}
    return element;
}

function removeClassName(elementName, KlassenName) {
    var element = $(elementName);
	if (!(element)) return;

    element.className = strip(element.className.replace(
      new RegExp("(^|\\s+)" + KlassenName + "(\\s+|$)"), ' '));
    return element;
}

function hasClassName(elementName, KlassenName) {
    var element = $(elementName);
	if (!(element)) return;

    var elementClassName = element.className;
    return (elementClassName.length > 0 && (elementClassName == KlassenName ||
      new RegExp("(^|\\s)" + KlassenName + "(\\s|$)").test(elementClassName)));
}

function element_visible(element) {
    return $(element).style.display != 'none';
}

function toggle(element) {
    var is_visible = element_visible(element);

	if (is_visible) {
		element_hide(element);
	} else {
		element_show(element);
	}
    return $(element);
}

function element_hide(element) {
    $(element).style.display = 'none';
    return element;
}

function element_show(element) {
    $(element).style.display = 'block';
    return element;
}

function toggleClass(elementName, KlassenName) {
    var element = $(elementName);
	if (!(element)) return;

	if (hasClassName(elementName, KlassenName)) {
		removeClassName(elementName, KlassenName);
	} else {
		addClassName(elementName, KlassenName);
	}
    return element;
}

function strip(string) {
    return string.replace(/^\s+/, '').replace(/\s+$/, '');
}

/**
 * validMailByID(elementID)
 *
 * validates a E-Mail address by giving the ID of the field where the address is
 * strored
 *
 * @param string  elementID the name of the field, where the address is stored
 *
 * @return boolean
 */
function validMailByID(elementID){
    var elementValue = $(elementID).value;
    if (!elementValue.match(/^[a-zA-Z0-9_-]+([\.|\+][a-zA-Z0-9_-]+)*@([0-9a-zA-Z][0-9a-zA-Z-]*[0-9A-Za-z]\.)+([A-Za-z]{2,4})$/)) {
        elementValue = "";
        return false;
    } else {
        return true;
    }
}

/**
 * disableOptions(id,start,end)
 *
 * clears the options on a select field
 *
 * @param string  form  the Tab where the field is placed
 * @param string  field the name of the field, which should be focused
 *
 * @return void
 */
function disableOptions(id,start,end){
    for (var i = start; i < end; i++) {
        $(id).options[1] = null;
    }
}

/**
 * filterKrankenhausTageGeld(value)
 *
 * fills options for the field 'krankentagegeld_ab'
 *
 * @param string  value  ?
 *
 * @return void
 */
function filterKrankenhausTageGeld(value) {
    if(value == 1){
        disableOptions('krankentagegeld_ab',1,6);
        $('krankentagegeld_ab').options[1].selected = 'true';
    } else if ($('krankentagegeld_ab').options.length < 8){
        fillOptions('krankentagegeld_ab',1,6);
    }
}

/**
 * fillOptions(id)
 *
 * fills options into a select field
 *
 * @param string  form  the Tab where the field is placed
 * @param string  field the name of the field, which should be focused
 *
 * @return void
 */
function fillOptions(id){
    opt = new Array('- -','3','8','15','25','29','43','92','183');
    for(var i = 0; i < $(id).options.length; i++){
        $(id).options[i] = null;
    }
    for(var i = 0; i < opt.length; i++){
        $(id).options[i] = new Option(opt[i],opt[i],false,true);
    }
    $(id).options[0].selected = 'true';
}

/**
 * validateDateFields(dayField, monthField, yearField)
 *
 * validates a date stored in separate fields for day, month and year
 *
 * @param string  dayField   the name of the day field
 * @param string  monthField the name of the month field
 * @param string  yearField  the name of the year field
 *
 * @return boolean
 */
function validateDateFields(dayField, monthField, yearField) {
    var day   = $(dayField).value;
    var month = $(monthField).value;
    var year  = $(yearField).value;
    return validateDate(day, month, year, 0);
}

/**
 * validateDateString(dateString, future)
 *
 * validates a date stored in a single field or in a string
 *
 * @param string  dateString the string containing the date
 * @param integer future     a flag to tell, if future dates are allowed
 *
 * @return boolean
 */
function validateDateString(dateString, future) {
    var date = new Date();
    var d    = dateString.split('.');
    //ignore, if input is empty
    if (dateString == '') {
        return true;
    }
    if (d.length == 2) {
        d[2] = d[1];
        d[1] = d[0];
        d[0] = '01';
    }
    if (d.length == 3) {
        if (d[2].lenght > 4) {
            d[2] = d[2].substr(0, 4);
        } else if  (d[2].lenght == 3) {
            d[2] += '0';
        } else if  (d[2].lenght == 2) {
            if (d[2] >= 50) {
                d[2] = '19' + d[2];
            } else {
                d[2] = '20' + d[2];
            }
        }
    } else {
        return false;
    }
    if (future < 0) {
        future = 0;
    }
    if (future > 2) {
        future = 2;
    }
    return validateDate(d[0], d[1], d[2], future);
}

/**
 * validateDate(day, month, year, future)
 *
 * validates a date
 *
 * @param integer day    the day value of a date
 * @param integer month  the month value of a date
 * @param integer year   the year value of a date
 * @param integer future a flag to tell, if future dates are allowed
 *
 * @return boolean
 */
function validateDate(day, month, year, future) {
    var date = new Date();
    var actualYear = date.getFullYear();
    if (future < 0) {
        future = 0;
    }
    if (future > 2) {
        future = 2;
    }
    if (IsDecimal(day, true) || IsDecimal(month, true) || IsDecimal(year, true)) {
        return false;
    } else if (day == '' || month == '' || year == '') {
        return false;
    }
    day   = Decimal(day, false);
    month = Decimal(month, false);
    year  = Decimal(year, false);
    if (day > 31) {
        return false;
    } else if (month > 12) {
        return false;
    } else if (month == 2 && day > 29) {
        return false;
    } else if (month == 2 && day == 29 && ((year % 4) > 0 || ((year % 4) == 0 && ((year % 100) == 0 && (year % 400) > 0)))) {
        return false;
    } else if ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30) {
        return false;
    } else {
        var tomorrow  = new Date(actualYear, date.getMonth(), date.getDate() + 30);
        var today     = new Date(actualYear, date.getMonth(), date.getDate());
        var fieldDate = new Date(year, month - 1, day);
        if ((future == 0 && fieldDate >= today) || (future == 2 && fieldDate < tomorrow) || year < (actualYear - 85)) {
            // no future dates
            // no dates more than 85 years ago
            return false;
        } else {
            return true;
        }
    }
}

/**
 * IsNumeric(sText, replace)
 *
 * checks if a var holds numeric data
 *
 * @param string  form  the Tab where the field is placed
 * @param string  field the name of the field, which should be focused
 *
 * @return boolean
 */
function IsNumeric(sText, replace) {
    if (sText == '') {
        return true;
    }
    dummy = Numeric(sText, replace);
    if (dummy === '') {
        return true;
    }
    return isNaN(dummy);
}

/**
 * Numeric(sText, replace)
 *
 * transforms a string to a numeric
 *
 * @param string  form  the Tab where the field is placed
 * @param string  field the name of the field, which should be focused
 *
 * @return boolean
 */
function Numeric(sText, replace) {
    if (replace !== true) {
        replace = false;
    }
    if (typeof(sText) != 'string') {
        sText = sText.toString();
    }
    if (replace === true && sText.indexOf(',') != -1) {
        sText = sText.replace(/\./g, '');
        sText = sText.replace(/,/, '.');
    }
    sText = parseFloat(sText).toString(10);
    if (sText.substr(0,3) == 'NaN') {
        return '';
    }
    pos   = sText.indexOf('.');
    if (pos === -1) {
        sText += '.00';
    } else {
        sText += '00';
        sText = sText.substring(0, pos) + '.' + sText.substr(pos + 1, 2);
    }
    return sText;
}

/**
 * IsDecimal(sText, replace)
 *
 * checks if a var holds decimal data
 *
 * @param string  form  the Tab where the field is placed
 * @param string  field the name of the field, which should be focused
 *
 * @return boolean
 */
function IsDecimal(sText, replace) {
    if (sText == '') {
        return true;
    }
    dummy = Decimal(sText, replace);
    if (dummy === '') {
        return true;
    }
    return isNaN(dummy);
}

/**
 * Decimal(sText, replace)
 *
 * transforms a string to a decimal
 *
 * @param string  form  the Tab where the field is placed
 * @param string  field the name of the field, which should be focused
 *
 * @return boolean
 */
function Decimal(sText, replace) {
    if (replace !== true) {
        replace = false;
    }
    if (typeof(sText) != 'string') {
        sText = sText.toString();
    }
    if (replace === true && sText.indexOf(',') != -1) {
        sText = sText.replace(/\./g, '');
        sText = sText.replace(/,/, '.');
    }
    sText = parseInt(sText, 10);
    if (sText == 'NaN') {
        sText = '';
    }
    return sText;
}

/**
 * IsPhone(sText, replace)
 *
 * checks if a var holds a phone number
 *
 * @param string  form  the Tab where the field is placed
 * @param string  field the name of the field, which should be focused
 *
 * @return boolean
 */
function IsPhone(sText, replace) {
    if ((sText.indexOf('0') != 0 && sText.indexOf('+') != 0) || IsDecimal(sText, replace)) {
        return true;
    } else {
        return false;
    }
}

/**
 * Phone(sText, replace)
 *
 * transforms a string to a phone number
 *
 * @param string  form  the Tab where the field is placed
 * @param string  field the name of the field, which should be focused
 *
 * @return boolean
 */
function Phone(sText, replace) {
    if (sText.indexOf('0') == 0) {
        sText = '0' + Decimal(sText, replace);
    } else if (sText.indexOf('+') == 0) {
        sText = '+' + Decimal(sText, replace);
    } else {
        sText = Decimal(sText, replace);
    }
    return sText;
}