// COPYRIGHT CYRACLE TECHNOLOGIES INC. ALL RIGHTS RESERVED. 2000

///////////////////////////////////////////////////////////////////
//
//	ALL GLOBAL CONSTANTS AND UTILITIY FUNCTIONS GO HERE
//
///////////////////////////////////////////////////////////////////

// all files that include this file _MUST_ include globalClientSideSettings.asp
//     in order to have CYRACLE_HOST and DEBUG defined correctly
PROTOCOL		= document.location.protocol + "//";    // FIGURE OUT THE PROTOCOL

if (typeof(DEBUG) == "undefined") {
	var DEBUG = false;
}

if (typeof(CYRACLE_HOST) == "undefined") {
	CYRACLE_HOST = document.domain;
}


//if ( DEBUG && typeof(CYRACLE_HOST) == "undefined" ) {
//    throw new Error("Somewhere along the line we didn't include common.asp");
//}

try
{
//	if ( USE_CUR_DOMAIN || typeof(CYRACLE_HOST) == "undefined" )
	if ( USE_CUR_DOMAIN )
	{
		CYRACLE_HOST = document.domain;
	}
}
catch(e){}

// Add the port number in if using a specified port number
if (document.location.port.length > 0)
  CYRACLE_HOST += ':' + document.location.port;

SERVLET_URL					= "http://" + CYRACLE_HOST + "/realtime/servlet/Cyracle";
SERVLET_URL_SSL				= "https://" + CYRACLE_HOST + "/realtime/servlet/Cyracle";
APPLET_URL					= "http://" + CYRACLE_HOST + "/applet/";
APPLET_URL_SSL				= "https://" + CYRACLE_HOST + "/applet/";
CYRACLE_URL					= "http://" + CYRACLE_HOST + "/ics/";
CYRACLE_URL_SSL				= "https://" + CYRACLE_HOST + "/ics/";
REALTIME_SCRIPT_URL			= "http://" + CYRACLE_HOST + "/rt/Scripts/";
REALTIME_SCRIPT_URL_SSL		= "https://" + CYRACLE_HOST + "/rt/Scripts/";
CUSTOMIZE_SERVLET_URL		= "http://" + CYRACLE_HOST + "/realtime/servlet/Customize";
CUSTOMIZE_SERVLET_URL_SSL	= "https://" + CYRACLE_HOST + "/realtime/servlet/Customize";

ACCOUNT_ID  = -1;				// These Values will be set again in function setDefault
DEPT_ID	    = -1;
TEMPLATE_ID = -1;
SP_LAUNCH	= false;
var CyracleEmailCookie = null;
var CyracleNameCookie = null;

