function $FL(name){
	var dd = $(name);
	return dd.options[dd.selectedIndex].text;
}
var Registration = Class.create({
	initialize : function() {
		this.step = 1;
		this.companyFields = 	['registration_company_tin',
								 'registration_company_name',
								 'registration_company_street',
								 'registration_company_city',
								 'registration_company_zip',
								 'registration_company_phone',
								 'registration_company_fax'
								 ];
		this.email_exists = false;
		this.login_exists = false;
	},
	validateText : function(name){
		var valid = true;
		if($F(name) == '')
		{
			valid = false;
			$(name + '_validator').style.display = 'inline';
		}
		else
		{
			$(name + '_validator').style.display = 'none';
		}
		return valid;
	},
	validateDropdown : function(name){
		try
		{
			var valid = true;
			if($F(name) == 0)
			{
				valid = false;
				$(name + '_validator').style.display = 'inline';
			}
			else
			{
				$(name + '_validator').style.display = 'none';
			}
			return valid;
		}
		catch(e)
		{
			//alert(name);
			//alert(e.message);
		}
	},
	validateCheckbox : function(name){
		var valid = true;
		if($F(name) == null)
		{
			valid = false;
			$(name + '_validator').style.display = 'inline';
		}
		else
		{
			$(name + '_validator').style.display = 'none';
		}
		return valid;
	},
	validateEmail : function(name){
		var email = $F(name);
		if(email != '')
		{
			$(name + '_validator').style.display = 'none';
			var regexp = new RegExp("^\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$", "i");
			//var regexp = new RegExp("[A-Za-z_]+([-+.'][A-Za-z_]+)*@[A-Za-z_]+([-.][A-Za-z_]+)*\\.[A-Za-z_]+([-.][A-Za-z_]+)*", "i");
			var valid = regexp.test($F(name));
			if(valid)
			{
				$(name + '_invalid').style.display = 'none';
			}
			else
			{
				$(name + '_invalid').style.display = 'inline';
			}
		}
		else
		{
			$(name + '_validator').style.display = 'inline';
			var valid = false;
		}

		return valid;
	},
	validateNIP : function(name){
		var nip = $F(name);
		if(nip != '')
		{
			$(name + '_validator').style.display = 'none';
			var regexp = new RegExp("^[0-9]{10}$");
			var valid = regexp.test($F(name));
			if(valid)
			{
				$(name + '_validator').style.display = 'none';
			}
			else
			{
				$(name + '_validator').style.display = 'inline';
			}
		}
		else
		{
			$(name + '_validator').style.display = 'inline';
			var valid = false;
		}
		return valid;
	},
	nextStep : function() {
		if(this.step <= 4)
		{
			this['step' + (this.step + 1)]();
		}
	},
	previousStep : function(){

		if(this.step >= 1)
		{
			this['step' + (this.step - 1)]();
		}
	},
	jumpTo : function(step){
		this['step' + step]();
	},
	resetTabs: function(){

	},
	checkStep1 : function(){
		var valid = true;
		var textboxes =['registration_name','registration_surname'];
		var dropdowns = ['registration_country','registration_position','registration_company_region'];
		for(var i = 0; i < textboxes.length; i++)
		{
			var local_valid = this.validateText(textboxes[i]);
			if(!local_valid) valid = false;
		}
		for(var i = 0; i < dropdowns.length; i++)
		{
			var local_valid = this.validateDropdown(dropdowns[i]);
			if(!local_valid) valid = false;
		}
		var local_valid = this.validateEmail('registration_email');
		if(!local_valid) valid = false;

		/*
		this.email_exists = false;
		if(local_valid)
		{
			new Ajax.Request('/ajax/check-email',{
				method : 'post',
				parameters : {
					email : $F('registration_email')
				},
				asynchronous : false,
				onSuccess : function(transport){
					try{
						var res = transport.responseText;
						if(res == 1){
							$('registration_email_validator').style.display = 'inline';
							this.email_exists = true;
						}
						else
						{
							this.email_exists = false;
						}
					}catch(e){
						alert(e);
					}
				}.bind(this)
			});
		}
		if(this.email_exists) valid = false;
		*/
		return valid;
	},
	checkStep2 : function(){
		var valid = true;
		var textboxes =['registration_company_tin','registration_company_name','registration_company_city','registration_company_zip','registration_company_street','registration_company_phone'];
		var dropdowns = [];
		for(var i =0; i < textboxes.length; i++)
		{
			var local_valid = this.validateText(textboxes[i]);
			if(!local_valid) valid = false;
		}

		for(var i =0; i < dropdowns.length; i++)
		{
			var local_valid = this.validateDropdown(dropdowns[i]);
			if(!local_valid) valid = false;
		}
		var validate_tin = true;

		local_valid = this.validateNIP('registration_company_tin');
		if(!local_valid)
		{
			valid = false;
			validate_tin = false;
		}

		if(validate_tin)
		{
			this.tin_exists = false;
			this.prompt = tin_already_exists;
			if(local_valid)
			{
				new Ajax.Request('/ajax/check-tin',{
					method : 'post',
					parameters : {
						tin : $F('registration_company_tin')
					},
					asynchronous : false,
					onSuccess : function(transport){
						try{
							var res = transport.responseText;
							if(res == 1){
								if(confirm(this.prompt))
								{
									this.tin_exists = false;
								}
								else
								{
									$('registration_company_tin').value = '';
									this.tin_exists = true;
								}
							}
							else
							{
								this.tin_exists = false;
							}
						}catch(e){
							alert(e);
						}
					}.bind(this)
				});
			}
			if(this.tin_exists) valid = false;
		}
		return valid;
	},
	checkStep3 : function(){
		var valid = true;
		var textboxes =['registration_login','registration_password','registration_password2'];
		var dropdowns = ['registration_information_channel'];
		var checkboxes = ['registration_rules_acceptance','registration_policies_acceptance'];
		for(var i =0; i < textboxes.length; i++)
		{
			var local_valid = this.validateText(textboxes[i]);
			if(!local_valid) valid = false;
		}
		for(var i =0; i < dropdowns.length; i++)
		{
			var local_valid  = this.validateDropdown(dropdowns[i]);
			if(!local_valid) valid = false;
		}
		for(var i =0; i < checkboxes.length; i++)
		{
			var local_valid = this.validateCheckbox(checkboxes[i]);
			if(!local_valid) valid = false;
		}
		if($F('registration_password') != $F('registration_password2'))
		{
			$('registration_password_validator').style.display = 'inline';
			$('registration_password2_validator').style.display = 'inline';
			valid = false;
		}
		var distributors = $$('.distributor_checkbox');
		var valid_distributors = false;

		for(var i = 0;i < distributors.length; i++){
			if(distributors[i].checked)
			{
				valid_distributors = true;
			}
		}
		if(!valid_distributors){
			$('registration_distributors_validator').style.display = 'inline';
		}
		else
		{
			$('registration_distributors_validator').style.display = 'none';
		}

		this.email_exists = false;
		if(local_valid)
		{
			new Ajax.Request('/ajax/check-login',{
				method : 'post',
				parameters : {
					login : $F('registration_login')
				},
				asynchronous : false,
				onSuccess : function(transport){
					try{
						var res = transport.responseText;
						if(res == 1){
							$('registration_login_validator').style.display = 'inline';
							this.login_exists = true;
						}
						else
						{
							this.login_exists = false;
						}
					}catch(e){
						alert(e);
					}
				}.bind(this)
			});
		}
		if(this.login_exists) valid = false;

		return valid;
	},
	step1 : function(){
		$('step_1').style.display = 'block';
		$('step_2').style.display = 'none';
		$('step_3').style.display = 'none';
		$('step_4').style.display = 'none';
		this.step = 1;
		return true;
	},
	step2 : function(){
		var proceed = false;
		if(this.step < 2)
		{
			proceed = this['checkStep' + this.step]();
		}
		else
		{
			proceed = true;
		}
		if(proceed)
		{
			$('step_1').style.display = 'none';
			$('step_2').style.display = 'block';
			$('step_3').style.display = 'none';
			$('step_4').style.display = 'none';
			this.step = 2;
		}
		return proceed;
	},
	step3 : function(){
		var proceed = false;
		if(this.step < 3)
		{
			proceed = this['checkStep' + this.step]();
		}
		else
		{
			proceed = true;
		}
		if(proceed)
		{
			$('step_1').style.display = 'none';
			$('step_2').style.display = 'none';
			$('step_3').style.display = 'block';
			$('step_4').style.display = 'none';
			this.step = 3;
		}
		return proceed;
	},
	step4 : function(){
		var proceed = false;
		if(this.step < 4)
		{
			proceed = this['checkStep' + this.step]();
		}
		else
		{
			proceed = true;
		}

		if(proceed)
		{
			this.updateSummary();
			$('step_1').style.display = 'none';
			$('step_2').style.display = 'none';
			$('step_3').style.display = 'none';
			$('step_4').style.display = 'block';
			this.step = 4;
		}
		return proceed;
	},
	step5 : function(){
		document.registration_form.submit();
	},
	clearStep2 : function(){

		this.clearCompanyFields();
		this.enableCompanyFields();

	},
	updateSummary : function (){
		// step1

		$('registration_summary_country').update($FL('registration_country'));
		$('registration_summary_name').update($F('registration_name'));
		$('registration_summary_surname').update($F('registration_surname'));
		$('registration_summary_email').update($F('registration_email'));
		$('registration_summary_phone').update($F('registration_phone'));
		$('registration_summary_mobile').update($F('registration_mobile'));
		$('registration_summary_position').update($FL('registration_position'));

		// step 2
		$('registration_summary_company_tin').update($F('registration_company_tin'));
		$('registration_summary_company_name').update($F('registration_company_name'));
		$('registration_summary_company_street').update($F('registration_company_street'));
		$('registration_summary_company_city').update($F('registration_company_city'));
		$('registration_summary_company_zip').update($F('registration_company_zip'));
		$('registration_summary_company_region').update($FL('registration_company_region'));
		$('registration_summary_company_phone').update($F('registration_company_phone'));
		$('registration_summary_company_fax').update($F('registration_company_fax'));
		$('registration_summary_position').update($FL('registration_position'));

		// step 3
		$('registration_summary_login').update($F('registration_login'));
		$('registration_summary_password').update($F('registration_password'));

		$('registration_summary_information_channel').update($FL('registration_information_channel'));
		var distributors = $$('.distributor_checkbox');
		var distributors_list = '';
		for(var i = 0;i < distributors.length; i++){
			if(distributors[i].checked)
			{
				if(distributors_list == '')
				{
					distributors_list += distributors[i].getAttribute('distributor_name');
				}
				else
				{
					distributors_list += ', ' + distributors[i].getAttribute('distributor_name');
				}
			}
		}
		$('registration_summary_distributors').update(distributors_list);
	},
	disableCompanyFields : function(){
		for(var k = 0; k < this.companyFields.length; k++){
			$(this.companyFields[k]).readOnly = true;
			$(this.companyFields[k]).style.borderWidth = '0px';
		}
	},
	clearCompanyFields : function(){
		for(var k = 0; k < this.companyFields.length; k++){
			$(this.companyFields[k]).value = '';
		}
	},
	enableCompanyFields : function(){
		for(var k = 0; k < this.companyFields.length; k++){
			$(this.companyFields[k]).readOnly = false;
			$(this.companyFields[k]).style.borderWidth = '1px';
		}
	},
	sendPdf : function(uid){
		new Ajax.Request('/index/send-pdf',{
			method : 'post',
			parameters : { user_id : uid },
			onSuccess : function(transport)
			{
				alert('Formularz zostal wyslany - sprawdz poczte.');
			}
		});
	}
});
var registration = new Registration();


