// MVGA Ecommerce Tracker
///////////////

function MVGA_Tracker( product_link_prefix, category_name, product_code, product_name, mvga_basketitems, mvga_orderitems )
{
	this.category_name			= category_name;
	this.product_link_prefix	= product_link_prefix;
	this.product_code 			= product_code;
	this.product_name 			= product_name;
	this.basketitems			= mvga_basketitems;
	this.orderitems				= mvga_orderitems;

	this.basketitems_lookup		= [];
	this.orderitems_lookup		= [];

	this.product_links			= [];
	this.product_attributes		= [];
	this.upsell_products		= [];

	this.main_product_form		= null;
	
	this.Page_Dispatch();
}

MVGA_Tracker.prototype.Page_Dispatch = function()
{
	if ( Screen == 'BASK' )			this.Process_BASK();
	else if ( Screen == 'PROD' )	this.Process_PROD();
	else if ( Screen == 'OUS1' ) 	this.Process_OUS1(); 
	else if ( Screen == 'OUSM' ) 	this.Process_OUSM(); 
	else if ( Screen == 'OSEL' ) 	this.Process_OSEL(); 

	if ( Screen == 'CTGY' ||
  		 Screen == 'PLST' ||
		 Screen == 'SRCH' )		
	{
		this.Process_List_Page();
	}
}

MVGA_Tracker.prototype.Process_BASK = function()
{
	var self = this;
	var i, i_len, action, page_forms;

	page_forms = document.getElementsByTagName( 'form' );

	for ( i = 0, i_len = page_forms.length; i < i_len; i++ )
	{
		action = page_forms[ i ].elements[ 'Action' ];

		if ( !action ) continue;

		this.Build_BasketAndOrderItem_Lookup();

		if ( action.value == 'RGRP' || action.value == 'RPRD' )
		{
			AddEvent( page_forms[ i ], 'submit', function( e )
			{
				return self.RemoveFromBasket( e );
			} );
		}
		else if ( action.value == 'QNTY' || action.value == 'QTYG' )
		{
			AddEvent( page_forms[ i ], 'submit', function( e )
			{
				return self.UpdateBasketQuantity( e );
			} );
		}
	}
}

MVGA_Tracker.prototype.RemoveFromBasket = function( e )
{
	var remove_form, basketitem_id;

	e 				= e || window.event;
	remove_form 	= e.srcElement || e.target;
	
	if ( !remove_form || !remove_form.elements ) return true;

	basketitem_id	= remove_form.elements[ 'Basket_Group' ] || remove_form.elements[ 'Basket_Line' ];

	if ( !basketitem_id ) return true;

	ga( 'require', 'ec' );

	ga( 'ec:addProduct', 
	{
		'id'		: this.basketitems_lookup[ basketitem_id.value ].code,
		'name'		: this.basketitems_lookup[ basketitem_id.value ].name,
		'quantity'	: this.basketitems_lookup[ basketitem_id.value ].quantity
	} );

	ga( 'ec:setAction', 'remove' );

	ga( 'send', 'event', 'remove_button', 'click', 'remove from cart', { 'hitCallback': function()
	{
		remove_form.submit();
	} } );

	return eventPreventDefault( e );
}

