function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

/*
	Bereinige String von Schweinerein
*/
function clear_data(string) {
	
	/*
		Sonderzeichen ändern
	*/
	string = string.replace(/#/g, '|RAUTE|');
	string = string.replace(/&/g, '|AMP|');
	string = string.replace(/%/g, '|PROZENT|');
	string = string.replace(/!/g, '|AUSRUFEZEICHEN|');
	string = string.replace(/\?/g, '|QMARK|');
	
	return string;
}

/*
	Kontakt-Formular betätigen
*/
function sendContact() {

	if (trim($('email').value) == '' || trim($('subject').value) == '' || trim($('text').value) == '') {
	
		$('errorMsg').style.display = 'block';
	
	} else {

		$('errorMsg').style.display = 'none';
		$('contactButton').style.display = 'none';
		$('contactLoading').style.display = 'block';

		new Ajax.Request(
			'./contact.php',
			{
				method: 'post',
				parameters: 'email=' + clear_data($('email').value) + '&subject=' + clear_data($('subject').value) + '&text=' + clear_data($('text').value),
				onComplete: function (x) {
					var result = x.responseText;
					
					if (result == 'invalid email') {
						$('errorMsg').style.display = 'block';
						$('errorMsg').innerHTML = 'Die angegebene E-Mail-Adresse ist nicht korrekt.';
						
						$('contactButton').style.display = 'block';
						$('contactLoading').style.display = 'none';
					} else if (result == 'no error') {
						$('errorMsg').style.display = 'block';
						$('errorMsg').innerHTML = 'Die Nachricht wurde erfolgreich versendet.';
					
						$('contactLoading').style.display = 'none';
					} else {
						alert(x.responseText);
					}
				}
			}
		);
	
	}
	
}

/*
	Einen Betrag in das PayPal-Form schreiben
*/
function paypalValue(id, value) {
	$(id).value = value.replace(/,/g, '.');
}

/*
	Preise berechnen
*/
function calcPreis(preis, amount, id1, id2) {

	var sum = preis / 1000 * amount;

	if (!isNaN(sum)) {
		$(id1).innerHTML = amount;
		$(id2).innerHTML = number_format(sum, 2, ',', '.');
	}
}

function number_format (number, decimals, dec_point, thousands_sep)
{
  var exponent = "";
  var numberstr = number.toString ();
  var eindex = numberstr.indexOf ("e");
  if (eindex > -1)
  {
    exponent = numberstr.substring (eindex);
    number = parseFloat (numberstr.substring (0, eindex));
  }
  
  if (decimals != null)
  {
    var temp = Math.pow (10, decimals);
    number = Math.round (number * temp) / temp;
  }
  var sign = number < 0 ? "-" : "";
  var integer = (number > 0 ? 
      Math.floor (number) : Math.abs (Math.ceil (number))).toString ();
  
  var fractional = number.toString ().substring (integer.length + sign.length);
  dec_point = dec_point != null ? dec_point : ".";
  fractional = decimals != null && decimals > 0 || fractional.length > 1 ? 
               (dec_point + fractional.substring (1)) : "";
  if (decimals != null && decimals > 0)
  {
    for (i = fractional.length - 1, z = decimals; i < z; ++i)
      fractional += "0";
  }
  
  thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ? 
                  thousands_sep : null;
  if (thousands_sep != null && thousands_sep != "")
  {
	for (i = integer.length - 3; i > 0; i -= 3)
      integer = integer.substring (0 , i) + thousands_sep + integer.substring (i);
  }
  
  return sign + integer + fractional + exponent;
}

function genAdCode(domain, user_id) {
	
	$('bannercode').value = '<script type="text/javascript" src="' + domain + '/advertisement.php?user_id=' + user_id + '&art=' + $('art').value + '&cat=' + $('cat').value + '"></script>';
	
}

function generateWMittel(domain, user_id, ad_id, art, website) {
	
	if (art == 'Layer') {
		art = 'layer';
	} else if (art == 'Banner') {
		art = 'banner';
	} else if (art == 'PopUp') {
		art = 'popup';
	} else if (art == 'iFrame-Banner') {
		art = 'iframebanner';
	}
	
	var link = art + '-' + encode64('m=vip&u=' + user_id + '&w=' + website + '&aid=' + ad_id);
	
	$('bannercode').value = '<script type="text/javascript" src="' + domain + '/' + link + '.js"></script>';
	
}

function checkRegForm() {

	if ($('vorname').value == '' || $('nachname').value == '' || $('strasse').value == '' || $('plz').value == '' || $('ort').value == '' || $('usernameReg').value == '' || $('email_address').value == '' || $('password1').value == '' || $('password2').value == '') {
			
		alert('Bitte geben Sie alle Daten in das Registrierungs-Formular ein!');
		
	} else {
		new Ajax.Request(
			'./ajax.php?mode=captchaCheck',
			{
				method: 'post',
				parameters: '',
				onComplete: function (x) {
					if ($('capt').value == x.responseText) {
						$('regForm').submit();
					} else {
						alert('Der eingegebene Captcha-Code ist leider nicht richtig! Bitte probieren Sie es noch einmal.');
					}
				}
			}
		);
	}

}

function changeWebsite() {

	var newCode = encode64('u=' + $('user_id').value + '&amp;w=' + $('website').value);

	if ($('bannerSize').value == '') {
		var newCodeBanner = encode64('u=' + $('user_id').value + '&amp;w=' + $('website').value);
	} else {
		var newCodeBanner = encode64('u=' + $('user_id').value + '&amp;w=' + $('website').value + '&amp;s=' + $('bannerSize').value);
	}
	
	if ($('iFrameBannerSize').value == '') {
		var newCodeiFrameBanner = encode64('u=' + $('user_id').value + '&amp;w=' + $('website').value);
	} else {
		var newCodeiFrameBanner = encode64('u=' + $('user_id').value + '&amp;w=' + $('website').value + '&amp;s=' + $('iFrameBannerSize').value);
	}
	
	$('layercode').innerHTML = $('layercode').innerHTML.replace($('defaultBase').value, newCode);
	$('popupcode').innerHTML = $('popupcode').innerHTML.replace($('defaultBase').value, newCode);
	$('popupcodelink').innerHTML = $('popupcodelink').innerHTML.replace($('defaultBase').value, newCode);
	
	$('bannercode').innerHTML = $('bannercode').innerHTML.replace($('defaultBaseBanner').value, newCodeBanner);
	$('iFrame-bannercode').innerHTML = $('iFrame-bannercode').innerHTML.replace($('defaultBaseiFrameBanner').value, newCodeiFrameBanner);
	
	$('defaultBase').value = newCode;
	$('defaultBaseBanner').value = newCodeBanner;

}

function encode64(inp){

    var key="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

    var chr1,chr2,chr3,enc3,enc4,i=0,out="";

    while(i<inp.length){

        chr1=inp.charCodeAt(i++);if(chr1>127) chr1=88;

        chr2=inp.charCodeAt(i++);if(chr2>127) chr2=88;

        chr3=inp.charCodeAt(i++);if(chr3>127) chr3=88;

        if(isNaN(chr3)) {enc4=64;chr3=0;} else enc4=chr3&63

        if(isNaN(chr2)) {enc3=64;chr2=0;} else enc3=((chr2<<2)|(chr3>>6))&63

        out+=key.charAt((chr1>>2)&63)+key.charAt(((chr1<<4)|(chr2>>4))&63)+key.charAt(enc3)+key.charAt(enc4);

    }

    return encodeURIComponent(out);

}

var slidingExport, slidingExport2, slidedOut = false;

function slideInStats(id, height) {

	var styleHeight = parseInt($(id).style.height.replace(/px/, ''));
	var marginTop = parseInt($(id).style.marginTop.replace(/px/, ''));
	
	if (styleHeight < height && slidingExport) {
		window.setTimeout("slideInStats('" + id + "', '" + height + "')", 10);
		
		d_block(id);
		$(id).style.overflow = 'hidden';
		$(id).style.height = styleHeight + 1 + 'px';
		$(id).style.marginTop = marginTop - 1 + 'px';
	}
}

function onOffOpaStats(id) {

	var opacity = $(id).style.opacity;

	if (opacity <= 1) {
		$(id).style.opacity = parseFloat(opacity) + 0.02;
		window.setTimeout("onOffOpaStats('" + id + "')", 40);
	}

}

function d_block(id) {
	$(id).style.display = 'block';
}

function d_none(id) {
	$(id).style.display = 'none';
}

function d_inline(id) {
	$(id).style.display = 'inline';
}

function year(id, mode) {

	var yet = parseInt($('yearNum' + id).value);

	var years = new Array(
		'2008',
		'2009',
		'2010',
		'2011',
		'2012',
		'2013',
		'2014',
		'2015'
	);
	
	if (mode == 'prev') {
	
		var num = parseInt(yet) - 1;
	
		if (num == 0) {
			d_none('prevArrow2' + id);
		}
		
		if (num < (years.length - 1)) {
			d_inline('nextArrow2' + id);
		}
	
		$('year' + id).innerHTML = years[num];
		$('yearValue' + id).value = years[num];
		$('yearNum' + id).value = num;
	
	} else if (mode == 'next') {
		var num = parseInt(yet) + 1;
	
		if (num > 0) {
			d_inline('prevArrow2' + id);
		}
		
		if (num == (years.length - 1)) {
			d_none('nextArrow2' + id);
		}
	
		$('year' + id).innerHTML = years[num];
		$('yearValue' + id).value = years[num];
		$('yearNum' + id).value = num;
	
	}

}

function month(id, mode) {

	var yet = parseInt($('monthNum' + id).value);

	var months = new Array(
		'Januar',
		'Februar',
		'März',
		'April',
		'Mai',
		'Juni',
		'Juli',
		'August',
		'September',
		'Oktober',
		'November',
		'Dezember'
	);
	
	if (mode == 'prev') {
	
		var num = parseInt(yet) - 1;
	
		if (num == 0) {
			d_none('prevArrow' + id);
		}
		
		if (num < 11) {
			d_inline('nextArrow' + id);
		}
	
		$('month' + id).innerHTML = months[num];
		$('monthValue' + id).value = (num + 1 < 10) ? ('0' + parseInt(num + 1)) : (num + 1);
		$('monthNum' + id).value = num;
	
	} else if (mode == 'next') {
		var num = parseInt(yet) + 1;
	
		if (num > 0) {
			d_inline('prevArrow' + id);
		}
		
		if (num == 11) {
			d_none('nextArrow' + id);
		}
	
		$('month' + id).innerHTML = months[num];
		$('monthValue' + id).value = (num + 1 < 10) ? ('0' + parseInt(num + 1)) : (num + 1);
		$('monthNum' + id).value = num;
	
	}

}

function pickDate(id, day) {

	if (id == 1) {
		$('d1').value = day;
		$('m1').value = $('monthValue1').value;
		$('y1').value = $('yearValue1').value;
	} else if (id == 2) {
		$('d2').value = day;
		$('m2').value = $('monthValue2').value;
		$('y2').value = $('yearValue2').value;
	}

}
