var usave=new Array();
var xpan=0;
var ypan=0;
var xstart=4;
var xstartw=4;
var tad=0;
var planelevation=0;
var planimage="none";
var plan1="0"
var xact="stay";
var roomloca1=new Array();
var roomloca2=new Array();     
var scrollxx=-500;
var scrollyy=-500;
var username="";
var useremail="";
var userplan="";


/* =======================================================================================================================
*********************************************  Elevations   **********************************************************
======================================================================================================================== */

function showElev(plan){
plan1=(plan);
document.getElementById("elevation").style.display="inline";
planimage=("http://custom-house-plan.com/planimage/" + plan + "-fm.jpg");
document.getElementById("ele").src=planimage;
}

function addElev(){
document.getElementById("ele1").src=planimage;
document.getElementById("ele1").style.display="inline";
planelevation=(plan1);
if(planelevation==plan1)
{
swapLayer('elevationbox','elevation');
}
document.getElementById("usethis").innerHTML="Change Exterior Style";
}

function elevMatch()
{
planimage=("http://custom-house-plan.com/planimage/" + planelevation + "-fm.jpg");
document.getElementById("ele").src=(planimage);
plan1=planelevation;
}





/* =======================================================================================================================
*********************************************  CHANGE FLOORS   **********************************************************
======================================================================================================================== */
function changeFloor(x) {


 if (x !=null)
 {
 afloor=x;
 }
 

 y=(Math.round(dscale*100));                       
z="url('images/paper" + y + ".jpg')";
document.getElementById('copythis').style.backgroundImage=z;
document.getElementById('copythis2').style.backgroundImage=z;
document.getElementById('reference').style.backgroundImage=z;

if(afloor==2)
{
swapLayer('copythis','copythis2');
xcopy="copythis";
xcopyr="copythis2";
afloor=1;
document.getElementById("copythis").className="xcopy";
document.getElementById("statusbar").innerHTML="First Floor";
document.getElementById('cfloor').innerHTML="Floor:1";
flashText('First Floor Plan');
}
else
{
swapLayer('copythis2','copythis');
xcopy="copythis2";
afloor=2;
xcopyr="copythis";
document.getElementById("copythis2").className="xcopy";
document.getElementById("statusbar").innerHTML="Second Floor";
 document.getElementById('cfloor').innerHTML="Floor:2";
flashText('Second Floor Plan');

}
checkDimension();
}

// --------- REFERENCE FLOOR ---------------------------------------------------------------------------------------

function drawReference()
{
var currentroom="";
var iroom="";
for (i in roomloc)
{
if(roomloc[i] != null)
{
roomact=("room" + i);
openRoom("s","out");
a=roomvalue;
if (a != afloor && a != null)
{
openRoom("x","out");
xx=(roomvalue);
xx=(Math.round(xx*dscale));
openRoom("y","out");
yy=(roomvalue);
yy=(Math.round(yy*dscale));
openRoom("w","out");
ww=(roomvalue);
ww=(Math.round(ww*dscale));
openRoom("h","out");
hh=(roomvalue);
hh=(Math.round(hh*dscale));
openRoom("z","out");
zz=(roomvalue);



currentroom="<div class=\"refroom\" style=\"left:" + xx + "px;top:" + yy + "px;width:" + ww + "px;height:" + hh + "px;z-index:" + zz + ";\"></div>";
iroom=(iroom+currentroom);

  }
}

}
 document.getElementById('reference').innerHTML=iroom;
 }


function showRef()
{

lon=document.getElementById(xcopy);  

if(lon.className=="xcopy")
{
  document.getElementById('reference').style.display="inline";  
   
lon.className="xcopy1";
lon.style.backgroundImage="none";

drawReference();

}
else
{
lon.className="xcopy";
 y=(Math.round(dscale*100));                       
z="url('images/paper" + y + ".jpg')";
lon.style.backgroundImage=z;
document.getElementById('reference').style.display="none";

 

}

 roomact="room0";

}
 

/* =======================================================================================================================
*********************************************  Automatically insert email addresss*****************************************
======================================================================================================================== */

function udEmail(x)
{
email=document.getElementById(x).value;
document.getElementById("email1").value=(email);
document.getElementById("email2").value=(email);
document.getElementById("email3").value=(email);
document.getElementById("email5").value=(email);
}


    
/* =====================================================================================================================
************************************************************************************************************************
************************************************************************************************************************
***********************************************************************************************************************
***********************************************************************************************************************
**************************************                         *******************************************************
**************************************         Plan Save       ********************************************************                        **************************************                         **********************************************************
**************************************                         ***********************************************************     ***********************************************************************************************************************
*************************************************************************************************************************
*************************************************************************************************************************                              ***************************************************************************************************************************  
=====================================================================================================================  */
function planSave(xact)
{
 
xstring="";
xstring2="";
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);

