// Server Settings
var cmJSLocation = "/common/coremetrics/v.40/";
var cmUtilsLocation = cmJSLocation;
var cmwJSLocation = "/common/jscripts/";

//changing isProduction to true for all environments, CM blocks internal environments by IP
var cmwIsProduction = true;

var myHost = location.hostname.toLowerCase();

// add logic for sites...must be ordered from specific to less specific
// also must indicate if this site should log statistics to production
if (myHost.indexOf('macys.weddingchannel.com') > -1)
{
    cmUtilsLocation += "macys.weddingchannel.com";
}
else if (myHost.indexOf('bloomingdales.weddingchannel.com') > -1)
{
    cmUtilsLocation += "bloomingdales.weddingchannel.com";
}    
else if (myHost.indexOf('weddingchannelstore.com') > -1)
{
    cmUtilsLocation += "weddingchannelstore.com";
}    
else if (myHost.indexOf('chineseweddingsbytheknot.com') > -1)
{
    cmUtilsLocation += "chineseweddingsbytheknot.com";
}    
else if (myHost.indexOf('beachweddingsbytheknot.com') > -1)
{
    cmUtilsLocation += "beachweddingsbytheknot.com";
}    
else if (myHost.indexOf('destinationweddingsbytheknot.com') > -1)
{
    cmUtilsLocation += "destinationweddingsbytheknot.com";
}    
else if (myHost.indexOf('weddingshop.theknot.com') > -1)
{
    cmUtilsLocation += "weddingshop.theknot.com";
}    
else if (myHost.indexOf('weddingpages.com') > -1)
{
    cmUtilsLocation += "weddingpages.com";
}    
else if (myHost.indexOf('weddingtracker.com') > -1)
{
    cmUtilsLocation += "weddingtracker.com";
}    
else if (myHost.indexOf('.weddingchannel.com') > -1)
{
    cmUtilsLocation += "weddingchannel.com";
}    
else if (myHost.indexOf('.theknot.com') > -1)
{
    cmUtilsLocation += "theknot.com";
}    
else if (myHost.indexOf('.thenest.com') > -1)
{
    cmUtilsLocation += "thenest.com";
}    
else if (myHost.indexOf('.thenestbaby.com') > -1)
{
    cmUtilsLocation += "thenestbaby.com";
}    
else if (myHost.indexOf('.thenestbaby.com') > -1)
{
    cmUtilsLocation += "thenestbaby.com";
}    
else if (myHost.indexOf('.lilaguide.com') > -1)
{
    cmUtilsLocation += "lilaguide.com";
}    
else if (myHost.indexOf('.giftregistrylocator.com') > -1)
{
    cmUtilsLocation += "giftregistrylocator.com";
}    
else if (myHost.indexOf('.shopforweddings.com') > -1)
{
    cmUtilsLocation += "shopforweddings.com";
}    
else if (myHost.indexOf('.partyspot.com') > -1)
{
    cmUtilsLocation += "partyspot.com";
}    
else if (myHost.indexOf('.promspot.com') > -1)
{
    cmUtilsLocation += "promspot.com";
}    

//add trailing slash
cmUtilsLocation += "/";    

// Global Variables
var cmwPageID = "";
var cmwCatID = "";
var cmwProductID = "";
var cmwProductName = "";
var cmwError = false;
var cmwProductView = false;
var cmwSearchTerm = null;
var cmwSearchResults = null;
var cmwCustID = "";
var cmwCustEmail = null;
var cmwCustCity = null;
var cmwCustState = null;
var cmwCustZip = null;
var cmwNewsletter = null;
var cmwSubFlag = null;
var cmwRegistration = false;

// Global Processes
if (typeof cmwProcessed == "undefined") 
{
	cmwProcessed = false;
	includejsfile(cmJSLocation + "eluminate.js");
	includejsfile(cmUtilsLocation + "cmdatatagutils.js");
}

// Functions
function TrackPage(myPageID,myCatID) 
{
	if(cmwProcessed) 
	{
		return false;
	}

	cmwPageID = myPageID;
	cmwCatID = myCatID;
	includejsfile(cmwJSLocation + "cmwCall.js");
}

function TrackSearch(mySearchTerm,mySearchResults)
{
	cmwSearchTerm = mySearchTerm;
	cmwSearchResults = mySearchResults;
	return true;
}

function TrackError(myPageID,myCatID) 
{
	if(cmwProcessed) 
	{
		return false;
	}

	cmwPageID = myPageID;
	cmwCatID = myCatID;
	cmwError = true;
	includejsfile(cmwJSLocation + "cmwCall.js");
}

function TrackRegistration(myCustID,myEmail,myCity,myState,myZip,myNewsletter,mySubFlag)
{
    cmwCustID = myCustID;
    cmwCustEmail = myEmail;
    cmwCustCity = myCity;
    cmwCustState = myState;
    cmwCustZip = myZip;
    cmwNewsletter = myNewsletter;
    cmwSubFlag = mySubFlag;
    cmwRegistration = true;
}

function TrackProductPage(myProductID,myProductName,myCatID) 
{
	if(cmwProcessed) 
	{
		return false;
	}

	cmwProductID = myProductID;
	cmwProductName = myProductName;
	cmwCatID = myCatID;
	cmwProductView = true;
	includejsfile(cmwJSLocation + "cmwCall.js");
}


function cmwCreateTags()
{
	if(cmwProcessed) 
	{
		return false;
	}

	if(cmwIsProduction) 
	{
		cmSetProduction();
	}
	
	if(cmwPageID == null) 
	{
		cmwPageID = document.URL;
		var qryidx = cmwPageID.indexOf("?");
		if(qryidx > -1) 
		{
			cmwPageID = cmwPageID.substr(0,qryidx);
		}
	}
	
	if(cmwCatID == null)
	{
		cmwCatID = "TK-General";
	}
	
	if(cmwError)
	{
	    cmCreateErrorTag(cmwPageID,cmwCatID);
	}
	else if (cmwProductView)
	{
	    cmCreateProductviewTag(cmwProductID,cmwProductName,cmwCatID);
	}
	else
	{
	    cmCreatePageviewTag(cmwPageID,cmwCatID,cmwSearchTerm,cmwSearchResults);
	}
	
	if(cmwRegistration)
	{
	    cmCreateRegistrationTag(cmwCustID,cmwCustEmail,cmwCustCity,cmwCustState,cmwCustZip,cmwNewsletter,cmwSubFlag);
	}
	
	
	cmwProcessed = true;
	
	return true;
}

function loadjsfile(filename)
{
	var fileref=document.createElement('script');
	fileref.setAttribute("language","javascript1.2");
	fileref.setAttribute("type","text/javascript");
	fileref.setAttribute("src", filename);
	document.getElementsByTagName("head")[0].appendChild(fileref);
}

function includejsfile(url) {
    document.write(unescape('%3Cscr' + 'ipt src="' + url + '" type="text/javascript"%3E%3C/scr' + 'ipt%3E'));
}
