// INITIAL VALUES

var xdist=0; // distance from mouse to left edge of object
var ydist=0; // distance from mouse to top edge of object
var moveloop=1; // has wall been selected for moving
var rmoveloop=0; // has room been selected for moving
var tmoveloop=0; // has text been selected for resizing


var mr=1; // room name variables    
var roomact="room0";
var sft=288;
var moveroom="notyet";
var sdt=0;
var rmalign=0;
var mwall="wall";
var mside="left";
var rclass="0";
var toolsopen=0                                              
var xcopy="copythis";
var xcopyr="copythis2";
var afloor=1;
var maxtop=3000;
var maxleft=3000;
var maxwidth=0;
var maxheight=0;
var unum=0;
var textact=0;
var dscale=1;
var roomloc=new Array();
var roomloc2=new Array();
var redox="";

var openplan="";
var opentext="";
var openelevation="";
var zindx=0;
var time=new Date();
var intime=time.getTime();
var savetime=0;



arleft="zzqb1|1144qq1qb2|1288qq2";
arright="zzqb1|1000qq1qb2|1144qq2";
artop="zzqb1|1044qq1qb2|1044qq2";
arbot="zzqb1|900qq1qb2|900qq2";


function showCords(event){

x=((event.clientX)-1-scrollxx);
x=(Math.round(x/dscale));
xf=(x/12);
xf=(Math.floor(xf));
xi=(Math.round(((x/12)-xf)*12));

y=((event.clientY)-scrollyy-9);
y=(Math.round(y/dscale));
yf=(y/12);
yf=(Math.floor(yf));
yi=(Math.round(((y/12)-yf)*12));




z=(xf + "'-" + xi + "\" X " + yf + "'-" + yi + "\" ");

document.getElementById("cords").innerHTML=z;

}


  /* Check Dimension -------------------------------------------------------------------------    */
  
  function checkDimension()
  {
xtotal=0;
leftx=(5000);
widthx=(0);
topx=(5000);
bottomx=(0); 


if (xcopy=="copythis")
{
xclay=1;
}
else
{
xclay=2;
}


z=1;
var i=0;
for (i=1;i<=ad;i++)
{
room=("room" + z);

a=document.getElementById(room);
   


if (a !== null)
{
apar=(a.parentNode.id);
if (apar==xcopy)
{

x=a.style.left;      
x=(x.replace("px",""));
x=(x*1);

if (x < leftx)
{
leftx=x;
}

xw=a.style.width; 
xw=(xw.replace("px",""));
xw=((xw*1));
xx=(x+xw);
 if (xx > widthx)
{
widthx=xx;
}
     

y=a.style.top;      
y=(y.replace("px",""));
y=(y*1);             

if (y < topx)
{
topx=y;
}

yy=a.style.height; 
yy=(yy.replace("px",""));
yy=(yy*1);
yw=(y+yy);
 if (yw > bottomx)
{
bottomx=yw;
}

if (a.className !="roomd" && a.className != "roome" && a.className != "roome1" && a.className != "roomd1")
{
xwa=(xw/dscale);
yya=(yy/dscale);
xtotal=(xtotal + (((xwa+8)*(yya+8))/144));
xtotal=(Math.floor(xtotal));

if (xclay==1)
{
document.getElementById('floorsize').innerHTML=(xtotal + " finished square feet");
}
else
{
document.getElementById('floorsize2').innerHTML=(xtotal + " finished square feet");

}
}




}
 }
z=(z+1);
  }


 widthact=(((widthx-leftx)/dscale) +8); 
depthact=(((bottomx-topx)/dscale)+8);  

if(widthact > 1)
{ 
  
widthx=(widthx-leftx + 8);  
bottomx=(bottomx-topx + 8); 




xfp=("footprint" + xclay);  
document.getElementById(xfp).style.left=(leftx + 1 + "px");
document.getElementById(xfp).style.width=(widthx + "px");
document.getElementById(xfp).style.top=(topx + "px");
document.getElementById(xfp).style.height=(bottomx + "px");



xtdim=("topdim" + xclay); 

xf=(widthact/12);
xf=(Math.floor(xf));
xi=(Math.round(((widthact/12)-xf)*12)); 
document.getElementById(xtdim).innerHTML=(xf + "\'-" + xi + "\"");


xsdim=("sidedim" + xclay); 
yf=(depthact/12);
yf=(Math.floor(yf));
yi=(Math.round(((depthact/12)-yf)*12)); 
document.getElementById(xsdim).innerHTML=(yf + "\'-" + yi + "\"");
   }   
    }




