/**
 * @author Sandeep Rudrawar  
 *Dt 4/10/2009
 * Copyright 2009 IBM Corp
 *
 */
 /*  Begin Helper Functions */
 //pageview connector 
 /* Start Comstants */
 var  CATEGORY_ITEM_PAGE_ACTIVITIES = 'Item Page Activities';
 var  ITEM_EMAIL_ACTIVITIES = 'Item Email Activities'; 
 var ITEM_EMAIL_ACTIVITIES ='Item Email Activities';
 var BROWSE_RELATED = 'Browse Related';
 var USER_ACTIVITIES = 'User Activities';

 /* End Constants */
 function SetCustomVariable(custVarSlot, custVarName, custVarValue, custVarScope) {

     //slots 1,2,3,4,5 total. Only 5 slots are available in total. Do not send any other value, it will be ignored. 
    //scope 1=>visitor level, 2=>Session Lvele 3=>Page Level. Do not send anything else apart from 1,2,3.

     pageTracker._setCustomVar(
      custVarSlot,                   // This custom var is set to this slot
      custVarName,           // Name of custom variable.
      custVarValue,      // Sets the value of "Section" to "Life & Style" for this particular aricle
      custVarScope                    // Sets the scope to page-level as default from GAHelper class.
                             );

 }
 
 function GetVirtualPageViewPath(level0,level1,level2,level3)
 {	var slash = '/';
	var retVal;
	if(IsDefined(level0) && IsDefined(level1) && IsDefined(level2) && IsDefined(level3))
	{
		retVal= slash + level0 + slash + level1 + slash +level2 + slash +level3;
	}
	else if (IsDefined(level0) && IsDefined(level1) && IsDefined(level2))
	{
		retVal= slash + level0 + slash + level1 + slash +level2 ;
	}
	else if(IsDefined(level0) && IsDefined(level1) )
	{
		retVal= slash + level0 + slash + level1;
	}
	else if(IsDefined(level0) )
	{
		retVal= slash + level0;
	}
	// All virtual pageviews should go under /Virtual Folder/ in GA reports.
	retVal = slash + 'Virtual' +retVal ; 
	return retVal;
	
 }
function IsDefined( variable)
{

	if(variable !=null && variable !='undefined'){ return true;}
	else{return false;}
	

}
function IsNullOrEmpty(variable)
{
	if(variable ==null || variable =='undefined' || variable =='')
	{ return true;}
	else {return false;}
}
 /*  End Helper Functions */