roomname=("roomname" + z);
  
roomcl=a.className;
x=a.style.left;
x=(x.replace("px",""));
x=(x/dscale);
y=a.style.top;
y=(y.replace("px",""));
y=(y/dscale);
wx=a.style.width;
wx=(wx.replace("px",""));
wx=(wx/dscale);
wy=a.style.height;
wy=(wy.replace("px",""));
wy=(wy/dscale);
zindx=a.style.zIndex;

b=document.getElementById(roomname).innerHTML;

if (apar=="copythis")
{
xstring=(xstring + "&qqq&" + room + "&v&" + roomcl + "&v&" + x + "&v&" + y + "&v&" + wx + "&v&" + wy+ "&v&" + b + "&v&" + zindx);
}
if (apar=="copythis2")
{
xstring2=(xstring2 + "&qqq&" + room + "&v&" + roomcl + "&v&" + x + "&v&" + y + "&v&" + wx + "&v&" + wy+ "&v&" + b + "&v&" + zindx);
}



}
z=(z+1);
}

//---- text save ---------------------------------

tstring="";
tstring2="";
z=1;

var i=0;
for (i=1;i<=tad;i++)
{
room=("txxt" + z);
txt=("tx" + z);
xcell=("td" + z);

xcell=document.getElementById(xcell);
txt=document.getElementById(txt);
a=document.getElementById(room);


if (a !== null)
{
apar=(a.parentNode.id);


x=a.style.left;
x=(x.replace("px",""));
x=(x/dscale);
y=a.style.top;
y=(y.replace("px",""));
y=(y/dscale);
wx=xcell.style.width;
wx=(wx.replace("px",""));

b=txt.value;

if (apar=="copythis")
{
tstring=(tstring + "&qqq&" + room + "&v&" +  x + "&v&" + y + "&v&" + wx + "&v&" + b);
}
if (apar=="copythis2")
{
tstring2=(tstring2 + "&qqq&" + room + "&v&" +  x + "&v&" + y + "&v&" + wx + "&v&" + b);
}

}
z=(z+1);
}


showLayer('saveplan');

savethis=(tstring + xstring);
savethis2=(tstring2 + xstring2);

document.getElementById("plan").value=savethis;
document.getElementById("plan2").value=savethis2;
document.getElementById("planelevation").value=planelevation;

if ((xact)=="leave")
{
document.getElementById("savetitle").innerHTML="Do you want to save your plan before you leave?";
document.getElementById("nothanks").innerHTML="<a href=\"/index.php\" style=\"font-size:12px\">No Thanks</a>";
document.getElementById("leave").value="2";
}

else if ((xact)=="finalize")
{
document.getElementById("savetitle").innerHTML="Finalize Plan<br><br>Step one:";
document.getElementById("nothanks").innerHTML=".";
document.getElementById("leave").value="3";
}


else
{
document.getElementById("savetitle").innerHTML="Save Plan";
document.getElementById("leave").value="0";
document.getElementById("nothanks").innerHTML=".";
}
}



/* =====================================================================================================================
************************************************************************************************************************
************************************************************************************************************************
***********************************************************************************************************************
***********************************************************************************************************************
**************************************                         *******************************************************
**************************************         SCROLLING       ********************************************************                        **************************************                         **********************************************************
**************************************                         ***********************************************************     ***********************************************************************************************************************
*************************************************************************************************************************
*************************************************************************************************************************                              ***************************************************************************************************************************  
=====================================================================================================================  */

 function togglePan()
  {
  if (ypan==1)
  {
ypan=0;
document.getElementById(xcopy).style.cursor="crosshair"; 
document.getElementById('reference').style.cursor="crosshair"; 
document.getElementById('statusbar').innerHTML="You can also use the right mouse button to pan your drawing";
     document.getElementById('panicon').src="images/pan1.gif";
    }      
  else
  {
ypan=1  
document.getElementById(xcopy).style.cursor="pointer";
 document.getElementById('reference').style.cursor="pointer";
document.getElementById('statusbar').innerHTML="Pan Drawing"; 
document.getElementById('panicon').src="images/pan3.gif";

     } 
  }
         
function startpanDrawing(event) 
{
if (ypan==1 && (event.button==1 || event.button==0))
{
zpan=1; 
}
else
{ zpan=0;}


if (event.button==2 || event.button==4 || zpan==1)
  {
  xstart=((event.clientX)-scrollxx);
  ystart=((event.clientY)-scrollyy);
document.getElementById('reference').style.cursor="pointer";
document.getElementById(xcopy).style.cursor="pointer";

xpan=1;
clearRoom(); 
clearText();  
  }


else
{
xpan=0;
clearRoom(); 

if(rmoveloop==0)
{
clearText(); 
}

}


}
  
