
var chatSpots = "chat/spots.php";

function init(o,i)
{
 if(this.handle)
 {
   o=this.handle.firstChild.getElementsByTagName('td')
   for(i=0;i<o.length;i++){
    o[i].onmouseover=function(){if('true'!=this.getAttribute('sys_selected')){this.firstChild.className='ctlTabTopOvr';this.lastChild.className='ctlTabBtmOvr'}}
    o[i].onmouseout=function(){if('true'!=this.getAttribute('sys_selected')){this.firstChild.className='ctlTabTop';this.lastChild.className='ctlTabBtm'}}
    o[i].onclick=function(o,i){
     if((o=this.getElementsByTagName('a')).length){document.location=o[0].href;return}
     o=this.parentNode.getElementsByTagName('td');
    for(i=0;i<o.length;i++){
      if('true'==o[i].getAttribute('sys_selected')){o[i].firstChild.className='ctlTabTop';o[i].lastChild.className='ctlTabBtm';o[i].lastChild.firstChild.className='ctlTab';o[i].setAttribute('sys_selected', 'false')}
      o[i].parentNode.parentNode.parentNode.nextSibling.childNodes[i].style.display=['none','block'][+(this==o[i])]
     }
     this.firstChild.className='ctlTabTopSel';this.lastChild.className='ctlTabBtmSel';this.lastChild.firstChild.className='ctlTabSel';this.setAttribute('sys_selected', 'true');
    }
    o[i].onselect=o[i].onselectstart=o[i].onmousedown=function(){return false}
   }
 }
 this.getSelectedIndex=function(o,i)
   {
     if(this.handle)
     {
       o=this.handle.firstChild.getElementsByTagName('td');
       for(i=0;i<o.length;i++)
       if('true'==o[i].getAttribute('sys_selected'))return i;return -1
     }
   }
}


function doThis()
{
 handle=sysGetObj('TabControl_0');
 init(handle, 0);
   requestNewSpots();
}

function sysGetObj(i) {return document.getElementById(i)}

// **************** SPOTS
var updateClusterInterval = 100000; // w milisekundach
var xmlHttpGetSpots = createXmlHttpRequestObject();
var lastSpotID = -1;

function createXmlHttpRequestObject()
{
  var xmlHttp;
  try
  {
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
    {
      try
      {
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      }
      catch (e) {}
    }
  }

  if (!xmlHttp)
    alert("ERROR during creating XMLHttpRequest object.");
  else
    return xmlHttp;
}


function requestNewSpots()
{
  if(xmlHttpGetSpots)
  {
    try
    {
      if (xmlHttpGetSpots.readyState == 4 ||
          xmlHttpGetSpots.readyState == 0)
      {
        var params = "";
          params = "id_spot="+lastSpotID;

        xmlHttpGetSpots.open("POST", chatSpots, true);
        xmlHttpGetSpots.setRequestHeader("Content-Type",
                                   "application/x-www-form-urlencoded");
        xmlHttpGetSpots.onreadystatechange = handleReceivingSpots;

        xmlHttpGetSpots.send(params);

      }
      else
      {
        setTimeout("requestNewSpots();", updateClusterInterval);
      }
    }
    catch(e)
    {
      displayError(e.toString());
    }
  }
}

function handleReceivingSpots()
{
  if (xmlHttpGetSpots.readyState == 4)
  {
    if (xmlHttpGetSpots.status == 200)
    {
      try
      {
        readSpots();
      }
      catch(e)
      {
        displayError(e.toString());
      }
    }
    else
    {
      displayError(xmlHttpGetSpots.statusText);
    }
  }
}

function readSpots()
{

var xml = xmlHttpGetSpots.responseXML.documentElement;


  var newSpot = xml.getElementsByTagName("new_spot").item(0).firstChild.data;
   if(newSpot == "true")
   {
  idArray = xml.getElementsByTagName("id");
  typeArray = xml.getElementsByTagName("type");
  userArray = xml.getElementsByTagName("user");
  stationArray = xml.getElementsByTagName("station");
  freqArray = xml.getElementsByTagName("freq");
  timeArray = xml.getElementsByTagName("date_cluster");
  commentArray = xml.getElementsByTagName("comments");

     if(lastSpotID==-1)
        {
          lastSpotID = xml.getElementsByTagName("id").item(0).firstChild.data;
          var sortSpots="true";
        }
      else {
      var sortSpots="false";
          lastSpotID = idArray.item(idArray.length - 1).firstChild.data;
    }
  displaySpots(idArray, typeArray, userArray, stationArray, freqArray, timeArray, commentArray, sortSpots);
  }
  else
  setTimeout("requestNewSpots();", updateClusterInterval);
}


