// db.js

document.getElementById("loadingMessage").innerHTML = 'Loading database...';

 

/*! Initialize the systemDB global variable. */

function initDB()
{
	
	try {
		if (!window.openDatabase) {
		alert("Arr!  iAye be designed te work with the iPhone and iPod Touch only.\r\rI'm afraid yer outta luck, laddie.  Or lassie.  Whichever ye be.  I'm not picky.");
		} else {
			var shortName = 'iAye';
			var version = '1.0';
			var displayName = 'iAye database';
			var maxSize = 1048576; // in bytes.  Make it a meg.
			var myDB = openDatabase(shortName, version, displayName, maxSize);
	 
			// You should have a database instance in myDB.
	 
		}
	} catch(e) {
		// Error handling code goes here.
		if (e == INVALID_STATE_ERR) {
			// Version number mismatch.
		alert("Invalid database version.");
		} else {
		alert("Unknown error "+e+".");
		}
		return;
	}
	
	
	// alert("Database is: "+myDB);
//	resetDatabase(myDB);
	createTables(myDB);
	systemDB = myDB;

}


function resetDatabase(db) {
	db.transaction(
		function (transaction) {
		transaction.executeSql('DROP TABLE portArray;');
		transaction.executeSql('DROP TABLE playerShip;');
		transaction.executeSql('DROP TABLE crewman;');
		transaction.executeSql('DROP TABLE globals;');
		transaction.executeSql('DROP TABLE missions;');
		}
	);
	createTables(db);
}

function dbLoadGlobals(db, goToInitialize) {
	
	if (goToInitialize == null) {
		goToInitialize = false;
	}
	
	db.transaction(
		function(transaction) {
			transaction.executeSql('SELECT * FROM globals', [], function(transaction, result) {
					if (result.rows.length == 1) {
						var row = result.rows.item(0);
						window.playerName = unescape(row['playerName']);
						window.playerShipName = unescape(row['playerShipName']);
						window.currentPort = row['currentPort'];
						window.startingPort = row['currentPort'];
						window.playerCash = row['playerCash'];
						window.currentStardate = row['currentStardate'];
						window.startingRandomNumberSeed = row['randomNumberSeed'];
						continueExists = true;
						document.getElementById("continueGameDiv").style.display = "inline";
						document.getElementById("startGameDiv").style.display = "inline";
						window.startingShipyardSeed = row['shipyardSeed'];
						window.startingBarCrewmenSeed = row['barCrewmenSeed'];
						if (goToInitialize == true) {
							randomNumberSeed = startingRandomNumberSeed;
							InitializeGame();
						}
					} else {
						document.getElementById("startGameDiv").style.display = "inline";
					}
				}
			);
		}
	);
	

}

