// Open Error-Message-Box
function showError(headline, content, errorClass)
{

    if (typeof errorClass == 'undefined') errorClass = 'notice';
    var errorContent = '<h1>' + headline + '</h1><p>' + content + '</p>';
    $("#errorMessage").addClass(errorClass).html(errorContent).slideDown();
	// scroll to top, so user can read the error message
	$(window).scrollTop(0);

}

// converts things like 10.000,00, 10,000.00, 10000 (i.e. normal numbers but german/english) to usable integer
function parseNumber(string) {

	// return, if nothing was consigned
	if(typeof string == 'undefined') return;

	// stip all non-digit characters from string
	string = string.replace(/([^0-9\.,]+)/g, "");

    var length = string.length
    var presumedDeviderPos      = length - 2;
    var presumedDevider         = string.slice(presumedDeviderPos - 1, presumedDeviderPos);

    // is there a devider?
    if (presumedDevider != "," && presumedDevider != ".") {
        string      = string.replace(/\./g, "").replace(/,/g, "");          // remove everything
        return parseFloat(string);
    } else if (presumedDevider == ",") {
        var preComma    = string.slice(0, presumedDeviderPos-1);
        var fromComma   = string.slice(presumedDeviderPos-1, length);
        preComma    = preComma.replace(/\./g, "");                          // remove all periods in preComma
        fromComma   = fromComma.replace(/,/g, ".");                         // replace last comma with period
        return parseFloat(preComma + fromComma);
    } else {
        string = string.replace(/,/g, "");                                  // remove all commas
        return (parseFloat(string));
    }

}

// formats number to this: 10.000,00 EUR
function showCurrency(nStr)
{
    nStr = nStr.toFixed(2);
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? ',' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + '.' + '$2');
    }
    return x1 + x2 + " EUR";
}

// formats number to this: 10.000,00
function showNumber(nStr)
{
    nStr = nStr.toFixed(2);
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? ',' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + '.' + '$2');
    }
    return x1 + x2;
}

// performance test (Usage: var start = performanceTest("start"); ... code to test ... performanceTest("stop", start);
function performanceTest(start, startTime) {

    var date            = new Date();
    var milliseconds    = date.getTime();

    if (start == "start") {

        return milliseconds;

    } else {

        console.log(milliseconds - startTime, "Laufzeit in Millisekunden");

    }

}

// colors for default value
var active_color 	= '#707173'; 	// Colour of user provided text
var inactive_color 	= '#bec3c0'; 	// Colour of default text

// jQuery Ready-Handler
$(function() {

    // Hide all elements which should be initially hidden
    $(".hidden").hide();

	// open and close hidden element (variable)
	$("a.slideTarget").click(function(event) {
		event.preventDefault();
		$($(this).attr("href")).slideToggle("slow");
	});

    // replace button link with span of same optic to prevent double clicking
    $(".preventDoubleClick.button").click(function(){
        var disabledLink = '<span id="' + $(this).attr("id") + '" class="' + $(this).attr("class") + '">' + $(this).text() + '</span>';
        $(this).replaceWith(disabledLink);
    });

	// disable submit button to prevent double submission
	// ATTENTION: using this stops transmission of submit-button value, so check if form-data was sent has to be changed
	$(".preventDoubleClick.submit").click(function(event){

	       // only disable field, if form is valid (jQuery Validator); and I put it inside a try-and-catch to prevent errors
	    try {
	   	    if ($(this).parent("form").valid()) {
	               $(this).attr("disabled","disabled");
	   	    }
	       } catch(err) {
	           $(this).attr("disabled","disabled");
	       }

	});

	// add zebra-stripes to odd cells in .zebra tables (via .each so each table starts new)
	$("table.zebra").each(function(){
		$(this).find("tbody tr:odd td").css("background-color", "#eee");
	});

	// defaultValue of input form (lighter color and get replaced)
	// Written by Rob Schmitt, The Web Developer's Blog (http://webdeveloper.beforeseven.com/)
	$("input.defaultValue").css("color", inactive_color);
	var defaultValues = new Array();
	$("input.defaultValue").focus(function() {
	  if (!defaultValues[this.id]) {
	    defaultValues[this.id] = this.value;
	  }
	  if (this.value == defaultValues[this.id]) {
	    this.value = '';
	    this.style.color = active_color;
	  }
	  $(this).blur(function() {
	    if (this.value == '') {
	      this.style.color = inactive_color;
	      this.value = defaultValues[this.id];
	    }
	  });
	});

	// standard dialog for displaying a confirmation-message
	$('a.confirm').click(function(){
	  return confirm("Sind Sie sicher?");
	});

	// generic class for go back to previous page
    $('a.goBack').click(function(event){
		event.preventDefault();
		history.back();
	});

	/*
	// show newsletter-box
	$("input#email").focus(function(){

		if ($("#detailsNewsletter").attr("display") == "hidden"){
			$("#detailsNewsletter p").hide();
			$("[name=newsletter[submit]]").hide();
		} else {
			$("#pseudoLinkMore").hide();
		}
		$("#detailsNewsletter").show("slow", function(){
			$("#detailsNewsletter p").slideDown();
			$("[name=newsletter[submit]]").show();
		});

	})
	*/

});
