
function checkCartQuantity(fld, type) {
	qty = fld.value;
	if (type == 'i') {
		var test = "0123456789";
		for (var i = 0; i < qty.length; i++) {
			if (test.indexOf(qty.charAt(i)) == -1) {
				alert('The entered quantity is not valid for the product');
				return;
			}
		}
	} else if (type == 'f') {
		var test = "0123456789.";
		for (var i = 0; i < qty.length; i++) {
			if (test.indexOf(qty.charAt(i)) == -1) {
				alert('The entered quantity is not valid for the product');
				return;
			}
		}
		var a = qty.split('.');
		if (a.length > 2) {
			alert('The entered quantity is not valid for the product');
		}
		
	}
}

function validateForm(frm) {
	emptyFields = new Array();
	var eml = stripSpaces(frm.YourEmail.value);
	if (eml.length > 0) {
		// Doing a login - check the password
		var pwd = stripSpaces(frm.YourPass.value);
		if (pwd.length == 0) {
			alert('Please enter your password to log in');
			return false;
		} else {
			return true;
		}
	} else {
		var ynm = stripSpaces(frm.YourName.value);
		if (ynm.length > 0) {
			checkFields = new Array("YourName","YourNewEmail","YourPassA","YourPassB","ContactNumber","DeliveryAddress");
			displayFields = new Array("Your Name","Email Address","Your Password","Repeat Your Password","Contact Number","Delivery Address");
			for (var i = 0; i < checkFields.length; i++) {
				val = stripSpaces(frm.elements[checkFields[i]].value);
				if (val.length == 0) {
					emptyFields[emptyFields.length] = displayFields[i];
				}
			}
			if (emptyFields.length > 0) {
				var msg = "The following fields were not filled in:\n";
				for (var i = 0; i < emptyFields.length; i++) {
					msg += emptyFields[i] + "\n";
				}
				msg += "Please fill them in and try again.";
				alert(msg);
				return false;
			} else if (frm.YourPassA.value != frm.YourPassB.value) {
				alert('Your password and the repeat password do not match. Please try again.');
				return false;
			} else {
				return true;
			}
		} else {
			alert('Please enter either the details to log in, or details to create a new account');
			return false;
		}
	}
}

function stripSpaces(inputStr) {
	//Strip leading and trailing spaces
	var x = inputStr;
	while (x.substring(0,1) == ' ') {
		x	= x.substring(1);
	}
	while (x.substring(x.length-1,x.length) == ' ') {
		x = x.substring(0,x.length-1);
	}
	return x;
}

function confirmPass(fld) {
	if (fld.value != fld.form.YourPassA.value) {
		alert('The entered passwords do not match. Please try again.');
	} else if (fld.value == "") {
		alert('Please ensure you enter a password and repeat password');
	}
}

function validatePasswords(frm) {
	var pwd = stripSpaces(frm.YourPassA.value);
	if (pwd.length == 0) {
		alert('Your password must contain a value');
		return false;
	} else if (frm.YourPassA.value != frm.YourPassB.value) {
		alert('Your password and the repeat password do not match. Please try again.');
		return false;
	} else {
		return true;
	}
}

