/*
   New Perspectives on HTML, XHTML, and DHTML
   Tutorial 15
   Review Assignment

   Filename: functions.js

   Functions List:

   placeIt(object, x, y)
      Places an object at the coordiantes (x,y)

   getXCoord(object)
      Returns the x-coordinate of an object

   getYCoord(object)
      Returns the x-coordinate of an object

   withinIt(x, y, object)
      Returns the Boolean value true if the coordiantes (x,y) lie within the
      boundaries of the object and false if otherwise.

   getDIV(x, y)
      Returns the DIV element located at the coordinates (x,y)

   swapIt(object1, object2)
      Swaps the positions of object1 and object2

*/

function placeIt(object, x, y) {
   object.style.left=x;
   object.style.top=y;
}

function getXCoord(object) {
   return parseInt(object.style.left);
}

function getYCoord(object) {
   return parseInt(object.style.top);
}

function withinIt(x, y, object) {
   within=false;
   otop=parseInt(object.style.top);
   obottom=otop+parseInt(object.style.height);
   oleft=parseInt(object.style.left);
   oright=oleft+parseInt(object.style.width);
 
   if ((y>otop && y<obottom) && (x>oleft && x<oright)) within=true;
   return within;
}

function getDIV(x,y) {
   DIVObject=null;
   inside=false;
   for (i=0; i<document.getElementsByTagName("div").length; i++) {
      inside=withinIt(x, y, document.getElementsByTagName("div")[i]);
      if (inside) DIVObject=document.getElementsByTagName("div")[i]
   }
   return DIVObject;
}

function swapIt(object1,object2) {
   x1=getXCoord(object1);
   y1=getYCoord(object1);
   x2=getXCoord(object2);
   y2=getYCoord(object2);

   placeIt(object1,x2,y2);
   placeIt(object2,x1,y1);
}
