if (typeof CWS == "undefined" || !CWS) {
	var CWS = {};
	
};

//get element/elemens by id
CWS.$ = function(element)
{
	if (arguments.length > 1)
	{
		for (var i = 0, elements = [], length = arguments.length; i < length; i++)
		{
			elements.push($(arguments[i]));
		}
		return elements;
	}
	if (typeof element == 'string')
	{
		element = document.getElementById(element);
	}
	return element;
};

//event handlers
CWS.$Event = function()
{
	this._handlers = [];
};

CWS.$Event.prototype.add = function(handler)
{
	this._handlers.push(handler);
};

CWS.$Event.prototype.remove = function(handler)
{
	var i;
	for (i = 0; i < this._handlers.length; i++)
	{
		var h = this._handlers[i];
		if (h.constructor == handler.constructor)
		{
			break;
		}
	}
	if (i < this._handlers.length)
	{
		this._handlers.splice(i, 1);
	}
};

CWS.$Event.prototype.Invoke = function()
{
	for (var i = 0; i < this._handlers.length; ++i)
	{
		this._handlers[i]();
	}
};

CWS.$Event.prototype.Invoke = function(param1)
{
	for (var i = 0; i < this._handlers.length; ++i)
	{
		this._handlers[i](param1);
	}
};

//window object
CWS.$Window = {};

CWS.$Window.OnLoad = new CWS.$Event();
CWS.$Window.old_onload = window.onload;

CWS.$Window.Load = function()
{
	CWS.$Window.OnLoad.Invoke();
	if (CWS.$Window.old_onload)
	{
		CWS.$Window.old_onload();
	}
};

CWS.$Window.AddOnLoad = function(func)
{
	CWS.$Window.OnLoad.add(func);
	window.onload = (function()
	{
		CWS.$Window.Load();
	});
};

CWS.isUndefined = function(object)
{
	return typeof object === "undefined";
}

CWS.merge = function(array, args)
{
	array = Array.prototype.slice.call(array, 0);
    return CWS.update(array, args);
}

CWS.update = function(array, args)
{
	var arrayLength = array.length, length = args.length;
    while (length--) array[arrayLength + length] = args[length];
    return array;
}

//binding functions
CWS.bind = function(func, context)
{
	if (arguments.length < 3 && CWS.isUndefined(arguments[1])) return func;
	var __method = func, args = Array.prototype.slice.call(arguments, 2);
	return function()
	{
		var a = CWS.merge(args, arguments);
		return __method.apply(context, a);
	}
} 

CWS.addEvtListener = function(obj, event, func, capture)
{
	if (obj.addEventListener)
	{
		obj.addEventListener(event, func, capture);
	} else if (obj.attachEvent)
	{
		obj.attachEvent("on" + event, func);
		if (capture && obj.setCapture)
		{
			obj.setCapture();
		}
	}
};

CWS.dropEvtListener = function(obj, event, func, capture)
{
	if (obj === null)
	{
		return;
	}
	if (obj.removeEventListener)
	{
		obj.removeEventListener(event, func, capture);
	} else if (obj.detachEvent)
	{
		obj.detachEvent("on" + event, func);
		if (capture && obj.setCapture)
		{
			try
			{
				obj.setCapture(false);
			} catch (e) { }
		}
	}
};

//AJAX supporting
$Ajx = function()
{

	this.READY_STATE_UNINITIALIZED = 0;
	this.READY_STATE_LOADING = 1;
	this.READY_STATE_LOADED = 2;
	this.READY_STATE_INTERACTIVE = 3;
	this.READY_STATE_COMPLETE = 4;

	if ($Ajx.UseHandler)
	{
		this.url = $Ajx.ServerPath + "AjxHandler.ashx?";
	} else
	{
		this.url = $Ajx.ServerPath + "?useajax=true&";
	}

	this.contentType = "application/x-www-form-urlencoded";
	this.method = "POST";
	this.onerror = this.defaultError;
	this.req = null;
	this.onload = null;
	this.data = null;
	this.log = '';

	this.isIE = window.ActiveXObject;
	this.isOpera = navigator.appName.toUpperCase().indexOf('OPERA') !== -1;

	if (!this.isIE && !this.isOpera && window.navigator.userAgent.indexOf("MSIE") > -1)
	{
		this.isOpera = true;
	}

	if (this.isIE)
	{
		this.isIESmaller7 = (navigator.appVersion.match(/MSIE ?([\d.]+);/)[1] < 7);
	}
};

$Ajx.Error = new CWS.$Event();

