<!DOCTYPE html>
<html>
   <head>
      <?php
         define('USER', 'jesse.turner@gatewaybaltic.com');
         define('TOKEN', 'reNaj8P1B4r3y9=fD8RaM0a4Lf30sdBda1d210we'); 
         require 'Solve360Service.php';
         $solve360Service = new Solve360Service(USER, TOKEN);
         //$comps = $solve360Service->getAllCompanies();
         $comps = $solve360Service->searchCompanies(array('layout' => '1','searchmode' => 'N5722950','searchvalue' => '','special' => 'OR', 'limit' => 2000));
         $data=array();
         $counter=false;
         foreach ($comps->children() as $ch) {
            $id=$ch->id;
            $name=$ch->name;
            $empl=$ch->custom5722950;
            $staff=$ch->custom10891353;
            if ($staff=='' && $name!='') {
               $counter=true;
               $data[]=array("$id","$name","$empl");   
            }
         }
         if ($counter==false) $data[]=array('EMPTY');   
      ?>
      
      <script type="text/javascript">
         var companies;
         var total;
         var step;
      
         function _el(id) {
            return document.getElementById(id);
         }
         
         function _write(id,text) {
            document.getElementById(id).innerHTML=text;
         }
         
         function _swap(ctr) {
            var xmlhttp;
            if (window.XMLHttpRequest) xmlhttp=new XMLHttpRequest();
            else xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
         
            xmlhttp.onreadystatechange=function() {
               if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                  var counter=xmlhttp.responseText;
                  ctr=parseInt(counter,10);
                  ctr+=step;
                  if (ctr<total) _swap(ctr);
                  else {
                     _write("status","Status: 100%");
                     _write("names","Task completed");
                  }
               }
            }
            
            var PHPdata="";
            var endCtr=ctr+step-1;
            if (endCtr>=total) endCtr=total-1;
            
            for (var i=ctr;i<=endCtr;i++) {
               PHPdata+=companies[i][0]+"_#_"+companies[i][2]+"_#_";
            }
            
            _write("status","Status: "+Math.floor(ctr/total*100)+"%");
            if (ctr==endCtr) _write("names","Company: "+companies[ctr][1]);
            else _write("names","Companies: "+companies[ctr][1]+" - "+companies[endCtr][1]);
            
            xmlhttp.open("POST","swapFields.php?ctr="+ctr,true);
            xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            xmlhttp.send("data="+PHPdata);          
         }
         
         // **************************************************************************************************
      </script>
      <style type="text/css">
         body { padding:15px 20px 20px 20px; margin:0; font-family:arial,sans-serif; font-size:100%; position:relative; }
         h1 { font-size:2.5em; }
         h2 { font-size:1.25em; }
         h3 { font-size:1.0em; }
         p,label { font-size:0.75em; }
         .bold { font-weight:bold; }
         .italic { font-style:italic; }
                  
      </style>
   </head>
   <body>
      <p id="status">Status: loading companies</p>
      <p id="names"></p>
      <script type="text/javascript">
         companies = <?php echo json_encode($data); ?>;
         total=companies.length;
         if (total==1 && companies[0][0]=="EMPTY") {
            _write("status","There are no companies to process");
         } else {
            step=Math.ceil(total/100);
            if (step==0) step=1;
            _swap(0);
         }
      </script>
   </body>
</html> 