function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function NewDSWin( url, FieldObj, FieldObj2, FieldObj3, FieldObj4, FieldObj5, FieldObj6 )
{
	var NewWin;

	if ( ! FieldObj5 ) { TStr5 = ""; }
	else { TStr5 = escape(FieldObj5); }

	if ( ! FieldObj6 ) { doRetVal = 0; }
	else { doRetVal = FieldObj6; }

	if ( FieldObj != null) { TStr1 = escape(FieldObj.value); }
	else { TStr1 = ""; }

	if ( FieldObj2 != null) { TStr2 = escape(FieldObj2.value); }
	else { TStr2 = ""; }

	if ( FieldObj3 != null) { TStr3 = escape(FieldObj3.value); }
	else { TStr3 = ""; }

	if ( FieldObj4 != null) { TStr4 = escape(FieldObj4.value); }
	else { TStr4 = ""; }

	NewURL = url + "?val1=" + TStr1 + "&val2=" + TStr2 + "&val3=" + TStr3 + "&val4=" + TStr4 + "&val5=" + TStr5 + "&RetVal=" + doRetVal;

	NewWin = window.open(NewURL,'name','height=330,width=750');
	if (window.focus) {NewWin.focus()}

}

function NewDBWin( url, winname, winht, winwd )
{
	var NewWin;
	NewURL = url;
	winOpt = "height=" + winht + ",width=" + winwd;
	NewWin = window.open(NewURL, winname, winOpt);
	if (window.focus) {NewWin.focus()}
}

/**
* Convert to form submission data from fields
* @param {string} DBFormName The name of the active form
* @param {string} DBFieldName Name of field containing price data
*/
function DBCalc(DBFormName, DBFieldName, DBItemName, DBItemDesc, DBEmail, DBItemEx)
{
	var DBFormObj = document.getElementById(DBFormName);
	if ( typeof DBFieldName != "undefined" ) {
		var DBFieldObj = document.getElementById(DBFieldName);
	} else {
		var DBFieldObj = document.getElementById("os0");
	}
	var DBCurSel = DBFieldObj.selectedIndex;
	var DBCurText = DBFieldObj.options[DBCurSel].text;

	TPrice = "0";
	TCurPos = DBCurText.lastIndexOf("$") + 1;
	TCurLen = DBCurText.length;
	if ( (TCurPos >= 0) && (TCurPos < TCurLen)) {
		TPrice = DBCurText.substring(TCurPos);
	}
	DBFormObj.amount.value = TPrice;

	if ( typeof DBItemName != "undefined" ) {
		DBFormObj.item_name.value = DBItemName;
		tStr = DBCurText + " - " + DBItemName;
		DBFormObj.os0.value = DBCurText;
	}
	if ( typeof DBItemEx != "undefined" ) {
		var DBItemSelObj = document.getElementById(DBItemEx);
		if ( DBItemSelObj.checked == true ) {
			DBFormObj.os0.value = DBFormObj.os0.value + " w/o ribbon";
		}
	}

	if ( typeof DBItemDesc != "undefined" ) {
		DBFormObj.on0.value = DBItemDesc;
	} else {
		DBFormObj.on0.value = "x";
	}

	if ( typeof DBEmail != "undefined" ) {
		DBFormObj.business.value = DBEmail;
	} else {
		DBFormObj.business.value = "designs@dolphinbead.com";
	}

	return true;
}