function dbLoadMainData(db)
{
	var i, j;
	db.transaction(
		function(transaction) {




			transaction.executeSql('SELECT * FROM globals', [], function(transaction, result) {
					var row = result.rows.item(0);
					window.playerName = unescape(row['playerName']);
					window.playerShipName = unescape(row['playerShipName']);
					window.currentPort = row['currentPort'];
					window.startingPort = row['currentPort'];
					window.playerCash = row['playerCash'];
					window.currentStardate = row['currentStardate'];
					continueExists = true;
				}
			);





			transaction.executeSql('SELECT * FROM portArray', [], function(transaction, result) {


					var row;


					for (i = 0; i < portArray.length; i++)
					{
						row = result.rows.item(i);
						
						portArray[i].portHostility = row['portHostility'];
						portArray[i].portFamiliarity = row['portFamiliarity'];
						

						portArray[i].portCurrentPrice[0] = Math.floor(row['currentPrice0']);
						portArray[i].portCurrentPrice[1] = Math.floor(row['currentPrice1']);
						portArray[i].portCurrentPrice[2] = Math.floor(row['currentPrice2']);
						portArray[i].portCurrentPrice[3] = Math.floor(row['currentPrice3']);
						portArray[i].portCurrentPrice[4] = Math.floor(row['currentPrice4']);
						portArray[i].portCurrentPrice[5] = Math.floor(row['currentPrice5']);
						portArray[i].portCurrentPrice[6] = Math.floor(row['currentPrice6']);
						portArray[i].portCurrentPrice[7] = Math.floor(row['currentPrice7']);
						
						portArray[i].portStock[0] = Math.floor(row['portStock0']);
						portArray[i].portStock[1] = Math.floor(row['portStock1']);
						portArray[i].portStock[2] = Math.floor(row['portStock2']);
						portArray[i].portStock[3] = Math.floor(row['portStock3']);
						portArray[i].portStock[4] = Math.floor(row['portStock4']);
						portArray[i].portStock[5] = Math.floor(row['portStock5']);
						portArray[i].portStock[6] = Math.floor(row['portStock6']);
						portArray[i].portStock[7] = Math.floor(row['portStock7']);
						
						portArray[i].routeSecurity = Math.floor(row['routeSecurity']);
						portArray[i].nation = Math.floor(row['nation']);
					}


				}
			);

// load mission data
			transaction.executeSql('SELECT * FROM missions', [], function(transaction, result) {


					var row;
					for (i = 0; i < currentMissions.length; i++)
					{
						row = result.rows.item(i);
						

						currentMissions[i].missionName = unescape(row['missionName']);
						currentMissions[i].missionDescription = unescape(row['missionDescription']);
						currentMissions[i].sourcePort = row['sourcePort'];
						currentMissions[i].targetPort = row['targetPort'];
						currentMissions[i].completionPort = row['completionPort'];
						currentMissions[i].timeLimit = row['timeLimit'];
						currentMissions[i].duringKarma = row['duringKarma'];
						currentMissions[i].successSourceKarma = row['successSourceKarma'];
						currentMissions[i].successTargetKarma = row['successTargetKarma'];
						currentMissions[i].failureSourceKarma = row['failureSourceKarma'];
						currentMissions[i].failureTargetKarma = row['failureTargetKarma'];
						currentMissions[i].successCash = row['successCash'];
						currentMissions[i].failureCash = row['failureCash'];
						currentMissions[i].cargoType = row['cargoType'];
						currentMissions[i].cargoQuantity = row['cargoQuantity'];
						currentMissions[i].specialCargoName = row['specialCargoName'];
						currentMissions[i].missionStatus = row['missionStatus'];
						
						currentMissions[i].targetType = row['targetType'];
						currentMissions[i].targetQuantity = row['targetQuantity'];

						currentMissions[i].successSourceGovernment = row['successSourceGovernment'];
						currentMissions[i].successSourceSecurity = row['successSourceSecurity'];
						currentMissions[i].successTargetGovernment = row['successTargetGovernment'];
						currentMissions[i].successTargetSecurity = row['successTargetSecurity'];
						
					}


				}
			);

	// load player ship and crew data


			transaction.executeSql('SELECT * FROM playerShip', [], function(transaction, result) {


					var row;
					var tmpTotalAttachments;
					row = result.rows.item(0);
					
					playerShip = new ShipPart(playerShipName, playerShipName, Math.floor(row['maxSpeed']), Math.floor(row['maxCargo']), Math.floor(row['maxCrew']), Math.floor(row['maxFuel']), Math.floor(row['firepower']), Math.floor(row['maxArmor']), 0, 0, Math.floor(result.rows.length-1), Math.floor(row['baseCost']), playerShip, row['posture']);
					
					playerShip.cargo[0] = row['cargo0'];
					playerShip.cargo[1] = row['cargo1'];
					playerShip.cargo[2] = row['cargo2'];
					playerShip.cargo[3] = row['cargo3'];
					playerShip.cargo[4] = row['cargo4'];
					playerShip.cargo[5] = row['cargo5'];
					playerShip.cargo[6] = row['cargo6'];
					playerShip.cargo[7] = row['cargo7'];
					playerShip.curCargo = playerShip.cargo[0] + playerShip.cargo[1] + playerShip.cargo[2] + playerShip.cargo[3] + playerShip.cargo[4] + playerShip.cargo[5] + playerShip.cargo[6] + playerShip.cargo[7];
					playerShip.armor = row['armor'];
					playerShip.baseCost = row['baseCost'];
					playerShip.imageSeed = row['imageSeed'];

					for (i = 1; i < (result.rows.length); i++)
					{
						row = result.rows.item(i);
						
						playerShip.attachments[(i-1)] = new ShipPart(unescape(row['partName']), unescape(row['partNumber']), Math.floor(row['maxSpeed']), Math.floor(row['maxCargo']), Math.floor(row['maxCrew']), Math.floor(row['maxFuel']), Math.floor(row['firepower']), Math.floor(row['maxArmor']), 0, 0, 0, Math.floor(row['baseCost']), playerShip, row['posture']);
						playerShip.attachments[(i-1)].x = row['x'];
						playerShip.attachments[(i-1)].y = row['y'];

						playerShip.attachments[(i-1)].cargo[0] = row['cargo0'];
						playerShip.attachments[(i-1)].cargo[1] = row['cargo1'];
						playerShip.attachments[(i-1)].cargo[2] = row['cargo2'];
						playerShip.attachments[(i-1)].cargo[3] = row['cargo3'];
						playerShip.attachments[(i-1)].cargo[4] = row['cargo4'];
						playerShip.attachments[(i-1)].cargo[5] = row['cargo5'];
						playerShip.attachments[(i-1)].cargo[6] = row['cargo6'];
						playerShip.attachments[(i-1)].cargo[7] = row['cargo7'];
						playerShip.attachments[(i-1)].curCargo = playerShip.attachments[(i-1)].cargo[0] + playerShip.attachments[(i-1)].cargo[1] + playerShip.attachments[(i-1)].cargo[2] + playerShip.attachments[(i-1)].cargo[3] + playerShip.attachments[(i-1)].cargo[4] + playerShip.attachments[(i-1)].cargo[5] + playerShip.attachments[(i-1)].cargo[6] + playerShip.attachments[(i-1)].cargo[7];
						playerShip.attachments[(i-1)].armor = row['armor'];
						playerShip.attachments[(i-1)].baseCost = row['baseCost'];
						playerShip.attachments[(i-1)].imageSeed = row['imageSeed'];
					}


				}
			, errorHandler);

// load and add crewmen


			transaction.executeSql('SELECT * FROM crewman', [], function(transaction, result) {


					var row;
					var tmpTotalAttachments;
					row = result.rows.item(0);

					for (i = 0; i < (result.rows.length); i++)
					{
						row = result.rows.item(i);

						AddCrewman(new Crewman(unescape(row['firstName']),unescape(row['lastName']), row['navigation'], row['gunnery'], row['engineering'], row['haggling'], row['cost'], row['face']), playerShip);
//						drawShipPart(playerShip.attachments[(i-1)]);
					}


				}
			, errorHandler);

		}, errorHandler, dbFinishGameInitialization
	);
	
	


	// all right -- set stuff back up.
//	setTimeout(dbFinishGameInitialization, 50);

}

