/**
* Some basic form checking rules.
*/
function UserMgr(url){
	this.actionUrl = url;
}
UserMgr.prototype.debug = true;
/* Needed for static form submission, if we're waiting on ajax */
UserMgr.form = false;
UserMgr.prototype.actionUrl = 'user.php?A=A';
UserMgr.busy = 0;
/**
* Test the signup form, note that this cannot use AJAX, since it's an OnSubmit handler
* and waiting for an ajax response may, in some cases, lock up a browser.
*/
UserMgr.prototype.checkSignupForm = function(form){
    var is_ok = true;
    var errors = "";
    if(form['PASSWORD'].value != form['PASSWORDC'].value){
        is_ok = false;
        errors += "Password, and confirm password do not match\n";
    }
    if(form['PASSWORD'].value.length < 6){
        is_ok = false;
        errors += "Password must be 6 or more characters\n";
    }
    var email = new RegExp(".+\@.+\..+");
    if(! email.test(form['EMAIL'].value)){
        errors += "Invalid email address\n";
		is_ok = false;
    }
    if(form['USERID'].value.length < 4){
        errors += "User ID must be more than 4 characters\n";
        is_ok = false;
    }
    // \W doesn't seem to work.
    rx = new RegExp("[^A-Za-z0-9_]");
    if(rx.test(form['USERID'].value)){
        errors += "Invalid user ID, please use only letters, numbers or underscores.\n";
        is_ok = false;
    }
    if(form['NAME'].value.length < 2){
        errors += "Please enter your name\n";
        is_ok = false;
    }
    if(! is_ok){
        alert(errors);
		return(false);
    }
	if(UserMgr.busy){
		UserMgr.form = form;
		UserMgr.waitAjax();
		return(false);
	}    
    return(is_ok);
}
/*
* Wait on ajax, when there is a busy flag.
*/
UserMgr.waitAjax = function(){
	if(UserMgr.busy){
		setTimeout('UserMgr.waitAjax()',250);
		return(false);
	}	
	if(UserMgr.form){
		UserMgr.form.submit();
		UserMgr.form = false;
	}
}

/**
* Send a basic urlencoded AJAX request to the browser asking if a given key exists.
*/
UserMgr.prototype.checkExists = function(key,msg,v){
	try {
		// Firefox has a problem with focus()
		var frm = v.form;
		var agent = navigator.userAgent.toLowerCase();
		if(agent.indexOf("firefox") != -1){
			for(var i = 0; i < frm.elements.length; ++i){
				frm.elements[i].setAttribute('autocomplete','off');
			}
		}
	}catch(fex){
		if(this.debug){
			alert(fex);
		}
	}
	try {
		// var ajax = new FormAjax('?DO_AJAX_CHECK_KEY');
		var ajax = new FormAjax(this.actionUrl + '&DO_AJAX_CHECK_KEY=1');
		ajax.method = 'POST';
		ajax.addVariable(key,v.value);
		// Presrve our own object, we'll need it next.
		var mgr = this;
		ajax.handleResponse = function(r){
			try {
				rsp = new FormAjax.Response(r,'message');	
			}catch(ex){
				mgr.handleKeyExists(v,msg);
			}
			--UserMgr.busy;
			return;
		}
		ajax.handleProblem = function(r){
			--UserMgr.busy;
			// Ignore problems, since we're on a signup form and don't want to introduce
			// any weird messages to a prospective signup.
			return;
		}
		++UserMgr.busy;
		ajax.submit();
	}catch(ex){
		alert(ex);
	}
}
UserMgr.prototype.handleKeyExists = function(v,msg){
	try {
		alert(msg);
		v.value = "";
		v.focus(); 
	}catch(ex){
		alert("Thrown here" + ex);
	}
}