// ---------------------------------------------------------------------------------------------------------------------

function panDrawing(event)
{
dxx=0;
 
 
if (xpan==1)
  {
x=event.clientX;
y=event.clientY;



scrollxx=(x-xstart);
scrollyy=(y-ystart);


layer=document.getElementById(xcopy);
layer.style.top=scrollyy + "px";
layer.style.left=scrollxx + "px";

layer=document.getElementById(xcopyr);
layer.style.top=scrollyy + "px";
layer.style.left=scrollxx + "px";

layer=document.getElementById("reference");
layer.style.top=scrollyy + "px";
layer.style.left=scrollxx + "px";



  }

else
{
xpan=0;
}
 }
 
 
function unpanDrawing(event)
{
if (event.button==2 || event.button==4 || event.button==1 || event.button==0)
  {
if (ypan==0)
{
document.getElementById(xcopy).style.cursor="crosshair";
document.getElementById('reference').style.cursor="crosshair"; 
document.getElementById('panicon').src="images/pan1.gif";
}
xpan=0;
}

moveloop=0;
rmoveloop=0;
}


/* ******************************************************************************************************************
**************************************  end scrolling *****************************************************************
******************************************************************************************************************  */



// Expand form depending on input -----------------------------------------------

function expForm(value,layer)
{
a=(value);
if (a==1)
{
document.getElementById(layer).style.display="inline";
}
else
{
document.getElementById(layer).style.display="none";
}

}

function contactUs(value)
{
if ((value)=="y")
document.getElementById("wecontact").innerHTML="Please check your email for a message from <a href=\"mailto:info@custom-house-plan.com\">info@custom-house-plan.com</a>";

else
{
document.getElementById("wecontact").innerHTML="";
}

}


function clearRoom()
 // clear any open rooms ----  
 {
 if (roomact!="room0" && rmoveloop==0)
{
document.getElementById("statusbar").innerHTML="Lay out your plan...";
layer=document.getElementById(roomact); 
if (layer !=null)
{       
layer.style.border="solid 5px #000066";  
layer.style.cursor="pointer";
}
if(moveloop==0)
{
roomact=("room0");
}
}
hideLayer('modify');
hideLayer('textedit'); 
           
}

function clearText()
 // clear any open text ----  
 {
 if (textact!= 0 && tmoveloop==0)
{
document.getElementById("statusbar").innerHTML="Lay out your plan...";
layer=document.getElementById(textact); 
layer2=document.getElementById(textact+"i"); 
if (layer !=null)
{       
layer.className="text";
layer2.style.display="none";
textact=0;
}
}

hideLayer('textedit');            
}











/* *********************************************************************************************************
*****************************   UNDO               ********************************************************
***************************************************************************************************   */

// save for undo
function undoSave()
{
unum=(unum+1);

 /*
usave[unum]=document.getElementById('maincontent').innerHTML;
roomloca1[unum]=roomloc;
roomloca2[unum]=roomloc2;



}

function undoUse()
{
redosave();
unum=(unum-1);
if (unum >=0)
{
document.getElementById('maincontent').innerHTML=usave[unum];
roomloc=roomloca1[unum];
roomloc2=roomloca2[unum];
}
*/
}

function redosave()
{
redox=document.getElementById('maincontent').innerHTML;
}

function exFor()
{
/*
var r=confirm("Press a button");
if (r==true)
  {
//
  }
else
  {
return false;
  } 
  */
  }                

  function keypress(e)  
   {
    var keycode;
     
    if (window.event) // IE
    {
        keycode = e.keyCode;
    }
    else // Netscape/FF/Op
    {
        keycode = e.which;
    }
//alert(keycode);


x=document.getElementById("modify").style.display;

  if(keycode==46 && x== "none")
  {
  removeRm();
  textDel();
  } 
  
if(keycode==13 && x == "inline")
  {
  changeRoom();
  }         
  
  if(keycode==27 && x == "inline")
  {
document.getElementById('modify').style.display="none";
  }    
 if (keycode==27 && x == "none" && roomact != "room0")
 {  
highLightroom(roomact,event); 
}             
   }


function newWin()
{
window.open('houseplan.php','_blank','directories=no,toolbar=no,location=0,menubar=no,scrollbars=no,resizable=yes,width=1600,height=1200,status=no');

   }
           
