$(document).ready(function(){
//	action();
});

function register_account() {
	var email_reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var err = false;
	$('#register_account_working').show();
	$('#registration_frm .error').hide("slow").html('');

		if ($('#user_first_name').val() == '') {
			$('#user_first_name_error_log').html('Enter you first name please.').show("slow");
			err = true;
		}

		if ($('#user_last_name').val() == '') {
			$('#user_last_name_error_log').html('Enter you last name please.').show("slow");
			err = true;
		}

		if (!$('#user_email').val().match(email_reg)){
			$('#user_email_error_log').html('Email is not valid.').show("slow");
			err = true;
		} else {
			$.ajax({
				type: "GET",
				url: "/ajax/ajax_my_account.php",
				data: "action=check_email&email=" + $('#user_email').val() + "&rand=" + Math.random(),
				success: function(data){

						if (data == 'false'){
							$('#user_email_error_log').html('Email address already registred.').show("slow");
							err = true;
						}

				}
			});
		}

		if ($('#user_password1').val() < 6) {
			$('#user_password1_error_log').html('Password is too short.').show("slow");
			err = true;
		}

		if ($('#user_password2').val() == '' || $('#user_password2').val() != $('#user_password1').val()) {
			$('#user_password2_error_log').html('Wrong password.').show("slow");
			err = true;
		}
/*
		if ($('#user_captcha_code').val() == '') {
			$('#user_captcha_code_error_log').html('Enter antispam code.').show("slow");
			err = true;
		}
*/
		if (err == true) {
			$('#register_account_working').hide();
			return;
		}

	$.ajax({
		type: "GET",
		url: "/ajax/ajax_my_account.php",
		async: false,
//		data: "action=register_account&user_first_name=" + $('#user_first_name').val() + "&user_last_name=" + $('#user_last_name').val() + "&user_email=" + $('#user_email').val() + "&user_password1=" + $('#user_password1').val() + "&user_password2=" + $('#user_password2').val() + "&user_captcha_code=" + $('#user_captcha_code').val() + "&rand=" + Math.random(),
		data: "action=register_account&user_first_name=" + $('#user_first_name').val() + "&user_last_name=" + $('#user_last_name').val() + "&user_email=" + $('#user_email').val() + "&user_password1=" + $('#user_password1').val() + "&user_password2=" + $('#user_password2').val() + "&rand=" + Math.random(),
		success: function(data){

				switch ($.trim(data)) {
					case 'true': {
						$('#register_account_working').hide();
						document.location.replace('/my_account/index.html?tab=personal');
						return;
						break;
					}
					case 'wrong capcha': {
						$('#user_captcha_code_error_log').html('Wrong antispam code.').show();
						$('#user_captcha_code').val('');
						break;
					}
					case 'already registred': {
						$('#user_email_error_log').html('Email address already registred.').show("slow");
						$('#user_captcha_code').val('');
						break;
					}
					default: {}
				}

			$('#register_account_working').hide("slow");
			make_captcha('user_captcha');
		}
	});
}

function recover_password() {
	var email_reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var err = false;
	$('#recover_password_working').show();
	$('#recovery_form .error').hide().html('');

		if (!$('#recovery_email').val().match(email_reg)){
			$('#recovery_email_error_log').html('Email is not valid.').show("slow");
			err = true;
		}

		if ($('#recovery_captcha_code').val() == '') {
			$('#recovery_captcha_code_error_log').html('Enter antispam code.').show("slow");
			err = true;
		}

		if (err == true) {
			$('#recover_password_working').hide();
			return;
		}

	$.ajax({
		type: "POST",
		dataType: "json",
		async: true,
		url: "/ajax/ajax_my_account.php?action=recover_password&rand=" + Math.random(),
		data: $("#recovery_frm").serialize(),
		success: function(json){

				if (json.status == 'ERR') {

						for (i = 0; i < json.error.length; ++i) {

								if (json.error[i].field == 'captcha_code') { 
									$('#recovery_captcha_code_error_log').html(json.error[i].message).show();
									$('#recovery_captcha_code').val('');
									make_captcha('recovery_captcha');
								} else {
									$('#recovery_' + json.error[i].field + '_error_log').html(json.error[i].message).show();
								}

						}

				} else {
					$('#recovery_form').html('Check your email. A new password has been send to your email.');
				}

		}
	});

	$('#recover_password_working').hide();
}

