// omniture.js
var debugMode = getURLParameter('OmnitureDebug');

// To store the page name to be passed in feedback url
var pageName = "";
var hierarchy = "";

// To store the eSpotPageImpressionsList
var eSpotPageImpressionsList = "";

// To store the cmsSpotPageImpressionsList
var cmsSpotPageImpressionsList = "";


function OmnitureDebug()
{
    if (debugMode) {
        window.open("","stats_debugger","width=600,height=600,location=0,menubar=0,status=1,toolbar=0,resizable=1,scrollbars=1").document.write("<script language=\"JavaScript\" src=\"https://sitecatalyst.omniture.com/sc_tools/stats_debugger.html\"></"+"script>" + "<script language=\"JavaScript\">window.focus();</script>")
    }
}

if(document.getElementById("globalNavigation") ) {

$$('li#shopMainNavSectionItems .subNavListingLink').each(function (e) {
					Event.observe(e, 'click', function(e) {
						var element = Event.element(e)			
						trackMainNavForOmniture('SHOP',element.id,element.name,'subNavListingLink',element.name,element.href);
						//Event.stop(e);
	});
});

$$('li#supportMainNavSectionItems .mainNavListingLink').each(function (e) {
					Event.observe(e, 'click', function(e) {
						var element = Event.element(e)			
						 trackMainNavForOmniture('SUPPORT',element.id,element.name,'mainNavListingLink',element.innerHTML,element.href);
						// Event.stop(e);
	});
});

$$('li#servicesMainNavSectionItems .subNavListingLink').each(function (e) {
					Event.observe(e, 'click', function(e) {
						var element = Event.element(e)			
						 trackMainNavForOmniture('SERVICES:Affiliate Programs',element.id,element.name,'subNavListingLink',element.innerHTML,element.href);
						// Event.stop(e);
	});
});
$$('li#servicesMainNavSectionItems .mainNavListingLink').each(function (e) {
					Event.observe(e, 'click', function(e) {
						var element = Event.element(e)			
						 trackMainNavForOmniture('SERVICES',element.id,element.name,'subNavListingLink',element.innerHTML,element.href);
						// Event.stop(e);
	});
});
$$('li#discoverMainNavSectionItems .mainNavListingLink').each(function (e) {
					Event.observe(e, 'click', function(e) {
						var element = Event.element(e)			
						 trackMainNavForOmniture('DISCOVER',element.id,element.name,'subNavListingLink',element.innerHTML,element.href);
						/// Event.stop(e);
	});
});

}	

/*	trackEduEppFCUser()
 *	------------------------------------------------------------------------
 *	PRE-CONDITIONS:			- 	Prototype.js must be included prior to this 
 *								function being called.
 *
 *	POST-CONDITIONS:		-	Will check for a certain <span> tag that is
 *								only writteon on the page when the user is 
 *								a valid Family Center, EPP or EDU user, or
 *								has come to the site through a proper 
 *								referring link that will enable to user to 
 *								browser an one of those types of users.
*/
function trackEduEppFCUser()
{
	/*	first, let's check to see if there is a bar in the header.  This
	 *	will allow us to be able to grab the inner HTML of stuff.
	 *
	 *	NOTE:  	The first if() conditional is to ensure we don't get a 
	 *			runtime error on MSIE browsers:
	*/
	if($('programStorefrontName')){
		if(($('programStorefrontName').innerHTML != null) || ($('programStorefrontName').innerHTML != ''))
		{
			/*	Since this SPAN tag only appears if the user has a valid 
			 *	EDU/EPP/FC cookie, we should now be clear to fire off 
			 *	this function.
			*/
			clearOmniture(s);
			s=s_gi(s_account);	
			s.linkTrackVars = "eVar4";	// eVar Specified for use by MS.
			s.linkTrackEvents="None";	// Currently no events to track
			
			
			/*	the innerHTML should say something like:
			 *	"Family Center Storefront"
			 *	"IBM Purchase Program Storefront"
			 *	"San Diego State University Storefront"
			 *
			 *	So let's grab this value and send it to Omniture.
			*/	
			s.eVar4 = $('programStorefrontName').innerHTML;
			s.tl(this, 'o', 'Store Context');
			//OmnitureDebug();
		}
	}
}
function trackMainNavForOmniture(dept,id,name,cssClass,text,href)	 {
	if(href != "" && href.startsWith('http') && text != "") {
	   text = text.unescapeHTML();
	    text = dept +':'+text;
	    text = text.replace(",",":");
	    //clearOmniture(s);
	    setpageName(pageName);
	    s = s_gi(s_account);
	    s.linkTrackVars = "eVar25";
	    s.linkTrackEvents = "None";
	    s.eVar25 = text;
	    s.tl(this,'o',pageName);
		OmnitureDebug();
	 }
	 
}

