/*
 * This Javascript that contains the SiteCatalyst variables used for Omniture data collection. This javascript must be included as a part of
 * omniture library to send the data to Omniture.This javascript contains the different Tag method called from JSP or Javascript.
 * The different tags are pageview tag,error tag,shop cart tag, order confirmation tag, product tag, 
 * registration tag and custom (category) tag. 
 * Note: Before adding any new parameter to any method make sure that calling program should also be updated.
 * Also modify the company_name for your implementation. Based on the solution design you can change/remove the custom variable
 * used in the javascript below. Javascript method's can be customizable as per your implementation.
 * 
 * Version - 1.1
 */

//This is global variable
var company_name='hhgregg'; // change as per your implementation.
var sProducts = "";
var sCartProducts = "";
var sOmnitureDeliveryPrice = "";

//sending the data to Omniture
function sendToOmniture() {
	var s_code=s.t();if(s_code)document.write(s_code);
}


/*
 * Method used for page view data collection. Add any extra param before that change the corresponding JSTL tag
 * to pass extra parameter.
 * @param page_id - page name
 * @param server_name - app server name
 * @param channel_name - commerce command name
 * @param search_term - Search Term used while searching. Value passed from search results page
 * @param search_results - # of search results.
 * @param contentType - Content Type of the page i.e for home page the value is 'HomePage'.
 */
function omniturePageViewTag(page_id,search_term,search_results,page_type,cha_nme,userType,mean_search,event,xparam) {
	var error="";
	if(xparam != undefined){
		var xparamTokenizedArray = xparam.split("|");
		error = xparamTokenizedArray[0];
	}
	
	if(page_type) {
		page_type = page_type.replace(/\|/g,":")
		s.pageName = page_id + page_type;
	} else	
		s.pageName = page_id;
	
	s.server = window.location.hostname;
	if(cha_nme != '')
		s.channel = cha_nme;

	if(event)
		s.events = event;
	if(event = "event49"){
		s.eVar49 = page_id+":"+error;
	}

	if(userType == 'R'){
		s.prop1='logged-in';
		s.eVar5 = 'logged-in';
		var mem = dojo.cookie('_om_var');
		if(mem != undefined) {
			mem = mem.split("|");
			var goldMem = mem[0].split("=");
			if(goldMem[1] == 'true' ) {
				s.prop2 = "hhgold member";
				s.eVar6 = "hhgold member";
			} else {
				s.prop2 = "non member";
				s.eVar6 = "non member";
			}
			
			var id = mem[1].split("=");
			s.eVar46 = id[1];
			s.prop41 = id[1];
			
		}
	}else{
		s.prop1='guest';
		s.prop2 = "non member";
		s.eVar6 = "non member"
		s.eVar5 = 'guest';
	}
	

	if(search_term) {
		s.eVar1=search_term;
		if(mean_search) {
			s.prop17 = search_term;
			s.prop18 = search_results;
			s.prop21 = search_term;
			s.events = "event3";
			s.eVar4 = search_term;
		}  else{
			if(search_results == '0') {
				s.prop17 = search_term;
				s.prop18 = 'zero';
				s.events = "event1,event2";
			} else {
				s.prop17 = search_term;
				s.prop18 = search_results;
				s.events = "event1";
			}
		}
	}

	sendToOmniture();
}

/*
 * Method used for user session tracking information. This is used for tracking the user when logged in.
 * Add any extra param before that change the corresponding JSTL tag to pass extra parameter.
 * @param user_id - UserId of the userreg table
 * @param logon_id - logon_id of the userreg table same as SSO Id passed in SAML.
 * @param email_id - email id of the user. It will not be sent to Omniture.
 * @param csr_id - Customer Service representative user id passed in SAML.
 * @param user_type - User type R/G or A.
 * @param session_id - Broadvision session ID.
 * @param portal_authenticated - either Y or N passed in SAML.
 * @param group_id - Group Id passed in SAML. Identifying wether user is public access or not.
 * 
 */
function omnitureUserRegistrationTag(logon_id,email_id,type,id,mem) {
	if(type == "login") {
		s.events = "event7";
		s.eVar46 = id;
		s.prop41 = id;
	}
} 

/*
 * This is the custom Javascript method used for tracking the Category pages navigated by user. 
 * @param path - Bread Crumb of the categories
 * @param Type - Category or Subcategory
 * 
 */

