function Bomgar() {
	var _host = "";
	var _protoRe = /^(http|https):\/\//;
	
	/* private */
	function _createURL(params, forPopup) {
		var qStr = "";
		for (var k in params) { qStr += "&"+encodeURIComponent(k)+"="+encodeURIComponent(params[k]); }
		qStr = "popup="+(forPopup ? "1" : "0") + "&c2cjs=1" + qStr;

		return _host+"api/start_session.ns?"+qStr;
	};

	function _openWindow(params) {
		return window.open(_createURL(params, true), 'clickToChat', 'toolbar=no,directories=no,status=no,menubar=no,resizable=yes,location=no,scrollbars=no');
	};

	function _redirectWindow(params) {
		window.location.href = _createURL(params, false);
	};

	function _startChat(params, doFull) {
		var w = _openWindow(params);
		if (w && !w.closed) { return; }
		else if (doFull) { _redirectWindow(params); return; }
	};
	
	function _startChatWithSurveyValues(surveyValues, fallbackToFullWindow) {
		surveyValues.issue_menu = '1';
		_startChat(surveyValues, fallbackToFullWindow);
	};

	/* public */

	// Set the public site hostname that click to chat should be started on.
	this.setSite = function(siteHostname) {
		if (!_protoRe.test(siteHostname)) { siteHostname = "http://"+siteHostname; }
		if (siteHostname[siteHostname.length-1] != '/') { siteHostname += '/'; }
		_host = siteHostname;
	};

	// Start a click to chat session using a session key, optionally falling back to a full browser window redirect if the popup window fails to open due to popup blockers.
	this.startChatWithSessionKey = function(sessionKey, fallbackToFullWindow) {
		var p = {short_key: sessionKey};
		_startChat(p, fallbackToFullWindow);
	};
	
	// Start a click to chat session using a session key and external key, optionally falling back to a full browser window redirect if the popup window fails to open due to popup blockers.
	this.startChatWithSessionKeyAndExternalKey = function(sessionKey, externalKey, fallbackToFullWindow) {
		var p = {short_key: sessionKey, external_key: externalKey};
		_startChat(p, fallbackToFullWindow);
	};
	
	// Start a click to chat session using just an issue id and no other front end survey fields.
	this.startChatWithIssueId = function(issueId, fallbackToFullWindow) {
		_startChatWithSurveyValues({id: issueId}, fallbackToFullWindow);
	};

	// Start a click to chat session by passing the entire front end survey form element. 
	// This will submit all non-button input element values on the form.
	// Any unexpected survey field names will be ignored.
	this.startChatWithIssueForm = function(formElement, fallbackToFullWindow) {
		var params = {};
		for (var i = 0; i < formElement.elements.length; i++) {
			var e = formElement.elements[i];
			if (e.name && e.value && e.type && e.type != 'button' && e.type != 'submit') {
				params[e.name] = e.value;
			}
		}
		formElement = undefined;
		params.issue_menu = '1';
		_startChat(params, fallbackToFullWindow);
		return false;
	};

	// Start a session with a representative id and name.
	this.startChatWithRepIdName = function(repId, repName, fallbackToFullWindow) {
		var p = {id: repId, name: repName};
		_startChat(p, fallbackToFullWindow);
	};

	return this;
}
var BG = Bomgar();