/* ===================================================================================================================
**********************************************************************************************************************
**********************************************************************************************************************
**********************************************************************************************************************
**********************************************************************************************************************
**************************************                         *******************************************************
**************************************       MOVE OBJECTS      *******************************************************                
*     *****************************                         ******************************************************* 
***********************************************************************************************************************
**********************************************************************************************************************
**********************************************************************************************************************        
*****************************************************************************
==================================================================================================================== */

// MOVE WALL --------------------------------------------------------

function highLight(wall,side){


wall=document.getElementById(wall);
wall.style.background="#ff0000";
mside=side;
}
function unhighLight(wall){  

wall=document.getElementById(wall);
wall.style.background="#000066";

}
      
function startWall(event)
{

if (moveloop==10)
{

a=(mwall.substr(4,1));
x=(mwall.substr(5));
y=("room" + x);
roomact=y;

  
switch (a)
 {
case ("t"):
moveTop(event,y);
break;

case ("r"):
moveRight(event,y);

break;

case ("b"):
moveBottom(event,y);
break;

case ("l"):
moveLeft(event,y);
break;
}

}
}

function moveWall(wall){

moveloop=10;

mwall=(wall);
wall=document.getElementById(wall);
wall.style.background="#ffff00";
 
}


function stopWall(wall,side){
moveloop=0;
wall=document.getElementById(wall);
wall.style.background="#000066";
wall.style.border="0px solid #6495ED";
side=side;
undoSave();
roomact="room0";

}




// MOVE BOTTOM -----------------------------------------------------------------------------------------------------

function moveBottom(event,mroom) {
if (moveloop==10 && xpan==0)
{
room=document.getElementById(mroom);
b=room.style.top;

b =(b.replace("px","")*1);
y=(event.clientY);
//y=(y.replace("px","")*1);
y=(y-scrollyy-6);





dxx=(y-b);

room.style.height=((dxx) + "px");

dxx=(dxx/dscale);


moveroom=document.getElementById(mroom);
moveroom=moveroom.id;
updateRoomsize(room);
checkDimension();     

openRoom('h','in',dxx);
 
}
}



// MOVE TOP  -----------------------------------------------------------------------------------------------------

function moveTop(event,mroom) {
if (moveloop==10 && xpan==0)
{
room=document.getElementById(mroom);
b=room.style.top;
a=room.style.height;
a =(a.replace("px","")*1);
b =(b.replace("px","")*1);

y=(event.clientY);
y=(y-(scrollyy)-8);


c=(b-y);
nheight=(a+c);

room.style.top=((y) + "px");
room.style.height=((nheight) + "px");


xyy=(Math.round(y/dscale));
nheight=(Math.round(nheight/dscale));


moveroom=document.getElementById(mroom);
moveroom=moveroom.id;
updateRoomsize(room);
checkDimension();

openRoom('y','in',xyy);
openRoom('h','in',nheight);

   
 }
}


// MOVE RIGHT -----------------------------------------------------------------------------------------------------

