memory = {
submitBtnClass: 'submitBtn',
productIdClass: 'productId',
categoryIdClass: 'categoryId',
quantityClass: 'qty',
submitBtn: '',
inputFields: [],
allLinks: [],
submitLinks: [],
init:function() {
  if (!document.getElementsByTagName || !document.getElementById) { return false; }
  memory.inputFields = document.getElementsByTagName("input");
  memory.allLinks = document.getElementsByTagName("a");
  j = 0;
  for(i=0; i<memory.allLinks.length; i++) {
    if (DOMhelp.cssjs('check',memory.allLinks[i],memory.submitBtnClass)) {
  	  memory.submitLinks[j] = memory.allLinks[i];
	  DOMhelp.addEvent(memory.submitLinks[j], 'click', memory.submitPurchase, false);
	  j = j + 1;
	}
  }
  for(i=0; i<memory.inputFields.length; i++) {
    if (DOMhelp.cssjs('check',memory.inputFields[i],memory.quantityClass)) {
  	  DOMhelp.addEvent(memory.inputFields[i], 'keyup', memory.submitPurchase, false);
	}
  }
},
validateQuantity:function(q) {
	var re = /^\d{1,4}$/;
	var errorMsg = "";
	var error = false;
	// not a number
	if (q == '') {
		error = true;
	}
	else if (q <= 0 | q > 250) {
		error = true;
	}
	else if (!re.test(q)) { 
		error = true;
	}
	if (error) {
		errorMsg = "To complete your purchase, please enter a valid quantity - a number between 1 and 250. For larger orders, please contact us at orders@edgetechcorp.com.";
		alert(errorMsg);
		return false;
	}
	else {
		return true;
	}
},
submitPurchase:function(e) {
	var obj = DOMhelp.getTarget(e);
	var cartPath;
	if (obj != 'NULL') {
		// Get the triggering object
		if (obj.nodeName.toLowerCase() == 'input') {
			var key = DOMhelp.getKey(e);
			// if this is the enter key then proceed else bail now totally
			if (key == 13) {
				// Get the id of the triggering object so that we know which one to submit
				var reNum = /\d{1,4}$/; 	// reg expression that finds a number 
				var s = String(obj.getAttribute("id")); // make the id a string for re to work
				// extract the numeric part of the id
				var targetIdNumArr = reNum.exec(s);
				// Get the full cart path
				for (i=0; i<memory.submitLinks.length; i++) {
					if (DOMhelp.cssjs('check',memory.submitLinks[i],memory.submitBtnClass)) {
						var s2 = String(memory.submitLinks[i].getAttribute("id"));
						var testIdNumArr = reNum.exec(s2);
						if (testIdNumArr[0] === targetIdNumArr[0]) {
							cartPath = String(memory.submitLinks[i].getAttribute('href'));
						}
					} 
				}
			}
			else {
				// quit and cancel everything
				DOMhelp.cancelClick(e);
				return false; // cancel submission no matter what
			}
		} 
		else if (obj.nodeName.toLowerCase() != 'a') {
			while (obj.nodeName.toLowerCase() != 'a')	{
				obj = obj.parentNode; 
			}
			// Get the id of the triggering object
			var reNum = /\d{1,4}$/; 	// reg expression that finds a number 
			var s = String(obj.getAttribute("id")); // make the id a string for re to work
			// extract the numeric part of the id
			var targetIdNumArr = reNum.exec(s); // an array of matches
			// Get full cart path 
			cartPath = String(obj.getAttribute('href')); // the action part has the cart path 
		} 
		//alert(cartPath);
		// Got the target id that triggered and the cart path base
		// once we have the href parse out the part we want
		var rePath = /.*\?/;
		var cartPathMatch = cartPath.match(rePath);
		cartPath = cartPathMatch[0];
		
		// Now get the product id, category id, and quantity
		var pid, q, cid; 
		for (i=0; i<memory.inputFields.length; i++) {
		  if (DOMhelp.cssjs('check',memory.inputFields[i],memory.productIdClass)) {
			var s2 = String(memory.inputFields[i].getAttribute("id"));
			var testIdNumArr2 = reNum.exec(s2);
			  if (targetIdNumArr[0] == testIdNumArr2[0]) {
				pid = memory.inputFields[i].getAttribute('value');
			  }
		  } 
		  if (DOMhelp.cssjs('check',memory.inputFields[i],memory.categoryIdClass)) {
			var s3 = String(memory.inputFields[i].getAttribute("id"));
			var testIdNumArr3 = reNum.exec(s3);
			  if (targetIdNumArr[0] == testIdNumArr3[0]) {
				cid = memory.inputFields[i].getAttribute('value');
			  }
		  }
		  if (DOMhelp.cssjs('check',memory.inputFields[i],memory.quantityClass)) {
			var s4 = String(memory.inputFields[i].getAttribute("id"));
			var testIdNumArr4 = reNum.exec(s4);
			  if (targetIdNumArr[0] == testIdNumArr4[0]) {
				q = memory.inputFields[i].value;
			  }
			}
		}
		// Got cart path and ids 
		loc = cartPath + "product_id=" + pid + "|" + q + "&category_id=" + cid;

		// Validate quantity and point the window to the cart
		if (memory.validateQuantity(q)) {
			window.location = loc; // redirect the location if the quantity validates
		}
	} // end if not null object
	DOMhelp.cancelClick(e);
	return false; // cancel submission no matter what
}
}
DOMhelp.addEvent(window,'load', memory.init, false);
