<?php

	//Security
	if( ! defined( 'IS_INIT' ) )
	{
		die( 'Unauthorized Access');
	}
    
    global $Handler;
    
    $name = $_POST['CharName'];
    if( preg_match( '/[^a-zA-Z0-9]/' , $name ) == 0 )
    {
        if( strlen( $name ) >= 3 && strlen( $name ) <= 15 )
        {
            if( $Handler->char->CheckNameAvaiable( $name ) == true )
            {
                if( $Handler->user->HasEnoughCoins( 5000 ) == true )
                {
                    if( $Handler->char->IsOnline() == false )
                    {
                        //Name from - to
                        $this->Handler->WriteLog( 'Name Change' , 'Success' , "$name" , "{$Handler->char->charinfo['sID']}" );
                        $Handler->char->UpdateName( $name );
                        $Handler->user->SubPoints( 5000 );
                        $Handler->theme->SetValue( 'MESSAGE' , $Handler->theme->GetValue( 'NAME_CHANGED' ) );
                        $Handler->theme->SetValue( 'GO_BACK' , 'Refresh' );
                        $Handler->theme->SetValue( 'RED_URL' , MAIN_URL . "?view=cp&do=characters&char=" . $name  );
                        $Handler->theme->Parse( 'success.html' );
                        exit;
                    }
                    else
                    {
                        $Handler->theme->SetValue( 'ERROR' , $Handler->theme->GetValue( 'CHAR_LOGGED_IN' ) );
                        $Handler->theme->SetValue( 'GO_BACK' , null );
                        $Handler->theme->SetValue( 'RED_URL' , MAIN_URL . $_SERVER['REQUEST_URI'] );
                        $Handler->theme->Parse( 'error.html' );
                    }
                }
                else
                {
                    $Handler->theme->SetValue( 'ERROR' , $Handler->theme->GetValue( 'ERR_MONEY' ) );
                    $Handler->theme->SetValue( 'GO_BACK' , null );
                    $Handler->theme->SetValue( 'RED_URL' , MAIN_URL . $_SERVER['REQUEST_URI'] );
                    $Handler->theme->Parse( 'error.html' );
                }
            }
            else
            {
                $Handler->theme->SetValue( 'ERROR' , $Handler->theme->GetValue( 'ERROR_NAME_TAKEN' ) );
                $Handler->theme->SetValue( 'GO_BACK' , null );
                $Handler->theme->SetValue( 'RED_URL' , MAIN_URL . $_SERVER['REQUEST_URI'] );
                $Handler->theme->Parse( 'error.html' );
            }
        }
        else
        {
            $Handler->theme->SetValue( 'ERROR' , $Handler->theme->GetValue( 'ERROR_NAME_LENGTH' ) );
            $Handler->theme->SetValue( 'GO_BACK' , null );
            $Handler->theme->SetValue( 'RED_URL' , MAIN_URL . $_SERVER['REQUEST_URI'] );
            $Handler->theme->Parse( 'error.html' );
        }
    }
    else
    {
        $this->Handler->WriteLog( 'Name Change' , 'Failed' , "Regex Failed" , "{$Handler->char->charinfo['sID']}" , "$name" );
        $Handler->theme->SetValue( 'ERROR' , $Handler->theme->GetValue( 'ERROR_NAME_CHARS' ) );
        $Handler->theme->SetValue( 'GO_BACK' , null );
        $Handler->theme->SetValue( 'RED_URL' , MAIN_URL . $_SERVER['REQUEST_URI'] );
        $Handler->theme->Parse( 'error.html' );
    }
?>
