function checkCorrectInput(formName) {
	if(isNaN(document.forms[formName].txtQty.value)) {
		alert("Please enter a valid number");
		return false;
	}
	if(document.forms[formName].txtQty.value == "") {
		alert("Please enter a number");
		return false;
	}
	return true;
}
function checkStockLevels(checkType, allowOutOfStock, typeName, typeID) {
	if(checkType == 1) {
		if(!checkCorrectInput('frmItem')) {return false;}
		var wanted = document.forms['frmItem'].txtQty.value;
		
		if(document.forms['frmItem'].cmbVar.value.substr(0, 7) == 'variant') {
			var variant = document.forms['frmItem'].cmbVar.value.substr(8);
			
			jQuery.ajax({
				type: "GET",
				url: "/_script/checkStockVariant.asp",
				dataType: "html",
				data: "varID=" + variant + "&wanted=" + wanted,
				success: function(response){
					if(response.search(/true/) != -1) {
						document.frmItem.submit();
					}
					else {
						if(allowOutOfStock) {
							var con = confirm('This item is currently out of stock.\nIf you order this product it may be sent at a separate time from any other orders.\nAre you sure you want to add this to your basket?');
							if(con) {
								document.frmItem.submit();
							}
						}
						else {
							alert('We do not have enough of this product in stock to satisfy your order.\nPlease adjust the amount you want to add to your basket.');
						}
					}
				},
				error: function(xhr){
					alert("Error occured during Ajax request, the error status is: " + xhr.status);
				}
			});
		}
		else if(document.forms['frmItem'].cmbVar.value.substr(0, 8) == 'quantity') {
			var quantity = document.forms['frmItem'].cmbVar.value.substr(9);
			
			jQuery.ajax({
				type: "GET",
				url: "/_script/checkStockQuantity.asp",
				dataType: "html",
				data: "quanID=" + quantity + "&wanted=" + wanted,
				success: function(response){
					if(response.search(/true/) != -1) {
						document.frmItem.submit();
					}
					else {
						if(allowOutOfStock) {
							var con = confirm('This item is currently out of stock.\nIf you order this product it may be sent at a separate time from any other orders.\nAre you sure you want to add this to your basket?');
							if(con) {
								document.frmItem.submit();
							}
						}
						else {
							alert('We do not have enough of this product in stock to satisfy your order.\nPlease adjust the amount you want to add to your basket.');
						}
					}
				},
				error: function(xhr){
					alert("Error occured during Ajax request, the error status is: " + xhr.status);
				}
			});
		}
	}
	else if(checkType == 2) {
		if(!checkCorrectInput('frmItem')) {return false;}
		var variant = document.forms['frmItem'].singleVarID.value;
		var wanted = document.forms['frmItem'].txtQty.value;
		
		jQuery.ajax({
			type: "GET",
			url: "/_script/checkStockVariant.asp",
			dataType: "html",
			data: "varID=" + variant + "&wanted=" + wanted + "&singleVar=1",
			success: function(response){
				if(response.search(/true/) != -1) {
					document.frmItem.submit();
				}
				else {
					if(allowOutOfStock) {
						var con = confirm('This item is currently out of stock.\nIf you order this product it may be sent at a separate time from any other orders.\nAre you sure you want to add this to your basket?');
						if(con) {
							document.frmItem.submit();
						}
					}
					else {
						alert('We do not have enough of this product in stock to satisfy your order.\nPlease adjust the amount you want to add to your basket.');
					}
				}
			},
			error: function(xhr){
				alert("Error occured during Ajax request, the error status is: " + xhr.status);
			}
		});
	}
	else if(checkType == 3) {		
		if(typeName == 1) {
			var wanted = document.getElementById('frmQty1_' + typeID).value;
			var discount = document.getElementById('discount').value;
			var original = document.getElementById('frmQtyO1_' + typeID).value;
			
			if(!exitUpdate(wanted, original)) {
				if(isNaN(document.getElementById('frmQty1_' + typeID).value)) { alert("Please enter a valid number"); return false;}
				if(document.getElementById('frmQty1_' + typeID).value == "") { alert("Please enter a number"); return false;}
				
				jQuery.ajax({
					type: "GET",
					url: "/_script/checkStockVariant.asp",
					dataType: "html",
					data: "varID=" + typeID + "&wanted=" + wanted + "&update=1",
					success: function(response){
						if(response.search(/true/) != -1) {
							window.location.href = "/_script/updateBasketScript.asp?varID=" + typeID + "&wanted=" + wanted + "&discount=" + discount + "&typeName=" + typeName;
						}
						else {
							if(allowOutOfStock) {
								var con = confirm('This item is currently out of stock.\nIf you order this product it may be sent at a separate time from any other orders.\nAre you sure you want to add this to your basket?');
								if(con) {
									window.location.href = "/_script/updateBasketScript.asp?varID=" + typeID + "&wanted=" + wanted + "&discount=" + discount + "&typeName=" + typeName;
								}
							}
							else {
								alert('We do not have enough of this product in stock to satisfy your order.\nPlease adjust the amount you want to add to your basket.');
								document.getElementById('frmQty1_' + typeID).value = original;
							}
						}
					},
					error: function(xhr){
						alert("Error occured during Ajax request, the error status is: " + xhr.status);
					}
				});
			}
			else {
				cancelEdit(typeName, typeID);
			}
		}
		else if(typeName == 2) {
			var wanted = document.getElementById('frmQty2_' + typeID).value;
			var discount = document.getElementById('discount').value;
			var original = document.getElementById('frmQtyO2_' + typeID).value;
			
			if(!exitUpdate(wanted, original)) {
				if(isNaN(document.getElementById('frmQty2_' + typeID).value)) { alert("Please enter a valid number"); return false;}
				if(document.getElementById('frmQty2_' + typeID).value == "") { alert("Please enter a number"); return false;}
				
				jQuery.ajax({
					type: "GET",
					url: "/_script/checkStockQuantity.asp",
					dataType: "html",
					data: "quanID=" + typeID + "&wanted=" + wanted + "&update=1",
					success: function(response){
						if(response.search(/true/) != -1) {
							window.location.href = "/_script/updateBasketScript.asp?varID=" + typeID + "&wanted=" + wanted + "&discount=" + discount + "&typeName=" + typeName;
						}
						else {
							if(allowOutOfStock) {
								var con = confirm('This item is currently out of stock.\nIf you order this product it may be sent at a separate time from any other orders.\nAre you sure you want to add this to your basket?');
								if(con) {
									window.location.href = "/_script/updateBasketScript.asp?varID=" + typeID + "&wanted=" + wanted + "&discount=" + discount + "&typeName=" + typeName;
								}
							}
							else {
								alert('We do not have enough of this product in stock to satisfy your order.\nPlease adjust the amount you want to add to your basket.');
								document.getElementById('frmQty2_' + typeID).value = original;
							}
						}
					},
					error: function(xhr){
						alert("Error occured during Ajax request, the error status is: " + xhr.status);
					}
				});
			}
			else {
				cancelEdit(typeName, typeID);
			}
		}
	}
	else if(checkType == 4) {
		if(!checkCorrectInput('frmItem')) {return false;}
		var wanted = document.forms['frmItem'].txtQty.value;
		
		if(document.forms['frmItem'].cmbVar2.value.substr(0, 7) == 'variant') {
			var variant = document.forms['frmItem'].cmbVar2.value.substr(8);
			
			jQuery.ajax({
				type: "GET",
				url: "/_script/checkStockVariant.asp",
				dataType: "html",
				data: "varID=" + variant + "&wanted=" + wanted,
				success: function(response){
					if(response.search(/true/) != -1) {
						document.frmItem.submit();
					}
					else {
						if(allowOutOfStock) {
							var con = confirm('This item is currently out of stock.\nIf you order this product it may be sent at a separate time from any other orders.\nAre you sure you want to add this to your basket?');
							if(con) {
								document.frmItem.submit();
							}
						}
						else {
							alert('We do not have enough of this product in stock to satisfy your order.\nPlease adjust the amount you want to add to your basket.');
						}
					}
				},
				error: function(xhr){
					alert("Error occured during Ajax request, the error status is: " + xhr.status);
				}
			});
		}
		else if(document.forms['frmItem'].cmbVar2.value.substr(0, 8) == 'quantity') {
			var quantity = document.forms['frmItem'].cmbVar2.value.substr(9);
			
			jQuery.ajax({
				type: "GET",
				url: "/_script/checkStockQuantity.asp",
				dataType: "html",
				data: "quanID=" + quantity + "&wanted=" + wanted,
				success: function(response){
					if(response.search(/true/) != -1) {
						document.frmItem.submit();
					}
					else {
						if(allowOutOfStock) {
							var con = confirm('This item is currently out of stock.\nIf you order this product it may be sent at a separate time from any other orders.\nAre you sure you want to add this to your basket?');
							if(con) {
								document.frmItem.submit();
							}
						}
						else {
							alert('We do not have enough of this product in stock to satisfy your order.\nPlease adjust the amount you want to add to your basket.');
						}
					}
				},
				error: function(xhr){
					alert("Error occured during Ajax request, the error status is: " + xhr.status);
				}
			});
		}
	}
	
	return false;
}
function checkStockFinal(formName, allowOutOfStock) {
	jQuery.ajax({
		type: "GET",
		url: "/_script/checkStockFinal.asp",
		dataType: "html",
		success: function(response){
			if(response.search(/true/) != -1) {
				eval("document." + formName + ".submit();");
			}
			else {
				if(allowOutOfStock) {
					var con = confirm('One or more item\'s in your basket are currently out of stock\nDo you still wish to proceed with the checkout process?');
					if(con) {
						eval("document." + formName + ".submit();");
					}
				}
				else {
					alert('We are very sorry, but since we last checked the availabilty of stock against your order our stock level has changed, we now do not have enough of this product in stock to satisfy your order.\nPlease adjust the amount you want to add to your basket. Click OK below and you will be diverted back to the view basket page where you can adjust your order.');
					window.location.href = "/editbasket.asp";
				}
			}
		},
		error: function(xhr){
			alert("Error occured during Ajax request, the error status is: " + xhr.status);
		}
	});
	
	return false;
}
function exitUpdate(wanted, original) {
	if(wanted == original) {
		return true;
	}
	return false;
}
function editBasketProduct(typeName, strID) {
	
	classNameStyle('a', 'staticBasket', 'hidden');
	document.getElementById('frmQtyS' + typeName + '_' + strID).style.visibility = 'hidden';
	document.getElementById('frmQtyS' + typeName + '_' + strID).style.position = 'absolute';
	document.getElementById('frmQty' + typeName + '_' + strID).style.visibility = 'visible';
	document.getElementById('frmQty' + typeName + '_' + strID).style.position = 'relative';
	document.getElementById('update' + typeName + '_' + strID).style.visibility = 'visible';
	document.getElementById('submitButton').disabled = "disabled";
	return false;
}
function cancelEdit(typeName, strID) {
	classNameStyle('a', 'staticBasket', 'visible');
	document.getElementById('frmQtyS' + typeName + '_' + strID).style.visibility = 'visible';
	document.getElementById('frmQtyS' + typeName + '_' + strID).style.position = 'relative';
	document.getElementById('frmQty' + typeName + '_' + strID).style.visibility = 'hidden';
	document.getElementById('frmQty' + typeName + '_' + strID).style.position = 'absolute';
	document.getElementById('update' + typeName + '_' + strID).style.visibility = 'hidden';
	document.getElementById('submitButton').disabled = "";
	return false;
}
function classNameStyle(strTagName, strClassName, strContent) {
	var classDivClass = document.getElementsByTagName(strTagName);
	
	for(var i=0; i<classDivClass.length; i++) {
		if(classDivClass[i].className == strClassName) {
			classDivClass[i].style.visibility = strContent;
		}
	}
}