function change_password() {
	var err = false;
	$('#change_password_working').show();
	$('#change_password_frm .error').hide().html('');
	$('#change_password_success').hide();

		if ($('#password').val().length < 6) {
			$('#change_password_error_log').html('Enter your current password.').show("slow");
			err = true;
		}

		if ($('#password1').val().length < 6) {
			$('#change_password1_error_log').html('Your new password is too short. Should be 6 characters or more. Please try again.').show("slow");
			err = true;
		}

		if ($('#password2').val() != $('#password1').val()) {
			$('#change_password2_error_log').html('Wrong password. Please retype once again.').show("slow");
			err = true;
		}

		if (err == true) {
			$('#change_password_working').hide();
			return;
		}
//ac0e07bf22ccb4b0472e8a3faeb36baa
	$.ajax({
		type: "POST",
		dataType: "json",
		url: "/ajax/ajax_my_account.php?action=change_password&rand=" + Math.random(),
		async: false,
		data: $("#change_password_frm").serialize(),
		success: function(json){

				if (json.status == 'ERR') {

						for (i = 0; i < json.error.length; ++i) {

								if (json.error[i].code == 1) {
									alert('Lig In Please.');
									document.location.replace('/index.html');
								}

							$('#change_' + json.error[i].field + '_error_log').html(json.error[i].message).show();
						}

				} else {
					$('#change_password_success').show();
				}

			$('#change_password_working').hide();
		}
	});
}

function add_support_message() {
	var err = false;
	$('#add_support_message_form .error').hide().html('');
	$('#add_support_message_success').hide();
/*
		if ($('#support_message').val() == '') {
			$('#author_error_log').html('Support Message is empty.').show("slow");
			err = true;
		}

		if (err == true) {
			return;
		}
*/
	$.ajax({
		type: "GET",
		dataType: "json",
		url: "/ajax/ajax_my_account.php?action=add_support_message&rand=" + Math.random(),
		async: true,
		data: $("#add_support_message_form").serialize(),
		success: function(json){

				if (json.status == 'OK') {
					$('#add_support_message_success').show("slow");
					$('#support_messages_list').append(json.HTML);
					$('#add_support_message_form').each(function(){ this.reset(); })
				} else {

						for (i = 0; i < json.error.length; ++i) {

								if (json.error[i].code == 1) {
									alert('Log In Please.');
									document.location.replace('/index.html');
								}

							$('#' + json.error[i].field + '_error_log').html(json.error[i].message).show();
						}

				}

			$('#update_shipping_info_working').hide(100);
		}
	});
}

function update_shipping_info(){
	$('#update_shipping_info_working').show();
	$('#update_shipping_info_success').hide("slow");
	$.ajax({
		type: "POST",
		dataType: "json",
		async: true,
		url: "/ajax/ajax_my_account.php?action=update_shipping_info&rand=" + Math.random(),
		data: $("#shipping_info_form").serialize(),
		success: function(json){

				if (json.status == 'OK') {
					$('#update_shipping_info_success').show("slow");
				} else {

						for (i = 0; i < json.error.length; ++i) {

								if (json.error[i].code == 1) {
									alert('Log In Please.');
									document.location.replace('/index.html');
								}

						}

				}

			$('#update_shipping_info_working').hide(100);
		}
	});
}