$Ajx.ServerPath = "";
$Ajx.UseHandler = true;

$Ajx.prototype.defaultError = function(error)
{
	$Ajx.Error.Invoke(error);
};

$Ajx.prototype.onReadyState = function()
{
	var executed = false;

	try
	{
		if (this.req.readyState == this.READY_STATE_COMPLETE)
		{
			executed = true;
			if (this.req.status == 200 || this.req.status === 0)
			{
				if (this.onload)
				{
					var resp;
					if (this.req.responseText)
					{
						resp = this.req.responseText;
					} else
					{
						resp = this.req.ResponseText;
					}

					this.onload(this.ProcessResponseResult(resp));
				}
			}
			else
			{
				this.onerror('Unknown error');
			}
		}
	}
	catch (e) { }
	finally
	{
		if (executed && this.onload)
		{
			this.release();
		}
	}
};

$Ajx.prototype.release = function()
{
	delete this.req.onreadystatechange;
	delete this.req;
	delete this.onload;
	delete this.onerror;
	delete this.data;

};

$Ajx.prototype.Update = function(url)
{
	this.url = url;
	this.Invoke();
};

$Ajx.prototype.createRequestInstance = function()
{
	this.req = null;

	if (window.XMLHttpRequest)
	{
		this.req = new XMLHttpRequest();
	} else if (window.ActiveXObject)
	{
		try
		{
			this.req = new ActiveXObject('Msxml2.XMLHTTP');
		} catch (e)
		{
			try
			{
				this.req = new ActiveXObject('Microsoft.XMLHTTP');
			} catch (e1)
			{
				try
				{
					this.req = new ActiveXObject('Msxml2.XMLHTTP.4.0');
				} catch (e2) { }
			}
		}
	}
	if (!this.req)
	{
		if (!parent.frames.hiddenFrame)
		{
			window.location.replace(this.url + "useFrames=true&href='" + window.location.href + "'");
		} else
		{
			this.req = new $Ajx.XmlHttpFrameRequest();
		}
	}
};

$Ajx.prototype.ProcessResponseResult = function(response)
{
	var expr = 'Exception;';
	if (response && response.length >= expr.length && response.substring(0, expr.length) == expr)
	{
		$Ajx.Reload();
		return null;
	}

	expr = 'Error;';
	if (response && response.length >= expr.length && response.substring(0, expr.length) == expr)
	{
		response = response.substring(expr.length, response.length);
		throw "Server exception: " + response;
	}

	expr = 'JSON;';
	if (response && response.length >= expr.length && response.substring(0, expr.length) == expr)
	{
		response = response.substring(expr.length, response.length);
		eval("response = " + response);
		return response;
	}

	var index = response.indexOf(expr);
	if (response && response.length >= expr.length && index > -1)
	{
		response = response.substring(index + expr.length, response.length);
		eval("response = " + response);
	}

	return response;
};

$Ajx.prototype.Invoke = function()
{
	var isAsync = this.onload !== null;

	try
	{
		this.createRequestInstance();
		if (!this.req)
		{
			return;
		}
		this.req.onreadystatechange = CWS.bind(this.onReadyState, this);

		try
		{
			this.req.open(this.method, this.url, isAsync);
		}
		catch (e)
		{
			var s = window.location.href.split("/");
			var s1 = this.url.split("/");
			if (s[2] != s1[2])
			{
				this.url = s[0] + "//" + s[2] + "/" + this.url.substr((s1[0] + "//" + s1[2]).length + 1);
				this.req.onreadystatechange = CWS.bind(this.onReadyState, this);
				this.req.open(this.method, this.url, isAsync);
			}
		}

		// couse of failing authentication scheme
		if (this.isOpera)
		{
			this.req.setRequestHeader('Connection', 'close');
		}
		// http_request.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
		if (this.contentType)
		{
			this.req.setRequestHeader('Content-Type', this.contentType);
		}
		this.req.send(this.data);
		if (!isAsync)
		{
			var resp;
			if (this.req.responseText)
			{
				resp = this.req.responseText;
			} else
			{
				resp = this.req.ResponseText;
			}
			return this.ProcessResponseResult(resp);
		}
	}
	catch (err)
	{
		var message;
		if (err.message)
		{
			message = err.message;
		}
		else
		{
			message = err;
		}

		this.onerror(message);
	}
	finally
	{
		if (!isAsync)
		{
			this.release();
		}
	}
};

