var _chatUpdateErrors=0;
var _chatUpdateErrorsLimit=3;

var _smallChatIdent;
var _wideChatIdent;

var _chatDataReady=1;

var _smallChatFirstLoad=1;
var _wideChatFirstLoad=1;


function fnkInitUpdaterChatFunctions()
{
  if(_chatDataReady==0)
  {
     _chatUpdateErrors++;
      if(_chatUpdateErrors>_chatUpdateErrorsLimit)
      {
          _chatDataReady=1;
      }
      else
      {
          return;
      }
  }

  _chatUpdateErrors=0;

  fnkUpdateSmallChatWindow();
  fnkUpdateWideChatWindow();
}

function fnkSendSiteChatMessages(rand)
{
  FormName=document.getElementById('FormSiteChat_'+rand);
  TextSohraneniyaname='chat_send_load_messages_'+rand;

  document.getElementById(TextSohraneniyaname).innerHTML='';
if(document.getElementById('dynamic_nakladka').style.display!='none')
{
fnkOpenDynamicNakladka(0);
}
  all_values=new Array();

  for(i=0; i<FormName.length; i++){if(FormName.elements[i].type!='text' &FormName.elements[i].type!='textarea' &FormName.elements[i].type!='file' &FormName.elements[i].type!='password' &FormName.elements[i].type!='select-one'){continue;}FormName.elements[i].className = 'input';} for(i=0; i<FormName.length; i++){if(!FormName.elements[i].type || FormName.elements[i].type=='file' || FormName.elements[i].type=='button'){continue;}if (FormName.elements[i].type=='checkbox'){if(FormName.elements[i].checked==true){all_values[FormName.elements[i].id]=1;}else{all_values[FormName.elements[i].id]=0;}}else{all_values[FormName.elements[i].id]=FormName.elements[i].value;}FormName.elements[i].disabled = true;}

  for (i in all_values)
  {
     all_values[i]=fnkConvertAllSpecSymbols(all_values[i]);
  }

  document.getElementById('chat_send_but_'+rand).style.display='none';
  document.getElementById('chat_send_load_'+rand).style.display='';

  JsHttpRequest.query
  (
                "ajax_dvigki_site_chat/_ajax_fnkSendSiteChatMessages.php",
                {
                 "all_values":all_values
                },

                function (result, errors)
                {
                 fnkInitUpdaterChatFunctions();
                 document.getElementById('chat_send_but_'+rand).style.display='';
                 document.getElementById('chat_send_load_'+rand).style.display='none';

                 for(i=0; i<FormName.length; i++)
                 {
                     FormName.elements[i].disabled = false;
                 }
                 document.getElementById('say_to_chat_'+rand).focus();
                 fnkReloadPictureChat();
                 if (result.err=="no")
                 {
                    document.getElementById('say_to_chat_'+rand).value='';
                 }
                 else
                 {
                       text_to_replace=
                       '<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td height="3"></td></tr></table>'+
                       '<table width="100%" border="0" cellspacing="0" cellpadding="0">'+
                           '<tr>'+
                                '<td style="font-size:10px;padding:5px; border:1px solid #FF0000; color:#000000" bgcolor="#FFECEC">'+
                                   result.err+
                                '</td>'+
                           '</tr>'+
                        '</table>'+
                       '<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td height="3"></td></tr></table>';

                       document.getElementById(TextSohraneniyaname).innerHTML=text_to_replace;

                       if (result.stop_field_id)
                       {
                           for(i in result.stop_field_id)
                           {
                               if(
                                   document.getElementById(result.stop_field_id[i]).type!='text' &
                                   document.getElementById(result.stop_field_id[i]).type!='textarea' &
                                   document.getElementById(result.stop_field_id[i]).type!='password' &
                                   document.getElementById(result.stop_field_id[i]).type!='select-one'
                                   ){continue;}
                               document.getElementById(result.stop_field_id[i]).className = 'input_err';
                           }
                       }
                    if(document.getElementById('dynamic_nakladka').style.display!='none')
                    {
                    fnkOpenDynamicNakladka(0);
                    }
                 }
                },
  true
  )
}