function DBCalcEx(DBFormName, DBFieldName, DBFieldName2, DBItemName, DBItemDesc, DBEmail, DBItemEx)
{
	var DBFormObj = document.getElementById(DBFormName);
	if ( typeof DBFieldName != "undefined" ) {
		var DBFieldObj = document.getElementById(DBFieldName);
	} else {
		var DBFieldObj = document.getElementById("os0");
	}

	var DBCurSel = DBFieldObj.selectedIndex;
	var DBCurText = DBFieldObj.options[DBCurSel].text;
	var DBFieldObj2 = document.getElementById(DBFieldName2);
	var DBCurSel2 = DBFieldObj2.selectedIndex;
	var DBCurText2 = DBFieldObj2.options[DBCurSel2].text;

	TPrice = "0";
	TCurPos = DBCurText.lastIndexOf("$") + 1;
	TCurLen = DBCurText.length;
	if ( (TCurPos >= 0) && (TCurPos < TCurLen)) {
		TPrice = DBCurText.substring(TCurPos);
	}
	DBFormObj.amount.value = TPrice;

	if ( typeof DBItemName != "undefined" ) {
		var ExItemInfo = document.getElementById(DBFieldName2);
		tStr = DBItemName + " - " + DBCurText2;
		DBFormObj.item_name.value = tStr;

		tStr = DBCurText + " - " + DBItemName;
		DBFormObj.os0.value = DBCurText;
	}
	if ( typeof DBItemEx != "undefined" ) {
		var DBItemSelObj = document.getElementById(DBItemEx);
		if ( DBItemSelObj.checked == true ) {
			DBFormObj.os0.value = DBFormObj.os0.value + " w/o ribbon";
		}
	}

	if ( typeof DBItemDesc != "undefined" ) {
		DBFormObj.on0.value = DBItemDesc;
	} else {
		DBFormObj.on0.value = "x";
	}

	if ( typeof DBEmail != "undefined" ) {
		DBFormObj.business.value = DBEmail;
	} else {
		DBFormObj.business.value = "designs@dolphinbead.com";
	}

	return true;
}

function DBCalcList( inSamplerVal )
{
	var tShipType = 0;
	var tTagType = 0;
	var tSampAmt = 5;
	if ( typeof inSamplerVal != "undefined" ) {
		if ( inSamplerVal.indexOf("foil") >= 0 ) {
			tTagType = 1;
			tSampAmt = 8;
		} else if ( inSamplerVal.indexOf("shimmer") >= 0 ) {
			tTagType = 2;
			tSampAmt = 8;
		}
	}

	selOpt = document.createElement("Option");

	var elem = document.getElementById('dolphin6').elements;
	for(var i = 0; i < elem.length; i++) {
		//		str += "<b>Type:</b>" + elem[i].type + "&nbsp&nbsp";
		//		str += "<b>Name:</b>" + elem[i].name + "&nbsp;&nbsp;";
		//		str += "<b>Value:</b><i>" + elem[i].value + "</i>&nbsp;&nbsp;";
		//		str += "<BR>";
		//-		str += elem[i].name;
		//-		str += " -- ";
		//-		str += elem[i].type;
		//-		str += "\r\n";
		CurCtl = elem[i];
		tstStr = elem[i].name.substring(0,6);

		if ( (elem[i].type == "select-one") && ((tstStr == "gncalc") || (tstStr == "gnrush")) ) {
			if (tstStr == "gncalc") {
				tShipType = 0;
			} else if (tstStr == "gnrush") {
				tShipType = 1;
				tSampAmt = Math.round(tSampAmt * 1.45);
			}
			DBCalcListCtl(CurCtl.name);
		}
	}
}

