stop();

select_tab( vote_view );

select_confirm_screen( _root.ConfirmScreen );

function select_confirm_screen(confirm_screen:String){
	central_cell_block.RegistrationConfirmation._visible = false;
	central_cell_block.login_block._visible = false;
	central_cell_block.design_exists._visible = false;
	central_cell_block.design_is_save._visible = false;
	central_cell_block.design_is_save_again._visible = false;
	if ( confirm_screen == "RegistrationConfirmation" ) central_cell_block.RegistrationConfirmation._visible = true;
	if ( confirm_screen == "Registration" ) central_cell_block.login_block._visible = true;
	if ( confirm_screen == "DesignExists" ) central_cell_block.design_exists._visible = true;
	if ( confirm_screen == "DesignIsSave" ) central_cell_block.design_is_save._visible = true;
	if ( confirm_screen == "DesignIsSaveAgain" ) central_cell_block.design_is_save_again._visible = true;
	_root.ConfirmScreen = "";
	}



function SaveDesign(design_params, UserId, design_title, rewrite_mode){
	
	var save_design:LoadVars = new LoadVars();
	save_design.UserId = UserId;
	save_design.Design = design_params;
	save_design.DesignTitle = escape(design_title);
	save_design.RewriteMode = rewrite_mode;
	save_design.sendAndLoad("flash_action.php?action=design_save", save_design, "POST");

	save_design.onLoad = function(success) {
		if (success) {
			if ( this.result == "OK" ){
				_root.UserHasDesign = true;
				if ( rewrite_mode == 2 ) select_confirm_screen( "DesignIsSaveAgain" );
				else select_confirm_screen( "DesignIsSave" );
			} else if ( this.result == "DesignExists" ) {
				select_confirm_screen( "DesignExists" );				
			} else {
				select_confirm_screen( "Error" );	// poka takogo ekrana netu	
			}
		}	
	}
}



if ( memory_event == "save_design" ) {
	
	if ( is_login ){
		SaveDesign( _root.memory_design_params, UserId, design_title, 1);
	} else {
		select_confirm_screen( "Registration" );
	}
	
};



_root.typeContainer._visible=false;
_root.styleContainer._visible=false;

load_design_button._visible = false;
save_design_button._visible = false;

add_design_button._visible = false;
add_vote_button._visible = false;



/* Set Tab Index begin*/

_root.home_and_other_links.tabEnabled=false;
_root.alloc.tabEnabled=false;
_root.footer_logo.tabEnabled=false;
_root.sts.tabEnabled=false;

_root.central_cell_block.tabChildren = true;
_root.central_cell_block.tabEnabled = false;
_root.central_cell_block.login_block.tabChildren = true;
_root.central_cell_block.login_block.tabEnabled = false;
_root.central_cell_block.login_block.registration.tabChildren = true;
_root.central_cell_block.login_block.registration.tabEnabled = false;
_root.central_cell_block.login_block.registration.registration_form_field_firstname.tabChildren = true;
_root.central_cell_block.login_block.registration.registration_form_field_firstname.tabEnabled = false;
_root.central_cell_block.login_block.registration.registration_form_field_lastname.tabChildren = true;
_root.central_cell_block.login_block.registration.registration_form_field_lastname.tabEnabled = false;
_root.central_cell_block.login_block.registration.registration_form_field_email.tabChildren = true;
_root.central_cell_block.login_block.registration.registration_form_field_email.tabEnabled = false;

_root.central_cell_block.login_block.login_form_email.tabIndex = 1;
_root.central_cell_block.login_block.login_form_password.tabIndex = 2;
_root.central_cell_block.login_block.login_button.tabIndex = 3;

_root.central_cell_block.login_block.registration.registration_form_field_firstname.registration_form_firstname.tabIndex = 44;
_root.central_cell_block.login_block.registration.registration_form_field_lastname.registration_form_lastname.tabIndex = 45;
_root.central_cell_block.login_block.registration.registration_form_field_email.registration_form_email.tabIndex = 46;
_root.central_cell_block.login_block.registration.register_button.tabIndex = 48;

/* Set Tab Index end*/



function login_event(event_from:String){  
	// event from "registration", "login"

	is_login = true;
	DisposeImage();
						
	DesignsList = [];
	get_designs_list();
	
	set_user_login_string();  

	central_cell_block.login_block.registration.register_white.onEnterFrame = null;
	
	if ( memory_event == "save_design" ) {
		memory_event = "";
		param = memory_design_params;
		central_cell_block.login_block._visible = false;
		central_cell_block.design_is_save._visible = true;		
		SaveDesign( memory_design_params, _root.UserId, _root.design_title, 1);
		}		
	else if ( memory_event == "load_design" ) {
		memory_event = "";
		LoadDesign(_root.UserId);
		}
	else {
		memory_event = "";
		if ( event_from == "registration" ) {
			central_cell_block.login_block._visible = false;				
			central_cell_block.RegistrationConfirmation._visible = true;
			}
		if ( event_from == "login" ) LoadDesign(_root.UserId);
		}	
	}



_root.central_cell_block.login_block.login_button.onPress=function(){
	
		_root.central_cell_block.login_block.login_form_email_error.text = "";
		_root.central_cell_block.login_block.login_form_password_error.text = "";

		login_params = new LoadVars();
		login_params.Login = _root.central_cell_block.login_block.login_form_email.text;
		login_params.Password = _root.central_cell_block.login_block.login_form_password.text;
		
		login_params.sendAndLoad( "flash_action.php?action=login", login_params, "POST");
		
		login_params.onLoad = function(success) {
			if (success) {

				if ( this.result == "OK" ){

						_root.UserId = this.UserId; 
						_root.SessionId = this.SessionId;
						_root.FullName = this.FullName; //(First Name + Last Name) 
						_root.UserEmail = login_params.Login;
						
						login_event("login");

					} else {
						if ( this.LoginError != undefined ) _root.central_cell_block.login_block.login_form_password_error.text = this.LoginError;				
					}
				}
			}
			
	}








