/*===============================================================
	Variables - custom code
===============================================================*/
	var gaAcctNum = ""; // 
	var m_gaAcctNum = ""; // master account

/*===============================================================
	Supporting Functions
===============================================================*/
	function evenueLinkTracker(link) {
		var tracker = _gaq._getAsyncTracker();
		var gaLinkString = tracker._getLinkerUrl(link,true);
		window.open(gaLinkString, '_blank');
		return false;
	}
	
	function gaAddItem(orderID, sku, productName, category, price, qty) {
		if(gaAcctNum!="") {
			window._gaq.push(['_addItem',
				orderID,				// order number
				sku,					// event code
				productName,			// event desc
				category,				// price level
				amountFormat(price),	// amount
				qty						// quantity 
			]);
		}
		if(m_gaAcctNum!="") {
			window._gaq.push(['m._addItem',
				orderID,				// order number
				sku,					// event code
				productName,			// event desc
				category,				// price level
				amountFormat(price),	// amount
				qty						// quantity 
			]);
		}
	}

/*===============================================================
	Initialize Google Analytics
===============================================================*/
	var _gaq = _gaq || [];
	
	if(gaAcctNum!="") {
		_gaq.push(['_setAccount', gaAcctNum]);
		_gaq.push(['_setDomainName', window.location.hostname]);
		_gaq.push(['_setAllowLinker', true]);
		_gaq.push(['_setAllowHash', false]);
		_gaq.push(['_setAllowAnchor', true]);
		_gaq.push(['_trackPageview']);
	}
	
	if(m_gaAcctNum!="") {
		_gaq.push(['m._setAccount', m_gaAcctNum]);
		_gaq.push(['m._setDomainName', window.location.hostname]);
		_gaq.push(['m._setAllowLinker', true]);
		_gaq.push(['m._setAllowHash', false]);
		_gaq.push(['m._setAllowAnchor', true]);
		_gaq.push(['m._trackPageview']);
	}
	
