
/* Copyright (c) 2003 Timothy Sakharov, Alexander Sakharov */

  var itemText;
  var itemVal;
  var ind;

  var i;
  var j;
  var tmp;
  var sr;

  var moves = 0;
  var tankVol = 4;
  var current = 0;
  var storage = new Array(5);
  for ( i = 0; i < 5; i++ ) {
    storage[i] = 0;
  }

function start_over_f() {
  tankVol = 4;
  current = 0;
  moves = 0;
  //storage = new Array(5);
  for ( j = 0; j < 5; j++ ) {
    storage[j] = 0;
    setStorage(j+1);
  }
  document.problem0.src = "../image/car1.gif";
  document.problem1.src = "../image/spacec.gif";
  document.problem2.src = "../image/spacec.gif";
  document.problem3.src = "../image/spacec.gif";
  document.problem4.src = "../image/spacec.gif";
  document.problem5.src = "../image/spacec.gif";
  document.problem6.src = "../image/spacec.gif";
  setTank();
  document.theForm.fuel.options[0].selected = true;
}

function left_f() {
  if ( current == 0 ) {
    alert("Cannot move left from Start!");
    return false;
  }
  if ( tankVol == 0 ) {
    alert("No fuel to move!");
    return false;
  }

  sr = location.search;
  sr = sr.substring(sr.indexOf("SRC="));

  if ( sr.length < 12 || sr.charAt(10) != 'o' || sr.charAt(8) != 'a' ) {
          return false;
  }

  current--;
  tankVol--;
  if ( current == 0 ) {
    tankVol = 4;
  }
  setTank();
  moves++;
  setImg(current);
  document.theForm.fuel.options[0].selected = true;
  return true;
}

function right_f() {
  if ( tankVol == 0 ) {
    alert("No fuel to move!");
    return false;
  }

  sr = location.search;
  sr = sr.substring(sr.indexOf("SRC="));

  if ( sr.length < 12 || sr.charAt(7) != 'h' || sr.charAt(4) != 's' ) {
          return false;
  }

  current++;
  tankVol--;
  setTank();
  moves++;
  setImg(current);
  document.theForm.fuel.options[0].selected = true;
  if ( current == 6 ) {
    if ( moves > 12 ) {
      alert("You solved the problem but made more than 12 moves. Try to do it in 12 moves.");
      return false;
    } else {
      location.replace("http://sakharov.net/puzzle/fuelsolved.html");
    }
  }
  return true;
}

function setTank() {
  for ( i = document.theForm.tank.options.length - 1; i >= 0; i-- ) {
        document.theForm.tank.options[i] = null;
  }

  document.theForm.tank.options[0]
    = new Option(tankVol, tankVol, false, false);
}

function setStorage(n) {
  var stor;
  var storVal;
  if ( storage[n-1]>0 ) {
    stor = storage[n-1];
    storVal = storage[n-1];
  } else { // storage[n-1] == 0
    stor = "";
    storVal = "-";
  }
  switch(n) {
    case 1:
    for ( i = document.theForm.stored1.options.length - 1; i >= 0; i-- ) {
        document.theForm.stored1.options[i] = null;
    }
    document.theForm.stored1.options[0]
        = new Option(stor, storVal, false, false);
    break;

    case 2:
    for ( i = document.theForm.stored2.options.length - 1; i >= 0; i-- ) {
        document.theForm.stored2.options[i] = null;
    }
    document.theForm.stored2.options[0]
        = new Option(stor, storVal, false, false);
    break;

    case 3:
    for ( i = document.theForm.stored3.options.length - 1; i >= 0; i-- ) {
        document.theForm.stored3.options[i] = null;
    }
    document.theForm.stored3.options[0]
        = new Option(stor, storVal, false, false);
    break;

    case 4:
    for ( i = document.theForm.stored4.options.length - 1; i >= 0; i-- ) {
        document.theForm.stored4.options[i] = null;
    }
    document.theForm.stored4.options[0]
        = new Option(stor, storVal, false, false);
    break;

    case 5:
    for ( i = document.theForm.stored5.options.length - 1; i >= 0; i-- ) {
        document.theForm.stored5.options[i] = null;
    }
    document.theForm.stored5.options[0]
        = new Option(stor, storVal, false, false);
    break;

    default:
    break;
  }
}

function setImg(m) {
  switch(m) {
    case 0:
    document.problem0.src = "../image/car1.gif";
    document.problem1.src = "../image/spacec.gif";
    break;

    case 1:
    document.problem1.src = "../image/car1.gif";
    document.problem0.src = "../image/spacec.gif";
    document.problem2.src = "../image/spacec.gif";
    break;

    case 2:
    document.problem2.src = "../image/car1.gif";
    document.problem1.src = "../image/spacec.gif";
    document.problem3.src = "../image/spacec.gif";
    break;

    case 3:
    document.problem3.src = "../image/car1.gif";
    document.problem2.src = "../image/spacec.gif";
    document.problem4.src = "../image/spacec.gif";
    break;

    case 4:
    document.problem4.src = "../image/car1.gif";
    document.problem3.src = "../image/spacec.gif";
    document.problem5.src = "../image/spacec.gif";
    break;

    case 5:
    document.problem5.src = "../image/car1.gif";
    document.problem4.src = "../image/spacec.gif";
    document.problem6.src = "../image/spacec.gif";
    break;

    case 6:
    document.problem6.src = "../image/car1.gif";
    document.problem5.src = "../image/spacec.gif";
    break;

    default:
    break;
  }
}

function fuel_f() {
    if ( current == 0 ) {
      alert("Car is all set already");
      return false;
    }

    ind = document.theForm.fuel.selectedIndex;
    itemText = document.theForm.fuel.options[ind].text;
    itemValue = document.theForm.fuel.options[ind].value;
    document.theForm.fuel.options[0].selected = true;
    if ( itemValue.indexOf("fuel") >= 0 ) {
      alert("Incorrect selection");
      return false;
    }

    sr = location.search;
    sr = sr.substring(sr.indexOf("SRC="));

    if ( sr.length < 12 || sr.charAt(11) != 'v' || sr.charAt(9) != 'r' ) {
          return false;
    }

    //alert(itemValue);
    var change = parseInt(itemValue);

    if ( tankVol + change < 0 ) {
      alert("Not enough fuel in tank");
      return false;
    }
    if ( tankVol + change > 4 ) {
      alert("More than tank volume");
      return false;
    }
    if ( change > 0 && storage[current-1] < change  ) {
      alert("Not enough fuel in storage");
      return false;
    }

    if ( change > 0 ) {
      tankVol += change;
      storage[current-1] -= change;
    } else { // change < 0
      tankVol += change;
      storage[current-1] -= change;
    }
    setTank();
    setStorage(current);
    return true;
}


/*
function stored_f() {
    alert("This selection cannot be changed!");
    return false;
}
*/