MVGA_Tracker.prototype.UpdateBasketQuantity = function( e )
{
	var quantity_form, basketitem_id, quantity;

	e 				= e || window.event;
	quantity_form 	= e.srcElement || e.target;

	if ( !quantity_form || !quantity_form.elements ) return true;

	basketitem_id	= quantity_form.elements[ 'Basket_Line' ] || quantity_form.elements[ 'Basket_Group' ];
	quantity		= quantity_form.elements[ 'Quantity' ];

	if ( !basketitem_id || !quantity ) return true;

	ga( 'require', 'ec' );

	if ( quantity.value > this.basketitems_lookup[ basketitem_id.value ].quantity )
	{
		ga( 'ec:addProduct', 
		{
			'id'		: this.basketitems_lookup[ basketitem_id.value ].code,
			'name'		: this.basketitems_lookup[ basketitem_id.value ].name,
			'quantity'	: quantity.value - this.basketitems_lookup[ basketitem_id.value ].quantity
		} );

		ga( 'ec:setAction', 'add' );

		ga( 'send', 'event', 'update_quantity', 'click', 'Update basket quantity', { 'hitCallback': function()
		{
			quantity_form.submit();
		} } );
	}
	else if ( quantity.value < this.basketitems_lookup[ basketitem_id.value ].quantity )
	{
		ga( 'ec:addProduct', 
		{
			'id'		: this.basketitems_lookup[ basketitem_id.value ].code,
			'name'		: this.basketitems_lookup[ basketitem_id.value ].name,
			'quantity'	: this.basketitems_lookup[ basketitem_id.value ].quantity - quantity.value 
		} );

		ga( 'ec:setAction', 'remove' );

		ga( 'send', 'event', 'update_quantity', 'click', 'Update basket quantity', { 'hitCallback': function()
		{
			quantity_form.submit();
		} } );
	}

	return eventPreventDefault( e );
}

MVGA_Tracker.prototype.Process_PROD = function()
{
	var self = this;
	
	this.Find_PROD_ADPR_Forms();
	this.Find_List_ADPR_Forms();
}

MVGA_Tracker.prototype.Find_PROD_Attributes = function()
{
	var i, i_len, name, index;

	if ( !this.main_product_form || !this.main_product_form.elements ) return;

	for ( i = 0; i < this.main_product_form.elements.length; i++ )
	{
		if ( typeof this.main_product_form.elements[ i ].type !== 'string' ||
			 typeof this.main_product_form.elements[ i ].name !== 'string' )															continue;
		if ( this.main_product_form.elements[ i ].type.toLowerCase() != 'hidden' )														continue;
		if ( this.main_product_form.elements[ i ].name.indexOf( ']:code' ) != this.main_product_form.elements[ i ].name.length - 6 )	continue;
		if ( this.main_product_form.elements[ i ].name.indexOf( 'Product_Attributes[' ) != 0 )											continue;

		name	= this.main_product_form.elements[ i ].name.replace( /Product_Attributes\[/g, '' );
		name	= name.replace( / /g, '' );
		name	= name.replace( /\]:code/g, '' );

		index	= parseInt( name );

		this.product_attributes[ index ] 		= new Object();
		this.product_attributes[ index ].code	= this.main_product_form.elements[ i ].value; 
		this.product_attributes[ index ].value	= '';
	}

	for ( i = 0; i < this.main_product_form.elements.length; i++ )
	{
		if ( typeof this.main_product_form.elements[ i ].type !== 'string' ||
			 typeof this.main_product_form.elements[ i ].name !== 'string' )															continue;
		if ( this.main_product_form.elements[ i ].name.indexOf( ']:value' ) != this.main_product_form.elements[ i ].name.length - 7 )	continue;
		if ( this.main_product_form.elements[ i ].name.indexOf( 'Product_Attributes[' ) != 0 )											continue;
		if ( this.main_product_form.elements[ i ].disabled )																			continue;

		name	= this.main_product_form.elements[ i ].name.replace( /Product_Attributes\[/g, '' );
		name	= name.replace( / /g, '' );
		name	= name.replace( /\]:value/g, '' );
		name 	= parseInt( name );

		if ( this.main_product_form.elements[ i ].type.toLowerCase() == 'select-one' )
		{
			this.product_attributes[ name ].value = encodeURIComponent( this.main_product_form.elements[ i ].options[ this.main_product_form.elements[ i ].selectedIndex ].value );
		}
		else if ( this.main_product_form.elements[ i ].type.toLowerCase() == 'radio' )
		{
			if ( this.main_product_form.elements[ i ].checked ) this.product_attributes[ name ].value = encodeURIComponent( this.main_product_form.elements[ i ].value );
		}
		else if ( this.main_product_form.elements[ i ].type.toLowerCase() == 'checkbox' )
		{
			if ( this.main_product_form.elements[ i ].checked )	this.product_attributes[ name ].value = encodeURIComponent( this.main_product_form.elements[ i ].value );
		}
		else if ( this.main_product_form.elements[ i ].type == 'text' )
		{
			if ( this.main_product_form.elements[ i ].value.length ) this.product_attributes[ name ].value = 'populated';
		}
		else if ( this.main_product_form.elements[ i ].type == 'textarea' )
		{
			if ( this.main_product_form.elements[ i ].value.length ) this.product_attributes[ name ].value = 'populated';
		}
	}
}