function moveRight(event,mroom) {

if (moveloop==10 && xpan==0)
{
room=document.getElementById(mroom);
b=room.style.left;
a=room.style.width;

a =(a.replace("px","")*1);
b =(b.replace("px","")*1);
b=(a+b); // right side

x=(event.clientX);

y=(event.clientY);
x=(x-8);
x=(x-scrollxx);


c=(x-b);
nwidth=(a+c);


room.style.width=((nwidth) + "px");


nwidth=(nwidth/dscale);

moveroom=document.getElementById(mroom);
moveroom=moveroom.id;
updateRoomsize(room);
checkDimension();   
openRoom('w','in',nwidth);
 
}
}



// MOVE LEFT -----------------------------------------------------------------------------------------------------

function moveLeft(event,mroom) {
if (moveloop==10 && xpan==0)
{
room=document.getElementById(mroom);
b=room.style.left;
a=room.style.width;
a =(a.replace("px","")*1);
b =(b.replace("px","")*1);

x=(event.clientX);
y=(event.clientY);
x=(x-5);
x=((x-scrollxx)*1);


c=(b-x);
nwidth=(a+c);


room.style.left=((x) + "px");
room.style.width=((nwidth) + "px");

wxx=(x/dscale);
nwidth=(nwidth/dscale);


moveroom=document.getElementById(mroom);
moveroom=moveroom.id;
updateRoomsize(room);
checkDimension();  
openRoom('w','in',nwidth);
openRoom('x','in',wxx);

}
}

// -- UPDATE DIMENSIONS SHOWN IN ROOM BOX ---------------


function updateRoomsize(room){
xx=room.style.width;
yy=room.style.height;
xx=(xx.replace("px","")*1);
yy=(yy.replace("px","")*1);
xx=(xx/dscale);
yy=(yy/dscale);

xf=(xx/12);
xf=(Math.floor(xf));
xi=(Math.floor(((xx/12)-xf)*12));

yf=(yy/12);
yf=(Math.floor(yf));
yi=(Math.floor(((yy/12)-yf)*12));
mr=(moveroom.substring(4));
document.getElementById("wf" + mr).innerHTML=xf;
document.getElementById("wi" + mr).innerHTML=xi;
document.getElementById("df" + mr).innerHTML=yf;
document.getElementById("di" + mr).innerHTML=yi;

          




//------------------------------------------------------

}






/*   ****************************************************************************
****************************************************************************
*************************************************************************
****************************************************************************
*******************************************************************************
****************************************************************************     */



//-------- MOVE ROOM ------------------------------------------------------------------------------

function moveobj(event)
{

if (roomact !="room0")
{
roomn=document.getElementById(roomact).id;
zmove=(roomn.substring(4));

if (rmoveloop==zmove && xpan==0 && roomn==roomact)
{


room=document.getElementById(roomact);
a=document.getElementById(roomact).getElementsByTagName("p");
a=(a[0].innerHTML);
room.className="room" + a;



ozindx=room.style.zIndex;
if(ozindx!=zindx)
{
zindx=(zindx+1);
room.style.zIndex=zindx;
}
room.style.opacity =".9";
room.style.filter = 'alpha(opacity=' + 90 + ')';




x=(event.clientX);
y=(event.clientY);
xx=((x)-(xdist));
yy=((y)-(ydist));
if(xx > 0)
{
room.style.left=((xx)+ "px");
room.style.top=((yy) + "px");

xx=(xx/dscale);
yy=(yy/dscale);

openRoom('x','in',xx);
openRoom('y','in',yy);


}

}
checkDimension();
}

}

function roomMove(event,room){

roomn=document.getElementById(room).id;
room=document.getElementById(room);

rmoveloop=(roomn.substring(4));
x=(event.clientX);
y=(event.clientY);
ey=room.style.top;
ex=room.style.left;
ex=(ex.replace("px",""));
ey=(ey.replace("px",""));
xdist=(x-ex);
ydist=(y-ey);
}

function roomStop(event,room){ 

rmoveloop=0;
a=document.getElementById(room).getElementsByTagName("p");
a=(a[0].innerHTML);                   
   
moveroom=(room);
room=document.getElementById(room);  

room.className="room" + a;
room.style.cursor="pointer";
room.style.opacity ="1.0";
room.style.filter = 'alpha(opacity=' + 100 + ')';  
}