// to set the hierarchy of the page. 
function setHierarchy(hier)
{
hierarchy=hier;
}

// sets the pagename from omniture
function setpageName(pagename){
	pageName=pagename;
	
	}
// sets the espots 
function setEspotPageImpressionsList(espotName){
	eSpotPageImpressionsList=eSpotPageImpressionsList+espotName;

	}	

// gets the espots
function getEspotPageImpressionsList(){

	return eSpotPageImpressionsList;
	
	}
	
// sets the cmsspots 
function setCmsSpotPageImpressionsList(cmsspotname){
	cmsSpotPageImpressionsList=cmsSpotPageImpressionsList+cmsspotname;

	}	

// gets the cmsspots
function getCmsSpotPageImpressionsList(){

	return cmsSpotPageImpressionsList;
	
	}	

// opens the feedback URL
function  callFeedBackURL()
  {
    var url="http://sonystyle.custhelp.com/cgi-bin/sonystyle.cfg/php/xml_api/cci/feedback.php?source=";
    url=url+pageName;
   	window.open(url);
	
  } 


//
// Get all URL parameters
//
function getURLParameters()
{
	// get the current URL
	var url = window.location.toString();
	// get the parameters
	url.match(/\?(.+)$/);
	var params = RegExp.$1;
	// split up the query string and store in an associative array		
	var params = params.split("&");
	var url_params = {};			 			
		 
	for(var i=0; i<params.length; i++)
	{
		var tmp = params[i].split("=");
		url_params[tmp[0]] = unescape(tmp[1]);
	}
	return url_params; 
}

//	
// Get a specific URL parameter if present	
//
function getURLParameter(name) 
{
	url_params = getURLParameters();
	return (url_params[name] == undefined) ? '' : url_params[name];		
}

//
// Clear Omniture variables
//
function clearOmniture(s)
{
	s.pageName='';
	s.server='';
	s.channel='';
	s.pageType='';
	s.prop1=''; s.prop2='';	s.prop3='';	s.prop4='';	s.prop5='';	s.prop6=''; s.prop7=''; s.prop8='';	s.prop9='';	s.prop10=''; s.prop11=''; s.prop12='';
	s.hier1='';
	s.campaign='';
	s.state='';
	s.zip='';
	s.events='';
	s.products='';
	s.purchaseID='';
	s.eVar1='';	s.eVar2='';	s.eVar3='';	s.eVar4='';	s.eVar5='';	s.eVar6='';	s.eVar7='';	s.eVar8='';	s.eVar11=''; s.eVar12=''; 
	s.eVar13=''; s.eVar14=''; s.eVar15=''; s.eVar16=''; s.eVar17=''; s.eVar18='';	s.eVar19=''; s.eVar20=''; s.eVar21=''; s.eVar22='';
	s.eVar26='';s.eVar27='';s.eVar28='';s.evar36='';
}
// 
// Track promo bundle 
//
function trackPromoBundle()
{

	clearOmniture(s);
	s=s_gi(s_account);
	s.linkTrackVars="eVar28";
	s.linkTrackEvents="None";
	s.eVar28='vaio:summerbundle';
	
	s.tl(this,'o','PromoBundle');
		OmnitureDebug();
}

function trackCMSpot(cmSpotName)
{
	clearOmniture(s);
	s=s_gi(s_account);
	s.linkTrackVars="eVar10";
	s.linkTrackEvents="None";
	s.eVar10=pageName+' > '+cmSpotName;
	//s.prop23=pageName+' > '+cmSpotName;
	s.tl(this,'o','CM Spots');
		OmnitureDebug();
}


function trackEndecaCMSpot(ruleName)
{
	clearOmniture(s);
	s=s_gi(s_account);
	s.linkTrackVars="eVar35";
	s.linkTrackEvents="None";
	s.eVar35=pageName+' > '+ruleName;
	s.tl(this,'o','Endeca Rules');
		OmnitureDebug();
}

