var errList = {};
var key = '';
var allFields = '';
//matches - asmith@mactec.com, foo12@foo.edu, bob.smith@foo.tv
var emailExp = new RegExp(/([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)/);

//matches - <b>,  </b>,  <p><b>some text</b></p>
var htmlExp = new RegExp(/<\/?[a-z][a-z0-9]*[^<>]*>/);
var codeReplace = / /;

//matches - #FFFFFF, #FF3421, #00FF00
var colorCodeExp = new RegExp(/(#){1}([a-fA-F0-9]){3,6}/);

/*matches - <script language=javascript>document.write("one");</script>*/
var jsExp = new RegExp(/(<script[^>]*>[\w|\t|\r|\W]*<\/script>)/);

//matches - 
var usernameExp = /^[a-zA-Z]{1}[a-zA-Z0-9_]{5,11}$/;

function submitForgotForm(forgot) {
	var ok = validateForgot(allFields);
	if(forgot == 'username')
		var form = '#forgotUsernameForm';
	else if(forgot == 'password')
		var form = '#forgotPasswordForm';
	var str = $(form).serialize();
	if(ok) {
		$.post("forgotInfo.php", str + '&forgot=' + forgot, function(data) {
			if(data.status==true){
				$('div[id^="forgot"]').empty().append(data.message)
			} else {
				var element = $('input[type!=submit]:visible:first').attr('id');
				var label = $('label[for="' + element + '"]');
				$('#' + element)
					.val('')
					.focus()
					.next()
					.before('<br class="extra" /><span class="error">' + data.message + '</span>');
				label.addClass('warning');
			}
		}
		,'json');
		//return true;
	} else {
		return false;
	}
}

function validateForgot(field) {
	var count = field.length;
	for(var i = 0; i < count; i++) {
		var id = $(field[i]).attr("id");
		var value = $(field[i]).val();
		key = id;
		
		if (!value) {
			errList[key]++;
			return invalidForgot(id, true);
			return false;
			break;
		}
		if( !(htmlExp.test(value)) && !(colorCodeExp.test(value)) && !(jsExp.test(value)) ) {
			if(id == 'email') {
				if(!emailExp.test(value)) {
					errList[key]++;
					return invalidForgot(id, false);
					return false;
					break;
				}
			} else if(id == 'username') {
				if(!(/^[a-zA-Z]{1}[a-zA-Z0-9_]{5,11}$/.test(value))) {
					errList[key]++;
					return invalidForgot(id, false);
					break;
				}
			}
		}
	}
	return true;
}

function invalidForgot(element, empty) {
	if(errList[element] > 1) {
		return false;
	} else {
		var label = $('label[for="' + element + '"]');
		
		var errorMessage = '';
		
		if(empty) {
			errorMessage = label.text() + ' is required. ';
		} else if(element == 'username') {
			if($('#' + element).val().length < 6 || $('#' + element).val().length > 12) {
				errorMessage = 'Usernames must be 6-12 characters long.';
			} else {
				errorMessage = 'Usernames may only contain A-Z,a-z,0-9,_ and must start with a letter.';
			}
		} else if(element == 'email') {
			errorMessage = label.text() + ' is not valid.';
		}
	
		clearForgotErrors();
		$('#' + element)
			.focus()
			.next()
			.before('<br class="extra" /><span class="error">' + errorMessage + '</span>');
		
		//$('<br class="extra"><br class="extra"><br class="extra"><br class="extra">').appendTo($row);
		label.addClass('warning');
		
		$('#' + element).val('');
		return false;
	}
}

function clearForgotErrors() {
	$('label').removeClass('warning');
	$('span.error').remove();
	$('br.extra').remove();
}

$(function() {
	var allFields = $('.forgotField');
	
	$('.forgotLink').live('click', function() {
		var forgot = $(this).text();
		var site = getQuerystring('site');
		var url = SECURE + 'forgot.php?site=' + site + '&f=' + forgot;
		window.location = url;
	});
	
	if(page == 'forgot.php') {
		var form = $('form[name^="forgot"]');
		var forgot = form.attr('id').slice(6,-4);
		
		allFields.blur(function() {
			var id = $(this).attr("id");
			var label = $('label[for="' + id + '"]');
			clearErrors();
			errList[id] = 0;
			validateForgot($(this));
			resize();
		});
		
		form.submit(function(event) {
			event.preventDefault();
			return submitForgotForm(forgot.toLowerCase());
		});
	}
	
	/*$('.forgotPassword').click(function() {
		$('#forgotPassword').toggle('slow');
		form = $('#forgotPasswordForm');
		form.submit(function(event) {
			event.preventDefault();
			return submitForgotForm('password');
		});
	});*/
});