function dbFinishGameInitialization() 
{

	var i;
	var tmpCrewmenSeed;
	currentPort = startingPort;

	var tmpStardate = currentStardate - 2101000;
	currentStardate = 2101000;
	
	tmpCrewmenSeed = barCrewmenSeed;

	AddToCurrentStardate(tmpStardate, true);

	calculateAdjustedValues(playerShip);
	
	governmentNames[playerGovernment] = playerName;
	governmentNames[playerGovernment] = playerName + "\'s";

	calculatePortTerritory();
	drawShipPart(playerShip);
	InitializeManageShipScreen();
//	UpdateNeighboringPorts(currentPort);
	SetPortsVisible();
	drawPortPlanet(currentPort, 'planetCanvas');	
	
	document.getElementById('currentPortName').innerHTML = portArray[currentPort].portName;
	document.getElementById('currentPortName2').innerHTML = portArray[currentPort].portName;
	document.getElementById('primaryDescriptor').innerHTML = primaryPlanetDescriptors[portArray[currentPort].primaryDescriptor];
	document.getElementById('secondaryDescriptor').innerHTML = secondaryPlanetDescriptors[portArray[currentPort].secondaryDescriptor];
	

	SetText("displayCash", playerCash);

	for (i=0; i<portArray.length; i++)
	{
		SelectPortOnMap(i);
	}
	SelectPortOnMap(currentPort);
	
	SetCurrentPort(currentPort);
//	alert("bar crewmen seed: " + window.startingBarCrewmenSeed);
	populateBarCrewmen(window.startingBarCrewmenSeed);
	populateShipyard(window.startingShipyardSeed);
	drawAllMissions();

	
	SwitchWindows('loadWindow', 'portMainScreen');
	document.getElementById("playerStatusBar").style.display = "inline";

}