/*===============================================================
	Ecommerce Portion
===============================================================*/
	if(typeof(order)!="undefined" && typeof(order.number)!="undefined" && typeof(cartCharges)!="undefined") {
	
		// ========== TRANSACTION OBJECT ==========:
		_gaq.push(['_addTrans',
			order.number,										// order number
			linkID,												// Affiliation
			amountFormat(Number(cartCharges.total)),			// Total
			"blank_text",										// Tax
			amountFormat(Number(cartCharges.delivery)+Number(cartCharges.order)), // Shipping (delivery and order charge)
			tktBillAddr.city,									// City
			tktBillAddr.state,									// State
			tktBillAddr.country									// Country
		]);
		
		// ========== Ticket Purchases ==========:
		for(var icount=0; icount<itemList_.length; icount++){
			// ========== Single Items ==========:						
			if(itemList_[icount].type=="SE") {			
				for(var j=0; j < itemList_[icount].reservationList.length; j++) {
					for(var k=0; k < itemList_[icount].reservationList[j].lineItemList.length; k++) {
						var temp_item = itemList_[icount].reservationList[j].lineItemList[k];
						gaAddItem(
							order.number,								// order number
							itemList_[icount].eventInfo.eventCode,		// event code
							itemList_[icount].eventInfo.description,	// event desc
							temp_item.description,						// price level
							amountFormat(temp_item.price),				// amount
							temp_item.qty								// quantity 
						);
					}
				}
			// ========== Combo Items ==========:							  
			} else if(itemList_[icount].type=="ST" || itemList_[icount].type=="DS") {					  	
				for(var j=0; j<itemList_[icount].priceTypeQtyList.length; j++) {
					var temp_item = itemList_[icount].priceTypeQtyList[j];
					gaAddItem(
						order.number,									// order number
						itemList_[icount].ticketInfo.eventCode,			// event code
						itemList_[icount].ticketInfo.description,		// event desc
						temp_item.description,							// price level
						amountFormat(temp_item.price),					// amount
						temp_item.qty									// quantity
					);
				}
			// ========== Misc Items ==========:
			} else if(itemList_[icount].type=="MI") {					  	
				for(var j=0; j < itemList_[icount].reservationList.length; j++) {
					var temp_item = itemList_[icount].reservationList[j].lineItem;
					gaAddItem(
						order.number,									// order number
						itemList_[icount].itemInfo.eventCode,			// event code
						itemList_[icount].itemInfo.description,			// event desc
						'Miscellaneous Item Purchases',					// price level
						amountFormat(Number(temp_item.price)+Number(temp_item.perItemFee)),	// amount
						temp_item.qty									// quantity 
					);
				}
			// ========== GC Items ==========:
			} else if(itemList_[icount].type=="GC") {	
				for(var j=0; j < itemList_[icount].reservationList.length; j++) {
					var temp_item = itemList_[icount].reservationList[j].lineItem;
					gaAddItem(
						order.number,									// order number
						itemList_[icount].itemInfo.eventCode,			// event code
						itemList_[icount].itemInfo.description,			// event desc
						'Gift Certificate Purchases',					// price level
						amountFormat(Number(temp_item.price)+Number(temp_item.perItemFee)),	// amount
						temp_item.qty									// quantity
					);
				}
			}
		}
		
		// ========== Donations ==========:
		for(var i=0; i < cartPledgeList_.length; i++) {
			gaAddItem(
				order.number,									// order number
				cartPledgeList_[i].code,						// event code
				cartPledgeList_[i].fundDrive,					// event desc
				'Donations',									// price level
				amountFormat(cartPledgeList_[i].paymentAmount),	// amount
				cartPledgeList_[i].paymentPlan					// quantity (payment plan)
			);
		}
		for(var i=0; i < cartPledgePayList_.length; i++) {
			gaAddItem(
				order.number,									// order number
				cartPledgePayList_[i].pledgePayCode,			// event code
				cartPledgePayList_[i].fundDrive,				// event desc
				'Donation/Pledge Payments',						// price level
				amountFormat(cartPledgePayList_[i].amount),		// amount
				'1'												// quantity 
			);
		}
		
		// ========== Transfers ==========:
		for(var i=0; i < cartAcceptTTList_.length; i++) {
			var transQty = cartAcceptTTList_[i].itemQty;
			if(cartAcceptTTList_[i].itemType=="TI") {
				for(var j=0; j<cartAcceptTTList_[i].seatList.length; j++) {
					transQty = Number(transQty) + Number(cartAcceptTTList_[i].seatList[j].qty);
				}
			}
			gaAddItem(
				order.number,									// order number
				cartAcceptTTList_[i].transferCode,				// event code
				cartAcceptTTList_[i].itemDesc,					// event desc
				'Transfers',									// price level
				amountFormat(cartAcceptTTList_[i].transferFee),	// amount
				transQty										// quantity 
			);
		}
		
		// ========== Renewals ==========:
		for(var i=0; i < cartRenewalList_.length; i++) {
			gaAddItem(
				order.number,									// order number
				cartRenewalList_[i].appId,						// event code
				cartRenewalList_[i].name,						// event desc
				'Renewal Applications',							// price level
				amountFormat(cartRenewalList_[i].amount),		// amount
				'1'												// quantity 
			);
		}
		
		// ========== Item Packages ==========:
		for(var i=0; i < cartPKList_.length; i++) {
			gaAddItem(
				order.number,									// order number
				cartPKList_[i].pkListInfo.cartPKNum,			// event code
				cartPKList_[i].pkItemList[0].itemInfo.pkDesc,	// event desc
				'Item Packages',								// price level
				cartPKList_[i].pkItemList[0].itemInfo.pkTotal,	// amount
				'1'												// quantity
			);
		}
		
		// ========== submits transaction to the Analytics servers ==========:
		
		if(gaAcctNum!="") {
			_gaq.push(['_trackTrans']);
		}
		
		if(m_gaAcctNum!="") {
			_gaq.push(['m._trackTrans']);
		}
	}

	(function() {
		var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
		ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	})();
