// Begin: common functions
// Copyright (c) 2005, iBasis, Inc.

/** 
 * Removes the jsessionid from urls
 */
function trimSessionId(string) {
	return string.replace(/;jsessionid=.+$/, '');
}

/**
 * Sets a session cookie
 */
function setSessionCookie(name, value, domain) {
	document.cookie = name + "=" + escape(value) + ((domain) ? "; domain=" + domain : "");
}


/**
 * Sets a session cookie on this domain
 */
function setDomainSessionCookie(name, value) {
	setSessionCookie(name, value, getDomain());
}


/**
 * Reads a cookie
 */
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));
}

/**
 * Finds the domain name of the site
 * i.e. produces "pingo.com" from "www.pingo.com"
 */
function getDomain() {
	var d = document.domain;
	if (typeof d == 'undefined' || d == null) {
		return;
	}
	return d.replace( /.+?(\.[^\.]+?\.[^\.]+?)$/, "$1" );
}

/**
 * Preloads an image
 */
function preloadImage(img) {
	if (typeof img == 'undefined' || img == null || ! document.images) {
		return;
	}
	try {
		var _img = new Image();
		_img.src = img;
	} catch (e) {
	}
}

/**
 * Trims the jsessionid from the image source and preloads image a the same time
 */
function preloadTrimSessionId(img) {
	var trimImg = trimSessionId(img);
	preloadImage(trimImg);
	return trimImg;
}

// End