//zwraca nacisniety klawisz
//dziala pod IE i N
function getkey(e){
 if (window.event)
    return window.event.keyCode;
 else if (e)
    return e.which;
 else
    return null;
}



function int_valid(e){
 var key, keychar;

 //pobranie znaku
 key = getkey(e);
 if (key == null) return true;

 // klawiesze kontrolne i funkcyjne
 if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
   return true;

 // pobierz znak
 keychar = String.fromCharCode(key);
//alert(e.currentTarget.value);
//alert(e.target.value);
 temp=e.currentTarget.value;
 if (temp.length==9) return false;
 // wzorzec inta
 //int_expr = /[0-9]/;
 int_expr = /^\d$/;
 //dopasowanie do wzorca inta
 if(int_expr.test(keychar))
  return true;

 // nic nie podpasowalo wiec zwroc false
 return false;
}



function kod_valid(e,obj){
 var key, keychar;
 var temp_kod;
 //pobranie znaku
 key = getkey(e);
 if (key == null) return true;

// klawiesze kontrolne i funkcyjne
 if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
   return true;

 // pobierz znak
 keychar = String.fromCharCode(key);

 temp_kod=obj.value+keychar;

 if (temp_kod.length==7) return false;

 //kod_expr = /[\d][\d]?[-]?[\d]?[\d]?[\d]?[\d]?/;

 //if(kod_expr.test(temp_kod))
 	//alert('ok');

 //proba dodania 2. znaku
 if(obj.value.length==2){
  obj.value=obj.value+'-';
 }


 // wzorzec inta
 //int_expr = /[0-9]/;
 int_expr = /[\d]/;//
 //dopasowanie do wzorca inta
 if(int_expr.test(keychar))
  return true;


 // nic nie podpasowalo wiec zwroc false
 return false;
}

function nip_valid(e,obj){
 var key, keychar;
 var temp_data;
$('registration_company_tin_invalid').style.display='none';
 //wzorce

 // wzorzec inta
 //int_expr = /[0-9]/;
 int_expr = /^[\d]$/;//



 //pobranie znaku
 key = getkey(e);
 if (key == null) return true;

// klawiesze kontrolne i funkcyjne
 if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
   return true;

 // pobierz znak
 keychar = String.fromCharCode(key);

 if(obj.value.length==10) return false;
 if(!int_expr.test(keychar)) return false;



 if(obj.value.length==9){
	var suma;

	suma  = 6*obj.value.charAt(0);
	suma += 5*obj.value.charAt(1);
	suma += 7*obj.value.charAt(2);
  	suma += 2*obj.value.charAt(3);
	suma += 3*obj.value.charAt(4);
	suma += 4*obj.value.charAt(5);
	suma += 5*obj.value.charAt(6);
	suma += 6*obj.value.charAt(7);
	suma += 7*obj.value.charAt(8);

	if(keychar!=(suma%11))
	{
		$('registration_company_tin_invalid').style.display='inline';
		return false;
	}

	return true;
 }

 return true;
}