function trackESpot(eSpotName)
{

	clearOmniture(s);
	s=s_gi(s_account);
	s.linkTrackVars="eVar5,prop6";
	s.linkTrackEvents="None";
	s.eVar5=pageName+' > '+eSpotName;
	s.prop6=pageName+' > '+eSpotName;


	s.tl(this,'o','E Spots');
		OmnitureDebug();
}


function trackDealerLocator()
{
	clearOmniture(s);
	s=s_gi(s_account);
	s.linkTrackVars="events";
	s.linkTrackEvents="event4";
	s.events="event4";
	s.tl(this,'o','DealerLocator');
		OmnitureDebug();
}

function trackAdvizia()
{
	clearOmniture(s);
	s=s_gi(s_account);
	s.linkTrackVars="events";
	s.linkTrackEvents="event13";
	s.events="event13";
	s.tl(this,'o','Advizia');
		OmnitureDebug();
}

function trackLoginAttempt()
{
	if ((typeof s != 'undefined') && (s != undefined)) clearOmniture(s);
	s=s_gi(s_account);
	s.linkTrackVars="events";
	s.linkTrackEvents="event6";
	s.events="event6";
	s.tl(this,'o','LoginAttempt');
		OmnitureDebug();
}


function trackRegistrationAttempt()
{
	if ((typeof s != 'undefined') && (s != undefined)) clearOmniture(s);
	s=s_gi(s_account);
	s.linkTrackVars="events";
	s.linkTrackEvents="event7";
	s.events="event7";
	s.tl(this,'o','RegistrationAttempt');
		OmnitureDebug();
}

function trackAddToWishList(partNumber)
{
	clearOmniture(s);
	s=s_gi(s_account);
	s.linkTrackVars="events,products";
	s.linkTrackEvents="event8";
	s.events="event8";
	s.products=';'+partNumber;
	s.tl(this,'o','Add to WishList');
		OmnitureDebug();
}

function trackSearchResults()
{
	clearOmniture(s);
	s=s_gi(s_account);
	s.linkTrackVars="events";
	s.linkTrackEvents="event5";
	s.events="event5";
	s.tl(this,'o','SearchResults');
		OmnitureDebug();
}

function trackSonyCardReferrals()
{
	clearOmniture(s);
	s=s_gi(s_account);
	s.linkTrackVars="events";
	s.linkTrackEvents="event9";
	s.events="event9";
	s.tl(this,'o','SonyCardReferrals');
		OmnitureDebug();
}

function trackCheckoutStart()
{
	var products = s.products;
	clearOmniture(s);
	s=s_gi(s_account);
	s.linkTrackVars="events,products";
	s.linkTrackEvents="scCheckout";
	s.events="scCheckout";
	s.products=products;
	s.tl(this,'o','CheckOutStart');
		OmnitureDebug();
}

/*
function trackAddToShoppingCart(sku)
{
	var cartEvent = "scOpen";
	var openFlag = getCookie("SY_OMOC","");
	if (openFlag) {
	    cartEvent = "scAdd";
	    //alert("scAdd triggered");
	}
	else {
	    setCookie("SY_OMOC","true");
	    //alert("scOpen triggered");
	}	
	clearOmniture(s);
	s=s_gi(s_account);
	s.linkTrackVars="events,products";
	s.linkTrackEvents=cartEvent;
	s.events=cartEvent;
	s.products=';'+sku;
	s.tl(this,'o','AddToShoppingCart');
	if (debugMode) window.open("","stats_debugger","width=600,height=600,location=0,menubar=0,status=1,toolbar=0,resizable=1,scrollbars=1").document.write("<script language=\"JavaScript\" src=\"http://192.168.112.2O7.net/stats_debugger.php\"></"+"script>" + "<script language=\"JavaScript\">window.focus();</script>");	
}
*/

function trackAddToShoppingCart(sku)
{	
	clearOmniture(s);
	s=s_gi(s_account);
	s.linkTrackVars="events,products";
	s.linkTrackEvents="scAdd";
	s.events="scAdd";
	s.products=';'+sku;  	
	s.tl(this,'o','AddToShoppingCart');
		OmnitureDebug();
}

function trackRemoveFromCart(sku)
{
	clearOmniture(s);
	s=s_gi(s_account);
	s.linkTrackVars="events,products";
	s.linkTrackEvents="scRemove";
	s.events="scRemove";
	s.products=';'+sku;	
	s.tl(this,'o','RemoveFromCart');
		OmnitureDebug();
}

