//=====================================================================||
//               NOP Design JavaScript Shopping Cart                   ||
//                                                                     ||
// For more information on SmartSystems, or how NOPDesign can help you ||
// Please visit us on the WWW at http://www.nopdesign.com              ||
//                                                                     ||
// Javascript portions of this shopping cart software are available as ||
// freeware from NOP Design.  You must keep this comment unchanged in  ||
// your code.  For more information contact FreeCart@NopDesign.com.    ||
//                                                                     ||
// JavaScript Shop Module, V.4.4.0                                     ||
//=====================================================================||

//---------------------------------------------------------------------||
//                       Global Options                                ||
//                      ----------------                               ||
// Shopping Cart Options, you can modify these options to change the   ||
// the way the cart functions.                                         ||
//                                                                     ||
// Language Packs                                                      ||
// ==============                                                      ||
// You may include any language pack before nopcart.js in your HTML    ||
// pages to change the language.  Simply include a language pack with  ||
// a script src BEFORE the <SCRIPT SRC="nopcart.js">... line.          ||
//  For example: <SCRIPT SRC="language-en.js"></SCRIPT>                ||
//                                                                     ||
// Options For Everyone:                                               ||
// =====================                                               ||
// * MonetarySymbol: string, the symbol which represents dollars/euro, ||
//   in your locale.                                                   ||
// * DisplayNotice: true/false, controls whether the user is provided  ||
//   with a popup letting them know their product is added to the cart ||
// * DisplayShippingColumn: true/false, controls whether the managecart||
//   and checkout pages display shipping cost column.                  ||
// * DisplayShippingRow: true/false, controls whether the managecart   ||
//   and checkout pages display shipping cost total row.               ||
// * DisplayTaxRow: true/false, controls whether the managecart        ||
//   and checkout pages display tax cost total row.                    ||
// * TaxRate: number, your area's current tax rate, ie: if your tax    ||
//   rate was 7.5%, you would set TaxRate = 0.075                      ||
// * TaxByRegion: true/false, when set to true, the user is prompted   ||
//   with TaxablePrompt to determine if they should be charged tax.    ||
//   In the USA, this is useful to charge tax to those people who live ||
//   in a particular state, but no one else.                           ||
// * TaxPrompt: string, popup message if user has not selected either  ||
//   taxable or nontaxable when TaxByRegion is set to true.            ||
// * TaxablePrompt: string, the message the user is prompted with to   ||
//   select if they are taxable.  If TaxByRegion is set to false, this ||
//   has no effect. Example: 'Arizona Residents'                       ||
// * NonTaxablePrompt: string, same as above, but the choice for non-  ||
//   taxable people.  Example: 'Other States'                          ||
// * MinimumOrder: number, the minium dollar amount that must be       ||
//   purchased before a user is allowed to checkout.  Set to 0.00      ||
//   to disable.                                                       ||
// * MinimumOrderPrompt: string, Message to prompt users with when     ||
//   they have not met the minimum order amount.                       ||
//                                                                     ||
// Payment Processor Options:                                          ||
// ==========================                                          ||
// * PaymentProcessor: string, the two digit payment processor code    ||
//   for support payment processor gateways.  Setting this field to    ||
//   anything other than an empty string will override your OutputItem ||
//   settings -- so please be careful when receiving any form data.    ||
//   Support payment processor gateways are:                           ||
//    * Authorize.net (an)                                             ||
//    * Worldpay      (wp)                                             ||
//    * LinkPoint     (lp)
//                                                                     ||
// Options For Programmers:                                            ||
// ========================                                            ||
// * OutputItem<..>: string, the name of the pair value passed at      ||
//   checkouttime.  Change these only if you are connecting to a CGI   ||
//   script and need other field names, or are using a secure service  ||
//   that requires specific field names.                               ||
// * AppendItemNumToOutput: true/false, if set to true, the number of  ||
//   each ordered item will be appended to the output string.  For     ||
//   example if OutputItemId is 'ID_' and this is set to true, the     ||
//   output field name will be 'ID_1', 'ID_2' ... for each item.       ||
// * HiddenFieldsToCheckout: true/false, if set to true, hidden fields ||
//   for the cart items will be passed TO the checkout page, from the  ||
//   ManageCart page.  This is set to true for CGI/PHP/Script based    ||
//   checkout pages, but should be left false if you are using an      ||
//   HTML/Javascript Checkout Page. Hidden fields will ALWAYS be       ||
//   passed FROM the checkout page to the Checkout CGI/PHP/ASP/Script  ||
//---------------------------------------------------------------------||

if ( typeof NW == 'undefined' ) {
	NW = function() { };
}

