
//////////////////////////////////////////////////////////////////////////
//
// Directly from  embedPost.js
//
//////////////////////////////////////////////////////////////////////////

var _ps_use_post_to_join = 0;

function ps_usePostToJoin( val ) {
   _ps_use_post_to_join = val;
}
ps_usePostToJoin(1);


function makeFormSyncStr() {
   var form_number = document.forms.length,
       form_sync_str = "",
       form_index = 0;

   for ( ; form_index < form_number; form_index++ ) {
      var form_obj = document.forms[form_index],
          element_number = form_obj.elements.length,
          element_index = 0;
      if ( form_obj.name == 'pS_PostSubmitForm' ) {
         continue;
      }
      var get_form = 0;
      if ( form_obj.method == 'get' ) {
         get_form = 1;
      }
      for ( ; element_index < element_number; element_index++) {
         var element_obj = form_obj.elements[element_index],
             element_type = element_obj.type,
             element_header =
                   form_index + ' ' +
                   ( get_form ? element_index + 1 : element_index ) + ' ' +
                   element_type + ' ';
         if ( element_type == 'text' || element_type == 'textarea' ||
              element_type == 'password' ) {
            form_sync_str += element_header + "'" +
                        escape( element_obj.value ) + "' ";
         } else if ( element_type == "select-one" ) {
            for ( var option_index = 0;
                  option_index < element_obj.options.length; option_index++) {
               if ( element_obj.options[option_index].selected ) {
                  form_sync_str += element_header + option_index + ' ';
               }
            }
         } else if ( element_type == "select-multiple" ) {
            var select_value = "",
                option_number = element_obj.options.length,
                option_index = 0;

            for (; option_index < option_number; option_index++) {
               if ( element_obj.options[option_index].selected ) {
                  select_value = select_value + '1';
               } else {
                  select_value = select_value + '0';
               }
            }
            form_sync_str += element_header + select_value + ' ';
         } else if ( element_type == "checkbox" || element_type == "radio" ) {
            form_sync_str += element_header + element_obj.checked + ' ';
         } else if ( element_type == "checkbox" || element_type == "radio" ) {
            form_sync_str += element_header + element_obj.checked + ' ';
         }
      }
   }
   return form_sync_str;
}


var ps_domain_name = "";
var ps_cookie_dir  = "/";
var ps_secure_cookie = 0;
var ps_do_cookie = 0;
var ps_do_form_data = 0;

function ps_setSecure( secure_cookie ) {
   ps_secure = ps_secure_cookie;
}

function ps_setDoCookie( do_cookie ) {
   ps_do_cookie = do_cookie;
}

//
// In case domain, other than 'document.domain', should be
//   assigned.
//
function ps_setDomainName( domain_name ) {
   ps_domain_name = domain_name;
}

//
// Do not do form initialization unless requested.
//
function ps_setFormInit( form_init ) {
   ps_do_form_data = form_init;
}

//
// This function assumes that all cookie values
//   should be carried into the cobrowse session
//   at '/'.  If it does not fit you need, you have
//   to specially write it.
//
function ps_makeCookieStr() {
   //- alert(' - - ps_makeCookieStr()\n' +
   //-       ' - - - ps_do_cookie = "' + ps_do_cookie + '"\n' +
   //-       ' - - - document.cookie = "' + document.cookie + '"');
   if ( !ps_do_cookie || document.cookie  == "" ) {
      //- alert(' - - - ignore cookie.');
      return "";
   }

   //
   // domain is not set, use the current page.
   //
   if ( ps_domain_name == "" ) {
      ps_domain_name = document.domain;
   }
   //- alert(' - - - domain name = ' + ps_domain_name);
   var cookie_string = document.cookie;
   cookie_string += ";domain=" + ps_domain_name +
                    ";path="   + ps_cookie_dir;
   if ( ps_secure_cookie ) {
      cookie_string += ";secure";
   }
   return "?pScookie_0=" + escape(cookie_string);
}

function ps_makeParameters( post_data ) {
   //- alert('ps_makeParameters("' + post_data + '")');
   //- alert('document.location = ' + document.location);
   //- alert('window.parent.document.location = ' + window.parent.document.location);
   //- alert('window.parent.body = ' + window.parent.body);
   //- alert('window.parent.body.location = ' + window.parent.body.location);
   var parameters = ps_makeCookieStr();
   //- parameters += "?pSinitUrl=" + document.location;

   //
   // For DigitalInsight's frames:
   //
   if ( typeof(top.body) != 'undefined') {
      // Add pSFrameInitUrl parameter only for DigitalInsight's frame structure.
      parameters += "?pSFrameInitUrl=body::" + top.body.location;
   }

   //- parameters += "?pSFrameInitUrl=body::" + top.body.location; // DigitalInsight
   parameters += "?pSinitUrl=" + top.document.location;

   if ( document.forms.length > 0 && ps_do_form_data ) {
      parameters += "??pSformVal??=" + escape( makeFormSyncStr() );
   }
   if ( typeof(post_data) != 'undefined' && post_data != null &&
        post_data != '' ) {
      parameters += "??pSPost??=" + post_data;
   }
   //- alert(' - - - parameters = ' + parameters );
   return parameters;

}

function ps_makeInitUrl(url, post_data) {
   //- alert('ps_makeInitUrl()');
   if ( _ps_use_post_to_join ) {
      return url;
   }
   return url + ps_makeParameters(post_data);
}


