<!--
// (c) 2000, 2001, WishWorld. All rights reserved.
// contact: wishworld@wishworld.com

var glPrice; // global price holderh

// fix strings (remove offisive chars)
function fixString(str) {

	var pbDesc = new String(str);
	var j;

	while((j = pbDesc.indexOf("\n")) != -1) {
		pbDesc= "" + (pbDesc.substring(0, j) + " " + pbDesc.substring((j + 1), pbDesc.length));
	}
	while((j = pbDesc.indexOf("\r")) != -1) {
		pbDesc= "" + (pbDesc.substring(0, j) + pbDesc.substring((j + 1), pbDesc.length));
	}
	while((j = pbDesc.indexOf("<")) != -1) {
		pbDesc= "" + (pbDesc.substring(0, j) + pbDesc.substring((j + 1), pbDesc.length));
	}
	while((j = pbDesc.indexOf(">")) != -1) {
		pbDesc= "" + (pbDesc.substring(0, j) + pbDesc.substring((j + 1), pbDesc.length));
	}
	while((j = pbDesc.indexOf("'")) != -1) {
		pbDesc= "" + (pbDesc.substring(0, j) + pbDesc.substring((j + 1), pbDesc.length));
	}

	return pbDesc;
}

// fix strings (remove dollar)
function fixDollar(str) {

	var pbDesc = new String(str);
	var j;

	while((j = pbDesc.indexOf("$")) != -1) {
		pbDesc= "" + (pbDesc.substring(0, j) + " " + pbDesc.substring((j + 1), pbDesc.length));
	}

	return pbDesc;
}

// convert the global price to a string and fix it (xxxx.xx)
function fixPrice() {
	var strPrice = glPrice.toString();
	var iDotLoc = strPrice.indexOf(".");

	if (iDotLoc != -1)
		return strPrice.substring(0, iDotLoc +3);
	else
		return strPrice + ".00";
}

// set the value of glPrice
function setPrice(strPrice) {
	var lPrice = parseFloat(strPrice);
	if (!isNaN(lPrice)) glPrice = lPrice;
}

// parse option strings to see if something changes my price...
function addPrice(str) {
	var lPrice = 0;
	var iPriceLoc = str.indexOf("$");
	var iPlusLoc = str.indexOf("+");

	if (iPriceLoc != -1) { // then I've got a price
		if (iPlusLoc != -1) { // then I've got a plus
			if (iPlusLoc < iPriceLoc) { // I care about it (e.g., + $10.00)
				lPrice = parseFloat(str.substr(iPriceLoc +1));
				if (!isNaN(lPrice)) glPrice += lPrice;
				glPrice += .00001; // fixes the float run
			}
		} // iPlusLoc != 0

		if (lPrice == 0) { // replace the price!
			lPrice = parseFloat(str.substr(iPriceLoc +1));
			if (!isNaN(lPrice)) glPrice = lPrice;
			glPrice += .00001; // fixes the float run
		}
	}

	return str;
}

// get the value of the form element (generic)
function getValue(element) {

	var eType = element.type.toLowerCase();

	if (eType == "select-one") {
		//if (element.selectedIndex > 0) { -- 00Aug18, jrh -- always take value
		var retVal = element[element.selectedIndex].value;
		if (retVal == "") retVal = element[element.selectedIndex].text;
		return addPrice(fixString(retVal));
		//} else
			//return null;
	} else
		return fixString(element.value);
}

// get the value of the form element (generic)
function getValuetext(element) {

	var eType = element.type.toLowerCase();

	if (eType == "select-one") {
		//if (element.selectedIndex > 0) { -- 00Aug18, jrh -- always take value
		var retVal = element[element.selectedIndex].text;
		if (retVal == "") retVal = element[element.selectedIndex].value;
		return addPrice(fixString(retVal));
		//} else
			//return null;
	} else
		return fixString(element.value);
}