// ***********************************************
//		Browser Detection goes here
//
var agt=navigator.userAgent.toLowerCase(); var appVer = navigator.appVersion.toLowerCase(); var is_minor = parseFloat(appVer); var is_major = parseInt(is_minor); var iePos = appVer.indexOf('msie'); if (iePos !=-1) { is_minor = parseFloat(appVer.substring(iePos+5,appVer.indexOf(';',iePos)))
is_major = parseInt(is_minor);}
var nav6Pos = agt.indexOf('netscape6'); if (nav6Pos !=-1) { is_minor = parseFloat(agt.substring(nav6Pos+10))
is_major = parseInt(is_minor)
}
if ((agt.indexOf('mozilla') != -1) && (agt.indexOf('netscape/') != -1))
{ is_minor = parseFloat(agt.substring(agt.indexOf('netscape/')+9))
is_major = parseInt(is_minor)
}
var is_nav = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
&& (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
&& (agt.indexOf('webtv')==-1)); var is_nav2 = (is_nav && (is_major == 2)); var is_nav3 = (is_nav && (is_major == 3)); var is_nav4 = (is_nav && (is_major == 4)); var is_nav4up = (is_nav && (is_major >= 4)); var is_navonly = (is_nav && ((agt.indexOf(";nav") != -1) || (agt.indexOf("; nav") != -1)) ); var is_nav6 = (is_nav && is_major==6); var is_nav6up = (is_nav && is_minor >= 6)
var is_nav5 = (is_nav && is_major == 5 && !is_nav6); var is_nav5up = (is_nav && is_minor >= 5); var is_ie = (iePos!=-1); var is_ie3 = (is_ie && (is_major < 4)); var is_ie4 = (is_ie && is_major == 4); var is_ie4up = (is_ie && is_minor >= 4); var is_ie5 = (is_ie && is_major == 5); var is_ie5up = (is_ie && is_minor >= 5); var is_ie6 = (is_ie && is_major == 6); var is_ie6up = (is_ie && is_minor >= 6); var is_aol = (agt.indexOf("aol") != -1); var is_aol3 = (is_aol && is_ie3); var is_aol4 = (is_aol && is_ie4); var is_opera = (agt.indexOf("opera") != -1); var is_webtv = (agt.indexOf("webtv") != -1); var is_safari = (agt.indexOf("safari") != -1); var is_ff = (agt.indexOf("Firefox") != -1 || agt.indexOf("firefox") != -1); if (is_ff)
{ is_minor = parseFloat(agt.substring(agt.indexOf('firefox/')+8))
is_major = parseInt(is_minor)
var is_ff3 = is_ff && is_major == 3;}
var is_ie7 = ( is_ie && is_major == 7 );

// *****************************************************
//	Client side utility function	
//
function createRequestObject() {
  FORM_DATA = new Object();
  separator = ',';
  query = '' + this.location;
  query = query.substring((query.indexOf('?')) + 1);
  if (query.length < 1) { return false; }  // Perhaps we got some bad data?
  keypairs = new Object();
  numKP = 1;
  while (query.indexOf('&') > -1) {
    keypairs[numKP] = query.substring(0,query.indexOf('&'));
    query = query.substring((query.indexOf('&')) + 1);
    numKP++;
  }
  keypairs[numKP] = query;
  for (i in keypairs) {
    keyName = keypairs[i].substring(0,keypairs[i].indexOf('='));
    keyValue = keypairs[i].substring((keypairs[i].indexOf('=')) + 1);
    while (keyValue.indexOf('+') > -1) {
      keyValue = keyValue.substring(0,keyValue.indexOf('+')) + ' ' + keyValue.substring(keyValue.indexOf('+') + 1);
    }
    keyValue = unescape(keyValue);
    if (FORM_DATA[keyName]) {
      FORM_DATA[keyName] = FORM_DATA[keyName] + separator + keyValue;
    } else {
      FORM_DATA[keyName] = keyValue;
    }
  }
  return FORM_DATA;
}

function changeGetVariable(oldLink, varName, newValue) {
	if (oldLink.indexOf(varName) == -1) {
		return oldLink + (oldLink.indexOf("?") == -1 ? "?" : "&") + varName + "=" + newValue;
	} else {
		return oldLink.replace(new RegExp(varName + "=[^&]*"), varName + "=" + newValue);
	}
}


// Lam added this function to detect whether there is a thick available
// for CYRACLE WEBSITE
function checkThickAvailable(accountID, deptID, styleID, email, screenName, thinFormat, thinStart,exitFrame, redirectPage)
{
	var url = CYRACLE_URL + "hiddenframe.asp?accountID=" + accountID + "&deptID=" + deptID;
	url = url + "&styleID=" + styleID + "&email=" + email + "&screenName=" + screenName + "&thinFormat=" + thinFormat;
	url = url + "&thinStart=" + thinStart + "&exitFrame=" + exitFrame + "&redirectPage=" + redirectPage; 
	var screenX = window.screenLeft + 10;
	var screenY = window.screenTop + 10;
	var invisibleWin = window.open(url, "hidden", "left=" + screenX + ",top=" + screenY + ",width=100,height=100,menu=no,addressbar=no,status=no");
	window.focus();
}