function ps_popupHelp(url, option, post_data) {
   //- alert('ps_popupHelp() . . . \n' +
   //-       'url = ' + url + "\n" +
   //-       'option = ' + option + "\n" +
   //-       'post_data = ' + post_data + "\n" );
   //- alert(' - - - ps_do_cookie = ' + ps_do_cookie + '\n' +
   //-       ' - - - window.name = ' + window.name + '\n' +
   //-       ' - - - document.location = ' + document.location + '\n' +
   //-       ' - - - document.cookie = ' + document.cookie + '\n' +
   //-       ' - - - _ps_use_post_to_join= ' + _ps_use_post_to_join + '\n' +
   //-       '');
   url = ps_makeInitUrl(url, post_data);
   //- alert(url);

   if ( !_ps_use_post_to_join ) {
      var new_win = window.open(url, "onlineHelp", option );
      if ( !new_win ) {
        alert( "Sorry, the help window is a popup window, but" +
               " we cannot open a popup window now.\n" +
               "If you use pop-up window blocker, please disable it so we " +
               "can help you now." );
      }
   } else { // _ps_use_post_to_join
      var parameters = ps_makeParameters( post_data );
      var win_id = "join_cobrowse_win_id";

      //- alert( parameters );
      document.forms.pS_PostSubmitForm.Parameter.value=parameters;
      document.forms.pS_PostSubmitForm.action=url;
      document.forms.pS_PostSubmitForm.target=win_id;

      window.open("", win_id, option);
      var new_win = window.open("", win_id, option);
      document.forms.pS_PostSubmitForm.submit();

      if ( typeof(new_win) == undefined || !new_win ) {
         alert('Sorry, your browser does not allow the openning of new ' +
               'browser windows which are required for cobrowsing.');
      }
   }
   return false;
}


//////////////////////////////////////////////////////////////////////////
// End of embedPost.js
//////////////////////////////////////////////////////////////////////////



// --------------------------------------------------------------------



// ----------------------------------------------------------------------
// The following should be configuration variables in html pages.
// ----------------------------------------------------------------------

ps_setFormInit(1);
ps_setDoCookie(1);


//
//
//
//- alert("psHelpPopup() defined");
function psHelpPopup(url, option, post_data) {
   //- alert('psHelpPopup()');
   //- alert('popup url = ' + url);
   // var post_data = ""
   // var loc = url.indexOf("?psPost=");
   // if ( loc > 0 ) {
      // post_data = url.substring(loc+8);
   // }
   // alert(' - - - post_data = ' + post_data + '\n' +
   //       ' - - - typeof(psPop)  = ' + typeof(psPop) );


   var option = "top=200,left=0," +
                "location=0,directories=0," +
                "status=0,toolbar=0," +
                "height=350,width=300," +
                "scrollbars=1," +
                "resizable=yes";


   //- url = "https://www.pageshare.net/cgi-bin/PageShare/digital_insight/popHelp.pl";
   url = "https://cb.digitalinsight.com/cgi-bin/PageShare/digital_insight/popHelp.pl";
   ps_popupHelp(url, option, post_data);
}

if ( top == window ) {
   window.name='user_question_win';
}

//
// For DigitalInsight Only.
//
var ps_location_string = new String(document.location);

if ( ps_location_string.search('Signon.cgi') > 0 && window == top &&
     ( !window.name ||  window.name != 'body' ) ) {
   ps_setDoCookie(0);
}

//- alert(' - - - ps_do_cookie = ' + ps_do_cookie + '\n' +
//-       ' - - - window.name = ' + window.name + '\n' +
//-       ' - - - document.location = ' + document.location + '\n' +
//-       '');

//
// For digitalInsight:
//
var pS_docobrowse_help = 0;
var pS_reload_internal = 300 * 1000;  // 5 minutes?

function doPopup() { 
   //- alert('doPopup()');
   psHelpPopup();
   pS_docobrowse_help = 1;
   //- setTimeout('psReloadBodyFrame()', 200000);
   setTimeout('psReloadBodyFrame()', pS_reload_internal);
}

if ( window.name == 'body' ) {
   //- alert('document.cookie = ' + document.cookie);
   var cookie_string = document.cookie;
   if ( cookie_string.indexOf("PS_autoreload=" + document.location + "_END") 
        >= 0 ) {
      //- alert('reload again later');
      setTimeout('psReloadBodyFrame()', pS_reload_internal);
   } else {
      document.cookie = "PS_autoreload=0";
   }
}

function psReloadBodyFrame() {
   //- alert(' - - - psReloadBodyFrame()');
   //- alert(' - - - document.location = ' + document.location);
   //- alert(window.name);
   //- alert(window.parent);
   //- alert(window.parent.body);
   if ( window.name == 'body' ) {
      document.cookie = "PS_autoreload=" + document.location + "_END";
      window.parent.body.location.reload();
      setTimeout('psReloadBodyFrame()', 3000);  // redundant ?
   }
}

//- alert('window.location = ' + window.location);
//- alert('window.parent.location = ' + window.parent.location);
//- alert('window.body = ' + window.body);
//- alert('window.body.document.location = ' + window.body.document.location);



var _ps_use_post_to_join = 1;

//////////////////////////////////////////////////////////////////////////
// 
// Directly from  embedPost.js
// When the web page use document.forms[N], we have to injuect the form at the
//    end of the document (at least at the end of forms).
//
//////////////////////////////////////////////////////////////////////////

function printHiddenPostForm( action ) {
   if ( _ps_use_post_to_join ) {
      document.write('<FORM METHOD="POST"' +
                     'action="' + action + '" ' +
                     'NAME="pS_PostSubmitForm">' +
                     '<INPUT type=hidden name=Parameter>' +
                     '</FORM>');
   }
}
printHiddenPostForm("");
//- alert(' - - - ps_do_cookie = ' + ps_do_cookie + '\n' +
//-       ' - - - window.name = ' + window.name + '\n' +
//-       ' - - - document.location = ' + document.location + '\n' +
//-       '');

