var user_manger = {};
var sendUserUrl = "user_insert.php";

function AJAX_USERMgr() {
  /* Find the form element with id "formInsert".
   * This makes the javascript non-intrusive */
  this.form = document.getElementById('userInsert');
  /* make the initial cache of the form data */
  YAHOO.util.Connect.setForm(this.form);
  this.cache = YAHOO.util.Connect._sFormData;
}
function logEvent(msg, status) {
	YAHOO.log(msg, status);
}
AJAX_USERMgr.prototype.userSend = function() {
  /* Update the data stored in YAHOO.util.Connect._sFormData
     to be the current from data*/
	var Dom = YAHOO.util.Dom; 
	this.form = Dom.get('userInsert');
	var the_saved = YAHOO.util.Dom.get("user_save_status");
	the_saved.innerHTML = "<em>saving...</em>";
				
  	/* make the initial cache of the form data */
	YAHOO.util.Connect.setForm(this.form);
	logEvent("userSend - user_send.js", "time");
	this.sentData = YAHOO.util.Connect._sFormData;
	//logEvent(this.sentData);
	//&& this.sentData!=this.cache
	if (this.sentData!='') {	
		logEvent(sendUserUrl + '?' + this.sentData);
		//logEvent("eventSend - event_send.js - 311");
		var arrData = this.sentData.split("&");
		
		var request = YAHOO.util.Connect.asyncRequest('POST', sendUserUrl, this.sentData,
		   {success: function(o){
				this.cache = this.sentData;
				response = o.responseText;
				//mark it saved
				var the_saved = YAHOO.util.Dom.get("save_status");
				the_saved.innerHTML = "saved!";
				refreshTimeOut();
			},
		   after: function(){
			   //
			},
		   scope: this});
	}
};
AJAX_USERMgr.prototype.userRefresh = function() {
	var Dom = YAHOO.util.Dom;
	var the_saved = Dom.get("save_status");
	the_saved.innerHTML = "&nbsp;";
	logEvent("userRefresh Completed", "window");
}
//load the person form
var checkUser = function(uuid) {
	var Dom = YAHOO.util.Dom;
	var current_username = Dom.get("username");
	
	//var theuser_form = Dom.get("user_form_container");
	//theuser_form.innerHTML = "<div align='center'><img src='remembermilk_orange.gif' width='20' height='20' alt='Please wait while we load the data'></div>";
	
	var userUrl = "user_check.php";
	//refresh the list
	var userset_callback = {
		success: function(o){
			response = o.responseText;
			logEvent("response:" + response);
			if (response!="") {									
				YAHOO.util.Dom.setStyle("username_taken", "display", "");				
			} else {
				YAHOO.util.Dom.setStyle("username_taken", "display", "none");				
			}
		},
	   after: function(){
			//this.setTimeOut();
		},
	   scope: this};
	
	logEvent(userUrl + '?username=' + current_username.value);
	var postData = 'username=' + current_username.value;
	var user_request = YAHOO.util.Connect.asyncRequest('GET', userUrl + '?username=' + current_username.value, userset_callback);
	//logEvent("setUser Completed", "window");
}
AJAX_USERMgr.prototype.setTimeOut = function() {
	logEvent("User setTimeOut", "time");
	/* use a closure so eventSend() executes in the correct scope */
	var thisC = this;
	//thisC.logEvent("time out");
	/* Look for changes in the form data every one second. */
	this.timeout = setTimeout(function() {
		thisC.userSend();
	}, 10);
};
AJAX_USERMgr.prototype.refreshTimeOut = function() {
	logEvent("User refreshTimeOut", "time");
	/* use a closure so eventRefresh() executes in the correct scope */
	var thisC = this;
	/* Look for changes in the form data every one second. */
	this.timeout = setTimeout(function() {
		thisC.userRefresh();
		}, 30000);
};
/*
var loadUser = function (uuid) {
	//logEvent("loading event('" + uuid + "')", "info");
	var tim = setTimeout("setUser('" + uuid + "')", 50);
}
*/
var checkUsername = function () {
	logEvent("checkUser", "info");
	var tim = setTimeout("checkUser()", 50);
}
