function checkIfUserExists() {
	
		var ajax = new Ajax.Request('/user/checkUsername', {
				
				parameters: {username: $F('username_q') },
				asynchronous: false,
				onSuccess: function(response, json){
					
					flag = json[0];

				 	if(flag=='true') {
				 		$('user_check_result').value = 1;
				 		if($('advice-user-unique') == null) {
				 			var myTemplate = new Template('<div class="note" id="advice-user-unique" style="display:none"><span class="note-inner">#{title}</span></div>');
				 			var show = {title: _("User already exists") };
				 			
							var advice = myTemplate.evaluate(show);
				 			new Insertion.After('username_q', advice);
				 			new Effect.Appear('advice-user-unique',{duration : 1 });
							advice.style.zIndex = (tooltipZindex++)
				 		}
				 		
				 	}
				 	else {
				 		if($('advice-user-unique') != null) {
				 			new Effect.Fade('advice-user-unique',{duration : 1 });
				 			$('user_check_result').value = 0;
				 		}
					}
		
					}
			}
		)
		//alert(user_test)
		if($F('user_check_result')== 0) return false
		else return true;
		
	
	}
	
	
	if ($('username_q')) {
  	Event.observe('username_q', 'blur', function(){
  		var res = Validation.get('validate-username').test($F('username_q'));
  		if (res) {
  			checkIfUserExists();
  		}
  		
  		
  	});
  }	
	
	Validation.add('validate-username', _('Username required. 5-20 characters.'), {
	     minLength : 4, // value must be at least
	     maxLength : 21 // value must be no longer 

	});
	Validation.add('validate-password', _('Password between 5-20 characters.'), {
	     minLength : 4, // value must be at least
	     maxLength : 21 // value must be no longer 

	});
	Validation.add('validate-password-retype', _('Repeat password'), {
		 equalToField: 'password'
	});
	
	
	Validation.add('validate-day', _('Insert correct date'), {
		 min : 1, // value must be at least
	   max : 31
	});
	
	Validation.add('validate-year', _('Insert correct date'), {
		 min : 1900 // value must be at least
		 //max : 1995
	});
	
	Validation.add('validate-year-min', _('You should have at least 14 years'), {
		 max : 2007 - 14 // value must be at least
		
	});
	
	Validation.add('validate-month', _('Insert correct date'), {
		 min : 1, // value must be at least
	     max : 12
	});
	
	if($('frmQuickRegister') != undefined) {
		var valid = new Validation('frmQuickRegister', {onSubmit:true, immediate : true});

		Event.observe('frmQuickRegister', 'submit', function(e) {

	 		var t = checkIfUserExists();
	 		//if(u) Event.stop(e);
	 		var r = valid.validate();
	 		//if(!r || t || countSpaces('username') > 0 ) Event.stop(e);
	 		if(!r || t) Event.stop(e);

		});
	}
	

	
	// added this focus feature on birthdate fields
	/*
	Event.observe('birthDay','keyup',function(){
		var valid = Validation.get('validate-day').test($F('birthDay'));
		var x = new String($('birthDay').value)
		if(valid && x.length ==2 ){
			$('birthMonth').focus()
		}
	})
	
	Event.observe('birthMonth','keyup',function(){
		var validm = Validation.get('validate-month').test($F('birthMonth'));
		var y = new String($('birthMonth').value)
		if(validm && y.length==2){
			$('birthYear').focus()
		}
	})
	
	Event.observe('birthDay','focus',function(){
		if($F('birthDay')== "TT"){
			$('birthDay').value=""
		}
	})
	Event.observe('birthMonth','focus',function(){
		if($F('birthMonth')== "MM"){
			$('birthMonth').value=""
		}
	})
	Event.observe('birthYear','focus',function(){
		if($F('birthYear')== "JJJJ"){
			$('birthYear').value=""
		}
	})	
	*/