// TRACKING.JS
// Javascript functions for tracking visitors

//-----------------------------------------------------------------
// CHARTBEAT variables
//-----------------------------------------------------------------
var _sf_startpt=(new Date()).getTime();

//-----------------------------------------------------------------
// CONSTANTS
//-----------------------------------------------------------------
var gk_ROOTURL = "www.indianbento.com";
var gk_DOMAIN = "indianbento.com";
var gk_VISITOR= "Visitor";
var gk_PANELIST = "Panelist";
var gk_GUEST = "Guest";
var gk_SHORTTITLE = "Untitled";
var gk_HOMEURL = "/";
var gk_COMPANYURL = "/company/press.php";
var gk_CUSTOMERSURL = "/company/press.php";
var gk_MENUURL = "/content/menu/";
var gk_BENTOBOXESURL = "/content/bentoboxes/";
var gk_RESTAURANTSURL = "/content/restaurants/wallstreetcafe.php";
var gk_MYURL = "./";
var gk_GOOGLE_ACCOUNT = "UA-5676632-3";
var gk_KEYNOTE_MONITOR = "KTXN";
var gk_YOTTAA_MONITOR = "YottaaMonitor";

//-----------------------------------------------------------------
// OBJECTS
//-----------------------------------------------------------------
//
// PageObject() is an object created on each page. The properties
// of this object are passed as arguments to the TrackVisitor() function.
// Each page assigns new values to the properties.
function PageObject( hPageParent, sVisitorName, sShortTitle, bTrackingOn, sURL  ) {
	// hPageParent - handle to another PageObject that is this page's parent
	//               Can be null, e.g., the home page has no parent
	// VisitorName - string containing name of visitor, either "guest" or "panelist"
	//               Must NOT be null
	// sShortTitle - string containing short title, e.g., "Sign Up", not "Bizmetric > Shopper > Sign Up"
	//               Must NOT be null	
	// TrackingOn  - boolean that tells us whether or not to track a visit to this page
	//               Default is true
        // sURL         - string containing the URL for this page
 	//               Can be null - in fact, is null for all pages except top-level pages, 
	//               such as the home page, or the Shopper index page

	var newObj = new Object();
	
  // Set Property PageParent 
  newObj.PageParent = (arguments.length >= 1) ? hPageParent : null;
	// Set Property VisitorName 
  newObj.VisitorName = (arguments.length >= 2) ? sVisitorName : null;
  // Set Property ShortTitle
  newObj.ShortTitle = (arguments.length >= 3) ? sShortTitle : null;
  // Set Property TrackingOn 	
  newObj.TrackingOn = (arguments.length >= 4) ? bTrackingOn : true;
  // Set Property URL
  newObj.URL = (arguments.length >= 5) ? sURL : null;
	
	// Properties that are not passed as arguments - they are same for all pages
  // Set Property CompanyName
  newObj.CompanyName = null;
  // Property PageTitle is the title of the page
  newObj.PageTitle = document.title;

  // Now return the newly created object;
  return newObj;
}

function ValidPageObject( hPage ) {
	// hPage -- Handle to a PageObject
	
	// A valid PageObject must have these properties be NOT null
	// VisitorName - string containing name of visitor
	// sShortTitle - string containing short title
	return ( hPage && hPage.VisitorName && hPage.ShortTitle );
}

//-----------------------------------------------------------------
// SHOWPAGEHISTORY()
//
function ShowPageHistory ( hPage ) {
	// hPage -- Handle to a PageObject
	
	// If the PageObject is not defined or does not have it's ShortTitle, exit
	if ( !ValidPageObject(hPage) )
		return;
	
	var pageTitle = new Array();
	var pageURL = new Array();
	var hasParent = (hPage.PageParent != null);
	var i = 0;
	var curPage = hPage;
	
	if ( hasParent ) {
		// Walk through the page hierarchy
		while ( hasParent ) {
			pageTitle[i] = curPage.ShortTitle;
			pageURL[i] = (curPage.URL != null) ? curPage.URL : gk_MYURL;
	  	hasParent = (curPage.PageParent != null);
			curPage = curPage.PageParent;
			i++;}}
	else {
		// It's the top-level home page with no parent
		// We just need to set the pageTitle array, since it's URL
		// will not be printed
		pageTitle[0] = curPage.ShortTitle;}
	
	// Now write out the array of page titles
	document.write('<font class="History">You are here: </font>');
	for ( i=pageTitle.length-1; i>=0; i-- ) {
		// If it's not the last item, then write a link, otherwise, just the page title
		if (i!=0){
	  	document.write('<A class="History" HREF="' + pageURL[i] + '">');
	  	document.write(pageTitle[i] + '</A>');
			document.write(' <font class="History">&gt;</font> '); }
		else
	  	document.write(' <font class="History">' + pageTitle[i] + '</font>');
	}
}

