// 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 = 'supportcenteronline.com';
}

//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){}

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);

// Note: On IE, start of appVersion return 3 or 4
// which supposedly is the version of Netscape it is compatible with.
// So we look for the real version further on in the string

var iePos  = appVer.indexOf('msie');
if (iePos !=-1) {
   is_minor = parseFloat(appVer.substring(iePos+5,appVer.indexOf(';',iePos)))
   is_major = parseInt(is_minor);
}

// Netscape6 is mozilla/5 + Netscape6/6.0!!!
// Mozilla/5.0 (Windows; U; Win98; en-US; m18) Gecko/20001108 Netscape6/6.0
var nav6Pos = agt.indexOf('netscape6');
if (nav6Pos !=-1) {
   is_minor = parseFloat(agt.substring(nav6Pos+10))
   is_major = parseInt(is_minor)
}

// Netscape 7 is mozilla/5 + Netscape/7.0
// Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.0.1) Gecko/20020823 Netscape/7.0 
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);    // new 010118 mhp
var is_nav6up = (is_nav && is_minor >= 6) // new 010118 mhp

var is_nav5   = (is_nav && is_major == 5 && !is_nav6); // checked for ns6
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);

// KNOWN BUG: On AOL4, returns false if IE3 is embedded browser
// or if this is the first browser window opened.  Thus the
// variables is_aol, is_aol3, and is_aol4 aren't 100% reliable.

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);

// *****************************************************
//	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);	
}

var thinWindow;
//OPEN NEW THIN
//thinOpen(3,9,2,'','',1,0,'')
//thinOpen(accountID, deptID, styleID, email, screenName, thinFormat, thinStart,exitFrame)
//Need to keep this for old compatability
function thinOpen(accountID, deptID, styleID, email, screenName, thinFormat, thinStart, exitFrame, customerID) {
	thinOpen2(accountID, deptID, -1, customerID);
}

function thinOpen2(accountID, deptID, templateID, customerID)
{
	//The function parameter should always have higher priority, but in
	//case that it is -1, replace it with the templateID based on setDefault(...)
	var thinQS = '?accountID=' + accountID + '&deptID=' + deptID + '&customerID=' + customerID + '&sessionID=';
	if (is_nav) thinQS = thinQS + "&nav=true";
	if (is_nav4) thinQS = thinQS + "&nav4=true&netscapeResize=1";
	if (is_nav6) thinQS = thinQS + "&nav6=true";
	if ( SP_LAUNCH ) thinQS = thinQS + "&spLaunch=true&spHost=" + escape(document.domain);
	if ( PROTOCOL == "https://" )
	{
		thinQS = thinQS + "&HTTPS=on";
	}
	else
	{
		thinQS = thinQS + "&HTTPS=off";
	}
	
	/*if((thinWindow) && !(thinWindow.closed)) { //chat already opened, IGNORE FOR DEBUGGING
		thinWindow.focus();
	} 
	*/
//	else {
		var resizable = "yes";
		if (is_nav) { resizable = "yes"; }
		var left = window.screen.width - 430;
		var thinUrl = CYRACLE_URL + "realtime/support.asp" + thinQS;
		var windowStyle = "top=100,left=" + left + ",width=380,height=520,menubar=no,location=no,resizable=" + resizable + ",toolbar=no,scrollbars=no,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 = "/inc/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;
}