function fnkUpdateSmallChatWindow()
{
  // отключаем когда включен большой чат
  if(document.getElementById('wide_chat_window_'+_wideChatIdent)){return;}

  _chatDataReady=0;
  document.getElementById('smallChatzagruzchik_'+_smallChatIdent).style.display='';
  // проверяем нужно ли прокрутить результат
  if (document.getElementById('site_chat_scroll_'+_smallChatIdent).checked==true){s_scroll=1;}else{s_scroll=0;}

  JsHttpRequest.query
  (
                "ajax_dvigki_site_chat/_ajax_fnkUpdateSmallChatWindow.php",
                {
                 "firstLoad":_smallChatFirstLoad
                },
                function (result, errors)
                {
                     document.getElementById('smallChatzagruzchik_'+_smallChatIdent).style.display='none';
                     if (result.err=="no")
                     {
                        concat_str='';
                        if(result.data_array)
                        {
                            for(i in result.data_array)
                            {
                             concat_str+=
                             '<div style="padding-top:2px; padding-bottom:2px">'+
                             '<table width="100%" border="0" cellspacing="0" cellpadding="0">'+
                                '<tr>'+
                                     '<td style="padding:2px;background:'+result.data_array[i]['bgcolor']+'">'+
                                       '<table width="100%" border="0" cellspacing="0" cellpadding="0">'+
                                          '<tr>'+
                                               '<td style="font-size:8px; color:#808080">'+result.data_array[i]['date']+'</td>'+
                                          '</tr>'+
                                          '<tr>'+
                                               '<td style="font-size:10px; color:#808080">'+result.data_array[i]['from_name']+''+result.data_array[i]['to_name']+':</td>'+
                                          '</tr>'+
                                          '<tr>'+
                                               '<td style="font-size:10px">'+result.data_array[i]['text']+'</td>'+
                                          '</tr>'+
                                       '</table>'+
                                     '</td>'+
                                '</tr>'+
                             '</table>'+
                             '</div>';
                            }
                        }

                        if(_smallChatFirstLoad==1)
                        {
                           document.getElementById('small_chat_window_'+_smallChatIdent).innerHTML=concat_str;
                        }
                        else
                        {
                           if(concat_str.length>0)
                           {
                             document.getElementById('small_chat_window_'+_smallChatIdent).innerHTML+=concat_str;
                           }
                        }

                        if (s_scroll==1)
                        {
                        setTimeout("document.getElementById('small_chat_window_"+_smallChatIdent+"').scrollTop=document.getElementById('small_chat_window_"+_smallChatIdent+"').scrollHeight",100);
                        }
                        _chatDataReady=1;
                        _smallChatFirstLoad=0;
                     }
                     else
                     {
                        _smallChatFirstLoad=1;
                        var text_to_replace=
                        '<table width="100%" border="0" cellspacing="0" cellpadding="0">'+
                            '<tr>'+
                                 '<td style="font-size:10px; padding:5px; border:1px solid #FF0000; color:#000000" bgcolor="#FFECEC">'+
                                    result.err+
                                 '</td>'+
                            '</tr>'+
                         '</table>';
                        document.getElementById('small_chat_window_'+_smallChatIdent).innerHTML=text_to_replace;
                     }
                },
  true
  )
}

