$(document).ready(function() {  
	/* for initialising cart*/
	$('#rotator').show() ;
	$('#nonrotator').hide() ;
	
	/* submit with quantity validation */
	$('form.addtocart').submit(function(e) {
		e.preventDefault();
		//var qty = $('.itemqty').val();
		var retid = $('.itemid').val();
		var element = $(this);
		//
		//var engr='';
		var engr = $('form textarea').val() ;
if (engr==undefined) {
engr='';
}
		var firstoption = $('select:first').val() ;	

		var firstoptiontype = $('select:first').attr('name') ;	
		var lastoption = $('select:last').val() ;
			
		//firstoptiontype = firstoptiontype + '          ';
if (firstoptiontype==undefined) {
firstoptiontype='';
}
		firstoptiontype = firstoptiontype.substring(9);

		var lastoptiontype = $('select:last').attr('name') ;
		
		//lastoptiontype = lastoptiontype + '         ';
if (lastoptiontype==undefined) {
lastoptiontype='';
}		
		lastoptiontype = lastoptiontype.substring(9);

		//progably an easier way
		//alert(mytest);
		//alert(element);
		///////////////////

		var m_id = element.attr('id');
		id = m_id.substring(1);

		//var qty =$('.itemqty'+id).val(); // will do from cart display

		//alert(qty);
		/////////////////// assume qty = 1 to start
		$.post('/index.php/store/check_quantity', { 'progid':id, 'ret_id':retid, 'engravingdetails':engr, 'firstoption':firstoption, 'lastoption':lastoption, 'firstoptiontype':firstoptiontype, 'lastoptiontype':lastoptiontype}, function(result) {
			//$('#bad_qty').replaceWith('');
			if (result == '1') {
			//alert('Please check your entry values');
			//	$('.itemqty'+id).after('<div id="bad_qty" style="color:red;">' + 
			//	'<p>(is not an integer. Please select again.)</p></div>');
			} else {
				//alert(result) ; // yes, it’s getting here but following doesn’t work second time around
				//$('#total').replaceWith(result);
				alert('The item has been added to the cart. Cart total is ' + result);
				// removed -- make click to show total instead
			}
		});
	})

	//$('form#cartform').submit(function(f) {
		// 	DO WITHOUT AJAX
	
	//})

	
});  