$Ajx.ExecuteHandler = function(handlerName, parameters)
{
	if ($Ajx.UseHandler)
	{
		return $Ajx.ServerPath + "AjxHandler.ashx?handlerName=" + handlerName + "&" + parameters;
	} else
	{
		return $Ajx.ServerPath + "?useajax=true&handlerName=" + handlerName + "&" + parameters;
	}
};

$Ajx.prototype.Execute = function()
{
	return this.execute(arguments);
};

$Ajx.Execute = function()
{
	var ajx = new $Ajx();
	return ajx.execute(arguments);
};

$Ajx.Test = function()
{
	var ajx = new $Ajx();
	ajx.createRequestInstance();
	if (!ajx.req)
	{
		return false;
	}

	return true;
};

$Ajx.Reload = function()
{
	window.location.replace(window.location.href);
};

$Ajx.prototype.execute = function(args)
{
	var i = args.length;

	if (i < 1)
	{
		return;
	}

	var arg0 = args[0];
	var j = 1;

	if (typeof (arg0) == 'function')
	{
		if (i < 2)
		{
			return;
		}

		this.onload = arg0;
		arg0 = args[1];
		j = 2;
	}

	this.url += "methodName=" + arg0;
	for (; j < i; j++)
	{
		var arg = this.serialize(args[j]);

		if (!this.data)
		{
			this.data = arg + ".;args";
		}
		else
		{
			this.data += arg + ".;args";
		}

	}
	return this.Invoke();
};

$Ajx.prototype.serialize = function(obj)
{
	var arg;
	if (obj === null)
	{
		arg = ".;null";
	} else if (obj instanceof Date)
	{
		arg = obj.getYear() + "," + obj.getMonth() + "," + obj.getDate() + "," + obj.getHours() + "," +
			obj.getMinutes() + "," + obj.getSeconds() + "," + obj.getMilliseconds();
	} else if (obj instanceof Array)
	{
		arg = ".;array[";

		for (var k = 0; k < obj.length; ++k)
		{
			if (k > 0)
			{
				arg += ",";
			}
			arg += this.serialize(obj[k]);
		}

		arg += "]";
	} else if (obj instanceof Object)
	{
		arg = ".;class[";

		for (var prop in obj)
		{
			if (typeof obj[prop] != 'function')
			{
				arg += prop + ":" + this.serialize(obj[prop]) + ".;prop";
			}
		}

		arg += "]";
	} else
	{
		arg = obj;
	}

	return arg;
};

$Ajx.XmlHttpFrameRequest = function()
{
	this.url = null;
	this.method = null;
	this.onreadystatechange = null;
	this.isAxync = false;
	this.headers = [];
	this.data = null;
};

$Ajx.XmlHttpFrameRequest.prototype.open = function(method, url, isAsync)
{
	this.url = url;
	this.method = method;
	this.isAxync = isAsync;
};

$Ajx.XmlHttpFrameRequest.prototype.setRequestHeader = function(name, value)
{
	for (var i = 0; i < this.headers.length; i++)
	{
		if (this.headers[i].name == name)
		{
			this.headers[i].value = value;
			return;
		}
	}
	this.headers.push({ "name": name, "value": value });
};

$Ajx.XmlHttpFrameRequest.prototype.send = function(data)
{
	this.data = data;
	return this.trySend();
};

$Ajx.XmlHttpFrameRequest.prototype.trySend = function()
{
	if (!this.isAxync)
	{
		//alert("Synchronous call is not supported.");
		//return;
	}

	//if(!this.isAxync)
	//{
	//	while($Ajx.XmlHttpFrameRequest.waiting)
	//	{
	//	 pause(1000);
	//	}
	//}
	//else
	if ($Ajx.XmlHttpFrameRequest.waiting)
	{
		setTimeout(CWS.bind(this.trySend, this), 100);
		return;
	}

	$Ajx.XmlHttpFrameRequest.waiting = true;

	var frame = parent.frames.hiddenFrame;
	var doc = frame.document;
	var form = doc.createElement('form');

	doc.body.appendChild(form);

	form.setAttribute("action", this.url);
	form.setAttribute("method", this.method);

	for (var i = 0; i < this.headers.length; i++)
	{
		switch (this.headers[i].name.toLowerCase())
		{
			//case "content-length": 
			//case "accept-encoding": 
			//	break; 
			//case "content-type": 
			//	form.setAttribute("enctype", this.headers[i].value); 
			//	break; 
			//default: 
			//	this.addInput(doc, form, this.headers[i].name, this.headers[i].value); 
		}
	}

	var element = doc.createElement("input");
	element.setAttribute("type", 'hidden');
	element.setAttribute("name", 'data');
	element.setAttribute("value", this.data);
	form.appendChild(element);

	form.submit();

	$Ajx.thiz = this;
	window.setTimeout('$Ajx.thiz.readystatechanged()', 1);
};