/* =====================================================================================================================
************************************************************************************************************************
************************************************************************************************************************
***********************************************************************************************************************
***********************************************************************************************************************
**************************************                         *******************************************************
**************************************     MODIFY OBJECTS      ********************************************************                        **************************************                         **********************************************************
**************************************                         ***********************************************************     ***********************************************************************************************************************
*************************************************************************************************************************
*************************************************************************************************************************                              ***************************************************************************************************************************  
=====================================================================================================================  */

// DELETE ROOM ---------------------------------------------------------------------
//-----------------------------------------------------------------------------

function removeRm()
  {

var el = document.getElementById(roomact);
x=(roomact.substring(4));

if (el !== null)
{
roomcl=el.className;
el.parentNode.removeChild(el);
document.getElementById("modify").style.display="none";
document.getElementById("statusbar").innerHTML="Room Deleted";
undoSave();
}

roomloc[x]=null;
checkDimension();
roomact="room0";
  }


function modRoom(event){

mr=(roomact.replace("room",""));

moveloop=0;

rooma=("room" + mr);
roomact=("room" + mr);
  
room=document.getElementById(rooma);

if (room !==null)
{
x=(event.clientX);
y=(event.clientY);
ey=room.style.top;
ex=room.style.left;
ew=room.style.width;
ex=(ex.replace("px","")*1);
ey=(ey.replace("px","")*1);
ew=(ew.replace("px","")*1);
ey=(ey+scrollyy);
ex=(ex+scrollxx);

ex=(ex*1);
ew=(ew*1);
 
roomname="roomname" + (mr);
newname=document.getElementById(roomname).innerHTML;
document.getElementById("rname").value=newname;


if (ex<800){
nx=(ex+ew+20);
}
else{
nx=(ex-170)
}
document.getElementById("modify").style.display="inline";
document.getElementById("modify").style.top=ey + "px";
document.getElementById("modify").style.left=nx+"px";
a=document.getElementById(rooma).getElementsByTagName("p");
a=(a[0].innerHTML);

switch (a)
{
case ("a"):
x=0;
break;

case ("b"):
x=1;
break;

case ("c"):
x=2;
break;

case ("d"):
x=3;
break;

case ("e"):
x=4;
break;
}

document.getElementById("flooring").selectedIndex=x;

}
else
{
alert("No room selected");
}
}


//-----------------------------------------------------------------------------------------------

function changeRoom(){

room="room" + (mr);

roomname="roomname" + (mr);
document.getElementById("modify").style.display="none";

newname=document.getElementById("rname").value;
document.getElementById(roomname).innerHTML=newname;






//------CHANGE FLOORING ------------------------------------------------------------------------------
xfl=document.getElementById("flooring").value;
xflv=("room" + xfl);
document.getElementById(room).className=xflv;
a=document.getElementById(room).getElementsByTagName("p"); 
a[0].innerHTML=xfl;

a=document.getElementById(room).getElementsByTagName("p");  // check to see if the item is stairs
a=(a[0].innerHTML);
if (a=="sv" && xw > xd)
{
a=document.getElementById(room).getElementsByTagName("p"); 
a[0].innerHTML="sh";
document.getElementById(room).className="roomsh";
 checkDimension();

}
 undoSave();
 
 openRoom('n','in',newname);
 openRoom('c','in',xfl);
}





/* =====================================================================================================================
************************************************************************************************************************
************************************************************************************************************************
***********************************************************************************************************************
***********************************************************************************************************************
**************************************                         *******************************************************
**************************************     SELECT OBJECTS       ********************************************************                        **************************************                         **********************************************************
**************************************                         ***********************************************************     ***********************************************************************************************************************
*************************************************************************************************************************
*************************************************************************************************************************                              ***************************************************************************************************************************  
=====================================================================================================================  */

