How to implement Aly's permutation test for comparison of variances in R? The excerpt below is from "Permutation, Parametric and Bootstrap Tests of Hypotheses", Third Ed. by Phillip Good (pages 58-61), section 3.7.2..
I am trying to implement this permutation test in R (see further below) to compare two variances. I am thinking now about how to calculate the p-value, and whether the test allows for different alternative hypothesis (greater, less, two-sided) and I am not sure on how to proceed.
Could you shed some light on this and perhaps give me some criticism about the code? Many thanks!

# Aly's non-parametric, permutation test of equality of variances
# From "Permutation, Parametric and Bootstrap Tests of Hypotheses", Third Ed. 
# by Phillip Good (pages 58-61), section 3.7.2.

# Implementation of delta statistic as defined by formula in page 60
# x_{i}, order statistics
# z = x_{i+1} - x_{i}, differences between successive order statistics
aly_delta_statistic <- function(z) {
  z_length <- length(z)
  m <- z_length + 1
  i <- 1:z_length
  sum(i*(m-i)*z)
}

aly_test_statistic <- function(sample1, sample2 = NULL, nperm = 1) {

  # compute statistic based on one sample only: sample1
  if(is.null(sample2)) {
    sample1 <- sort(sample1)
    z <- diff(sample1)
    return(aly_delta_statistic(z))
  }

  # statistic based on randomization of the two samples
  else {
    m1 <- length(sample1)
    m2 <- length(sample2)
    # allocate a vector to save the statistic delta
    statistic <- vector(mode = "numeric", length = nperm)
    for(j in 1:nperm) {
      # 1st stage resampling (performed only if samples sizes are different)
      # larger sample is resized to the size of the smaller
      if(m2 > m1) {
         sample2 <- sort(sample(sample2, m1))
         m <- m1
      } else {
         sample1 <- sort(sample(sample1, m2))
         m <- m2
      }
      # z-values: z1 in column 1 and z2 in column 2.
      z_two_samples <- matrix(c(diff(sample1), diff(sample2)), ncol = 2)
      # 2nd stage resampling
      z <- apply(z_two_samples, 1, sample, 1)
      statistic[j] <- aly_delta_statistic(z)
    }
    return(statistic)
  }
}

 A: Yes I think you can test equality against the possibility that population 1 has a variance larger than population 2 or less than population 2 or if you have no reason to expect the result one way or the other.  
In the text it seems that Phil is doing a two-sided test because he talks about as extreme or more extreme value than you get under the null hypothesis.  If it was one-sided he should say as large or larger or on the other side as small or smaller.  As he finds only 2 out of the 16 possible permutations he gets a p-value of 2/16 = 0.125. He then says accept the null hypothesis whereas many of us would prefer to say do not reject.
Regarding the R code I am not an expert in R so I cannot tell you about the accuracy of the code.  If it was taken from the book it is likely correct or there would probably be an errata sheet with a correction.
Whether you use a parametric test for the equality of the variances or non-parametric approaches like bootstrap or permutation tests it is very difficult to reject the null hypothesis with small sample sizes especially 4 samples in each group as Phil did here.  But I think he did it for simplicity to explain the method simply and thoroughly.
