﻿<?xml version="1.0" encoding="utf-8" ?>
<Template Title = "New Template">
		<import-xml src = "Templates/TemplateTools.xml" id = "TemplateTools" />
		<div class ="field">
		<div class = "field_label">
			Name:
		</div>
		<div class = "field_input">
			<input type = "text" class = "input_text" rid = "template_name" />
		</div>
	</div>
	<div class ="field">
		<div class = "field_label">
			Description:
		</div>
		<div class = "field_input">
			<input type = "text" class = "input_text" rid = "template_description" />
		</div>
		<div class = "field_description">
				 (<i>optional</i>)
		</div>
	</div>
	<textarea rid ="template_text" wrap ="off" style ="background-color: #CFCFCF; overflow:auto;border:0px solid #000000;margin:0px;width:100%;height:125px;"></textarea>
	<div>
			<input type ="checkbox" rid ="chk_use_tools" /> Include template tools.
			<br />
			<input type ="checkbox" rid ="chk_enable_callbacks" /> Include template_init and template_destroy callbacks.
			<br />
			<input type ="checkbox" rid ="chk_use_rich_select" /> Include RichSelect fragment tool.
			<br />
			<input type ="checkbox" rid ="chk_use_tab1" /> Include Tab fragment tool.
			<br />
			<input type ="checkbox" rid ="chk_use_tab2" /> Include Tab gizmo.
			<br />
			<input type ="checkbox" rid ="chk_use_wide_select" /> Include WideSelect gizmo.
			<br />
			<input type ="checkbox" rid ="chk_use_canvas" /> Include Canvas gizmo. (<i>Internet Explorer Canvas Hack not included</i>)

	</div>
	<div>
		<input type ="button"	value ="Create" onclick ="${this}.CreateNewTemplate()" />
		<input type ="button"	value ="Close" onclick ="${this}.Close()" />
	</div>
	<embedded-script>
		<![CDATA[
		template_init : function(){
			
			this.GetElementByRID("template_text").value = "<!-- "
				+ "Enter valid XHTML"
			// + "Use ${this} for localized XHTML -> Component scripting,"
			// + "\nand ${this.id} to access the component id string.\n"
			+ " -->\n";
			;
			
		},
		CreateNewTemplate : function(){
				var sName = this.GetElementByRID("template_name").value.replace(/$\s*/,"").replace(/\s*$/,"");
				if(sName.length == 0){
					this.setStatus("Invalid name.  Name must be specified.");
					return;
				}
				var sXhtml = this.GetElementByRID("template_text").value;
				var oX = 0;
				try{
						oX = Hemi.xml.parseXmlDocument("<test>" + sXhtml + "</test>");
				}
				catch(e){
				}

				if((oX == null || oX.documentElement == null || oX.documentElement.nodeName == "parsererror") && !confirm("Warning - content is not valid XHTML/XML.  Continue creating template?")) return;

				var oBuilder = Hemi.registry.service.getObject(this.getProperties().opener_id);

				oBuilder.ClearTemplate();

				var oConstruct = oBuilder.NewComponentConstruct(oBuilder.GetTemplateModel());
				oConstruct.changes++;
				
				var aBuff = [];
						
				oBuilder.GetElementByRID("template_name").value = this.GetElementByRID("template_name").value;
				oBuilder.GetElementByRID("template_description").value = this.GetElementByRID("template_description").value;
				if(this.GetElementByRID("chk_enable_callbacks").checked){
					oBuilder.AddFunctionStatement(oConstruct, "template_init","// Invoked when template is loaded, after all embedded_init functions are invoked");
					oBuilder.AddFunctionStatement(oConstruct, "template_destroy","// Invoked when a destroy signal is raised");
				}
				var bTools = this.GetElementByRID("chk_use_tools").checked;
				
				var bRichSelect = this.GetElementByRID("chk_use_rich_select").checked;
				var bTab1 = this.GetElementByRID("chk_use_tab1").checked;
				var bTab2 = this.GetElementByRID("chk_use_tab2").checked;
				var bWideSelect = this.GetElementByRID("chk_use_wide_select").checked;
				var bUseCanvas = this.GetElementByRID("chk_use_canvas").checked;
						
				if(bTools || bRichSelect || bTab1){
						aBuff.push("<!-- import-xml is defined in Hemi.app.space.definitions to use Hemi.xml.getXml to load an XML file and match at the root for html-fragment -->")
						aBuff.push("<import-xml src = \"Templates/TemplateTools.xml\" id = \"TemplateTools\" />");
				}


				if(aBuff.length) aBuff.push("");
				
				oConstruct.xhtml_content = aBuff.join("\n") + sXhtml;
				
				oBuilder.getObjects().template_construct = oConstruct;
				oBuilder.BuildTemplate();
				this.getProperties().built = 1;
				this.Close();
		},
		local_handle_close : function(oWin){
			/// if(this.getProperties().built == 1) return;
            var oBuilder = Hemi.registry.service.getObject(this.getProperties().opener_id);
            if(oBuilder && typeof oBuilder.getFocus == "function") oBuilder.getFocus();
		}
  ]]>
	</embedded-script>
</Template>