function DBCalcListCtl( inUpdCtl )
{
	var CurCtl = document.getElementById(inUpdCtl);

//	var tShipType = 0;
//	var tTagType = 0;
//	var tSampAmt = 5;
//	if ( typeof inSamplerVal != "undefined" ) {
//		if ( inSamplerVal.indexOf("foil") >= 0 ) {
//			tTagType = 1;
//			tSampAmt = 8;
//		} else if ( inSamplerVal.indexOf("shimmer") >= 0 ) {
//			tTagType = 2;
//			tSampAmt = 8;
//		}
//	}
//
//	selOpt = document.createElement("Option");
//	var elem = document.getElementById('dolphin6').elements;
//	CurCtl = elem[i];
//	tstStr = elem[i].name.substring(0,6);

	valStr = CurCtl.options[0].text;

	// Starting level
	srchPos = valStr.indexOf(" ");
	tmpQty = valStr.substring(0, srchPos);
	savePos = srchPos;
	srchPos = valStr.indexOf("$");
	tmpAmt = valStr.substring(srchPos+1);
	lablType =valStr.substring(savePos, srchPos - savePos);
//	alert("|" + lablType + "|a");

	// Mult 2 (200, 1000)
	selOpt = document.createElement("Option");
	wrkQty = tmpQty * 2;
	tmpDispQty = wrkQty;
	tmpDispAmt = Math.round(tmpAmt * 2 * 0.985);
	tmpDispQty = addCommas(tmpDispQty);
	selOpt.text = tmpDispQty + lablType + " - $" + tmpDispAmt;
	selOpt.value = tmpDispAmt;
	CurCtl.options.add(selOpt, 1);

	// Mult 2.5(5)  (500, 2500)
	selOpt = document.createElement("Option");
	wrkQty = Math.round(wrkQty * 2.5);
	tmpDispQty = wrkQty;
	tmpDispAmt = Math.round(tmpAmt * 5 * 0.97);
	tmpDispQty = addCommas(tmpDispQty);
	selOpt.text = tmpDispQty + lablType + " - $" + tmpDispAmt;
	selOpt.value = tmpDispAmt;
	CurCtl.options.add(selOpt, 2);

	// Mult 2(10) (1000, 5000)
	selOpt = document.createElement("Option");
	wrkQty = Math.round(wrkQty * 2);
	tmpDispQty = wrkQty;
	tmpDispAmt = Math.round(tmpAmt * 10 * 0.955);
	tmpDispQty = addCommas(tmpDispQty);
	selOpt.text = tmpDispQty + lablType + " - $" + tmpDispAmt;
	selOpt.value = tmpDispAmt;
	CurCtl.options.add(selOpt, 3);

	// Mult 2(20) (2000, 10000)
	selOpt = document.createElement("Option");
	wrkQty = Math.round(wrkQty * 2);
	tmpDispAmt = Math.round(tmpAmt * 20 * 0.94);
	tmpDispQty = wrkQty;
	tmpDispQty = addCommas(tmpDispQty);
	selOpt.text = tmpDispQty + lablType + " - $" + tmpDispAmt;
	selOpt.value = tmpDispAmt;
	CurCtl.options.add(selOpt, 4);

	// Mult 2.5(50) (5000, 25000)
	selOpt = document.createElement("Option");
	wrkQty = Math.round(wrkQty * 2.5);
	tmpDispQty = wrkQty;
	tmpDispAmt = Math.round(tmpAmt * 50 * 0.92);
	tmpDispQty = addCommas(tmpDispQty);
	selOpt.text = tmpDispQty + lablType + " - $" + tmpDispAmt;
	selOpt.value = tmpDispAmt;
	CurCtl.options.add(selOpt, 5);

	// Mult 2(100) (10000, 50000)
	selOpt = document.createElement("Option");
	wrkQty = Math.round(wrkQty * 2);
	tmpDispQty = wrkQty;
	tmpDispAmt = Math.round(tmpAmt * 100 * 0.90);
	tmpDispQty = addCommas(tmpDispQty);
	selOpt.text = tmpDispQty + lablType + " - $" + tmpDispAmt;
	selOpt.value = tmpDispAmt;
	CurCtl.options.add(selOpt, 6);
}

