//============================================================FUNCTIONS

/*cell******************************************************
 *  Constructor for cell object
 *****************************************************************/
function cell(name, image, hAlign, vAlign, rows, columns, type)
{
   this.name = name;      //name (needed to write HTML call to drawCell)
   this.image = image;    //image is the picture which is displayed
   this.hAlign = hAlign;  //vAlign is the vertical orientation ('top' or 'bottom')
   this.vAlign = vAlign;  //hAlign is the horizontal orientation ('left' or 'right')
   this.type = type;      //type determines available options (corner, spandrail, baluster)
   this.rows = rows;      //rows is the rowspan
   this.columns = columns;//columns is the colspan
   this.selected = false; // boolean value which becomes true when cell is clicked
  
   return this;
}


/*changeCell******************************************************
 * Preconditions: .image is the name of the image that will replace
 * 		current image on the door. 
 *
 * Postconditions: redraws the door
 * 
 *****************************************************************/
function changeCell(image)
{
   for(i in CellList)
   {
      if(CellList[i].selected)       // find selected cell
      {
         CellList[i].image = image;  // change cell's image to 'image'
      }
   }
   drawDoor();                       // redraw door
}


/*display_div************************************************************
 * Preconditions: Divisions are all located on door pages.
 *
 * Postconditions:  Setting all the relevant divisions to "none" so that
 * 			they are not shown when the page starts up.
 ********************************************************************/
function display_div(s_div_id)
{
    // Hide other divisions
    bracketsTR.style.display = "none"
    bracketsTL.style.display = "none"
    bracketsBR.style.display = "none"
    bracketsBL.style.display = "none"
    bracketsTRR.style.display = "none"
    bracketsTLL.style.display = "none"
    bracketsBRR.style.display = "none"
    bracketsBLL.style.display = "none"
    spandrails.style.display = "none"
    static.style.display = "none"    

    s_div_id.style.display = ""     //display relevant division.
    document.location.href = "#top"  
}


/*drawCell************************************************************
 * Preconditions: theCell must refer to an externally defined cell 
 *                object.
 *
 * Postconditions: cell_str is returned.  It will contain HTML code to
 *                 display the cell.
 * 
 ********************************************************************/
function drawCell(theCell)
{
   cell_str = "";

   cell_str += "   <td align=" + theCell.hAlign;     // cell hAlign
   cell_str += " valign=" + theCell.vAlign;          // cell vAlign
   cell_str += " rowspan=" + theCell.rows;           // cell rowspan
   cell_str += " colspan=" + theCell.columns;        // cell colspan
   cell_str += " bgcolor=\"white\"";                   // cell bgcolor 
   cell_str += " onClick=\"selectCell(" + theCell.name + ");";
   cell_str += " display_div(" + theCell.type + ")\">";
                                                     // cell onClick action
   cell_str += "<img src=" + theCell.image;
   cell_str += " style=\"cursor:hand\" />";  // cell image
   cell_str += "</td>";                              // end cell

   return cell_str
}

/*printWindow************************************************************
 * Preconditions: door_str must be exteranlly defined.
 *                
 *
 * Postconditions: A new window is opened containing an image of the door
 * 			which is drawn based on the externally defined door_str
 ********************************************************************/
function printWindow()
{
	var print_str = "<html><title>printable</title><body>This is a test</body></html>"
	new_window = open("","displayWindow","width=300,height=600,left=10,top=10");

	// open new document 
	new_window.document.open();

	// Text of the new document
	// Replace your " with ' or \" or your document.write statements will fail
	new_window.document.write("<html><title>Printable Door</title>");

	new_window.document.write(door_str);
	new_window.document.write("<br><button onclick='window.print();window.close()'>PRINT</button>");
	new_window.document.write("</body></html>");


	// close the document
	new_window.document.close(); 

}



/*selectCell******************************************************
 * Preconditions: theCell is a cell object.
 *                It is passed BY REFERENCE.
 *                
 * Postconditions: The specified cell will be set to true, all 
 *                 other cells to false.
 * 
 *****************************************************************/
function selectCell(theCell)
{
   for(i in CellList)
   {
      CellList[i].selected=false;   // set all cells to unselected
   }
   theCell.selected=true;           // set theCell to selected
}


/*submit_door*******************************************************
 * Preconditions: door_str must be externally defined, 
 *			door_type externally defined,
 *                CellList contains cell objects,
 *                frm_submit and input_div defined in the HTML code
 *
 * Postconditions: creates dynamic input fields which contain the 
 *                 relevant information about the customized door,
 *                 then it submits the form called frm_submit, which
 *                 holds the input field data. 
 *******************************************************************/
