$(document).ready(function() {
	var calc = function() {
		var value = $(this).val().replace(/,/, ".");

		if (parseFloat(value) == value) {
			value = parseFloat(value).toFixed(2);

			$(this).val(value);

			var tax = $("#taxrate option:selected").val();
			if (parseInt(tax) != tax)
				tax = $("#taxrate input").val();

			if (parseInt(tax) == tax) {
				if ($(this).is(".net")) {
					var gross = (parseFloat(value) * (1 + (parseInt(tax) / 100)));

					$(this).closest("tr").find("input.gross").val(gross.toFixed(2));
				}
				else if ($(this).is(".gross")) {
					var net = (parseFloat(value) / (1 + (parseInt(tax) / 100)));

					$(this).closest("tr").find("input.net").val(net.toFixed(2));
				}
			}
		}
		else {
			$(this).val("");
		}
	}

	var sumup = function() {
		var sumnet = 0.0;
		var sumgross = 0.0;

		var obj;
		var qty;
		var net;
		var tax;
		var gross;

		$("#basket .arcontainer tbody tr").each(function() {
			qty = parseInt($(this).find("input.quantity").val());
			tax = parseFloat($(this).find("input.taxrate").val().replace(/,/, "."));
			net = parseFloat($(this).find("input.price").val().replace(/,/, "."));
			gross = (parseFloat(net) * (1 + (parseInt(tax) / 100))).toFixed(2);

			sumnet = sumnet + qty * net;
			sumgross = sumgross + qty * gross;
		});

		if (isNaN(sumnet)) {
			$("#basketsumnet").html("<i>Angaben überprüfen</i>");
			$("#basketsumgross").html("<i>Angaben überprüfen</i>");
		}
		else {
			$("#basketsumnet").html(sumnet.toFixed(2));
			$("#basketsumgross").html(sumgross.toFixed(2));
		}
	}

	// Attach automatic net/gross calculation to input fields
	$("#pricing input.net, #pricing input.gross, #price input.net, #price input.gross").each(function() {
		$(this).blur(calc);
	});

	// Apply the calculation once at startup
	$("#pricing input.net, #pricing input.gross, #price input.net, #price input.gross").each(calc);

	$("#refreshsum").click(sumup);
	sumup();
});





/*
$(document).ready(function() {
	// Iterate through form elements and build the form field hint
	$(".field > label[for][title]").
		each(function() {
			$(this).after('<div class="hint">'+$(this).attr('title')+'</div>');
			$(this).attr('title', '');
			$(this).css('cursor', 'help');
			$(this).css('text-decoration', 'underline');

			$(this).parent().find('div[class=hint]').click(function() {
				$(this).parent().find('div[class=hint]').hide();
			});
		}).
		mouseover(function() {
			$(this).parent().find('div[class=hint]').fadeIn(200);
		}).
		mouseout(function() {
			$(this).parent().find('div[class=hint]').fadeOut(80);
		});

	var focus;

	// Iterate through all form fields...
	$("input, select, textarea").each(function() {
		if ((focus == null) || ((focus.className != "error") && this.className == "error"))
			if (this.type != "hidden")
				focus = this;
	});

	if (focus != null)
		focus.focus();

/*  EXPERIMENTAL!!!!
 *
	$("input[watermark != ''], textarea[watermark != '']").each(function() {
		var position = $(this).offset();

		$(this).before('<span class="watermark" style="position: absolute; top: '+position.top+'px; left: '+position.left+'px; margin: 6px 0 0 3px; width: '+$(this).innerWidth()+'px; color: #ccc;">'+$(this).attr('watermark')+'</span>');



		$(this).focus(function() {
			$(this).parent().find('.watermark').hide();
		});

		$(this).parent().find('.watermark').focus(function() {
			$(this).hide();
		});

		$(this).blur(function() {
			if ($(this).attr('value') == '')
				$(this).parent().find('.watermark').show();
		});
	});

});
*/

/*
function wymSiteIntegration(wym, wdw, callbackurl) {
	var ImageCallback = function(data) {
		$("#source", wdw.document).append(data);

		wdw.setTimeout(function(){
			$(".pwdoverlay", wdw.document).hide();
		}, 300);
    }

	var DialogCallback = function(data) {
		$("#source", wdw.document).append(data);

		var val = $(".wym_href", wdw.document).val();

		if (val == '') {
			$(".wym_href", wdw.document).val("http://www.");
		}
		else {
			$(".int_links > option", wdw.document).each(function(i, o){
				if (val == $(o).val()) {
					$(o).attr('selected', 'selected');
					$(".wym_href", wdw.document).attr("readonly", true);
					$(o).text().trim();
				}
			});
		}

		wdw.setTimeout(function(){
			$(".pwdoverlay", wdw.document).hide();
		}, 300);
    }

	var type = $("input.wym_dialog_type", wdw.document).val();

	switch(type) {
		case WYMeditor.DIALOG_LINK:
			$("head", wdw.document).append('<link rel="stylesheet" href="assets/wymeditor.css" type="text/css" />');
			$("body", wdw.document).prepend('<div class="pwdoverlay"></div>');

			$("legend", wdw.document).after("<div class=\"row\"><label>Link</label><select id=\"source\"><option value=\"http://www.\">Externes Ziel</option></select></div>");

			$.get("?site;ajax;links;", DialogCallback);

	    	$("#source", wdw.document).change(function() {
	    		if ($(this).val() == 'http://www.') {
	    			$(".wym_href", wdw.document).removeAttr("readonly");
	    		}
	    		else {
	    			$(".wym_href", wdw.document).attr("readonly", true);
	    			$(".wym_title", wdw.document).val($('#source :selected', wdw.document).text().trim());
	    		}

	    		$(".wym_href", wdw.document).val($(this).val());
	    	});
			break;

		case WYMeditor.DIALOG_IMAGE:
			$("head", wdw.document).append('<link rel="stylesheet" href="assets/wymeditor.css" type="text/css" />');
			$("body", wdw.document).prepend('<div class="pwdoverlay"></div>');

			$("legend", wdw.document).after("<div class=\"row\"><label>Bild</label><select id=\"source\"><option value=\"http://www.\">Externes Bild</option></select></div>");
			$(".wym_src", wdw.document).val("http://www.");

			// Get template id to forward it to the ajax request
			var tpl = parseInt($("#template").val());
			tpl = isNaN(tpl) ? "" : tpl;

			$.get("?site;ajax;images;" + tpl, ImageCallback);

	    	$("#source", wdw.document).change(function() {
	    		if ($(this).val() == 'http://www.')
	    			$(".wym_src", wdw.document).removeAttr("readonly");
	    		else
	    			$(".wym_src", wdw.document).attr("readonly", true);

	    		$(".wym_src", wdw.document).val($(this).val());
	    	});
			break;
	}
}
*/