function fnkUpdateWideChatWindow()
{
  // отключаем когда включен большой чат
  if(!document.getElementById('wide_chat_window_'+_wideChatIdent)){return;}

  _chatDataReady=0;
  document.getElementById('wideChatzagruzchik_'+_wideChatIdent).style.display='';
  // проверяем нужно ли прокрутить результат
  if (document.getElementById('site_chat_scroll_'+_wideChatIdent).checked==true){s_scroll=1;}else{s_scroll=0;}

  JsHttpRequest.query
  (
                "ajax_dvigki_site_chat/_ajax_fnkUpdateWideChatWindow.php",
                {
                 "firstLoad":_wideChatFirstLoad
                },
                function (result, errors)
                {
                     if(!document.getElementById('wide_chat_window_'+_wideChatIdent)){return;}
                     document.getElementById('wideChatzagruzchik_'+_wideChatIdent).style.display='none';
                     if (result.err=="no")
                     {
                        concat_str='';
                        if(result.data_array)
                        {
                            for(i in result.data_array)
                            {
                             concat_str+=
                             '<div style="padding-top:5px; padding-bottom:5px">'+
                             '<table width="100%" border="0" cellspacing="0" cellpadding="0">'+
                                '<tr>'+
                                     '<td style="padding:5px; border:1px solid #CCCCCC;background:'+result.data_array[i]['bgcolor']+'">'+
                                        '<table width="100%" border="0" cellspacing="0" cellpadding="0">'+
                                           '<tr valign="top">'+
                                                '<td>'+
                                                     '<table border="0" cellspacing="0" cellpadding="0">'+
                                                        '<tr>'+
                                                             '<td style="border:1px solid #CCCCCC; padding:1px"><img src="'+result.data_array[i]['avaimage']+'" style="width:60px; height:60px" border="0"></td>'+
                                                        '</tr>'+
                                                     '</table>'+
                                                '</td>'+
                                                '<td width="100%">'+
                                                    '<table width="100%" border="0" cellspacing="0" cellpadding="0">'+
                                                       '<tr>'+
                                                            '<td style="padding-left:10px">'+result.data_array[i]['from_name']+''+result.data_array[i]['to_name']+':</td>'+
                                                            '<td align="right" style="white-space:nowrap; padding-left:10px; font-size:10px; color:#808080">'+result.data_array[i]['date']+'</td>'+
                                                       '</tr>'+
                                                    '</table>'+
                                                    '<table width="100%" border="0" cellspacing="0" cellpadding="0">'+
                                                       '<tr><td height="5"></td></tr>'+
                                                       '<tr><td height="1" bgcolor="#CCCCCC"></td></tr>'+
                                                       '<tr><td height="5"></td></tr>'+
                                                    '</table>'+
                                                    '<table width="100%" border="0" cellspacing="0" cellpadding="0">'+
                                                       '<tr>'+
                                                            '<td style="padding-left:10px">'+result.data_array[i]['text']+'</td>'+
                                                       '</tr>'+
                                                    '</table>'+
                                                '</td>'+
                                           '</tr>'+
                                        '</table>'+
                                   '</td>'+
                                '</tr>'+
                             '</table>'+
                             '</div>';
                            }
                        }

                        if(_wideChatFirstLoad==1)
                        {
                           document.getElementById('wide_chat_window_'+_wideChatIdent).innerHTML=concat_str;
                        }
                        else
                        {
                           if(concat_str.length>0)
                           {
                             document.getElementById('wide_chat_window_'+_wideChatIdent).innerHTML+=concat_str;
                           }
                        }

                        if (s_scroll==1)
                        {
                        setTimeout("document.getElementById('wide_chat_window_"+_wideChatIdent+"').scrollTop=document.getElementById('wide_chat_window_"+_wideChatIdent+"').scrollHeight",100);
                        }
                        _chatDataReady=1;
                        _wideChatFirstLoad=0;
                     }
                     else
                     {
                        _wideChatFirstLoad=1;
                        var text_to_replace=
                        '<table width="100%" border="0" cellspacing="0" cellpadding="0">'+
                            '<tr>'+
                                 '<td style="font-size:10px; padding:5px; border:1px solid #FF0000; color:#000000" bgcolor="#FFECEC">'+
                                    result.err+
                                 '</td>'+
                            '</tr>'+
                         '</table>';
                        document.getElementById('wide_chat_window_'+_wideChatIdent).innerHTML=text_to_replace;
                     }
                },
  true
  )
}

