var alphanumex =   /^([a-zA-Z])(\d|[a-zA-Z]){3,17}$/;
var alphaex =   /^[a-zA-Z]+$/;
//var alphanumex = /^([a-zA-Z])+([a-zA-Z]+|\d{0,2})$/;

var passwordex = /^(\w|[!@#\$%\^&\*\-]|\s){6,12}$/;

var emailex = /^((([a-zA-Z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+(\.([a-zA-Z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+)*)@((((([a-zA-Z]|[0-9])([a-zA-Z]|[0-9]|\-){0,61}([a-zA-Z]|[0-9])\.))*([a-zA-Z]|[0-9])([a-zA-Z]|[0-9]|\-){0,61}([a-zA-Z]|[0-9])\.)[\w]{2,4}|(((([0-9]){1,3}\.){3}([0-9]){1,3}))|(\[((([0-9]){1,3}\.){3}([0-9]){1,3})\])))$/;

// Disable copy and paste via keyboard or mouse on verify_email field--WORKS
jQuery(function($) {
	$("#verify_email").keydown(function(event) {
		//alert(window.event);
		if (event.ctrlKey || event.metaKey) {
			return false;
		}
	});

});

jQuery(function($) {
	$('#verify_email').bind("contextmenu", function(e) {
		return false;
	});
});

// toggle change child password
jQuery(function($) {
	$('a#change_child_password').bind("click", function(e) {
		$("div#child_password").toggle("slow");
		return false;
	});
});


// load the child's information
function load_child(user_id_val) {
	var selected = user_id_val;
	$.ajax({
		type: "POST",
		url: '/app/website/account/childinfo',
		dataType: 'json',
		data: ({
			user_id: selected,
			ajax: 1
		}),
		success: function(data) {
			//$('#month').val(data.month);
			//$('#year').val(data.year);
			//$('#day').val(data.day);
			$('#user_age').val(data.user_age);
			$('#user_gender').val(data.user_gender);
			$('#first_name').val(data.user_first_name);
			$('#last_name').val(data.user_last_name);
			$('#user_status').val(data.user_status);
			$('#user_id').val(data.user_id);
			$('#user_parent_email').val(data.user_parent_email);

			// check if inactive
			$('#user_status_field .errors_field_list').children().remove();
			if ($('#user_status').val() == 'Inactive')
				display_errors('#user_status_field', 'Account must be active to play.');
			else
				clear_errors('#user_status_field');	
		}
	});


	//$('#edit_child').show();
	//$('#allow').removeAttr('disabled');

}
// end load_child function

// ajax function to retrieve child information for editing
function edit_account_settings() {
	if ($('#user_password_field').attr('title').length > 0 || $('#user_password_confirm_field').attr('title').length > 0) {
		$('#child_password').show();
	}

	// populate data automatically for first child
	$("#choose_child option:first").attr('selected', true);
	var first_selected = $("#choose_child option:first").val();
	load_child(first_selected);

	// populate data when child is clicked
	$('#choose_child').bind('change', function() {
		var selected = $("#choose_child option:selected").val();
		load_child(selected);
	});

}

// display errors from post
function jQueryEventFieldError() {
	var elm = document.getElementsByTagName('fieldset');

	for (var i = 0; i < elm.length; i++) {
		if (elm[i].className.match(/error_field/)) {

			$('#' + elm[i].id).addClass('error_field');
			$('#' + elm[i].id + ' .errors_field_bottom').show();
			$('#' + elm[i].id + ' .errors_field_list').show();
			$('#' + elm[i].id + ' .errors_field_list').append('<li>' + $('#' + elm[i].id).attr("title") + '</li>');

		}
	}
}

// validate register step 1
function validate_register_step1() {
	// this step does not display errors
}

// validate register step 2
function validate_register_step2() {
	// validate first name
	$('#first_name').blur(function() {
		validate_name('first_name');
	});

	// validate last name
	$('#last_name').blur(function() {
		validate_name('last_name');
	});

	// validate email
	$('#email').blur(function() {
		validate_email('email');
	});

	// validate verify_email
	$('#verify_email').blur(function() {
		validate_email('verify_email');
		verify_email('verify_email');
	});

	// validate user name
	$('#username').blur(function() {
		validate_username('username');
	});

	// validate password 
	$('#password').blur(function() {
		validate_password('password');
	});

	// verify password
	$('#verify_password').blur(function() {
		validate_password('verify_password');
		verify_password('verify_password','password');
	});

}

// validate register step 2
function validate_register_step3() {

	$('#allow').bind("click", function(e) {
		var deny_submit = 0;
		deny_submit += validate_agreement('agreement');
		return (deny_submit > 0) ? false : true;
	});
	
	$('#agreement').bind("click", function(e) {
		validate_agreement('agreement');
	});

}

function requireFirstHalf() {
	form = countForms(); // form position
	if (arguments.length) {
		args = arguments;

		document.onkeyup = function(e) {
			allow_second_half()
		}
		document.onchange = function(e) {
			allow_second_half()
		}
		document.onclick = function(e) {
			allow_second_half()
		}
		allow_second_half();
	}

	//eventFieldError();
}

function allow_second_half() {
	if (fieldCheck()) {
		$('#second_half').show();
		requireFields('username', 'password', 'verify_password')
	}
	else {
		//$('#second_half').hide();
		document.getElementById('allow').disabled = true;
	}
}



function validate_login() {

	// validate username
	$('#username').blur(function() {
		validate_username('username');
	});
	// validate password 
	$('#password').blur(function() {
		validate_password('password');
	});
}



// validate parent register step 1
function validate_parent_register_step1() {
	// validate first name
	$('#first_name').blur(function() {
		validate_name('first_name');
	});

	// validate last name
	$('#last_name').blur(function() {
		validate_name('last_name');
	});

	// validate user name
	$('#username').blur(function() {
		validate_username('username');
	});

	// validate password 
	$('#password').blur(function() {
		validate_password('password');
	});

	// verify password
	$('#verify_password').blur(function() {
		validate_password('verify_password');
		verify_password('verify_password','password');
	});

}

// validate account settings 
function validate_account_settings() {

	
	$('#allow').bind("click", function(e) {
		var deny_submit = 0;
		deny_submit += validate_name('first_name');
		deny_submit += validate_name('last_name');
		deny_submit += validate_email('user_parent_email');
		deny_submit += validate_gender('user_gender');
		deny_submit += validate_password2('user_password');
		deny_submit += validate_password2('user_password_confirm');
		deny_submit += verify_password('user_password_confirm','user_password');
		return (deny_submit > 0) ? false : true;  
	});

	// validate first name
	$('#first_name').blur(function() {
		validate_name('first_name');
	});

	// validate last name
	$('#last_name').blur(function() {
		validate_name('last_name');
	});

	// validate user_parent_email
	$('#user_parent_email').blur(function() {
		validate_email('user_parent_email');
	});
	
	// validate gender
	$('#user_gender').blur(function() {
		validate_gender('user_gender');
	});

	// validate password 
	$('#user_password').blur(function() {
		validate_password('user_password');
	});

	// verify password
	$('#user_password_confirm').blur(function() {
		validate_password('user_password_confirm');
		verify_password('user_password_confirm','user_password');
	});

}



// display errors
function display_errors(field, msg) {
	$(field).addClass('error2');
	$(field).addClass('error_field');
	$(field + ' .errors_field_bottom').show();
	$(field + ' .errors_field_list').show();
	$(field + ' .errors_field_list').append('<li>' + msg + '</li>');
}

// clear errors
function clear_errors(field) {
	$(field).removeClass('error2');
	$(field).removeClass('error_field');
	$(field + ' .errors_field_bottom').hide();
	$(field + ' .errors_field_list').hide();
}

// validate name
function validate_name(input) {
	var found_errors = 0;

	// clear previously displayed errors
	$('#' + input + '_field .errors_field_list').children().remove();

	// if not between 2-15 chars
	if ($('#' + input).val().length < 2 || $('#' + input).val().length > 15) {
		display_errors('#' + input + '_field', "Whoops!  Please enter a name between 2 and 15 alphabetical characters.");
		found_errors = 1;
	}
	// if contains numbers
	if (/(\d|\s)/.test($('#' + input).val()) == true) {
		display_errors('#' + input + '_field', 'Must not contain numbers or spaces.');
		found_errors = 1;
	}
	if (alphaex.test($('#' + input).val()) == false) {
		display_errors('#' + input + '_field', 'Must contain only alphabetical characters');
		found_errors = 1;
	}
	// if no errors found
	if (found_errors == 0) {
		clear_errors('#' + input + '_field');
		return 0;
	}
	return 1;

}

// validate username
function validate_username(input) {
	var found_errors = 0;

	// clear previously displayed errors
	$('#' + input + '_field .errors_field_list').children().remove();

	if ($('#' + input).val().length < 4 || $('#' + input).val().length > 18) {
		display_errors('#' + input + '_field', 'The username must be between 4-18 alphanumeric characters.');
		found_errors = 1;
	}
	
    var count;
	
    if (($('#' + input).val().match(/\d/g))) {
        count = $('#' + input).val().match(/\d/g).length;
    }
    else {
        count = 0;
    }
    //console.log("There are " + count + " digits");
    
    if ( count > 2 ) {
		display_errors('#' + input + '_field', 'The username cannot contain more than two digits');
		found_errors = 1;
    }

	if (alphanumex.test($('#' + input).val()) == false) {
		display_errors('#' + input + '_field', 'The username must begin with a letter and contain no more than two digits');
		found_errors = 1;
	}
	// if no errors found
	if (found_errors == 0) {
		clear_errors('#' + input + '_field');
		return 0;
	}
	return 1;
}

// validate email
function validate_email(input) {
	var found_errors = 0;

	// clear previously displayed errors
	$('#' + input + '_field .errors_field_list').children().remove();

	if (emailex.test($('#' + input).val()) == false) {
		display_errors('#' + input + '_field', 'A valid email is required');
		found_errors = 1;
	}
	// if no errors found
	if (found_errors == 0) {
		clear_errors('#' + input + '_field');
		return 0;
	}
	return 1;
}

// validate password
function validate_password(input) {
	var found_errors = 0;

	// clear previously displayed errors
	$('#' + input + '_field .errors_field_list').children().remove();

	if (passwordex.test($('#' + input).val()) == false) {
		display_errors('#' + input + '_field', 'Must be between 6 and 12 alphanumeric characters, including these symbols: (!@#\$-\%^&*)');
		found_errors = 1;
	}



	// if no errors found
	if (found_errors == 0) {
		clear_errors('#' + input + '_field');
	}
}

// validate password
function validate_password2(input) {
	var found_errors = 0;

	// clear previously displayed errors
	$('#' + input + '_field .errors_field_list').children().remove();

	if ($('#' + input).val() != '') {
		if (passwordex.test($('#' + input).val()) == false) {
			display_errors('#' + input + '_field', 'Must be between 6 and 12 alphanumeric (!@#\$-\%^&*) characters');
			found_errors = 1;
		}
		if ($('#' + input).val().toLowerCase() == 'password' ) {
			display_errors('#' + input + '_field', 'Your password cannot be "password"');
			found_errors = 1;
		}
	}
	// if no errors found
	if (found_errors == 0) {
		clear_errors('#' + input + '_field');
		return 0;
	}
	return 1;
}

// verify password
function verify_password(input,input2) {
	var found_errors = 0;

	// clear previously displayed errors
	$('#' + input + '_field .errors_field_list').children().remove();

	if ($('#' + input).val() !== $('#' + input2).val()) {
		display_errors('#' + input + '_field', "We're sorry but the passwords do not match.  Please try again.");
		found_errors = 1;
	}
	// if no errors found
	if (found_errors == 0) {
		clear_errors('#' + input + '_field');
		return 0;
	}
	return 1;
}

// verify email
function verify_email(input) {
	var found_errors = 0;

	// clear previously displayed errors
	$('#' + input + '_field .errors_field_list').children().remove();

	if ($('#' + input).val().toLowerCase() !== $('#email').val().toLowerCase()) {
		display_errors('#' + input + '_field', "We're sorry but the email addresses you provided don't match.  Please try again.");
		found_errors = 1;
	}
	// if no errors found
	if (found_errors == 0) {
		clear_errors('#' + input + '_field');
	}
}



// validate change_password 
function validate_change_password() {
	// validate first name
	// validate password 
	$('#new_password').blur(function() {
		validate_password('new_password');
	});

	// verify password
	$('#verify_password').blur(function() {
		validate_password('verify_password');
		verify_password('verify_password','new_password');
	});

}


// validate gender
function validate_gender(input) {
	var found_errors = 0;

	// clear previously displayed errors
	$('#' + input + '_field .errors_field_list').children().remove();
	
	if ($('#' + input + ' option:selected').val() == '' || $('#' + input + ' option:selected').val() == undefined) {
		display_errors('#' + input + '_field', 'Required Information');
		found_errors = 1;
	}
	// if no errors found
	if (found_errors == 0) {
		clear_errors('#' + input + '_field');
		return 0;
	}
	return 1;
}

// validate agreement
function validate_agreement(input) {

	if ($('#agreement').is(':checked')) {
		$('#agree_prompt').hide();
		return 0;
	}
	else {
		$('#agree_prompt').show();
		return 1;
	}

}

