UltraCart.prototype.ComputeTotals = ultracart_ComputeTotals;
function ultracart_ComputeTotals()
{
this.DeleteItemsWithNoQuantity();
this.UpdateTotals();
// if(this.isLineItemDiscountValid()) this.UpdateDiscounts();
// if(this.isLineItemTaxValid()) this.UpdateLineItemTax();
// this.GroupDiscount()
this.Subtotal = this.GetColumnTotal("Total");
this.SumTax = (this.TaxLocalePostal + this.TaxStateProvince + this.TaxCountry);
this.SumDiscount = (this.DiscountGroup + this.DiscountLineItem + this.Discount);
this.GrandTotal = this.Subtotal + this.ShippingCost + this.SumTax - this.SumDiscount;
this.Persist();
}

UltraCart.prototype.UpdateQuantities = ultracart_UpdateQuantities;
function ultracart_UpdateQuantities(formElementName)
{
var items = new Enumerator(Request.Form(formElementName));
var j = 0;
var indexQuantity = this.GetIndexOfColName(this.QUANTITY);
while(!items.atEnd())
{
var qty = parseFloat(items.item());
// Remove item if not a number or less than zero or if it was explicitly removed
if (isNaN(qty) || qty < 0 || String(Request.Form("RemoveCartItem" + j)) != "undefined" )
{
this.CartDataTable[indexQuantity][j] = 0;
}
else
{
this.CartDataTable[indexQuantity][j] = qty;
}
j++;
items.moveNext();
}

this.ComputeTotals();
}

UltraCart.prototype.GetIndexOfColName = ultracart_GetIndexOfColName;
function ultracart_GetIndexOfColName(colName)
{
var retIndex = -1;
for (var i=0; i<this.numCols; i++)
{
if (this.cartColNames[i] == colName)
{
retIndex = i;
break;
}
}
return retIndex;
}

UltraCart.prototype.GetColumnValue = ultracart_GetColumnValue;
function ultracart_GetColumnValue(colName, row)
{
var retValue = "";
var indexCol = this.GetIndexOfColName(colName);
this.Assert(!isNaN(row), "GetColumnValue: row is not a number - row = " + row);
this.Assert( indexCol >= 0, "GetColumnValue: Could not find column \"" + colName + "\" in the cart");
this.Assert(row >= 0, "GetColumnValue: Bad row number input to cart - row = " + row);
this.Assert(this.GetItemCount()>0, "GetColumnValue: The cart is empty - the requested data is unavailable");
this.Assert(row<this.GetItemCount(), "GetColumnValue: The line item number is greater than the number of items in the cart - row = " + row + "; GetItemCount = " + this.GetItemCount());
if (this.GetItemCount()>0) retValue = this.CartDataTable[indexCol][row];
return retValue;
}

UltraCart.prototype.GetColumnTotal = ultracart_GetColumnTotal;
function ultracart_GetColumnTotal(colName)
{
var colTotal = 0.00;
index = this.GetIndexOfColName(colName);
this.Assert(index >= 0, "GetColumnValue: Could not find column \"" + colName + "\" in the cart");
for (var i=0; i<this.CartDataTable[index].length; i++)
{
colTotal += parseFloat(this.CartDataTable[index][i]);
}
return colTotal;
}

UltraCart.prototype.FindItem = ultracart_FindItem;
function ultracart_FindItem(values)
{
var newRow = -1;
for(var iRow=0; iRow<this.GetItemCount(); iRow++)
{
found = true; // assume found
for (var iCol=0; iCol<this.numCols; iCol++)
{
if(this.cartColIdentifier[iCol] != "N")
{
if(this.CartDataTable[iCol][iRow] != values[iCol])
{
found = false;
break;
}
}
}
if(found)
{
newRow = iRow;
break;
}
}
return newRow;
}