$Ajx.XmlHttpFrameRequest.prototype.readystatechanged = function()
{
	var doc = parent.frames.hiddenFrame.document;
	if (doc.readyState == "complete")
	{
		this.status = 200;
		this.readyState = 4;
		this.responseText = doc.body.innerHTML;
		if (this.onreadystatechange)
		{
			this.onreadystatechange();
		}
		$Ajx.XmlHttpFrameRequest.waiting = false;
		return;
	}
	window.setTimeout('$Ajx.thiz.readystatechanged()', 100);
};
function Tracking() {
	this.custID;
	this.proactive = '';
	this.timerInterval = 3000;
	document.write("<div id='TrackingDiv'></div>");
	CWS.$Window.AddOnLoad(CWS.bind(this.Load, this));
}

Tracking.prototype.loaded = function(result) {
	if (result && result.length > 0) {
		if (result == 'proactive') {
			this.proactive = result;

			if (this.timerInterval && this.timerInterval > 0)
				window.setTimeout("Tracking.Check()", this.timerInterval);
		}
		else {
			eval(result);
		}
	}
	else {
		if (this.timerInterval && this.timerInterval > 0)
			window.setTimeout("Tracking.Check()", this.timerInterval);
	}
}

Tracking.prototype.Load = function() {
	if (this.timerInterval && this.timerInterval > 0)
		window.setTimeout("Tracking.Check()", this.timerInterval);
	else window.setTimeout("Tracking.Check()", 1000);
}


Tracking.prototype.Check = function() {
	$Ajx.Execute(CWS.bind(this.loaded, this), "dotnetLIVEHELP.Checker.CheckForChatting", this.custID + "|" + this.proactive);
}

Tracking=new Tracking();

var popup = null;

ConfirmContent = function(){};

ConfirmContent.open = function() {
	ConfirmContent.close();
	return ConfirmContent.openURL(ConfirmContent.url);
}

ConfirmContent.openURL = function(url)
{
	try
	{
		popup = window.open(url, '_blank', 'width=600, height=600, resizable=0, scrollbars=1, toolbar=0, status=0');

		if (popup == null ||
			(
				navigator.userAgent.toLowerCase().indexOf('chrome') > -1
				&& popup.window.screenY <= 0
				&& popup.window.screenX <= 0)
			)
			return false;

		if (window.opera)
			if (!popup.opera)
				return false;
	}
	catch (err)
	{
		return false;
	}

	return true;
}

ConfirmContent.close = function() {
	CWS.$('confirmWindow').style.display = 'none';
}

ConfirmContent.closeClick = function() {
	$Ajx.Execute("dotnetLIVEHELP.Checker.CloseChat", ConfirmContent.custId, ConfirmContent.sessionId);
	ConfirmContent.close();
}

CheckConfirmation = function(url, showDiv, custId, sessionId)
{
	ConfirmContent.passed = false;
	ConfirmContent.url = url;
	ConfirmContent.custId = custId;
	ConfirmContent.sessionId = sessionId;
	//var win=ConfirmContent.open();
	if (showDiv == '2')
	{
		CheckConfirmation.ShowConfirmation();
	}
	else if (showDiv == '1')
	{
		ConfirmContent.open();
	}
	else if (showDiv == '3')
	{
		if (!ConfirmContent.open())
		{
			CheckConfirmation.ShowConfirmation();
		}
	}
}

CheckConfirmation.ShowConfirmation = function(openHandler, closeHandler)
{
	CWS.$('confirmWindow').style.display = 'block';
	CWS.$('btYes').onclick = ConfirmContent.open;
	CWS.$('btNo').onclick = ConfirmContent.closeClick;
}

CheckConfirmation.BlockerPassed = function()
{
	ConfirmContent.passed = true;
}

CheckConfirmation.CheckChrome = function()
{
	if (!ConfirmContent.passed)
	{
		if (popup != null)
		{
			popup.Chat.blocked = true;
			popup.close();
			popup = null;
		}
		CheckConfirmation.ShowConfirmation();
	}
}
Tracking.custID='101792';
Tracking.timerInterval=3000;
$Ajx.ServerPath='https://ssl.usbackgroundchecks.com/KressLiveChat/';