var chatwin = null;

function chat(params, chatwin)
{
     // activate existing window if exists and not closed
     if (chatwin != null && !chatwin.closed) {
          chatwin.focus();
          return chatwin;
     }

     var url = 'http://clavardage.sematix.com:8000/web/chat_hall';
     var name = 'chat';
     var width = 600;
     var height = 500;
     var pos_y = Math.round((screen.availHeight - height)/2);
     var pos_x = Math.round((screen.availWidth - width)/2);
     var new_window = window.open('', name, 'menubar=no, status=no, scrollbars=no, resizable=no, width=' + width + ', height=' + height + ', top=' + pos_y + ', left=' + pos_x);
     if (!new_window) return false;

     var html = '';
     html += '<html><head><META HTTP-EQUIV="Content-Type" content="text/html; charset=utf-8"></head>';
     html += '<body><form id="formid" method="post" action="' + url + '">';
     html += '<input type="hidden" name="params" value="' + params + '"/>';
     html += '</form>';
     html += '<script type="text/javascript">document.getElementById("formid").submit();</sc' + 'ript>';
     html += '</body></html>';

     // if we can't write to new_window, it's because it was previously opened
     try {
          new_window.document.write(html);
          new_window.document.close();
          }
     catch(err) {
          new_window.focus();
          };
     return new_window;
}

function chat_subject(params, chatwin)
{
     // activate existing window if exists and not closed
     if (chatwin != null && !chatwin.closed) {
          chatwin.focus();
          return chatwin;
     }

     var url = 'http://clavardage.sematix.com:8000/web/chat_hall';
     var name = 'chat';
     var width = 600;
     var height = 500;
     var pos_y = Math.round((screen.availHeight - height)/2);
     var pos_x = Math.round((screen.availWidth - width)/2);
     var new_window = window.open('', name, 'menubar=no, status=no, scrollbars=no, resizable=no, width=' + width + ', height=' + height + ', top=' + pos_y + ', left=' + pos_x);
     if (!new_window) return false;

     // add subject index to params
     var select = document.getElementById("subject")
     var index  = select.selectedIndex

     var html = '';
     html += '<html><head><META HTTP-EQUIV="Content-Type" content="text/html; charset=utf-8"></head>';
     html += '<body><form id="formid" method="post" action="' + url + '">';
     html += '<input type="hidden" name="params" value="' + params + '"/>';
     html += '<input type="hidden" name="params" value="[{subject_index, ' + index + '}]"/>';
     html += '</form>';
     html += '<script type="text/javascript">document.getElementById("formid").submit();</sc' + 'ript>';
     html += '</body></html>';

     // if we can't write to new_window, it's because it was previously opened
     try {
          new_window.document.write(html);
          new_window.document.close();
          }
     catch(err) {
          new_window.focus();
          };
     return new_window;
}
