/*
Copyright (c) 2006 Author
Author: Anil Sharma for the book "Real-World AJAX: Secrets of the Masters"

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions
of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

// Reference to Form component
var projectSummary = null;
// Reference to the block element containing Form
// This is attached in the main area
var projectSummaryContainer = null;
// Actual form element
var projectSummaryElement = null;

// attaches project summary form to MainArea2
// loads the template from the server if required
function displayProjectSummary() {
   if (projectSummaryContainer == null) {
      // project summary template is not yet loaded
      // load the template
      loadProjectSummaryElement();
   } else {
      // project summary template has been loaded
      // simply attach it
      // See Util.js for the details of attachElement function.
      attachElement('MainArea2', projectSummaryContainer);
   }
}

// loads HTML template for the project symmary form
function loadProjectSummaryElement() {
   var req = new DataRequest(basePageURL + 'ProjectSummary.html', requestCompletedProjectSummaryElement,
   requestFailedProjectSummaryElement, requestTimedoutProjectSummaryElement);
   ajaxEngine.processRequest(req);
}

// call back after the project summary is loaded
// response.responseText gives the HTML retrieved
function requestCompletedProjectSummaryElement(response) {
   projectSummaryContainer = setElementFromHtml(response.responseText, 'MainArea2', 'projectSummaryContainer');
   // our projectSummaryContainer element contains the projectSummaryElement
   projectSummaryElement = document.getElementById("projectSummaryElement");
   // create a Form component
   projectSummary = new Form();
   // associate projectSummaryElement with it
   projectSummary.setHtmlElement(projectSummaryElement);
   
   // associates field components with the corresponding elements
   projectSummary.setFieldComp(new TextComp("name"));
   projectSummary.setFieldComp(new TextComp("resource"));
   projectSummary.setFieldComp(new TextComp("description"));
   
   projectSummary.setFieldComp(new DateComp("startDate"));
   projectSummary.setFieldComp(new DateComp("endDate"));
   
   projectSummary.setFieldComp(new SelectComp("status"));
   
   projectSummaryTemplateLoaded();
}

function requestFailedProjectSummaryElement(dataRequest) {
   alert("Request failed, url: " + dataRequest.requestURL);
}

function requestTimedoutProjectSummaryElement(dataRequest) {
   alert("Request timedout, url: " + dataRequest.requestURL);
}