function roomSel(room,event){

if (moveloop !=5)
{
layer=document.getElementById(room);

rclass=layer.className;
nclass=rclass + "1";
layer.className=nclass;
}
 }

function roomUnsel(room){
layer=document.getElementById(room);
layer.className=rclass;
}

function highLightroom(room,event){
x=(room);

if (x !== roomact)
{
undoSave();
}

clearText();

y=(x.replace("room","roomname"));
z=(x.replace("room",""));


rtype=(room.substring(0,1));
 

layer=document.getElementById(room);

layer.style.border="dashed 5px #ff0000";
layer.style.cursor="move";

yy=document.getElementById(y);

if (rtype == "r")
{
yyy=yy.innerHTML;
  
document.getElementById("statusbar").innerHTML="<span style=\"cursor:pointer\" onclick=\"modRoom(event,0)\")>" + yyy + " selected.<br>Click here to edit this room</span>";
}
else
{
document.getElementById("statusbar").innerHTML="Text selected";

}

if (roomact != x)
{
z=document.getElementById(roomact);
if (z !== null)
{
z.style.border="solid 5px #000066";
            
}
roomact=x;

}
else
{
document.getElementById("statusbar").innerHTML="Layout your plan...";

if (yy !== null && rtype=="r")
{
document.getElementById(roomact).style.border="solid 5px #000066";
}
else
{
document.getElementById(roomact).style.border="solid 0px";
}


document.getElementById(roomact).style.cursor="pointer";
roomact="room0";

}

 }

/* =====================================================================================================================
************************************************************************************************************************
************************************************************************************************************************
***********************************************************************************************************************
***********************************************************************************************************************
**************************************                         *******************************************************
**************************************     INSERT ROOM         ********************************************************                        **************************************                         **********************************************************
**************************************                         ***********************************************************     ***********************************************************************************************************************
*************************************************************************************************************************
*************************************************************************************************************************                              ***************************************************************************************************************************  
=====================================================================================================================  */