NW.Cart = function() {

	//Options for Everyone:
	MonetarySymbol        = ' &euro;';
	DisplayNotice         = true;
	DisplayShippingColumn = true;
	DisplayShippingRow    = true;
	DisplayTaxRow         = true;
	TaxRate               = 0.21;
	TaxByRegion           = false;
	TaxPrompt             = 'For tax purposes, please select if you are an Arizona resident before continuing';
	TaxablePrompt         = 'Arizona Residents';
	NonTaxablePrompt      = 'Other States';
	MinimumOrder          = 0.00;
	MinimumOrderPrompt    = 'Your order is below our minimum order, please order more before checking out.';

	//Payment Processor Options:
	PaymentProcessor      = '';

	//Options for Programmers:
	OutputItemId          = 'ID_';
	OutputItemQuantity    = 'QUANTITY_';
	OutputItemPrice       = 'PRICE_';
	OutputItemName        = 'NAME_';
	OutputItemShipping    = 'SHIPPING_';
	OutputItemAddtlInfo   = 'ADDTLINFO_';
	OutputOrderSubtotal   = 'SUBTOTAL';
	OutputOrderShipping   = 'SHIPPING';
	OutputOrderTax        = 'TAX';
	OutputOrderTotal      = 'TOTAL';
	AppendItemNumToOutput = true;
	HiddenFieldsToCheckout = false;

	//=====================================================================||
	//---------------------------------------------------------------------||
	//    YOU DO NOT NEED TO MAKE ANY MODIFICATIONS BELOW THIS LINE        ||
	//---------------------------------------------------------------------||
	//=====================================================================||

	//---------------------------------------------------------------------||
	//                      Language Strings                               ||
	//                     ------------------                              ||
	// These strings will not be used unless you have not included a       ||
	// language pack already.  You should NOT modify these, but instead    ||
	// modify the strings in language-**.js where ** is the language pack  ||
	// you are using.                                                      ||
	//---------------------------------------------------------------------||
	if ( !bLanguageDefined ) {
		strEmpty   = 'Your cart is empty';
		strSorry   = 'I\'m Sorry, your cart is full, please proceed to checkout.';
		strAdded   = ' added to your shopping cart.';
		strRemove  = 'Click "Ok" to remove this product from your shopping cart.';
		strILabel  = 'Product Id';
		strDLabel  = 'Product Name/Description';
		strQLabel  = 'Quantity';
		strPLabel  = 'Price';
		strSLabel  = 'Shipping';
		strRLabel  = 'Remove From Cart';
		strRButton = 'Remove';
		strSUB     = 'SUBTOTAL';
		strSHIP    = 'SHIPPING';
		strTAX     = 'TAX';
		strTOT     = 'TOTAL';
		strErrQty  = 'Invalid Quantity.';
		strNewQty  = 'Please enter new quantity:';
		bLanguageDefined = true;
	}

	//---------------------------------------------------------------------||
	// FUNCTION:    getCookieVal                                           ||
	// PARAMETERS:  offset                                                 ||
	// RETURNS:     URL unescaped Cookie Value                             ||
	// PURPOSE:     Get a specific value from a cookie                     ||
	//---------------------------------------------------------------------||
	function getCookieVal(offset) {
		var endstr = document.cookie.indexOf (';', offset);
		if ( endstr == -1 ) {
			endstr = document.cookie.length;
		}
		return (unescape(document.cookie.substring(offset, endstr)));
	}

	//---------------------------------------------------------------------||
	// FUNCTION:    FixCookieDate                                          ||
	// PARAMETERS:  date                                                   ||
	// RETURNS:     date                                                   ||
	// PURPOSE:     Fixes cookie date, stores back in date                 ||
	//---------------------------------------------------------------------||
	function FixCookieDate(date) {
		var base = new Date(0),
			skew = base.getTime();
		date.setTime (date.getTime() - skew);
	}

	//---------------------------------------------------------------------||
	// FUNCTION:    GetCookie                                              ||
	// PARAMETERS:  Name                                                   ||
	// RETURNS:     Value in Cookie                                        ||
	// PURPOSE:     Retrieves cookie from users browser                    ||
	//---------------------------------------------------------------------||
	function GetCookie(name) {
		var i = 0, j,
			arg = name + '=',
			alen = arg.length,
			clen = document.cookie.length;
		while ( i < clen ) {
			j = i + alen;
			if ( document.cookie.substring(i, j) == arg ) {
				return(getCookieVal (j));
			}
			i = document.cookie.indexOf(' ', i) + 1;
			if ( i === 0 ) {
				break;
			}
		}
		return (null);
	}

	//---------------------------------------------------------------------||
	// FUNCTION:    SetCookie                                              ||
	// PARAMETERS:  name, value, expiration date, path, domain, security   ||
	// RETURNS:     Null                                                   ||
	// PURPOSE:     Stores a cookie in the users browser                   ||
	//---------------------------------------------------------------------||
	function SetCookie(name, value, expires, path, domain, secure) {
		document.cookie = name + '=' + escape (value) +
					((expires) ? '; expires=' + expires.toGMTString() : '') +
					((path) ? '; path=' + path : '') +
					((domain) ? '; domain=' + domain : '') +
					((secure) ? '; secure' : '');
	}

	//---------------------------------------------------------------------||
	// FUNCTION:    DeleteCookie                                           ||
	// PARAMETERS:  Cookie name, path, domain                              ||
	// RETURNS:     null                                                   ||
	// PURPOSE:     Removes a cookie from users browser.                   ||
	//---------------------------------------------------------------------||
	function DeleteCookie (name, path, domain) {
		if ( GetCookie(name) ) {
			document.cookie = name + '=' +
						((path) ? '; path=' + path : '') +
						((domain) ? '; domain=' + domain : '') +
						'; expires=Thu, 01-Jan-70 00:00:01 GMT';
		}
	}

	//---------------------------------------------------------------------||
	// FUNCTION:    MoneyFormat                                            ||
	// PARAMETERS:  Number to be formatted                                 ||
	// RETURNS:     Formatted Number                                       ||
	// PURPOSE:     Reformats Dollar Amount to #.## format                 ||
	//---------------------------------------------------------------------||
	function moneyFormat(input) {
		var cents, decimalAt,
			tmp = new String(input),
			dollars = Math.floor(input);
		for ( decimalAt = 0; decimalAt < tmp.length; decimalAt++ ) {
			if ( tmp.charAt(decimalAt) == '.' ) {
				break;
			}
		}
		cents = '' + Math.round(input * 100);
		cents = cents.substring(cents.length - 2, cents.length)
		dollars += ((tmp.charAt(decimalAt + 2) == '9') && (cents == '00')) ? 1 : 0;
		if ( cents == '0' ) {
			cents = '00';
		}
		return (dollars + '.' + cents);
	}

	//---------------------------------------------------------------------||
	// FUNCTION:    RadioChecked                                           ||
	// PARAMETERS:  Radio button to check                                  ||
	// RETURNS:     True if a radio has been checked                       ||
	// PURPOSE:     Form fillin validation                                 ||
	//---------------------------------------------------------------------||
	function RadioChecked( radiobutton ) {
		var bChecked = false,
			rlen = radiobutton.length;
		for ( i = 0; i < rlen; i++ ) {
			if ( radiobutton[i].checked ) {
				bChecked = true;
			}
		}
		return bChecked;
	}

	//---------------------------------------------------------------------||
	// FUNCTION:    QueryString                                            ||
	// PARAMETERS:  Key to read                                            ||
	// RETURNS:     value of key                                           ||
	// PURPOSE:     Read data passed in via GET mode                       ||
	//---------------------------------------------------------------------||
	QueryString.keys = new Array();
	QueryString.values = new Array();
	function QueryString(key) {
		var i, value = null;
		for ( i = 0; i < QueryString.keys.length; i++ ) {
			if ( QueryString.keys[i] == key ) {
				value = QueryString.values[i];
				break;
			}
		}
		return value;
	}

	//---------------------------------------------------------------------||
	// FUNCTION:    QueryString_Parse                                      ||
	// PARAMETERS:  (URL string)                                           ||
	// RETURNS:     null                                                   ||
	// PURPOSE:     Parses query string data, must be called before Q.S.   ||
	//---------------------------------------------------------------------||
	function QueryString_Parse() {
		var i,
			query = window.location.search.substring(1);
			pairs = query.split('&');
		for ( i = 0; i < pairs.length; i++ ) {
			var pos = pairs[i].indexOf('=');
			if ( pos >= 0 ) {
				var argname = pairs[i].substring(0, pos);
				var value = pairs[i].substring(pos + 1);
				QueryString.keys[QueryString.keys.length] = argname;
				QueryString.values[QueryString.values.length] = value;
			}
		}
	}

	//---------------------------------------------------------------------||
	// FUNCTION:    ValidateCart                                           ||
	// PARAMETERS:  Form to validate                                       ||
	// RETURNS:     true/false                                             ||
	// PURPOSE:     Validates the managecart form                          ||
	//---------------------------------------------------------------------||
	var g_TotalCost = 0;
	function ValidateCart( theForm ) {
		if ( TaxByRegion ) {
			if ( !RadioChecked(eval('theForm.' + OutputOrderTax)) ) {
				alert( TaxPrompt );
				return false;
			}
		}
		if ( MinimumOrder >= 0.01 ) {
			if ( g_TotalCost < MinimumOrder ) {
				alert( MinimumOrderPrompt );
				return false;
			}
		}
		return true;
	}

	//---------------------------------------------------------------------||
	// FUNCTION:    GetItem                                                ||
	// PARAMETERS:  Item number                                            ||
	// RETURNS:     Array of fields in item                                ||
	// PURPOSE:     Retrieve order item fields                             ||
	//---------------------------------------------------------------------||
	function GetItem(item) {
		var Token0, Token1, Token2, Token3, Token4,
			fields = [], database = GetCookie('Order.' + item);
		Token0 = database.indexOf('|', 0);
		Token1 = database.indexOf('|', Token0+1);
		Token2 = database.indexOf('|', Token1+1);
		Token3 = database.indexOf('|', Token2+1);
		Token4 = database.indexOf('|', Token3+1);
		fields[0] = database.substring( 0, Token0 );
		fields[1] = database.substring( Token0+1, Token1 );
		fields[2] = database.substring( Token1+1, Token2 );
		fields[3] = database.substring( Token2+1, Token3 );
		fields[4] = database.substring( Token3+1, Token4 );
		fields[5] = database.substring( Token4+1, database.length );
		return fields;
	}

	return {

		//---------------------------------------------------------------------||
		// FUNCTION:    Add                                                    ||
		// PARAMETERS:  Form Object                                            ||
		// RETURNS:     Cookie to user's browser, with prompt                  ||
		// PURPOSE:     Adds a product to the user's shopping cart             ||
		//---------------------------------------------------------------------||
		Add : function(thisForm) {
			var	notice = '',
				iNumberOrdered = 0,
				bAlreadyInCart = false;
			iNumberOrdered = GetCookie('NumberOrdered');
			if ( !iNumberOrdered ) {
				iNumberOrdered = 0;
			}
			if ( !thisForm.ID_NUM ) {
				strID_NUM    = '';
			} else {
				strID_NUM    = thisForm.ID_NUM.value;
			}
			if ( !thisForm.QUANTITY ) {
				strQUANTITY  = '1';
			} else {
				strQUANTITY  = thisForm.QUANTITY.value;
			}
			if ( !thisForm.PRICE ) {
				strPRICE     = '0.00';
			} else {
				strPRICE     = thisForm.PRICE.value;
			}
			if ( !thisForm.NAME ) {
				strNAME      = '';
			} else {
				strNAME      = thisForm.NAME.value;
			}
			if ( !thisForm.SHIPPING ) {
				strSHIPPING  = '0.00';
			} else {
				strSHIPPING  = thisForm.SHIPPING.value;
			}
			if ( !thisForm.ADDITIONALINFO ) {
				strADDTLINFO = '';
			} else {
				strADDTLINFO = thisForm.ADDITIONALINFO[thisForm.ADDITIONALINFO.selectedIndex].value;
			}
			if ( thisForm.ADDITIONALINFO2 ) {
				strADDTLINFO += '; ' + thisForm.ADDITIONALINFO2[thisForm.ADDITIONALINFO2.selectedIndex].value;
			}
			if ( thisForm.ADDITIONALINFO3 ) {
				strADDTLINFO += '; ' + thisForm.ADDITIONALINFO3[thisForm.ADDITIONALINFO3.selectedIndex].value;
			}
			if ( thisForm.ADDITIONALINFO4 ) {
				strADDTLINFO += '; ' + thisForm.ADDITIONALINFO4[thisForm.ADDITIONALINFO4.selectedIndex].value;
			}
			//Is this product already in the cart? If so, increment quantity instead of adding another.
			for ( i = 1; i <= iNumberOrdered; i++ ) {
				fields = GetItem(i);
				if ( fields[0] == strID_NUM &&
					fields[2] == strPRICE &&
					fields[3] == strNAME &&
					fields[5] == strADDTLINFO ) {
					bAlreadyInCart = true;
					dbUpdatedOrder = strID_NUM +
									'|' + (parseInt(strQUANTITY) + parseInt(fields[1])) +
									'|' + strPRICE +
									'|' + strNAME +
									'|' + strSHIPPING +
									'|' + strADDTLINFO;
					strNewOrder = 'Order.' + i;
					DeleteCookie(strNewOrder, '/');
					SetCookie(strNewOrder, dbUpdatedOrder, null, '/');
					notice = strQUANTITY + ' ' + strNAME + strAdded;
					break;
				}
			}
			if ( !bAlreadyInCart ) {
				iNumberOrdered++;
				if ( iNumberOrdered > 12 ) {
					alert( strSorry );
				} else {
					dbUpdatedOrder = strID_NUM +
									'|' + strQUANTITY +
									'|' + strPRICE +
									'|' + strNAME +
									'|' + strSHIPPING +
									'|' + strADDTLINFO;
					strNewOrder = 'Order.' + iNumberOrdered;
					SetCookie(strNewOrder, dbUpdatedOrder, null, '/');
					SetCookie("NumberOrdered", iNumberOrdered, null, '/');
					notice = strQUANTITY + ' ' + strNAME + strAdded;
				}
			}
			if ( DisplayNotice ) {
				alert(notice);
			}
			return false;
		},

		//---------------------------------------------------------------------||
		// FUNCTION:    Manage                                                 ||
		// PARAMETERS:  Null                                                   ||
		// RETURNS:     Product Table Written to Document                      ||
		// PURPOSE:     Draws current cart product table on HTML page          ||
		//---------------------------------------------------------------------||
		Manage : function( el ) {
			var iNumberOrdered = 0;    //Number of products ordered
			var fTotal         = 0;    //Total cost of order
			var fTax           = 0;    //Tax amount
			var fShipping      = 0;    //Shipping amount
			var strTotal       = '';   //Total cost formatted as money
			var strTax         = '';   //Total tax formatted as money
			var strShipping    = '';   //Total shipping formatted as money
			var strOutput      = '';   //String to be written to page
			var bDisplay       = true; //Whether to write string to the page (here for programmers)
			iNumberOrdered = GetCookie('NumberOrdered');
			if ( iNumberOrdered === null ) {
				iNumberOrdered = 0;
			}
			if ( bDisplay ) {
				strOutput = '<table width="100%" class="nopcart"><tr>' +
							'<td class="nopheader"><b>' + strILabel + '</b></td>' +
							'<td class="nopheader"><b>' + strDLabel + '</b></td>' +
							'<td class="nopheader"><b>' + strQLabel + '</b></td>' +
							'<td class="nopheader"><b>' + strPLabel + '</b></td>' +
							(DisplayShippingColumn ? '<td class="nopheader" align="center"><b>' + strSLabel + '</b></td>' : '') +
							'<td class="nopheader"><b>' + strRLabel + '</b></td></tr>';
			}
			if ( iNumberOrdered === 0 ) {
				strOutput += '<tr><td colspan="6" class="nopentry"><center><br ><b>' + strEmpty + '</b><br ><br ></center></td></tr>';
			}
			for ( i = 1; i <= iNumberOrdered; i++ ) {
				fields = GetItem(i);
				fTotal     += (parseInt(fields[1]) * parseFloat(fields[2]) );
				fShipping  += (parseInt(fields[1]) * parseFloat(fields[4]) );
				fTax        = (fTotal * TaxRate);
				strTotal    = moneyFormat(fTotal);
				strTax      = moneyFormat(fTax);
				strShipping = moneyFormat(fShipping);
				if ( bDisplay ) {
					strOutput += '<tr><td class="nopentry">'  + fields[0] + '</td>';
					if ( fields[5] == '' ) {
						strOutput += '<td width="200" class="nopentry">'  + fields[3] + '</td>';
					} else {
						strOutput += '<td width="200" class="nopentry">'  + fields[3] + ' - <i>' + fields[5] + '</i></td>';
					}
					strOutput += '<td class="nopentry" align="center"><input type="text" name="Q" size="2" value="' + fields[1] + '" onchange="NW.Cart.Change(' + i + ', this.value);"></td>';
					strOutput += '<td class="nopentry" align="right">' + moneyFormat(fields[2]) + MonetarySymbol + '</td>';
					if ( DisplayShippingColumn ) {
						if ( parseFloat(fields[4]) > 0 ) {
							strOutput += '<td class="nopentry">' + moneyFormat(fields[4]) + MonetarySymbol  + '</td>';
						} else {
							strOutput += '<td class="nopentry" align="right">N/A</td>';
						}
					}
					strOutput += '<td class="nopentry" align="center"><input type="button" value=" ' + strRButton + ' " onclick="NW.Cart.Remove(' + i + ')" class="nopbutton"></td></tr>';
				}
				if ( AppendItemNumToOutput ) {
					strFooter = i;
				} else {
					strFooter = '';
				}
				if ( HiddenFieldsToCheckout ) {
					strOutput += '<input type="hidden" name="' + OutputItemId        + strFooter + '" value="' + fields[0] + '">';
					strOutput += '<input type="hidden" name="' + OutputItemQuantity  + strFooter + '" value="' + fields[1] + '">';
					strOutput += '<input type="hidden" name="' + OutputItemPrice     + strFooter + '" value="' + fields[2] + '">';
					strOutput += '<input type="hidden" name="' + OutputItemName      + strFooter + '" value="' + fields[3] + '">';
					strOutput += '<input type="hidden" name="' + OutputItemShipping  + strFooter + '" value="' + fields[4] + '">';
					strOutput += '<input type="hidden" name="' + OutputItemAddtlInfo + strFooter + '" value="' + fields[5] + '">';
				}
			}
			if ( bDisplay ) {
				strOutput += '<tr><td class="noptotal" colspan="4"><b>' + strSUB + '</b></td>';
				strOutput += '<td class="noptotal" colspan="2" align="right"><b>' +  (strTotal || '0.00') + MonetarySymbol + '</b></td>';
				strOutput += '</tr>';
				if ( DisplayShippingRow ) {
					strOutput += '<tr><td class="noptotal" colspan="4"><b>' + strSHIP + '</b></td>';
					strOutput += '<td class="noptotal" colspan="2" align="right"><b>' + (strShipping || '0.00') + MonetarySymbol + '</b></td>';
					strOutput += '</tr>';
				}
				if ( DisplayTaxRow || TaxByRegion ) {
					if ( TaxByRegion ) {
						strOutput += '<tr><td class="noptotal" colspan="4"><b>' + strTAX + ' ' + (TaxRate * 100) + '%</b></td>';
						strOutput += '<td class="noptotal" colspan="2" align="right"><b>';
						strOutput += '<br ><input type=radio name="' + OutputOrderTax + '" value="' + strTax + '">';
						strOutput += TaxablePrompt + ': ' + strTax + MonetarySymbol;
						strOutput += '<br ><input type=radio name="' + OutputOrderTax + '" value="0.00">';
						strOutput += NonTaxablePrompt + ': ' + '0.00' + MonetarySymbol;
						strOutput += '</b></td>';
						strOutput += '</tr>';
					} else {
						strOutput += '<tr><td class="noptotal" colspan="4"><b>' + strTAX + ' ' + (TaxRate * 100) + '%</b></td>';
						strOutput += '<td class="noptotal" colspan="2" align="right"><b>' + (strTax || '0.00') + MonetarySymbol + '</b></td>';
						strOutput += '</tr>';
					}
				}
				if ( !TaxByRegion ) {
					strOutput += '<tr><td class="noptotal" colspan="4"><b>' + strTOT + '</b></td>';
					strOutput += '<td class="noptotal" colspan="2" align="right"><b>'+ moneyFormat((fTotal + fShipping + fTax)) + MonetarySymbol + '</b></td>';
					strOutput += '</tr>';
				}
				strOutput += '</table>';
				if ( HiddenFieldsToCheckout ) {
					strOutput += '<input type="hidden" name="' + OutputOrderSubtotal + '" value="' + strTotal + MonetarySymbol + '">';
					strOutput += '<input type="hidden" name="' + OutputOrderShipping + '" value="' + strShipping + MonetarySymbol  + '">';
					strOutput += '<input type="hidden" name="' + OutputOrderTax + '"      value="' + strTax + MonetarySymbol + '">';
					strOutput += '<input type="hidden" name="' + OutputOrderTotal + '"    value="' + moneyFormat((fTotal + fShipping + fTax)) + MonetarySymbol + '">';
				}
			}
			g_TotalCost = (fTotal + fShipping + fTax);
			el.innerHTML = strOutput;
		},

		//---------------------------------------------------------------------||
		// FUNCTION:    Remove                                                 ||
		// PARAMETERS:  Order Number to Remove                                 ||
		// RETURNS:     Null                                                   ||
		// PURPOSE:     Removes an item from a users shopping cart             ||
		//---------------------------------------------------------------------||
		Remove : function(RemOrder) {
			if ( confirm( strRemove ) ) {
				NumberOrdered = GetCookie('NumberOrdered');
				for ( i = RemOrder; i < NumberOrdered; i++ ) {
					NewOrder1 = 'Order.' + (i + 1);
					NewOrder2 = 'Order.' + (i);
					database = GetCookie(NewOrder1);
					SetCookie(NewOrder2, database, null, '/');
				}
				NewOrder = 'Order.' + NumberOrdered;
				SetCookie('NumberOrdered', NumberOrdered - 1, null, '/');
				DeleteCookie(NewOrder, '/');
				this.Manage(document.getElementsByClassName('scmanage')[0]);
			}
		},

		//---------------------------------------------------------------------||
		// FUNCTION:    Change                                                 ||
		// PARAMETERS:  Order Number to Change Quantity                        ||
		// RETURNS:     Null                                                   ||
		// PURPOSE:     Changes quantity of an item in the shopping cart       ||
		//---------------------------------------------------------------------||
		Change : function(OrderItem, NewQuantity) {
			if ( isNaN(NewQuantity) ) {
				alert( strErrQty );
			} else {
				fields = GetItem(OrderItem);
				dbUpdatedOrder = fields[0] +
								'|' + NewQuantity +
								'|' + fields[2] +
								'|' + fields[3] +
								'|' + fields[4] +
								'|' + fields[5];
				strNewOrder = 'Order.' + OrderItem;
				DeleteCookie(strNewOrder, '/');
				SetCookie(strNewOrder, dbUpdatedOrder, null, '/');
				this.Manage(document.getElementsByClassName('scmanage')[0]);
			}
		},

		//---------------------------------------------------------------------||
		// FUNCTION:    Checkout                                               ||
		// PARAMETERS:  Null                                                   ||
		// RETURNS:     Product Table Written to Document                      ||
		// PURPOSE:     Draws current cart product table on HTML page for      ||
		//              checkout.                                              ||
		//---------------------------------------------------------------------||
		Checkout : function( el ) {
			var iNumberOrdered = 0;    //Number of products ordered
			var fTotal         = 0;    //Total cost of order
			var fTax           = 0;    //Tax amount
			var fShipping      = 0;    //Shipping amount
			var strTotal       = '';   //Total cost formatted as money
			var strTax         = '';   //Total tax formatted as money
			var strShipping    = '';   //Total shipping formatted as money
			var strOutput      = '';   //String to be written to page
			var bDisplay       = true; //Whether to write string to the page (here for programmers)
			var strPP          = '';   //Payment Processor Description Field
			iNumberOrdered = GetCookie('NumberOrdered');
			if ( iNumberOrdered === null ) {
				iNumberOrdered = 0;
			}
			if ( TaxByRegion ) {
				QueryString_Parse();
				fTax = parseFloat( QueryString( OutputOrderTax ) );
				strTax = moneyFormat(fTax);
			}
			if ( bDisplay ) {
				strOutput = '<table width="100%" class="nopcart"><tr>' +
							'<td class="nopheader"><b>' + strILabel + '</b></td>' +
							'<td class="nopheader"><b>' + strDLabel + '</b></td>' +
							'<td class="nopheader"><b>' + strQLabel + '</b></td>' +
							'<td class="nopheader"><b>' + strPLabel + '</b></td>' +
							(DisplayShippingColumn?'<td class="nopheader"><b>' + strSLabel + '</b></td>':'') +
							'</tr>';
			}
			for ( i = 1; i <= iNumberOrdered; i++ ) {
				fields = GetItem(i);
				fTotal += (parseInt(fields[1]) * parseFloat(fields[2]) );
				fShipping += (parseInt(fields[1]) * parseFloat(fields[4]) );
				if ( !TaxByRegion ) {
					fTax = (fTotal * TaxRate);
				}
				strTotal = moneyFormat(fTotal);
				if ( !TaxByRegion ) {
					strTax = moneyFormat(fTax);
				}
				strShipping = moneyFormat(fShipping);
				if ( bDisplay ) {
					strOutput += '<tr><td class="nopentry">'  + fields[0] + '</td>';

					if ( fields[5] == '' ) {
						strOutput += '<td width="200" class="nopentry">'  + fields[3] + '</td>';
					} else {
						strOutput += '<td width="200" class="nopentry">'  + fields[3] + ' - <i>' + fields[5] + '</i></td>';
					}
					strOutput += '<td width="64" class="nopentry" align="center">' + fields[1] + '</td>';
					strOutput += '<td class="nopentry" align="center">' + (moneyFormat(fields[2]) || '0.00') + MonetarySymbol + '</td>';

					if ( DisplayShippingColumn ) {
						if ( parseFloat(fields[4]) > 0 ) {
							strOutput += '<td width="128" class="nopentry" align="right">' + (moneyFormat(fields[4]) || '0.00') + MonetarySymbol + '</td>';
						} else {
							strOutput += '<td width="128" class="nopentry" align="right">N/A</td>';
						}
					}
					strOutput += '</tr>';
				}
				if ( AppendItemNumToOutput ) {
					strFooter = i;
				} else {
					strFooter = '';
				}
				if ( PaymentProcessor != '' ) {
					//Process description field for payment processors instead of hidden values.
					//Format Description of product as:
					// ID, Name, Qty X
					strPP += fields[0] + ', ' + fields[3];
					if ( fields[5] != '' ) {
						strPP += ' - ' + fields[5];
					}
					strPP += ', Qty. ' + fields[1] + "\n";
				} else {
					strOutput += '<input type="hidden" name="' + OutputItemId        + strFooter + '" value="' + fields[0] + '">';
					strOutput += '<input type="hidden" name="' + OutputItemQuantity  + strFooter + '" value="' + fields[1] + '">';
					strOutput += '<input type="hidden" name="' + OutputItemPrice     + strFooter + '" value="' + fields[2] + '">';
					strOutput += '<input type="hidden" name="' + OutputItemName      + strFooter + '" value="' + fields[3] + '">';
					strOutput += '<input type="hidden" name="' + OutputItemShipping  + strFooter + '" value="' + fields[4] + '">';
					strOutput += '<input type="hidden" name="' + OutputItemAddtlInfo + strFooter + '" value="' + fields[5] + '">';
				}

			}
			if ( bDisplay ) {
				strOutput += '<tr><td class="noptotal" colspan="3"><b>' + strSUB + '</b></td>';
				strOutput += '<td class="noptotal" colspan="2" align="right"><b>' + (strTotal || '0.00') + MonetarySymbol + '</b></td>';
				strOutput += '</tr>';
				if ( DisplayShippingRow ) {
					strOutput += '<tr><td class="noptotal" colspan="3"><b>' + strSHIP + '</b></td>';
					strOutput += '<td class="noptotal" colspan="2" align="right"><b>' + (strShipping || '0.00') + MonetarySymbol + '</b></td>';
					strOutput += '</tr>';
				}
				if ( DisplayTaxRow || TaxByRegion ) {
					strOutput += '<tr><td class="noptotal" colspan="3"><b>' + strTAX + ' ' + (TaxRate * 100) + '%</b></td>';
					strOutput += '<td class="noptotal" colspan="2" align="right"><b>' + (strTax || '0.00') + MonetarySymbol + '</b></td>';
					strOutput += '</tr>';
				}
				strOutput += '<tr><td class="noptotal" colspan="3"><b>' + strTOT + '</b></td>';
				strOutput += '<td class="noptotal" colspan="2" align="right"><b>' + (moneyFormat((fTotal + fShipping + fTax)) || '0.00') + MonetarySymbol + '</b></td>';
				strOutput += '</tr>';
				strOutput += '</table>';
				if ( PaymentProcessor == 'an') {
					//Process this for Authorize.net WebConnect
					strOutput += '<input type="hidden" name="x_Version" value="3.0">';
					strOutput += '<input type="hidden" name="x_Show_Form" value="PAYMENT_FORM">';
					strOutput += '<input type="hidden" name="x_Description" value="' + strPP + '">';
					strOutput += '<input type="hidden" name="x_Amount" value="' + moneyFormat((fTotal + fShipping + fTax)) + '">';
				} else if ( PaymentProcessor == 'wp') {
					//Process this for WorldPay
					strOutput += '<input type="hidden" name="desc" value="' + strPP + '">';
					strOutput += '<input type="hidden" name="amount" value="' + moneyFormat((fTotal + fShipping + fTax)) + '">';
				} else if ( PaymentProcessor == 'lp') {
					//Process this for LinkPoint         
					strOutput += '<input type="hidden" name="mode" value="fullpay">';
					strOutput += '<input type="hidden" name="chargetotal" value="' + moneyFormat((fTotal + fShipping + fTax)) + '">';
					strOutput += '<input type="hidden" name="tax" value="' + (strTax || '0.00') + MonetarySymbol + '">';
					strOutput += '<input type="hidden" name="subtotal" value="' + (strTotal || '0.00') + MonetarySymbol + '">';
					strOutput += '<input type="hidden" name="shipping" value="' + (strShipping || '0.00') + MonetarySymbol + '">';
					strOutput += '<input type="hidden" name="desc" value="' + strPP + '">';
				} else {
					strOutput += '<input type="hidden" name="' +OutputOrderSubtotal + '" value="' + (strTotal || '0.00') + MonetarySymbol + '">';
					strOutput += '<input type="hidden" name="' +OutputOrderShipping + '" value="' + (strShipping || '0.00') + MonetarySymbol + '">';
					strOutput += '<input type="hidden" name="' +OutputOrderTax + '"      value="' + (strTax || '0.00') + MonetarySymbol + '">';
					strOutput += '<input type="hidden" name="' +OutputOrderTotal + '"    value="' + (moneyFormat((fTotal + fShipping + fTax)) || '0.00') + MonetarySymbol + '">';
				}
			}
			el.innerHTML = strOutput;
		},

		//---------------------------------------------------------------------||
		// FUNCTION:    Check                                                  ||
		// PARAMETERS:  Quantity to                                            ||
		// RETURNS:     Quantity as a number, and possible alert               ||
		// PURPOSE:     Make sure quantity is represented as a number          ||
		//---------------------------------------------------------------------||
		Check : function(checkString) {
			var strNewQuantity = '';
			for ( i = 0; i < checkString.length; i++ ) {
				ch = checkString.substring(i, i+1);
				if ( (ch >= '0' && ch <= '9') || (ch == '.') ) {
					strNewQuantity += ch;
				}
			}
			if ( strNewQuantity.length < 1 ) {
				strNewQuantity = '1';
			}
			return(strNewQuantity);
		},

		//---------------------------------------------------------------------||
		// FUNCTION:    Update                                                 ||
		// PARAMETERS:  Form element                                           ||
		// RETURNS:     true if cart is found, false otherwise                 ||
		// PURPOSE:     Add cart list to form for submission                   ||
		//---------------------------------------------------------------------||
		Update : function(orderForm) {
			var iNumberOrdered = 0;
			iNumberOrdered = GetCookie('NumberOrdered');
			if ( iNumberOrdered === null ) {
				iNumberOrdered = 0;
			}
			for ( i = 1; i <= iNumberOrdered; i++ ) {
				fields = GetItem(i);
				orderForm['shoplist'].value += escape(fields.join('|')) + '\n';
			}
		},

		List : function() {

			var i, item, fields, items = [ ],
				iNumberOrdered = GetCookie('NumberOrdered') || 0;

			for ( i = 1; i <= iNumberOrdered; i++ ) {
				fields = GetItem(i);
				item = { };
				item['item_number_' + i] = fields[0];
				item['quantity_'    + i] = fields[1];
				item['amount_'      + i] = fields[2];
				item['item_name_'   + i] = fields[3];
				item['shipping_'    + i] = fields[4];
				items.push(item);
			}

			return items;
		},

		Post: function(url, data) {

			var i, form, frag, html, length, main, name, parms,
				URL = /(https?:\/\/([\w]+-?[\w]+)((?:\.[\w]+-?[\w]+){1,}))/;

			if (URL.test(url) && data && data.cmd && data.upload &&
				data.business && data.items && data.items.length) {
				parms = data['items'];
				length = data['items'].length;
				frag = document.createDocumentFragment();
				main = frag.appendChild(document.createElement('div'));
				html = '<form method="post" action="' + url + '">';
				for (name in data) {
					if (name !== 'items') {
						html += '<input type="hidden" value="' + data[name] + '" name="' + name + '">';
					}
				}
				for (i = 0; length > i; ++i) {
					for (name in parms[i]) {
						html += '<input type="hidden" value="' + parms[i][name] + '" name="' + name + '">';
					}
				}
				html += '</form>';
				main.innerHTML = html;
				form = main.firstChild;
				frag.replaceChild(form, main);
				document.body.appendChild(frag);
				form.submit();
			}

		},

		Send: function(form) {

			var order = { };

			order.lc = 'IT';
			order.cmd = '_cart';
			order.upload = '1';
			order.charset = 'utf-8';
			order.currency_code = 'EUR';
			order.business = 'dperini@nwbox.com';

			form.first_name && (order.first_name = form.first_name.value);
			form.last_name && (order.last_name = form.last_name.value);
			form.address1 && (order.address1 = form.address1.value);
			form.email && (order.email = form.email.value);
			form.city && (order.city = form.city.value);
			form.zip && (order.zip = form.zip.value);
			form.state && (order.state = form.state.value);
			form.country && (order.country = form.country.value);
			form.area && (order.night_phone_a = form.area.value);
			form.phone && (order.night_phone_b = form.phone.value);

			order.items = NW.Cart.List();

			NW.Cart.Post('https://www.sandbox.paypal.com/cgi-bin/webscr', order);

		}

	};

	//=====================================================================||
	//               END NOP Design SmartPost Shopping Cart                ||
	//=====================================================================||

}();

// document.getElementsByClassName
if (!document.getElementsByClassName) {
	// (c)lass string to match
	// (n)ode ref (start from)
	// (t)ag only include type
	// (d)ocument root context
	document.getElementsByClassName=
		function(c,n,t,d){
			var e,i,j=0,r=[];t=t||'*';n=n||d||document;e=n.getElementsByTagName(t);
			i=e.length-1;while(i>=0){if((" "+e[i].className+" ").indexOf(c)>0){r[j]=e[i];j++;}i--;}
			return r;
		};
}

function start() {
	var el = document.getElementsByClassName('scmanage');
	if (el.length > 0) {
		NW.Cart.Manage(el[0]);
		return;
	}
	el = document.getElementsByClassName('sccheckout');
	if (el.length > 0) {
		NW.Cart.Checkout(el[0]);
		return;
	}
}

if (window.addEventListener) {
	window.addEventListener('load', start, false);
} else if (window.attachEvent) {
	window.attachEvent('onload', start);
} else {
	window.onload = start;
}

