	function isRegClient() {
		return $('#reg_client:checked').length;
	}
	function regSwitched(obj)
	{
		var isclient = isRegClient();
		
		$("#reg p").hide();
		
		$('#reg input').each(function(){
			if($(this).attr("#fillvalue") > "") {
			
				if(this.id == "user_password" || this.id == "user_password2" || this.id == "pwd_auth") {
					$("#"+this.id).val("");
					$("#"+this.id).hide();
					$("#p"+this.id).show();
				};
			
				$(this).val($(this).attr("#fillvalue"));
			}
		});
		
		if(!isclient) {
			$('#reg .partner_adds').show();
			$('#reg .client_adds').hide();
		}
		else {
			$('#reg .partner_adds').hide();
			$('#reg .client_adds').show();
		}
		$('#captcha_reg_img').attr('src', '/partner/captcha.asp?type=captcha-reg&reload=1&t='+(new Date()).getTime())
	}
	function doRegister()
	{
		var isclient = isRegClient();
		//var checkFlds = "#user_login,#user_password,#user_password2,#user_email,#captcha_reg_inp"
		//if(isclient) checkFlds += "" //",#user_first_name,#user_last_name"
		//else checkFlds += ",#partner_url"
		var err = 0;
		var info = {act:'register'};
		// $(checkFlds).each(function(){
			// var obj = $(this);
			// if(obj.val() == '' || obj.val() == obj.attr("#fillvalue")) {
				// showErr(obj.attr('id'));
				// err = 1;
			// } else {
				// $('#err_'+obj.attr('id')).hide();
			// }
			// info[obj.attr('id')] = dec(obj.val());
		// });
		
		if($('#user_login').val() == '' || $('#user_login').val() == $('#user_login').attr("#fillvalue")) {
			//showErr(obj.attr('id'));
			displayErrorMessage("err_user_login", "err_missed_login");
			err = 1;
		} else {
			$('#err_user_login').hide();
			info[$('#user_login').attr('id')] = dec($('#user_login').val());
		}
		
		if($('#user_password').val() == '' || $('#user_password').val() == $('#user_password').attr("#fillvalue")) {
			//showErr(obj.attr('id'));
			displayErrorMessage("err_user_password", "err_missed_password");
			err = 1;
		} else {
			$('#err_user_password').hide();
			info[$('#user_password').attr('id')] = dec($('#user_password').val());
		}
		
		if($('#user_password2').val() != $('#user_password').val()) {
			showErr('user_password2', errosObj["err_password2"]);
			err = 1;
		}
		else {
			$('#err_user_password2').hide();
			info[$('#user_password2').attr('id')] = dec($('#user_password2').val());
		}

		if($('#user_email').val() == '' || $('#user_email').val() == $('#user_email').attr("#fillvalue")) {
			//showErr(obj.attr('id'));
			displayErrorMessage("err_user_email", "err_missed_email");
			err = 1;
		} else if(!checkEmail($('#user_email').val())) {
			displayErrorMessage("err_user_email", "err_email");
			err = 1;
		} else {
			$('#err_user_email').hide();
			info[$('#user_email').attr('id')] = dec($('#user_email').val());
		}

		if($('#captcha_reg_inp').val() == '' || $('#captcha_reg_inp').val() == $('#captcha_reg_inp').attr("#fillvalue")) { 
				displayErrorMessage("err_captcha_reg", "err_missed_captcha");
				err = 1;
		} else {
			info['captcha_reg'] =  dec($('#captcha_reg_inp').val())
			$('#err_captcha_reg').hide();
		}
		info['validation_code'] = $('#validation_code').val()
	
		// if(!checkEmail($('#user_email').val())) {
			// showErr('user_email', errosObj["err_email"]);
			// err = 1;
		// } else {
			// $('#err_user_email').hide();
		// }
		if(!isclient) {
			if(!$('#terms:checked').length) {
				showErr('terms', errosObj["err_terms"]);
				err = 1;
			}
			else {
				$('#err_terms').hide();
			}
			
			info['partner_url'] = '';		
			if($('#partner_url').val() == '' || $('#partner_url').val() == $('#partner_url').attr("#fillvalue")) {
				displayErrorMessage("err_partner_url", "err_missed_partner_url");
				err = 1;
			} else {
				$('#err_partner_url').hide();
				info['partner_url'] = dec($('#partner_url').val());
			}
		
		} else {
		
			info['user_first_name'] = '';
			info['user_last_name'] = '';
			
			/*
			if($('#user_first_name').val() == '' || $('#user_first_name').val() == $('#user_first_name').attr("#fillvalue")) {
				displayErrorMessage("err_user_first_name", "err_missed_first_name");
				err = 1;
			} else {
				//if( $('#user_first_name').val() != $('#user_first_name').attr('defaultValue')) {
				$('#err_user_first_name').hide();
				info['user_first_name'] = dec($('#user_first_name').val());
			}
			
			if($('#user_last_name').val() == '' || $('#user_last_name').val() == $('#user_last_name').attr("#fillvalue")) {
				displayErrorMessage("err_user_last_name", "err_missed_last_name");
				err = 1;
			} else {
			//if( $('#user_last_name').val() != $('#user_last_name').attr('defaultValue')) {
				$('#err_user_last_name').hide();
				info['user_last_name'] = dec($('#user_last_name').val());
			}*/
			
			if($('#user_first_name').val() != $('#user_first_name').attr("#fillvalue"))
				info['user_first_name'] = dec($('#user_first_name').val());
			if($('#user_last_name').val() == $('#user_last_name').attr("#fillvalue"))
				info['user_last_name'] = dec($('#user_last_name').val());
		}

		if(!err) {
			$.post(isclient?uurl:purl, info, function(data) {
				var obj;
				eval('obj='+data);
				for(i in obj.err) {showErr(i,obj.err[i]);}
				if(obj.success) {
					document.location.href=isclient?userURL:partnerURL+'partner_banners.asp'
				} else {
					$('#captcha_reg_img').attr('src', '/partner/captcha.asp?type=captcha-reg&reload=1&t='+(new Date()).getTime())
				}
			});
		}
		return false;
	}
	function checkEmail(el){
	    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	    return filter.test(el)
	};
	
	function dec(str) {
		var s = '';
		for(i=0; i<str.length;i++) {
			s += '%'+str.charCodeAt(i);
		}
		return s;
	}
	
	
	function isLogingClient() {
		return $('#auth_client:checked').length;
	}
	function loginSwitched(obj)
	{
		var isclient = isLogingClient();
		$("#auth p").hide();
		
		$('#auth input').each(function(){
			if($(this).attr("#fillvalue") > "") {
			
				if(this.id == "user_password" || this.id == "user_password2" || this.id == "pwd_auth") {
					$("#"+this.id).val("");
					$("#"+this.id).hide();
					$("#p"+this.id).show();
				};
			
				$(this).val($(this).attr("#fillvalue"));
			}
		});
		
		if(!isclient) {
			$('#auth .partner_adds').show();
			$('#auth .client_adds').hide();
		}
		else {
			$('#auth .partner_adds').hide();
			$('#auth .client_adds').show();
		}
	}
	
	/*
		{{{ 
			методы частично дублируют логику
			исправлять пока нет времени
	*/
	function showErr(id,str) {
		if(str) {
			$('#err_'+id).attr('em', $('#err_'+id).html()).html(str);
		}
		else if($('#err_'+id).attr('em')) {
			$('#err_'+id).html($('#err_'+id).attr('em'));
		}
		$('#err_'+id).show();
	}
	
	function displayErrorMessage(errContainerId, errMessageKey)
	{
		$('#'+errContainerId).show();
		$('#'+errContainerId).text(errosObj[errMessageKey]);
	}
	/*
		}}}
	*/
	function doLogin()
	{
			var isclient = isLogingClient();
			var err = 0;
			var ur = uurl;
			if($('#login_auth').val() == '' || $('#login_auth').val() == $('#login_auth').attr("#fillvalue")) {
				//$('#err_login_auth').show(); 
				
				displayErrorMessage("err_login_auth", "err_missed_login");
				
				err = 1;
			}
			else {
				$('#err_login_auth').hide();
			}
			if($('#pwd_auth').val() == '' || $('#pwd_auth').val() == $('#pwd_auth').attr("#fillvalue")) { 
				//$('#err_pwd_auth').show();
				displayErrorMessage("err_pwd_auth", "err_missed_password");
				err = 1;
			}
			else {
				$('#err_pwd_auth').hide();
			}
			if(!isclient) 
			{	
				var ur = purl
				if($('#captcha_auth').val() == '' || $('#captcha_auth').val() == $('#captcha_auth').attr("#fillvalue")) { 
					//$('#err_captcha_auth').show();
					displayErrorMessage("err_captcha_auth", "err_missed_captcha");
					err = 1;
				}
				else $('#err_captcha_auth').hide()
			}
			
			if(!err) {
				$.get(ur, {login_auth:$('#login_auth').val(),pwd_auth:$('#pwd_auth').val(),captcha_auth:$('#captcha_auth').val(),remember:$('#remember:checked').length}, function(data){
					var obj
					eval("obj="+data)
					if(obj.success) {
						if(isclient) {
							document.location.href='/';
						} else { 
							document.location.href=partnerURL+'partner_statistics.asp';
						}
					} else {
						$('#partner_captcha').attr('src', '/partner/captcha.asp?reload=' + getRnd())
						if(!isclient) 
						{
							if(obj.captcha_auth) {
								//$('#err_captcha_auth').show();
								displayErrorMessage("err_captcha_auth", "err_captcha");
							} else {
								$('#err_captcha_auth').hide();
							}
						}
						if(obj.pwd_auth) {
							//$('#err_pwd_auth').show();
							displayErrorMessage("err_pwd_auth", "err_wrong_login");
						} else {
							$('#err_pwd_auth').hide();
						}
						if(obj.login_auth) {
							//$('#err_login_auth').show();
							displayErrorMessage("err_login_auth", "err_login");
						} else {
							$('#err_login_auth').hide();
						}
					}
				});
			}
		
		return false;
	}
	function getRnd() {
	    return new Date().getTime();
	}