function trackProductTab(sku,tabName)
{
	var productTabName = sku + ":" + tabName;
// commented for defect 7764 	
//	clearOmniture(s);  
//	s=s_gi(s_account);
	s.linkTrackVars="prop7";
	s.linkTrackEvents="prodView,event11";
	s.prop7=productTabName;
	s.tl(this,'o','Product Tabs');
		OmnitureDebug();
}

function trackCTOCustomize()
{
	clearOmniture(s);
	s=s_gi(s_account);
	s.linkTrackVars="events";
	s.linkTrackEvents="event14";
	s.events="event14";
	s.tl(this,'o','CTO Customize');
		OmnitureDebug();
}

//
// Function to determine the store context (eVar20)
//
function getStoreContext(fcStoreId,storeId,logonId)
{
	var storeContext = "";
	//var storeId = getURLParameter("storeId");
	
	// Are we in Family Center?
	if (storeId == fcStoreId) storeContext="FCP";
	
	// Are we in Telesales?
	if (window.parent.name == "telesales" && (
		window.name=="CSR_ACTIONS" ||
		window.name=="CSR_STATUS" ||		
		window.name=="SONY_STYLE"))
	{
		var prefix = logonId.substr(0,2);
		// Is this a Retail Terminal?
		if (prefix=="ec" || prefix=="eb" || prefix=="rc")
			storeContext = "Retail";
		else
			storeContext = "Telesales";
	}	
	return storeContext;
}

function trackCartAccessory()
{
    clearOmniture(s);
	s=s_gi(s_account);
	s.linkTrackVars="events";
	s.linkTrackEvents="event15";
	s.events="event15";
	s.tl(this,'o','Cart Accessory');
		OmnitureDebug();
}
function trackCartEngraving()
{
    clearOmniture(s);
	s=s_gi(s_account);
	s.linkTrackVars="events";
	s.linkTrackEvents="event18";
	s.events="event18";
	s.tl(this,'o','Cart Engraving');
		OmnitureDebug();
}
function trackCartESP()
{
    clearOmniture(s);
	s=s_gi(s_account);
	s.linkTrackVars="events";
	s.linkTrackEvents="event16";
	s.events="event16";
	s.tl(this,'o','CartESP');
		OmnitureDebug();
}
function trackCartIS()
{
    clearOmniture(s);
	s=s_gi(s_account);
	s.linkTrackVars="events";
	s.linkTrackEvents="event19";
	s.events="event19";
	s.tl(this,'o','Cart IS');
		OmnitureDebug();
}

var _cmspotObject = new Object();
var modulesDiv;
var divCollection;
_cmspotObject.SetupCMSpotTracking = function()
{
	modulesDiv = document.getElementById('modules');
	if ( modulesDiv != null)
	{
	divCollection = modulesDiv.getElementsByTagName("a");
	
        for (var i=0; i<divCollection.length; i++) {
		if (!divCollection[i].onclick)	{
		//divCollection[i].href = "#";
			divCollection[i].onclick = function(){
				var tag1 = this.getElementsByTagName("img");
				if (tag1[0] != undefined && tag1[0] != null && tag1[0] != "")
				{
					setCmsSpotPageImpressionsList(_cmspotObject.getIMGName(tag1[0]).toString());
					_cmspotObject.trackCMSOmniture(this.href, _cmspotObject.getIMGName(tag1[0]).toString());
					//trackCMSpot(_cmspotObject.getIMGName(tag1[0]).toString());
					//location.href=this.href;
					return false;
				}
			}
		}else {
			divCollection[i].functiondummy = divCollection[i].onclick;
			divCollection[i].onclick = function(){
				var tag1 = this.getElementsByTagName("img");
				if (tag1[0] != undefined && tag1[0] != null && tag1[0] != "")
				{
			 		setCmsSpotPageImpressionsList(_cmspotObject.getIMGName(tag1[0]).toString());
					_cmspotObject.trackCMSOmniture(this.href, _cmspotObject.getIMGName(tag1[0]).toString());
					//trackCMSpot(_cmspotObject.getIMGName(tag1[0]).toString());
					this.functiondummy();
					//location.href=this.href;
					return false;
				}else{
			 	this.functiondummy();
			 }
			}
        }
        }
	
	}
}

