
var qtip_position = new Object();
qtip_position['topLeft'] = 'bottomRight';
qtip_position['topMiddle'] = 'bottomMiddle';
qtip_position['topRight'] = 'bottomLeft';
qtip_position['rightTop'] = 'leftBottom';
qtip_position['rightMiddle'] = 'leftMiddle';
qtip_position['rightBottom'] = 'leftTop';
qtip_position['bottomRight'] = 'topLeft';
qtip_position['bottomMiddle'] = 'topMiddle';
qtip_position['bottomLeft'] = 'topRight';
qtip_position['leftBottom'] = 'rightTop';
qtip_position['leftMiddle'] = 'rightMiddle';
qtip_position['leftTop'] = 'rightBottom';

/*
 * Tooltips für das Hauptmenü
 */
$('[class*=qtip]').each(function()
{
   $(this).qtip({ 
	   content: $('#' + $(this).attr('id') + '_content').html(), 
	   style: {
	   		width: {min:100, max:800},
			tip: qtip_position[$('#' + $(this).attr('id') + '_content').attr('class')],
			name: 'dark'
	   } ,
       position: {
            //adjust: {screen: true},
            corner: {
            	target: $('#' + $(this).attr('id') + '_content').attr('class'),
				tooltip: qtip_position[$('#' + $(this).attr('id') + '_content').attr('class')]
            }
       },
	   show: { delay: 1000 }
   });
});

/*
 * Tooltips als Hilfeelemente
 */
$('[class*=tooltip_help]').each(function()
{
   $(this).qtip({ 
	   content: $('#' + $(this).attr('id') + '_content').html(), 
	   style: {
	   		width: {min:100, max:800},
			tip: qtip_position[$('#' + $(this).attr('id') + '_content').attr('class')],
			name: 'dark'
	   } ,
       position: {
            //adjust: {screen: true},
            corner: {
            	target: $('#' + $(this).attr('id') + '_content').attr('class'),
				tooltip: qtip_position[$('#' + $(this).attr('id') + '_content').attr('class')]
            }
       },
	   show: { delay: 0 }
   });
});

function berechne_gesamt_summe(){
	$("#id_grundpreis_netto").attr("disabled","disabled");
	$("#id_gesamtpreis_brutto").attr("disabled","disabled");
	$("#id_gesamtpreis_netto").attr("disabled","disabled");
	$("#id_grundpreis_brutto").keyup(berechne_brutto_netto)
}

function enable_input(){
	$("input:disabled").removeAttr("disabled");
}


/*
 * Berechnet die Summe aller Preisfelder und die entsprechenden Netto Beträge.
 */
function berechne_brutto_netto(){
	var summe = 0;
	var grundpreis_netto = 0;
	var grundpreis_brutto = 0;
	var gesamtpreis_brutto = 0;
	var gesamtpreis_netto = 0;
	
	insert_initial_values();
	grundpreis_brutto = parseCurrency($("#id_grundpreis_brutto").val());
	grundpreis_netto = (grundpreis_brutto / 1.19).toFixed(2);
	$("#id_grundpreis_netto").val(displayCurrency(grundpreis_netto));


	$("input")
    .filter(function() {
        return this.id.match(/(id_form-\d+-preis)/);
    })
	.each(function () {
		if (!isNaN(parseCurrency($(this).val()))){
			summe = summe + parseCurrency($(this).val());
		}
	});
	
	gesamtpreis_brutto = (summe + grundpreis_brutto).toFixed(2);
	$("#id_gesamtpreis_brutto").val(displayCurrency(gesamtpreis_brutto));

	gesamtpreis_netto = (gesamtpreis_brutto / 1.19).toFixed(2);
	$("#id_gesamtpreis_netto").val(displayCurrency(gesamtpreis_netto));
	
	
	if ($("#id_grundpreis_netto").val() == "NaN"){
		$("#id_grundpreis_netto").val("0,00");
	}
	if ($("#id_gesamtpreis_brutto").val() == "NaN"){
		$("#id_gesamtpreis_brutto").val("0,00");
	}
	if ($("#id_gesamtpreis_netto").val() == "NaN"){
		$("#id_gesamtpreis_netto").val("0,00");
	}
}