function displaySpots(idArray, typeArray, userArray, stationArray, freqArray, timeArray, commentArray, sortSpots)
{
 if(sortSpots=="false"){
  for(var i=0; i<idArray.length; i++)
  {
    var chatID = idArray.item(i).firstChild.data.toString();


    var type = typeArray.item(i).firstChild.data.toString();
    var user = userArray.item(i).firstChild.data.toString();
    var station = stationArray.item(i).firstChild.data.toString();
    var freq = freqArray.item(i).firstChild.data.toString();
    var time = timeArray.item(i).firstChild.data.toString();
    var comment = commentArray.item(i).firstChild.data.toString();

    var htmlMessage = "";
       htmlMessage +="<table style=\"width:570px;\" cellpadding=\"1\" cellspacing =\"1\" valing=\"top\"><tr>";
       htmlMessage +="<td  class=\"clusterScrol\" style=\"width:95px;\">" + time + "</td>";
       htmlMessage +="<td  class=\"clusterScrol\" style=\"width:30px;text-align:center;\">" + type + "</td>";
       htmlMessage +="<td  class=\"clusterScrol\" style=\"width:87px;\">" + station + "</td>";
       htmlMessage +="<td  class=\"clusterScrol\" style=\"width:60px;\">" + user+ "</td>";
       htmlMessage +="<td  class=\"clusterScrol\" style=\"width:66px;text-align:center;\">" + freq+ "</td>";
       htmlMessage +="<td  class=\"clusterScrol\" >" + comment + "</td></tr></table>";
    displaySpot (htmlMessage);
  }
}
 else
 {
  for(var z=idArray.length - 1; z>=0; z--)
  {
     var chatID = idArray.item(z).firstChild.data.toString();
    var type = typeArray.item(z).firstChild.data.toString();
    var user = userArray.item(z).firstChild.data.toString();
    var station = stationArray.item(z).firstChild.data.toString();
    var freq = freqArray.item(z).firstChild.data.toString();
    var time = timeArray.item(z).firstChild.data.toString();
    var comment = commentArray.item(z).firstChild.data.toString();

    var htmlMessage = "";
       htmlMessage +="<table style=\"width:577px;\" cellpadding=\"1\" cellspacing =\"1\" valing=\"top\"><tr>";
       htmlMessage +="<td  class=\"clusterScrol\" style=\"width:95px;background:#FFFFF4;\">" + time + "</td>";
       htmlMessage +="<td  class=\"clusterScrol\" style=\"width:37px;text-align:center;background:#FFFFF4;\">" + type + "</td>";
       htmlMessage +="<td  class=\"clusterScrol\" style=\"width:85px;background:#FFFFF4;\">" + station + "</td>";
       htmlMessage +="<td  class=\"clusterScrol\" style=\"width:59px;background:#FFFFF4;\">" + user+ "</td>";
       htmlMessage +="<td  class=\"clusterScrol\" style=\"width:66px;text-align:center;background:#FFFFF4;\">" + freq+ "</td>";
       htmlMessage +="<td  class=\"clusterScrol\" style=\"background:#FFFFF4;\">" + comment + "</td></tr></table>";


    displaySpot (htmlMessage);
  }

 }
  setTimeout("requestNewSpots();", updateClusterInterval);

}

function displaySpot(message)
{
if(message!=""){
  var oScroll = document.getElementById("scroll_cluster");
// var scrollDown = (oScroll.scrollHeight - oScroll.scrollTop <=
                  //  oScroll.offsetHeight );
  oScroll.innerHTML = message+oScroll.innerHTML;
  oScroll.scrollTop = 0;
  }
    message="";
}

