///////////////////////////////////////////////////////////////////////////////////
//
// Topica Conversion Tracking Library
// (c) 2004 Topica Inc, All rights reserved
// Written by Konstantin Gredeskoul
//
///////////////////////////////////////////////////////////////////////////////////

function appendValue(params, value) {
	var SEP = '\1';
	if (params == '' || params == null)
		return value;
	else
		return params + SEP + value;
}

///////////////////////////////////////////////////////////////////////////////////
function Item(value, price, quantity) {
	this.value 			= value;
	this.price			= (price == null) 		? '' : price;
	this.quantity		= (quantity == null) 	? '' : quantity;
}

///////////////////////////////////////////////////////////////////////////////////
function TopicaDataObject(token) {
	this.token 			= token;
	this.data			= new Object(); // associative array, keyed by field, eg 'f:212'
}

TopicaDataObject.prototype.addData  = function(field, value, price, quantity) {
	// field and value are required parameters
	if (field == null || value == null || value == '') return;

	var fieldArray;
	if (this.data[field] != null)
		fieldArray = this.data[field];
	else  {
		fieldArray = new Array();
		this.data[field] = fieldArray;
	}
	
	fieldArray[fieldArray.length] = new Item(value, price, quantity);
}

TopicaDataObject.prototype.registerHost = "c.topica.com";

TopicaDataObject.prototype.register = function (isSecure) {
	var out = 'uid=' + escape(this.token);
	var params = null;
	for (var field in this.data) {
		var fieldArray = this.data[field];
		for (i = 0; i < fieldArray.length; i++) {
			var item = fieldArray[i];
			if (typeof item == "object" && item.constructor == Item) {
				params = appendValue(params, field);
				params = appendValue(params, item.value);
				params = appendValue(params, item.price);
				params = appendValue(params, item.quantity);
			}
		}
	}
	if (params != null) 
		out += "&data=" + escape(params);

	var protocol = (isSecure == 1) ? 'https' : 'http';

	var url = protocol + '://' + this.registerHost + "/tp?" + out;
	var frame = 
		"<iframe src=\"" + url + 
		"\" scrolling='no' frameborder=0 hspace=0 vspace=0 width=0 height=0></iframe>";
	document.write(frame);
}

///////////////////////////////////////////////////////////////////////////////////

