// 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) {
	name = '&' + name + '=';
	var 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) {

	username = getCookie('username');

 	if (!window.images_server){//if (window.images_server == null || window.images_server == undefined) {
 		images_server = server;
 	}

	if (username) {
		html =  '<tr>' + '\n';
		html += '  <td valign="middle"><a href="/logout" class="searchTextSmaller">LOGOUT</a></td>' + '\n';
		html += '  <td valign="middle">&nbsp;<a href="/logout" class="searchTextSmaller"><img src="'+images_server+'/images/header/Webdings1.gif" width="12" height="12" align="absmiddle" border="0"></a></td>' + '\n';
		html += '</tr>' + '\n';
		html += '<tr>' + '\n';
		html += '  <td colspan="2" class="itemTextLink1Smaller">(&nbsp;' + username + '&nbsp;)</td>' + '\n';
		html += '</tr>' + '\n';
	} else {
		html =  '<tr>' + '\n';
		html += '  <td valign="middle"><a href="/login" class="searchTextSmaller">LOGIN</a></td>' + '\n';
		html += '  <td>&nbsp;<a href="/login" class="searchTextSmaller"><img src="'+images_server+'/images/header/Webdings1.gif" width="12" height="12" align="absmiddle" border="0"></a></td>' + '\n';
		html += '</tr>' + '\n';
		html += '<tr>' + '\n';
		html += '  <td valign="middle"><a href="/register" class="itemTextLink1Smaller2">REGISTER</a></td>' + '\n';
		html += '  <td valign="middle">&nbsp;<a href="/register" class="itemTextLink1Smaller2"><img src="'+images_server+'/images/header/Webdings1.gif" width="12" height="12" align="absmiddle" border="0"></a></td>' + '\n';
		html += '</tr>' + '\n';
	}

	document.write(html);

	var tid = getParam('tid');
	var sourceid = getParam('sourceid');
	var cj_aid = getParam('AID');
	var cj_pid = getParam('PID');
	var cj_sid = getParam('SID');
	var cookie_value = '';

	if (sourceid) {
		setCookie('GOVLIQ_ORIGINAL_SOURCE', sourceid, 30, '/', '.govliquidation.com');
	}

	if (tid) {
		setCookie('GOVLIQ_TID', tid, 1000, '/', '.govliquidation.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(
			'GOVLIQ_CJ',
			cookie_value,
			30,
			'/',
			'.govliquidation.com'
		);
	}
}
