//****************************************************************************
// RULES
//****************************************************************************

function openRuleCondition(id) {
    var dialog = Dialog.load("/dialogs/rule-condition.xsp", "rule-condition-dialog");
    dialog.id = id;
    $("form-rule-condition").setEnabled(true, true);
    dialog.open(null, function(cond) { if (cond != null) refresh(); });
}


//****************************************************************************
// TEMPLATE DESIGNER
//****************************************************************************

// Google Maps API Key
// ABQIAAAAiXSrPhWEIi-LuKVN6DuWyRSks_LVMaMPW0kM0FbKNlDF2GxrkRS6fOAqcvlWwe7eWjdm-m9KVJPJLg

/*
var letters = "abcdefghijkmnopqrstuvwxyz";
var typeNames = [
    "none",     // 0
    "text",     // 1
    "number",   // 2
    "date",     // 3
    "datetime", // 4
    "time",     // 5
    "checkbox", // 6
    "select",   // 7
    "photo",    // 8
    "audio",    // 9
    "phone",    // 10
    "email",    // 11
    "currency", // 12
    "sub",      // 13
    "integer",  // 14
    "decimal",  // 15
    "fraction", // 16
    "location"  // 17
];

var QuestionType = {
     NONE:     0,
     TEXT:     1,
     NUMBER:   2,
     DATE:     3,
     DATETIME: 4,
     TIME:     5,
     CHECKBOX: 6,
     SELECT:   7,
     PHOTO:    8,
     AUDIO:    9,
     PHONE:    10,
     EMAIL:    11,
     CURRENCY: 12,
     SUB:      13,
     INTEGER:  14,
     DECIMAL:  15,
     FRACTION: 16,
     LOCATION: 17
}

function filterEntries(key, value) {
    if (value == null) return null;
    if (key == "type") return typeNames[value];
    if (key == "options") return value.map(function(opt) { return opt.name; }).join("<br/>");
    if (key == "multiple" && this["type"] != QuestionType.SELECT) return "";
    return value;
}
*/

function setTemplate(template) {
    // console.log("Template: "+template);
    setParameter("template", template);
    window.location.search = getQuery();
}

function setTemplateFullName(templateFullName) {
    setParameter("templateFullName", templateFullName);
    window.location.search = getQuery();
}

function setUser(user) {
    setParameter("user", user);
    window.location.search = getQuery();
}

function paintLocations(map, locs) {
    // Build paths
    var paths = new Object();
    var point, bounds = null;
    for (var i = 1; i < locs.length; i++) {
        var loc = locs[i];
        var user = loc[0];
        var lat = loc[3], lng = loc[4];
        if (paths[user] == null) paths[user] = new Array();
        paths[user].push(point = new GLatLng(lat, lng));
        
        // Bounds
        if (bounds == null) bounds = new GLatLngBounds(point,point);
        else bounds.extend(point);
    }

    // Init map
    map.init(bounds.getCenter());
    var gm = map.googleMap;
    
    // Draw markers and polylines
    var c = 0;
    for (user in paths) {
        var path = paths[user];
        var markerOptions = { icon: Map.getSmallIcon(c) };
        var polyline = new GPolyline(path, Map.COLORS[c++], 10);
        gm.addOverlay(polyline);
        for (var i = 1; i < path.length; i++) gm.addOverlay(new GMarker(path[i], markerOptions));
    }
}

function displayChart(chart, rpcCall) {
    chart = $(chart);
    if (chart._initialized) return;
    chart._initialized = true;
    
    // Initialize RPC call
    if (isString(rpcCall)) {
    	switch (rpcCall) {
    		case "year":  rpcCall = function(fn) { RPC.API.messageYearCount(-1, fn); }; break;
    		case "month": rpcCall = function(fn) { RPC.API.messageMonthCount(-1, -1, fn); }; break;
    		case "day":   rpcCall = function(fn) { RPC.API.messageDayCount(-1, -1, -1, fn); }; break;
    	}
    }

    // Make call
    rpcCall(function(labelsAndValues) {
    	chart.setHorLabels(labelsAndValues[0]);
    	chart.setDataSets([{ data: labelsAndValues[1] }], true);     	
    });
}

