
// ---------------------------------------------------------------------------------------------------------//
// SMART LIST V1
// Copyright Tekar S.R.L., All rights reserved
// www.tekar.net


//USAGE EXAMPLE

/*

var List = new SmartList('smart_list.php');

List.Query('product', Array('id','name'), '91', Array('id > 2'), testCallBack, 'test');

function testCallBack(request){
    var aText = List.GetResult(request);
    alert('respuesta');
    alert(aText[0]);
    alert(aText[1][0].id + ' ' + aText[1][0].name);

    //alert(aText[1][1][1]);
}
*/

// ---------------------------------------------------------------------------------------------------------//
// SMART LIST

// ---------------------------------------------------------------------------------------------------------//
// SL_Query

/*
Hace el pedido a la lista.
Parameters:
    obj, El objeto: 'product'
    properties, Un array de propiedades del objeto: Array('id','left(name,30) as name','code','photo1')
    keyword, keyword para buscar: 'algo'
    conditions, Array de condiciones: Array('name like "%algo%"','status_id = 1')
Ej:
SmartSendRequest('product', Array('id','left(name,30) as name','code','photo1'), keyword, conditions)
*/

function SL_Query(obj, properties, keyword, conditions, CallBackFunction, MessageNum)
{

    var params = 'k=' + keyword + '&o=' + obj + '&p=' + properties.toJSON() + '&m=' + MessageNum + '&c=' + conditions.toJSON(); // + '&f=' + String(format);

    //alert(params)
    if(this.Debug == true){
        trace('<b>SENT</b><br>' + params);
    }

    var myAjax = new Ajax.Request(
        this.URL,
        {
            method: 'POST',
            parameters: params,
            onComplete: CallBackFunction
        });
}

function SL_GetResult(request)
{

    var sResponseText = String(request.responseText);

    //alert(sResponseText);
    if(this.Debug == true){
        trace('<b>RECEIVED</b><br>' + sResponseText);
    }
    var aResponse = sResponseText.split('|-|');

    aResponse[1] = aResponse[1].evalJSON();

    return(aResponse);
}

function SL_Trace(text){
    if(this.Debug){
        trace(text);
    }
}

function SmartList(QueryURL){
    this.URL        = QueryURL;
    this.Debug      = false;
    this.Query      = SL_Query;
    this.GetResult  = SL_GetResult;
    this.Trace      = SL_Trace;
}

// ---------------------------------------------------------------------------------------------------------//
// DEBUG FUNCTIONS

function trace(text){
    document.getElementById('smart_list_debug').innerHTML = '<hr>' + text + document.getElementById('smart_list_debug').innerHTML;
}

function var_dump(obj) {
    if(typeof obj == "object") {
        return "Type: "+typeof(obj)+((obj.constructor) ? "\nConstructor: "+obj.constructor : "")+"\nValue: " + obj;
    } else {
        return "Type: "+typeof(obj)+"\nValue: "+obj;
    }
}

function Now(){
    var currentTime = new Date()
    var hours = currentTime.getHours()
    var minutes = currentTime.getMinutes()
    var seconds = currentTime.getSeconds()

    if (minutes < 10){
        minutes = "0" + minutes;
    }
    if (seconds < 10){
        seconds = "0" + seconds;
    }

    var buffer = hours + ":" + minutes + ":" + seconds + " ";
    if(hours > 11){
        buffer = buffer + "PM";
    } else {
        buffer = buffer + "AM";
    }
    return(buffer);
}