function submit_door()
{

	new_window = open("","displayWindow","width=800,height=600,left=10,top=10, scrollbars=yes, resizable=yes, toolbar=yes, menubar=yes");

	// open new document 
	new_window.document.open();

	// Text of the new document
	// Replace your " with ' or \" or your document.write statements will fail
	new_window.document.write("<html>\n<title>Submit Form</title>");
        new_window.document.write("\n<body>");
        new_window.document.write("\n<table>\n <tr>\n  <td>");
	new_window.document.write(door_str);
        new_window.document.write("\n  </td>\n  <td>");
        new_window.document.write(submit_form());

        var input_str="";
        input_str+="\n<table cellspacing=\"2\" cellpadding=\"4\" border=\"0\">";
        input_str+="\n <tr> ";
        input_str+="\n  <td>Door Design:</td>";
        input_str+="\n  <td>";
        input_str+="\n  <input type=\"Text\" size=\"20\" name=\"design\" value=\"Custom\" maxlength=\"50\">";
        input_str+="\n  </td>";
        input_str+="\n </tr>";
	input_str+="\n <tr> ";
        input_str+="\n  <td>Door Quantity:</td>";
        input_str+="\n  <td>";
        input_str+="\n  <input type=\"Text\" size=\"10\" name=\"quantity\" maxlength=\"10\">";
        input_str+="\n  </td>";
        input_str+="\n </tr>";
        input_str+="\n <tr>";
        input_str+="\n  <td>Door Measurements:</td>";
        input_str+="\n  <td>";
        input_str+="\n  A-B: <input type=\"Text\" size=\"10\" name=\"A-B\" maxlength=\"10\">";
        input_str+="\n  <br>C-D: <input type=\"Text\" size=\"10\" name=\"C-D\" maxlength=\"10\">";
        input_str+="\n  <br>E-F : <input type=\"Text\" size=\"10\" name=\"E-F\" maxlength=\"10\">";
        input_str+="\n  <br>G-H: <input type=\"Text\" size=\"10\" name=\"G-H\" maxlength=\"10\">";
	input_str+="\n  <br> <a href=\"http://www.vintagedoors.com/measuredoor.html\" target=\"_blank\">";
	input_str+="\nHow To Measure Your Door</a><br>";
	input_str+="\n  </td>";
        input_str+="\n </tr>";
        input_str+="\n <tr>";
        input_str+="\n  <td valign=\"top\">Choice of Wood:</td>";
        input_str+="\n  <td>";
        input_str+="\n   <input type=\"Text\" size=\"20\" name=\"woodchoice\" maxlength=\"20\">";
        input_str+="\n  <br />";
        input_str+="\n  <span class=\"footer\">(poplar, oak, ash, mahogany, other)</span></td>";
        input_str+="\n </tr>";
        input_str+="\n <tr>"; 
        input_str+="\n <td valign=\"top\">Screening:</td>";
        input_str+="\n <td>Fiberglass" 
        input_str+="\n <input type=\"radio\" name=\"screening\" value=\"fiberglass\">";
        input_str+="\n  Copper"; 
        input_str+="\n <input type=\"radio\" name=\"screening\" value=\"copper\">";
        input_str+="\n  Aluminum"; 
        input_str+="\n <input type=\"radio\" name=\"screening\" value=\"aluminum\">";
        input_str+="\n  <br />";
       	input_str+="\n  <span class=\"footer\">(all screen doors priced in fiberglass, you may choose copper or ";
        input_str+=" aluminum at additional charges)</span></td>";
        input_str+="\n  </tr>";
        input_str+="\n  <tr> ";
        input_str+="\n  <td>Storm Panel(optional):</td>";
        input_str+="\n  <td>Tempered"; 
        input_str+="\n  <input type=\"radio\" name=\"stormpanel\" value=\"tempered\">";
        input_str+="\n  Plexiglass"; 
        input_str+="\n  <input type=\"radio\" name=\"stormpanel\" value=\"plexiglass\">";
        input_str+="\n  </td>";
        input_str+="\n  </tr>";
        input_str+="\n  <tr>";
        input_str+="\n  <td>Frame Color:</td>";
        input_str+="\n  <td>White";
        input_str+="\n  <input type=\"radio\" name=\"framecolor\" value=\"white\">";
        input_str+="\n  Bronze"; 
        input_str+="\n  <input type=\"radio\" name=\"framecolor\" value=\"bronze\">";
        input_str+="\n  <br>";
        input_str+="\n  <span class=\"footer\">(for screen and storm panel)</span></td>";
        input_str+="\n  </tr>";
        input_str+="\n   <tr>";
        input_str+="\n  <td>Name:</td>";
        input_str+="\n  <td>";
        input_str+="\n  <input type=\"Text\" size=\"20\" name=\"name\" maxlength=\"50\">";
        input_str+="\n  </td>";
        input_str+="\n  </tr>";
        input_str+="\n   <tr>"; 
        input_str+="\n  <td>Address:</td>";
        input_str+="\n    <td>";
        input_str+="\n   <input type=\"Text\" size=\"20\" name=\"address\" maxlength=\"50\">";
        input_str+="\n    </td>";
        input_str+="\n    </tr>";
        input_str+="\n     <tr>";
        input_str+="\n    <td>City:</td>";
        input_str+="\n     <td>";
        input_str+="\n    <input type=\"Text\" size=\"20\" name=\"city\" maxlength=\"20\">";
        input_str+="\n     </td>";
        input_str+="\n     </tr>";
        input_str+="\n     <tr> ";
        input_str+="\n     <td>State/Province:</td>";
        input_str+="\n     <td>";
        input_str+="\n     <input type=\"Text\" size=\"2\" name=\"state\" maxlength=\"15\">";
        input_str+="\n     </td>";
        input_str+="\n     </tr>";
        input_str+="\n     <tr> ";
        input_str+="\n   <td>Zip/Postal Code:</td>";
        input_str+="\n   <td>";
        input_str+="\n   <input type=\"Text\" size=\"10\" name=\"zip\" maxlength=\"10\">";
        input_str+="\n   </td>";
        input_str+="\n   </tr>";
        input_str+="\n   <tr> ";
        input_str+="\n   <td>Country:</td>";
        input_str+="\n   <td>";
        input_str+="\n   <input type=\"Text\" size=\"20\" name=\"country\" maxlength=\"20\">";
        input_str+="\n   </td>";
        input_str+="\n   </tr>";
        input_str+="\n   <tr>";
        input_str+="\n   <td>Email:</td>";         
	input_str+="\n   <td>";
        input_str+="\n   <input type=\"text\" name=\"submit_by\" size=\"20\" maxlength=\"50\">";
        input_str+="\n   </td>";
        input_str+="\n   </tr>";
		input_str+="\n   <tr>";
        input_str+="\n   <td>Confirm Email:</td>";         
	input_str+="\n   <td>";
        input_str+="\n   <input type=\"text\" name=\"submit_by_confirm\" size=\"20\" maxlength=\"50\">";
        input_str+="\n   </td>";
        input_str+="\n   </tr>";
	input_str+="\n   <tr>";
        input_str+="\n   <td>Phone:</td>";
        input_str+="\n    <td>";
        input_str+="\n    <input type=\"text\" name=\"phone\" size=\"20\" maxlength=\"50\">";
        input_str+="\n    </td>";
        input_str+="\n    </tr>";
        input_str+="\n    <tr>";
        input_str+="\n    <td valign=\"top\">Additional Requests/Comments:</td>";
        input_str+="\n    <td>";
        input_str+="\n    <textarea name=\"comments\" cols=\"25\" rows=\"4\"></textarea>";
        input_str+="\n   </td>";
        input_str+="\n   </tr>";
        input_str+="\n   <tr>"; 
        input_str+="\n   <td colspan=\"2\">";
        input_str+="\n   <input type=\"Submit\" value=\"send\">";	
	input_str+="\n   <br>\n<input type=\"hidden\" name=\"recipient\" value=\"sales@vintagedoors.com\">"
	input_str+="\n   <input type=\"hidden\" name=\"form_id\" value=\"Custom Screen/Storm Door Quote Request\">";
	input_str+="\n   <input type=\"hidden\" name=\"redirect\" value=\"http://www.vintagedoors.com/thankyou.html\">";
	input_str+="\n   <input type=\"hidden\" name=\"missing_fields_redirect\"";
        input_str+="\n   value=\"http://www.vintagedoors.com/formerror.html\">";
	input_str+="\n   <input type=\"hidden\" name=\"required\" value=\"name,address,city,state,country\">";
        input_str+="\n   </td>";
        input_str+="\n   </tr>";
        input_str+="\n   </table>";

	new_window.document.write(input_str);
        
        new_window.document.write("\n   </form>\n  </td>\n </tr>\n</table>\n</body>\n</html>");


	// close the document
	new_window.document.close(); 

}


/*submit_form*******************************************************
 * Preconditions: Holds the input data fields
 *
 * Postconditions: Submits door to company 
 *******************************************************************/
function submit_form()
{
        var input_str = "";

        input_str += "\n<form id=\"frm_submit\" action=\"../../mailer.php\"";
        input_str += "\n method=\"post\">";
        input_str += "\n<div id=\"door_specs\" style=\"display:none\">";
        input_str += "\n<input name=\"door_type\" type=text value=";
        input_str += door_type + " />";
        input_str += "\n<br />";


	for(i in CellList)
        {
          if(CellList[i].type != 'static')
          {
           input_str += "\n<input name=" + CellList[i].name + " type=text ";
           input_str += "\n    value=" + CellList[i].image + " />";
           input_str += "\n<br />"; 
          }
        }

        input_str += "</div>";

        return input_str;
}