function DBCalcListOrig( inUpdCtl )
{
	var tShipType = 0;
	var tTagType = 0;
	var tSampAmt = 5;
	if ( typeof inSamplerVal != "undefined" ) {
		if ( inSamplerVal.indexOf("foil") >= 0 ) {
			tTagType = 1;
			tSampAmt = 8;
		} else if ( inSamplerVal.indexOf("shimmer") >= 0 ) {
			tTagType = 2;
			tSampAmt = 8;
		}
	}

	selOpt = document.createElement("Option");

	var elem = document.getElementById('dolphin6').elements;
	for(var i = 0; i < elem.length; i++) {
		//		str += "<b>Type:</b>" + elem[i].type + "&nbsp&nbsp";
		//		str += "<b>Name:</b>" + elem[i].name + "&nbsp;&nbsp;";
		//		str += "<b>Value:</b><i>" + elem[i].value + "</i>&nbsp;&nbsp;";
		//		str += "<BR>";
		//-		str += elem[i].name;
		//-		str += " -- ";
		//-		str += elem[i].type;
		//-		str += "\r\n";
		CurCtl = elem[i];
		tstStr = elem[i].name.substring(0,6);

		if ( (elem[i].type == "select-one") && ((tstStr == "gncalc") || (tstStr == "gnrush")) ) {
			if (tstStr == "gncalc") {
				tShipType = 0;
			} else if (tstStr == "gnrush") {
				tShipType = 1;
				tSampAmt = Math.round(tSampAmt * 1.45);
			}

			valStr = CurCtl.options[0].text;

			// Starting level
			srchPos = valStr.indexOf(" ");
			tmpQty = valStr.substring(0, srchPos);
			savePos = srchPos;
			srchPos = valStr.indexOf("$");
			tmpAmt = valStr.substring(srchPos+1);
			lablType =valStr.substring(savePos, srchPos - savePos);
//			alert("|" + lablType + "|a");

			// Mult 2 (200, 1000)
			selOpt = document.createElement("Option");
			wrkQty = tmpQty * 2;
			tmpDispQty = wrkQty;
			tmpDispAmt = Math.round(tmpAmt * 2 * 0.985);
			tmpDispQty = addCommas(tmpDispQty);
			selOpt.text = tmpDispQty + lablType + " - $" + tmpDispAmt;
			selOpt.value = tmpDispAmt;
			CurCtl.options.add(selOpt, 1);

			// Mult 2.5(5)  (500, 2500)
			selOpt = document.createElement("Option");
			wrkQty = Math.round(wrkQty * 2.5);
			tmpDispQty = wrkQty;
			tmpDispAmt = Math.round(tmpAmt * 5 * 0.97);
			tmpDispQty = addCommas(tmpDispQty);
			selOpt.text = tmpDispQty + lablType + " - $" + tmpDispAmt;
			selOpt.value = tmpDispAmt;
			CurCtl.options.add(selOpt, 2);

			// Mult 2(10) (1000, 5000)
			selOpt = document.createElement("Option");
			wrkQty = Math.round(wrkQty * 2);
			tmpDispQty = wrkQty;
			tmpDispAmt = Math.round(tmpAmt * 10 * 0.955);
			tmpDispQty = addCommas(tmpDispQty);
			selOpt.text = tmpDispQty + lablType + " - $" + tmpDispAmt;
			selOpt.value = tmpDispAmt;
			CurCtl.options.add(selOpt, 3);

			// Mult 2(20) (2000, 10000)
			selOpt = document.createElement("Option");
			wrkQty = Math.round(wrkQty * 2);
			tmpDispAmt = Math.round(tmpAmt * 20 * 0.94);
			tmpDispQty = wrkQty;
			tmpDispQty = addCommas(tmpDispQty);
			selOpt.text = tmpDispQty + lablType + " - $" + tmpDispAmt;
			selOpt.value = tmpDispAmt;
			CurCtl.options.add(selOpt, 4);

			// Mult 2.5(50) (5000, 25000)
			selOpt = document.createElement("Option");
			wrkQty = Math.round(wrkQty * 2.5);
			tmpDispQty = wrkQty;
			tmpDispAmt = Math.round(tmpAmt * 50 * 0.92);
			tmpDispQty = addCommas(tmpDispQty);
			selOpt.text = tmpDispQty + lablType + " - $" + tmpDispAmt;
			selOpt.value = tmpDispAmt;
			CurCtl.options.add(selOpt, 5);

			// Mult 2(100) (10000, 50000)
			selOpt = document.createElement("Option");
			wrkQty = Math.round(wrkQty * 2);
			tmpDispQty = wrkQty;
			tmpDispAmt = Math.round(tmpAmt * 100 * 0.90);
			tmpDispQty = addCommas(tmpDispQty);
			selOpt.text = tmpDispQty + lablType + " - $" + tmpDispAmt;
			selOpt.value = tmpDispAmt;
			CurCtl.options.add(selOpt, 6);

			// Add sampler
			selOpt = document.createElement("Option");
			tmpDispQty = 10;
			if ( tShipType == 0 ) {
				tmpDispAmt = "5";
			} else if ( tShipType == 1 ) {
				tmpDispAmt = "8";
			}
			tmpDispAmt = tSampAmt;
			selOpt.text = "Sampler - " + tmpDispQty + " for $" + tmpDispAmt;
			selOpt.value = tmpDispAmt;
//			CurCtl.options.add(selOpt);
		}
	}
}

