﻿// JScript File


function getXmlRequest(){
    var xmlreq;
    
   
        try {
            xmlreq = new ActiveXObject("Msxml4.XMLHTTP");
        } catch (e) 
        {
            try {
                xmlreq = new  ActiveXObject("Microsoft.XMLHTTP");
                } catch (E) 
                {
                    xmlreq = false;
                }
         }
        

    if (!xmlreq && typeof XMLHttpRequest != 'undefined') 
    {
        try 
        {
            xmlreq = new XMLHttpRequest();
           
        } catch (e) 
        {
            xmlreq = false;
        }
    }
    return xmlreq;
}    
function AJAXRequestIdentify(x,y)  
          { 
          ShowGears();
              xmlHttp= getXmlRequest(); 
              xmlHttp.onreadystatechange = AJAXRequestIdentifyCallback; 
              
              window.status='Getting identify ...'; 
              xmlHttp.open('POST', 'AJAXHandler.ashx?action=identify&x='+x+'&y='+y, true); 
              
              xmlHttp.send(null);  
          } 
          
function AJAXRequestIdentifyCallback()  
{ 

  if(xmlHttp.readyState == 4)  
  { 
      if(xmlHttp.status == 200)  
      {  
        o = document.getElementById('tdIdentify');
        o.innerHTML=xmlHttp.responseText;
       ShowIdentify();
       HideGears();
        window.status='Identify done';
        
      } 
  }
 } 
  
  function AJAXDoPan (pcntTop,pcntLeft,width,height)
  {
  ShowGears(); 
              xmlHttp= getXmlRequest(); 
              xmlHttp.onreadystatechange = AJAXDoPanCallback; 
              //xmlHttp.onerror = AJAXDoPanError;
              window.status='moving...'; 
              xmlHttp.open('POST', 'AJAXHandler.ashx?action=pan&pcntTop='+pcntTop+
              '&pcntLeft='+pcntLeft+'&width='+width+'&height='+height, true); 
              xmlHttp.send(null); 
              
  }
  
  function AJAXDoPanCallback()
  {
       
    if(xmlHttp.readyState == 4)  
      {  
        o = document.getElementById('imgMap');
        o.src=xmlHttp.responseText;
      
       HideGears();
     //  HideIdentify()
       window.status='moving done.';
        
     } 
     
  }
  
  function AJAXDoPanError()
  {
    window.status='AJAXDoPanError.';
  }
  
  
  function AJAXPrepareImsService (name) 
  {
  // подготавливаем сервис в фоновом режиме
  
              xmlHttp= getXmlRequest(); 
              xmlHttp.onreadystatechange = AJAXPrepareImsServiceCallback; 
              //xmlHttp.onerror = AJAXDoPanError;
              window.status='prepearing service...'; 
              xmlHttp.open('POST', 'AJAXHandler.ashx?action=PrepareImsService&n='+name, true); 
              xmlHttp.send(null); 
              
  }
  
  function AJAXPrepareImsServiceCallback ()
  {
    if(xmlHttp.readyState == 4)  
        {  
            window.status='';
        }
  }
  
  
  
 
 function AJAXPrepareImsService (name, fullname) 
  {
  // подготавливаем сервис в фоновом режиме
  
              xmlHttp= getXmlRequest(); 
              xmlHttp.onreadystatechange = AJAXPrepareImsServiceCallback; 
              //xmlHttp.onerror = AJAXDoPanError;
              window.status='prepearing service...'; 
              xmlHttp.open('POST', 'AJAXHandler.ashx?action=PrepareImsService&n='+name+'&fn='+fullname, true); 
              xmlHttp.send(null); 
              
  }
  
  function AJAXPrepareImsServiceCallback ()
  {
    if(xmlHttp.readyState == 4)  
        {  
            window.status='';
        }
  }
  
    function AJAXPrepareSDEWorkSpace() 
  {  
              xmlHttp= getXmlRequest(); 
              xmlHttp.onreadystatechange = AJAXPrepareSDEWorkSpaceCallback; 
              //xmlHttp.onerror = AJAXDoPanError;
              window.status='prepearing SDEWorkSpace...'; 
              xmlHttp.open('POST', 'AJAXHandler.ashx?action=PrepareSDEWorkSpace', true); 
              xmlHttp.send(null); 
              
  }
  
  function AJAXPrepareSDEWorkSpaceCallback ()
  {
    if(xmlHttp.readyState == 4)  
        {  
            window.status='';
        }
  }
  
  
/// Запрос обращений    
//function AJAXRequestRefs()
//{
//return "11.02.2001[,]Контакт[,]aaafghdfjgh;dkljfhkdjfhgkdj[,]10[,]10[;]13.04.2007[,]Конdfkjghsdhgsdтакт[,]ddd[,]20[,]30[;]a[;]b[;]c[;]d[;]ddd[,]20[,]30[;]a[;]b[;]c[;]d[;]";
//}

