/* if there are problems with a product that has had this feature recently added, check line 314 in shopproductfeaturesgenerate.asp and make sure the hardcoded beginning feature number is high enough to accomodate the number of initial features for the product */

function formAdder(rootLayer,startingnum) {
    this.layer = (typeof (rootLayer) == "object") ? rootLayer : (document.getElementById && document.getElementById(rootLayer)) ? document.getElementById(rootLayer) : null;
    this.id = (!this.layer) ? "layer" : this.layer.id;
    this.amount = startingnum;
    this.tag = "textarea";
    this.longTagSet = "";
    this.wrapperTag = "SPAN";
    this.wrapperId = "_wrapper_";
    this.className = "yourClass";
    this.replacer = "@#";
    this.last = null;
    this.amountIds = [];
    this.total = startingnum;
    this.set = (function (longTagSet) {if (typeof (longTagSet) != "string") {return false;}longTagSet = longTagSet.replace(/^\s+|\s+$/, "");var reg = new RegExp(this.replacer + "\\d+");var isSimpleTag = reg.test(longTagSet);if (!isSimpleTag) {this.tag = longTagSet;this.longTagSet = "";} else {this.longTagSet = longTagSet;}});
    this.replace = (function (longTagSet) {var array = this.swappers();if (/^@#\d+/.test(longTagSet)) {return array[longTagSet];}longTagSet = longTagSet || this.longTagSet.substring(0);for (var r in array) {longTagSet = longTagSet.replace(new RegExp(r, "g"), array[r]);}return longTagSet;});
    this.swappers = (function () {return {'@#1':this.id + "[]", '@#2':this.id + (this.amount - 1), '@#3':this.id, '@#4':this.amount - 1};});
    this.addLayer = this.addlayer = this.add_layer = this.add = (function () {
    	if (!this.layer || !document.getElementById) {return false;}
    	if (!this.longTagSet) {
    		var tagType = this.tag;
    		if (this.tag == "text" || this.tag == "radio" || this.tag == "checkbox" || this.tag == "hidden" || this.tag == "button") {tagType = "INPUT";}
			var newTag = document.createElement(tagType.toUpperCase());
			newTag.id = "" + this.id + this.amount;
			this.amountIds[++this.amountIds.length - 1] = newTag.id;
			this.amount++;
			newTag.name = this.id + "[]";
			newTag.className = this.className;
			if (tagType == "input") {newTag.type = this.tag;}
			this.layer.appendChild(newTag);
			this.last = document.getElementById(newTag.id);
			++this.total;
			return this.layer.childNodes[this.layer.childNodes.length - 1];
		}
		else {
			var newTag = document.createElement(this.wrapperTag);
			newTag.id = "" + this.wrapperId + this.id + this.amount;
			this.amountIds[++this.amountIds.length - 1] = newTag.id;
			this.amount++;
			newTag.className = this.className;
			newTag.innerHTML = this.replace(this.longTagSet, this.swappers());
	//window.alert(newTag.innerHTML);
			this.layer.appendChild(newTag);
			var idForLast = (this.longTagSet.indexOf(this.replacer + "2") == -1) ? newTag.id : (document.getElementById(this.id + (this.amount - 1))) ? this.id + (this.amount - 1) : newTag.id;
			this.last = document.getElementById(idForLast);
			++this.total;
			return document.getElementById("" + this.id + this.amount);
		}
		return false;
	});
    this.removeLayer = this.removelayer = this.remove_layer = this.remove = (function (which, remains) {
		if (!this.layer || !document.getElementById) {return false;}
		if (!this.amountIds.length) {return false;}
		remains = (!remains) ? 1 : 0;
		if (!(this.total - 1) && remains) {
			alert("You cannot remove all the fields of this form set: at least one must stay.");
			return false;
		}
		which = (!which) ? this.amountIds[this.amountIds.length - 1] : which;
		if (document.getElementById(which)) {
			var removed = this.layer.removeChild(document.getElementById(which));
			for (var i = 0; i < this.amountIds.length; i++) {
				if (this.amountIds[i] == which) {this.amountIds.splice(i, 1);this.amount--;break;}
		}
		--this.total;
		return removed;
	}
	return false;
});
/* keep this comment to reuse freely:
http://www.unitedscripters.com */}

function updateQty(thisform) {
	var q = 0;
	//scan all qty fields, add them up, put result in total qty
	 for (var i=0; i <= thisform.elements.length-1;i++) {
		if (thisform.elements[i].id.substr(0,3) == "qty") {
			q = q + parseInt(thisform.elements[i].value);
		}
	 }
	 thisform.quantity.value = q;
}
