$.fn.extend
(
	{
		User: 
		{
			OnSearch: function()
			{
				$("#SearchForm").validate({
					rules : {
						search_string: { required: true, minlength: 2 }
					},
					messages : {
						search_string: '<label>Введите хотя бы 2 символа!</label>'
					}
				});
			},
			OnMakingOrder: function()
			{
				$("#MakingOrderForm").validate({
					rules : {
						name: { required: true },
						surname: { required: true },
						personal_code: { required: true },
						country: { required: true },
						city: { required: true },
						address: { required: true },
						box: { required: true },
						phone: { required: true },
						
						email : {
							required: true,
							email:true
						}
					},
					messages : {
						name: '<span class="label">Это поле должено быть заполнено!</span>',
						surname: '<span class="label">Это поле должено быть заполнено!</span>',
						personal_code: '<span class="label">Это поле должено быть заполнено!</span>',
						country: '<span class="label">Это поле должено быть заполнено!</span>',
						city: '<span class="label">Это поле должено быть заполнено!</span>',
						address: '<span class="label">Это поле должено быть заполнено!</span>',
						box: '<span class="label">Это поле должено быть заполнено!</span>',
						phone: '<span class="label">Это поле должено быть заполнено!</span>',
						email: '<span class="label">Пожалуйста, введите корректный e-mail адрес!</span>'
					}
				});
			},
			PutGoodToBasket:function(Sku)
			{
				$.ajax({
				  type: 'GET',
				  url: 'http://www.gemstor.lv/ajax/ajax_user.php?action=PutGoodToBasket',
				  data: 
					'Sku='+Sku,
				  success: function(msg){
					if(msg=='1')
					{
						document.getElementById("PutInBasketDone").style.display="inline";
						setTimeout('document.getElementById("PutInBasketDone").style.display="none"', 2000);
					}
					if(msg=='0')
					{
						document.getElementById("PutInBasketNot").style.display="inline";
						setTimeout('document.getElementById("PutInBasketNot").style.display="none"', 2000);
					}
				  }
				});
				$.ajax({
				  type: 'GET',
				  url: 'http://www.gemstor.lv/ajax/ajax_user.php?check=GoodsCountInBasket',
				  success: function(msg){
					$("#CountOfGoodsInBasket").html(msg);
				  }
				});
			},
			PutGoodToBasketNew:function(Sku)
			{
				$("#user .text").removeClass("hidden");
				$.ajax({
				  type: 'GET',
				  url: 'http://www.gemstor.lv/ajax/ajax_user.php?action=PutGoodToBasketNew',
				  data: 
					'Sku='+Sku,
				  success: function(msg){
					if(msg=='0') {
						
						document.getElementById("PutInBasketNot").style.display="inline";
						setTimeout('document.getElementById("PutInBasketNot").style.display="none"', 2000);
						
					} else {
						
						var product = eval('(' + msg + ')');
						
						$("#PutInBasketDone .price .number").html(product.Price);
						$("#PutInBasketDone .title").html(product.MainTitle);
						$("#PutInBasketDone .sku .insert").html(product.Sku);
						
						$("#PutInBasketDone .image img").attr("src", product.gallery.thumbs[0]);
						
						$("#CountOfGoodsInBasket").html(product.TotalCount);

						document.getElementById("PutInBasketDone").style.display="block";
						//setTimeout('document.getElementById("PutInBasketDone").style.display="none"', 2000);
						
					}
				  }
				});
			},
			RemoveGoodFromWishList:function(Id)
			{
				document.getElementById("WishListGoodId_"+Id).style.display="none";
				$.ajax({
				  type: 'GET',
				  url: 'http://www.gemstor.lv/ajax/ajax_user.php?action=RemoveGoodFromWishList',
				  data: 
					'Id='+Id,
				  success: function(msg){
				  }
				});
				$.ajax({
				  type: 'GET',
				  url: 'http://www.gemstor.lv/ajax/ajax_user.php?check=CountOfGoodsInWishList',
				  success: function(msg){
					if(msg==0) {
						$(".table-of-products").hide();
					}
					document.getElementById("CountOfGoodsInWishList").innerHTML=msg;
					document.getElementById("CountOfGoodsInWishListBig").innerHTML=msg;
				  }
				});
			},
			PutGoodToWishList:function(Sku)
			{
				$.ajax({
				  type: 'GET',
				  url: 'http://www.gemstor.lv/ajax/ajax_user.php?action=PutGoodToWishList',
				  data: 
					'Sku='+Sku,
				  success: function(msg){
					
					if(msg=='1')
					{
						document.getElementById("PutInWishListDone").style.display="inline";
						setTimeout('document.getElementById("PutInWishListDone").style.display="none"', 2000);
					}
					if(msg=='0')
					{
						document.getElementById("PutInWishListNot").style.display="inline";
						setTimeout('document.getElementById("PutInWishListNot").style.display="none"', 2000);
					}
				  }
				});
				$.ajax({
				  type: 'GET',
				  url: 'http://www.gemstor.lv/ajax/ajax_user.php?check=CountOfGoodsInWishList',
				  success: function(msg){
					document.getElementById("CountOfGoodsInWishList").innerHTML=msg;
				  }
				});
			},
			PutGoodToWishListNew:function(Sku)
			{
				$.ajax({
				  type: 'GET',
				  url: 'http://www.gemstor.lv/ajax/ajax_user.php?action=PutGoodToWishListNew',
				  data: 
					'Sku='+Sku,
				  success: function(msg){
					
					if(msg=='0') {
						document.getElementById("PutInWishListNot").style.display="inline";
						setTimeout('document.getElementById("PutInWishListNot").style.display="none"', 2000);
					} else {
						
						var product = eval('(' + msg + ')');
						
						$("#PutInWishListDone .price .number").html(product.Price);
						$("#PutInWishListDone .title").html(product.MainTitle);
						$("#PutInWishListDone .sku .insert").html(product.Sku);
						
						$("#PutInWishListDone .image img").attr("src", product.gallery.thumbs[0]);
						
						$("#CountOfGoodsInWishList").html(product.TotalCount);
						
						document.getElementById("PutInWishListDone").style.display="block";
						//setTimeout('document.getElementById("PutInWishListDone").style.display="none"', 2000);
					}
				  }
				});
			},
			RemoveGoodFromBasket:function(Id)
			{
				$.ajax({
				  type: 'GET',
				  url: 'http://www.gemstor.lv/ajax/ajax_user.php?action=RemoveGoodFromBasket',
				  data: 
					'Id='+Id,
					  success: function(msg){
						document.getElementById("BasketGoodId_"+Id).style.display="none";
						oldTotalPrice = $("#total_price_span").html();
						oldTotalProductsPrice = $("#totalPriceOfProducts").html();
						if(msg) {
							$("#__price").val(oldTotalPrice - msg);
							$("#total_price_span").html(oldTotalPrice - msg);
							$("#totalPriceOfProductsBottom").html(oldTotalProductsPrice - msg);
							
						}
					  }
				});
				$.ajax({
				  type: 'GET',
				  url: 'http://www.gemstor.lv/ajax/ajax_user.php?check=GoodsCountInBasket',
				  success: function(msg){
					if(msg==0) {
						$(".table-of-products").hide();
					}
					document.getElementById("CountOfGoodsInBasket").innerHTML=msg;
					document.getElementById("CountOfGoodsInBasketBig").innerHTML=msg;
				  }
				});
			},
			OnPasswordRemind: function()
			{
				$("#PasswordRemind").validate({
					rules : {
						email : {
							required: true,
							email:true,
							remote : "http://www.gemstor.lv/ajax/ajax_user.php?check=email_to_remind"
						}
					},
					messages : {
						email: '<span class="label">Пожалуйста, введите корректный e-mail адрес!</span>'
					}
				});
			},
			OnRegister: function()
			{
				$("#RegForm").validate({
					rules : {
						name: { required: true },
						
						surname: { required: true },
						
						password: { required: true },
						
						password_confirm: { 
							required: true,
							equalTo: "#password"
						},
						agree : {
							required: true
						},
						email : {
							required: true,
							email:true,
							remote : "http://www.gemstor.lv/ajax/ajax_user.php?check=email_exists"
						}
					},
					messages : {
						name: '<span class="label">Это поле должено быть заполнено!</span>',
						surname: '<span class="label">Это поле должено быть заполнено!</span>',
						password: '<span class="label">Это поле должено быть заполнено!</span>',
						password_confirm: '<span class="label">Не соответствует полю пароль</span>',
						email: '<span class="label">Пожалуйста, введите корректный e-mail адрес!</span>',
						agree: '<span class="label">Чтобы зарегистрироваться нужно ваше согласие!</span>'
					}
				});
			},
			OnChangePassword: function()
			{
				$("#ChangePasswordForm").validate({
					rules : {
						old_password: { required: true, remote : "http://www.gemstor.lv/ajax/ajax_user.php?check=password_ok" },
						new_password: { required: true }
					},
					messages : {
						old_password: '<span class="label">Wrong!</span>'
					}
				});
			},
			OnChange: function()
			{
				$("#ChangeForm").validate({
					rules : {
						name: { required: true },
						
						surname: { required: true },
						
						personal_code: { required: true },
						
						email : {
							required: true,
							email:true,
							remote : "http://www.gemstor.lv/ajax/ajax_user.php?check=email_exists"
						}
					},
					messages : {
						name: '<span class="label">Это поле должено быть заполнено!</span>',
						surname: '<span class="label">Это поле должено быть заполнено!</span>',
						personal_code: '<span class="label">Это поле должено быть заполнено!</span>',
						email: '<span class="label">Пожалуйста, введите корректный e-mail адрес!</span>'
					}
				});
			}
	}});
