/*
*    ajaxRequest - Calls our modules in an ajax fashion
*    @param    string    runModule
*    @param    string    runClass
*    @param    string    runEvent
*    @param    mixed     data
*    @access   public
*    @return   mixed
*
*/
function ajaxRequest(runModule, runClass, runEvent, jsonData){
     
     if(runModule == "undefined" || runEvent == "undefined")
          return;
          
     if(runClass.empty()){
          runClass = runModule;
     }

     if(typeof jsonData == 'string'){
          if(!jsonData.empty()){
               console.log("Data is: "+ jsonData);
               jsonData = JSON.parse(jsonData);
          }
     }
     
     var results = null;
     
     $.ajax({
          type: "POST",
          async: false,
          cache: false,
          url: "index.php",
          data: "module=" + runModule + "&class=" + runClass + "&event=" + runEvent + "&data=" + JSON.stringify(jsonData),
          dataType: "json",
          success: function(jsonObj){
               results = jsonObj;
               
               $("input[type=checkbox], input[type=radio]").css("border", "0px");
          },
          error: function (XMLHttpRequest, textStatus, errorThrown) {
               alert("Sorry, but we are experiencing some technical difficulties");
               results = null;
          }
     });
          
     return results; 
}

/*
*    loadPage - Submits a page request to the controller which 
*               will be loaded in #content
*    @param    string    runModule
*    @param    string    runClass
*    @param    string    runEvent
*    @param    mixed     data
*    @access   public
*    @return   false
*/
function loadShadowBox(runModule, runClass, runEvent, data){
	var curReg = registry.getInstance();
     
	if(runModule == "undefined" || runEvent == "undefined")
          return;
          
     if(data && data.constructor != Array){
          
          if(typeof data == "object"){
               data = JSON.stringify(data);
          }
          
          data = new Array(new String(data));
     }
     
     if(runClass.empty()){
          runClass = runModule;
     }
     
     $("#shadow_box_profile .sb_right").empty();
     $("#shadow_box_profile .sb_right").html('<img src="images/contentLoader.gif" class="loader">');
      
     var workspace_width = $("#workspace").width()+20;
     var workspace_height = $("#workspace").height()-61;
     
     $("#shadow_box").load("index.php", 
                         {'module': runModule, 
                          'class': runClass, 
                          'event': runEvent, 
                          'data': data.join("::")}, 
                          function(responseText, textStatus, XMLHttpRequest){ 

                                   $("#shadow_box_profile").css("left", ((workspace_width-650)/2)+"px");
                                   $("#shadow_box_profile").css("max-height", (workspace_height+70)-(50*2));
                                   $("#shadow_box_profile .sb_right").css("height", (workspace_height+70)-(50*2)-50);
                                   $("#shadow_box_profile .sb_right table").css("width", "95%");
                                   $("#shadow_box_profile .descr_table").css("width","100%");
                                   
                                   if(textStatus == "error"){
                                        alert("The action you have taken has caused an error. "+ 
                                              "Please try the action again");
                                        
                                        return;
                                        
                                   }else if(textStatus == "timeout"){
                                        alert("The system has timed out because your current " + 
                                              "request has taken longer than 30 seconds to " + 
                                              "complete. Please go back and try your request again.");
                                        
                                        return;
                                   }
                                   
                                   $("input[type=checkbox], input[type=radio]").css("border", "0px");
                                   
                                   // Track the AJAX page request with Google Analytics
                                   if (pageTracker !== false) pageTracker._trackPageview("/ajax/"+runModule+"-"+runClass+"-"+runEvent);
                          });
                         
     
     $("#shadow_box").css("display", "block");
	$("#shadow_box_background").css("display", "block");
     $("#shadow_box_img").css("display", "block");
     $("#shadow_box_background").css("height", workspace_height+"px");
	$("div#box_full .body select").css("visibility", "hidden");
     $("#match_box_arrow img").attr("src", "images/match_box_arrow_shadow.gif");
     return false; 
}

/*
*    loadExploreResults - Submits a page request to the controller which 
*                  will be loaded in #subContent
*    @param    string    runModule
*    @param    string    runClass
*    @param    string    runEvent
*    @param    mixed     data
*    @access   public
*    @return   false
*/
function loadExploreResults(runModule, runClass, runEvent, data){
     
     if(runModule == "undefined" || runEvent == "undefined")
          return;
          
     if(data != "undefined" && data.constructor != Array){
          data = new Array(new String(data));
     }

     if(runClass.empty()){
          runClass = runModule;
     }
     
     /* Set the type out for all ajax request */
     $.ajaxSetup({timeout: 30000});
     
     $("#box_results").empty();
       
     $("#box_results").load("index.php", 
                               {'module': runModule, 
                                'class': runClass, 
                                'event': runEvent, 
                                'data': data.join("::")}, 
                              function(responseText, textStatus, XMLHttpRequest){ 
                              
                                   if(textStatus == "error"){
                                        alert("The action you have taken has caused an error. "+ 
                                              "Please try the action again.");
                                        
                                        return;
                                   }else if(textStatus == "timeout"){
                                        alert("The system has timed out because your current " + 
                                              "request has taken longer than 30 seconds to " + 
                                              "complete. Please go back and try your request again.");
                                        
                                        return;
                                   }
                                   
                                   $("input[type=checkbox], input[type=radio]").css("border", "0px");
                              });

     return false; 
}