_cmspotObject.getIMGName = function (img_element){
	var string1 = img_element.src
	var len = string1.length
	var rs = 0
	for (i = len; i > 0; i--) {
	vb = string1.substring(i,i+1)
		if (vb == "/" && rs == 0) {
		return string1.substring(i+1,len)
		rs = 1
		}
	} 

}

_cmspotObject.trackCMSOmniture = function (var_link, var_code)
{
	trackCMSpot(var_code);
	var timer = window.setTimeout('location.href="'+var_link+'";', 500);
}

//<!-- Start of methods for crmPortal-->
function trackCRMLoginAttempt() {
    if (typeof s != "undefined" && s != undefined) {
        clearOmniture(s);
    }
    s = s_gi(s_account);
    s.linkTrackVars = "events";
    s.linkTrackEvents = "event21";
    s.events = "event21";
    s.tl(this, "o", "CRM_Login_Attempt");
		OmnitureDebug();
}

function trackCRMPortalModule(obj, module, linkname) {
    clearOmniture(s);
    s = s_gi(s_account);
    s.linkTrackVars = "eVar26";
    s.linkTrackEvents = "None";
    s.eVar26 = module;
    s.tl(obj, "o", 'CRM' + '_'+ module +'_' + linkname);
		OmnitureDebug();
}

function trackCRMPortalExternalLinks(obj, module, linkname) {
    //clearOmniture(s);
    s = s_gi(s_account);
    s.tl(obj, "e", 'CRM' + '_'+ module +'_' + linkname);
		OmnitureDebug();
}

// Function for tracking Ratings and Reviews
// Function for tracking Ratings and Reviews
function pageChanged(pageName, pageStatus){
    if(typeof rateReviewSKU !='undefined'){
        trackProductTab(rateReviewSKU, pageName + ":" + pageStatus);
    }
    else{
       trackProductTab('Error_with_SKU', pageName + ":" + pageStatus);
    }
}
//<!-- End of methods for crmPortal -->


function trackSearchESupport(keyword,pageName)
{
	clearOmniture(s);
	setpageName(pageName);
	s=s_gi(s_account);
    s.linkTrackVars = "eVar34";
    s.linkTrackEvents = "None";	
	s.eVar34 = keyword;
	s.tl(this,'o',pageName);
		OmnitureDebug();
}

function trackSearchAlsoTry(pageName)
{
	//alert('trackSearchAlsoTry s_account'+s_account+'\npageName:'+pageName);		
	clearOmniture(s);
	setpageName(pageName);
	s=s_gi(s_account);
    s.linkTrackVars = "events";
    s.linkTrackEvents = "event17";		
	s.events="event17";
	s.tl(this,'o',pageName);
		OmnitureDebug();
}	

function trackSearchAddToCart(pageName)
{
	clearOmniture(s);
	setpageName(pageName);
	s=s_gi(s_account);
    s.linkTrackVars = "events";
    s.linkTrackEvents = "event26";	
	s.events="event26";
	s.tl(this,'o',pageName);
		OmnitureDebug();
}	

function trackSearchSortRefinement(sortRefinement,pageName)
{
	//alert('trackSearchSortRefinement s_account'+s_account+'\sortRefinement:'+sortRefinement+'\npageName:'+pageName);	
	clearOmniture(s);		
	setpageName(pageName);
	s=s_gi(s_account);
	s.linkTrackVars="eVar21,prop8";
	s.linkTrackEvents="None";
	s.prop8=sortRefinement;
	s.eVar21=s.prop8;
	s.tl(this,'o',pageName);
		OmnitureDebug();
}

// Function for tracking the decision of the user during
// the proactive chat implementation.  Accept,Ignore, Decline
function userChatDecision(chatDecision){
	clearOmniture(s);
	s.linkTrackVars="prop24";
	s.linkTrackEvents="None";
	s.prop24=chatDecision;
	s.tl(this, 'o', 'Chat Decision');
		OmnitureDebug();
}

// Function for tracking the clicks to the dynamic
// proactive chat button displayed once a business
// rule has been triggered and the user ignores or 
// declines the request
function userChatDynamicButton(buttonClicked){
	clearOmniture(s);
	s=s_gi(s_account);
	s.linkTrackVars="events";
	s.linkTrackEvents="event22";
	s.events="event22";
	s.tl(this,'o', 'Dynamic Chat Button Click');
		OmnitureDebug();
}
