<!-- hide JavaScript

    /** LaunchKanaRealtime.js **/

    /**
     *   KANA Realtime
	 *
     *   Copyright (c) 1995-2001 by KANA Software, Inc.
     *   181 Constitution Drive
     *   Menlo Park, CA 94025
     *   http://www.kana.com
     *
     *   All rights reserved. Patents Pending.
     **/


    // launches the KANA Realtime browser window (note: all of these parameters are optional and will
    // use a default if not specified.  however, parameters can not be skipped unless there is nothing
    // specified after it: for example, if strKanaRealtimeURL is specified than strReferringURL must
    // be specified but the rest do not need to be specified.)
    //
    //    strReferringURL     - the URL of the referring page that is launching the KANA Realtime
    //                          browser window (default is "", usually specified as location.href)
    //    strKanaRealtimeURL  - the full or relative URL to the KanaRealtime.html page in the KanaRealtime
    //                          directory (default is "KanaRealtime/KanaRealtime.html" which works for
    //                          the demo site created for each instance, typically the KANA Realtime
    //                          browser window will be launched from a completely different site so this
    //                          will usually by the full URL to that page)
    //    strFormPage         - the form page to be used (default is "AskFirstQuestionForm.html", and
    //                          the form page must be relative from the KanaRealtime directory)
    //    strFormType         - the type of form (default is "", this has no use except to pass along as
    //                          a custom field to KANA Response so that it can be used in the rules, viewed
    //                          by the CSR, used for reporting, etc.)
    //    strFormTitle        - the title of the form to be displayed in the KANA Realtime browser window
    //                          (default is "")
    //    strCustomFields     - custom fields (additional data can be passed to the KANA Realtime browser
    //                          window and the format of this string is "NAME=VALUE", where "NAME" is the
    //                          name of the custom field and "VALUE" is the value of the custom field.
    //                          for multiple custom fields in this string the format of the string is
    //                          "NAME1=VALUE1&NAME2=VALUE2&NAME3=VALUE3" etc.  note: these fields are
    //                          automatically passed on to the KANA Response server but the Response server
    //                          will discard them unless they are created as custom fields in Response)
    //    strCaseID           - the case ID to be sent to KANA Response to continue a case (default is "")
    //    strRequestedChannel - the requested channel (default is "IMAIL", the other option is "EMAIL")
    //    iWindowWidth        - the browser window width (default depends on browser - see below)
    //    iWindowHeight       - the browser window height (default depends on browser - see below)
    //
    function launchKanaRealtime( strReferringURL,
                                 strKanaRealtimeURL,
                                 strFormPage,
                                 strFormType,
                                 strFormTitle,
                                 strCustomFields,
                                 strCaseID,
                                 strRequestedChannel,
                                 iWindowWidth,
                                 iWindowHeight )
    {
        // use no referring URL if none supplied
        if ( launchKanaRealtime.arguments.length < 1 )
        {
            strReferringURL = "" ;
        }

        // use the default KANA Realtime URL if none supplied
        if ( launchKanaRealtime.arguments.length < 2 )
        {
            strKanaRealtimeURL = "" ;
        }

        // use the default ask first question form if none supplied
        if ( launchKanaRealtime.arguments.length < 3 )
        {
            strFormPage = "" ;
        }

        // use no form type if none supplied
        if ( launchKanaRealtime.arguments.length < 4 )
        {
            strFormType = "" ;
        }

        // use no form title if none supplied
        if ( launchKanaRealtime.arguments.length < 5 )
        {
            strFormTitle = "" ;
        }

        // use no custom fields if none supplied
        if ( launchKanaRealtime.arguments.length < 6 )
        {
            strCustomFields = "" ;
        }

        // use no case ID if none supplied
        if ( launchKanaRealtime.arguments.length < 7 )
        {
            strCaseID = "" ;
        }

        // use the default requested channel if none supplied
        if ( launchKanaRealtime.arguments.length < 8 )
        {
            strRequestedChannel = "" ;
        }

        // use the default window width and height if both not supplied
        if ( launchKanaRealtime.arguments.length < 9 )
        {
            iWindowWidth  = 0 ;
            iWindowHeight = 0 ;
        }


        // get the URL with the specified parameters
        var strURL = getFullKanaRealtimeURL( strReferringURL,
                                             strKanaRealtimeURL,
                                             strFormPage,
                                             strFormType,
                                             strFormTitle,
                                             strCustomFields,
                                             strCaseID,
                                             strRequestedChannel ) ;
        // get the window name
        var strWindowName = getNewWindowName() ;

        // get the window features
        var strWindowFeatures = getBrowserWindowFeatures( iWindowWidth, iWindowHeight ) ;


        // launch the KANA Realtime browser window
        window.open( strURL, strWindowName, strWindowFeatures, true ) ;


        return ;
    }


    // returns the full URL for launching the KANA Realtime browser window by
    // putting the supplied parameters in the query string for the URL
    function getFullKanaRealtimeURL( strReferringURL,
                                     strKanaRealtimeURL,
                                     strFormPage,
                                     strFormType,
                                     strFormTitle,
                                     strCustomFields,
                                     strCaseID,
                                     strRequestedChannel )
    {
        // use default KANA Realtime URL if none supplied (this needs to be either the full
        // or relative path to KanaRealtime.html in the KanaRealtime dir for the instance)
        if ( strKanaRealtimeURL == "" )
        {
            strKanaRealtimeURL = "KanaRealtime/KanaRealtime.html" ;
        }

        // use default requested channel if none supplied
        if ( strRequestedChannel == "" )
        {
            strRequestedChannel = "IMAIL" ;
        }

        // use default form page if none supplied
        if ( strFormPage == "" )
        {
            strFormPage = "AskFirstQuestionForm.html" ;
        }


        // start creating the full URL with the requested channel
        var strURL = strKanaRealtimeURL + "?_REQUESTED-CHANNEL_=" + escape( strRequestedChannel ) ;


        // add the case ID to the URL if we have one
        if ( strCaseID != "" )
        {
            strURL += "&_CASE-ID_=" + escape( strCaseID ) ;
        }


        // add the form page to the URL if we have one
        if ( strFormPage != "" )
        {
            strURL += "&IMAIL_FORMPAGE=" + escape( strFormPage ) ;
        }

        // add the form type to the URL if we have one
        if ( strFormType != "" )
        {
            strURL += "&IMAIL_FORMTYPE=" + escape( strFormType ) ;
        }

        // add the form title to the URL if we have one
        if ( strFormTitle != "" )
        {
            strURL += "&IMAIL_FORMTITLE=" + escape( strFormTitle ) ;
        }

        // add the referring URL to the URL if we have one
        if ( strReferringURL != "" )
        {
            strURL += "&IMAIL_REFERRINGURL=" + escape( strReferringURL ) ;
        }


        // add the day of the week to the URL
        strURL += "&IMAIL_DAYOFWEEK=" + escape( getDayOfWeek() ) ;


        // add custom fields to the URL if we have any
        if ( strCustomFields != "" )
        {
            strURL += getEscapedCustomFields( strCustomFields ) ;
        }


        return( strURL ) ;
    }


    // returns a new unique window name (made unique by appending the current time in
    // milliseconds since Jan 1 1970 at 00:00:00 to the end of the window name)
    // (note: using '_blank' doesn't seem to be reliable in all cases with all browsers)
    function getNewWindowName()
    {
        var theDate = new Date() ;
        var strWindowName = "KanaRealtime_" + theDate.getTime() ;

        return( strWindowName ) ;
    }


    // returns the features to be used for the browser window
    function getBrowserWindowFeatures( iRequestedWindowWidth,
                                       iRequestedWindowHeight )
    {
        var iScreenWidth  = 0 ;
        var iScreenHeight = 0 ;
        var iWindowWidth  = 0 ;
        var iWindowHeight = 0 ;
        var iWindowLeft   = 0 ;
        var iWindowTop    = 0 ;


        // is this a version 4 browser or above?
        if ( parseFloat( navigator.appVersion ) >= 4 )
        {
            // yes - get the actual screen width and height
            iScreenWidth  = screen.width  ;
            iScreenHeight = screen.height ;
        }
        else
        {
            // no - use default screen width and height
            iScreenWidth  = 640 ;
            iScreenHeight = 480 ;
        }


        // have both the window width and height been specified?
        if ( ( iRequestedWindowWidth  != 0 ) &&
             ( iRequestedWindowHeight != 0 ) )
        {
            // yes - use them
            iWindowWidth  = iRequestedWindowWidth  ;
            iWindowHeight = iRequestedWindowHeight ;
        }
        else
        {
            // no - use the default window width and height
            if ( navigator.userAgent.toLowerCase().indexOf( "msie" ) != -1 )
            {
                // Microsoft Internet Explorer size settings
                iWindowWidth  = 440 ;
                iWindowHeight = 464 ;
            }
            else
            {
                // Netscape or other browser size settings
                iWindowWidth  = 460 ;
                iWindowHeight = 520 ;
            }
        }


        // determine the coordinates to center the window
        iWindowLeft = Math.round( ( iScreenWidth  - iWindowWidth  ) / 2 ) ;
        iWindowTop  = Math.round( ( iScreenHeight - iWindowHeight ) / 2 ) ;


        // set the window features (note: the features in the string should be separated by commas
        // and not have any spaces in them -- Netscape in particular has a problem with spaces)
        var strWindowFeatures = "WIDTH="   + iWindowWidth  +
                                ",HEIGHT=" + iWindowHeight +
                                ",LEFT="   + iWindowLeft   +
                                ",TOP="    + iWindowTop    +
                                ",MENUBAR=NO,STATUS=NO,RESIZABLE=NO" ;


        return( strWindowFeatures ) ;
    }


    // returns the current day of the week as a string (i.e. Sunday, Monday, etc. )
    function getDayOfWeek()
    {
        var  theDate = new Date() ;
        var  iDayOfWeek = theDate.getDay() ;
        var  strDayOfWeek ;


        if ( iDayOfWeek == 0 )
        {
            strDayOfWeek = "Sunday" ;
        }
        else if ( iDayOfWeek == 1 )
        {
            strDayOfWeek = "Monday" ;
        }
        else if ( iDayOfWeek == 2 )
        {
            strDayOfWeek = "Tuesday" ;
        }
        else if ( iDayOfWeek == 3 )
        {
            strDayOfWeek = "Wednesday" ;
        }
        else if ( iDayOfWeek == 4 )
        {
            strDayOfWeek = "Thursday" ;
        }
        else if ( iDayOfWeek == 5 )
        {
            strDayOfWeek = "Friday" ;
        }
        else if ( iDayOfWeek == 6 )
        {
            strDayOfWeek = "Saturday" ;
        }


        return( strDayOfWeek ) ;
    }


    // returns the custom fields string with the values for the fields properly escaped
    // (e.g. "IMAIL_CUSTOM1=test one&IMAIL_CUSTOM2=test two" after being escaped becomes
    // "&IMAIL_CUSTOM1=test%20one&IMAIL_CUSTOM2=test%20two")
    function getEscapedCustomFields( strCustomFields )
    {
        var strEscapedCustomFields = "" ;
        var aFields       ;
        var aFieldParts   ;
        var strFieldName  ;
        var strFieldValue ;


        // split up the custom fields
        aFields = strCustomFields.split( "&" ) ;

        // iterate through all the custom fields
        for ( var i = 0 ; i < aFields.length ; i++ )
        {
            // split the custom field into a name and value
            aFieldParts   = aFields[ i ].split( "=" ) ;
            strFieldName  = aFieldParts[ 0 ] ;
            strFieldValue = aFieldParts[ 1 ] ;

            // add the escaped custom field to the string
            strEscapedCustomFields += "&" + escape( strFieldName ) + "=" + escape( strFieldValue ) ;
        }


        return( strEscapedCustomFields ) ;
    }


    // returns the value for the given parameter in a query string (e.g. if the query string
    // is http://mywebserver/mywebshare/KanaRealtime.html?form=myform&title=mytitle and the
    // parameter is 'form', then this function will return the value 'myform') -- an empty
    // string will be returned if the parameter does not exist or the parameter has no value
    function getQueryParameterValue( strQuery,
                                     strParameter )
    {
        // the value string
        var strValue = "" ;


        // can we find the specified parameter in the query string? (first try with "?")
        var strParamEquals = "?" + strParameter + "=" ;
        var iParamStart    = strQuery.indexOf( strParamEquals ) ;

        if ( iParamStart == -1 )
        {
            // no - can we find the specified parameter in the query string? (now try with "&")
            strParamEquals = "&" + strParameter + "=" ;
            iParamStart    = strQuery.indexOf( strParamEquals ) ;
        }


        // did we find the specified parameter?
        if ( iParamStart != -1 )
        {
            // yes - get the starting location for the value string
            var iValueStart = iParamStart + strParamEquals.length ;

            // get the value string pointed to by the parameter
            strValue = strQuery.substring( iValueStart, strQuery.length ) ;

            // is there another parameter in the value string?
            var iNextParameter = strValue.indexOf( "&" ) ;

            if ( iNextParameter != -1 )
            {
                // yes - truncate the value string before that parameter
                strValue = strValue.substring( 0, iNextParameter ) ;
            }
        }


        // unescape the value string
        strValue = unescape( strValue ) ;


        return( strValue ) ;
    }

	
    // MODIFIED by JOHN HSUAN
	// returns the value of the location parameter in the URL string (e.g. if the query string
    // is http://mywebserver/Dallas/something.html then this function will return the value 'Dallas')
    function getLocParamVal( strQuery, strParam )
    {
        // the value string
        var strValue = "" ;

        // can we find the specified parameter in the query string?
        var strParamEquals = "http://" + strParam + "/" ;
        var iParamStart    = strQuery.indexOf( strParamEquals ) ;
		
		// no - try using 'https://'
        if ( iParamStart == -1 )
        {
            strParamEquals = "https://" + strParam + "/" ;
            iParamStart    = strQuery.indexOf( strParamEquals ) ;
			if ( iParamStart == -1 )
			{
				// you can substitude the below with a redirect to an error page
				// or other actions to notify the web master the URL domain has changed
				//alert("The Domain Is No Longer \"www.cox.com\"");
			}
        }

        // did we find the specified parameter?
        if ( iParamStart != -1 )
        {
            // yes - get the starting location for the value string
            var iValueStart = iParamStart + strParamEquals.length ;

            // get the value string pointed to by the parameter
            strValue = strQuery.substring( iValueStart, strQuery.length ) ;

            // is there another parameter in the value string?
            var iNextParameter = strValue.indexOf( "/" ) ;

            if ( iNextParameter != -1 )
            {
                // yes - truncate the value string before that parameter
                strValue = strValue.substring( 0, iNextParameter ) ;
            }
        }

        // unescape the value string
        strValue = unescape( strValue ) ;

        return( strValue ) ;
    }
	
	
	// MODIFIED by JOHN HSUAN
	// returns the current time in MM/DD/HH AM/PM/YYYY
    function getCurrentTime()
    {
        // get the current date/time
		var strTime
		var timeStamp		= new Date();
		var currentYear		= timeStamp.getYear();
		var currentMonth	= timeStamp.getMonth();
        var currentDate		= timeStamp.getDate();
		var currentHour		= getFormatHours();
		
		strTime		=	currentMonth + "/";
		strTime		+=	currentDate + "/";
		strTime		+=	currentHour + "/";
		strTime		+=	currentYear;
		
        return( strTime );
		}
		
		
	// MODIFIED by JOHN HSUAN
	// This will retrun the time of day
	function getFormatHours()
	{	
		var curDateTime = new Date();
		var curHour = curDateTime.getHours();
		var curAMPM = " AM";
		var curTime = "";
		if (curHour >= 12)
		{
			curHour -= 12;
			curAMPM = " PM";
		}

		// to use just the hour (i.e., 9 AM)
		curTime = curHour + "" + curAMPM;

		return(curTime);
	}

