// JavaScript Document

// TEST SCRIPT
function a(message) {
	if (message === undefined) {
		alert('Test is working');
	} else {
		alert(message);
	}
}

/* DATABASEOBJECT OBJECT */
/* ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ */
function DatabaseObject(tableName, databaseArray) {
	//this.db_fields = new Array();
	this.id;
	this.value;
	this.tableName = tableName;
	
	//this.sqlQuery;
	this.oldArray = new Array();
	this.array = new Array();
	/*
	this.array.objectID.attribute;
	*/
	
	if (!(databaseArray === undefined)) {
		this.array = databaseArray;
	}
}

DatabaseObject.prototype.toString = function() {
	return "This DatabaseObject tableName is: " + this.tableName;
};

DatabaseObject.prototype.valueOf = function() {
	return "This DatabaseObject id is: " + this.id;
};

DatabaseObject.prototype.equals = function(oDatabaseObject) {
	return (this.id == oDatabaseObject.id);
};

DatabaseObject.prototype.compareTo = function(oDatabaseObject) {
	if (!(this.value === undefined) && !(oDatabaseObject.value === undefined)) {
		return (this.value - oDatabaseObject.value);
	} else if (!(this.value === undefined) && !(oDatabaseObject.value === undefined)) {
		return ("Both tables do not have values!");
	}
};

DatabaseObject.prototype.print_r = function() {
	/* Refreshes Manage Users Panel */
	var output;
	for (var object in this.array) {
		var record = this.array[object];
		output += "{";
		for (var field in record) {
			output += field+": "+record[field]+"; ";
		}
		output += "}";
	}
	return output;
	//setInterval(refresher(), 1000);
}

DatabaseObject.prototype.query = function(action, object) {
	var json = JSON.stringify(this);
	$.ajax({
		   type: "GET", 
		   url: "_js/xmljs.php", 
		   dataType: "html", 
		   data: {json: json, action: action}, 
		   success: function(data) {
			   var query_id = Math.floor(Date.now()+(Math.random()*1947602837593754));
			   var output = '<query id="'+query_id+'">'+data+'</query>';
			   $("#xmljs").append(output);
			   var myCode = 'object.'+action+'("success", '+query_id+')';
			   eval(myCode);
		   }, 
		   error: function(data,text,error) {
			   //alert('Failure: '+data.responseText+', '+text+', '+error);
		   }
	});
	return true;
}

/*
//DATABASE OBJECT METHOD TEMPLATE
*/

DatabaseObject.prototype.create = function(status, xmljsid) {
	if (status === undefined || xmljsid === undefined) {
		this.query("create", this);
	} else if (status == "success") {
		alert(status+", "+xmljsid);
		return true;
	} else {
		return false;
	}
}

DatabaseObject.prototype.update = function(status, xmljsid) {
	if (status === undefined || xmljsid === undefined) {
		this.query("update", this);
	} else if (status == "success") {
		
		$("xmljs#"+xmljsid).remove();
		return true;
	} else {
		return false;
	}
}

DatabaseObject.prototype.destroy = function(status, xmljsid) {
	if (status === undefined || xmljsid === undefined) {
		this.query("destroy", this);
	} else if (status == "success") {
		
		$("xmljs#"+xmljsid).remove();
		return true;
	} else {
		return false;
	}
}

DatabaseObject.prototype.authenticate = function(status, xmljsid) {
	if (status === undefined || xmljsid === undefined) {
		this.query("authenticate", this);
	} else if (status == "success") {
		
		alert(this.id);
		
		//return this.xmlPopulate(xmljsid) ? true : false;
	} else {
		return false;
	}
}

DatabaseObject.prototype.find_by_id = function(status, xmljsid) {
	if (status === undefined || xmljsid === undefined) {
		this.query("find_by_id", this);
	} else if (status == "success") {
		return this.xmlPopulate(xmljsid) ? true : false;
	} else {
		return false;
	}
}

DatabaseObject.prototype.find_all = function(status, xmljsid) {
	if (status === undefined || xmljsid === undefined) {
		// Initial call
		if (!(this.tableName === undefined)) {
			// Call this 
			this.query("find_all", this);
		} else {
			alert('XMLJS ERROR: DatabaseObject.find_all() is a method that requires DatabaseObject.tableName be defined.');
		}
	} else if (status == "success") {
		// Code to be execute if the AJAX request from this.query() is successful. Populates this.array with the records from this.query
		// Goes through the XML that is retrieved
		return this.xmlPopulate(xmljsid) ? true : false;
	} else {
		return false;
	}
}

DatabaseObject.prototype.xmlPopulate = function(xmljsid) {
	this.oldArray = this.array;
	var query = document.getElementById(xmljsid);
	var action = query.firstChild.innerHTML;
	// Gets the actual set of records
	var database_object = query.childNodes[1];
	// Loops through the set of records...
	/*if (database_object.childElementCount = 1) {
		var tempObject = database_object.childNodes[0];
		this.id = tempObject.id;
	}*/
	for (i=0; database_object.childElementCount>i; i++) {
		// ...to retrive the JSON of the record...
		var tempObject = database_object.childNodes[i];
		// ...establishes the record's ID as the key in this.array, such that this.array[id] is an object with the attributes of that record
		var tempObjectId = tempObject.id;
		// ...populates this.array[id] with the object from the JSON
		this.array[tempObjectId] = JSON.parse(tempObject.innerHTML);
	}
	// Removes the XML from the page
	$("query#"+xmljsid).remove();
	return true;
}

/* USER OBJECT */
/* ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ */
function User(tableName, databaseArray) {
	DatabaseObject.call(this, tableName, databaseArray);
	this.id = id;
	this.password;
	this.username;
	this.email;
	this.first_name;
	this.last_name;
}

var UserObject = new DatabaseObject("users");
UserObject.find_all();
