// cose tracker

/* DEBUG MESSAGES FOR LATER DISPLAY */
var debugMessages = new Array();
/* SET DEBUG to TRUE or FALSE here for testing */
var debugEnabled = false;
/* Tell the debug whether to print messages only if printDebug is called or everytime someone calls a debug function */
var debugVerbose = true;

var trackViewsEnabled = true;

/**
 * Record a visitor Hit on the partner site
 * Required: partnerID
 * Required: widgetBrandID
 * Return: unique search session ID, for recording the search term related to this Hit
 */
function recordView(partnerID, widgetBrandID) {
		/* TRACKEER OPTIONS */
		var connectionURL = "http://cose.macraesbluebook.com/dominus/tracker/tracker.cfm";
		var partnerIDParam = "partnerid";
		var widgetBrandIDParam = "widgetbrandid";
		var widgetLocationURIParam = "widgetlocationuri";
		var connectionMethod = "GET";

			
			
		// check for partnerID
		if (partnerID == null || typeof partnerID != 'number') {
			debug("PartnerID was null or was not a number", true);
			// if wrong argument, return from it, can't record it
			return "";
		}
		
		// check for widgetBrandID, here we can be a little bit lenient and if it is null or not a number, we just ignore it
		if (widgetBrandID == null || typeof widgetBrandID != 'number') {
			debug("(warning) WidgetBrandID was null or not a number, but we're still good", false);
			// set widgetBrandID to 0 meaning, whatever the default is on the receiving end
			widgetBrandID = 0;
		}
		
		// if ajax is not enabled, return nothing, stop all processing
		if (!trackViewsEnabled) return "";
			
		// now, here is where the real work happens
			
		// build url
		var url = connectionURL + "?" + partnerIDParam + "=" + partnerID + "&" + widgetBrandIDParam + "=" + widgetBrandID;
		url = url + "&" + widgetLocationURIParam + "=" + escape(window.location.href);
		
		// use a pseudo send to tracker in the form of a javascript include file
		sendData(url);			
		
		// if server is down, call default css styles
		// defaultCssSetup();
		
		
}

/**
 * Send the required data over to the tracker
 */
function sendData(url) {
	var sc=document.createElement('script');
  sc.type='text/javascript';
  sc.id="cose_tracker";
  sc.src=url;
  document.getElementsByTagName('head')[0].appendChild(sc);  	
}

/**
 * Import the desired CSS, with this function if cose server is down, there will be no styling done. 
 * 
 */
function importCss(){
	var css = document.createElement('link');
	css.type= "text/css";
	css.rel="stylesheet";
	css.href="htpp://cose.macraesbluebook.com/dominus/css/widgets/source/style.css";
	document.getElementsByTagName('body')[0].appendChild(css);
}

// FUNCTIONS BELOW ARE FOR TESTING ONLY, DEBUGING IS DISABLED BY DEFAULT, DO NOT ENABLE IT IN PRODUCTION

/* record a debug message */
function debug(msg, error) {
	// if debug is not enabled, just return from function
	if (!debugEnabled) return;
	
	var dmlength = debugMessages.length;
	debugMessages[dmlength] = msg;
	if (debugVerbose)
		if (error)
			document.write("ERROR: ");
		else
			document.write("DEBUG: ");
		
		document.writeln(msg + "<br>");
}

/* PRINTS OUT AN INFO MESSAGE */
function info(msg) {
		document.writeln("INFO: " + msg + "<br>");
}

/* print out debug messages */
function printDebug() {
	for (var i=0; i< debugMessages.length; i++) {
			document.writeln("DEBUG: " + debugMessages[i] + "<br>");
	}
}

