﻿/// <reference path="jquery-1.4.1-vsdoc.js" />
// validate that entry in all numeric text boxes is a number
$jq(document).ready(function() {
	$jq(".numerictextbox").live('keydown', function(event) {
		// Allow only backspace and delete
		if (event.keyCode == 46 || event.keyCode == 8) {
			// let it happen, don't do anything 
		}
		else {
			// Ensure that it is a number and stop the keypress
			if (event.keyCode < 48 || event.keyCode > 57 && event.keyCode < 96 || event.keyCode > 105) {
				event.preventDefault();
			}
		}
	});

	$jq('.paginator a').click(function() {
	    $jq('#page').val($jq(this).attr('page'));
	});
});

// validate that text entry field is numeric
function IsNumeric(input) {
	return (input - 0) == input && input.length > 0;
}

function addToCart(productId, quantity, callbackFunction) {
	if (!IsNumeric(quantity))
		quantity = 1;
	var test = '#qty' + productId;
	$jq('#qty' + productId).val('');
	$jq.ajax({
		url: "/ProductCatalog/AddToCart/" + productId + "/" + quantity,
		type: "Post",
		data: "",
		contentType: "application/json; charset=utf-8",
		async: true,
		cache: false,
		dataType: "json",
		success: function(data) {
			loadProductGrid();
			loadMiniCart();
			if (callbackFunction)
				callbackFunction(data);
		},
		error: function(data) {
			alert("Unhandled Error Occured");
		}
	});
}

function getProductsToAdd() {
	var items = new Array();
	$jq('.productQty').each(function() {
		if ($jq(this).val().length > 0) {
			var item = {};
			var productId = $jq(this).attr('productId');
			item.ProductId = productId;
			item.Color = $jq('#productColor[productid="' + productId + '"]').val();
			item.Size = $jq('#size-list[productid="' + productId + '"]').val();
			item.Quantity = $jq(this).val();
			items.push(item);
			$jq(this).val("");
		}
	});
	if (items.length == 0) {
		$jq('.productQty').each(function() {
			var item = {};
			var productId = $jq(this).attr('productId');
			item.ProductId = productId;
			item.Color = $jq('#productColor[productid="' + productId + '"]').val();
			item.Size = $jq('#size-list[productid="' + productId + '"]').val();
			item.Quantity = "1";
			items.push(item);
		});
	}
	return JSON.stringify(items);
}

function addAllToCart(jsonString, callbackFunction) {
	$jq.ajax({
		url: "/ProductCatalog/AddAllToCart?jsonString=" + jsonString,
		type: "Post",
		data: "",
		contentType: "application/json; charset=utf-8",
		async: true,
		cache: false,
		dataType: "json",
		success: function(data) {
			loadMiniCart();
			if (callbackFunction)
				callbackFunction(data);
		},
		error: function(data) {
			alert("Unhandled Error Occured");
		}
	});
}

function saveWishList(wishListId, jsonString, callbackFunction) {
	$jq.ajax({
		url: "/WishList/SaveWishList/" + wishListId + "?jsonString=" + jsonString,
		type: "Post",
		data: "",
		contentType: "application/json; charset=utf-8",
		async: true,
		cache: false,
		dataType: "json",
		success: function(data) {
			if (callbackFunction)
				callbackFunction(data);
		},
		error: function(data) {
			alert("Unhandled Error Occured");
		}
	});
}


function removeFromWishList(wishListId, wishListProductId, callbackFunction) {
	$jq.ajax({
		url: "/WishList/RemoveWishListProduct/" + wishListId + "/" + wishListProductId,
		type: "Post",
		data: "",
		contentType: "application/json; charset=utf-8",
		async: true,
		cache: false,
		dataType: "json",
		success: function(data) {
			if (callbackFunction)
				callbackFunction(data);
		},
		error: function(data) {
			alert("Unhandled Error Occured");
		}
	});
}

function removeWishList(wishListId, callbackFunction) {
	$jq.ajax({
		url: "/WishList/Remove/" + wishListId,
		type: "Post",
		data: "",
		contentType: "application/json; charset=utf-8",
		async: true,
		cache: false,
		dataType: "json",
		success: function(data) {
			if (callbackFunction)
				callbackFunction(data);
		},
		error: function(data) {
			alert("Unhandled Error Occured");
		}
	});
}