function TrackEvent(category, action, label,value)
{			
	TrackEventAction(category,action,label,value);			
}
function TrackEventAction(category, action, label,value)
{
		
	if(IsDefined(pageTracker ))
	{		
		if( IsNullOrEmpty( value)== true)
		{			
			pageTracker._trackEvent(category , action, label);
		}
		else
		{
			pageTracker._trackEvent(category , action, label,value);
		}
	}
}
function TrackPageView(drillDownPath)
{
	if(IsDefined(pageTracker) == true )
	{	
		if(IsNullOrEmpty(drillDownPath) == false)
		{			
			pageTracker._trackPageview(drillDownPath);
		}
		else{pageTracker._trackPageview();}
	}	
	
}
function SetVar(varName)
{
	if( IsDefined(pageTracker ) && IsDefined(varName)  )
	{		
		pageTracker._setVar(varName);
	}
}
function GATrackBannerActivity(linkname)
{
	/* following variables are declared on MasterCategoryPage only*/
	if(IsDefined (USER_ACTIVITIES)==true && IsDefined(BANNER_LABEL)==true )
	{	
		TrackEvent(USER_ACTIVITIES, linkname , BANNER_LABEL);
	}
}
function GATrackFileDownLoad(downloadName) 
{
	if(IsNullOrEmpty(downloadName)==false  )
	{
		var drilldownPath = GetVirtualPageViewPath('Downloads',downloadName);
		TrackPageView(drilldownPath);
	}
}
function GATrackExitLink(exitLinkName) {
    if (IsNullOrEmpty(exitLinkName) == false) {
        var drilldownPath = GetVirtualPageViewPath('Exit Links', exitLinkName);
        TrackPageView(drilldownPath);
    }

}
// Change the name as GATrackActivity(linkname)
function GATrackItemActivity(linkname)
{
	if(IsDefined(linkname) == false)
	{
		return ;
	}
	
	if(linkname == "View Holiday Banner Detail")
	{
		GATrackBannerActivity(linkname);
		return ;
	}
	if( IsDefined(CATEGORY_ITEM_PAGE_ACTIVITIES) && IsDefined(LABEL_PRODUCT_TITLE) && !IsNullOrEmpty(linkname)  )
	{
		TrackEvent(CATEGORY_ITEM_PAGE_ACTIVITIES, linkname, LABEL_PRODUCT_TITLE);
	}
}
function GATrackItemEmailActivity(activityName)
{
	
	if( IsDefined(CATEGORY_ITEM_PAGE_ACTIVITIES) && IsDefined(ITEM_EMAIL_ACTIVITIES) && !IsNullOrEmpty(activityName)  )
	{	
		TrackEvent(CATEGORY_ITEM_PAGE_ACTIVITIES, ITEM_EMAIL_ACTIVITIES ,activityName );
	}
}
function TrackEngagementBrowseWithRefinements()
{	
	TrackPageView(ENGAGEMENT_BROWSE_REFINEMENT);
	
}
// Shpng Bag Tracking
function GATrackActionAddToShoppingBag(sourceOfAdd )
{	
var cookieCnt = parseInt(readCookie("shoppingbagcnt"));
var drillDownLevel1 ;
var drillDownLevel0 = 'Shopping Bag';
var drillDownPath ;

		if (!isNaN(cookieCnt) && cookieCnt > 0)
		{	// nothing to do 
		}
		else 
		{
			drillDownLevel1 = 'sc Open';
			TrackEvent (drillDownLevel0, drillDownLevel1, sourceOfAdd); 
					
		}
		drillDownLevel1 = 'sc Add';
		//drillDownPath =  GetVirtualPageViewPath(drillDownLevel0,drillDownLevel1,sourceOfAdd);
		//TrackPageView (drillDownPath);		
		TrackEvent(drillDownLevel0,drillDownLevel1,sourceOfAdd);
}
//saved items 
function GATrackActionAddToSavedItems (sourceOfAdd)
{
	var drillDownLevel1 = 'Add' ;
	var drillDownLevel0 = 'Saved Items';
	var drillDownLevel2 = sourceOfAdd;
	//var drillDownPath = GetVirtualPageViewPath(drillDownLevel0,drillDownLevel1,drillDownLevel2);
	TrackEvent(drillDownLevel0,drillDownLevel1,drillDownLevel2);
	//TrackPageView(drillDownPath);
	
}
function GATrackActionEngagementAdd()
{
	var drillDownLevel0 = 'Saved Items';
	var drillDownLevel1 = 'Engagement add' ;
	var drillDownLevel2 = 'Engagement Item Page';
	//var drillDownPath = GetVirtualPageViewPath(drillDownLevel0, drillDownLevel1 ,drillDownLevel2);
	TrackEvent(drillDownLevel0, drillDownLevel1 ,drillDownLevel2);
	//TrackPageView(drillDownPath);		
	
}
function GATrackAction(argPageTitleSource , argAction)
{
	if(! IsNullOrEmpty(argAction ))
	{
		if( argAction == 'Add to SavedItems' )
		{
			GATrackActionAddToSavedItems (argPageTitleSource);
		}
		else if ( argAction == 'Add to ShoppingBag')
		{
			GATrackActionAddToShoppingBag(argPageTitleSource); 
		}
		else 
		{
			if(IsDefined(USER_ACTIVITIES)==true && IsDefined(argPageTitleSource)==true && IsDefined(argAction))
			{
				TrackEvent(USER_ACTIVITIES,argPageTitleSource,argAction);
			}
		}
	}
}
function GATrackCategoryBrowseEvent(viewAllAction)
{

	if( IsDefined(USER_ACTIVITIES) == true && IsDefined(BROWSE_RELATED) == true  && IsDefined(viewAllAction))
	{
		TrackEvent(USER_ACTIVITIES,BROWSE_RELATED,viewAllAction);
	}

}
function GATrackSearchTerm(searchTerm)
{
// Here /Search should be preceded otherwise it will flood in  Content Drilldown reports. 
  var newURL ;// = location.pathname;
  if(IsDefined(searchTerm) == true )
  {
	//newURL = '/Virtual/Search' +newURL +'?searchTerm='+searchTerm;
	newURL = '/Shopping/CategoryBrowse.aspx?searchTerm=' + searchTerm;	
	TrackPageView(newURL);
  }
}
function AddTransaction(OrderID,Affiliation,Total,Tax,Shipping ,City ,State,Country)
{	
	if(IsDefined(OrderID) && IsDefined(Affiliation)&& IsDefined(Total)&& IsDefined(Tax)&& 
		IsDefined(Shipping)&& IsDefined(City)&& IsDefined(State) && IsDefined(Country))
	{
	
		pageTracker._addTrans(OrderID,Affiliation,Total,Tax,Shipping ,City ,State,Country);
	}
	else if( IsDefined(OrderID) && IsDefined(Affiliation)&& IsDefined(Total) )
	{
		pageTracker._addTrans(OrderID , Affiliation , Total);
	}
}
function AddItem(OrderID, SKU ,ProductName , Category, UnitPrice, Quantity)
{
	if( IsDefined(OrderID) && IsDefined(SKU) && IsDefined(ProductName) 	   &&
		IsDefined(Category) && IsDefined(UnitPrice) &&  IsDefined(Quantity)	)
	{
	
		pageTracker._addItem(OrderID ,SKU ,ProductName, Category , UnitPrice , Quantity);
	}
}
function TrackTransaction()
{

	if(IsDefined(pageTracker))
	{			
		pageTracker._trackTrans();
	}
}

function GATrackModuleEvent(moduleName, flashTitle)
{
	
	if(IsNullOrEmpty(flashTitle)==true)
	{
		flashTitle = moduleName;
	}
	
	if((IsNullOrEmpty(flashTitle)== false))
	{	var modulePathName ='Modules';
     	var drilldownPath=	GetVirtualPageViewPath(modulePathName,flashTitle );			
		TrackPageView(drilldownPath);
	}	
}