function sendInfo(){
var a_send = document.getElementById("activ_send");
var a_email = document.getElementById("activ_email");
var a_call = document.getElementById("activ_call");
var a_qrg = document.getElementById("activ_qrg");
var a_island = document.getElementById("activ_island");
var a_by = document.getElementById("activ_by");
var a_qsl = document.getElementById("activ_qsl");
var a_date1 = document.getElementById("activ_date1");
var a_date2 = document.getElementById("activ_date2");
var a_comment = document.getElementById("activ_comment");
var sms = document.getElementById("sms");

      //    alert(a_comment.value);

     advAJAX.post
     ( {
         url : "./add_user_activity.php",
         parameters :
         {
            "a_send" : a_send.value,
            "a_email" : a_email.value,
            "a_call" : a_call.value,
            "a_qrg" : a_qrg.value,
            
            "a_island" : a_island.value,
            "a_by" : a_by.value,
            "a_qsl" : a_qsl.value,
            "a_date1" : a_date1.value,
            "a_date2" : a_date2.value,
            "a_comment" : a_comment.value

         },
         onInitialization : function ()
                           {

                                    sms.innerHTML = "PLEASE wait !!! ";

                            },
        onSuccess : function (obj)
                               {
                                var result = obj.responseXML.getElementsByTagName("result").item(0).firstChild.data ;
                                var sms_comment = obj.responseXML.getElementsByTagName("sms").item(0).firstChild.data ;
                                if(result==0){

                                sms.innerHTML = "<span style=\"color:red;font:bold 14px arial;\"> ERROR</span> ... "+sms_comment;
                                }
                                if(result==1){
                                sms.innerHTML = "<span style=\"color:#00FFFF;font:bold 14px arial;\"> MANY THANK, </span> &nbsp;&nbsp;your information was sended to update team";

                               var a_call = document.getElementById("activ_call");
                                a_call.value = "";
                                 a_qrg.value = "";
                                  a_island.value = "";
                                   a_by.value = "";
                                    a_qsl.value = "";
                                     a_date1.value = "";
                                      a_date2.value = "";
                                       a_comment.value = "";
                                }
                               },
        onError : function (obj)
                                {

                                 }
       }
     )

  }

function view_more(x){
var div_more=document.getElementById("view"+x);
var click_div=document.getElementById("click"+x);
if(click_div.innerHTML=="view more"){
   click_div.style.color="red";
   click_div.innerHTML="CLOSE";
   div_more.style.display="block";
}else {
   click_div.style.color="blue";
   click_div.innerHTML="view more";
   div_more.style.display="none";
}}

function Sview_more(x){
var div_more=document.getElementById("Sview"+x);
var click_div=document.getElementById("Sclick"+x);
if(click_div.innerHTML=="view more"){
   click_div.style.color="red";
   click_div.innerHTML="CLOSE";
   div_more.style.display="block";
}else {
   click_div.style.color="blue";
   click_div.innerHTML="view more";
   div_more.style.display="none";
}}

function hideDiv(){
var search_div=document.getElementById("search_div");
//search_div.style.display="none";
}

function searchData(page){
var divData=document.getElementById("search_div");   // div do wyrzucenia danych z xml
var searchField=document.getElementById("search_field").value;   // pole do wpisywania call aktywacji
var resultField=document.getElementById("resultField");   // pole hidden, zapisane ile wyszukano aktywacji
var smsDiv=document.getElementById("smsDiv");            // div do wypisania komunikatu


var searchButton=document.getElementById("searchButton");   // submit formularza

     //  alert("activationsXMLview.php?call="+searchField+"&page="+page);

if(resultField.value==0){

    smsDiv.innerHTML="lack of data to review";
    var top="";
top+="<table style =\"border:2px solid brown;border-top:0;margin-left:1px;\" width=\"595\" cellspacing=\"0\" cellpadding=\"0\">";
top+="<tr>";
      top+="<TH WIDTH=\"100px\" class=\"th_t\">callsign</TH>";
      top+="<TH WIDTH=\"40\" class=\"th_t\" style=\"border-left:0px;\">type</TH>";
      top+="<TH WIDTH=\"\" ALIGN = \"LEFT\" class=\"th_t\" style=\"border-left:0px;\">island</TH>";
      top+="<TH WIDTH=\"80\" class=\"th_t\" style=\"border-left:0px;\">date start</TH>";
      top+="<TH WIDTH=\"80\" class=\"th_t\" style=\"border-left:0px;\">date finish</TH>";
      top+="<TH WIDTH=\"70\" class=\"th_t\" style=\"border-left:0px;\">more</TH>";
top+="</tr></table>";
      divData.innerHTML=top;
} else {
          advAJAX.get
          (
             {
              url: "activationsXMLview.php?call="+searchField+"&page="+page,
                     
              onInitialization : function ()
                           {
                                    searchButton.value = "please wait";
                                    smsDiv.innerHTML="PLEASE WAIT ... PLEASE WAIT";
                                    
                            },
              onSuccess : function(obj)
              {

                   viewData(obj.responseXML,page);
              }

          });

       }
}


