
/* Copyright (c) 2003 Timothy Sakharov, Alexander Sakharov */

var i;
var j;
var k;

var sr;


function swapLeftMan() {
  sr = location.search;
  sr = sr.substring(sr.indexOf("SRC=")+4);

  if ( document.leftsp.width != 10 ) {
    alert("Boat is on the other side!");
    return;
  }
  if ( sr.length < 7 || sr.charAt(6) != 'o' ) {
    return false;
  }
  if ( document.leftman.src.indexOf("boy.gif") >= 0 ) {
    if ( document.boat1.src.indexOf("spacec.gif") >= 0 ) {
      document.boat1.src = "../image/boy.gif";
    } else if ( document.boat2.src.indexOf("spacec.gif") >= 0 ) {
      document.boat2.src = "../image/boy.gif";
    }
    document.leftman.src = "../image/spacec.gif";
  }
}

function swapLeftWolf() {
  sr = location.search;
  sr = sr.substring(sr.indexOf("SRC=")+4);

  if ( document.leftsp.width != 10 ) {
    alert("Boat is on the other side!");
    return;
  }
  if ( sr.length < 7 || sr.charAt(3) != 'h' ) {
    return false;
  }
  if ( document.leftwolf.src.indexOf("wolf.gif") >= 0 ) {
    if ( document.boat1.src.indexOf("spacec.gif") >= 0 ) {
      document.boat1.src = "../image/wolf.gif";
    } else if ( document.boat2.src.indexOf("spacec.gif") >= 0 ) {
      document.boat2.src = "../image/wolf.gif";
    }
    document.leftwolf.src = "../image/spacec.gif";
  }
}

function swapLeftGoat() {
  sr = location.search;
  sr = sr.substring(sr.indexOf("SRC=")+4);

  if ( document.leftsp.width != 10 ) {
    alert("Boat is on the other side!");
    return;
  }
  if ( sr.length < 7 || sr.charAt(6) != 'o' ) {
    return false;
  }
  if ( document.leftgoat.src.indexOf("goat.gif") >= 0 ) {
    if ( document.boat1.src.indexOf("spacec.gif") >= 0 ) {
      document.boat1.src = "../image/goat.gif";
    } else if ( document.boat2.src.indexOf("spacec.gif") >= 0 ) {
      document.boat2.src = "../image/goat.gif";
    }
    document.leftgoat.src = "../image/spacec.gif";
  }
}

function swapLeftCabbage() {
  sr = location.search;
  sr = sr.substring(sr.indexOf("SRC=")+4);

  if ( document.leftsp.width != 10 ) {
    alert("Boat is on the other side!");
    return;
  }
  if ( sr.length < 7 || sr.charAt(3) != 'h' ) {
    return false;
  }
  if ( document.leftcabbage.src.indexOf("cabbage.gif") >= 0 ) {
    if ( document.boat1.src.indexOf("spacec.gif") >= 0 ) {
      document.boat1.src = "../image/cabbage.gif";
    } else if ( document.boat2.src.indexOf("spacec.gif") >= 0 ) {
      document.boat2.src = "../image/cabbage.gif";
    }
    document.leftcabbage.src = "../image/spacec.gif";
  }
}

function swapRightMan() {
  sr = location.search;
  sr = sr.substring(sr.indexOf("SRC=")+4);

  if ( document.rightsp.width != 10 ) {
    alert("Boat is on the other side!");
    return;
  }
  if ( sr.length < 7 || sr.charAt(6) != 'o' ) {
    return false;
  }
  if ( document.rightman.src.indexOf("boy.gif") >= 0 ) {
    if ( document.boat1.src.indexOf("spacec.gif") >= 0 ) {
      document.boat1.src = "../image/boy.gif";
    } else if ( document.boat2.src.indexOf("spacec.gif") >= 0 ) {
      document.boat2.src = "../image/boy.gif";
    }
    document.rightman.src = "../image/spacec.gif";
  }
}

function swapRightWolf() {
  sr = location.search;
  sr = sr.substring(sr.indexOf("SRC=")+4);

  if ( document.rightsp.width != 10 ) {
    alert("Boat is on the other side!");
    return;
  }
  if ( sr.length < 7 || sr.charAt(3) != 'h' ) {
    return false;
  }
  if ( document.rightwolf.src.indexOf("wolf.gif") >= 0 ) {
    if ( document.boat1.src.indexOf("spacec.gif") >= 0 ) {
      document.boat1.src = "../image/wolf.gif";
    } else if ( document.boat2.src.indexOf("spacec.gif") >= 0 ) {
      document.boat2.src = "../image/wolf.gif";
    }
    document.rightwolf.src = "../image/spacec.gif";
  }
}