//-----------------------------------------------------------------
// COOKIES
//-----------------------------------------------------------------
// Create a cookie with the specified name and value.
// The cookie expires at year 2002
function SetCookie(sName, sValue)
{
  date = new Date("Jan 1, 2021");
  document.cookie = sName + "=" + escape(sValue) + "; expires=" + date.toGMTString() + "; path=/; domain=www.indianbento.com";
}

// Retrieve the value of the cookie with the specified name.
function GetCookie(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }

  // a cookie with the requested name does not exist
  return null;
}

function DelCookie(sName)
{
  document.cookie = sName + "=" + "dummy" + "; expires=Fri, 31 Dec 1999 23:59:59 GMT; path=/; domain=www.indianbento.com";
}

// see if we need to ignore this page for hitbox tracking

if (GetCookie("isEmployee") != null && GetCookie("isEmployee") == "true")
{
  // cookie found
  var gb_is_employee = true;
}
else
{
  // cookie not found
  
  // if gb_is_employee is not previously defined
  if (gb_is_employee == null)
  {
    var gb_is_employee = false;
  }
}
	
//-----------------------------------------------------------------
// TRACKING FUNCTIONS
//-----------------------------------------------------------------	
function Track( hPage ) {
    // hPage -- is a handle to a PageObject object
	
    // Do not track a visit to a page if any of these is TRUE:
    // - the page indicates that it should not be tracked (hPage.TrackingOn is off)
    // - the visitor's name is blank, which should never be the case
    // - the visitor is an employee (see Cookie functions above)
    // - the domain is something other than www.indianbento.com 
    if ( !hPage.TrackingOn || (hPage.VisitorName == null) || gb_is_employee
	 || document.URL.indexOf(gk_ROOTURL,0)==-1 )
	return;
			
    var sVisitor;
    // If the Company is blank, or if the Company and Visitor Name are 
    // the same, then display just the visitor's name (i.e. don't show
    // a blank company or company whose name is same as the visitor's name
    if ( (hPage.CompanyName == null) || (hPage.CompanyName == hPage.VisitorName))
	sVisitor = hPage.VisitorName;
    else
	sVisitor = hPage.VisitorName + ", " + hPage.CompanyName;
    
    // Track the visitor and the page
    TrackPage(hPage.PageTitle);	

    // If you want to track visits by people, e.g. joey@yahoo.com, then uncomment the following line:
    // TrackVisitor(sVisitor);
}

//-----------------------------------------------------------------
function TrackPage(sPage) {
    // The goal of this function is to track visits to pages 
    // We use the web analytics tracking code to track this visit to a page
    // <sPage> usually looks like "Indian Bento > Panel > Deals" or "Bombay Bento > Home"
    // the first part of the page name is a convention used by the site
    
    // Strip off the string "Indian Bento > " from the title
    // For now, uncommenting this code because this might strip "Indian Bento" in the rest of the page name as well
    // sPage = strReplace(sPage,'xxx > ','');

    // Google Analytics async code
    document.write("<script type='text/javascript'>");
    document.write("var _gaq = _gaq || [];");
    document.write("_gaq.push(['_setAccount', 'UA-5676632-3']);");
    document.write("_gaq.push(['_trackPageview']);");
    document.write("_gaq.push(['_trackPageLoadTime']);");
    document.write("(function() {");
    document.write("var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;");
    document.write("ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';");
    document.write("var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);");
    document.write("})();");
    document.write("</script>");

    // Chartbeat async code
    document.write("<script type='text/javascript'>");
    document.write('var _sf_async_config={uid:22218,domain:"indianbento.com"};');
    document.write("(function(){");
    document.write("  function loadChartbeat() {");
    document.write("    window._sf_endpt=(new Date()).getTime();");
    document.write("    var e = document.createElement('script');");
    document.write("    e.setAttribute('language', 'javascript');");
    document.write("   e.setAttribute('type', 'text/javascript');");
    document.write("    e.setAttribute('src',");
    document.write('       (("https:" == document.location.protocol) ? "https://a248.e.akamai.net/chartbeat.download.akamai.com/102508/" : "http://static.chartbeat.com/") +');
    document.write('     "js/chartbeat.js");');
    document.write(" document.body.appendChild(e);");
    document.write("  }");
    document.write("  var oldonload = window.onload;");
    document.write("  window.onload = (typeof window.onload != 'function') ?");
    document.write("     loadChartbeat : function() { oldonload(); loadChartbeat(); };");
    document.write("})();");
    document.write("</script>");

    // GetClicky code
    if (navigator.userAgent.indexOf(gk_KEYNOTE_MONITOR, 0) == -1 &&
	navigator.userAgent.indexOf(gk_YOTTAA_MONITOR, 0) == -1) {
	// The userAgent string did not identify it as a  monitoring agent, so it's a real person, track it with GetClicky
	document.write("<a title='Clicky Web Analytics' href='http://getclicky.com/91601'>");
	document.write("<img src='http://static.getclicky.com/media/links/badge.gif' border='0'  width='1' height='1'>");
	document.write("</a>");
	document.write("<script src='http://static.getclicky.com/91601.js' type='text/javascript'></script>");
    }

} // end TrackPage()

