jQuery(document).ready(function() {
	
	
	jQuery('#login_submit').click(function() {
		if (jQuery('#login input[name=username]').val().length >= 1 && jQuery('#login input[name=username]').val() != "E-Mail" && jQuery('#login input[name=password]').val().length >= 1 && jQuery('#login input[name=password]').val() != "Passwort")
		{
			jQuery.post(
				'/login.php',
				jQuery('#login').serializeArray(),
				function(response) {
					if(response == "success")
					{
						window.location.reload();
					}
					else
					{
						jQuery('#login_falsch_meinewelt').click();
					}
				}
			);
		}
		return false;
	});
	
	jQuery('#press_login_submit').click(function() {
		if (jQuery('#presslogin input[name=username]').val().length >= 1 && jQuery('#presslogin input[name=username]').val() != "E-Mail" && jQuery('#presslogin input[name=password]').val().length >= 1 && jQuery('#presslogin input[name=password]').val() != "Passwort")
		{
			jQuery.post(
				'/presslogin.php',
				jQuery('#presslogin').serializeArray(),
				function(response) {
					if(response == "success")
					{
						window.location.reload();
					}
					else
					{
						jQuery('#login_falsch_press').click();
					}
				}
			);
		}
		return false;
	});
	
	jQuery('#submit_myPressLoginError').click(function() {
		if (jQuery('#myPressLoginError input[name=username]').val().length >= 1 && jQuery('#myPressLoginError input[name=username]').val() != "E-Mail" && jQuery('#myPressLoginError input[name=password]').val().length >= 1 && jQuery('#myPressLoginError input[name=password]').val() != "Passwort")
		{
			jQuery.post(
				'/presslogin.php',
				jQuery('#myPressLoginError').serializeArray(),
				function(response) {
					if(response == "success")
					{
						window.location.reload();
					}
					else
					{
						jQuery('#myPressLoginError input[name=username]').val("");
						jQuery('#myPressLoginError input[name=password]').val("");
						lightbox('whole','pressLoginErrorFromPressNotLoggedIn', '#login_falsch_press');
					}
				}
			);
		}
		return false;
	});
	jQuery('#submit_myPressNotLoggedIn').click(function() {
		if (jQuery('#myPressNotLoggedIn input[name=username]').val().length >= 1 && jQuery('#myPressNotLoggedIn input[name=username]').val() != "E-Mail" && jQuery('#myPressNotLoggedIn input[name=password]').val().length >= 1 && jQuery('#myPressNotLoggedIn input[name=password]').val() != "Passwort")
		{
			jQuery.post(
				'/presslogin.php',
				jQuery('#myPressNotLoggedIn').serializeArray(),
				function(response) {
					if(response == "success")
					{
						window.location.reload();
					}
					else
					{
						jQuery('#myPressNotLoggedIn input[name=username]').val("");
						jQuery('#myPressNotLoggedIn input[name=password]').val("");
						lightbox('whole','pressLoginErrorFromPressNotLoggedIn', '#lightboxOuter_PressNotLoggedIn');
					}
				}
			);
		}
		return false;
	});
	
	jQuery('#submit_myWorldLoginError').click(function() {
		if (jQuery('#myWorldLoginError input[name=username]').val().length >= 1 && jQuery('#myWorldLoginError input[name=username]').val() != "E-Mail" && jQuery('#myWorldLoginError input[name=password]').val().length >= 1 && jQuery('#myWorldLoginError input[name=password]').val() != "Passwort")
		{
			jQuery.post(
				'/login.php',
				jQuery('#myWorldLoginError').serializeArray(),
				function(response) {
					if(response == "success")
					{
						// the link to the target URL may be saved in the global variable 'loginTargetUrl'
						// this can be done in the onclick method of the link for example
						if (typeof(loginTargetUrl) != "undefined") {
							window.location = loginTargetUrl;
						} else {
							window.location.reload();
						}
					}
					else
					{
						jQuery('#myWorldLoginErrorfehlermeldung').removeClass('hideme')
						jQuery('#myWorldLoginError input[name=username]').val("");
						jQuery('#myWorldLoginError input[name=password]').val("");
					}
				}
			);
		}
		return false;
	});

	jQuery('#submit_get_new_password').click(function() {
		if (jQuery('#myWorldGetNewPasswordemail').val().length >= 1)
		{
			jQuery.post(
				'/requestpw.php',
				jQuery('#myWorldGetNewPasswordform').serializeArray(),
				function(response) {
					if(jQuery("status",response).text() == "success")
					{
						jQuery('#myWorldRecoverPassword').html(jQuery("text",response).text());
					}
					else
					{
						jQuery('#myWorldRecoverPasswordfehlermeldung').html(jQuery("text",response).text());
					}
				},
				"xml"
			);
		}
		return false;
	});
	
	//Added by nxg, deal with press forgot password
	jQuery('#press_submit_get_new_password').click(function() {
		if (jQuery('#myPressGetNewPasswordemail').val().length >= 1)
		{
			jQuery.post(
				'/press_requestpw.php',
				jQuery('#myPressGetNewPasswordemail').serializeArray(),
				function(response) {
					if(jQuery("status",response).text() == "success")
					{				
						jQuery('#myPressRecoverPassword').html(jQuery("text",response).text());
					}
					else
					{
						jQuery('#myPressRecoverPasswordfehlermeldung').html(jQuery("text",response).text());
					}
				},
				"xml"
			);
		}
		return false;
	});
	
	jQuery('#submit_change_password').click(function() {
		if (jQuery('#old_password').val().length >= 1)
		{
			if (jQuery('#new_password').val().length >= 1 && jQuery('#new_password').val() == jQuery('#new_password_confirm').val())
			{
				jQuery.post(
					'/changepw.php',
					jQuery('#myWorldChangePasswordform').serializeArray(),
					function(response) {
						if(response == "success")
						{
							jQuery('#myWorldChangePassword').html("Passwort wurde erfolgreich geändert.");
						}
						else
						{
							jQuery('#fehlermeldung_changepw').html(response);
						}
					}
				);
			}
			else
			{
				jQuery('#fehlermeldung_changepw').html("Die neuen Passwörter stimmen nicht überein.");
			}
		}
		else
		{
			jQuery('#fehlermeldung_changepw').html("Bitte bisherige Passwort eingeben.");
		}
		return false;
	});
	
	jQuery('#Press_submit_change_password').click(function() {
		if (jQuery('#old_password').val().length >= 1)
		{
			if (jQuery('#new_password').val().length >= 1 && jQuery('#new_password').val() == jQuery('#new_password_confirm').val())
			{
				jQuery.post(
					'/presschangepw.php',
					jQuery('#myPressChangePasswordform').serializeArray(),
					function(response) {
						if(response == "success")
						{
							jQuery('#myPressChangePassword_container').html("Passwort wurde erfolgreich geändert.");
						}
						else
						{
							jQuery('#Press_fehlermeldung_changepw').html(response);
						}
					}
				);
			}
			else
			{
				jQuery('#Press_fehlermeldung_changepw').html("Die neuen Passwörter stimmen nicht überein.");
			}
		}
		else
		{
			jQuery('#Press_fehlermeldung_changepw').html("Bitte bisherige Passwort eingeben.");
		}
		return false;
	});
	
	jQuery('.contact_request').click(function() {
		div_obj = jQuery(this);
		jQuery.post(
			'/contact_request.php',
			jQuery(this).parents('form').serializeArray(),
			function(response) {
				if(response == "success")
				{
					jQuery(div_obj).parents('div.myWorldAddAsContact').html("Kontaktanfrage wurde gestellt.");
				}
				else
				{
					jQuery(div_obj).parents('div.myWorldAddAsContact').html(response);
				}
			}
		);
		return false;
	});
	
	jQuery('.ir-control-length').keyup(function() {
		var maxlength = 200;
		var text = jQuery(this).val();
		if (text.length > maxlength)
		{
			jQuery(this).val(text.substring(0, maxlength));
			jQuery(this).siblings('p.error').html('Der Text wurde gekürzt. Es sind max. 200 Zeichen erlaubt.');
		}
	});
	
	jQuery('#myWorldDelUser_close').click( function(e) { lbClose(e) });
	function lbClose(e) {
		e.preventDefault();
		jQuery('#lightboxOuter_myWorldDelUser').css({ top: "-3000px" });
		jQuery("#LightBoxWrapper").fadeTo(0,0).hide();
	};

});
