// BEGIN:  FOOTSMART CODE

var dotomiXMLhttp = getXmlHttpRequestObject();
var showPixel = true;
var dotomiPageTypeId = '';
var catName = '';
var catId = '';
var subCatName = '';
var subCatId = '';
var imageUrl = '';
var sectCode = '';
var searchTerm = '';
var brandName = '';
var ecometryId = '';
var orderSubTotal = '';
var orderItems = '';
var currentGuid = '';
var customerType = '';
var refCode = '';

function displayDotomi(guidCookieName, sourceCookieName)
{
	currentGuid  = GetGuidForDotomi(guidCookieName);
	refCode		 = GetRefCode(sourceCookieName);
	customerType = GetCustomerType();

	switch(dotomiPageTypeId)
	{
		case 100:
			// Order Confirmation Page Gets Special Parameters
			SetDotomiPixelImageOnConfirmation(ecometryId, orderSubTotal, customerType, refCode, currentGuid, orderItems);
			break;

		default:
			// All Other Pages
			SetDotomiPixelImage(dotomiPageTypeId, catName, catId, subCatName, subCatId, imageUrl, sectCode, customerType, refCode, currentGuid, searchTerm, brandName);
			break;
	}
}

function GetCustomerType()
{
	return getData('XmlHttp.aspx?getCustomerType=1&guid=' + currentGuid, 'New Visitor');
}

function GetRefCode(cookieName)
{
	return getData('XmlHttp.aspx?GetRefCode=1&originalSource=' + GetCookie(cookieName), 'Undefined');
}

function GetGuidForDotomi(cookieName)
{
	var guidValue = GetCookie(cookieName);

	if(guidValue == null || guidValue == '')
	{
		var d = new Date();
		var formattedDate = d.getDate() + '|' + d.getMonth() + '|' + d.getFullYear() + '|' + d.getHours() + '|' + d.getMinutes() + '|' + d.getSeconds() + '|' + d.getMilliseconds();
		guidValue = getData('XmlHttp.aspx?customerGuid=1&timeFactor=' + formattedDate, '');

		if(guidValue == '')
		{
			guidValue = GenerateGuid();
		}
	}

	return guidValue;
}

function GenerateGuid()
{
	// This function is ONLY called when the AJAX code designed to get a 
	// new GUID cookie errors out
	//
	var result, i, j;
	result = '';

	for(j = 0; j < 32; j++)
	{
		if( j == 8 || j == 12 || j == 16 || j == 20)
		{
			result = result + '-';
			i = Math.floor(Math.random()*16).toString(16).toUpperCase();
			result = result + i;
		}
	}

	return result;
}

function getData(urlText, defaultValue)
{
	var returnValue = defaultValue;

	try
	{
		//dotomiXMLhttp = getXmlHttpRequestObject();

		if (dotomiXMLhttp)
		{
			//dotomiXMLhttp.onreadystatechange = GetAJAXOutput;
			dotomiXMLhttp.open("GET", urlText, false);
			dotomiXMLhttp.send(null);
			returnValue = dotomiXMLhttp.responseText;
		}
		else{
			returnValue = defaultValue + '_1_NOAJAX';
		}
	}
	catch(err)
	{
		returnValue = defaultValue + '_2_AJAXERROR';
	}

	return returnValue;
}

function displayDotomiResults()
{
	var s = "" + 
		"<div id='test' style='background-color:#eeeeee;position:absolute;top:25px;left:5px;height:290px;width:940px;border:#000000 1px dashed;padding:5px;'>" + 
		"showPixel: "		 + showPixel + "<br>" +
		"dotomiPageTypeId: " + dotomiPageTypeId + "<br>" +
		"ecometryId: "		 + ecometryId + "<br>" +
		"orderSubTotal: "	 + orderSubTotal + "<br>" +
		"customerType: "	 + customerType + "<br>" +
		"refCode: "			 + refCode + "<br>" +
		"orderItems: "		 + orderItems + "<br>" +
		"catName: "			 + catName + "<br>" +
		"catId: "			 + catId + "<br>" +
		"subCatName: "		 + subCatName + "<br>" +
		"subCatId: "		 + subCatId + "<br>" +
		"imageUrl: "		 + imageUrl + "<br>" +
		"sectCode: "		 + sectCode + "<br>" +
		"currentGuid: "		 + currentGuid + "<br>" +
		"searchTerm: "		 + searchTerm + "<br>" +
		"brandName: "		 + brandName + "<br>" +
		"";

	if(showPixel)
	{
		if(dotomiPageTypeId == 100)	
		{
			// Order Confirmation Page Gets Special Parameters
			s += "SetDotomiPixelImageOnConfirmation(" + ecometryId + "," + orderSubTotal + "," + customerType + "," + refCode + "," + currentGuid + "," + orderItems + ");";
		}
		else
		{
			// All Other Pages Do This
			s += "SetDotomiPixelImage(" + dotomiPageTypeId + "," + catName + "," + catId + "," + subCatName + "," + subCatId + "," + imageUrl + "," + sectCode + "," + customerType + "," + refCode + "," + currentGuid + "," + searchTerm + "," + brandName + ");";
		}
	}

	s += "</div>";

	document.write(s);
}