function addToWishList(wishListName, productId, quantity, callbackFunction) {
	if (!IsNumeric(quantity))
		quantity = 1;

	$jq.ajax({
		url: "/WishList/AddProductToWishlist/" + wishListName + "/" + productId + "/" + quantity,
		type: "Post",
		data: "",
		contentType: "application/json; charset=utf-8",
		async: true,
		cache: false,
		dataType: "json",
		success: function(data) {
            //wtf was this?
		},
		error: function(data) {
			alert("Unhandled Error Occured");
		}
	});
}

function quickOrderFindProduct(productNum, quantity, rowDOM, callbackFunction) {
	if (quantity.length == 0)
		quantity = 1;

	$jq.ajax({
		url: "/QuickOrder/FindProduct/" + productNum + "/" + quantity,
		type: "Post",
		data: "",
		contentType: "application/json; charset=utf-8",
		async: true,
		cache: false,
		dataType: "json",
		success: function(data) {
			if (callbackFunction)
				callbackFunction(data, rowDOM);
		},
		error: function(data) {
			alert("Unhandled Error Occured");
		}
	});
}

function submitOnEnter(textBox, e) {

	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;

	if (keycode == 13) {
		textBox.form.submit();
		return false;
	}
	else
		return true;
}

function loadMiniCart() {
	$jq.ajax({
		url: '/MiniCart',
		async: true,
		cache: false,
		success: function(data) {
			$jq('#minicart').html(data);
		},
		error: function(data) {
			alert("Unhandled Error Occured");
		}
	});
}

function loadProductGrid() {
	$jq.ajax({
		url: '/OrderGrid',
		async: true,
		cache: false,
		success: function(data) {
			$jq('#cartgrid').html(data);
		},
		error: function(data) {
			alert("Unhandled Error Occured");
		}
	});
}

function loadWishListGrid(wishListId) {
	$jq.ajax({
		url: '/WishList/WishListGrid/' + wishListId,
		async: true,
		cache: false,
		success: function(data) {
			$jq('#wishlistgrid').html(data);
		},
		error: function(data) {
			alert("Unhandled Error Occured");
		}
	});
}

function loadOrderHistoryGrid(pageSize, page, sort, jsonString) {
	if (jsonString != null) {
		$jq.ajax({
			url: '/Account/OrderHistoryGrid/' + pageSize + '/' + page + '/' + sort + '?jsonString=' + jsonString,
			async: true,
			cache: false,
			success: function(data) {
				$jq('#orderhistorygrid').html(data);
			},
			error: function(data) {
				alert("Unhandled Error Occured");
			}
		});
	}
	else {
		$jq.ajax({
			url: '/Account/OrderHistoryGrid/' + pageSize + '/' + page + '/' + sort,
			async: true,
			cache: false,
			success: function(data) {
				$jq('#orderhistorygrid').html(data);
			},
			error: function(data) {
				alert("Unhandled Error Occured");
			}
		});
	}

}

function loadInvoiceHistoryGrid(pageSize, page, sort, jsonString) {
	if (jsonString != null) {
		$jq.ajax({
			url: '/Account/InvoiceHistoryGrid/' + pageSize + '/' + page + '/' + sort + '?jsonString=' + jsonString,
			async: true,
			cache: false,
			success: function(data) {
				$jq('#invoicehistorygrid').html(data);
			},
			error: function(data) {
				alert("Unhandled Error Occured");
			}
		});
	}
	else {
		$jq.ajax({
			url: '/Account/InvoiceHistoryGrid/' + pageSize + '/' + page + '/' + sort,
			async: true,
			cache: false,
			success: function(data) {
				$jq('#invoicehistorygrid').html(data);
			},
			error: function(data) {
				alert("Unhandled Error Occured");
			}
		});
	}

}

function changePage(page) {
	var form = document.getElementById('paramForm');
	form.page.value = page;
	form.submit();
}

function changePageSize(obj) {
	var form = document.getElementById('paramForm');
	form.page.value = "1";
	form.pageSize.value = obj.options[obj.selectedIndex].value;
	form.submit();
}

function changeSortBy(obj) {
	var form = document.getElementById('paramForm');
	form.page.value = "1";
	form.sortby.value = obj.options[obj.selectedIndex].value;
	form.submit();
}

