
jQuery(function (){
	jQuery("input.qtyInput").live("keydown", function (e) { 
		var key = e.charCode || e.keyCode || 0;
		// allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
		return (
		key == 13 ||
		key == 8 || 
		key == 9 ||
		key == 46 ||
		(key >= 37 && key <= 40) ||
		(key >= 48 && key <= 57) ||
		(key >= 96 && key <= 105));
	});
});
function isPositiveNumber(inStr){
	//var numRegex = !/^-?\d+$/;
	return /^\d+$/.test(inStr);
}

$(document).ready(function () {
	

	$('.qtyInput').focus(function(){
		if (this.value == this.defaultValue){
			$(this).select();
		}
	});
	var qtyAjaxDelay = undefined;
	
	$('form.qtyForm').live('submit',function(){
											 
		var sPath = window.location.href;
		var myArray = sPath.split('/');
		var isLoggedin = false;
		if( myArray[3].length == 0 || myArray[3] == null) {
			myArray[3] = 'product-list.asp';
		}	
											 
		var checkLoggedIn = $('#isLoggedIn').val();		
		if (checkLoggedIn == 0) {
			window.location = 	'login.asp?next='+encodeURIComponent(myArray[3]);		
		}
		else {
			isLoggedin = true;	
		}
		
		var idProduct = $(this).find("input[name='idProduct']").val();
		var qty = $(this).find("input[name='qty']").val();
		var action = $(this).attr('action');
		
		

		var errorMsgs = new Array();
		var x = 0;

		if (!isPositiveNumber(qty)) {
			errorMsgs[x] = 'Please enter a valid qty.';
			x++;
		}else{
			if (parseInt(qty) == 0){
				errorMsgs[x] = 'Please enter a valid qty.';
				x++;				
			}
		}

		if (errorMsgs.length == 0 && isLoggedin == true){
			//alert($(this).html());
			$(this).parents(".priceTagCTA").find(".priceDefault").hide();
			$(this).parents(".priceTagCTA").find(".priceLoader").show();
			//$(this).parent(".priceTagCTA").replaceWith('<div class="price loading"></div></div>')
			var t = this;
			if (qtyAjaxDelay !== undefined){clearTimeout(qtyAjaxDelay);}
				qtyAjaxDelay = setTimeout(function(){
					$.ajax({type: "POST",
							url: action,
							dataType: 'json',
							data: { 'ajaxapp'	: 'addtocart',
									'idProduct'	: idProduct,
									'qty'	: qty },
							beforeSend: function() {
								//$('#qty' + idProduct).val('');
							},
							success: function(r) {

								var status		= r.root.status
								var statusCode	= r.root.statusCode
								var statusMsg	= r.root.statusMsg
								
									if (status){
										if (r.root.cartQty){
											$('.headerStatusCart strong').text(r.root.cartQty);
										}
										$(t).parents(".priceTagCTA").find(".priceAdded").show();
										$(t).parents(".priceTagCTA").find(".priceLoader").hide();
										
										var idOrder = r.root.idOrder;								
										var Params = { 
												'ajaxapp'		: 'updateCart',
												'idOrder'		: idOrder
											};
										$.ajax({type: "POST",
											dataType : 'html',
											url: "ajax-callback.asp",
											data : Params,
											beforeSend  : function(){
												//$('ul.productList').addClass('loading');
											},
											success: function(r) {
												$('#sidebar').html(r);
											}
										 });
										
										
									}else{
										
										$(t).parents(".priceTagCTA").find(".priceLoader").hide();
										$(t).parents(".priceTagCTA").find(".priceDefault").show();
										alert("Error - " + statusMsg.replace('\u005Cn','\n'));
									}
							
							},
							error: function() {
								
								alert('Problem updating. Please try again.');
							}
					});					
									
				},300);			
		}
		return false;
	});
	
	
	//Search functions and display
	function doSearch() {
				//alert('test');
				var action = $('#prodSearch').attr('action');
				var prodFilter = 	$('#productFilter').val();
				var prodSearch = $('#productSearch').val();
				var prodSort = $('#productSort').val();
				var idCategory = $('input[name="idCategory"]').val();
				var isParent = $('input[name="parent"]').val();					
				if ($('input[name="parent"]').val() == 'True' ) {
				 isParent = 1;
				}
				else {
					isParent = 0;
				}
								
				var searchParams = { 
					'ajaxapp'		: 'showProdList',
					'productFilter'	: prodFilter,
					'productSort'	: prodSort,
					'idCategory'	: idCategory,
					'productSearch'	: prodSearch,
					'idParent'		: isParent
				};
				
					if (qtyAjaxDelay !== undefined){clearTimeout(qtyAjaxDelay);}
					$('ul.productList').addClass('loading');
					qtyAjaxDelay = setTimeout(function(){
						$.ajax({type: "GET",
								dataType : 'html',
								url: "product-list.asp",
								data: searchParams,
								beforeSend  : function(){
									//$('ul.productList').addClass('loading');
								},
								success: function(r) {
									$('ul.productList').removeClass('loading');	
									$('#main').html(r);
									$('.productList > li:nth-child(3n-2)').addClass('f');
									$('.productList > li:nth-child(3n)').addClass('l');
									// Set first row
									$('.productList > li:nth-child(1), .productList > li:nth-child(2), .productList > li:nth-child(3)').addClass('firstRow');
								},
								error: function() {
									alert('Problem updating. Please try again.');
								}
						});
					},400);	
					
				}
				
				$('#productFilter').live('change',function() {
					doSearch();
					//return false;
				});	
				
				$('#productSort').live('change',function() {
					doSearch();
					//return false;
				});
				
				$('#prodSearch').live('submit',function() {
					doSearch();
					return false;
				});
	
	
});