function swapRightGoat() {
  sr = location.search;
  sr = sr.substring(sr.indexOf("SRC=")+4);

  if ( document.rightsp.width != 10 ) {
    alert("Boat is on the other side!");
    return;
  }
  if ( sr.length < 7 || sr.charAt(6) != 'o' ) {
    return false;
  }
  if ( document.rightgoat.src.indexOf("goat.gif") >= 0 ) {
    if ( document.boat1.src.indexOf("spacec.gif") >= 0 ) {
      document.boat1.src = "../image/goat.gif";
    } else if ( document.boat2.src.indexOf("spacec.gif") >= 0 ) {
      document.boat2.src = "../image/goat.gif";
    }
    document.rightgoat.src = "../image/spacec.gif";
  }
}

function swapRightCabbage() {
  sr = location.search;
  sr = sr.substring(sr.indexOf("SRC=")+4);

  if ( document.rightsp.width != 10 ) {
    alert("Boat is on the other side!");
    return;
  }
  if ( sr.length < 7 || sr.charAt(3) != 'h' ) {
    return false;
  }
  if ( document.rightcabbage.src.indexOf("cabbage.gif") >= 0 ) {
    if ( document.boat1.src.indexOf("spacec.gif") >= 0 ) {
      document.boat1.src = "../image/cabbage.gif";
    } else if ( document.boat2.src.indexOf("spacec.gif") >= 0 ) {
      document.boat2.src = "../image/cabbage.gif";
    }
    document.rightcabbage.src = "../image/spacec.gif";
  }
}

function swapBoat(n) {
  if ( sr.length < 7 || sr.charAt(5) != 'r'
  || sr.charAt(1) != sr.charAt(4) ) {
    return;
  }
  if ( document.rightsp.width == 10 ) {
    if ( n == 1
    && document.boat1.src.indexOf("boy.gif") >= 0 ) {
      document.rightman.src = "../image/boy.gif";
      document.boat1.src = "../image/spacec.gif";
    } else if ( n == 1
    && document.boat1.src.indexOf("wolf.gif") >= 0 ) {
      document.rightwolf.src = "../image/wolf.gif";
      document.boat1.src = "../image/spacec.gif";
    } else if ( n == 1
    && document.boat1.src.indexOf("goat.gif") >= 0 ) {
      document.rightgoat.src = "../image/goat.gif";
      document.boat1.src = "../image/spacec.gif";
    } else if ( n == 1
    && document.boat1.src.indexOf("cabbage.gif") >= 0 ) {
      document.rightcabbage.src = "../image/cabbage.gif";
      document.boat1.src = "../image/spacec.gif";
    } else if ( n == 2
    && document.boat2.src.indexOf("boy.gif") >= 0 ) {
      document.rightman.src = "../image/boy.gif";
      document.boat2.src = "../image/spacec.gif";
    } else if ( n == 2
    && document.boat2.src.indexOf("wolf.gif") >= 0 ) {
      document.rightwolf.src = "../image/wolf.gif";
      document.boat2.src = "../image/spacec.gif";
    } else if ( n == 2
    && document.boat2.src.indexOf("goat.gif") >= 0 ) {
      document.rightgoat.src = "../image/goat.gif";
      document.boat2.src = "../image/spacec.gif";
    } else if ( n == 2
    && document.boat2.src.indexOf("cabbage.gif") >= 0 ) {
      document.rightcabbage.src = "../image/cabbage.gif";
      document.boat2.src = "../image/spacec.gif";
    }
  } else { // to the left
    if ( n == 1
    && document.boat1.src.indexOf("boy.gif") >= 0 ) {
      document.leftman.src = "../image/boy.gif";
      document.boat1.src = "../image/spacec.gif";
    } else if ( n == 1
    && document.boat1.src.indexOf("wolf.gif") >= 0 ) {
      document.leftwolf.src = "../image/wolf.gif";
      document.boat1.src = "../image/spacec.gif";
    } else if ( n == 1
    && document.boat1.src.indexOf("goat.gif") >= 0 ) {
      document.leftgoat.src = "../image/goat.gif";
      document.boat1.src = "../image/spacec.gif";
    } else if ( n == 1
    && document.boat1.src.indexOf("cabbage.gif") >= 0 ) {
      document.leftcabbage.src = "../image/cabbage.gif";
      document.boat1.src = "../image/spacec.gif";
    } else if ( n == 2
    && document.boat2.src.indexOf("boy.gif") >= 0 ) {
      document.leftman.src = "../image/boy.gif";
      document.boat2.src = "../image/spacec.gif";
    } else if ( n == 2
    && document.boat2.src.indexOf("wolf.gif") >= 0 ) {
      document.leftwolf.src = "../image/wolf.gif";
      document.boat2.src = "../image/spacec.gif";
    } else if ( n == 2
    && document.boat2.src.indexOf("goat.gif") >= 0 ) {
      document.leftgoat.src = "../image/goat.gif";
      document.boat2.src = "../image/spacec.gif";
    } else if ( n == 2
    && document.boat2.src.indexOf("cabbage.gif") >= 0 ) {
      document.leftcabbage.src = "../image/cabbage.gif";
      document.boat2.src = "../image/spacec.gif";
    }
  }
}

