// Prevents double submission of forms
var submitCount = 0;
$(document).ready(function() {
	$("form").submit(function() {
		if (submitCount == 0) {
			submitCount++;
			return true;
		} else {
			return false;
		}
	});
});

function submitOnce(form) {
	$(form).submit();
}

function submitCustomerData(form, skipValidation) {
	$(form).find("input[name=skipValidation]").val(skipValidation);
	submitOnce(form);
}

function submitOrder(form, removeInvalidVouchers) {
	$(form).find("input[name=removeInvalidVouchers]").val(removeInvalidVouchers);
	submitOnce(form);
}

// Clears default value from password field
function clearPassword(field, togglePasswordMeter) {
	defaultValue = "$>;|#%*q8_~'^)#";
	if ($(field).is(".dummy")) {
		$(field).blur(function() {
			if ($(field).val() == "") {
				$(field).val(defaultValue);
				$(field).addClass("dummy");
				if (togglePasswordMeter) {
					$(field).parent().siblings(".password-meter").hide();
				}
			}
		});
		$(field).removeClass("dummy");
		$(field).val("");
	} else {
		$(field).unbind("blur");
	}
}

// Restricts text area to maximum number of rows and columns
function checkTextArea(elem, maxRows, maxCols) {
	linebreak = '\n';
	textArray = elem.val().split(linebreak);
	cleanedUp = new Array();
	for(var i = 0; i < maxRows; i++) {
		if(textArray[i] != null) {
			cleanedUp[i] = textArray[i].slice(0, maxCols)
		}
	}
	elem.val(cleanedUp.join(linebreak));
}

// Dropdowns
$(document).ready(function() {
	$(".dropdown_list li").click(function() {
		// im dropdown "titel" soll das stehen, was im aktuellen element steht
		$(this).parents(".dropdown_sub").siblings(".dropdown_title").html($(this).html());
		// das hidden input field soll den wert des aktuellen elements bekommen
		$(this).parents(".dropdown_sub").siblings("input[type=hidden]").val($(this).children(".hidden").text());
		$(this).parents(".dropdown_sub").siblings("input[type=hidden]").change();
		// das dropdownmenü soll wieder zuklappen
		$(this).parents(".dropdown_sub").slideUp("fast");
	});
});

// Ein- und ausklappen des dropdowns
function dropdown(element){
	var dropdownList = $(element).siblings(".dropdown_sub");
	if (dropdownList.css("display") == "none"){
		if (dropdownList.height() > 400) {
			dropdownList.height("400px");
		}
		dropdownList.width($(element).parent(".dropdown").width() - 10);
	}
	dropdownList.slideToggle("fast");
}

// Country select fields
function retrieveCountries(evt) {
	var countryListHtml = '';

	evt = (evt) ? evt : ((event) ? event : null);

	if (evt) {
		var charCode = (evt.charCode) ? evt.charCode : evt.keyCode;
		var typedChar = String.fromCharCode(charCode);
		if (charCode == 192) {
			typedChar = "Oe";
		}
		if (charCode == 222) {
			typedChar = "Ae";
		}
	}

	$(document).ready(function() {
		$("#typedChar").html(typedChar);

		$.getJSON("AjaxRetrieveCountriesAction.action?retrieveCountries=",{prefix: $("#typedChar").text(), ajax: "true"}, function(c){
			$("#countries").empty();

			var i = 0;
			for (item in c) {
				if (c[item] != ''){
					i++;
				}
			}

			if (i > 0) {
				var item;
				countryListHtml = "<div class=\"dropdown\" style=\"z-index:11;\">";

				var i = 0;
				var check = false;
				for (item in c) {
					if (i == 0 && !check) {
						countryListHtml += "<div class=\"dropdown_title\" onclick=\"dropdown(this)\">" +
							"<div id=\"item\" onclick=\"javascript:rejectInputField();\" style=\"width: 150px\">" + c[item] + "</div><div class=\"hidden\">" + item + "</div></div>" +
							"<div class=\"dropdown_sub\">" +
							"<ul class=\"dropdown_list_countries\">";
						break;
					}
				}
				for (item2 in c) {
					countryListHtml += "<li>" + c[item2] + "<div class=\"hidden\">" + item2 + "</div></li>";
				}

				countryListHtml += "</ul>" +
					"</div>" +
					"<input type=\"hidden\" name=\"address.country\" value=\"" + item + "\" />" +
					"</div>";

				$("#countries").html(countryListHtml);

				$(".dropdown_list_countries li").click(function() {
					var newHtml = "<div id=\"item\" onclick=\"javascript:rejectInputField();\" style=\"width: 150px\">" + $(this).html() + "</div>";
					$(this).parents(".dropdown_sub").siblings(".dropdown_title").html(newHtml);
					$(this).parents(".dropdown_sub").siblings("input[type=hidden]").val($(this).children(".hidden").text());
					$(this).parents(".dropdown_sub").slideUp("fast");
				});
			} else {
				$("#countries").html("<input type=\"text\" id=\"country\" name=\"address.country\" onkeyup=\"javascript:retrieveCountries(event);\" />");
			}
		});
	});
}

function rejectInputField() {
	$(document).ready(function(){
		$("#countries").empty();
		$("#countries").html("<input type=\"text\" id=\"country\" name=\"address.country\" onkeyup=\"javascript:retrieveCountries(event);\" />");
		$("#country").focus();
	});
}

$(document).ready(function() {
	$('#comments').maxlength({ 'useInput' : true });
	$('#empfehlen_text').maxlength({ 'useInput' : true });
});