function addRm(x)
{
document.getElementById("addroom").selectedIndex=x;


newRoom();   
}

  
function newRoom() {

nroom=document.getElementById('addroom').value;
if(document.getElementById('welcomeinfo')!=null)
{
hideLayer('welcomeinfo');
}

xwf=(nroom.substring(1,3)*1);
xdf=(nroom.substring(3,5)*1);
roomcl=(nroom.substring(0,1));
nroom=(nroom.substring(5));
x=(Math.abs(scrollxx)+300);
y=Math.abs((scrollyy)-100); 

var adz=ad;
if(ad > 20)
{
adz=(ad-20);
}

x=(x+(adz*10));
y=(y+(adz*10))



wx=(xwf*12);
dx=(xdf*12);

wxx=(wx*dscale);
dxx=(dx*dscale);


ad=(ad+1);

//----------------------------------

zindx=(zindx+1);

insertroom="<div id=\"room" + ad + "\" class=\"room" + roomcl + "\" style=\"position:absolute;left:" + (x) + "px;top:" + (y) + "px;width:" + (wxx) + "px;height:" + (dxx) + "px;z-index=" + zindx + "\"><p  class=\"ht\">" + roomcl + "</p><div id=\"walll" + ad + "\" class=\"walll\" onmousedown=\"moveWall('walll" + ad + "')\" onmouseup=\"stopWall('walll" + ad + "','l')\" onmouseout=\"unhighLight('walll" + ad + "','l')\" onmouseover=\"highLight('walll" + ad + "','l')\"  ></div><div id=\"wallr" + ad + "\" class=\"wallr\" onmousedown=\"moveWall('wallr" + ad + "')\" onmouseup=\"stopWall('wallr" + ad + "','r')\" onmouseout=\"unhighLight('wallr" + ad + "','r')\" onmouseover=\"highLight('wallr" + ad + "','r')\" ></div><div id=\"wallt" + ad + "\" class=\"wallt\" onmousedown=\"moveWall('wallt" + ad + "')\" onmouseup=\"stopWall('wallt" + ad + "','t')\" onmouseout=\"unhighLight('wallt" + ad + "','t')\" onmouseover=\"highLight('wallt" + ad + "','t')\" ></div><div id=\"wallb" + ad + "\" class=\"wallb\" onmousedown=\"moveWall('wallb" + ad + "')\" onmouseup=\"stopWall('wallb" + ad + "','b')\" onmouseout=\"unhighLight('wallb" + ad + "','b')\" onmouseover=\"highLight('wallb" + ad + "','b')\" ></div><table class=\"dtable\" cellpadding=\"0\" cellspacing=\"0\"><tr><td onmouseover=\"roomSel('room" + ad + "')\"  onmouseout=\"roomUnsel('room" + ad + "')\" onmousedown=\"roomMove(event,'room" + ad + "')\" onmouseup=\"roomStop(event,'room" + ad + "')\" onmousemove=\"moveobj(event,'room" + ad + "')\" ondblclick=\"modRoom(event,'xx" + ad + "')\" onclick=\"highLightroom('room" + ad + "',event)\"><div id=\"roomname" + ad + "\" class=\"roomname\">" + nroom + "</div><div class=\"dimensions\"><span id=\"wf" + ad + "\">" + xwf + "</span>'-<span id=\"wi" + ad + "\">0&quot;</span> X <span id=\"df" + ad + "\">" + xdf + "</span>'-<span id=\"di" + ad + "\">0&quot;</span></div></td></tr></table></div>";
          



x=(x/dscale);
y=(y/dscale);
wxx=(wxx/dscale);
dxx=(dxx/dscale);


if (roomcl=="d" || roomcl=="e")
{
fin=0;
}
else
{
fin=1
}


roomloc[ad]=(x + "," + y + "," + wxx + "," + dxx +  "," + roomcl + "," + zindx + "," + nroom + "," + fin + "," + afloor);




a=document.getElementById(xcopy).innerHTML;
document.getElementById(xcopy).innerHTML=(a + insertroom);

document.getElementById("addroom").selectedIndex="0";

document.getElementById("statusbar").innerHTML=nroom + " added."; 
undoSave();
checkDimension();
 xz=("room" + ad);


if(ad==6)
{ 
messx=document.getElementById('savednames').value;
messy=(messx.split(","));

if(messy[1]==null )
{
checkMessage();
sentone=1;
}
}
}









function validate_form(thisform)
{

with (thisform)
  {
  if (validate_email(email,"Not a valid e-mail address!")==false)
    {return false;}
  }
}



function confirmMessage()
{
emlx=document.getElementById("contactem").innerHTML;
document.getElementById("contactem").innerHTML="Your message has been sent.<br><br>Thank you!";



var t1=setTimeout("document.getElementById('contactem').innerHTML=emlx",8000);
}

function switchLay(name,open){
x=1;
i=0;

while (i!="xx")
{
a=((name) + x);
b=((name) + (open));
b=document.getElementById(b);

a=document.getElementById(a);
if (a !== null)
{
if (a != b){
a.style.display="none";
toolsopen=0;
}
}
else
{
i="xx"
}
x=x+1;
}


if (b !== null){


var myLayer = b.style.display;
if(myLayer=="none"){
b.style.display="block";
toolsopen=1;
} else {
b.style.display="none";
toolsopen=0;
}

} 
}


function switchTool(name,open)
{
if (toolsopen==1)
{
switchLay(name,open);

}
}


function newPlan(){
document.getElementById(xcopy).innerHTML="";
ad=(0*1);
arleft="zzqb1|1144qq1qb2|1288qq2";
arright="zzqb1|1000qq1qb2|1144qq2";
artop="zzqb1|1044qq1qb2|1044qq2";
arbot="zzqb1|900qq1qb2|900qq2";

hideLayer('newplan');
}



function expand(layer)
{
document.getElementById(layer).style.height="340px";

}


