//===redirect user based on navigation selection
function fViewProductDetails(vItemID) {
	self.location.href = "catalog.asp?itemid="+ vItemID
}



//===make sure shopping cart is not empty
function fCheckCartItems(vThisForm) {
	if (document[vThisForm].emptycart) {
		alert("There are no items in your shopping cart at this time.")
		return(false)
	}
}



//===make sure shopping cart is not empty
function fCheckCartQuantities(vItemCount) {
	for (i = 1; i < vItemCount + 1; i++) {
		var vThisField = "quantity" +vItemCount
		var vThisNumber = document.shoppingcart[vThisField].value
		if (isNaN(vThisNumber)) {
			alert("The field -quantity- must be numeric.")
			return (false)
		} else if (!(isNaN(vThisNumber))) {
			if (document.shoppingcart[vThisField].value < 1) {
				alert("The field -quantity- must be a whole number (ie. 1 or greater).")
				return (false)
			} else if (document.shoppingcart[vThisField].value > 1) {
				var vOldNumber = document.shoppingcart[vThisField].value
				var vNewNumber = parseInt(vOldNumber).toString()
				if (vOldNumber.length == vNewNumber.length && vNewNumber != "NaN") {
					return (true)
				} else {
					alert("The field -quantity- must be a whole number (ie. 1 or greater).")
					return (false)
				}
			}
		}
	}
}



