/////////////////////////////////////////////////
//                                             //
//  KANA Response Live Functions               //
//  Version $Revision: 1.1 $                  //
//                                             //
//  Copyright (c) 2004 - KANA Inc.    		   //
//  All Rights Reserved.  Patent Pending.      //
//                                             //
/////////////////////////////////////////////////

//===================================================================
// Default IChannel And Server locations
// 1. Set the gIChannelID value to be the value of your default iChannel.
// 2. If you have an on-premise solution, set the gServer value to be
// the fully qualified domain name of the web server running KANA Response Live.
//===================================================================
var gIChannelID = "Default";
var gServer = "router.conavigator.com";

//===================================================================
// startMeetMe launches the basic Conav application w/o DSP
//
// IChannelID: IChannelID for interaction
// AttachedData: Company-specific data to attach to customer's acocunt
// CustomerName: the name of the customer requesting live help
// ConenctToName: the id of the rep to whom the customer is connecting
//===================================================================
function startMeetMe(iChannelID, attachedData, customerName, connectToName, server) {
	window.startVarApp = startConavVarApp;
	window.getWinProperties = getConavWinProperties;
	setServerDomain(server);
	setCustomerName(customerName);
	setConnectToName(connectToName);
	launchRegularStartPage(iChannelID, attachedData);
}


//===================================================================
// startDSPMeetMe launches the Conav application using Dynamic Start Page
//
// IChannelID: IChannelID for interaction
// AttachedData: Company-specific data to attach to customer's acocunt
// CustomerName: the name of the customer requesting live help
// ConenctToName: the id of the rep to whom the customer is connecting
//===================================================================
function startDSPMeetMe(iChannelID, attachedData, customerName, connectToName, server) {
	window.startVarApp = startConavVarApp;
	window.getWinProperties = getConavWinProperties;
	setServerDomain(server);
	setCustomerName(customerName);
	setConnectToName(connectToName);
	launchDynamicStartPage(iChannelID, attachedData);
}



//===================================================================
// startDirectCall launches the Direct Call application
//
// IChannelID: IChannelID for interaction
// AttachedData: Company-specific data to attach to customer's acocunt
//===================================================================
function startDirectCall(iChannelID, attachedData, server) {
	window.startVarApp = startDirectCallVarApp;
	window.getWinProperties = getCallbackWinProperties;
	setServerDomain(server);
	launchDynamicStartPage(iChannelID, attachedData); 	
}


//===================================================================
// startCallback launches the Basic Callback application
//
// IChannelID: IChannelID for interaction
// AttachedData: Company-specific data to attach to customer's acocunt
//===================================================================
function startCallback(iChannelID, attachedData, server) {
	window.startVarApp = startCallbackVarApp;
	window.getWinProperties = getCallbackWinProperties;
	setServerDomain(server);
	launchDynamicStartPage(iChannelID, attachedData);
}


//===================================================================
// startCallbackExtended launches the Callback application for a given
// customer telephone number.
//
// IChannelID: IChannelID for interaction
// AttachedData: Company-specific data to attach to customer's acocunt
// customerName: name of the customer
// telephoneNumber: Telephone number
// countryCode:  Telephone Country Code
//
//===================================================================
function startCallbackExtended(iChannelID, attachedData, customerName, telephoneNumber, countryCode, server) {
	window.gTelephoneNumber = telephoneNumber;
	window.gCountryCode = countryCode;
	window.gCustomerName = customerName;
	window.startVarApp = startCustomCallbackVarApp;
	window.getWinProperties = getCallbackWinProperties;
	setServerDomain(server);
	launchDynamicStartPage(iChannelID, attachedData);
}


//====================================================================================
//====================================================================================
//====================================================================================
// Private Functions and variables for KANA Use Only
// Do not modify or call these functions.
//====================================================================================
//====================================================================================
//====================================================================================

var gHelpWindow = "help";
var gLaunchUrl;
var gAttachedData;
var gConnectToName = "";
var gCustomerName = "";
var gAppType = "";
var gConavHtdocsPath = "/CONAV/HTD/";
var gHTTP = "http://";
var gHTTPS = "https://";

function setIChannelID(iChannelID) {
	if(typeof(iChannelID) == "undefined" || iChannelID == null)
		return;

	window.gIChannelID = iChannelID;
}

function getIChannelID() {
	return window.gIChannelID;
}

function setServerDomain(server) {
	if(typeof(server) == "undefined" || server == null )
		return;

	window.gServer = server;
}

function getServerDomain() {
	return window.gServer;
}

function setLaunchUrl(url) {
	window.gLaunchUrl = url;
}

function getLaunchUrl() {
	return window.gLaunchUrl;
}

function getHelpWindowName() {
	return window.gHelpWindow;
}

function setConnectToName(connectToName) {
	if(typeof(connectToName) == "undefined" || connectToName == null)
		return;

	window.gConnectToName = connectToName;
}

function getConnectToName() {
	return window.gConnectToName;
}

function setCustomerName(customerName) {
	if(typeof(customerName) == "undefined" || customerName == null)
		return;

	window.gCustomerName = customerName;
}

function getCustomerName() {
	return window.gCustomerName;
}

function setAttachedData(attachedData) {
	window.gAttachedData = attachedData;
}

function getAttachedData() {
	return window.gAttachedData;
}

function getConavHtdocsPath() {
	return window.gConavHtdocsPath;
}

