var xmlObj;
function initializeDB(str){
	var cols = trim(str.slice(6, str.indexOf("from")));
	if (cols == -1){
		alert("Invalid Select Query");
		return;
	}
	var beginInd = str.indexOf("from") + "from".length;
	var db = trim(str.slice(beginInd, str.length));
	if (db.indexOf(" ") != -1){
		db = trim(db.slice(0, db.indexOf(" ")));
	} else {
		db = trim(db.slice(0,db.length));
	}
	if (db == "" || db.slice(-4) != ".xml"){
		alert("Invalid Select Query, Must Specify an XML Database File");
		return;
	}
	
	if (window.ActiveXObject) {
		xmlObj = new ActiveXObject("Microsoft.XMLDOM");
		xmlObj.async=false;
		xmlObj.load(db);
	} else if(window.XMLHttpRequest) {
		var d = new XMLHttpRequest();
		d.open("GET", db, false);
		d.send(null);
		xmlObj=d.responseXML;
	} else {
		xmlObj = document.implementation.createDocument("","",null);
		xmlObj.async=false;
		xmlObj.load(db);
	}
}

function query(str){
	var str = trim(str).toLowerCase();
	var method = str.slice(0,6);
	if (xmlObj == null){
		initializeDB(str);
	}
	
	if (method == "select"){
		return selectS(str);
	} else if (method == "update"){
		updateS(str);
	} else if (method == "insert"){
		insertS(str);
	} else if (method == "delete"){
		deleteS(str);
	} else {
		alert("Invalid query statement");
		return;
	}
}
function selectS(str){
	var cols = trim(str.slice(6, str.indexOf("from")));
	if (cols == -1){
		alert("Invalid Select Query");
		return;
	}
	var beginInd = str.indexOf("from") + "from".length;
	var db = trim(str.slice(beginInd, str.length));
	if (db.indexOf(" ") != -1){
		db = trim(db.slice(0, db.indexOf(" ")));
	} else {
		db = trim(db.slice(0,db.length));
	}
	if (db == "" || db.slice(-4) != ".xml"){
		alert("Invalid Select Query, Must Specify an XML Database File");
		return;
	}
	
	var columnsToPrint = [];
	if (cols != "*"){
		if (cols != "fields"){
			var temp = "";
			for (var i=0; i < cols.length; i++){
				if (cols.charAt(i) != ","){
					temp += cols.charAt(i);
				} else {
					columnsToPrint[columnsToPrint.length] = trim(temp);
					temp = "";
				}
			}
			columnsToPrint[columnsToPrint.length] = trim(temp);
		} else {
			return recurseFieldsIntoArray(xmlObj.childNodes, [], "");
		}
	}
	
	var beginInd = str.indexOf("where");
	if (beginInd != -1){
		var specified = trim(str.slice(beginInd+"where".length, str.length));
	} else {
		var specified = "";
	}
	
	var valsToPrint = [];
	valsToPrint[0] = [];
	var temp = "";
	
	for (var i=0; i < specified.length; i++){
		if (specified.slice(i, i+1) == "=" && temp.length > 0){
			var startInd = specified.indexOf("'")+1
			var value = specified.slice(startInd, specified.indexOf("'", startInd));
			valsToPrint[temp] = value;
			valsToPrint[0][valsToPrint.length-1] = temp;
			var temp = "";
			/*i = specified.indexOf("'", startInd)+1;
			if (specified.indexOf("or", i) != -1){
				if (specified.indexOf("'", i) < specified.indexOf("or", i)){
					alert("Invalid Select Statement after WHERE: or")
				}
			}
			if (specified.indexOf("and", i) != -1){
				if (specified.indexOf("'", i) < specified.indexOf("and", i)){
					alert("Invalid Select Statement after WHERE: and")
				}
			}*/
		} else if (specified.slice(i, i+1) != " " && specified.slice(i, i+1) != "=") {
			//temp += specified[i];
			temp += specified.slice(i, i+1);
		} else if (specified.slice(i, i+1) == "="){
			alert("Invalid Select Statement after WHERE");
			return "";
		}
	}

	var data = getDataMatrix(xmlObj, columnsToPrint, valsToPrint);
	
	//if a query ends in assoc, this code will execute.  This changes the data matrix into an 
	//associative array
	if (str.slice(str.length - 5) == "assoc"){
		//temp will turn into data
		var temp = [];
		//Add a row that has all the fields to print
		temp[0] = [];
		//Cycle through the fields and add the fields to the first row
		//Add an empty array to all associations
		for (var j=0; j < data[0].length; j++){
			temp[data[0][j]] = [];
			temp[0][j] = data[0][j];
		}
		
		//Cycle through the data adding a new row to each association for every row in the data
		for (var i = 1; i < data.length; i++){
			for (var j=0; j < data[0].length; j++){
				temp[data[0][j]][temp[data[0][j]].length] = data[i][j];
			}
		}
		
		//replace data with this new data matrix
		var data = temp;
	}
	//var data = getDataMatrix(xmlObj, columnsToPrint, [["track","Advice from the Field"], ["name","NIATx Fundamentals: A Kick-Start to Process Improvement"]]);
	return data;
}

function updateS(str){
	//not yet implemented
	return;
}
function insertS(str){
	//not yet implemented
	return;
}
function deleteS(str){
	//not yet implemented
	return;
}