function changeFilter(obj) {
	var form = document.getElementById('paramForm');
	var selectedValue = obj.options[obj.selectedIndex].value;
	if (selectedValue == "") {
		for (var i = 0; i < obj.options.length; i++) {
			var val = obj.options[i].value.replace(/^\s+|\s+$/g, "");
			if (val.length == 0) {
				continue;
			}
			if (form.filters.value.indexOf(val) >= 0) {
				form.filters.value = form.filters.value.replace(val, "");
			}
		}
		var valueArray = form.filters.value.split(',');
		var newValue = "";
		for (var j = 0; j < valueArray.length; j++) {
			if (valueArray[j].replace(/^\s+|\s+$/g, "").length != 0) {
				newValue += valueArray[j];
			}
		}
	}
	else {
		form.filters.value = form.filters.value.length == 0 ? selectedValue : form.filters.value + ',' + selectedValue;
	}
	form.page.value = "1";
	form.submit();
}

function changeCategory(id) {
	var form = document.getElementById('paramForm');
	form.page.value = "1";
	form.categoryId.value = id;
	form.submit();
}


function clearFilters() {
	var form = document.getElementById('paramForm');
	form.filters.value = "";
	form.page.value = "1";
	form.submit();
}
function isValidEmail(emailStr) {
	var emailRegEx = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	return emailRegEx.test(emailStr);
}
function isValidZip(zipStr) {
	var zipRegEx = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
	return zipRegEx.test(zipStr);
}
function searchSumbitOnClick() {
	var theForm = document.forms["searchform"];
	var isValid = validateSearch();
	if (isValid == true)
		theForm.submit();
	else
		theForm.criteria.focus();
}
function validateSearch() {
	var theForm = document.forms["searchform"];
	var criteria = theForm.criteria.value.replace(/^\s+|\s+$/g, "");
	return criteria.length != 0 && criteria.toLowerCase() != 'search';
}


function subscribeSubmit() {
	var theForm = document.forms["subscribeForm"];
	var email = theForm.email.value.replace(/^\s+|\s+$/g, "");
	var isValid = isValidEmail(email);
	if (isValid == true) {
		var postData = { backurl: window.location.pathname, email: email };
		$jq.ajax({
			url: "/Account/SubscribeToNewsletter/" + email,
			type: "Post",
			data: postData,
			contentType: "application/json; charset=utf-8",
			async: true,
			cache: false,
			dataType: "json",
			success: function(data) {
				if (data.URL)
					window.location = data.URL;
				else {
					if (data == 'Already Subscribed') {
						tb_show("Message", "?TB_inline&height=180&width=250&inlineId=subscription_already&modal=true", null);
						theForm.email.value = "";

					}
					else if (data == 'Successfully Subscribed') {
						tb_show("Message", "?TB_inline&height=180&width=250&inlineId=subscription_done&modal=true", null);
						theForm.email.value = "";
					}
					else {
						tb_show("Message", "?TB_inline&height=180&width=250&inlineId=subscription_done&modal=true", null);
						theForm.email.value = "";
					}
				}


			},
			error: function(data) {
				tb_show("Message", "?TB_inline&height=180&width=250&inlineId=ajax_error&modal=true", null);
			}
		});
	}
	else {
		tb_show("Message", "?TB_inline&height=180&width=250&inlineId=subscription_invalidemail&modal=true", null);
	}

	return false;
}

function subscribeOnClick() {
	var theForm = document.forms["subscribeForm"];
	var email = theForm.email.value.replace(/^\s+|\s+$/g, "");
	var isValid = isValidEmail(email);
	if (isValid == true) {
		var postData = { backurl: window.location.pathname, email: email };
		$jq.ajax({
			url: "/Account/SubscribeToNewsletter/" + email,
			type: "Post",
			data: postData,
			contentType: "application/json; charset=utf-8",
			async: true,
			cache: false,
			dataType: "json",
			success: function(data) {
				if (data.URL)
					window.location = data.URL;
				else {
					if (data == 'Already Subscribed') {
						tb_show("Message", "?TB_inline&height=180&width=250&inlineId=subscription_already&modal=true", null);
						theForm.email.value = "";

					}
					else if (data == 'Successfully Subscribed') {
						tb_show("Message", "?TB_inline&height=180&width=250&inlineId=subscription_done&modal=true", null);
						theForm.email.value = "";
					}
					else {
						tb_show("Message", "?TB_inline&height=180&width=250&inlineId=subscription_done&modal=true", null);
						theForm.email.value = "";
					}
				}
			},
			error: function(data) {
				tb_show("Message", "?TB_inline&height=180&width=250&inlineId=ajax_error&modal=true", null);
			}
		});
	}
	else {
		tb_show("Message", "?TB_inline&height=180&width=250&inlineId=subscription_invalidemail&modal=true", null);
	}
}
     