function calcLipped(tFillType, tChkContName, tListContName, tSelType, tExPct)
{
	var tIncLips = 1;

	tAddPct = 4;
	if ( typeof tExPct != "undefined" ) {
		tAddPct = tExPct;
	} else {
		tAddPct = 4;
	}
	tCalcEx = 1.0 + (tAddPct / 10);

	var tChkCont = document.getElementById(tChkContName)
	var tListCont = document.getElementById(tListContName)
	if ( tSelType  == "0" ) {
		if ( tChkCont.checked == true ) {
			tIncLips = 1;
		} else {
			tIncLips = 0;
		}
	} else {
		if ( tSelType == "with" ) {
			if ( tChkCont.value == 0 ) return;
			tIncLips = 1;
			tChkCont.value = 0;
		} else if ( tSelType == "without" ) {
			if ( tChkCont.value == 1 ) return;
			tIncLips = 0;
			tChkCont.value = 1;
		}
	}

	initStr = tListCont.options[0].text;
	srchPos = initStr.indexOf(" ");
	tmpQty = initStr.substring(0, srchPos);
	rmdStr = initStr.substring(srchPos);
	if ( tIncLips == 1 ) {
		newQty = Math.round(tmpQty / tCalcEx);
		tStr = rmdStr.replace(" w/out lips", " with lips");
		newStr = newQty + tStr;
		tListCont.options[0].text=newStr;
	} else {
		newQty = Math.round(tmpQty * tCalcEx);
		tStr = rmdStr.replace(" with lips", " w/out lips");
		newStr = newQty + tStr;
		tListCont.options[0].text=newStr;
	}
	SelOptPos = 1;
	while ( (typeof tListCont.options[SelOptPos] == "object") && (tListCont.options[SelOptPos] != null) ) {
		testStr = tListCont.options[SelOptPos].text;
		if ( testStr.search(/sampler/i) == -1 ) {
			tListCont.remove(1);
		} else {
			SelOptPos++;
		}
	}
	DBCalcListCtl(tListContName);
}

function calcRibbon(tFillType, tChkContName, tListContName, tSelType, tExPct)
{
	var tIncLips = 1;

	tAddPct = 4;
	if ( typeof tExPct != "undefined" ) {
		tAddPct = tExPct;
	} else {
		tAddPct = 4;
	}
	tCalcEx = 1.0 + (tAddPct / 10);

	var tChkCont = document.getElementById(tChkContName)
	var tListCont = document.getElementById(tListContName)
	if ( tSelType  == "0" ) {
		if ( tChkCont.checked == true ) {
			tIncLips = 1;
		} else {
			tIncLips = 0;
		}
	} else {
		if ( tSelType == "with" ) {
			if ( tChkCont.value == 0 ) return;
			tIncLips = 1;
			tChkCont.value = 0;
		} else if ( tSelType == "without" ) {
			if ( tChkCont.value == 1 ) return;
			tIncLips = 0;
			tChkCont.value = 1;
		}
	}

	initStr = tListCont.options[0].text;
	srchPos = initStr.indexOf(" ");
	tmpQty = initStr.substring(0, srchPos);
	rmdStr = initStr.substring(srchPos);
	if ( tIncLips == 1 ) {
		newQty = Math.round(tmpQty / tCalcEx);
		tStr = rmdStr.replace(" w/out ribbon", " with ribbon");
		newStr = newQty + tStr;
		tListCont.options[0].text=newStr;
	} else {
		newQty = Math.round(tmpQty * tCalcEx);
		tStr = rmdStr.replace(" with ribbon", " w/out ribbon");
		newStr = newQty + tStr;
		tListCont.options[0].text=newStr;
	}
	SelOptPos = 1;
	while ( (typeof tListCont.options[SelOptPos] == "object") && (tListCont.options[SelOptPos] != null) ) {
		testStr = tListCont.options[SelOptPos].text;
		if ( testStr.search(/sampler/i) == -1 ) {
			tListCont.remove(1);
		} else {
			SelOptPos++;
		}
	}
	DBCalcListCtl(tListContName);
}
