var Registration = Class.create();

Registration.prototype = {

	initialize: function() {
		//temporary for ajax responses.
		this.registrationError = 0;
		document.observe("dom:loaded", function() {
			var inputs = $('regForm').select('input' ,'select');
			if(inputs) {
				inputs.each(function(item) {
					Event.observe(item, 'focus', function() {
						registration.inputFocus(item);
					});
					Event.observe(item, 'blur', function() {
						registration.inputBlur(item);
					});		
				});
			}
		});
	},

	checkFirstAndLastName: function(checkwhat) {
		pattern = /[0-9_+=\[\]{}\\\'\"\:\;?/\<\,\>\.*|]+/i;
			if ($F(checkwhat).match(pattern)) {
				$(checkwhat+'Error').innerHTML = eval(checkwhat+'ErrorText');
				return false;
			} else {
				$(checkwhat+'Error').innerHTML = '';
				return true;
			}
	},
	checkGender: function() {
		if ($('genderm').checked == true || $('genderm').checked == true) {
			$('genderError').innerHTML = '';
			return true;
		} else {
			$('genderError').innerHTML = genderErrorText;
			return false;
		}
	},
	checkAge: function() {
		if($F('year') != 0 && $F('month') != 0 && $F('day') != 0) {
			$('birthdayError').innerHTML = '';
			return true;
		} else {
			$('birthdayError').innerHTML = birthdayErrorText;
			return false;
		}
	},
	checkLocation: function(defaultCountry) {
		if($F('country') != 0) {
			if($F('country') == defaultCountry) {
				 if($F('city') > 0) {
				 	$('cityError').innerHTML = '';
				 	$('countryError').innerHTML = '';
					return true;
				 } else {
				 	$('cityError').innerHTML = cityErrorText;
				 	$('countryError').innerHTML = '';
					return false;
				 }
			} else {
				$('cityError').innerHTML = '';
				return true;
			}
		} else {
			$('countryError').innerHTML = countryErrorText;
			return false;
		}
	},
	checkEmail: function() {
		new Ajax.Request(PUBLIC_URL+'ajax-registration/check-email',
		{
			method: 'post',
			asynchronous: false,
			postBody: Object.toQueryString({ email: $F('email')}),
			onSuccess: function(transport) {
				pattern = /no-reg-error/;
				if(transport.responseText.match(pattern)) {
					registration.registrationError = 0;
					$('emailError').innerHTML = '';
				} else {
					registration.registrationError = 1;
					$('emailError').innerHTML = transport.responseText;
				}
			}
		});

		if (this.registrationError == 0) {
			return true;
		} else {
			return false;
		}
	},

	goStep2: function(defaultCountry) {
		error = false;
		if (!this.checkEmail()) error = true;
		if (!this.checkFirstAndLastName()) error = true;
		if (!this.checkGender()) error = true;
		if (!this.checkAge()) error = true;
		if (!this.checkLocation(defaultCountry)) error = true;

		if(!error) {
			$('step1Container').style.display = 'none';
			$('step2Container').style.display = 'block';
		} else {
			return false;
		}
	},

	checkUsername: function() {
		tmp = 0;
		pattern1 = /^[a-z]+[a-z\d_]*\.?[a-z\d_]*[a-z\d]+$/i;
		pattern2 = /[._]{2,}/i;

		if ($F('username') != '') {
			if ($F('username').match(pattern1) && !$F('username').match(pattern2) && $F('username').length >= 4 && $F('username').length <= 32) {
				//good syntax, let's check db.
				new Ajax.Request(PUBLIC_URL+'ajax-registration/check-username',
				{
					method: 'post',
					asynchronous: false,
					postBody: Object.toQueryString({ username: $F('username')}),
					onSuccess: function(transport) {
						pattern = /no-reg-error/;
						if(transport.responseText.match(pattern)) {
							registration.registrationError = 0;
							$('usernameError').innerHTML = '';
						} else {
							registration.registrationError = 1;
							$('usernameError').innerHTML = transport.responseText;
						}
					}
				});

				if (this.registrationError == 0) {
					return true;
				} else {
					return false;
				}
			} else {
				//bad username syntax
				$('usernameError').innerHTML = usernameBadSyntaxErrorText;
				return false;
			}
		} else {
			$('usernameError').innerHTML = usernameEmptyErrorText;
			return false;
		}
	},

	checkPassword: function() {
		if ($F('password').length < 6) {
			$('passwordError').innerHTML = passwordLengthErrorText;
			return false;
		}
		$('passwordError').innerHTML = '';
	},
	showHint: function() {
		$('usernameHint').toggle();
	},

	inputFocus: function(element) {
		if(element.id.startsWith('birth')) {
			$('birthDateHelper').show();
		} else if($(element.id + 'Helper')) {
			$(element.id + 'Helper').show();
		}
		element.addClassName('reg_form_list_focus');
	},
	
	inputBlur: function(element) {
		if(element.id.startsWith('birth')) {
			$('birthDateHelper').hide();
		} else if($(element.id + 'Helper')) {
			$(element.id + 'Helper').hide();
		}
		element.removeClassName('reg_form_list_focus');
	},
	showCountry: function(abroad) {
		if($F('city') == abroad) {
			$('countryList').show();
		} else {
			$('countryList').hide();
		}
	},
	showCompanyForm: function() {
		$('headPers').hide(); $('headBiz').show(); 
		$('pGenderM').hide(); $('cGenderM').show();
		$('pGenderF').hide(); $('cGenderF').show();
		$('contactPerson').show();
        $('justPerson').hide();
		$('bizForm').show();
		$('regType').value = 'biz';
	},
	showUserForm: function() {
		$('headPers').show(); $('headBiz').hide(); 
		$('pGenderM').show(); $('cGenderM').hide();
		$('pGenderF').show(); $('cGenderF').hide();
		$('contactPerson').hide();
		$('justPerson').show();
		$('bizForm').hide();
		$('regType').value = 'pers';
	},
	finish: function() {
		
	}
}

var registration = new Registration();