$(document).ready(function(){

	$("#konto_tab_links li").click(function(){
		if(! $(this).hasClass("active")){
			nr = $("#konto_tab_links li").index($(this));
			$(this).parent().find(".active").removeClass("active");
			$(this).addClass("active");
			$("#konto_tabs > div").hide();
			$("#konto_tabs > div:eq("+ nr +")").show();
		}
	})

	$('#rLand').sSelect({defaultText: ""});
	$('#lLand').sSelect({defaultText: ""});
		
	
/*	$("#rLand").selectdecorator({
		DIVClass : "selectholder"
	});

	$("#lLand").selectdecorator({
		DIVClass : "selectholder"
	});
	*/
	//$("#rLand").parent().find("li[val='Deutschland']").click();

	
//------reg form
	$("#btn-update-userinfo").click(function(){$("#customer_info").submit(); return false});
	var options = {
		beforeSubmit:  function() {
			if (!$("#customer_info").valid()) return false;

			show_popup("popup_update");
			$("#popup_update .ttl").html("Daten werden gesendet.");
			$("#popup_update .txt").html("Bitte warten...");
			return true;
		},
		success:  update_ok,
		error:      function(XMLHttpRequest, textStatus, errorThrown) { alert("Error processing the request")},
		url: '/profile/?mode=json_update_customer',
		type:      "post",
		dataType:  "json"
	};


	$("#customer_info").ajaxForm(options).validate({
		errorPlacement: function(error, element) {

		},

		rules: {
			rAnrede: "required",
			rVorname: "required",
			rName: "required",
			rStrasse: "required",
			rPlz: "required",
			rOrt: "required",
			rLand: "required",
			rTelefon: "required",
			Email : {required: true,  email: true}

		}
	});

	
	
//------Bank form
	$("#btn-update-bankinfo").click(function(){$("#bank_info").submit(); return false});
	var options = {
		beforeSubmit:  function() {
			if (!$("#bank_info").valid()) return false;

			show_popup("popup_update");
			$("#popup_update .ttl").html("Daten werden gesendet.");
			$("#popup_update .txt").html("Bitte warten...");
			return true;
		},
		success:  update_ok,
		error:      function(XMLHttpRequest, textStatus, errorThrown) { alert("Error processing the request")},
		url: '/profile/?mode=json_update_bank',
		type:      "post",
		dataType:  "json"
	};


	$("#bank_info").ajaxForm(options).validate({
		errorPlacement: function(error, element) {

		},

		rules: {
			AccOwner: "required",
			AccBank: "required",
			AccNumber: "required",
			AccBankCode: "required"
		}
	});

		
	

	//------password change  form
	$("#btn-update-password").click(function(){$("#password_info").submit(); return false;});
	var options = {
		beforeSubmit:  function() {
			if (!$("#password_info").valid()) return false;

			show_popup("popup_update");
			$("#popup_update .ttl").html("Daten werden gesendet.");
			$("#popup_update .txt").html("Bitte warten...");


			return true;
		},
		success:  update_ok,
		error:      function(XMLHttpRequest, textStatus, errorThrown) { alert("Error processing the request")},
		url: '/profile/?mode=json_update_password',
		type:      "post",
		dataType:  "json"
	};


	$("#password_info").ajaxForm(options).validate({
		errorPlacement: function(error, element) {

		},

		rules: {
			oldPassword: "required",
			Password: {required: true},
			Password_chk: {
				required: true,
				equalTo: "#password_info input[name='Password']"
			}

		}
	});




});


function update_ok(data) {

	if(data["error"]) {

		$("#popup_update .ttl").html("Fehler!");
		$("#popup_update .txt").html(data["error"]);

		return;
	}

	$("#popup_update .ttl").html("Mitteilung.");
	$("#popup_update .txt").html("Die Daten wurden aktualisiert.");
	fill_info(data);
	setTimeout (function(){hide_popup("popup_update", true);}, 1000);

}