function dbSaveState() {
	// yeah, yeah.  It's cheap and dirty, and if I were a good little developer, I'd update instead of simply flushing the DB and repopulating.  Sosumi.
	document.getElementById("savingIcon").style.display = "inline";
//	dbTransactionsInProcess = 4;
	setTimeout(dbSaveState2, 50);

//	document.getElementById("playerStatusBar").style.display = "none";

}

function dbSaveState2() {
	var i;
	resetDatabase(systemDB);
	dbAddPlayerShip(systemDB, playerShip);
	for (i=0; i<playerShip.attachments.length; i++) {
		dbAddPlayerShip(systemDB, playerShip.attachments[i]);
	}
	dbAddPort(systemDB, -1);
	dbAddMissions(systemDB, -1);
	dbAddGlobals(systemDB);
}

function dbStartTransaction() {
//	alert("starting transaction");
	dbTransactionsInProcess = dbTransactionsInProcess + 1;
}

function dbCheckForCompletion() {
	dbTransactionsInProcess = dbTransactionsInProcess - 1;
	if (dbTransactionsInProcess <= 0)
	{
		document.getElementById("savingIcon").style.display = "none";
		dbTransactionsInProcess = 0;
	} else {
//		alert("still going: " + dbTransactionsInProcess);
		document.getElementById("savingIcon").style.display = "inline";
	}
}

function dbAddPlayerShip(db, workingShipPart) {


//	var activeShip;
	var j;

	dbStartTransaction();
	db.transaction(
		function (transaction) {
//			alert('Inserting ship: "' + workingShipPart.partNumber + '"');
			transaction.executeSql('INSERT INTO playerShip (partName, partNumber, maxSpeed, curCargo, maxCargo, cargo0, cargo1, cargo2, cargo3, cargo4, cargo5, cargo6, cargo7, maxCrew, maxFuel, fuel, firepower, maxArmor, armor, x, y, posture, baseCost, imageSeed) VALUES(\'' + escape(workingShipPart.partName) + '\', \'' + escape(workingShipPart.partNumber) + '\', ' + workingShipPart.maxSpeed + ', ' + workingShipPart.curCargo + ', '  + workingShipPart.maxCargo + ', ' + workingShipPart.cargo[0] + ', ' + workingShipPart.cargo[1] + ', ' + workingShipPart.cargo[2] + ', ' + workingShipPart.cargo[3] + ', ' + workingShipPart.cargo[4] + ', ' + workingShipPart.cargo[5] + ', ' + workingShipPart.cargo[6] + ', ' + workingShipPart.cargo[7] + ', ' + workingShipPart.maxCrew + ', ' + workingShipPart.maxFuel + ', ' + workingShipPart.fuel + ', ' + workingShipPart.firepower + ', ' + workingShipPart.maxArmor + ', ' + workingShipPart.armor + ', ' + workingShipPart.x + ', ' + workingShipPart.y + ', \'' + workingShipPart.posture + '\', ' + workingShipPart.baseCost + ', ' + workingShipPart.imageSeed + ');', [], dbCheckForCompletion, errorHandler);
		}
	)
	
	for (j = 0; j < workingShipPart.crew.length; j++) {
		dbAddCrewman(db, workingShipPart.crew[j]);
	}
}