//-----------------------------------------------------------------
function TrackVisitor(sVisitor) {
  // The goal of this function is to track visits by people
  // Visitor is a string "joe@mybizmeter.com"
  //
  // We use the HitBox tracking code to track this visit to a page
  // which we name "Visitor > sVisitor", e.g, "Visitor > joe@mybizmeter.com"
  
	var sPageTitle;
	
  // First, track the visit to the page
	// Check if the visitor name is generic, do not track it
	if ( sVisitor == gk_GUEST || sVisitor == gk_PANELIST )
		return;
		
	// We used strip off the visitor's domain from email address
	// Keep this code around if needed again
	//		sVisitor = sVisitor.substring(0, sVisitor.indexOf("@",0));
	sPageTitle = gk_VISITOR + " >" + sVisitor;
		
	// HITBOX doesn't show page names longer than 65 characters
	sPageTitle = sPageTitle.substring(0,65);

  //-----------------------------------------------------------------  
  // HitBOX requires that all spaces in the Page Name are replaced
  // by a plus sign (+). 
  sPageTitle = strReplace(sPageTitle,' ','+');

  //-----------------------------------------------------------------
  // NOTE: We canceled Hitbox on 7/31/02, so commenting out code
  // Now write the HitBOX tracking code with the Page Name
  // Note: the account number is before the string "62EN3"
  //document.write('<IMG SRC="https://hg1.hitbox.com/HG?hc=w124&cd=1&hb=WE500504P5SE62EN3&n=');
  // Now write the PageTitle and finish the HitBOX code
  //document.write(sPageTitle + '" height=1 width=1 border="0">');
} // end TrackVisitor()



//-------------------------------------------------
// PRIVATE
function strReplace(sIn, sExpFrom, sExpTo) {
  // sIn		: source string
  // sExpFrom	: string containing the expression to be replaced
  // sExpTo		: string containing the expression to replace with
  // Returns 	: new string that has sExpFrom replaced with sExpTo

	var re=new RegExp(sExpFrom,'gi');
	var strNew=sIn.replace(re,sExpTo);
	return strNew;
}

//-------------------------------------------------
// GLOBAL PAGEOBJECTS 
// These variables use the notation "gp_xxx"
// args to PageObject are ( hPageParent, sVisitorName, sShortTitle, bTrackingOn, sURL )

// HOME page object
var gp_HOME = PageObject(null, gk_GUEST, "Home", true, gk_HOMEURL);
// MENU page object
var gp_MENU = PageObject(gp_HOME, gk_PANELIST, "Menu", false, gk_MENUURL);
// BENTO BOXES page object
var gp_BENTOBOXES = PageObject(gp_HOME, gk_PANELIST, "Bento Boxes", false, gk_BENTOBOXESURL);
// COMPANY page object
var gp_COMPANY = PageObject(gp_HOME, gk_GUEST, "Company", false, gk_COMPANYURL);
// CUSTOMERS page object
var gp_CUSTOMERS = PageObject(gp_COMPANY, gk_GUEST, "Customers", false, gk_CUSTOMERSURL);
// RESTAURANTS page object
var gp_RESTAURANTS = PageObject(gp_HOME, gk_PANELIST, "Restaurants", false, gk_RESTAURANTSURL);
														 														 


