// Calling page must have paramsDiv & libraryDiv objects for these functions
// show and manipulate

function plugin_main()
{
   HideAllLibraryDivs();

   // Hide the library div
   paramsDiv.style.display = "";

   // Create the parameters in paramsDiv (this must exist)
   Plain_write_paramsDiv();

   // Initialise any button classes we have just created
   InitialiseButtonDivs();

   // Store global variables and function calls
   globalStore.OnCreateBlankFlat = OnCreateBlankFlat;
}

function Plain_write_paramsDiv()
{
   // Empty any existing content
   paramsDiv.innerHTML = "";

   // Create an array to hold the HTML
   var hArr = new Array();
   hArr.push("<table border=0 cellspacing=0 cellpadding=1 width=100% style='table-layout:fixed;margin-left:3px'>");
   hArr.push("<colgroup><col width=40> <col width = 35%> <col width = 65% ></colgroup>");
   hArr.push("<tr><td><img src='" + artcam.HtmlRootDir + "../SharedImages/JewelSmith/Filename.gif'></td><td>" + gNameEdit + ":</td>");
   hArr.push("<td><input style='margin-left:1px' type=edit id=NameEdit></td></tr>");
   hArr.push("<tr><td></td><td>" + gWidthEdit + ":</td>");
   hArr.push("<td><input style='margin-left:1px' type=edit value='50' size=3 number positive id=WidthEdit>"+gMM+"</td></tr>");
   hArr.push("<tr><td></td><td>" + gHeightEdit + ":</td>");
   hArr.push("<td><input style='margin-left:1px' type=edit value='50' size=3 number positive id=HeightEdit>"+gMM+"</td></tr>");
   hArr.push("<tr><td></td><td>" + gResolutionEdit + ":</td>");
   hArr.push("<td><input style='margin-left:1px' type=edit value='30' size=3 number positive integer id=ResolutionEdit>"+gPixelsPerMM+"</td></tr>");
   hArr.push("</table>");
   hArr.push("<div class='mouseOut' button_name='forward_button' targetFunction='globalStore.OnCreateBlankFlat' style='width=30px;float:right;'>");
   hArr.push("<img alt="+ gAcceptButtonAlt +" id=back_img src ='" + artcam.HtmlRootDir + "../SharedImages/accept.gif' align=absmiddle border=0 width=30 height=30>");
   hArr.push("</div>");

   // Add the back button HTML to the array
   AddBackButtonHTML(hArr);

   // Bung all our HTML into the div and display it
   paramsDiv.innerHTML = hArr.join("\n");
   globalStore.NameEdit = eval(NameEdit);
   globalStore.NameEdit.value = gDefaultFilename;
   globalStore.WidthEdit = eval(WidthEdit);
   globalStore.HeightEdit = eval(HeightEdit);
   globalStore.ResolutionEdit = eval(ResolutionEdit);

   SubclassNumberEditControls();
   paramsDiv.style.display = "";
}

function OnCreateBlankFlat()
{
   if(!artcam.ProjectAssemblyExists())
   {
      artcam.Alert(gNoProjectLoaded,0);
      return;
   }

   if(globalStore.NameEdit.value=="")
   {
      artcam.MessageBox(gNoName);
      return;
   }

   // IE7 Workaround.
   // ---------------
   // In the code below, we were calling artcam.CreateNewModel, which in turn called
   // a script function on this page 'FileCheck' in GeneralTools.js. This function resets the location
   // of this page so that it is due for reload. A problem occurs, because straight after, we call
   // SaveArtFile which throws up a message box. This messagebox has a message pump, which allows
   // messages to get to the IE window. This allows the IE window to get on with reloading the page,
   // BEFORE the Save function call returns to the page. When this function finally does return, there
   // is no page and IE7 complains. This is probably due to a more serious security restriction in IE7
   // that IE6 let us get away with.
   //
   // Our simplest way around this issue it to hide the FileCheck function during Open, then
   // to call it AFTER any GUI has been shown. In this way, the page wont reload unexpectedly.
   // Then we call FileCheck afterwards.
   //
   // Temporarily Store FileCheck Function and set it to a function that does nothing.
   var FileCheckHolder = FileCheck;
   FileCheck = function() {/*Do Nothing*/}

   if (true == CreateThreeAxisFlat(globalStore.WidthEdit.getValue(),
                  globalStore.HeightEdit.getValue(), globalStore.ResolutionEdit.getValue()))
   {
      SaveArtFile("Flat", globalStore.NameEdit.value);
   }

   // Restore FileCheck and call it
   FileCheck = FileCheckHolder;
   FileCheck();

   OnCloseAndGotoAssistantTab();
   OnBackButton();
}