function dbAddCrewman(db, crewmanToAdd) {
	dbStartTransaction();
	db.transaction(
		function (transaction) {
			transaction.executeSql('INSERT INTO crewman (firstName, lastName, navigation, gunnery, engineering, haggling, cost, face) VALUES(\'' + escape(crewmanToAdd.firstName) + '\', \'' + escape(crewmanToAdd.lastName) + '\', ' + crewmanToAdd.navigation + ', ' + crewmanToAdd.gunnery + ', ' + crewmanToAdd.engineering + ', ' + crewmanToAdd.haggling + ', ' + crewmanToAdd.cost +  ', ' + crewmanToAdd.face + ');', [], dbCheckForCompletion, errorHandler);
		}
	)
}


function dbAddMissions(db, missionID) {

	var i;

	if (missionID == -1) {
		for (i=0; i<currentMissions.length; i++) {
			dbAddMissions(db, i);
		}
		return;
	}

	dbStartTransaction();
 	db.transaction(
		function (transaction) {
			transaction.executeSql('INSERT INTO missions (missionName, missionDescription, sourcePort, targetPort, completionPort, timeLimit, duringKarma, successSourceKarma, successTargetKarma, failureSourceKarma, failureTargetKarma, successCash, failureCash, cargoType, cargoQuantity, specialCargoName, missionStatus, targetType, targetQuantity, successSourceGovernment, successSourceSecurity, successTargetGovernment, successTargetSecurity) VALUES(\'' +  escape(currentMissions[missionID].missionName) + '\', \'' + escape(currentMissions[missionID].missionDescription) + '\', ' +  currentMissions[missionID].sourcePort + ', ' + currentMissions[missionID].targetPort + ', ' + currentMissions[missionID].completionPort + ', ' + currentMissions[missionID].timeLimit + ', ' + currentMissions[missionID].duringKarma + ', ' + currentMissions[missionID].successSourceKarma + ', ' + currentMissions[missionID].successTargetKarma + ', ' + currentMissions[missionID].failureSourceKarma + ', ' + currentMissions[missionID].failureTargetKarma + ', ' +  currentMissions[missionID].successCash + ', ' + currentMissions[missionID].failureCash + ', ' + currentMissions[missionID].cargoType + ', ' + currentMissions[missionID].cargoQuantity + ', \'' +  currentMissions[missionID].specialCargoName + '\', \'' + currentMissions[missionID].missionStatus + '\'' + ', \'' + currentMissions[missionID].targetType + '\', ' + currentMissions[missionID].targetQuantity + ', ' + currentMissions[missionID].successSourceGovernment + ', ' + currentMissions[missionID].successSourceSecurity + ', ' + currentMissions[missionID].successTargetGovernment + ', ' + currentMissions[missionID].successTargetSecurity + ');', [], dbCheckForCompletion, errorHandler);
		}
	)
}


function dbAddGlobals(db) {

/*
playerName VARCHAR(255) NOT NULL DEFAULT "", 
playerShipName VARCHAR(255) NOT NULL DEFAULT "", 
currentPort INTEGER NOT NULL DEFAULT 0, 
playerCash INTEGER NOT NULL DEFAULT 0, 
currentStardate INTEGER NOT NULL DEFAULT 0, 
randomNumberSeed INTEGER NOT NULL DEFAULT 0
*/
	dbStartTransaction();
 	db.transaction(
		function (transaction) {
			transaction.executeSql('INSERT INTO globals (playerName, playerShipName, currentPort, playerCash, currentStardate, randomNumberSeed, shipyardSeed, barCrewmenSeed) VALUES(\'' +  escape(playerName) + '\', \'' + escape(playerShipName) + '\', ' +  currentPort + ', ' + playerCash + ', ' + currentStardate + ', ' + startingRandomNumberSeed + ', ' + shipyardSeed + ', ' + barCrewmenSeed + ');', [], dbCheckForCompletion, errorHandler);
//			alert("adding to globals");
		}
	)
}


