//This script copywrite 2006 William "Jump" Whittington.
//It may not be copied, used, altered, or transfered in
//any way other than to be used in code applications
//provided by me in their original form.
var currentSelect = '';
function _loadTextDoc(url, query_string, callback) {
	entry_check = new vB_AJAX_Handler(true);
	entry_check.onreadystatechange(callback);
	entry_check.send(url, query_string);
}
function _processNameInput() {
	if (entry_check.handler.readyState == 4) {
		if (entry_check.handler.status == 200) {
			liveCheck('', entry_check.handler.responseText);
		}
	}
}
function liveCheck(input, response, min_user_length) {
	var namecheck	 = document.getElementById('nameCheck');
	if (response != ''){
		switch (response){
			case '3':_replaceText(namecheck, lfsr_username_invalid_semicolon, 'namebad');break;
			case '2':_replaceText(namecheck, lfsr_username_invalid_censored, 'namebad');break;
			case '1':_replaceText(namecheck, lfsr_username_not_available, 'namebad');break;
			case '0':_replaceText(namecheck, lfsr_username_available, 'namegood');break;
		}
	} else {
		if (input.length >= parseInt(min_user_length)){
			charCheck = /(\?<!&#\d{3}|&#\d{4}|&#\d{5});/;
			if (charCheck.test(input))
			{
				alert('duh');
			}
			_loadTextDoc('username_check.php', 'username='+PHP.urlencode(input), _processNameInput);
		} else {
			if (input.length >= 1){
				_replaceText(namecheck, lfsr_username_too_short, 'nametooshort');
			} else {
				_replaceText(namecheck, lfsr_username_choose, 'namechoose');
			}
		}
	}
}
function _replaceText(el, text, class_name, colspan, width) {
	if (el.firstChild){
		el.removeChild(el.firstChild);
		if (el.firstChild){
			el.removeChild(el.firstChild);
		}
	}
	el.appendChild(document.createTextNode(text));
	el.className = class_name;
}

function match_fields(id,type){
	fieldcheck = document.getElementById(id);
	if (type == 'password'){
		field1 = document.forms.register.password;
		field2 = document.forms.register.passwordconfirm;
		matchAllow = false; //temp
	}else{
		field1 = document.forms.register.email;
		field2 = document.forms.register.emailconfirm;
	}
	if (!matchAllow){
		switch (true){
			case field1.value == '' || field2.value == '':
				_replaceText(fieldcheck, lfsr_match_fill_both, 'nametooshort');
				break;
			case field1.value != field2.value:
				_replaceText(fieldcheck, lfsr_match_false, 'namebad');
				break;
			default:_replaceText(fieldcheck, lfsr_match_true, 'namegood');break;
		}
	}
}
function _processRefererInput() {
	if (entry_check.handler.readyState == 4) {
		if (entry_check.handler.status == 200) {
			checkReferer('', entry_check.handler.responseText);
		}
	}
}
function checkReferer(input, response, min_user_length){
	referer_check = document.getElementById('lfsreferer');
	var field1 = document.forms.register.referrername;
	if (response != ''){
		switch (response){
			case '1':_replaceText(referer_check, lfsr_username_member_true, 'namegood');break;
			default:_replaceText(referer_check, lfsr_username_member_false, 'namebad');;break;
		}
	} else {
		switch (true){
			case input.length >= parseInt(min_user_length):
				_loadTextDoc('username_check.php', 'username='+PHP.urlencode(input), _processRefererInput);
				break;
			case input.length >= 1:
				_replaceText(referer_check, lfsr_username_member_false, 'namebad');
				break;
		}
	}
}
function _processEmailInput(){
	if (entry_check.handler.readyState == 4) {
		if (entry_check.handler.status == 200) {
			checkEmail('', entry_check.handler.responseText);
		}
	}
}
function checkEmail(input, response){
	email_check = document.getElementById('lfseconfirm');
	matchAllow = true;
	switch(true){
		case response == '':
			if(currentSelect != input){
				if (email_check.firstChild != lfsr_email_validation_progress && input != ''){
					_replaceText(email_check, lfsr_email_validation_progress, 'emailtooshort');
				}
				var validCheck = /^[A-z0-9.!#$%&\'*+-/=?^_`{|}~]+@([0-9.]+|([^\s]+\.+[A-z]{2,6}))$/;
				switch (true){
					case validCheck.test(input):
						_loadTextDoc('email_check.php', 'email='+PHP.urlencode(input), _processEmailInput);
						currentSelect = input;
						break;
					case input.length >= 1:
						_replaceText(email_check, lfsr_email_invalid, 'emailbad');
						break;
				}
			}
			break;
		case response != '':
			switch (response){
				case '5':_replaceText(email_check, lfsr_email_reject+' '+lfsr_email_submit_valid, 'emailbad');break;
				case '4':_replaceText(email_check, lfsr_email_no_responce, 'emailbad');break;
				case '3':_replaceText(email_check, lfsr_email_no_connect+' '+lfsr_email_submit_valid, 'emailbad');;break;
				case '2':_replaceText(email_check, lfsr_email_banned, 'emailbad');break;
				case '1':_replaceText(email_check, lfsr_email_exists, 'emailbad');break;
				case '0':_replaceText(email_check, lfsr_email_valid, 'emailgood');matchAllow = false;break;
			}
			break;
	}
}