// END:    FOOTSMART CODE



// BEGIN: Dotomi tagging
// ver 2.0  2/12/08
//
// Ver 2.0 updates:
// 	* Added "userId" field to SetDotomiPixelImage and SetDotomiPixelImageOnConfirmation - field contains unique user Id
//	* Added "items" to SetDotomiPixelImageOnConfirmation - field contains order skus and values in the format: sku;price,sku;price,... 
//
//     Additional notes: 
//		* the field "refCode" should be used to pass the traffic source (e.g. email, affiliate, paid search, direct type, etc.)
//		* the filed "newCust" should be used to pass the customer status (new, repeat, 1x buyer shoes, 1x buyer non-show, 2x buyer show, 2x buyer non-shoe, .. etc.)
//
function SetDotomiPixelImage(id, category, category_id, sub_category, sub_category_id, image_url, section_code, newCust, refCode, userId, searchTerms, brand)
{
	var baseFields = "dtm_com=28&dtm_fid=101";
	var customFields = [];

	// custom fields start
	customFields['cli_promo_id'] = id;
	customFields['dtmc_category'] = category;
	customFields['dtmc_category_code'] = category_id;
	customFields['dtmc_sub_category'] = sub_category;
	customFields['dtmc_sub_category_code'] = sub_category_id;
	customFields['dtmc_prod_img_url'] = image_url;
	customFields['dtmc_section'] = section_code; // 1=shoes, 2=health, 3=clearance
	customFields['dtm_user_id'] = userId;
	customFields['dtmc_search_terms'] = searchTerms;
	customFields['dtmc_brand'] = brand;
	// custom fields end

	DotomiWrite( baseFields, customFields, newCust, refCode );
}

function SetDotomiPixelImageOnConfirmation(ecometry_order_id, subtotal, newCust, refCode, userId, items)
{
	var baseFields = "dtm_com=29&dtm_fid=102";
	var customFields = [];

	// custom fields start
	customFields['cli_promo_id'] = "100";
	customFields['dtmc_transaction_id'] = ecometry_order_id;
	customFields['dtm_conv_val'] = subtotal;
	customFields['dtm_user_id'] = userId;
	customFields['dtm_items'] = items;
	// custom fields end

	DotomiWrite( baseFields, customFields, newCust, refCode );
}

function DotomiWrite( baseFields, flds, newCust, refCode ) {
	var iframeSrc = window.location.protocol + "//login.dotomi.com/ucm/UCMController?dtm_cid=2083&dtm_format=5&dtm_cmagic=3e9e39&" + baseFields;

    if( newCust != null && newCust != "") flds['dtmc_newcust'] = newCust;      // Y || N
    if( refCode != null && refCode != "") flds['dtmc_refcode'] = refCode;      

    var ref = document.referrer;
    if( ref != null && ref != "" && ref.indexOf("www.footsmart.com")==-1)
        flds['dtmc_referrer']=ref; 

	for(var item in flds)
	{
		if (typeof flds[item] != "function" && typeof flds[item] != "object")
		{
			iframeSrc += "&" + item + "=" + escape(flds[item]);
		}
	}

	setTimeout('timeOutDotomi()', 4000);

	$('response_frame').src = iframeSrc;
	//document.write('<div id="dtmdiv" style="display:none;"><iframe name="response_frame" src="' + iframeSrc + '"></iframe></div>');
}

function timeOutDotomi()
{
	var dtmdiv = document.getElementById("dtmdiv");
	if (dtmdiv != null)
	{
		dtmdiv.innerHTML = "";
	}
}
// END: Dotomi Tagging