// scaling ------------------------------------------------------
 function planScale(x){
 if(x=="in")
 {
 newscale=((dscale*1)+.1)  
 if(newscale > 1.1)
 {
 newscale=dscale;
document.getElementById('statusbar').innerHTML="You have reached the zoom limits";
flashText('You have reached the zoom limits');
 }   
  }
 else
 {
newscale=((dscale*1)-.1)
}   
 if(newscale < 0.5)
 {
 newscale=dscale;
 document.getElementById('statusbar').innerHTML="You have reached the zoom limits";
            flashText('You have reached the zoom limits');
 } 
 
 if(newscale != dscale)
 {         

 y=(Math.round(newscale*100));
 document.getElementById('scdisp').innerHTML=(y + "%");
 
zbw=(2500 * newscale); 
zbh=(1500 * newscale);  

z="url('images/paper" + y + ".jpg')";
if(document.getElementById(xcopy).className!="xcopy1")
{ 
document.getElementById('copythis').style.backgroundImage=z;
document.getElementById('copythis2').style.backgroundImage=z;
}
 
document.getElementById('reference').style.backgroundImage=z; 
 document.getElementById('copythis').style.width=zbw;
  document.getElementById('copythis').style.height=zbh;
  document.getElementById('copythis2').style.width=zbw;
  document.getElementById('copythis2').style.height=zbh;
   document.getElementById('reference').style.width=zbw;
  document.getElementById('reference').style.height=zbh;  

i=0;
for (i in roomloc)
{

room=("room" + i);
if(roomloc[i] != null)
{
ra=document.getElementById(room);


roomact=(room);

openRoom('x','out');
xx=roomvalue;   
openRoom('y','out');
yy=roomvalue;
openRoom('w','out');
wx=roomvalue;
openRoom('h','out');
wy=roomvalue;

ra.style.left=(Math.round(xx*newscale)) + "px";                
ra.style.top=(Math.round(yy*newscale)) + "px";   
ra.style.width=(Math.round(wx*newscale)) + "px";  
ra.style.height=(Math.round(wy*newscale)) + "px";

 }
 i++; 
}

layer=document.getElementById(xcopy);
layer.style.top=(scrollyy) + "px";
layer.style.left=(scrollxx) + "px";

layer=document.getElementById(xcopyr);
layer.style.top=(scrollyy) + "px";
layer.style.left=(scrollxx*newscale) + "px";


// text scale ----------------------
i=0;
for (i=1;i<=tad;i++)
{
a=("txxt" + i);
b=document.getElementById(a);
if(b != null)
{
x=b.style.top;
x =(x.replace("px","")*1);
x=(Math.round(x/dscale));
x=(Math.round(x*newscale));
y=b.style.left;
y =(y.replace("px","")*1);
y=(Math.round(y/dscale));
y=(Math.round(y*newscale));
z=b.style.width;
z =(z.replace("px","")*1);
z=(Math.ceil(z/dscale));
z=(Math.ceil(z*newscale));
   

switch(newscale)
{
case 1:
zz=15;
break;

case 1.1:
zz=14;
break;

case 0.9:
zz=13;
break;

case 0.8:
zz=11;
break;
   
case 0.7:
zz=10;
break;

case 0.6:
zz=8;
break;

case 0.5:
zz=7;
break;
}


b.style.top=(x + "px");
b.style.left=(y + "px");
if(z>10)
{
b.style.width=(z + "px");
}
b.style.fontSize=(zz + "px");
  }
  }
 }            
 roomact="room0";
dscale=newscale;            
checkDimension();
drawReference();

 }
 
 
 //---- text save ---------------------------------

/*
z=1;

var i=0;
for (i=1;i<=tad;i++)
{
room=("txxt" + z);
txt=("tx" + z);
xcell=("td" + z);

xcell=document.getElementById(xcell);
txt=document.getElementById(txt);
a=document.getElementById(room);


if (a !== null)
{
apar=(a.parentNode.id);


x=a.style.left;
x=(x.replace("px",""));
x=(x/dscale);
y=a.style.top;
y=(y.replace("px",""));
y=(y/dscale);
wx=xcell.style.width;
wx=(wx.replace("px",""));

b=txt.value;

if (apar=="copythis")
{
tstring=(tstring + "&qqq&" + room + "&v&" +  x + "&v&" + y + "&v&" + wx + "&v&" + b);
}
if (apar=="copythis2")
{
tstring2=(tstring2 + "&qqq&" + room + "&v&" +  x + "&v&" + y + "&v&" + wx + "&v&" + b);
}

}
z=(z+1);
}
*/


//scrollyy=(scrollyy*newscale);
//scrollxx=(scrollxx*newscale); 








function terms()
{
document.getElementById("terms").innerHTML="Terms of use and license agreement:<br>THIS SOFTWARE IS PROVIDED BY RYAN MOE HOME DESIGN \"AS IS\" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL RYAN MOE HOME DESIGN BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.";

}
