$(document).ready(function(){
	$("#register").submit(function(){
		return validate(); // The form will only submit if validate returns true
	});
	//loginForm();
	loginLables();
});
function loginLables(){
	var pwInput;
	formInputSwitch();
	formLables();
	formFocusEvents();
	formBlurEvents();
}
function formInputSwitch(){
	$("input").each(function(i){
		if( $(this).attr('type') == 'password' &&  $(this).val() == '' ){
			//console.log('switch password');
			var id = $(this).attr('id');
			pwInput = $(this);
			$(this).before( '<input class="tempInput fadedInput" id="'+id+'" name="'+id+'" type="text"></input>' ).remove();
			
			$('#'+id).focus(function(){
				//console.log('text password focus');
				formInputSwitch();
				
			});
			formLables();
		}else if( $(this).attr('class') == 'tempInput fadedInput' ){
			// Replace password filed back
			//console.log('text password unswitch');
			$(this).before( pwInput ).remove();
			$(pwInput).focus();
		}
	});
}
function formLables(){
	// Add lables as default values
	$("#loginform label").each(function(i){
		if( $('#'+$(this).attr('for') ).val() == '' || $('#'+$(this).attr('for') ).val() == $(this).text() && $('#'+$(this).attr('for') ).attr('type') != 'password' ){
			$('#'+$(this).attr('for') ).addClass('fadedInput').val( $(this).text() );
		}
	});
}
function formFocusEvents(){
	$("#loginform label").each(function(i){
		var label = $(this);
		// Focus events
		$('#'+$(this).attr('for') ).focus(function(){
			//console.log('Standard focus');
			if( $(this).val() == label.text() ){
				$(this).val( '' ).removeClass('fadedInput');
			}
		});
	});
}
function formBlurEvents(){
	$("#loginform label").each(function(i){
		var label = $(this);
		// Blur events
		$('#'+$(this).attr('for') ).blur(function(){
			//console.log('Standard blur');
			if( $(this).val() == '' ){
				$(this).val( label.text() ).addClass('fadedInput');
			}			
		});	
	});	
}


function validate(){
	// Validate the form here
	
	$('.fieldRequired').each(function(i){
		$(this).removeClass('fieldRequired');
	});
	
	var errorText = '';

	// This will cycle through DOM looking for lables, from there it will cycle through their inputs checking for if they are required or validEmail
	// If something doesn't validate it will display an alert using the label and add a class that can be styled to represent it needs atention
	$('label').each(function(i){
		var field = $(this).text();
		$('#'+$(this).attr('for')+'.required').each(function(i){
			if( $(this).val() == '' ){
				errorText = errorText + " \n\r" + field + " is required";
				$(this).addClass('fieldRequired');
			}
		});
		$('#'+$(this).attr('for')+'.validEmail').each(function(i){
			if( !isEmailValid($(this).val()) ){
				errorText = errorText + " \n\r" + field + " must be valid";
				$(this).addClass('fieldRequired');
			}
		});
	});
	
	if (errorText != ''){
		alert("Sorry there are the following issues with your details: \n\r"+errorText);
		return false;
	}else{
		return true;
	}
}

function isEmailValid(str)
{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	return reg.test(str);
}