UltraCart.prototype.CleanRedirect = ultracart_CleanRedirect;
function ultracart_CleanRedirect(redirectToPage)
{
if (redirectToPage == "") redirectToPage = Request.ServerVariables("URL");
if (String(redirectToPage).indexOf("?") == -1 && Request.QueryString != "")
{
var newQS = "?";
// Remove our unwanted query parameters.
var re = /AddToCartID|EmptyCart|RemoveCartItem|UC_SaveToTabl e/ig;
for (var items=new Enumerator(Request.QueryString); !items.atEnd(); items.moveNext())
{
if ( !re.test(items.item()) )
{
if (newQS.length > 1) newQS += "&";
newQS += items.item() + "=" + Server.URLencode(Request.QueryString(items.item()) );
}
}
re = null;
if (newQS.length > 1) redirectToPage += newQS;
}
Response.Redirect(redirectToPage);
}

UltraCart.prototype.AddItem = ultracart_AddItem;
function ultracart_AddItem(bindingColumns, bindingValues, incrementDupItems, redirectToPage, redirectOnDup)
{
var bindingColumns = this.VbToJsArray(bindingColumns);
var bindingValues = this.VbToJsArray(bindingValues);

// sort cart columns
var b = new Array(this.numCols);
for(var i=0; i<this.numCols; i++)
{
var index = this.GetIndexOfColName(bindingColumns[i]);
b[index] = bindingValues[i];
}

bindingColumns = null;
bindingValues = null;
var values = b;

var newRow = this.FindItem(values);
if (newRow == -1)
{
// add new item
newRow = this.GetItemCount();
for (var iCol=0; iCol<this.numCols; iCol++)
{
this.CartDataTable[iCol][newRow] = values[iCol];
}
}
else if (incrementDupItems)
{
// increment
var indexQuantity = this.GetIndexOfColName(this.QUANTITY);
this.CartDataTable[indexQuantity][newRow] = parseFloat(this.CartDataTable[indexQuantity][newRow]) + parseFloat(values[indexQuantity]);
if (isNaN(this.CartDataTable[indexQuantity][newRow]))
{
this.CartDataTable[indexQuantity][newRow] = 1;
}
}
else
{
// redirect to dup page
redirectToPage = redirectOnDup;
}
this.ComputeTotals(); // not needed if redirect because reload will recompute
this.CleanRedirect(redirectToPage);
}

UltraCart.prototype.SerializeRows = ultracart_SerializeRows;
function ultracart_SerializeRows(colDelim, rowDelim)
{
var serialCart = "";
for (var iRow=0; iRow<this.GetItemCount(); iRow++)
{
if (iRow != 0)
{
serialCart += rowDelim;
}

for (var iCol=0; iCol<this.numCols; iCol++)
{
if (iCol != 0)
{
serialCart += colDelim;
}

serialCart += this.CartDataTable[iCol][iRow];
}
}
return serialCart;
}

UltraCart.prototype.DeserializeRows = ultracart_DeserializeRows;
function ultracart_DeserializeRows(serialCart, colDelim, rowDelim)
{
var cartRows = String(serialCart).split(rowDelim)
for (iRow = 0; iRow < Rows.length; iRow++)
{
if (cartRows[iRow] != "undefined" && cartRows[iRow] != "")
{
var cartCols = cartRows[iRow].split(colDelim);
for (var iCol = 0; iCol<cartCols.length; iCol++)
{
this.CartDataTable[iCol][iRow] = cartCols[iCol];
}
}
}
}

UltraCart.prototype.SetCookie = ultracart_SetCookie;
function ultracart_SetCookie()
{
var cookieName = this.GetCookieName();
Response.Cookies(cookieName)("ShippingCost") = this.ShippingCost;
Response.Cookies(cookieName)("ShippingMethod") = this.ShippingMethod;
Response.Cookies(cookieName)("TaxStateProvince") = this.TaxStateProvince;
Response.Cookies(cookieName)("TaxCountry") = this.TaxCountry;
Response.Cookies(cookieName)("TaxLocalePostal") = this.TaxLocalePostal;
Response.Cookies(cookieName)("Discount") = this.Discount;
Response.Cookies(cookieName)("Other") = this.Other;
Response.Cookies(cookieName)("DT") = this.SerializeRows(this.COOKIECOLDEL, this.COOKIEROWDEL);
if( this.CookieLifeTime > 0)
{
Response.Cookies(cookieName).Expires = GetCookieExp(this.CookieLifeTime);
}
}

