/*
    JavaScript para integracion de AJAX 
    Desarrollado por: Mario Serrano Leones
    Licencia GPL 
 */


var ajax;
var _divId;
var _formId;
var _autoHide;
var _delay;
var _lastRowId=null;
var _showLoading;
var _eval;

function funcionCallback() {
    
    
    if( ajax.readyState == 4 ) {
        
        if( ajax.status == 200 ) {            
            document.getElementById(_divId).innerHTML = processResponse(ajax.responseText);                         
        }else if(ajax.status == 400){
            document.getElementById(_divId).innerHTML = "No se encuentra recurso..";
            
        }
    }else{
        if(_showLoading == null){
            document.getElementById(_divId).innerHTML = "<div class='LoadingMessage'><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><center> Cargando...</center></div>";	
        }
        
    }
}

function createXMLHttpRequest(){
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        ajax = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
        try {
            ajax = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                ajax = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }
    
    if (!ajax) {
        alert("Su explorador no soporta AJAX!!! Utilice FireFox o InternetExporer 6");
        return false;
    }
    
}

function setAutohide(autohide,delay){    
    _autohide = autohide
    if(_autohide== "undefined"){
        _autohide = false;
    }
    
    _delay = delay;
    if(_delay == "undefined"){
        _delay = 1000;
    }    
}


function postTask(page,divId){
    _eval = null;   
    goToPage(page,divId);
    _eval = false;
    
}

function goToPage(page,divId,autohide,delay,rowId,showLoading) {    
    setAutohide(autohide,delay);
    _divId = divId;
    _formId = null; 
    _showLoading = showLoading;
    if(_showLoading == "undefined"){
        _showLoading = true;
    }
    
    
    ajax = false; 
    
    
    processRowId(rowId);
    
    createXMLHttpRequest();    
    ajax.onreadystatechange = funcionCallback;
    ajax.open('GET', page, true);
    ajax.send(null);    
    
    
}

function sendPOSTRequest(formId,divId,autohide,delay) {
    setAutohide(autohide,delay);
    _divId = divId;
    _formId = formId;
    
    var action = document.getElementById(formId).action;
    
    ajax=false;
    
    createXMLHttpRequest();
    
    parameters = getFormParameters(formId);    
    ajax.onreadystatechange = funcionCallback;  
    ajax.open("POST",action,true);
    ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ajax.setRequestHeader("Content-length", parameters.length);
    ajax.setRequestHeader("Connection", "close");
    ajax.send(parameters);    
    deselectRowId();
}

//-------Utility functions---------//


function getFormParameters(formId){
    
    var formvars='';
    
    var childElements=document.getElementById(formId);
    
    for(var i=0;i<childElements.length;i++){
        formvars = formvars + childElements[i].name+"="+encodeURI(childElements[i].value)+"&";
    }
    
    return formvars;    
}

function clearDiv(divId){
    document.getElementById(divId).innerHTML = "";
    deselectRowId();
}

function processRowId(id){
    if(id != "undefined"){
        try{
            deselectRowId();
            document.getElementById(id).style.backgroundColor = "#EAFFCC";
            _lastRowId = id;
        }catch(e){}
    }
    
}

function deselectRowId(){
    try{
        /*if(_lastRowId!=null){
            document.getElementById(_lastRowId).style.backgroundColor = "#FFFFFF";            
        } */
        var table = document.getElementById("dataTable");
        for(i=0; i<table.rows.length;i++){
            table.rows[i].style.backgroundColor = "#FFFFFF";    
        }
        
    }catch(e){
        //alert(e);
    }
}

function deleteRowId(){
    
    if(_lastRowId!=null){        
        rowIndex = document.getElementById(_lastRowId).rowIndex;
        document.getElementById("dataTable").deleteRow(rowIndex);        
    }    
}

function updateRowId(){
    
}

function resetCurrentForm(){
    try{
        document.getElementById(_formId).reset();
    }catch(e){}
}

function processResponse(responseTxt){
    try{        
        code = responseTxt.substring(0,1);     
        txt = responseTxt.substring(1,responseTxt.length);
        if(code == "@"){
            deleteRowId();
            return txt;
        }else if(code == "#"){
            updateRowId();
            return txt;
        }else if(code == "*"){
            resetCurrentForm();
            return txt;
        }else{
            return responseTxt;
        }
    }catch(e){
        //alert(e);
    }
    _lastRowId=null;
}


