﻿// JScript File

function GetLocationDetails(lon, lat) {
     
    $('#overlaytext').html("<img src='grfx/loader.gif'>");   
     
    $.ajax({
          type: "POST",
          url: "default.aspx/GetLocationDetails",
          data: "{'longitude':'" + lon + "', 'latitude':'" + lat + "'}",
          contentType: "application/json; charset=utf-8",
          dataType: "json",
    
            success: function(msg) {
                var data = eval(msg.d);   
                BuildDetailsTable(data);
            },
            failure: function(msg) {
               $('#overlaytext').html("A problem occured while retrieving office data.\nNo data could be displayed.");
            }
     
     });     
        
}

function LoadOfficeDetails(id) {
     $.ajax({
          type: "POST",
          url: "default.aspx/GetOfficeDetails",
          data: "{'id':'" + id + "'}",
          contentType: "application/json; charset=utf-8",
          dataType: "json",
         
            success: function(msg) {
               var data = eval(msg.d);   
               return data;
            },
            failure: function(msg) {
               $('#overlaytext').html("A problem occured while retrieving office data.\nNo data could be displayed.");
            }
     
     }); 
}
function GetOfficeDetails(id) {
     
    $('#overlaytext').html("<img src='grfx/loader.gif'>");   
    var data = LoadOfficeDetails(id);
    if (data != null) BuildDetailsTable(data); 
        
        
}

var officedata;
var pageSize = 15;
function GetOfficeListByCountry(country) {
     
    $('#officelist').html("<img src='grfx/loader_bar.gif'>");         
     
    $.ajax({
          type: "POST",
          url: "Default.aspx/GetOfficeListByCountry",
          data: "{'country':'" + country + "'}",
          contentType: "application/json; charset=utf-8",
          dataType: "json",
            
            success: function(msg) {
                officedata = eval(msg.d);   
               BuildOfficeTable(0);
            },
            failure: function(msg) {
              $('#offices').html("A problem occured while retrieving office data.\nNo data could be displayed.");
            }
     
     });   
}

function GetOfficeListByArea(lonSW, latSW, lonNE, latNE) {
     
    
    $('#officelist').html("<img src='grfx/loader_bar.gif'>");         
     
    $.ajax({
          type: "POST",
          url: "Default.aspx/GetOfficeListByArea",
          data: "{'lonSW':'" + lonSW + "', 'latSW':'" + latSW + "', 'lonNE':'" + lonNE + "', 'latNE':'" + latNE + "'}",
          contentType: "application/json; charset=utf-8",
          dataType: "json",
            
            success: function(msg) {
                officedata = eval(msg.d);   
                BuildOfficeTable(0);
            },
            failure: function(msg) {
              $('#offices').html("A problem occured while retrieving office data.\nNo data could be displayed.");
            }
     
     });         
}

function BuildDetailsTable(data) {
     
    var cText  = "<div class='blockTitle' >office details</div>";
   
    for (var n = 0; n < data.length; n++) {
        
        cText += "<div class='block'><strong>" + data[n].Company + "</strong><br/>";
        cText += data[n].Address + "<br/>";
		if (data[n].AddressSuffix != '') cText += data[n].AddressSuffix + "<br/>";
        cText += data[n].City + "<br/>";
        cText += data[n].Zip + "<br/>";
        cText += data[n].Country + "<br/><br/>";
        cText += "<img src='grfx/icon_phone.gif' alt='Telephone' align='absmiddle'>" + data[n].Telephone + "<br/>";
        cText += "<img src='grfx/icon_fax.gif' alt='Fax'>" + data[n].Fax + "<br/>";
        if (data[n].Url != '') { cText += "<img src='grfx/icon_url.gif' alt='Homepage'><a href=\"" + checkUrl(data[n].Url) + "\">" + data[n].Url + "</a></div>"; }
    
    }
  
    $('#overlaytext').html(cText);
    $('#overlay').css('display','block');
     
}

function checkUrl(url) {
     if (url.indexOf('http') < 0) url = "http://" + url;
     return url;
}

function BuildOfficeTable(pageindex) {
      
  pageIndex = pageindex;
  var table = '<table class="offices" cellspacing="0">\n<thead><tr><th width="20">&nbsp;</th><th>Company</th><th width="100">City</th><th width="100">Country</th></thead>\n<tbody>\n';
  if (officedata != null) {
    
     var maxlength = pageSize*(pageIndex+1);
     if (maxlength > officedata.length) maxlength = officedata.length;
        
     var n = 0;
     for (var i = (pageSize*pageIndex); i < maxlength; i++){
         
         if (officedata[i] != null) {

              var row = '<tr>';
              row += '<td><img src="grfx/icon_showonmap.gif" onclick="showOffice(\'' + officedata[i].Longitude + '\', \'' + officedata[i].Latitude + '\', ' + officedata[i].Id + ');" class="link" /></td>';
              row += '<td class="company">' + officedata[i].Company + '</td>';
              row += '<td class="city">' + officedata[i].City + '</td>';
              row += '<td class="country">' + officedata[i].Country + '</td>';
              row += '</tr>\n';
           
              table += row;
         }
         
     }
     
      
     table += '</tbody></table>';
     var pager = BuildOfficePager();
     table = pager + table + pager; 
     
     
   }
   
   $('#officelist').html(table);

}

function BuildOfficePager() {

     var pager = '';
     if (officedata.length > pageSize) {
          
          pager = "<table class='pager'><tr>";
          var pages = Math.floor((officedata.length-1)/pageSize);
          for (var p = 0; p <= pages; p++) {
               if (p == pageIndex) {
                    pager += "<td><b>" + (p+1) + "</b></td>";
               } else {
                    pager += "<td><span onClick='BuildOfficeTable(" + p + ")'>" + (p+1) + "</span></td>";
               }
          }
          pager += "</tr></table>";
          
     }

     return pager;
}