jQuery(document).ready(function() {	   
	
    $(".addToCart").click(function () { 
    	
    	if($(".addToCart").attr('id')!='' && $(".addToCart").attr('id')>0) {
    		  jQuery.ajax({
		        type: "POST",
		        url: "/ru/order/",
		        data: "id="+$(".addToCart").attr('id')+'&action=add',
		        async: false,
		        cache: false,
		        success: function(msg){
		        	
		        	$('.infoMessage').html(msg);	
		        		        
			        for(i = 0; i < 3; i++)
					{
					  $(".infoMessage").fadeIn(400);
					  $(".infoMessage").fadeOut(200);
					}
					
                     $(".infoMessage").fadeIn(400);
			       // $('.infoMessage').fadeOut(5000); 
			        
			    }
			   });
    	}
    	
    	return false;
    });
    
    //передача данных на сервер, если покидает корзину
  
    
   $(".productNum").keyup( function(event){
     
       //check if was entered number
      if (!((event.keyCode >= 27 && event.keyCode<= 57) || event.keyCode == 46) && !( event.keyCode>=96 && event.keyCode<=106)) {
       //alert($(this).attr('value').substr(0,$(this).attr('value').length - 1));
        var newValue = $(this).attr('value').substr(0,$(this).attr('value').length - 1);
        if(newValue.length==0) newValue = 0;
        $(this).attr('value', newValue);
      }

      var pId = $(this).attr('id');   
      $total = $(".mprice_"+pId).html() *  $(this).attr('value');
      $("#tmprice_"+pId).html($total);
       
      jQuery.ajax({
        type: "POST",
        url: "/ru/order/",
        data: "id="+pId+'&action=edit'+'&qty='+$(this).attr('value'),
        success: function(msg){}
	   });		 
	  refrechCart(); 
	  
    });
    
    
    //delete product from cart
    $(".removeProcuct").bind( 'click', function(){
    	
     if(confirm("Вы уверены?")){
     	
     	$('#block_'+$(this).attr('id')).toggle(1000);
     	$('#block_'+$(this).attr('id')).remove();
     	jQuery.ajax({
	        type: "POST",
	        url: "/ru/order/",
	        data: "id="+$(this).attr('id')+'&action=del',
	        success: function(msg){}
	    });	
	    
	    $('#total_price').html('0');
     	refrechCart();
     }
      return false;
    });
    

    
    function refrechCart()
    {
      var $totalCart = 0;
      $(".tmprice").each(function(i){
		  $totalCart += +$("#"+this.id).html();		 
		 });
      if($totalCart=='' || $totalCart==0)	{
      	$('#infoRow').removeClass();
      	$('#sendOrder').hide();
      	$('#clearOrder').hide();
      } else {
      	$('#sendOrder').show();
      	$('#clearOrder').show();
      }
      
	  $("#total_price").html($totalCart);
    }
    
    //clear cart
    $("#clearOrder").click(function(){
      if(confirm("Вы уверены?")){
     	
     	$('.cartRow').each( function(){
     	  $(this).remove();
     	});
     	
     	jQuery.ajax({
	        type: "POST",
	        url: "/ru/order/",
	        data: "action=clearAll",
	        success: function(msg){}
	    });	
	    
     	$('#total_price').html('');   
     	$('#infoRow').removeClass();
     	$('#sendOrder').hide();
        $('#clearOrder').hide();
      }
    
    });  
    
    
   
    $("#cartForm").validate({
        rules : {
            fio : {required : true},
            phone : {required:true},
            email : {required:true, email: true}
        },
        messages : {
            fio : {required : errorRequiredMsg},
            phone : errorRequiredMsg,
            email:  {
                     required : errorRequiredMsg,
                     email : errorEmailFormatMsg
                    }
        }
    });
    
    
    
    $("#searchBlock").submit( function () { 
    	
    	var temp = new Array();
        
        if ($("#searchStr").attr('value')==undefined || $("#searchStr").attr('value')=='') {
        	temp = $("#searchBlock").attr('action').split('/');
            var alertMessage = 'Введите фразу для поиска!';
            if (temp[1] == 'en') alertMessage = 'Fill the search field!';
            
        	alert(alertMessage);
        	$("#searchStr").focus();
        	return false;         	
        }

        return true;         
    });
    
    
    
    $(".orderInCatalog").click(function () { 
    	
    	if($(".orderInCatalog").attr('id')!='' && $(".orderInCatalog").attr('id')>0) {
    		  var mess = "#mess"+$(this).attr('id');
    		  
    		  jQuery.ajax({
		        type: "POST",
		        url: "/ru/order/",
		        data: "id="+$(this).attr('id')+'&action=add',
		        async: false,
		        cache: false,
		        success: function(msg) {		        	
		        	
		        	$(mess).html(msg);	
   
			        for(i = 0; i < 3; i++)
					{
					  $(mess).fadeIn(400);
					  $(mess).fadeOut(200);
					}
					
                     $(mess).fadeIn(400);
			       // $('.infoMessage').fadeOut(5000); 
			        
			    }
			   });
    	}
    	return false;
    });
    
   	
});





