function turn8(lr)
{ 
 if (turn_index[i] == 0) 
    { 
     if (lr == 'l' && ind_flip[i] == 0) 
     {
       func45(); turn_index[i]=3;
     }
     if (lr == 'l' && ind_flip[i] == 1) 
     {
       func50(); turn_index[i]=3;
     }
     if (lr == 'r' && ind_flip[i] == 0)
     {
       func47(); turn_index[i]=1;
     }
     if (lr == 'r' && ind_flip[i] == 1)
     {
       func52(); turn_index[i]=1;
     }
     if (lr == 'flip')
        {
          if (ind_flip[i] == 0)  
             { 
               func49(); ind_flip[i]=1; 
             }
             else
             {
               func48(); ind_flip[i]=0; 
             }
        }
    }    
    else
 if (turn_index[i] == 3)
    { 
     if (lr == 'l' && ind_flip[i] == 0) 
     {
       func46(); turn_index[i]=2;
     }
     if (lr == 'l' && ind_flip[i] == 1) 
     {
       func51(); turn_index[i]=2;
     }
     if (lr == 'r' && ind_flip[i] == 0)
     {
       func48(); turn_index[i]=0;
     }      
     if (lr == 'r' && ind_flip[i] == 1)
     {
       func49(); turn_index[i]=0;
     }
     if (lr == 'flip')
        {           
          if (ind_flip[i] == 0)  
             {
               func52(); ind_flip[i]=1; turn_index[i]=1;
             }
             else
             {
               func47(); ind_flip[i]=0; turn_index[i]=1; 
             }
        }
    }
    else
 if (turn_index[i] == 2)
    { 
     if (lr == 'l' && ind_flip[i] == 0) 
     {
       func47(); turn_index[i]=1;
     }
     if (lr == 'l' && ind_flip[i] == 1) 
     {
       func52(); turn_index[i]=1;
     }
     if (lr == 'r' && ind_flip[i] == 0)
     {
       func45(); turn_index[i]=3;
     }      
     if (lr == 'r' && ind_flip[i] == 1)
     {
       func50(); turn_index[i]=3;
     }
     if (lr == 'flip')
        {           
          if (ind_flip[i] == 0)  
             {
               func51(); ind_flip[i]=1; 
             }
             else
             {
               func46(); ind_flip[i]=0;  
             }
        }
    }
    else
 if (turn_index[i] == 1)
    { 
     if (lr == 'l' && ind_flip[i] == 0) 
     {
       func48(); turn_index[i]=0;
     }
     if (lr == 'l' && ind_flip[i] == 1) 
     {
       func49(); turn_index[i]=0;
     }
     if (lr == 'r' && ind_flip[i] == 0)
     {
       func46(); turn_index[i]=2;
     }      
     if (lr == 'r' && ind_flip[i] == 1)
     {
       func51(); turn_index[i]=2;
     }
     if (lr == 'flip')
        {           
          if (ind_flip[i] == 0)  
             {
               func50(); ind_flip[i]=1; turn_index[i]=3;
             }
             else
             { 
               func45(); ind_flip[i]=0; turn_index[i]=3; 
             }
        }
    }
}


function turn8a()
{  
document.getElementById("fig8").style.left=a[i]+'px';
document.getElementById("fig8a").style.top=t1a[i]+'px';
document.getElementById("fig8a").style.left=l1a[i]+'px';
document.getElementById("fig8b").style.top=t1b[i]+'px';
document.getElementById("fig8b").style.left=l1b[i]+'px';
document.getElementById("fig8c").style.top=t1c[i]+'px';
document.getElementById("fig8c").style.left=l1c[i]+'px';
document.getElementById("fig8d").style.top=t1d[i]+'px';
document.getElementById("fig8d").style.left=l1d[i]+'px';
document.getElementById("fig8e").style.top=t1e[i]+'px';
document.getElementById("fig8e").style.left=l1e[i]+'px';
}

function func45()
{ 
  t1a[i]=68; l1a[i]=0;
  t1b[i]=68; l1b[i]=34;
  t1c[i]=34; l1c[i]=34;    
  t1d[i]=34; l1d[i]=68;
  t1e[i]=0; l1e[i]=34; 
  turn8a();
}

function func46()
{
  t1a[i]=68; l1a[i]=68;
  t1b[i]=34; l1b[i]=68;
  t1c[i]=34; l1c[i]=34;    
  t1d[i]=0; l1d[i]=34;
  t1e[i]=34; l1e[i]=0; 
  turn8a();
}

function func47()
{    
  t1a[i]=0;  l1a[i]=68;
  t1b[i]=0;  l1b[i]=34;
  t1c[i]=34; l1c[i]=34;    
  t1d[i]=34; l1d[i]=0;
  t1e[i]=68; l1e[i]=34; 
  turn8a();
}
function func48()
{
  t1a[i]=0;  l1a[i]=0;
  t1b[i]=34; l1b[i]=0;
  t1c[i]=34; l1c[i]=34;    
  t1d[i]=68; l1d[i]=34;
  t1e[i]=34; l1e[i]=68; 
  turn8a();
}
function func49()
{ 
  t1a[i]=0;  l1a[i]=68;
  t1b[i]=34; l1b[i]=68;
  t1c[i]=34; l1c[i]=34;    
  t1d[i]=68; l1d[i]=34;
  t1e[i]=34; l1e[i]=0; 
  turn8a();
}

function func50()
{
  t1a[i]=0;  l1a[i]=0;
  t1b[i]=0;  l1b[i]=34;
  t1c[i]=34; l1c[i]=34;    
  t1d[i]=34; l1d[i]=68;
  t1e[i]=68; l1e[i]=34; 
  turn8a();
}

function func51()
{    
  t1a[i]=68; l1a[i]=0;
  t1b[i]=34; l1b[i]=0;
  t1c[i]=34; l1c[i]=34;    
  t1d[i]=0; l1d[i]=34;
  t1e[i]=34; l1e[i]=68; 
  turn8a();
}
function func52()
{ 
  t1a[i]=68; l1a[i]=68;
  t1b[i]=68; l1b[i]=34;
  t1c[i]=34; l1c[i]=34;    
  t1d[i]=34; l1d[i]=0;
  t1e[i]=0; l1e[i]=34; 
  turn8a();
 }