function dbAddPort(db, portID) {

	var activeShip;
	var i;

	if (portID == -1) {
		for (i=0; i<portArray.length; i++) {
			dbAddPort(db, i);
		}
		return;
	}
	dbStartTransaction();
 	db.transaction(
		function (transaction) {
			transaction.executeSql('INSERT INTO portArray (portHostility, portFamiliarity, currentPrice0, currentPrice1, currentPrice2, currentPrice3, currentPrice4, currentPrice5, currentPrice6, currentPrice7, portStock0, portStock1, portStock2, portStock3, portStock4, portStock5, portStock6, portStock7, routeSecurity, nation) VALUES(' + portArray[portID].portHostility + ', ' + portArray[portID].portFamiliarity + ', ' + portArray[portID].portCurrentPrice[0] + ', ' + portArray[portID].portCurrentPrice[1] + ', ' + portArray[portID].portCurrentPrice[2] + ', ' + portArray[portID].portCurrentPrice[3] + ', ' + portArray[portID].portCurrentPrice[4] + ', ' + portArray[portID].portCurrentPrice[5] + ', ' + portArray[portID].portCurrentPrice[6] + ', ' + portArray[portID].portCurrentPrice[7] + ', ' + portArray[portID].portStock[0] + ', ' + portArray[portID].portStock[1] + ', ' + portArray[portID].portStock[2] + ', ' + portArray[portID].portStock[3] + ', ' + portArray[portID].portStock[4] + ', ' + portArray[portID].portStock[5] + ', ' + portArray[portID].portStock[6] + ', ' + portArray[portID].portStock[7] + ', ' + portArray[portID].routeSecurity + ', ' + portArray[portID].nation + ');', [], dbCheckForCompletion, errorHandler);
		}
	)
}

