var Registration = Class.create();

Registration.prototype = {

	initialize: function() {
		//temporary for ajax responses.
		this.registrationError = 0;
	},

	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;
		}
	},

	checkBjEmail: function() {
		new Ajax.Request(PUBLIC_URL+'ajax-registration/check-email',
		{
			method: 'post',
			asynchronous: false,
			postBody: Object.toQueryString({ email: $F('regEmail'), jobs: 1}),
			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;
					document.getElementById('emailRowContainer').style.height="50px";
				}
			}
		});

		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 = usernameAvailableText;
							$('usernameError').addClassName('f_green');
						} else {
							registration.registrationError = 1;
							$('usernameError').innerHTML = transport.responseText;
							$('usernameError').removeClassName('f_green');
							$('usernameHint').style.display = 'inline';
						}
					}
				});

				if (this.registrationError == 0) {
					return true;
				} else {
					return false;
				}
			} else {
				//bad username syntax
				$('usernameError').innerHTML = usernameBadSyntaxErrorText;
				$('usernameHint').style.display = 'inline';
				$('usernameError').removeClassName('f_green');
				return false;
			}
		} else {
			$('usernameError').innerHTML = usernameEmptyErrorText;
			$('usernameHint').style.display = 'inline';
			$('usernameError').removeClassName('f_green');
			return false;
		}


	},

	checkPassword: function() {
		if ($F('regPassword').length < 6) {
			$('passwordError').innerHTML = passwordLengthErrorText;
			return false;
		} else if ($F('regPassword') != $F('confirmPassword')) {
			$('passwordError').innerHTML = '';
			$('confirmPasswordError').innerHTML = passwordMatchErrorText;
			return false;
		} else {
			$('passwordError').innerHTML = '';
			$('confirmPasswordError').innerHTML = '';
			return true;
		}
	},

	showHint: function() {
		$('usernameHint').toggle();
	},

	finish: function() {
		console.log(this.checkUsername());
	}
}

var registration = new Registration();

function focusinput(id, focus)
{
	if (focus == 1)
		id.style.backgroundColor = '#F1FCDC';
	else
		id.style.backgroundColor = '#FFFFFF';
}

function checkNickAvailibility()
{
	$('nickAvailibilityText').innerHTML = '';
	//	$('sendingProgress').style.display = 'block';
		new Ajax.Request(PUBLIC_URL+'ajax/check-nick-availibility',
		{
			method: 'post',
			postBody: Object.toQueryString({ username: $F('username')}),
			onSuccess: function(transport) {
//				$('sendingProgress').style.display = 'none';
				$('nickAvailibilityText').innerHTML = transport.responseText;
			}
		});
}

function checkRegistrationForm()
{
	var form = document.forms.registration;

	if (form.username)
	{
		var username = form.username.value;

		if(username.length < 4 || username.length > 32 || username.length < 4 || !username.match(/^[a-z]+[a-z\d_]*\.?[a-z\d_]*[a-z\d]+$/i) || username.match(/[._]{2,}/i))
		{
			alert(form.errorUsername.value);
			form.username.focus();
			return false;
		}

	}

	if(form.password.value.length < 6 || form.password.value.length > 20)
	{
		alert(form.errorPass.value);
		form.password.focus();
		return false;
	}
	if(form.password.value != form.password2.value)
	{
		alert(form.errorPass2.value);
		form.password2.focus();
		return false;
	}
	filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	//if(form.email.value =='' || !filter.test(trimAll(form.email.value)))
	tmp = form.email.value.match(filter);
	if((tmp==null) || tmp[0]!=form.email.value)
	{
		alert(form.errorEmail.value);
		form.email.focus();
		return false;
	}
	if(form.firstName.value == '')
	{
		alert(form.errorName.value);
		form.name.focus();
		return false;
	}

	if(form.lastName.value == '')
	{
		alert(form.errorName.value);
		form.name.focus();
		return false;
	}

	if (form.height.value != '')
	{
		if(!form.height.value.match(/^\d*$/i) || form.height.value < 0 || form.height.value > 255)
		{
			alert(form.errorHeight.value);
			form.height.focus();
			return false;
		}
	}
	return true;
}

function checkFirstpage(from) {
	if ($('recaptcha_response_field') == undefined) {
		var captcha = '';
	} else {
		var captcha = $F('recaptcha_response_field');
	}

	if ($('hidden') == undefined) {
		var captcha_key = '';
	} else {
		var captcha_key = $F('hidden');
	}

	if ($('companyFiscalCodePrefix') == undefined) {
		companyFiscalCodePrefix = '';
	} else {
		companyFiscalCodePrefix = $F('companyFiscalCodePrefix');
	}
	if (from == 'firstpage') {
		document.registrationForm.submit();
	} else {
		if($F('site') == 'jobs')
		{
			$('bjreg_centerbtn').innerHTML = plsWait;
			document.getElementById('bjreg_centerbtn').onclick = function(){ };
			new Ajax.Request(PUBLIC_URL+'ajax-registration/check-registration',
				{
					method: 'post',
					evalJS: 'force',
					postBody: Object.toQueryString({
						accountType: $F('accountType'),
						firstName: $F('firstName'),
						lastName: $F('lastName'),
						gender: $RF('registrationForm','gender'),
						email: $F('regEmail'),
						username: $F('username'),
						day: $F('day'),
						month: $F('month'),
						year: $F('year'),
						country: $F('country'),
						city: $F('city'),
						password: $F('regPassword'),
						//confirmPassword: $F('confirmPassword'),
						captcha: captcha,
						captcha_key: captcha_key,
						from: from,
						companyName: $F('companyName'),
						//companyDescription: $F('companyDescription'),
						companyAddress: $F('companyAddress'),
						companyFiscalCodePrefix: companyFiscalCodePrefix,
						companyFiscalCode: $F('companyFiscalCode'),
						companyEmplayeeNumber: $F('companyEmplayeeNumber'),
						companyDomainOfActivity: $F('companyDomainOfActivity'),
						companyTelephone: $F('companyTelephone'),
						//companyFax: $F('companyFax'),
						//companyHomepage: $F('companyHomepage'),
						site: $F('site'),
						part: $F('part')
					}),
					onSuccess: function(transport) {
						//
					}
				});
		}
		else
		new Ajax.Request(PUBLIC_URL+'ajax-registration/check-registration',
				{
					method: 'post',
					evalJS: 'force',
					postBody: Object.toQueryString({
						accountType: $RF('registrationForm','accountType'),
						firstName: $F('firstName'),
						lastName: $F('lastName'),
						gender: $RF('registrationForm','gender'),
						email: $F('regEmail'),
						username: $F('username'),
						day: $F('day'),
						month: $F('month'),
						year: $F('year'),
						country: $F('country'),
						city: $F('city'),
						password: $F('regPassword'),
						confirmPassword: $F('confirmPassword'),
						captcha: captcha,
						captcha_key: captcha_key,
						from: from,
						companyName: $F('companyName'),
						companyDescription: $F('companyDescription'),
						companyAddress: $F('companyAddress'),
						companyFiscalCodePrefix: companyFiscalCodePrefix,
						companyFiscalCode: $F('companyFiscalCode'),
						companyEmplayeeNumber: $F('companyEmplayeeNumber'),
						companyDomainOfActivity: $F('companyDomainOfActivity'),
						companyTelephone: $F('companyTelephone'),
						companyFax: $F('companyFax'),
						companyHomepage: $F('companyHomepage'),
						site: $F('site'),
						part: $F('part')
					}),
					onSuccess: function(transport) {
						//
					}
				});
	}

}