/**
 * 
 * Updatesandmessages.js,  The Bitrock Network Service PHP API
 *  
 * Provides an ajax interface to the client api.
 *
 * @author BitRock www.BitRock.com
 *
 */

// Stores the id of the container that will be modified
var UAM_label;  

// Boolean variable that generate
var UAM_processingURL;

// Stores the request handler
var UAM_request;

// Default Id 
var UAM_id_interface    = 'UpdatesAndMessagesBody';

// PHP target for the requests
var UAM_target          = '/plugins/updatesandmessages/GenView.php';

// Operation variables
var UAM_operation_var       = 'update_client_do';
var UAM_id_var              = 'update_client_id';
var UAM_selection_var       = 'update_client_selection';
var UAM_selection_operation = 'update_client_do_selection';

// Message shown while processing a request
var UAM_charging_message     = '<div class="processing_message"> Processing ... </div>';


/**
 * Obtains the list of notifications and shows it on the specific tag
 */
function UAM_show_list(option){
    var params = option? UAM_operation_var+'='+option : null;
    UAM_url_request(UAM_target, UAM_id_interface, "GET",params);
    return false;
}

/**
 * Obtains one message and shows it in the
 */
function UAM_show_message(id){
    UAM_url_request(UAM_target, UAM_id_interface, "GET", UAM_id_var + '=' + id);
    return false;
}

/*
 * Mark a message as archived
 */
function UAM_archive_message(id){
    UAM_url_request(UAM_target, UAM_id_interface, "GET", UAM_operation_var+'=archive&'+UAM_id_var+'='+id);
    return false;
}

/**
 * Mark message as not archived
 */
function UAM_not_archive_message(id){
    UAM_url_request(UAM_target, UAM_id_interface, "GET", UAM_operation_var+'=not_archive&'+UAM_id_var+'='+id);
    return false;
}


/**
 * Performs an operation over the selection of messages and returns the result
 */
function UAM_process_selection(option){
    var checkbox = document.getElementsByName(UAM_selection_var+'[]');
    var params = UAM_selection_operation+'='+option;

    for (var i=0;i < checkbox.length;i++){
        if (checkbox[i].checked)
            params += '&'+UAM_selection_var+'[]='+checkbox[i].value;
    }
    UAM_url_request(UAM_target, UAM_id_interface, "POST", params);
    return false;  
}


/**
 * Submits the selection of messages changing the operation to perform
 */
function UAM_submit_list(option){
    document.getElementById(UAM_selection_operation).value= option;
    document.UpdateClientForm.submit();
    return false;    
}


/**
 *
 * Function that performs a request to a specific url
 *
 * @url            target of the request
 * @target_label   id of destination element
 * @method         http mehod used
 * @params         an urlencoded list of parameters
 *
 */
function UAM_url_request(url, target_label, method, params){
    if (url == ''|| target_label == '') return;
    if (method == '' || method == null) method = "GET";
    if (params == '')                   params = null;

    UAM_label = target_label;

    if (window.XMLHttpRequest) {
        UAM_request = new XMLHttpRequest();
    }else if (window.ActiveXObject){
        UAM_request = new ActiveXObject("Microsoft.XMLHTTP");
        isIE = true;
    }

    if (UAM_request){
        UAM_request.onreadystatechange = UAM_processReqChange;
        if (method=="GET" && params != null)
            url += '?'+params;
        UAM_request.open(method, url, true);

        // Send the proper header information along with the post request    
        if (method=="POST" && params != null){
            UAM_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            UAM_request.setRequestHeader("Content-length", params.length);
            UAM_request.setRequestHeader("Connection", "close");
        }
        UAM_request.send(params);            
    }
}


/**
 * Called in each event onreadystatechange of object UAM_request
 */
function UAM_processReqChange(){
  UAM_procesandoURL = true;
  var content = document.getElementById(UAM_label);
  if (content != null){
    if(UAM_request.readyState == 4){
      // Substitution of the response text in the container object
      content.innerHTML = UAM_request.responseText;
      processingURL = false;
    }else{
      // Message showed while receiving the response
      content.innerHTML = UAM_charging_message; 
    }
  }
}