function omnitureCategoryTag(path,type,var6,var7,var11,var12,var15,var16) {
	
	switch(type){
	
		case "home":
			s.prop3 = 'home';
			s.prop4 = 'home';
			s.prop5 = 'home';
			s.eVar9 =  'home';
			s.hier1 = 'home';
			break;
		
		case "Category":
			s.prop3 = path;
			s.prop4 = path;
			s.prop5='browse';
			s.eVar7 = path;
			s.eVar8 = path;
			s.eVar9 = path;
			s.hier1 = path;
			break;

		case "storeLocator":
			s.channel= 'customer service';
			s.events = 'event46';
			s.prop19 = path;
			s.eVar36 = path;
			s.prop5='storeLocator';
			s.prop20 = var6;
			break;			
			
		case "mobileCategory":
			var sub_category;
			var category;
			path = path.substring(1, path.length);
			if(path){
				var category_names = path.split('|');
				
				if(category_names != null) {
					category = category_names[0];
					if(category == null) {
						category = "";
					}
					sub_category= category_names[1];
					if(sub_category == null) {
						sub_category = "";
					}
					s.channel=category;
					s.prop3=category;
					s.prop4=category + '|'+sub_category;
					s.eVar7=category;
					s.eVar8= s.prop4;
					s.eVar9 = path;
					s.hier1 = path;
				}
			}
			break;
			
			
		case "SubCategory":
			var sub_category;
			var category;
			path = path.substring(1, path.length);
			if(path){
				var category_names = path.split('|');
				
				if(category_names != null) {
					category = category_names[0];
					if(category == null) {
						category = "";
					}
					s.prop3=category;
					s.prop4=path;
					s.eVar7=category;
					s.eVar8=path;
					s.eVar9 = path;
					s.hier1 = path;
					s.prop5='browse';
				}
			}
			break;
		
		case "product":
			var category_names = path.split('|');
			
			if(category_names != null) {
				category = category_names[1];
				if(category == null) {
					category = "";
				}
				subcategory = category_names[2];
				if(subcategory == null) {
					subcategory = "";
					s.hier1 = category; 
					s.eVar9 = category;
					s.prop4=category;
				} else {
					s.hier1 = category + "|" + subcategory;
					s.eVar9 = category + "|" + subcategory;
					s.prop4 = category + "|" + subcategory;
				}
				s.prop3=category;
			} 
			break;
			
		case "browse":
			var pageid;
			var root_category;
			var sub_category;
			var category;
			if(path){
				s.hier1 = path.substring(1, path.length); 
				s.eVar9 = path.substring(1, path.length);
				var category_names = path.split('|');
				
				if(category_names != null) {
					category = category_names[1];
					if(category == null) {
						category = "";
					}
					subcategory = category_names[2];
					if(subcategory == null) {
						subcategory = "";
					}
					s.prop3=category;
					s.eVar7=s.prop3;
					s.prop4=category + "|" + subcategory;
					s.eVar8=s.prop4;
					s.prop5='browse';
					s.prop6=var6;
					s.eVar13=var6;
					if(var7) {
						s.prop6=var7;
						s.eVar13=var7;
					}

					s.prop11=var11.substring(1, var11.length);
					s.eVar11 = s.prop11;
					
					s.prop12=var12.substring(1, var12.length);
					s.eVar12 = s.prop12;
	
					s.prop15=var15;
					s.prop16=var16;
				}
			}
			break;
		
		case "search":
			s.prop5='search';
			s.prop16 = var16;
			s.prop6=var6;
			s.eVar13=var6;
			s.prop7=var7;
			s.eVar14=var7;
			s.eVar4 = var11;
			s.eVar9='search';
			s.prop3='search';
			s.prop4='search';
			s.hier1='search';
			break;
			
		case "compare":
			s.prop5='compare';
			s.eVar7='compare';
			s.eVar8='compare';
			s.eVar9='compare';
			s.prop3='compare';
			s.prop4='compare';
			s.hier1='compare';
			break;
			
		default:
			s.events = path;
			
	}
}

/*
 * Method used for used product tracking. Add any extra param before that change the corresponding JSTL tag. 
 * Note: Some of the variables are not used for tracking even passed as parameter. The reason is that later you can modify
 * the javascript code below to use that variable. This will not require to change at JSP level.
 * to pass extra parameter.
 * @param pagename - page name of the product display page.
 * @param productname - product name.
 * 
 */

function omnitureProductTag(pagename,catid,category,productname,partnumber,event,var15,var19) {
	if(event != 'compare')
		sProducts = '';

	if(sProducts == '')
		sProducts = this.sProducts + ";"+partnumber;
	else 
		sProducts = this.sProducts + ",;"+partnumber;

	omnitureProductEvent(event,var15,partnumber,var19);
}

function omnitureProductEvent(event,var15,partnumber,var19){
	if(event == 'prodView') {
		s.events = "prodView";
		s.eVar15 = var15;
		s.products = sProducts;
		s.eVar19 = var19;
	} else if (event == 'compare') {
		s.events = "event6";
		s.products = sProducts;
		s.prop29 = var15;
		if(var15 == '1')
			s.prop25 = partnumber;
		if(var15 == '2')
			s.prop26 = partnumber;
		if(var15 == '3')
			s.prop27 = partnumber;
		if(var15 == '4')
			s.prop28 = partnumber;
	} else {
		s.events = event;
		s.products = sProducts;

	}
}