function AJAXRequestRefs(dateFrom,dateTo)
  {
  ShowGears();
              xmlHttp= getXmlRequest();
              //xmlHttp.onreadystatechange = AJAXRenderMapImageCallback; 
              // xmlHttp.onerror = AJAXSendReferenceError;
             
              //запрос синхронный !!!
              xmlHttp.open('POST', 'AJAXHandler.ashx?action=RequestRefs', false); 
              //xmlHttp.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
              try
              {
              xmlHttp.send('<data dateFrom="'+dateFrom +
                               '" dateTo="'+dateTo+'" />'); 
              HideGears();
              var s=xmlHttp.responseText;
              if (s.indexOf('[FLAG_OK]')!=-1)
              {
                s=s.replace('[FLAG_OK]','');
                if (s=='' || s=='undefined') return 'Нет обращений.';
                return s;                 
              
              }
              else return 'Ошибка извлечения данных! '+xmlHttp.responseText;
              
              }
              catch (err)
              {
              HideGears();
              return err.description;
              }
              
              
             
  }

function AJAXSendReference (contact_info,text,pcntLeft,pcntTop)
  {
  ShowGears();
              xmlHttp= getXmlRequest(); 
              xmlHttp.onreadystatechange = AJAXSendReferenceCallback; 
             // xmlHttp.onerror = AJAXSendReferenceError;
             
              //xmlHttp.overrideMimeType('text/html; charset=UTF-8');
              xmlHttp.open('POST', 'AJAXHandler.ashx?action=reference', true); 
              //xmlHttp.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
              xmlHttp.send(contact_info +'[,]'+text+'[,]'+pcntLeft+'[,]'+pcntTop); 
  }
  
  function AJAXSendReferenceCallback ()
  {
  
   if (xmlHttp.readyState==4) {
   if (xmlHttp.status==200 && xmlHttp.responseText=='[FLAG_OK]') 
   {
   
   HideGears();
   ShowDivRef();

  alert('Обращение успешно отправлено!');
   
   }
    else 
    {
    AJAXSendReferenceError();
     }
  }
  
  
  }
  
  function AJAXSendReferenceError ()
  {
  HideGears();
  var str='При сохранении обращения произошла ошибка';
  if (xmlHttp!=null ) str+='\n\r '+ xmlHttp.responseText;
  alert(str);
  }
  
  function AJAXRenderMapImage (service_minx,service_maxx,
  service_miny,service_maxy,
  image_width,image_height)
  {
  ShowGears();
              xmlHttp= getXmlRequest();
              xmlHttp.onreadystatechange = AJAXRenderMapImageCallback; 
             // xmlHttp.onerror = AJAXSendReferenceError;
             
              //xmlHttp.overrideMimeType('text/html; charset=UTF-8');
              xmlHttp.open('POST', 'AJAXHandler.ashx?action=RenderMapImage', true); 
              //xmlHttp.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
              xmlHttp.send('<data service_minx="'+service_minx +
                               '" service_maxx="'+service_maxx+
                               '" service_miny="'+service_miny+
                               '" service_maxy="'+service_maxy+
                               '" image_width="'+image_width+
                               '" image_height="'+image_height+'" />'); 
  }
  
  function AJAXRenderMapImageCallback ()
  
  {
  
   if (xmlHttp.readyState==4) {
   if (xmlHttp.status==200 && xmlHttp.responseText.indexOf('[FLAG_OK]') !=-1) 
   {
   
   HideGears();
   var image_url= xmlHttp.responseText.replace('[FLAG_OK]','') ;
   
   var img;
   img = document.getElementById('imgRefPreview')
   
   if (!img) img = document.createElement('IMG');
   
   img.style.position='absolute';
   img.style.borderStyle='solid';
   img.style.borderWidth='1px';
   img.style.borderColor='black';
   //img.style.backgroundColor='ccffdd';
   img.style.left=ControlLeft(document.getElementById('imgMap'))+'px';
   img.style.top=ControlTop(document.getElementById('imgMap'))+'px';
   //img.style.width='295px';
   //img.style.height='450px';
   img.style.visibility='visible';
   img.style.overflow='auto';
   img.id='imgRefPreview';
   img.src=image_url;
   img.style.zIndex='300';
   document.getElementById('imgMap').parentNode.appendChild(img);
   document.getElementById('divMarker').style.zIndex=img.style.zIndex+1;
   ShowMarker(ControlLeft(img)+200, ControlTop(img)+150);
   }
    else 
    {
    AJAXRenderMapImageError();
     }
  }
  
  
  }
  
  function AJAXRenderMapImageError ()
  {
  HideGears();
  
  }
  