function createTables(db)
{
	 // screw good relational design.  Just get the data in there.
	db.transaction(
		function (transaction) {
			transaction.executeSql('CREATE TABLE IF NOT EXISTS portArray(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, portHostility INTEGER NOT NULL DEFAULT 0, portFamiliarity INTEGER NOT NULL DEFAULT 0, currentPrice0 INTEGER NOT NULL DEFAULT 0, currentPrice1 INTEGER NOT NULL DEFAULT 0, currentPrice2 INTEGER NOT NULL DEFAULT 0, currentPrice3 INTEGER NOT NULL DEFAULT 0, currentPrice4 INTEGER NOT NULL DEFAULT 0, currentPrice5 INTEGER NOT NULL DEFAULT 0, currentPrice6 INTEGER NOT NULL DEFAULT 0, currentPrice7 INTEGER NOT NULL DEFAULT 0, portStock0 INTEGER NOT NULL DEFAULT 0, portStock1 INTEGER NOT NULL DEFAULT 0, portStock2 INTEGER NOT NULL DEFAULT 0, portStock3 INTEGER NOT NULL DEFAULT 0, portStock4 INTEGER NOT NULL DEFAULT 0, portStock5 INTEGER NOT NULL DEFAULT 0, portStock6 INTEGER NOT NULL DEFAULT 0, portStock7 INTEGER NOT NULL DEFAULT 0, routeSecurity FLOAT NOT NULL DEFAULT 0, nation INTEGER NOT NULL DEFAULT 0)', [], nullDataHandler, errorHandler);

			transaction.executeSql('CREATE TABLE IF NOT EXISTS playerShip(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, partName VARCHAR(255) NOT NULL DEFAULT "", partNumber VARCHAR(50) NOT NULL DEFAULT "", maxSpeed INTEGER NOT NULL DEFAULT 0, curCargo INTEGER NOT NULL DEFAULT 0, maxCargo INTEGER NOT NULL DEFAULT 0, cargo0 INTEGER NOT NULL DEFAULT 0, cargo1 INTEGER NOT NULL DEFAULT 0, cargo2 INTEGER NOT NULL DEFAULT 0, cargo3 INTEGER NOT NULL DEFAULT 0, cargo4 INTEGER NOT NULL DEFAULT 0, cargo5 INTEGER NOT NULL DEFAULT 0, cargo6 INTEGER NOT NULL DEFAULT 0, cargo7 INTEGER NOT NULL DEFAULT 0, maxCrew INTEGER NOT NULL DEFAULT 0, maxFuel INTEGER NOT NULL DEFAULT 0, fuel INTEGER NOT NULL DEFAULT 0, firepower INTEGER NOT NULL DEFAULT 0, maxArmor INTEGER NOT NULL DEFAULT 0, armor INTEGER NOT NULL DEFAULT 0, x INTEGER NOT NULL DEFAULT 0, y INTEGER NOT NULL DEFAULT 0, posture VARCHAR(25) NOT NULL DEFAULT "", baseCost INTEGER NOT NULL DEFAULT 0, imageSeed INTEGER NOT NULL DEFAULT 0);', [], nullDataHandler, errorHandler);

			transaction.executeSql('CREATE TABLE IF NOT EXISTS crewman(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, firstName VARCHAR(50) NOT NULL DEFAULT "",lastName VARCHAR(50) NOT NULL DEFAULT "", navigation INTEGER NOT NULL DEFAULT 0, gunnery INTEGER NOT NULL DEFAULT 0, engineering INTEGER NOT NULL DEFAULT 0, haggling INTEGER NOT NULL DEFAULT 0, cost INTEGER NOT NULL DEFAULT 0, face INTEGER NOT NULL DEFAULT 0);', [], nullDataHandler, errorHandler);

			transaction.executeSql('CREATE TABLE IF NOT EXISTS missions(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, missionName VARCHAR(255) NOT NULL DEFAULT "", missionDescription VARCHAR(1023) NOT NULL DEFAULT "", sourcePort INTEGER NOT NULL DEFAULT 0, targetPort INTEGER NOT NULL DEFAULT 0, completionPort INTEGER NOT NULL DEFAULT 0, timeLimit INTEGER NOT NULL DEFAULT 0, duringKarma INTEGER NOT NULL DEFAULT 0, successSourceKarma INTEGER NOT NULL DEFAULT 0, successTargetKarma INTEGER NOT NULL DEFAULT 0, failureSourceKarma INTEGER NOT NULL DEFAULT 0, failureTargetKarma INTEGER NOT NULL DEFAULT 0, successCash INTEGER NOT NULL DEFAULT 0, failureCash INTEGER NOT NULL DEFAULT 0, cargoType INTEGER NOT NULL DEFAULT 0, cargoQuantity INTEGER NOT NULL DEFAULT 0, specialCargoName VARCHAR(255) NOT NULL DEFAULT "", missionStatus VARCHAR(25) NOT NULL DEFAULT "", targetType VARCHAR(25) NOT NULL DEFAULT "", targetQuantity INTEGER NOT NULL DEFAULT 0, successSourceGovernment INTEGER NOT NULL DEFAULT 0, successSourceSecurity FLOAT NOT NULL DEFAULT 0, successTargetGovernment INTEGER NOT NULL DEFAULT 0, successTargetSecurity FLOAT NOT NULL DEFAULT 0);', [], nullDataHandler, errorHandler);

			transaction.executeSql('CREATE TABLE IF NOT EXISTS globals(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, playerName VARCHAR(255) NOT NULL DEFAULT "", playerShipName VARCHAR(255) NOT NULL DEFAULT "", currentPort INTEGER NOT NULL DEFAULT 0, playerCash INTEGER NOT NULL DEFAULT 0, currentStardate INTEGER NOT NULL DEFAULT 0, randomNumberSeed INTEGER NOT NULL DEFAULT 0, shipyardSeed INTEGER NOT NULL DEFAULT 0, barCrewmenSeed INTEGER NOT NULL DEFAULT 0);', [], nullDataHandler, errorHandler);

		}
	);
 
}

function alertResults(transaction, results) {
	alert("results: " + results.rows.item(0)['recordCount']);
}

function errorHandler(transaction, error)
{
    // Error is a human-readable string.
    alert('Oops--database error, arr.  Error was '+error.message+' (Code '+error.code+'.)  Tell us about it: email bugs at iaye-game.com.  Arr.');
 
    // Handle errors here
    var we_think_this_error_is_fatal = true;
    if (we_think_this_error_is_fatal) return true;
    return false;
}

 

/*! This is used as a data handler for a request that should return no data. */
function nullDataHandler(transaction, results)
{
}

jsFilesLoaded = jsFilesLoaded + 1;