_root.central_cell_block.login_block.forgot_password.forgot_password_inner.onPress=function(){
	forgot_password();
	};
	
	
function forgot_password() {
	
		_root.central_cell_block.login_block.forgot_password.gotoAndStop(2);
		_root.central_cell_block.login_block.login_form_password_error.text = "";
		
		fg_password = new LoadVars();
		fg_password.Email = _root.central_cell_block.login_block.login_form_email.text;
		fg_password.sendAndLoad( "flash_action.php?action=forgot_password", fg_password, "POST");
		
		fg_password.onLoad = function(success) {
			if (success) {

				if ( this.result == "OK" ){

						_root.central_cell_block.login_block.forgot_password.gotoAndStop(3);

					} else {
						_root.central_cell_block.login_block.forgot_password.gotoAndStop(1);
						_root.central_cell_block.login_block.login_form_email.setFocus();
						if ( this.EmailError != undefined ) _root.central_cell_block.login_block.login_form_password_error.text = this.EmailError;				
					
						_root.central_cell_block.login_block.forgot_password.forgot_password_inner.onPress=function(){
							forgot_password();
							}					
					
					}
				}
			}
	};




central_cell_block.login_block.registration.register_button._visible = true;
central_cell_block.login_block.registration.register_white._visible = false;

_root.central_cell_block.login_block.registration.register_button.onPress=function(){
	registration_function();
}



function registration_function(){
	
	central_cell_block.login_block.registration.register_button._visible = false;
	central_cell_block.login_block.registration.register_white._visible = true;

	var time_counter:Number = 0;
	central_cell_block.login_block.registration.register_white.onEnterFrame = function(){
		time_counter++;
		if ( time_counter > 50 ){
			time_counter = 0;
			registration_function();
		}
	}	
	
	
	_root.central_cell_block.login_block.registration.registration_result_message.registration_error_message.text = "";
	
	_root.central_cell_block.login_block.registration.registration_form_field_firstname.gotoAndStop(1);
	_root.central_cell_block.login_block.registration.registration_form_field_lastname.gotoAndStop(1);
	_root.central_cell_block.login_block.registration.registration_form_field_email.gotoAndStop(1);

	registration_params = new LoadVars();
	registration_params.FirstName = _root.central_cell_block.login_block.registration.registration_form_field_firstname.registration_form_firstname.text;
	registration_params.LastName = _root.central_cell_block.login_block.registration.registration_form_field_lastname.registration_form_lastname.text;
	registration_params.Email = _root.central_cell_block.login_block.registration.registration_form_field_email.registration_form_email.text;
	registration_params.sendAndLoad( "flash_action.php?action=register", registration_params, "POST");
		
	registration_params.onLoad = function(success) {
			
		var error_count = 0;
			
		if (success) {
			if ( this.result == "OK" ){
				_root.UserId = this.UserId; 
				_root.SessionId = this.SessionId;
				_root.FullName = registration_params.FirstName + " " +  registration_params.LastName;
				_root.UserEmail = registration_params.Email;
				login_event("registration");
			}
		else {
					
				central_cell_block.login_block.registration.register_white.onEnterFrame = null;	
					
				central_cell_block.login_block.registration.register_button._visible = true;
				central_cell_block.login_block.registration.register_white._visible = false;
					
				if ( this.FirstNameError != undefined ) {
					_root.central_cell_block.login_block.registration.registration_form_field_firstname.gotoAndStop(2);
					error_count++;
				}
					
				if ( this.LastNameError != undefined ) {
					_root.central_cell_block.login_block.registration.registration_form_field_lastname.gotoAndStop(2);
					error_count++;
				}
					
				if ( this.EmailError != undefined ) {
					_root.central_cell_block.login_block.registration.registration_form_field_email.gotoAndStop(2);
					error_count++;
				}

				if ( error_count > 0 ){
					
					_root.central_cell_block.login_block.registration.registration_result_message.gotoAndStop(2);

					if ( this.EmailError != undefined ) {
						_root.central_cell_block.login_block.registration.registration_result_message.registration_error_message.text = this.EmailError;
						_root.central_cell_block.login_block.registration.registration_form_field_email.registration_form_email.setFocus();
					}
					
					if ( this.LastNameError != undefined ) {
						_root.central_cell_block.login_block.registration.registration_result_message.registration_error_message.text = this.LastNameError;
						_root.central_cell_block.login_block.registration.registration_form_field_lastname.registration_form_lastname.setFocus();
					}
					
					if ( this.FirstNameError != undefined ) {
						_root.central_cell_block.login_block.registration.registration_result_message.registration_error_message.text = this.FirstNameError;
						_root.central_cell_block.login_block.registration.registration_form_field_firstname.registration_form_firstname.setFocus();
					}
				} 
			}
		}
	}
}


central_cell_block.design_exists.button_yes.onPress = function() {
		SaveDesign( memory_design_params, _root.UserId, _root.design_title, 2);	
	}
	
central_cell_block.design_exists.button_no.onPress = function() {
		go_design_mode(memory_design_params);
	}
	
	
central_cell_block.design_is_save_again.vote_for_other_designs.onPress = function(){
	go_vote_mode();
	}	
	
central_cell_block.design_is_save.vote_for_other_designs.onPress = function(){
	go_vote_mode();
	}
	
central_cell_block.RegistrationConfirmation.submit_my_design.onPress = function(){
	go_design_mode("");
	}
	
central_cell_block.RegistrationConfirmation.vote_for_other_designs.onPress = function(){
	go_vote_mode();
	}