// waiting for dom to be ready
$(document).ready( function() {

	// jquery object of textfields
	var textfields = $("#gameform input:text");

	// css class for textfield highlight
	var highlight = "pseudoFocus";
	
	// highlight textfield on focus
	textfields.focus( function() {
		$(this).addClass(highlight);
	} );
	
	// remove highlight if textfield is empty on blur
	textfields.blur( function() {
		if(this.value == "") $(this).removeClass(highlight);
	} );

	// add form check on submit
	$("#gameform").submit( function() {

		// trim textfield values
		textfields.each( function() {
			this.value = this.value.replace(/^(\s*)/, "");
			this.value = this.value.replace(/(\s*)$/, "");
		} );
	
		if(textfields[0].value == textfields[1].value) {
			if(textfields[0].value == "") { // no names at all
				alert("Bitte Namen eingeben!");
				textfields[0].focus();
			}
			else { // equal names
				alert("Bitte verschiedene Namen wählen!");
				textfields[1].select();
			}
			return false;
		}
		return true;
	} );
	
	// focus first player input field
	textfields[0].focus();
	
} );