UltraCart.prototype.GetCookieName = ultracart_GetCookieName;
function ultracart_GetCookieName()
{
return (Request.ServerVariables("SERVER_NAME") + this.Name);
}

UltraCart.prototype.DestroyCookie = ultracart_DestroyCookie;
function ultracart_DestroyCookie()
{
var CookieName = this.GetCookieName();
Response.Cookies(CookieName) = "";
Response.Cookies(CookieName).Expires = "1/1/90";
}

UltraCart.prototype.BuildInsertColumnList = ultracart_BuildInsertColumnList;
function ultracart_BuildInsertColumnList(uniqueCol, destTableCols)
{
var colList = "[" + uniqueCol + "]";
for (var i = 0; i < destTableCols.length; i++)
{
if (destTableCols[i] != "")
{
colList += ", [" + destTableCols[i] + "]";
}
}
colList = "(" + colList + ")";
return colList;
}

UltraCart.prototype.BuildInsertValueList = ultracart_BuildInsertValueList;
function ultracart_BuildInsertValueList(isUniqueColNumeric, uniqueColValue, mappedCartCols, iRow)
{
var values;
if(isUniqueColNumeric)
{
values = uniqueColValue;
}
else
{
values = "'" + uniqueColValue.toString().replace(/'/g, "''") + "'";
}

var index;
for (var iCol=0; iCol<mappedCartCols.length; iCol++)
{
index = this.GetIndexOfColName(mappedCartCols[iCol]);

if (this.cartColType[iCol] != "text")
{
this.Assert(!isNaN(this.CartDataTable[index][iRow]), "SaveCart: A non-numeric value was found in the column " + this.cartColNames[iCol] + " when the database was only expecting numeric values.");
values += ", " + this.CartDataTable[index][iRow];
}
else
{
values += ", '" + (this.CartDataTable[index][iRow]).toString().replace(/'/g, "''") + "'";
}
}
return "(" + values + ")";
}

UltraCart.prototype.SaveCart = ultracart_SaveCart;
function ultracart_SaveCart(adoConn,dbTable,uniqueCol,isUni queColNumeric,uniqueColValue,cartCols,orderDetails ColNames)
{
trapError = true; // helps debug errors that maybe caught

cartCols = this.VbToJsArray(cartCols);
orderDetailsColNames = this.VbToJsArray(orderDetailsColNames);

var insertColList = this.BuildInsertColumnList(uniqueCol,orderDetailsC olNames);
var insertClause = "INSERT INTO " + dbTable + " " + insertColList + " VALUES ";

var trapSQL = "SQL dump:
";
for (var iRow=0; iRow<this.GetItemCount(); iRow++)
{
var valList = this.BuildInsertValueList(isUniqueColNumeric,uniqu eColValue,cartCols,iRow);
var sqlInsertStatement = insertClause + valList;

trapSQL += "[" + iRow + "] " + "
" + sqlInsertStatement + "
";

if(trapError)
{
try
{
adoConn.Execute(sqlInsertStatement,"", 1);
}
catch(e)
{
this.SaveError(e, trapSQL);
return 1;
}
}
else
{
adoConn.Execute(sqlInsertStatement,"", 1);
}
}
return 0;
}

UltraCart.prototype.SaveError = ultracart_SaveError;
function ultracart_SaveError(e,sql)
{
Session("UC_ErrorNumber") = (e.number & 0xFFFF);
Session("UC_Description") = e.description;
Session("UC_SQLStatement") = sql;
}
</script>