MVGA_Tracker.prototype.Find_PROD_ADPR_Forms = function()
{
	var self = this;
	var i, i_len, action, page_forms, product_code;

	page_forms = document.getElementsByTagName( 'form' );

	for ( i = 0, i_len = page_forms.length; i < i_len; i++ )
	{
		action			= page_forms[ i ].elements[ 'Action' ];
		product_code	= page_forms[ i ].elements[ 'Product_Code' ];

		if ( !action || !product_code ) 			continue;
		if ( action.value != 'ADPR' )				continue;

		if ( product_code.value != Product_Code )	continue;
	
		AddEvent( page_forms[ i ], 'submit', function( e )
		{
			self.Find_PROD_Attributes();
			self.AddToBasketSubmit( e );
		} );

		this.main_product_form = page_forms[ i ];
		break;
	}
}

MVGA_Tracker.prototype.Process_OUS1 = function()
{
	var self = this;
	var i, i_len, action, page_forms, product_code;

	page_forms = document.getElementsByTagName( 'form' );

	for ( i = 0, i_len = page_forms.length; i < i_len; i++ )
	{
		action			= page_forms[ i ].elements[ 'Action' ];
		product_code	= page_forms[ i ].elements[ 'Product_Code' ];

		if ( !action || !product_code )	continue;
		if ( action.value != 'AUPR' )	continue;

		AddEvent( page_forms[ i ], 'submit', function( e )
		{
			return self.UpsellOUS1FormSubmit( e );
		} );
	}
}

MVGA_Tracker.prototype.Process_OUSM = function()
{
	var self = this;
	var i, i_len, action, page_forms;

	page_forms = document.getElementsByTagName( 'form' );

	for ( i = 0, i_len = page_forms.length; i < i_len; i++ )
	{
		action = page_forms[ i ].elements[ 'Action' ];

		if( !action || action.value != 'AUPM' )	continue;

		AddEvent( page_forms[ i ], 'submit', function( e )
		{
			self.Find_OUSM_Selection( e );
			return self.UpsellFormSubmit( e );
		} );

		break;
	}
}

MVGA_Tracker.prototype.UpsellOUS1FormSubmit = function( e )
{
	var i, upsell_form, product_code;

	e 			= e || window.event;
	upsell_form = e.srcElement || e.target;

	if ( !upsell_form || !upsell_form.elements ) return true;

	product_code	= upsell_form.elements[ 'Product_Code' ];	

	if ( !product_code ) return true;

	ga( 'require', 'ec' );

	ga( 'ec:addProduct', 
	{
		'name'		: product_code.value,
		'quantity'	: 1 
	} );

	ga( 'ec:setAction', 'add' );

	ga( 'send', 'event', 'upsell_add_to_cart', 'click', 'Upsell', { 'hitCallback': function()
	{
		upsell_form.submit();
	} } );

	return eventPreventDefault( e );
}

MVGA_Tracker.prototype.UpsellFormSubmit = function( e )
{
	var i, upsell_form;

	e 			= e || window.event;
	upsell_form = e.srcElement || e.target;

	if ( !upsell_form || !upsell_form.elements ) return true;

	ga( 'require', 'ec' );

	for ( i = 0; i < this.upsell_products.length; i++ )
	{
		if ( !this.upsell_products[ i ] ) continue;

		ga( 'ec:addProduct', 
		{
			'name'		: this.upsell_products[ i ].code,
			'quantity'	: 1 
		} );
	}
	
	ga( 'ec:setAction', 'add' );

	ga( 'send', 'event', 'upsell_add_to_cart', 'click', 'Upsell', { 'hitCallback': function()
	{
		upsell_form.submit();
	} } );

	return eventPreventDefault( e );
}