// add a multiple option product (inital release for CookieBabyInc)
function addAdvProduct(form, storeCode) {

	// this function reads the form that called it and builds a product list
	// and finishes by calling addProduct()

	var lProd = "|:|"; // local 'product' spec
	var elements = form.elements;
	var eleType, eleName, iColonLoc;
	var lclDesc, lclQty, lclPrice, lclBuyURL, lclF1, lclF2, lclF3;
	var lCustomVal, lProductVal, lprVal;
	var iF1Val, iF2Val, iF3Val;
	var rgF1Val = new Array();
	var rgF2Val = new Array();
	var rgF3Val = new Array();

	// initial values
	lclDesc = lclBuyURL = lclQty = lclPrice = lclF1 = lclF2 = lclF3 = "";
	iF1Val = iF2Val = iF3Val = 0;
	glPrice = 0;
	lProductVal = 0;

	// walk thru form and get 'stuff'
	for(var i = 0; i < elements.length; i++) {
		eleType = elements[i].type.toLowerCase();
		eleName = elements[i].name.toLowerCase();

		// while Microsoft supports 'switch', it may not be available under other browsers.
		// need to use if statements to figure out what I've got.
		if (eleType != "submit") { // don't care about this type

			// get the name and value of the input. I'm looking for specific keywords...
			//   Description == description || desc || name || addName || item || product || describe
			//   Quantity == quantity || qty || addQty || jac_quantity_input
			//   Price == price || cost || addPrice || price1
			//   BuyURL == buyurl || return
			//   Code == code || f1 || addID || sku || pf_id || product_id || PartNo || id
			//   Color == color || f2 || p1 || AttributeA || color_id
			//   Size == size || f3 || p2 || addSize || AttributeB || size_id
			// it's also possible to have elements named customX. This is special.
			// it's also possible to have elements named product1. This is special.
			// override keywords: wlprice, wlbuyurl, wldesc.

		// Description
			if (eleName == "description" || eleName == "desc" || eleName == "describe" || eleName == "name" || eleName == "product" || eleName == "addname" || eleName == "wldesc" || eleName == "item") {
				if (eleName == "wldesc") // override
					lclDesc = getValue(elements[i]);
				else if (lclDesc == "")
					lclDesc = getValue(elements[i]);
			} else
		// Quantity
			if (eleName == "quantity" || eleName == "qty" || eleName == "addqty" || eleName == "jac_quantity_input") {
				lclQty = getValue(elements[i]);
			} else
		// Price
			if (eleName == "price" || eleName == "price1" || eleName == "cost" || eleName == "addprice" || eleName == "wlprice") {
				if (eleName == "wlprice") // override
					lclPrice = getValue(elements[i]);
				else if (lclPrice == "" && !glPrice)
					lclPrice = getValue(elements[i]);
				lclPrice = fixDollar(lclPrice);
				setPrice(lclPrice); // set to glPrice value (number);
			} else
		// BuyURL
			if (eleName == "buyurl" || eleName == "return" || eleName == "wlbuyurl") {
				if (eleName == "wlbuyurl") // override
					lclBuyURL = getValue(elements[i]);
				else if (lclBuyURL == "")
					lclBuyURL = getValue(elements[i]);
			} else
		// F1
			if (eleName == "code" || eleName == "f1" || eleName == "id" || eleName == "addid" || eleName == "sku" || eleName == "pf_id" || eleName == "product_id" || eleName == "partno") {
				lclF1 = getValue(elements[i]);
			} else
		// F2
			if (eleName == "color" || eleName == "color_id" || eleName == "f2" || eleName == "p1" || eleName == "attr_value2" || eleName == "attributea" ) {
				lclF2 = getValue(elements[i]);
				if (eleName == "color_id") lclF2 = getValuetext(elements[i]);
			} else
		// F3
			if (eleName == "size" || eleName == "size_id" || eleName == "f3" || eleName == "p2" || eleName == "addsize" || eleName == "attr_value1" || eleName == "attributeb" ) {
				lclF3 = getValue(elements[i]);
				if (eleName == "size_id") lclF3 = getValuetext(elements[i]);
			} else
		// {customX}
			if (eleName.substring(0, 6) == "custom") { // e.g., CookieBabyInc and others.
				// find out the number range of this element
				lCustomVal = parseInt(eleName.substr(6));
				if (isNaN(lCustomVal)) lCustomVal = 0;

				if (lCustomVal > 0 && lCustomVal < 21) { // 1..20
					if(eleValue = getValue(elements[i]))
						rgF1Val[iF1Val++] = eleValue;
				} else
				if (lCustomVal > 20 && lCustomVal < 41) { // 21..40
					if(eleValue = getValue(elements[i]))
						rgF2Val[iF2Val++] = eleValue;
				} else
				if (lCustomVal > 40 && lCustomVal < 61) {
					if(eleValue = getValue(elements[i]))
						rgF3Val[iF3Val++] = eleValue;
				}
			} else
		// {productpr}
			if (eleName == "productpr") { // e.g., scnaturals
					eleValue = getValue(elements[i]);
				
					iColonLoc = eleValue.indexOf(":");
					lclDesc = eleValue.substring(0, iColonLoc);
					lclPrice = parseFloat(eleValue.substr(iColonLoc +1));
					if (!glPrice) setPrice(lclPrice);
					
					lProductVal++;
			} else
		// {productprX}
			if (eleName.substring(0, 9) == "productpr") { // e.g., BornToLove
				// is this 1 or 2? Only accept values from 1
				if (parseInt(eleName.substr(9)) == 1) {
					eleValue = getValue(elements[i]);
				
					iColonLoc = eleValue.indexOf(":");
					lclDesc = eleValue.substring(0, iColonLoc);
					lclPrice = parseFloat(eleValue.substr(iColonLoc +1));
					if (!glPrice) setPrice(lclPrice);
					
					lProductVal++;
				}
			} else
		// {product1[]}
			if (eleName.substring(0, 8) == "product1") { // e.g., ElegantChild
				if (lProductVal++ == 0)
					lclDesc = getValue(elements[i]);
				else {
					if (lProductVal < 6) {
						if(eleValue = getValue(elements[i]))
							rgF1Val[iF1Val++] = eleValue;
					} else
					if (lProductVal > 5 && lProductVal < 10) {
						if(eleValue = getValue(elements[i]))
							rgF2Val[iF2Val++] = eleValue;
					} else
					if(eleValue = getValue(elements[i]))
						rgF3Val[iF3Val++] = eleValue;
				}
			} else
		// {product[]}
			if (eleName == "product[]") { // e.g., BabyMineStore
				if (eleType == "hidden")
					lclDesc = getValue(elements[i]);
				else // eleType == submit-one
					lclF1 = getValuetext(elements[i]);
			} else
		// {attrX}
			if (eleName.substring(0, 4) == "attr") { // e.g., OrganicBebe
					if(eleValue = getValuetext(elements[i]))
						rgF2Val[iF2Val++] = eleValue;
			} 
		} // !submit
	}

	// check to see if I need to build strings from the rgVal[]'s
	if (rgF1Val.length > 0) {
		if(lclF1 != "") lclF1 += ",";
		lclF1 += rgF1Val.slice(0);
	}
	if (rgF2Val.length > 0) {
		if(lclF2 != "") lclF2 += ",";
		lclF2 += rgF2Val.slice(0);
	}
	if (rgF3Val.length > 0) {
		if(lclF3 != "") lclF3 += ",";
		lclF3 += rgF3Val.slice(0);
	}

	// clean up if needed
	if (lclQty == "") lclQty = "1";
	if (lclPrice == "") lclPrice = "0.00";

	// build the 'product' string
	lclPrice = fixPrice();
	lProd += "desc:" + lclDesc + "|";
	lProd += "qty:" + lclQty + "|";
	lProd += "price:" + lclPrice + "|";
	lProd += "buyurl:" + lclBuyURL + "|";
	lProd += "f1:" + lclF1 + "|";
	lProd += "f2:" + lclF2 + "|";
	lProd += "f3:" + lclF3 + "|";

	// call addProduct()
	addProduct(storeCode, lProd);
}

//-->