/*
 * Erstellt für jedes neue Preis Feld einen keyup Handler, 
 * so dass der Wert des Feldes in die Gesamtsumme mit einfließt. 
 */
function register_keydown_handler(){
	$("input")
    .filter(function() {
        return this.id.match(/(id_form-\d+-preis)/);
    })
	.each(function () {
		$(this).keyup(berechne_brutto_netto);
	});
	insert_initial_values();
}


/*
 * Belegt alle Preis Felder mit dem Wert 0.
 * Prüft ob die eingegebenen Werte Zahlen sind.
 */
function insert_initial_values(){
	$("input")
    .filter(function() {
        return this.id.match(/(preis)/);
    })
	.each(function () {
		if (!isCurrency($(this).val())) {
			$(this).val("");
		}
		while ($(this).val().charAt(0) == '0') {
		    if ($(this).val().length == 1 || $(this).val() == '0,00' || $(this).val() == '0,' || $(this).val() == '0,0') { break };
		    $(this).val($(this).val().substr(1, $(this).val().length-1));
		}

	});
}

function reverseCurrency(currency){
	return parseFloat(currency.replace(".", ",")); 
}

function parseCurrency(currency){
	return parseFloat(currency.replace(",", ".")); 
}

function displayCurrency(input){
	return String(input).replace(".", ","); 
}


/*
 * Prüft ob die Eingabe eine Zahl ist.
 */
function isInteger(str){
	if(typeof str != "undefined" && str!=null && str.length>0){
		if (!isNaN(str)){
	    	return true;
	    }
	}
	return false;
}

function isCurrency(str){
	if(typeof str != "undefined" && str!=null && str.length>0){
		if (!isNaN(parseCurrency(str))){
	    	return true;
	    }
	}
	return false;
}

$(document).ready(function(){
	$("#such_feld").toggleVal();
	$("input[name='email']").toggleVal();
	$("input[name='passwort']").toggleVal();
});


function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

/*
 * Recaptcha Optionen
 */
var RecaptchaOptions = {
	theme : 'white',
	lang : 'de'
};

/*
 * Ajax Funktion zum speichern einer Nachricht
 */
function nachricht_speichern(auktion_id, parent_id){
	nachricht = $('#id_nachricht').val()
	$.getJSON('/auktion/' + auktion_id + '/nachricht_speichern_ajax/', {auktionId: auktion_id, parentId: parent_id, nachrichtInhalt: nachricht}, nachricht_speichern_callback);
}

/*
 * Ajax Callback Funktion zum speichern einer Nachricht
 */
function nachricht_speichern_callback(json){
	$('#nachricht_div').html(json.response_template);
}

/*
 * Ajax Funktion zum erstellen einer Antworten-Forms
 */
function nachricht_antwort(auktion_id, parent_id){
	$.getJSON('/auktion/' + auktion_id + '/nachricht_ajax/', {auktionId: auktion_id, parentId: parent_id}, nachricht_antwort_callback);
}

/*
 * Ajax Callback Funktion zum erstellen einer Antworten-Forms
 */
function nachricht_antwort_callback(json){
	$('#nachricht_div').html(json.response_template);
}



function enddatum_berechnen(){
	$.getJSON('/auktion/erstellen/konfigurator/4/enddatum_ajax/', {auktions_dauer: $("#id_auktions_dauer").val()}, enddatum_berechnen_callback);
}

function enddatum_berechnen_callback(json){
	$('#enddatum_ajax').html(json.enddatum);
}




function call_url(url){
	window.location = url;
}


function switch_laufende_abgelaufene(url){
	if(url.search(/laufende/i)){
		url = url.replace(/laufende/i, "abgelaufene");
	} else {
		if (url.search(/laufende/i)) {
			url = url.replace(/abgelaufene/i, "laufende");
		}
	}
	window.location = url;
}