MVGA_Tracker.prototype.Find_OUSM_Selection = function( e )
{
	var i, name, index, ousm_form;
	
	e 			= e || window.event;
	ousm_form 	= e.srcElement || e.target;

	if ( !ousm_form || !ousm_form.elements ) return;

	for ( i = 0; i < ousm_form.elements.length; i++ )
	{
		if ( typeof ousm_form.elements[ i ].type !== 'string' ||
			 typeof ousm_form.elements[ i ].name !== 'string' )												continue;
		if ( ousm_form.elements[ i ].type.toLowerCase() != 'hidden' )										continue;
		if ( ousm_form.elements[ i ].name.indexOf( ']:code' ) != ousm_form.elements[ i ].name.length - 6 )	continue;
		if ( ousm_form.elements[ i ].name.indexOf( 'Product[' ) != 0 )										continue;

		name	= ousm_form.elements[ i ].name.replace( /Product\[/g, '' );
		name	= name.replace( / /g, '' );
		name	= name.replace( /\]:code/g, '' );

		index	= parseInt( name );

		this.upsell_products[ index ] 		= new Object();
		this.upsell_products[ index ].code	= ousm_form.elements[ i ].value; 
		this.upsell_products[ index ].value	= '';
	}

	for ( i = 0; i < ousm_form.elements.length; i++ )
	{
		if ( typeof ousm_form.elements[ i ].type !== 'string' ||
			 typeof ousm_form.elements[ i ].name !== 'string' )											continue;
		if ( ousm_form.elements[ i ].type.toLowerCase() != 'checkbox' )									continue;
		if ( ousm_form.elements[ i ].name.indexOf( ']' ) != ousm_form.elements[ i ].name.length - 1 )	continue;
		if ( ousm_form.elements[ i ].name.indexOf( 'Upsell_Selected[' ) != 0 )							continue;
		if ( ousm_form.elements[ i ].disabled )															continue;

		name	= ousm_form.elements[ i ].name.replace( /Upsell_Selected\[/g, '' );
		name	= name.replace( / /g, '' );
		name	= name.replace( /\]/g, '' );
		name 	= parseInt( name );

		if ( ousm_form.elements[ i ].checked )
		{
			this.upsell_products[ name ].value = encodeURIComponent( ousm_form.elements[ i ].value );
		}
	}
}

MVGA_Tracker.prototype.Process_OSEL = function()
{
	var self = this;
	var i, form_screen, page_forms;

	page_forms = document.getElementsByTagName( 'form' );

	for ( i = 0; i < page_forms.length; i++ )
	{
		form_shipping 	= page_forms[ i ].elements[ 'ShippingMethod' ];
		form_payment 	= page_forms[ i ].elements[ 'PaymentMethod' ];

		if ( !form_shipping || !form_payment ) continue;

		this.Build_BasketAndOrderItem_Lookup();

		AddEvent( page_forms[ i ], 'submit', function( e )
		{
			return self.OSELFormSubmit( e );
		} );

		break;
	}
}

MVGA_Tracker.prototype.OSELFormSubmit = function( e )
{
	var i, osel_form, shipping_selector, payment_selector, shipping_selection, payment_selection;

	e 					= e || window.event;
	osel_form			= e.srcElement || e.target; 

	if ( !osel_form || !osel_form.elements ) return true;

	shipping_selection	= '';
	payment_selection	= '';

	shipping_selector	= osel_form.elements[ 'ShippingMethod' ];
	payment_selector	= osel_form.elements[ 'PaymentMethod' ];

	if ( shipping_selector ) 	shipping_selection = shipping_selector.options[ shipping_selector.selectedIndex ].value;
	if ( payment_selector ) 	payment_selection = payment_selector.options[ payment_selector.selectedIndex ].value;

	ga( 'ec:setAction', 'checkout_option', { 'step': 1, 'option': shipping_selection + ', ' + payment_selection } );

	ga( 'send', 'event', 'Checkout', 'Option', 
	{
		hitCallback: function()
		{
			if ( osel_form ) osel_form.submit();
		}
	} );

	return eventPreventDefault( e );
}