function add_to_wish_list(item_id){
	$('#add_to_wish_list_' + item_id + '_working').show();
	$.ajax({
		type: "POST",
		dataType: "json",
		async: true,
		url: "/ajax/ajax_my_account.php?action=add_to_wish_list&rand=" + Math.random(),
		data: "item_id=" + item_id,
		success: function(json){

				if (json.status == 'OK') {
					$('#add_to_wish_list_' + item_id).unbind('click').html('In Your WishList').attr("href", "/my_account/index.html?tab=wishlist").attr("class",'');
				}

		}
	});
	$('#add_to_wish_list_' + item_id + '_working').hide();
}

function remove_from_wish_list(item_id){
	$('#wishlist_item_' + item_id + '_working').show();
	$.ajax({
		type: "POST",
		dataType: "json",
		async: true,
		url: "/ajax/ajax_my_account.php?action=remove_from_wish_list&rand=" + Math.random(),
		data: "item_id=" + item_id,
		success: function(json){

				if (json.status == 'OK') {
					$('#wishlist_item_' + item_id).remove();
				}

		}
	});
	$('#add_to_wish_list_' + item_id + '_working').hide();
}

function logout() {
	$.ajax({
		type: "POST",
		dataType: "json",
		async: true,
		url: "/ajax/ajax_my_account.php?action=logout&rand=" + Math.random(),
		data: $("#logout_frm").serialize(),
		success: function(json) {

				if (json.status == 'OK') {

							switch (json.page) {

								case 'checkout': {
									$('#loginBody').html(json.HTML);
									break;
								}

								default: {
									$('#loginFrom').html(json.HTML);
									document.location.replace('/index.html');
								}

							}

				}

		}
	});
}

function login(){
	var email_reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var err = false;
	$('#login_frm .error').hide().html('');

		if (!$('#login').val().match(email_reg)){
			$('#login_box_error_log').html('Email is not valid.').show("slow");
			return;
		}

		if ($('#password').val() == '') {
			$('#login_box_error_log').html('Password field is empty.').show("slow");
			return;
		}

	$.ajax({
		type: "POST",
		dataType: "json",
		url: "/ajax/ajax_my_account.php?action=login&rand=" + Math.random(),
		async: true,
		data: $("#login_frm").serialize(),
		success: function(json) {

				switch (json.status) {
					case 'ERR': {

							for (i = 0; i < json.error.length; ++i) {
								$('#login_box_error_log').html(json.error[i].message).show('slow');
							}

						break;
					}
					case 'OK': {

							switch (json.page) {
								case 'checkout1': 
								case 'checkout': {
									$('#loginBody').html(json.HTML);

										for (i = 0; i < json.params.length; ++i) {

												if (json.params[i].field == 'Customer_Country') {
													$('#Customer_Country').selectOptions(json.params[i].value);
													$('#shipping_country').selectOptions(json.params[i].value);
													set_shopping_cart_shipping_(json.params[i].value, 'XX');

														if (json.params[i].value == '1') {
															$('#shipping_state_block').show();
														} else {
															$('#shipping_state_block').hide();
														}

												} else if (json.params[i].field == 'shipping_state') {
													$('#shipping_state').selectOptions(json.params[i].value);
												} else {
													$('#' + json.params[i].field).val(json.params[i].value);
												}

										}

									break;
								}
								default: {
									$('#loginFrom').html(json.HTML);
									document.location.replace('/my_account/index.html?tab=personal');
									return;
								}
							}

							if (window.login_action) {
								login_action();
							}
							
						break;
					}
					default: {
						$('#login_box_error_log').html('Authorisation Error. Please contact our support').show('slow');
					}
				}

		}
	});
}

function check_email(email){
	$.ajax({
		type: "GET",
		url: "/ajax/ajax_my_account.php",
		data: "action=check_email&email=" + email + "&rand=" + Math.random(),
		success: function(data){
			if (data == 'true')
				return 'true';
			else
				return 'false';
		}
	});
}

function is_logged_in(){
	return	$.ajax({
					type: "GET",
					url: "/ajax/ajax_my_account.php",
					async: false,
					data: "action=is_logged_in&rand=" + Math.random()
				}).responseText;
}
