function updateModuleWithServerData(module, request) {
   module.showLoader(0);
   var url = "./index.php?option=com_resosco2&task=task1";
   url += "&req[" + module.id + "][module_id]=" + encodeURIComponent(module.id);
   url += "&req[" + module.id + "][type]=" + encodeURIComponent(request.type);
   url += "&req[" + module.id + "][contest]=" + encodeURIComponent(request.contest);
   if (null != request.t1) {
      url += "&req[" + module.id + "][t1]=" + encodeURIComponent(request.t1);
   }

   var options = {method: 'post'};
   options.onComplete = function(jsonObj) {
      if (null != jsonObj.error) {
         module.raiseMessage(jsonObj.error);
         return false;
      }
      
         if (null != jsonObj['modules']) {
            var modules = jsonObj['modules'];
            $each(modules, function(module, id) {
      try {
               var properties = {};
               if ('standings' == module.request.type) {
                  properties['contest'] = module.request.contest;
               }
               if ('matches' == module.request.type) {
                  properties['contest'] = module.request.contest;
                  properties['t1'] = module.request.t1;
                  properties['t2'] = module.request.t2;
                  
                  var d1 = new Date();
                  d1.setTime(properties['t1'] * 1000);
                  var d2 = new Date();
                  d2.setTime(properties['t2'] * 1000);
               }
               if (null == RESOSCO_modules[id]) {
                  return;
               }
               var newModule = RESOSCO_modules[id];
               newModule.setModuleData(module.request.type, properties, module.html);
               newModule.hideLoader();
               if (null != jsonObj['data']) {
                  updateData(jsonObj['data']);
               }
      } catch(ex) {
//         console.log("1 " + ex.message);
      }
            });
         }
   }

   var json = new Json.Remote(url, options);
   json.send();
}

function initUpdateModules(modules) {
   if (!(modules.length > 0)) {
      return false;
   }
   var requestItems = new Array();
   for (var i = 0; i < modules.length; i++) {
      var module = modules[i];
      if (0 == module.status) {
         module.showLoader(0);
      }
      if (-1 == module.status) {
         module.showLoader(1);
      }

      var requestItem = {module_id: module.id, contest: module.contest};
      var currentModulesItem = module.getCurrentItem();
      requestItem.type = currentModulesItem.type;
      if ('match' == currentModulesItem.type) {
         requestItem['match_id'] = new Array();
         if (null != currentModulesItem.properties && null != currentModulesItem.properties['match_id']) {
            requestItem['match_id'] = currentModulesItem.properties['match_id'];
         }
      }
      requestItems.push(requestItem);
   }

   var url = "./index.php?option=com_resosco2&task=task1";
   for (var i = 0; i < requestItems.length; i++) {
      var requestItem = requestItems[i];
      url += "&req[" + requestItem['module_id'] + "][module_id]=" + encodeURIComponent(requestItem['module_id']);
      url += "&req[" + requestItem['module_id'] + "][type]=" + encodeURIComponent(requestItem['type']);
      url += "&req[" + requestItem['module_id'] + "][contest]=" + encodeURIComponent(requestItem['contest']);
      if ('match' == requestItem['type'] && null != requestItem['match_id']) {
         $each(requestItem['match_id'], function(matchId){
            url += "&req[" + requestItem['module_id'] + "][match_id][]=" + encodeURIComponent(matchId);
         });
      }
   }

   var options = {method: 'post'};
   options.onComplete = function(jsonObj) {
      try {
         if (null != jsonObj.error) {
            $each(RESOSCO_modules, function(module){
               module.raiseMessage(jsonObj.error);
            });
            return false;
         }
         
         if (null != jsonObj['modules']) {
            var modules = jsonObj['modules'];
            $each(modules, function(module, id) {
               if (!module) {
                  return false;
               }
               var properties = {};
               if ('standings' == module.request.type) {
                  properties['contest'] = module.request.contest;
               }
               if ('matches' == module.request.type) {
                  properties['contest'] = module.request.contest;
                  properties['t1'] = module.request.t1;
                  properties['t2'] = module.request.t2;
               }
               var newModule = RESOSCO_module.getInstance(id);
               newModule.setModuleData(module.request.type, properties, module.html);
               newModule.hideLoader();
               if (null != jsonObj['data']) {
                  updateData(jsonObj['data']);
               }
            });
         }
      } catch(ex) {
//         console.log("2 " + ex.message);
      }
   }

   var json = new Json.Remote(url, options);
   json.send();
}

function updateData(data) {
   if (null != data['matches']) {
      $each(data['matches'], function(item){
         if (null == item['match_id']) {
            return;
         }
         RESOSCO_matches[item['match_id']] = new RESOSCO_Match(item);
      });
   }
}

function updateMatches(items) {
   if (!items.length) {
      return false;
   }
   
   var url = "./index.php?option=com_resosco2&task=task2";
   
   $each(items, function(item){
      if (null == item['id']) {
         return false;
      }
      url += "&req[]=" + encodeURIComponent(item['id']);
   })
   
//   alert(url);
   
   var options = {method: 'post'};
   options.onComplete = function(jsonObj) {
         if (null == jsonObj['data']['matches'] || !jsonObj['data']['matches'].length) {
            return false;
         }
         $each(jsonObj['data']['matches'], function(item){
            if (null == item['match_id']) {
               return false;
            }
            var match = RESOSCO_Match.getInstance(item['match_id']);
            if (null == match) {
               return false;
            }
            match.updateMatch(item);
         });
         
   }

   var json = new Json.Remote(url, options);
   json.send();
}
