// name - name of the cookie
// value - value of the cookie
// [days] - number of days before cookie expires
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function setCookie(name, value, days, path, domain, secure) {
	if (days) {
		expires = new Date();
		expires.setTime(expires.getTime() + days * 86400000);
	}
	var curCookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
	document.cookie = curCookie;
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function getParam(name, s) {
	var params = '';
	name = '&' + name + '=';
	if(s)
		params = '&' + s;
	else
		params = '&' + window.location.search.substr(1);
	var start = params.indexOf(name);
	if (start != -1) {
		start += name.length;
		var end = params.indexOf('&',start);
		if (end == -1) end = params.length;
		return params.substring(start,end);
	} else {
		return null;
	}
}

function dynamic_code(server) {

	var tid = getParam('tid');
	var sourceid = getParam('sourceid');
	var cj_aid = getParam('AID');
	var cj_pid = getParam('PID');
	var cj_sid = getParam('SID');
	var cj_url = getParam('URL');
	var cookie_value = '';

	if (sourceid) {
		setCookie(
			'WWW_ORIGINAL_SOURCE',
			sourceid,
			30,
			'/',
			'.liquidation.com'
		);
	}

	if (tid) {

		setCookie(
			'WWW_TID',
			tid,
			1000,
			'/',
			'.liquidation.com'
		);

		document.write('<IMG SRC=/tid?tid=' + tid + '&url='
			+ document.location.hostname
			+ document.location.pathname
			+ document.location.search + '>');
	}

	if (cj_aid) {
		cookie_value = 'AID=' + cj_aid + '&PID=' + ((cj_pid) ? cj_pid : '') + '&SID=' + ((cj_sid) ? cj_sid : '');
		
		setCookie(
			'WWW_CJ',
			cookie_value,
			30,
			'/',
			'.liquidation.com'
		);

		if (cj_url) {
			document.location = unescape(cj_url);
		}
	}

}

function write_banner(type) {

	var html    = banner_html_data[type];
	var weights = banner_weight_data[type];

	// make an array with the indexes of banner_weight_data where each value is there weight times
	// thus, if we select a random index from this matrix it has weight chance of being picked
	// even if we have 100 banner adds all with weights of 100 (the max) thats still only 10,000 at constant time O(10,000)
	// likely this # will be closer to 8 banner ads at weights < 10 each so thats constant time O(80) 
	var matrix = new Array();
	for (var i = 0; i < weights.length; i++) {
		var weight = weights[i];
		for (var j = 0; j <= weight; j++) {
			matrix[i+j] = i;
		}
	}

	var index = 0;
	var randomNumber = 0;

	randomNumber = randomvalue(0, matrix.length-1);
	if (randomNumber < 0) { randomNumber *= -1 }

	index = matrix[randomNumber];

	document.write(html[index]);

	return;
}

function randomvalue(low, high) { return Math.floor(Math.random() * (1 + high - low) + low); }

