// Cart JavaScript Document
// Requirements : JQUERY


/** 
Add item to cart by adding its info to the cart session using AJAX 
*/
function Add_To_Cart( product_id, color, size, qty, msg, lang ) {
			  
		var qty = Math.round ( Number( qty ) );
		
		$.ajax( { 
				type: "POST", 
				url: "cart/add.php", 
				data: ({ product_id : product_id, color : color, size : size, qty : qty } ),
				success: function(){
						$('#cart_msg').text( "(" + qty + " " + msg + ")" );
						$('#cart_image').html( '<a href="main.php?section=cart"><img src="images/cart_' + lang + '.gif" border="0" alt="" /></a>' );
				}
	 });

}


/** 
Delete item from cart by removing its index from the session cart array using AJAX 
*/
function Delete_From_Cart( index, msg ) {
  
		if ( confirm( msg ) ){
				$.ajax( { 
						type: "POST", 
						url: "cart/delete.php", 
						data: ({ index : index } ),
						success: function(){
								window.location = 'main.php?section=cart';
						}
				});
		} else {
			 return false;
		}

}


/** 
Validate the checkout form 
 TO REMOVE IF NOT USE!!!
*/
function Checkout_Validation ( card_number_msg, expiry_date_msg, date_format_msg ){
	 
		var expiry_date = $('#credit_card_expiry_date').val();
		var date_format = new RegExp("[0-9]{2}[\/\-/][0-9]{2}", "i");
  if ( expiry_date != "" && !date_format.test( expiry_date ) ){
			 alert( date_format_msg );
				$('#credit_card_expiry_date').focus();
				return false;
		} else {
				var expiry_chunks = expiry_date.split("/");
				var expiry_month = expiry_chunks[0];
				var expiry_year = expiry_chunks[1];
		}

		var today = new Date();
		var max_date = new Date();
  max_date.setFullYear( '20' + expiry_year, expiry_month, 1 );
		
		if ( $('#credit_card_number').val() != "" && ( isNaN( $('#credit_card_number').val() ) || $('#credit_card_number').val().length < 16 ) ){
			 alert( card_number_msg );
				$('#credit_card_number').focus();
		} else if ( today > max_date ) {
			 alert( expiry_date_msg );
				$('#credit_card_expiry_date').focus();
		} else {
			$('#checkout').submit()
		}
	
}