MVGA_Tracker.prototype.AddToBasketSubmit = function( e )
{
	var product_form, product_code, quantity;
	
	e 				= e || window.event;
	product_form 	= e.srcElement || e.target;

	if ( !product_form || !product_form.elements ) return true; 

	product_code	= product_form.elements[ 'Product_Code' ];
	quantity		= product_form.elements[ 'Quantity' ];

	if ( !product_code || !quantity ) return true;

	ga( 'require', 'ec' );

	ga( 'ec:addProduct', 
	{
		'id'		: product_code.value,
		'name'		: product_code.value,
		'quantity'	: quantity ? parseInt( quantity.value ) : 1,
		'variant'	: this.Generate_Variant_Label(),
		'category'	: this.category_name
	} );

	ga( 'ec:setAction', 'add' );

	ga( 'send', 'event', 'detail_add_to_cart', 'click', 'add to cart', { 'hitCallback': function()
	{
		product_form.submit();
	} } );

	return eventPreventDefault( e );
}

MVGA_Tracker.prototype.Generate_Variant_Label = function()
{
	var i, i_len, label;

	label = '';

	for ( i = 0, i_len = this.product_attributes.length; i < i_len; i++ )
	{
		if ( !this.product_attributes[ i ] ) continue;

		if ( this.product_attributes[ i ].value )
		{
			if ( label.length ) label += ', ';
			label += this.product_attributes[ i ].code + ': ' + this.product_attributes[ i ].value;
		}	
	}

	return label;
}

MVGA_Tracker.prototype.List_AddToBasketSubmit = function( e )
{
	var product_form, product_code;

	e 				= e || window.event;
	product_form	= e.srcElement || e.target;
	
	if ( !product_form || !product_form.elements ) return true;

	product_code	= product_form.elements[ 'Product_Code' ];

	if ( !product_code ) return true;

	ga( 'require', 'ec' );

	ga( 'ec:addProduct', 
	{
		'id'		: product_code.value,
		'name'		: product_code.value,
		'quantity'	: 1,
		'category'	: this.category_name
	} );
	
	ga( 'ec:setAction', 'add', { 'list' : this.Get_Listing_Label( Screen ) } );

	ga( 'send', 'event', 'list_add_to_cart', 'click', 'add to cart', { 'hitCallback': function()
	{
		product_form.submit();
	} } );

	return eventPreventDefault( e );
}

MVGA_Tracker.prototype.Find_List_ADPR_Forms = function()
{
	var self = this;
	var i, i_len, action, page_forms, product_code;

	page_forms = document.getElementsByTagName( 'form' );

	for ( i = 0, i_len = page_forms.length; i < i_len; i++ )
	{
		action			= page_forms[ i ].elements[ 'Action' ];
		product_code	= page_forms[ i ].elements[ 'Product_Code' ];

		if ( !action || !product_code ) 			continue;
		if ( product_code.value == Product_Code ) 	continue;
		if ( action.value != 'ADPR' )				continue;
	
		AddEvent( page_forms[ i ], 'submit', function( e )
		{
			return self.List_AddToBasketSubmit( e );
		} );
	}
}

MVGA_Tracker.prototype.Process_List_Page = function()
{
	var self = this;
	var i, i_len, page_links, product_links;

	page_links	= document.getElementsByTagName( 'a' );

	for ( i = 0, i_len = page_links.length; i < i_len; i++ )
	{
		if ( page_links[ i ].getAttribute( 'data-mm-linktype' ) === 'product-list-link' )
		{
			AddEvent( page_links[ i ], 'click', function( e )
			{
				return self.ProductLinkClick( e );
			} );
		}
	}

	this.Find_List_ADPR_Forms();
}

