
var Cartorder =
	{
		init: function()
		{
			$('#cartorder-form .order-send').click(function(){
				Cartorder.submit($('#cartorder-form'));
				return false;
			})
			

		},
		submit: function(e)
		{	
			$.ajax({
				  type: $(e).attr('method'),
				  data: $(e).serialize(),
				  url: $(e).attr('action'),
				  dataType: 'json',
				  cache: false,
				  beforeSend: function()
				  {
				        var hasError = false;

				        var nameVal = $("#cartorder-form #firstname");
				        if(nameVal.val() == nameVal.attr('alt') || nameVal.val() == '') {
				            hasError = true;
				        }
				        				
				        var telVal = $("#cartorder-form #telephone");
				        if(telVal.val() == telVal.attr('alt') || telVal.val() == '') {
				            hasError = true;
				        }

				        if(hasError == true) 
				        { 
				        	$(".your-data .cartorder-error-all").show();
				        	return false; 
				        }
				        
				        return true;
				      
				  },
				  success: function(response)
				  {
					  $(".your-data .cartorder-error-all").hide();

					if(response.success)
					{
						document.location = response.location;
					} else
					{

						$.each(response.errors, function(key, item){
						  var el = $('#cartorder-form #'+key);
						  if(el.length)
						  {
							  el.addClass('warning');
						  } else
						  {
							  $('.your-data .cartorder-error-all').show();
						  }
						});

					}
				  },
				  error: function()
				  {
					  $('.your-data .cartorder-error-all').text('Internal Error');
					  $('.your-data .cartorder-error-all').show();
				  }

				});

				return false;
			
		}
}

