
/* Copyright (c) 2003 Timothy Sakharov, Alexander Sakharov */

  var itemText;
  var itemVal;
  var ind;

  var i;
  var tmp;
  var sr;


function load_f() {
      var t = location.search;
      var n = t.indexOf("TTL=");

      total = parseInt(t.substring(n+4));

      sr = t;
      sr = sr.substring(sr.indexOf("SRC="));
      if ( sr.length < 9 || sr.charAt(6) != 'k' || sr.charAt(11) != 'v' ) {
          return false;
      }
}

function translate(c) {
  if ( c == '-' ) return 'a';
  if ( c == '|' ) return 'b';
  if ( c == '/' ) return 'c';
  if ( c == '\\' ) return 'd';
}

function backTranslate(c) {
  if ( c == 'a' ) return '-';
  if ( c == 'b' ) return '|';
  if ( c == 'c' ) return '/';
  if ( c == 'd' ) return '\\';
}

function verifyOne(c) {
  if ( c != '-' && c != '|' && c != '/' && c != '\\' )
    return false;
  else
    return true;
}

function isDupl(a, b, c, d) {
  if ( a == b || b == c || c == d || a == c || a == d || b == d )
    return true;
  else
    return false;
}

function verifySigns() {
  if ( !verifyOne(document.theForm.problem11.value)
  || !verifyOne(document.theForm.problem12.value)
  || !verifyOne(document.theForm.problem13.value)
  || !verifyOne(document.theForm.problem14.value)
  || !verifyOne(document.theForm.problem21.value)
  || !verifyOne(document.theForm.problem22.value)
  || !verifyOne(document.theForm.problem23.value)
  || !verifyOne(document.theForm.problem24.value)
  || !verifyOne(document.theForm.problem31.value)
  || !verifyOne(document.theForm.problem32.value)
  || !verifyOne(document.theForm.problem33.value)
  || !verifyOne(document.theForm.problem34.value)
  || !verifyOne(document.theForm.problem41.value)
  || !verifyOne(document.theForm.problem42.value)
  || !verifyOne(document.theForm.problem43.value)
  || !verifyOne(document.theForm.problem44.value)
  ) {
    return false;
  }
  return true;
}

function verifyDupl() {
  if ( isDupl(document.theForm.problem11.value, document.theForm.problem12.value,
      document.theForm.problem13.value, document.theForm.problem14.value)
  || isDupl(document.theForm.problem21.value, document.theForm.problem22.value,
      document.theForm.problem23.value, document.theForm.problem24.value)
  || isDupl(document.theForm.problem31.value, document.theForm.problem32.value,
      document.theForm.problem33.value, document.theForm.problem34.value)
  || isDupl(document.theForm.problem41.value, document.theForm.problem42.value,
      document.theForm.problem43.value, document.theForm.problem44.value)
  || isDupl(document.theForm.problem11.value, document.theForm.problem21.value,
      document.theForm.problem31.value, document.theForm.problem41.value)
  || isDupl(document.theForm.problem12.value, document.theForm.problem22.value,
      document.theForm.problem32.value, document.theForm.problem42.value)
  || isDupl(document.theForm.problem13.value, document.theForm.problem23.value,
      document.theForm.problem33.value, document.theForm.problem43.value)
  || isDupl(document.theForm.problem14.value, document.theForm.problem24.value,
      document.theForm.problem34.value, document.theForm.problem44.value)
  || isDupl(document.theForm.problem11.value, document.theForm.problem22.value,
      document.theForm.problem33.value, document.theForm.problem44.value)
  ||isDupl(document.theForm.problem14.value, document.theForm.problem23.value,
      document.theForm.problem32.value, document.theForm.problem41.value)
  ) {
    return false;
  }
  return true;
}

function submit_f() {

    sr = location.search;
    sr = sr.substring(sr.indexOf("SRC="));
    if ( sr.length < 9 || sr.charAt(7) != 'h' || sr.charAt(10) != 'o' ) {
          return false;
    }

    if ( !verifySigns() ) {
      alert("Unacceptable character");
      return;
    }
    if ( !verifyDupl() ) {
      alert("Incorrect - repeated signs");
      return;
    }

    var qStr = translate(document.theForm.problem11.value)
    + translate(document.theForm.problem12.value)
    + translate(document.theForm.problem13.value)
    + translate(document.theForm.problem14.value)
    + translate(document.theForm.problem21.value)
    + translate(document.theForm.problem22.value)
    + translate(document.theForm.problem23.value)
    + translate(document.theForm.problem24.value)
    + translate(document.theForm.problem31.value)
    + translate(document.theForm.problem32.value)
    + translate(document.theForm.problem33.value)
    + translate(document.theForm.problem34.value)
    + translate(document.theForm.problem41.value)
    + translate(document.theForm.problem42.value)
    + translate(document.theForm.problem43.value)
    + translate(document.theForm.problem44.value);

    location.replace("http://sakharov.net/puzzle/distinctsolved.html?SOLUTION=" + qStr);
    return true;

}


// --------------------

function load_solved_f() {

  var k1;
  var k2;

  var solution = location.search;
  var n = solution.indexOf("SOLUTION=");
  solution = solution.substring(n+9);
  //alert(solution);

  document.theForm.problem11.value = backTranslate( solution.charAt(0));
  document.theForm.problem12.value = backTranslate( solution.charAt(1));
  document.theForm.problem13.value = backTranslate( solution.charAt(2));
  document.theForm.problem14.value = backTranslate( solution.charAt(3));
  document.theForm.problem21.value = backTranslate( solution.charAt(4));
  document.theForm.problem22.value = backTranslate( solution.charAt(5));
  document.theForm.problem23.value = backTranslate( solution.charAt(6));
  document.theForm.problem24.value = backTranslate( solution.charAt(7));
  document.theForm.problem31.value = backTranslate( solution.charAt(8));
  document.theForm.problem32.value = backTranslate( solution.charAt(9));
  document.theForm.problem33.value = backTranslate( solution.charAt(10));
  document.theForm.problem34.value = backTranslate( solution.charAt(11));
  document.theForm.problem41.value = backTranslate( solution.charAt(12));
  document.theForm.problem42.value = backTranslate( solution.charAt(13));
  document.theForm.problem43.value = backTranslate( solution.charAt(14));
  document.theForm.problem44.value = backTranslate( solution.charAt(15));

}