function monitorOpen()
{
	var windowStyle = "left=20,top=10,width=800,height=600,resizable=yes,scrollbars=yes,status=no,";
	window.open(CYRACLE_URL + 'realtime/realtime.asp','',windowStyle);	
}

function chatTranscriptOpen(transcriptUrl,deptID)
{
  var thinQS = '?transcriptUrl='+transcriptUrl+"&deptID="+deptID;

	if (is_nav)  thinQS = thinQS + "&nav=true";
	if (is_nav4) thinQS = thinQS + "&nav4=true&netscapeResize=1";
	if (is_nav6) thinQS = thinQS + "&nav6=true";

  var left = window.screen.width - 430;
  var thinUrl = "/ics/support/chatdetails.asp" + thinQS;
  var windowStyle = "top=100,left=" + left + ",width=450,height=520,menubar=no,location=no,resizable=yes,toolbar=no,scrollbars=yes,status=no,";

  window.open(thinUrl,'',windowStyle);
}

var thinWindow;

//Used internally for Spring 09 chat and later, not used in Fall 08 chat
function chatOpen(accountID, deptID) 
{
	thinOpen2(accountID, deptID, -1, null);
}

//The Fall 08 chat link has been dreprecated. Generate a new Spring 09 chat link from the Service Desk -> Setup.
function thinOpen(accountID, deptID, styleID, email, screenName, thinFormat, thinStart, exitFrame, customerID) 
{
	alert("Sorry, this chat link is no longer supported.");
}

function thinOpen2(accountID, deptID, templateID, customerID)
{
	var thinQS = '?accountID=' + accountID + '&deptID=' + deptID + '&HTTPS=' + (PROTOCOL == "https://" ? 'yes' : 'no');

	if (is_nav)  thinQS = thinQS + "&nav=true";
	if (is_nav4) thinQS = thinQS + "&nav4=true&netscapeResize=1";
	if (is_nav6) thinQS = thinQS + "&nav6=true";

  var left = window.screen.width - 430;
  //var thinUrl = CYRACLE_URL + "support/chat/chatwindow.asp" + thinQS;
  var thinUrl = "/ics/support/chat/chatwindow.asp" + thinQS;
  var windowStyle = "top=100,left=" + left + ",width=450,height=650,menubar=no,location=no,resizable=yes,toolbar=no,scrollbars=yes,status=no,";

  thinWindow = window.open(thinUrl,'',windowStyle);
}

function setDefault(accountID, deptID, templateID, name, email) //clients from 9/7/01
{
	this.ACCOUNT_ID = accountID;
	this.DEPT_ID = deptID;
	this.TEMPLATE_ID = templateID;
	CyracleEmailCookie = email;
	CyracleNameCookie = name;
	var rtjs = REALTIME_SCRIPT_URL + "client_dept_" + DEPT_ID + ".js";
	document.write("<script src='" + rtjs + "'></script>");
}

if ( PROTOCOL == "https://" )
{
	CYRACLE_URL = CYRACLE_URL_SSL;
	SERVLET_URL = SERVLET_URL_SSL;
	APPLET_URL = APPLET_URL_SSL;
	CUSTOMIZE_SERVLET_URL = CUSTOMIZE_SERVLET_URL_SSL;
	REALTIME_SCRIPT_URL = REALTIME_SCRIPT_URL_SSL;
}

String.prototype.isEmpty = function()
{
	return this.replace(/^\s+|\s+$/g, "") == "";
}

/**
 * Gets an object with given ID from the page.
 * If ID is undefined, name will be used and the first element
 * with the name will be returned.
 *
 * @param sId ID of the item
 * @return object Object of the provided ID
 **/
function $(sId)
{
    var obj = document.getElementById(sId);
    
    // In case the element only has name but not ID defined
    if (!obj)
    {
        obj = document.getElementsByName(sId);
        if (obj != null)
        {
            obj = obj[0];
        }
    }
    
    return obj;
}
