document.observe('dom:loaded', function(event) {

	// Clear inputs
	$$('.input_clear').each(function(obj) {
	
		// Get initial value
		var input_value = obj.value;
		
		// Clear initial value on focus
		obj.observe('focus', function(event) {
		
			// ... if the value is the same as the inital value; clear it
			if ( obj.value == input_value)
				obj.value = "";
			
		});
		
		// If it's still empty after blur, fill it up again
		obj.observe('blur', function(event) {
		
			// ... if the value is the same as the inital value; clear it
			if ( obj.value == "")
				obj.value = input_value;
			
		});
	
	});

	var init_vals = new Array();
	
	init_vals['firstname'] = 	"Förnamn";
	init_vals['lastname'] = 	"Efternamn";
	init_vals['adress'] = 		"Adress";
	init_vals['zipcode'] = 		"Postnummer";
	init_vals['city'] = 		"Ort";
	init_vals['persnr'] = 		"Personnummer / Org.nr.";
	init_vals['phonenr'] = 		"Telefonnummer";
	init_vals['email'] = 		"E-postadress";
	init_vals['password'] = 	"Lösenord";
	init_vals['password2'] = 	"Upprepa lösenord";

	$$('.userform').each(function(obj) {
			
		// If it already is the init or less than 3 characters, it's invalid
		if ( obj.value == init_vals[obj.name] || (obj.value != '' && obj.value.length < 3) )
			obj.addClassName('invalid');
			
		// Fill it with init data
		if ( obj.value == '' )
			obj.value = init_vals[obj.name];
	
		// On focus
		obj.observe('focus', function(event) {
		
			// Special stuff for password fields
			if ( obj.name == "password" || obj.name == "password2" )
				obj.setAttribute('type','password');
		
			// If the value is the same as the initial value, then remove it
			if ( obj.value == init_vals[obj.name] )
				obj.value = '';
		
		});
		
		// On blur
		obj.observe('blur', function(event) {
		
			// When the user leaves the field, if it's still empty, fill it up
			if ( obj.value == '' ) {
			
				// Special stuff for password fields
				if ( obj.name == "password" || obj.name == "password2" )
					obj.setAttribute('type','text');
			
				obj.value = init_vals[obj.name];
				
			}
				
			// Validate the field
			validate_user_field(obj);
		
		});
		
		// Validate every keydown
		obj.observe('keydown', function(event) { validate_user_field(obj) });
	
	});
	
	// Function for validation
	function validate_user_field(obj) {

		if ( obj.value == init_vals[obj.name] || obj.value.length < 2 )
			obj.addClassName('invalid');
		else
			obj.removeClassName('invalid');
			
		// Special validation for password-fields, they must match
		if ( obj.name == "password" || obj.name == "password2" ) {
		
			if ( $('password').value != $('password2').value )
				$('password2').addClassName('invalid');
			else
				$('password2').removeClassName('invalid');
		
		}

	}
	
	// Limit text-areas
	$$('.text_limit').each(function(obj) {
	
		// Check keydown
		obj.observe('keyup', function(event) {
		
			// Get content
			var content = obj.value;
			
			// If the length exceeds the limit, cut it
			if ( content.length > 280 ) {
				content = content.substring(0, 280);
				obj.innerHTML = content;
				obj.value = content;
			}
		
		});
	
	});

});