function fnkLoadSiteUsersOnLine()
{
  if(!document.getElementById('wide_chat_users_on_site_window_'+_wideChatIdent)){return;}

  document.getElementById('load_users_online_'+_wideChatIdent).style.display='';

  JsHttpRequest.query
  (
                "ajax_dvigki_site_chat/_ajax_fnkLoadSiteUsersOnLine.php",
                {

                },
                function (result, errors)
                {
                   if(!document.getElementById('wide_chat_users_on_site_window_'+_wideChatIdent)){return;}
                   document.getElementById('load_users_online_'+_wideChatIdent).style.display='none';
                   document.getElementById('total_users_online_'+_wideChatIdent).innerHTML=result.total_online;
                   // обновляем дроп даун меню
                   fnkSetValuesToSelect('ch_to_person_'+_wideChatIdent,result.all_users);

                   if (result.err=="no")
                   {
                       concat_str='';
                       for(i in result.data_array)
                       {
                        concat_str+=
                        '<div style="padding-top:2px; padding-bottom:2px">'+
                           '<table width="100%" border="0" cellspacing="0" cellpadding="0">'+
                              '<tr valign="top">'+
                                   '<td width="100%">'+
                                       '<table width="100%" border="0" cellspacing="0" cellpadding="0">'+
                                           '<tr><td height="1" bgcolor="#CCCCCC"></td></tr>'+
                                           '<tr><td height="5"></td></tr>'+
                                       '</table>'+
                                       '<table width="100%" border="0" cellspacing="0" cellpadding="0">'+
                                          '<tr valign="top">'+
                                               '<td style="white-space:nowrap">'+result.data_array[i]['buttons']+'</td>'+
                                               '<td width="100%" style="padding-left:10px; padding-right:10px">'+result.data_array[i]['from_name']+'</td>'+
                                          '</tr>'+
                                       '</table>'+
                                   '</td>'+
                                   '<td>'+
                                        '<table border="0" cellspacing="0" cellpadding="0">'+
                                           '<tr>'+
                                                '<td style="border:1px solid #CCCCCC; padding:1px"><img src="'+result.data_array[i]['avaimage']+'" style="width:40px; height:40px" border="0"></td>'+
                                           '</tr>'+
                                        '</table>'+
                                   '</td>'+
                              '</tr>'+
                           '</table>'+
                        '</div>';
                       }
                       document.getElementById('wide_chat_users_on_site_window_'+_wideChatIdent).innerHTML=concat_str;
                   }
                   else
                   {
                        var text_to_replace=
                        '<table width="100%" border="0" cellspacing="0" cellpadding="0">'+
                            '<tr>'+
                                 '<td style="font-size:10px; padding:5px; border:1px solid #FF0000; color:#000000" bgcolor="#FFECEC">'+
                                    result.err+
                                 '</td>'+
                            '</tr>'+
                         '</table>';
                        document.getElementById('wide_chat_users_on_site_window_'+_wideChatIdent).innerHTML=text_to_replace;
                   }
                },
  true
  )
}

function fnkSetValuesToSelect(select_id,values_array)
{
    if(!document.getElementById(select_id)){return;}
    if(values_array.length<=0){return;}

    objSel=document.getElementById(select_id);

    cur_select=objSel.value;

	objSel.options.length=0;
	for (i in values_array)
	{
	objSel.options[i]=new Option();
	objSel.options[i].innerHTML = values_array[i]['text'];
	objSel.options[i].value = values_array[i]['value'];
	}

    objSel.value=cur_select;
}

function fnkIWhantToSay(to_id,privat)
{
   select_id='ch_to_person_'+_wideChatIdent;
   if(!document.getElementById(select_id)){return;}
   objSel=document.getElementById(select_id);
   objSel.value=to_id;

   if(privat==1)
   {
   document.getElementById('ch_privat_'+_wideChatIdent).value=1;
   }
   else
   {
   document.getElementById('ch_privat_'+_wideChatIdent).value=0;
   }

   document.getElementById('say_to_chat_'+_wideChatIdent).focus();
}

function fnkLoadWideChatWindow()
{
  fnkOpenPodlogkaBlack();
  fnkOpenDynamicNakladka(1);

  if(document.getElementById('dynamic_nakladka').style.display=='none'){return;}

  JsHttpRequest.query
  (
                "ajax_dvigki_site_chat/_ajax_fnkLoadWideChatWindow.php",
                {

                },

                function (result, errors)
                {
                 if(document.getElementById('dynamic_nakladka').style.display=='none'){return;}
                 document.getElementById('dynamic_nakladka_zagolovok').innerHTML=result.zagolovok;
                 _wideChatIdent=result.rand;

                 if (result.err=="no")
                 {
                    document.getElementById('dynamic_nakladka').style.display='none';
                    document.getElementById('dynamic_nakladka_text').innerHTML=result.returned_text;
 					fnkOpenDynamicNakladka(0);
 					_wideChatFirstLoad=1;
 					_smallChatFirstLoad=1;
 					fnkLoadSiteUsersOnLine();
 					fnkInitUpdaterChatFunctions();
                 }
                 else
                 {
                       var text_to_replace=
                       '<table width="100%" border="0" cellspacing="0" cellpadding="0">'+
                           '<tr>'+
                                '<td style="padding:5px; border:1px solid #FF0000; color:#000000" bgcolor="#FFECEC">'+
                                   result.err+
                                '</td>'+
                           '</tr>'+
                        '</table>';
						document.getElementById('dynamic_nakladka_text').innerHTML=text_to_replace;
					    document.getElementById('dynamic_nakladka').style.display='none';
	 					fnkOpenDynamicNakladka(0);
                 }
                },
  true
  )
}