/*
 * Method used for concatenating the products to use it shop cart tag.This is called from cart add/display/remove/checkout pages.
 * Add any extra param before that change the corresponding JSTL tag to pass extra parameter.
 * Note: Some of the variables are not used for tracking even passed as parameter. The reason is that later you can modify
 * the javascript code below to use that variable.
 * @param categoryid - category Id of the product. its the number
 * @param prodname - product name.
 * @param partnumber - SKU part number
 * 
 */

function omnitureShopCartActionTag(catid,category,prodname,prodpartnumber,price,carrier,catentryId,storeNo,deliveryId) {
	if(deliveryId){
		;
	}
	else {
		if(sCartProducts == '')
			sCartProducts = this.sCartProducts + ";"+prodpartnumber;
		else 
			sCartProducts = this.sCartProducts + ",;"+prodpartnumber;
	}
		
}

/*
 * Method used for sending the omniture the events like Add/remove/checkout/view cart and corresponding information
 * This is called from cart add/display/remove/checkout process. Add any extra param before that change the corresponding 
 * JSTL tag to pass extra parameter.
 * @param event - event scView,scAdd/scOpen,scCheckout,scRemove.
 * 
 */

function omnitureShopCartTag(event,price,type) {
	s.events = event;
	s.products = sCartProducts;
	if(event == 'scAdd' || event == 'scOpen,scAdd'){
		s.eVar16 = price;
		s.eVar18 = "";
		s.eVar19 = 'Available Online';
	}
	this.sCartProducts = "";
}

/*
 * Method used for concatenating the products to use it shop cart tag.This is called from Order Confirmation page.
 * Add any extra param before that change the corresponding JSTL tag to pass extra parameter.
 * Note: Some of the variables are not used for tracking even passed as parameter. The reason is that later you can modify
 * the javascript code below to use that variable.
 * @param categoryid - category Id of the product. its the number
 * @param prodname - product name.
 * @param partnumber - SKU part number
 * @param quantity - Quantity purchased
 * @param total_price - price of the item
 * 
 */

function omnitureOrderActionTag(catid,category,prodname,partnumber,quantity,total_price,totalitemprice,carrier,catentryId,storeNo,deliveryId) {
	
	if(deliveryId)
		this.sCartProducts = this.sCartProducts + ";" + partnumber + ";" + quantity + ";" + totalitemprice + ";;eVar25=delivery,";
	else {
		if(carrier == 'StorePickup')
			this.sCartProducts = this.sCartProducts + ";" + partnumber + ";" + quantity + ";" + totalitemprice + ";event36="+ storeNo +";eVar25=" + carrier + "|eVar26=" + storeNo + ",";
		else
			this.sCartProducts = this.sCartProducts + ";" + partnumber + ";" + quantity + ";" + totalitemprice + ";;eVar25="+carrier+",";
	}
}

/*
 * Method used for sending order data to omniture.This is called from Order Confirmation page.
 * Add any extra param before that change the corresponding JSTL tag to pass extra parameter.
 * Note: Some of the variables are not used for tracking even passed as parameter. The reason is that later you can modify
 * the javascript code below to use that variable.
 * @param orderId - order id
 * @param state - state
 * @param zip - zip code
 * @param promoname - each promoname is concatenated with '|'
 * @param discounts - each discount is concatenated with '|'
 * @param shipping -  shipping charges
 * @param shippingmethod - shipping method
 * @param paymentmethod - payment method like visa/master card etc
 * @param totalsaving - total discounts
 */

function omnitureOrderTag(orderId,state,zip,promoname,promocode,shipping,shippingmethod,paymentmethod,totalsaving,tax,stores) {
	s.events="purchase,event30,event37,event38,event39";
	var foundEvent36 = shippingmethod.search(/StorePickup/i);
	if(foundEvent36 >= 0)
		s.events="purchase,event30,event37,event38,event39," + "event36";
	if(totalsaving == '0') {
		totalsaving = "0.00";
	} 
	if(shipping == '0') {
		shipping = "0.00"
	}
	if(tax == '0') {
		tax = "0.00"
	}
	s.products = this.sCartProducts + ";Discount;;;event37="+totalsaving+ ",;Shipping;;;event38=" + shipping 
								+ ",;Tax;;;event39=" + tax;
	s.purchaseID = orderId;
	s.state = state;
	s.zip = zip;
	s.eVar34 = zip;
	s.eVar33=promocode;
	s.eVar38=promoname;
	s.eVar31=shippingmethod;
	s.eVar32=paymentmethod.substring(1, paymentmethod.length);
	s.eVar35 = stores;
	this.sProducts = "";

}

/*
 * Method used for sending error related data to omniture.This is called from Error pages.
 * Add any extra param before that change the corresponding JSTL tag to pass extra parameter.
 * @param type - Type of error page like page name
 */

function omnitureErrorTag() {
	s.server = window.location.hostname;	
	s.channel = 'error';
	s.pageName = 'D=g';
	s.events = 'event50';

sendToOmniture();

}

function addupdateItem(){
	if(document.getElementById("omniturePartnumber").value != '' ){
		sendToOmniture();
	}
	
}