function viewData(xml,a_page){
var searchButton=document.getElementById("searchButton");
var divData=document.getElementById("search_div");   // div do wyrzucenia danych z xml
var searchField=document.getElementById("search_field").value;   // pole do wpisywania call aktywacji
var resultField=document.getElementById("resultField");   // pole hidden, zapisane ile wyszukano aktywacji
var smsDiv=document.getElementById("smsDiv");            // div do wypisania komunikatu


var total_page= parseInt(xml.getElementsByTagName("totalPage").item(0).firstChild.data);
var total_rows= parseInt(xml.getElementsByTagName("totalRows").item(0).firstChild.data);

var top="";
top+="<table style =\"border:2px solid brown;border-top:0;margin-left:1px;\" width=\"595\" cellspacing=\"0\" cellpadding=\"0\">";
top+="<TR>";
      top+="<TH WIDTH=\"100px\" class=\"th_t\">callsign</TH>";
      top+="<TH WIDTH=\"40\" class=\"th_t\" style=\"border-left:0px;\">type</TH>";
      top+="<TH WIDTH=\"\" ALIGN = \"LEFT\" class=\"th_t\" style=\"border-left:0px;\">island</TH>";
      top+="<TH WIDTH=\"80\" class=\"th_t\" style=\"border-left:0px;\">date start</TH>";
      top+="<TH WIDTH=\"80\" class=\"th_t\" style=\"border-left:0px;\">date finish</TH>";
      top+="<TH WIDTH=\"70\" class=\"th_t\" style=\"border-left:0px;\">more</TH>";
top+="</TR>";

                  if(total_rows==0){
                       searchButton.value="Search data";
                       smsDiv.innerHTML="lack of data to review";
                       top+="</table>";
                       divData.innerHTML=top;
                  }else{
                  var a_id= xml.getElementsByTagName("a_id");
                  var a_type= xml.getElementsByTagName("a_type");
                  var a_call= xml.getElementsByTagName("a_call");
                  var a_qrg= xml.getElementsByTagName("a_qrg");
                  var a_island= xml.getElementsByTagName("a_island");
                  var a_id_isl= xml.getElementsByTagName("a_id_isl");
                  var a_by= xml.getElementsByTagName("a_by");
                  var a_qsl_by= xml.getElementsByTagName("a_qsl_by");
                  var a_start= xml.getElementsByTagName("a_start");
                  var a_end= xml.getElementsByTagName("a_end");
                  var a_comment= xml.getElementsByTagName("a_comment");
                  var b="";
                  var borderpx=1;
                  for (var i=0; i<total_rows; i++)
                  {
                     var sms="";
                           sms+="frequency: "+a_qrg.item(i).firstChild.data+"<br />";
                           sms+="activite by: "+a_by.item(i).firstChild.data+"<br />";
                           sms+="QSL manager: "+a_qsl_by.item(i).firstChild.data+"<br />";
                           sms+=a_comment.item(i).firstChild.data;
      
      
                 if(borderpx==1)  bpx="2px"; else bpx="1px";
                 b+= "<TR>\n";
                 b+= "<TD ALIGN=\"LEFT\" style=\"height:16px;border-top:"+bpx+" solid brown;border-right:1px solid brown;font:bold 11px arial,verdana;color:#4A0000;padding-left:4px;\" bgcolor=\"#FFFFF4\">"+a_call.item(i).firstChild.data+"</TD>\n";
                 b+= "<TD ALIGN=\"CENTER\" style=\"height:16px;border-top:"+bpx+" solid brown;border-right:1px solid brown;font:bold 11px verdana;color:#4A0000;\" bgcolor=\"#FFFFF4\">"+a_type.item(i).firstChild.data+"</TD>\n";
                              if(a_id_isl.item(i).firstChild.data==0){
                                   b+= "<TD ALIGN=\"LEFT\" style=\"height:16px;border-top:"+bpx+" solid brown;border-right:1px solid brown;font:12px verdana;color:#4A0000;padding-left:2px;\" bgcolor=\"#FFFFF4\">"+a_island.item(i).firstChild.data+"</TD>\n";
                              }
                              else {
                                    if(a_type.item(i).firstChild.data=="IOTA" || a_type.item(i).firstChild.data=="N/V")
                                     b+= "<TD ALIGN=\"LEFT\" style=\"height:16px;border-top:"+bpx+" solid brown;border-right:1px solid brown;font:12px verdana;color:#4A0000;padding-left:2px;\" bgcolor=\"#FFFFF4\"><a class=\"aa\" href=\"./1_1.php?ID_is="+a_id_isl.item(i).firstChild.data+"\">"+a_island.item(i).firstChild.data+"</a></TD>\n";
                                     else
                                      b+= "<TD ALIGN=\"LEFT\" style=\"height:16px;border-top:"+bpx+" solid brown;border-right:1px solid brown;font:12px verdana;color:#4A0000;padding-left:2px;\" bgcolor=\"#FFFFF4\"><a class=\"aa\" href=\"./1_iwi.php?ID_iwi="+a_id_isl.item(i).firstChild.data+"\">"+a_island.item(i).firstChild.data+"</a></TD>\n";
                               }
                                      b+= "<TD ALIGN=\"CENTER\" style=\"height:16px;border-top:"+bpx+" solid brown;border-right:1px solid brown;font:12px verdana;color:#4A0000;\" bgcolor=\"#FFFFF4\">"+a_start.item(i).firstChild.data+"</TD>\n";
                                      b+= "<TD ALIGN=\"CENTER\" style=\"height:16px;border-top:"+bpx+" solid brown;border-right:1px solid brown;font:12px verdana;color:#4A0000;\" bgcolor=\"#FFFFF4\">"+a_end.item(i).firstChild.data+"</TD>\n";
                                      b+= "<TD ALIGN=\"CENTER\" style=\"cursor:pointer;height:16px;border-top:"+bpx+" solid brown;border-right:0px;\" bgcolor=\"#FFFFF4\"><div onClick=\"Sview_more("+a_id.item(i).firstChild.data+")\" id=\"Sclick"+a_id.item(i).firstChild.data+"\" style=\"font:bold 11px arial, verdana;color:blue;\">view more</div></TD>\n";
                                      b+= "</TR>\n";
                                      
                                      
                                      b+= "<TR>\n";
                                      b+= "<TD colspan=\"6\">\n";
                                      b+= "<div style=\"text-align:left;background-color:#FFFFF4;padding:5px;border-top:1px solid brown;font:11px verdana;display:none;\" id=\"Sview"+a_id.item(i).firstChild.data+"\">"+sms+"</div>";
                                      b+= "</TD>\n";
                                      b+= "</TR>\n";
                  borderpx++;
                  }
          if(total_page>1){
                 b+= "<TR>\n";
                 b+= "<TD colspan=\"6\" style=\"padding:2px;text-align;center;background-color:#FFFFF4;font:bold 12px arial,verdana;border-top:2px solid brown;\" >\n";
               //  b+= "<div style=\"background-color:#FFFFF4;padding:5px;border-top:1px solid brown;font:11px verdana;display:block;\" id=\"Sview"+a_id.item(i).firstChild.data+"\">"+sms+"</div>";
                 b+="page: ";
                 x=1;
                 for(var z=0;z<total_page;z++){
                     if(a_page==x){
                      b+=" <div style=\"display:inline;color:red;padding:4px;\">[ "+x+" ]</div> ";
                     } else{
                            b+=" <div onClick=\"searchData("+x+")\" style=\"display:inline;cursor:pointer;color:blue;padding:4px;\"> "+x+" </div> ";
                            }
                 x++;
                 }
                 b+= "</TD>\n";
                 b+= "</TR>\n";
          }
b+= "</table>\n";
divData.style.display="block";
divData.innerHTML=top+b;

                 if(total_rows==1){
                 smsDiv.innerHTML="view:&nbsp;&nbsp;<font color=\"red\">( 1 )</font>&nbsp;&nbsp;activation";
                 }else {
                       if(total_page==1)
                 smsDiv.innerHTML="view:&nbsp;&nbsp;<font color=\"red\">( "+total_rows+" )</font>&nbsp;&nbsp;activations";
                       else
                 smsDiv.innerHTML="view:&nbsp;&nbsp;<font color=\"red\">( "+total_rows+" )</font>&nbsp;&nbsp;activations&nbsp;&nbsp;&nbsp;&nbsp; page &nbsp;"+a_page+" &nbsp; of &nbsp;"+total_page+" &nbsp; pages";
                 }


//smsDiv.innerHTML="total page: "+total_page;
searchButton.value="Search data";
}
}







function countActivations(){
       var searchField=document.getElementById("search_field").value;
       var smsDiv=document.getElementById("smsDiv");
       var resultField=document.getElementById("resultField");   // pole hidden, zapisane ile wyszukano aktywacji
     if(searchField.length>0){
          advAJAX.get
          (
             {
              url: "activationsXMLcount.php?call=" + searchField,
              onSuccess : function(obj)
              {

                  xml=obj.responseXML;
                  var a = xml.getElementsByTagName("result").item(0).firstChild.data;
                  resultField.value=a;
                 if(a==1){
                 smsDiv.innerHTML="searched result:&nbsp;&nbsp;<font color=\"red\">( 1 )</font>&nbsp;&nbsp;activation";
                 }else {
                 smsDiv.innerHTML="searched result:&nbsp;&nbsp;<font color=\"red\">( "+a+" )</font>&nbsp;&nbsp;activations";
                 }

             }
          });
      }
      else {
       smsDiv.innerHTML="search activation by callsign";
      
      }
}



