function Ezweb_BuildSessionUrl( parms ) {
  if( parms == 'CONFIG' )
    return EzWeb_ConfigBaseSessionUrl() + 'S_CONFIG_MANAGER/';
  if( parms == 'WEBSITE_EXTAGENT' || parms == 'WEBSITE_CALLCENTER' || parms == 'SCPT_EXTAGENT' || parms == 'SCPT_CALLCENTER')
    return EzWeb_ConfigBaseSessionUrl() + 'EASYWEBIA/' + parms;
  if( parms == 'WEBSITE_CONSUMER' )
    return EzWeb_ConfigBaseSessionUrl() + 'EASYWEBIC/' + parms;
  return EzWeb_ConfigBaseSessionUrl() + 'EASYWEBGCS/' + parms;
}

function EzWeb_ConfigBaseSessionUrl() {
  var now = new Date();
  var nowSec = now.getSeconds();
  var serverUrlList = EzWeb_ServerUrlList();
  var serverUrlListLen = serverUrlList.length;
  /* read cookies */
  var prevServerChoiceCookieName = 'EzWebPrevServerChoice';
  var prevServerChoice = parseInt( EzWeb_ReadCookie( prevServerChoiceCookieName ) );
  /* select server */
  var serverChoice;
/* temporarily disabled -- until session recovery logic via IIS is implemented
  if( prevServerChoice && !isNaN( prevServerChoice ) )
    serverChoice = prevServerChoice;
*/
  if( !serverChoice || isNaN( serverChoice ) || serverChoice < '1' || serverChoice > serverUrlListLen + 1 )
    serverChoice = ( nowSec % serverUrlListLen ) + 1;
  var serverStartUrl = serverUrlList[ serverChoice - 1 ];
  /* store cookie */
  var cookieExpirationDate = new Date( now.getTime() + EzWeb_SessionRecoveryTimeoutMs() );
  EzWeb_WriteCookie( prevServerChoiceCookieName, serverChoice, cookieExpirationDate );
  return serverStartUrl;
}

function EzWeb_ConfigBaseFileUrl() {
  return 'http://borg2.contourgroup.com/easyweb_data/';
}

function EzWeb_ServerUrlList() {
  var serverUrlList = new Array();
  /* modify the list of URLs below to contain active EasyWeb servers */
  // IIS HTTP server
  serverUrlList[ 0 ] = 'http://borg2.contourgroup.com/cooliis/cooliisr.dll?session/';
  serverUrlList[ 1 ] = 'http://mira2.contourgroup.com/cooliis/cooliisr.dll?session/';
  return serverUrlList;
}

function EzWeb_SessionRecoveryTimeoutMs() {
  return ( 1000 * 60 * 60 * 4 ); // presently set for 4 hours
}

function EzWeb_ReadCookie( cookieName ) {
  var cookieValue = '';
  if( document && document.cookie ) {
    var allCookies = document.cookie
    var pos = allCookies.indexOf( cookieName + '=' );
    if( pos >= 0 ) {
      pos += cookieName.length + 1;
      var end = allCookies.indexOf( ';', pos );
      if( end < 0 )
        end = allCookies.length;
      cookieValue = unescape( allCookies.substring( pos, end ) );
    }
  }
  return cookieValue;
}

function EzWeb_WriteCookie( cookieName, cookieValue, cookieExpirationDate ) {
  document.cookie = 
    cookieName + '=' + escape( cookieValue ) + 
    '; expires=' + cookieExpirationDate.toGMTString() +
    '; path=/';
}

function EzWeb_StoreLocation() {
  var frameLocation = null;
  try {
    document.domain = 'contourgroup.com';
    frameLocation = window.frames[ 0 ].location.href;
  }
  catch(error) {
    return;
  }
  if( frameLocation ) {
    var nLen = frameLocation.search( /page\//i );
    if( nLen != -1 ) {
      document.Ezweb_eventCloseSessionURL = frameLocation.substr( 0, nLen ) +'close'+ frameLocation.substr( nLen + 4 );
    }
  }
}

function EzWeb_CloseSession() {
if( document && document.Ezweb_eventCloseSessionURL != null )
  window.open( document.Ezweb_eventCloseSessionURL, 'CloseSessionWindow', 'width=1,height=1,left=3000,top=3000,screenX=3000,screenY=3000,innerHeight=1,innerWidth=1,alwaysLowered=yes,z-lock=yes' );
}