function checkNumber() {

  if ( document.leftman.src.indexOf("boy.gif") < 0 ) {
    if ( document.leftwolf.src.indexOf("wolf.gif") >= 0
    && document.leftgoat.src.indexOf("goat.gif") >= 0 ) {
	  alert("You lost - Goat is eaten!");
        document.leftgoat.src = "../image/spacec.gif";
    }
    if ( document.leftcabbage.src.indexOf("cabbage.gif") >= 0
    && document.leftgoat.src.indexOf("goat.gif") >= 0 ) {
	  alert("You lost - Cabbage is eaten!");
        document.leftcabbage.src = "../image/spacec.gif";
    }
  }

  if ( document.rightman.src.indexOf("boy.gif") < 0 ) {
    if ( document.rightwolf.src.indexOf("wolf.gif") >= 0
    && document.rightgoat.src.indexOf("goat.gif") >= 0 ) {
	  alert("You lost - Goat is eaten!");
        document.rightgoat.src = "../image/spacec.gif";
    }
    if ( document.rightcabbage.src.indexOf("cabbage.gif") >= 0
    && document.rightgoat.src.indexOf("goat.gif") >= 0 ) {
	  alert("You lost - Cabbage is eaten!");
        document.rightcabbage.src = "../image/spacec.gif";
    }
  }
}

function checkResult() {
  if ( document.rightman.src.indexOf("boy.gif") >= 0
  && document.rightwolf.src.indexOf("wolf.gif") >= 0
  && document.rightgoat.src.indexOf("goat.gif") >= 0
  && document.rightcabbage.src.indexOf("cabbage.gif") >= 0 ) {
    location.replace("http://sakharov.net/puzzle/wgcsolved.html");
  }
}

function submit_left() {
  sr = location.search;
  sr = sr.substring(sr.indexOf("SRC=")+4);

  if ( document.leftsp.width != 120 )
    return;
  if ( document.boat1.src.indexOf("spacec.gif") >= 0
  && document.boat2.src.indexOf("spacec.gif") >= 0 ) {
    alert("Nobody is in the boat!");
    return;
  }
  if ( document.boat1.src.indexOf("boy.gif") < 0
  && document.boat2.src.indexOf("boy.gif") < 0 ) {
    alert("Unmanned boat!");
    return;
  }
  document.leftsp.width = 10;
  document.rightsp.width = 120;
  swapBoat(1);
  swapBoat(2);
  checkNumber();
  checkResult();
}

function submit_right() {
  sr = location.search;
  sr = sr.substring(sr.indexOf("SRC=")+4);

  if ( document.rightsp.width != 120 )
    return;
  if ( document.boat1.src.indexOf("spacec.gif") >= 0
  && document.boat2.src.indexOf("spacec.gif") >= 0 ) {
    alert("Nobody is in the boat!");
    return;
  }
  if ( document.boat1.src.indexOf("boy.gif") < 0
  && document.boat2.src.indexOf("boy.gif") < 0 ) {
    alert("Unmanned boat!");
    return;
  }
  document.rightsp.width = 10;
  document.leftsp.width = 120;
  swapBoat(1);
  swapBoat(2);
  checkNumber();
  checkResult();
}

function submit_start() {
    document.leftsp.width = 10;
    document.rightsp.width = 120;
    document.leftman.src = "../image/boy.gif";
    document.leftwolf.src = "../image/wolf.gif";
    document.leftgoat.src = "../image/goat.gif";
    document.leftcabbage.src = "../image/cabbage.gif";
    document.boat1.src = "../image/spacec.gif";
    document.boat2.src = "../image/spacec.gif";
    document.rightman.src = "../image/spacec.gif";
    document.rightwolf.src = "../image/spacec.gif";
    document.rightgoat.src = "../image/spacec.gif";
    document.rightcabbage.src = "../image/spacec.gif";

}



