
function leadingZero(nr)
{
	if (nr < 10) nr = "0" + nr;
	return nr;
}
function load()
{
    
    dashcode.setupParts();
    AbrirDB();
    CargarDB();
    document.getElementById("list").object.reloadData();
    CargarDB();
    }

function AbrirDB()
{
myDB = openDatabase("Blood","1.0","Blood Pressure",200000);
myDB.transaction(function(tx) {
        tx.executeSql("SELECT * FROM Tensiones order by fecha desc", [], function(tx,result) {
            for (var i = 0; i < result.rows.length; ++i) {
                var row = result.rows.item(i);
                listDataSource._rowData[i]=row['fecha']+"-Alta: "+row['alta']+"-Baja: "+row['baja'];}        }, function(tx, error) {
            tx.executeSql("CREATE TABLE Tensiones (id REAL UNIQUE, fecha TEXT,alta TEXT, baja TEXT)", [], function(result) { 
                
            });
        });
    });
    CargarDB();
    document.getElementById("list").object.reloadData();
}

function CargarDB()
{
myDB.transaction(function(tx2) {
        tx2.executeSql("SELECT id, fecha,alta,baja FROM Tensiones order by Fecha desc", [], function(tx2, result) {
            for (var i = 0; i < result.rows.length; ++i) {
                var row = result.rows.item(i);
                listDataSource._rowData[i]=row['fecha']+"-Alta: "+row['alta']+"-Baja: "+row['baja'];
                            }
                  }, function(tx2, error) {
            alert('Failed to retrieve notes from database - ' + error.message);
            return;
        });
    });
document.getElementById("list").object.reloadData();
}

var listDataSource = {
	_rowData: [],
	numberOfRows: function() {
		return this._rowData.length;
	},
	prepareRow: function(rowElement, rowIndex, templateElements) {
		if (templateElements.label) {
			templateElements.label.innerText = this._rowData[rowIndex];
		}

		rowElement.onclick = function(event) {
           var browser = document.getElementById('browser').object; // Replace with id of Browser
           browser.goForward('view4', 'Toma de tensión');  // Replace with id of next level's view (or the
            var b = listDataSource._rowData[rowIndex];
            var temp = new Array();
            temp = b.split("-");
            document.getElementById("txtFechaT").innerHTML = "Fecha: " + temp[0];
            document.getElementById("txtSistolica").innerHTML = temp[1];
            document.getElementById("txtDiastolica").innerHTML = temp[2];
            
            
		};
	}
};


function takeYear(theDate)
{
	x = theDate.getYear();
	var y = x % 100;
	y += (y < 38) ? 2000 : 1900;
	return y;
}


function alta(event)
{
var browser = document.getElementById('browser').object; // Replace with id of Browser
browser.goForward('view3', 'Alta de Toma de tensión');  // Replace with id of next level's view (or the 
var today = new Date();
document.getElementById("txtFecha").value = takeYear(today) + "/" + leadingZero(today.getMonth()+1) + "/" + leadingZero(today.getDate()); }


function grabaAlta(event)
{
fecha = document.getElementById("txtFecha").value;
alta = document.getElementById("txtAlta").value;
baja = document.getElementById("txtBaja").value;
myDB.transaction(function (tx) 
        {
            tx.executeSql("INSERT INTO Tensiones (fecha,alta,baja) VALUES ('" + fecha + "','" + alta + "','" + baja + "')");
        }); 
CargarDB();
var browser = document.getElementById('browser').object; // Replace with id of Browser
browser.goBack('view1', '');  // Replace with id of next level's view (or the view itself and the head
CargarDB(); 
}


function AcercaDe(event)
{
   
var browser = document.getElementById('browser').object; // Replace with id of Browser
browser.goForward('view2', 'Acerca de...');  // Replace with id of next level's view (or the view
}


function Elimina(event)
{
a = document.getElementById("txtFechaT").innerHTML;
temp = a.split(' ');
fecha = temp[1];
a = document.getElementById("txtSistolica").innerHTML;
temp = a.split(' ');
alta = temp[1];
a = document.getElementById("txtDiastolica").innerHTML;
temp = a.split(' ');
baja = temp[1];

myDB.transaction(function (tx) 
        {
            tx.executeSql("DELETE FROM Tensiones WHERE fecha ='" + fecha + "' and alta ='" + alta + "' and baja='" + baja + "'");
           
        }); 
CargarDB();

var browser = document.getElementById('browser').object; // Replace with id of Browser
browser.goBack('view1', '');  

CargarDB(); 

}