MVGA_Tracker.prototype.ProductLinkClick = function( e )
{
	e = e || window.event;

	var product_link = e.srcElement || e.target;

	if ( !product_link ) return true;

	if ( product_link.nodeName == 'IMG' ) product_link = product_link.parentNode;

	if( !product_link.href ) return true;

	ga( 'require', 'ec' );

	for ( i = 0; i < mvga_productlist.length; i++ )
	{
		ga( 'ec:addProduct', 
		{
			'id'		: mvga_productlist[ i ].code,
			'name'		: mvga_productlist[ i ].name,
			'category'	: this.category_name,
			'position'	: mvga_productlist[ i ].position
		} );

		ga( 'ec:setAction', 'click', { 'list' : this.Get_Listing_Label( Screen ) } );

		ga( 'send', 'event', 'product_list_click', 'click', 'View Product Details', { 'hitCallback': function()
		{ 
			document.location.href = product_link.href;
		} } );
	}

	return eventPreventDefault( e );
}

MVGA_Tracker.prototype.Build_BasketAndOrderItem_Lookup = function()
{
	var i, i_len;

	for ( i = 0, i_len = this.basketitems.length; i < i_len; i++ )
	{
		this.basketitems_lookup[ this.basketitems[ i ].line_id ] 			= new Object();
		this.basketitems_lookup[ this.basketitems[ i ].line_id ].code 		= this.basketitems[ i ].code;
		this.basketitems_lookup[ this.basketitems[ i ].line_id ].name		= this.basketitems[ i ].name;
		this.basketitems_lookup[ this.basketitems[ i ].line_id ].price 		= this.basketitems[ i ].price;
		this.basketitems_lookup[ this.basketitems[ i ].line_id ].quantity 	= this.basketitems[ i ].quantity;
	}

	for ( i = 0, i_len = this.orderitems.length; i < i_len; i++ )
	{
		this.orderitems_lookup[ this.orderitems[ i ].line_id ] 				= new Object();
		this.orderitems_lookup[ this.orderitems[ i ].line_id ].code 		= this.orderitems[ i ].code;
		this.orderitems_lookup[ this.orderitems[ i ].line_id ].name			= this.orderitems[ i ].name;
		this.orderitems_lookup[ this.orderitems[ i ].line_id ].price		= this.orderitems[ i ].price;
		this.orderitems_lookup[ this.orderitems[ i ].line_id ].quantity 	= this.orderitems[ i ].quantity;
	}
}

MVGA_Tracker.prototype.Get_Listing_Label = function( screen )
{
	switch ( screen )
	{
		case 'PROD' :	return 'Related Products';
		case 'CTGY' :	return 'Category Listing';
		case 'PLST' :	return 'All Products';
		case 'SRCH' :	return 'Search Results';
	}
}

function getScopedElementsByClassName( className, scope )
{
    var regex_match, all_elements, results, i;
    
    regex_match     = new RegExp( "(?:^|\\s)" + className + "(?:$|\\s)" );
    all_elements    = scope.getElementsByTagName( '*' );
    results         = [];
    
    for ( i = 0; all_elements[ i ] != null; i++ )
    {
		if ( all_elements[ i ].className && ( all_elements[ i ].className.indexOf( className ) != -1 ) && regex_match.test( all_elements[ i ].className ) )
		{
			results.push( all_elements[ i ] );
		}
    }
    
    return results;
}

function AddEvent( obj, eventType, fn  )
{
	if( obj.addEventListener )
	{
		obj.addEventListener( eventType, fn, false );
		return true;
	}
	else if( obj.attachEvent )
	{
		var r = obj.attachEvent( 'on' + eventType, fn );
		return r;
	}
	else
	{
		return false;
	}
}

function eventPreventDefault( event )
{
	if ( event.preventDefault )
	{
		return event.preventDefault();
	}

	event.returnValue = false;

	return false;
}
