<!DOCTYPE html>
<html>
   <head>
      <?php
         define('USER', 'jesse.turner@gatewaybaltic.com');
         define('TOKEN', 'reNaj8P1B4r3y9=fD8RaM0a4Lf30sdBda1d210we'); 
         require 'Solve360Service.php';
         
         $solve360Service = new Solve360Service(USER, TOKEN);
      	
         $cons = $solve360Service->searchContacts(array('layout' => '1', 
                        'searchmode' => 'Sany','searchvalue' => "",
                        'limit' => '99999', 'sortfield' => '', 'sortdir' => 'ASC','special' => 'OR'));
         
         $contacts=array();
         foreach ($cons->children() as $ch) {
            $conId=$ch->id;
            $conName=$ch->name;
            $conWebsite=$ch->website;
            $conCompany=$ch->company;
            $conCountry=$ch->custom4698752;
            
            if (strlen($conName)>0) $contacts[]=array("$conId","$conName","$conWebsite","$conCountry","$conCompany");
       	}
       	
      
       	$comps = $solve360Service->searchCompanies(array('layout' => '1', 
                        'searchmode' => 'Sany','searchvalue' => "",
                        'limit' => '99999', 'sortfield' => '', 'sortdir' => 'ASC','special' => 'OR'));
         
         $companies=array();
      
         foreach ($comps->children() as $ch) {
            $compId=$ch->id;
            $compName=$ch->name;
            $compWebsite=$ch->website;
            $compCountry=$ch->custom4593431;
            if (strlen($compName)>0) $companies[]=array("$compId","$compName","$compWebsite","$compCountry");
         }
      
      
       	//echo json_encode(array($companies,$contacts));
      ?>
      <script type="text/javascript" src="library_functions.js"></script>
      <script type="text/javascript">
         var companies;
         var contacts;
         var table;
         var ID=0;
         var NAME=1;
         var WEBSITE=2;
         var COUNTRY=3;
         var COMPANYLIST=4;
         
         function _compare(str1,str2) {
            //alert(str1+ " ... "+str2);
            if (str1.indexOf(str2)>=0 && str2.indexOf(str1)>=0) return true;
            else return false;
         }
         
         function _getContactList(comp) {
            var cList=new Array();
            var tempList;
            var conStr;
            var pos;
            
            for (var i=0;i<contacts.length;i++) {
               conStr=contacts[i][COMPANYLIST];
               tempList=new Array();
               while (conStr.indexOf(",")>=0) {
                  var pos=conStr.indexOf(",");
                  tempList.push(conStr.slice(0,pos));
                  conStr=conStr.slice(pos+1);
                  if (conStr.charAt(0)==" ") conStr=conStr.slice(1);
               }
               tempList.push(conStr);
               for (var j=0;j<tempList.length;j++) {
                  if (_compare(companies[comp][NAME],tempList[j])) {
                     cList.push(i);
                     //alert(companies[comp][NAME]+" => "+contacts[i][COMPANYLIST]);
                  }
               }
            }
            return cList;
         }
      </script>
      <style type="text/css">
         body { padding:0 20px 20px 20px; font-family:arial,sans-serif; font-size:100%; }
         h1 { font-size:2.5em; }
         h2 { font-size:1.25em; }
         h3 { font-size:1.0em; }
         p { font-size:0.75em; }
         .bold { font-weight:bold; }
         .italic { font-style:italic; }
         .shaded { background:#dddddd; }
         .coloured { color:green; }
         
         table { border-collapse:collapse; font-size:0.875em; padding:0; margin:0; width:100%; }
         td { text-align:left; vertical-align:top; border-left:1px solid black; border-right:1px solid black; padding:5px; cursor:default; }
         th { text-align:left; vertical-align:bottom; border-left:1px solid black; border-right:1px solid black; padding:5px; cursor:default; }
      </style>
   </head>
   <body>
      <p id="infobox">Loading...</p>
      <div id="container"></div>
      <script type="text/javascript">
         var homeless,isEntry;
         var conHomeless;
         companies = <?php echo json_encode($companies); ?>;
         contacts = <?php echo json_encode($contacts); ?>;
         table=new myTable("compTable");
         table.newRow();
         table.addSimpleHeader("Company");
         table.addSimpleHeader("Website");
         table.addSimpleHeader("Country");
         table.addSimpleHeader("Contact","conid","coloured");
         table.addSimpleHeader("Website","siteid","coloured");
         table.addSimpleHeader("Country","countryid","coloured");
         var shaded=false;
         for (var i=0;i<companies.length;i++) {
            _write("infobox","Loading..."+companies[i][NAME]);
            homeless=false;
            if (companies[i][COUNTRY]=="" || companies[i][COUNTRY]=="N/A" || companies[i][COUNTRY]=="NA") homeless=true;
            if (companies[i][WEBSITE].length<5 || homeless) {
               var conList=_getContactList(i);
               shaded=!shaded;
               isEntry=false;
               for (var j=0;j<conList.length;j++) {
                  var cont=conList[j];
                  conHomeless=false;
                  if (contacts[cont][COUNTRY]=="" || contacts[cont][COUNTRY]=="N/A" || contacts[cont][COUNTRY]=="NA") conHomeless=true;
                  if ((homeless && !conHomeless) || (companies[i][WEBSITE].length<5 && contacts[cont][WEBSITE].length>4)) {
                     isEntry=true;
                     if (shaded) table.newRow("row"+i+"_"+j,"shaded");
                     else table.newRow("row"+i+"_"+j);
                     table.addCell(companies[i][NAME]);
                     table.addCell(companies[i][WEBSITE]);
                     table.addCell(companies[i][COUNTRY]);
                     table.addCell(contacts[cont][NAME],"name"+i+"_"+j,"coloured");
                     table.addCell(contacts[cont][WEBSITE],"site"+i+"_"+j,"coloured");
                     table.addCell(contacts[cont][COUNTRY],"cnty"+i+"_"+j,"coloured");
                  }
               }
               if (!isEntry) shaded=!shaded;
            } 
         }
         table.addRow();
         _add("container",table.table);
      </script>
   </body>
</html>    