function launchRegularStartPage(iChannelID, attachedData) {
	launchStartPage(iChannelID, attachedData, "invokeRegularStartPage()");
}

function launchDynamicStartPage(iChannelID, attachedData) {
	launchStartPage(iChannelID, attachedData, "invokeDynamicStartPage()");
}

function launchStartPage(iChannelID, attachedData, launchFunctionStr) {
	setIChannelID(iChannelID);
	setAttachedData(attachedData);

	//the ssl check cannot be made with opera 5.
	if( window.location.protocol == "http:" && !isOpera() ) {
		checkSSLEnabled(launchFunctionStr);
	}
	else {
		eval(launchFunctionStr);
	}
}

function checkSSLEnabled(launchFunctionStr) {
	var testImg = new Image(1,1);
	testImg.onerror = new Function("handleNoSSL()");
	testImg.onload = new Function(launchFunctionStr);
	testImg.src = gHTTPS + getServerDomain() + getConavHtdocsPath() + getIChannelID() + "/shared/images/blank.gif?time=" + (new Date()).getTime();
}

function invokeDynamicStartPage() {
	if(typeof(isHipboneSharedWindow) != "undefined") {
		launchRecursiveErrorMessage();
		return;
	}

	if(isMacButNotNetscape()) {
		startVarApp();
		return;
	}

	var link = "responseLiveLauncher.html?ichannel=" + escape(getIChannelID()) + "&host=" + escape(getServerDomain()) + "&apptype=" + gAppType + getAttachedDataQueryParam();
	launchWindow(link);
}

function invokeRegularStartPage() {
	if(typeof(isHipboneSharedWindow) != "undefined") {
		launchRecursiveErrorMessage();
		return;
	}

	startVarApp();
}

function startConavVarApp() {
	var link = gHTTPS + getServerDomain() + getConavHtdocsPath() + getIChannelID() + "/StartConavPage?time=" + (new Date()).getTime() + "&NAME=" + escape(getCustomerName()) + "&CONNECT_TO=" + escape(getConnectToName()) + getAccountSpecificQueryParam();
	launchWindow(link);
}

function startDirectCallVarApp() {
	var link = gHTTPS + getServerDomain() + getConavHtdocsPath() + getIChannelID() + "/login/oem/callback/n_to_p.thtml?METHOD=phoneCallBack.thtml";
	launchWindow(link);
}

function startCallbackVarApp() {
	var link = gHTTPS + getServerDomain() + getConavHtdocsPath() + getIChannelID() + "/login/oem/callback/n_to_p.thtml";
	launchWindow(link);
}

function startCustomCallbackVarApp() {
	var link = gHTTPS + getServerDomain() + getConavHtdocsPath() + getIChannelID() + "/login/oem/callback/n_to_p.thtml?"
   	+ "PHONE_NUMBER=" + escape(gTelephoneNumber)
   	+ "&COUNTRY_CODE=" + escape(gCountryCode)
   	+ "&CUSTOMER_NAME=" + escape(gCustomerName)
   	+ "&TIME=" + (new Date()).getTime();
	launchWindow(link);
}

function launchRecursiveErrorMessage() {
	var link = gHTTP + getServerDomain() + getConavHtdocsPath() + getIChannelID() + "/login/recursiveConav.thtml";
	launchWindow(link);
}

function handleNoSSL() {
	var link = gHTTP + getServerDomain() + getConavHtdocsPath() + getIChannelID() + "/login/sslDisabled.thtml";
	launchWindow(link);
}

function getConavWinProperties() {
	return 'scrollbars=yes,resizable=' + (isNetscape()?'no':'yes') + ',width=270,height=400,screenX=10,screenY=10,top=10,left=10';
}

function getCallbackWinProperties() {
	return 'scrollbars=yes,resizable=' + (isNetscape()?'no':'yes') + ',width=300,height=430,screenX=10,screenY=10,top=10,left=10';
}

function getAttachedDataQueryParam() {
	var attachedDataQStr = "";
	if(getAttachedData()) {
		attachedDataQStr = "&ATTACHED_DATA=" + escape(getAttachedData());
	}

	return attachedDataQStr;
}

function getAccountSpecificQueryParam() {
	var attachedDataQStr = "";
	if(getAttachedData()) {
		attachedDataQStr = "&ACCTSPECIFIC=" + escape(getAttachedData());
	}

	return attachedDataQStr;
}

//===================================================================
// AOL-safe window.open()
//===================================================================

function launchWindow(launchUrl) {
	setLaunchUrl(launchUrl);
	if(isAOL()) {
		var scriptletHTML = "<object data='responseLiveScriptletLauncher.html' type='text/x-scriptlet' width=0 height=0></object>";
		document.body.insertAdjacentHTML('BeforeEnd', scriptletHTML);
		return;
	}

	window.open(getLaunchUrl(), getHelpWindowName(), getWinProperties());
}

function isNetscape() {
	return (navigator.appName.indexOf("Netscape")!=-1);
}

function isAOL() {
	return (navigator.appVersion.indexOf("AOL")!=-1);
}

function isOpera() {
	return (navigator.userAgent.indexOf("Opera")>-1);
}

function isMac() {
	return (navigator.userAgent.indexOf("Mac")>-1);
}

function isMacButNotNetscape() {
	return (isMac() && !isNetscape());
}