//===validate forms before submission
function fValidate(vThisForm){
	
	//===search site
	if (vThisForm == "searchcatalog") {
		if (document[vThisForm].criteria.value == "") {
			alert("The field -criteria- cannot be left blank.")
			return(false)
		}
	}
	
	//===add items to cart
	if (vThisForm == "additemtocart") {
		var vThisNumber = document[vThisForm].quantity.value
		if (isNaN(vThisNumber)) {
			alert("The field -quantity- must be numeric.")
			return (false)
		} else if (!(isNaN(vThisNumber))) {
			if (document[vThisForm].quantity.value < 1) {
				alert("The field -quantity- must be a whole number (ie. 1 or greater).")
				return (false)
			} else if (document[vThisForm].quantity.value > 1) {
				var vOldNumber = document[vThisForm].quantity.value
				var vNewNumber = parseInt(vOldNumber).toString()
				if (vOldNumber.length == vNewNumber.length && vNewNumber != "NaN") {
					return (true)
				} else {
					alert("The field -quantity- must be a whole number (ie. 1 or greater).")
					return (false)
				}
			}
		}
	}
	
	//===check out
	if (vThisForm == "checkoutstep1") {
		if (document[vThisForm].email.value == "") {
			alert("The field -email- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].password.value == "") {
			alert("The field -password- cannot be left blank.")
			return(false)
		}
	}
	if (vThisForm == "checkoutstep2") {
		if (document[vThisForm].accountid.value == "") {
			if (document[vThisForm].createnewcustomer.checked == true) {
				if (document[vThisForm].password.value == "") {
					alert("New Customers: \n\n The field -password- cannot be left blank.")
					return(false)
				}
			}
		}	
		var vThisEmail = new String(document[vThisForm].email.value)
		if (document[vThisForm].email.value == "" || vThisEmail.search("@") == -1) {
			alert("Billing Information: \n\n The field -email- cannot be left blank and must be properly formatted.")
			return(false)
		}
		if (document[vThisForm].firstname.value == "") {
			alert("Billing Information: \n\n The field -firstname- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].lastname.value == "") {
			alert("Billing Information: \n\n The field -lastname- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].address.value == "") {
			alert("Billing Information: \n\n The field -address- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].city.value == "") {
			alert("Billing Information: \n\n The field -city- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].state.selectedIndex < 1) {
			alert("Billing Information: \n\n The field -state- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].state.options[document[vThisForm].state.selectedIndex].value == "Other" && document[vThisForm].province.value == "") {
			alert("Billing Information: \n\n The field -province- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].state.options[document[vThisForm].state.selectedIndex].value != "Other" && document[vThisForm].province.value != "") {
			alert("Billing Information: \n\n You must first select -other- in the field State in order to submit a province.")
			return(false)
		}
		if (document[vThisForm].zip.value == "") {
			alert("Billing Information: \n\n The field -zip- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].country.value == "") {
			alert("Billing Information: \n\n The field -country- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].phone.value == "") {
			alert("Billing Information: \n\n The field -phone- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].shippingsameasbilling.checked == false) {
			if (document[vThisForm].shippingfirstname.value == "") {
				alert("Shipping Information: \n\n The field -firstname- cannot be left blank.")
				return(false)
			}
			if (document[vThisForm].shippinglastname.value == "") {
				alert("Shipping Information: \n\n The field -lastname- cannot be left blank.")
				return(false)
			}
			if (document[vThisForm].shippingaddress.value == "") {
				alert("Shipping Information: \n\n The field -address- cannot be left blank.")
				return(false)
			}
			if (document[vThisForm].shippingcity.value == "") {
				alert("Shipping Information: \n\n The field -city- cannot be left blank.")
				return(false)
			}     
			if (document[vThisForm].shippingstate.selectedIndex < 1) {
				alert("Shipping Information: \n\n The field -state- cannot be left blank.")
				return(false)
			}
			if (document[vThisForm].shippingstate.options[document[vThisForm].shippingstate.selectedIndex].value == "Other" && document[vThisForm].shippingprovince.value == "") {
				alert("Shipping Information: \n\n The field -province- cannot be left blank.")
				return(false)
			}
			if (document[vThisForm].shippingstate.options[document[vThisForm].shippingstate.selectedIndex].value != "Other" && document[vThisForm].shippingprovince.value != "") {
				alert("Shipping Information: \n\n You must first select -other- in the field State in order to submit a province.")
				return(false)
			}
			if (document[vThisForm].shippingzip.value == "") {
				alert("Shipping Information: \n\n The field -zip- cannot be left blank.")
				return(false)
			}
			if (document[vThisForm].shippingcountry.value == "") {
				alert("Shipping Information: \n\n The field -country- cannot be left blank.")
				return(false)
			}
			if (document[vThisForm].shippingphone.value == "") {
				alert("Shipping Information: \n\n The field -phone- cannot be left blank.")
				return(false)
			}
		}
		if (document[vThisForm].cardholder.value == "") {
			alert("Credit Card Information: \n\n The field -holdername- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].cardbillingzip.value == "") {
			alert("Credit Card Information: \n\n The field -billingzip- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].cardtype.selectedIndex < 1) {
			alert("Credit Card Information: \n\n The field -type- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].cardnumber.value == "") {
			alert("Credit Card Information: \n\n The field -number- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].cardexpdate.value == "") {
			alert("Credit Card Information: \n\n The field -expdate- cannot be left blank.")
			return(false)
		}
		
//		if (document[vThisForm].cardexpdate.value == "" || document[vThisForm].cardexpdate.value.length < 5 || document[vThisForm].cardexpdate.value.charAt(2) != "/") {
//			alert("Credit Card Information: \n\n The field -expdate- cannot be left blank and must be entered in the following format: mm/yy.")
//			return(false)
//		}
//			for (i = 0; i < document[vThisForm].cardexpdate.value.length; i++) {
//				if (i != 2) {
//					var thisChar = new Number(document[vThisForm].cardexpdate.value.charAt(i))
//					if (isNaN(thisChar)) {
//						alert("Credit Card Information: \n\n The field -expdate- must be numeric and entered in the following format: mm/yy.")
//						return(false)
//					}
//				}
//			}
//			var vMonth = new Number(document[vThisForm].cardexpdate.value.charAt(0) * 10 + document[vThisForm].cardexpdate.value.charAt(1) * 1)
//			//var vDay = new Number(document[vThisForm].cardexpdate.value.charAt(3) * 10 + document[vThisForm].cardexpdate.value.charAt(4) * 1)
//			if (vMonth > 12) {
//				alert("Credit Card Information: \n\n The value for the Month in the field -expdate- is not valid.")
//				return(false)
//			}
			//if (vDay > 31) {
			//	alert("Credit Card Information: \n\n The value for the Day in the field -expdate- is not valid.")
			//	return(false)
			//}
			//if (vMonth != 1 && vMonth != 3 && vMonth != 4 && vMonth != 5 && vMonth != 6 && vMonth != 7 && vMonth != 8 && vMonth != 9 && vMonth != 10 && vMonth != 11 && vMonth != 12 && vDay > 29) {
			//	alert("Credit Card Information: \n\n The value for the Day in the field -expdate- is not valid. This month cannot have more than 29 days.")
			//	return(false)
			//}
			//if (vMonth != 1 && vMonth != 3 && vMonth != 5 && vMonth != 7 && vMonth != 8 && vMonth != 10 && vMonth != 12 && vDay > 30) {
			//	alert("Credit Card Information: \n\n The value for the Day in the field -expdate- is not valid. This month cannot have more than 30 days.")
			//	return(false)
			//}		
	}
	
	//===search site
	if (vThisForm == "orderstatus_search") {
		if (document[vThisForm].email.value == "") {
			alert("The field -email- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].authorizationnumber.value == "") {
			alert("The field -authorizationnumber- cannot be left blank.")
			return(false)
		}
	}
	
	//===add / edit accounts
	if (vThisForm == "addaccounts" || vThisForm == "editaccounts") {
		if (document[vThisForm].accounttype.selectedIndex < 1) {
			alert("Account Information: \n\n The field -accounttype- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].username.value == "") {
			alert("Account Information: \n\n The field -username- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].password.value == "") {
			alert("Account Information: \n\n The field -password- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].state.options[document[vThisForm].state.selectedIndex].value == "Other" && document[vThisForm].province.value == "") {
			alert("Billing Information: \n\n The field -province- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].state.options[document[vThisForm].state.selectedIndex].value != "Other" && document[vThisForm].province.value != "") {
			alert("Billing Information: \n\n You must first select -other- in the field State in order to submit a province.")
			return(false)
		}
		if (document[vThisForm].shippingstate.options[document[vThisForm].shippingstate.selectedIndex].value == "Other" && document[vThisForm].shippingprovince.value == "") {
				alert("Shipping Information: \n\n The field -province- cannot be left blank.")
				return(false)
		}
		if (document[vThisForm].shippingstate.options[document[vThisForm].shippingstate.selectedIndex].value != "Other" && document[vThisForm].shippingprovince.value != "") {
			alert("Shipping Information: \n\n You must first select -other- in the field State in order to submit a province.")
			return(false)
		}
		if (document[vThisForm].cardnumber.value != 1) {
			var vThisNumber = ""
			for (i = 1; i < document[vThisForm].cardnumber.value.length; i++) {
				vThisNumber = vThisNumber + document[vThisForm].cardnumber.value.charAt(i)
			}
			//alert("vThisNumber = " +vThisNumber)
			if (isNaN(vThisNumber)) {
				alert("The field -cardnumber- must be numeric.")
				return (false)
			}
		}
		if (document[vThisForm].cardexpdate.value != "") {
			if (document[vThisForm].cardexpdate.value.length < 5 || document[vThisForm].cardexpdate.value.charAt(2) != "/") {
				alert("Credit Card Information: \n\n The field -expdate- must be entered in the following format: mm/yy.")
				return (false)
			}
			for (i = 0; i < document[vThisForm].cardexpdate.value.length; i++) {
				if (i != 2) {
					var thisChar = new Number(document[vThisForm].cardexpdate.value.charAt(i))
					if (isNaN(thisChar)) {
						alert("Credit Card Information: \n\n The field -expdate- must be numeric and entered in the following format: mm/yy.")
						return (false)
					}
				}
			}
			var vMonth = new Number(document[vThisForm].cardexpdate.value.charAt(0) * 10 + document[vThisForm].cardexpdate.value.charAt(1) * 1)
			if (vMonth > 12) {
				alert ("Credit Card Information: \n\n The value for the Month in the field -expdate- is not valid.")
				return (false)
			}
		}
	}
	
	//===add / edit categories
	if (vThisForm == "addcategories" || vThisForm == "editcategories") {
		if (document[vThisForm].name.value == "") {
			alert("The field -name- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].description.value == "") {
			alert("The field -description- cannot be left blank.")
			return(false)
		}
	}
	
	//===add / edit items
	if (vThisForm == "additems" || vThisForm == "edititems") {
		if (document[vThisForm].category.selectedIndex < 1) {
			alert("The field -category- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].model.value == "") {
			alert("The field -model- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].price.value != "") {
			if (isNaN(document[vThisForm].price.value)) {
				alert("The field -price- must be numeric.")
				return (false)
			}
		}
		if (document[vThisForm].price.value <= 0) {
			alert("The field -price- must be greater than 0.")
			return(false)
		}
		if (document[vThisForm].shipping.value != "") {
			if (isNaN(document[vThisForm].shipping.value)) {
				alert("The field -shipping- must be numeric.")
				return (false)
			}
		}
		if (document[vThisForm].shipping.value <= 0) {
			alert("The field -shipping- must be greater than 0.")
			return(false)
		}
	}
	
	//===update orders status
	if (vThisForm == "updateorderstatus") {
		if (document[vThisForm].filled.selectedIndex < 1) {
			alert("Order Information: \n\n The field -orderfilled- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].filled.options[document[vThisForm].filled.selectedIndex].value == "Filled" && document[vThisForm].approved.options[document[vThisForm].approved.selectedIndex].value != "Yes") {
			alert("Order Information: \n\n Orders cannot be considered filled if they have not yet been approved.")
			return(false)
		}
		if (document[vThisForm].filled.options[document[vThisForm].filled.selectedIndex].value == "Not Filled" && document[vThisForm].reasonnotfilled.value == "") {
			alert("Order Information: \n\n The field -reasonnotfilled- cannot be left blank.")
			return(false)
		}
		var thisEmail = new String(document[vThisForm].email.value)
		if (document[vThisForm].email.value == "" || thisEmail.match( "@" ) == null) {
			alert("Billing Information: \n\n The field -email- cannot be left blank and must be properly formatted.")
			return(false)
		}
		if (document[vThisForm].firstname.value == "") {
			alert("Billing Information: \n\n The field -firstname- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].lastname.value == "") {
			alert("Billing Information: \n\n The field -lastname- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].address.value == "") {
			alert("Billing Information: \n\n The field -address- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].city.value == "") {
			alert("Billing Information: \n\n The field -city- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].state.selectedIndex < 1) {
			alert("Billing Information: \n\n The field -state- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].state.options[document[vThisForm].state.selectedIndex].value == "Other" && document[vThisForm].province.value == "") {
			alert("Billing Information: \n\n The field -province- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].state.options[document[vThisForm].state.selectedIndex].value != "Other" && document[vThisForm].province.value != "") {
			alert("Billing Information: \n\n You must first select -other- in the field State in order to submit a province.")
			return(false)
		}
		if (document[vThisForm].zip.value == "") {
			alert("Billing Information: \n\n The field -zip- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].country.value == "") {
			alert("Billing Information: \n\n The field -country- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].phone.value == "") {
			alert("Billing Information: \n\n The field -phone- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].shippingsameasbilling.checked == false) {
			if (document[vThisForm].shippingfirstname.value == "") {
				alert("Shipping Information: \n\n The field -firstname- cannot be left blank.")
				return(false)
			}
			if (document[vThisForm].shippinglastname.value == "") {
				alert("Shipping Information: \n\n The field -lastname- cannot be left blank.")
				return(false)
			}
			if (document[vThisForm].shippingaddress.value == "") {
				alert("Shipping Information: \n\n The field -address- cannot be left blank.")
				return(false)
			}
			if (document[vThisForm].shippingcity.value == "") {
				alert("Shipping Information: \n\n The field -city- cannot be left blank.")
				return(false)
			}     
			if (document[vThisForm].shippingstate.selectedIndex < 1) {
				alert("Shipping Information: \n\n The field -state- cannot be left blank.")
				return(false)
			}
			if (document[vThisForm].shippingstate.options[document[vThisForm].shippingstate.selectedIndex].value == "Other" && document[vThisForm].shippingprovince.value == "") {
				alert("Shipping Information: \n\n The field -province- cannot be left blank.")
				return(false)
			}
			if (document[vThisForm].shippingstate.options[document[vThisForm].shippingstate.selectedIndex].value != "Other" && document[vThisForm].shippingprovince.value != "") {
				alert("Shipping Information: \n\n You must first select -other- in the field State in order to submit a province.")
				return(false)
			}
			if (document[vThisForm].shippingzip.value == "") {
				alert("Shipping Information: \n\n The field -zip- cannot be left blank.")
				return(false)
			}
			if (document[vThisForm].shippingcountry.value == "") {
				alert("Shipping Information: \n\n The field -country- cannot be left blank.")
				return(false)
			}
			if (document[vThisForm].shippingphone.value == "") {
				alert("Shipping Information: \n\n The field -phone- cannot be left blank.")
				return(false)
			}
		}
		if (document[vThisForm].cardholder.value == "") {
			alert("Credit Card Information: \n\n The field -holdername- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].cardbillingzip.value == "") {
			alert("Credit Card Information: \n\n The field -billingzip- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].cardtype.selectedIndex < 1) {
			alert("Credit Card Information: \n\n The field -type- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].cardnumber.value == "") {
			alert("Credit Card Information: \n\n The field -number- cannot be left blank.")
			return(false)
		}
		if (document[vThisForm].cardexpdate.value == "") {
			alert("Credit Card Information: \n\n The field -expdate- cannot be left blank.")
			return(false)
		}
		
		if (document[vThisForm].cardexpdate.value == "" || document[vThisForm].cardexpdate.value.length < 5 || document[vThisForm].cardexpdate.value.charAt(2) != "/") {
			alert("Credit Card Information: \n\n The field -expdate- cannot be left blank and must be entered in the following format: mm/yy.")
			return(false)
		}
		for (i = 0; i < document[vThisForm].cardexpdate.value.length; i++) {
			if (i != 2) {
				var thisChar = new Number(document[vThisForm].cardexpdate.value.charAt(i))
				if (isNaN(thisChar)) {
					alert("Credit Card Information: \n\n The field -expdate- must be numeric and entered in the following format: mm/yy.")
					return(false)
				}
			}
		}
		var vMonth = new Number(document[vThisForm].cardexpdate.value.charAt(0) * 10 + document[vThisForm].cardexpdate.value.charAt(1) * 1)
		//var vDay = new Number(document[vThisForm].cardexpdate.value.charAt(3) * 10 + document[vThisForm].cardexpdate.value.charAt(4) * 1)
		if (vMonth > 12) {
			alert("Credit Card Information: \n\n The value for the Month in the field -expdate- is not valid.")
			return(false)
		}
	}
	
	//===contact info
	if (vThisForm == "contactinfo") {
		if (document[vThisForm].name.value == "") {
			alert("The field -name- cannot be left blank.")
			return(false)
		}
		var vThisEmail = document[vThisForm].email.value
		if (document[vThisForm].email.value == "" || vThisEmail.search("@") == -1) {
			alert("The field -email- cannot be left blank and must be properly formatted.")
			return(false)
		}
	}
}



//===prompt user before deleting records
function fConfirmDeletion() {
	if (!(confirm("This record will be permanently removed from the database. \n\n Are you sure you wish to delete this record?"))) {
		return(false)
	}
}



//===print page
function fPrint() {
	if (window.print) {
		setTimeout('window.print()',1)
	} else if (agt.indexOf("mac") != -1) {
		alert("Press 'Cmd + P' on your keyboard to print this order.")
	} else {
		alert("Press 'Ctrl + P' on your keyboard to print this order.")
	}
}
