var bDebug=false;
var coremetricsIsOn=true;

// check for undefined vars
if ( typeof ( ouPageID ) == "undefined" ) var ouPageID = "";
if ( typeof ( ouCategoryID ) == "undefined" ) var ouCategoryID = "";
if ( typeof ( ouPageType ) == "undefined" ) var ouPageType = "basic";
if ( typeof ( ouProductID ) == "undefined" ) var ouProductID = "";
if ( typeof ( ouProductName ) == "undefined" ) var ouProductName = "";
if ( typeof ( ouQuantity ) == "undefined" ) var ouQuantity = "";
if ( typeof ( ouUnitPrice ) == "undefined" ) var ouUnitPrice = "";
if ( typeof ( ouCustomerID ) == "undefined" ) var ouCustomerID = "";
if ( typeof ( ouEmail ) == "undefined" ) var ouEmail = "";
if ( typeof ( ouCity ) == "undefined" ) var ouCity = "";
if ( typeof ( ouState ) == "undefined" ) var ouState = "";
if ( typeof ( ouZipCode ) == "undefined" ) var ouZipCode = "";

//	set pageID to page name and path, if not defined
var sPath = String(window.location);	
// strip http://
var sPage = sPath.substring( sPath.lastIndexOf('//') + 2 );
// get domain
var ouDomain = sPage.substring( 0,sPage.indexOf('/') );
if ( ouPageID == "" ) {
	// get pageID (folder/filename)
	ouPageID = sPage.substring( sPage.indexOf('/')+1 );
	// strip .*
	if ( ouPageID.indexOf('.') > 0 ) {
		ouPageID = ouPageID.substring( 0, ouPageID.indexOf('.') );
	} else {
		ouPageID = "index";
	}
}	

// check for category ID
if ( ouCategoryID == "" ) {
	// get catID (folder)
	tmpPageID = sPage.substring( sPage.indexOf('/')+1 );
	//tmpPageID = tmpPageID.substring( 0, tmpPageID.indexOf('.') );
	if ( tmpPageID.lastIndexOf('/') > 0 ) {
		// get folder
		ouCategoryID = tmpPageID.substring( 0, tmpPageID.lastIndexOf('/') );
	} else {
		// use pageID
		ouCategoryID = ouPageID;
	}
	
	// handle exceptions
	if ( ouCategoryID.substring(0,9) == 'LiveClass' ) {
		ouCategoryID = "LiveClass";
	} else if ( ouCategoryID.substring(0,17) == 'StrategySpotlight' ) {
		ouCategoryID = "StrategySpotlight";
	}
}

// strip trailing "/"
//if ( ouCategoryID.substring( ouCategoryID.length ) == '/' ) ouCategoryID = ouCategoryID.substring( 0, ouCategoryID.length );

// get category ID prefix/suffix to indicate source URL
var ouCatIDPrefix = gup("CatIDPrefix");
var ouCatIDSuffix = gup("CatIDSuffix");
if ( ouCatIDPrefix != "" ) {
	ouCategoryID = ouCatIDPrefix + "." + ouCategoryID;
}
if ( ouCatIDSuffix != "" ) {
	ouCategoryID = ouCategoryID+ "." + ouCatIDSuffix;
}

// show category ID
if ( bDebug == true ) alert("ouPageID: " + ouPageID + "\nouCategoryID: " + ouCategoryID);

if ( coremetricsIsOn == true ) {
	cmSetProduction();
	switch ( ouPageType ) {		//{basic, productView, viewCart, orderComplete, registration, error}
		case "basic":
			cmCreatePageviewTag(ouPageID, ouCategoryID, null);
			if ( bDebug == true ) alert("cm - page view");
			break;
		case "productView":
		if ( bDebug == true ) alert("cm - product view");
			cmCreateProductviewTag(ouProductID, ouProductName, ouCategoryID);
			break;
		case "viewCart":
			if ( bDebug == true ) alert("cm - product added to cart");
			cmCreatePageviewTag(ouPageID, ouCategoryID, null);
			cmCreateShopAction5Tag(ouProductID, ouProductName, ouQuantity, ouUnitPrice, ouCategoryID);
			cmDisplayShop5s();
			break;
		case "order":
			if ( bDebug == true ) alert("cm - order received");
			cmCreatePageviewTag(ouPageID, ouCategoryID, null);
			cmCreateShopAction9Tag(ouProductID, ouProductName, ouQuantity, ouUnitPrice, ouCustomerID, ouOrderID, ouOrderSubtotal, ouCategoryID);
			cmDisplayShop9s();
			cmCreateOrderTag(ouOrderID, ouOrderSubtotal, "0", ouCustomerID, ouCity, ouState, ouZipCode);
			cmCreateRegistrationTag(ouCustomerID, ouEmail, ouCity, ouState, ouZipCode);
			break;
		case "registration":
			if ( bDebug == true ) alert("cm - order received");
			cmCreatePageviewTag(ouPageID, ouCategoryID, null);
			cmCreateRegistrationTag(ouCustomerID, ouEmail, ouCity, ouState, ouZipCode);
			break;
		default:
			// throw page view tag by default
			cmCreatePageviewTag(ouPageID, "noPageType", null);
			break;
	}
} else {
	// do nothing
}

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}