﻿<?xml version="1.0" encoding="utf-8" ?>

<Template Title ="Project Builder">
	<import-style src ="Styles/jsprof.css" id ="jsprof" />
	<import-style src ="Styles/wideselect.css" id ="wideselect" />
	<import-xml src = "Templates/TemplateTools.xml" id = "TemplateTools" />
	<import-xml src = "Fragments/BuilderControls.xml" id = "BuilderControls" />

	<div class="title">Project Description</div>
		<div>
			<select rid ="run_project_as"><option>Scripted</option><option>Declarative</option></select>
			<input type ="button" onclick ="${this}.RunProject()" value ="Run" />
			<span rid ="project_info">[ Volatile Project ]</span>
		</div>
	<div class ="field">
		<div class = "field_label">
			Name:
		</div>
		<div class = "field_input">
			<input type = "text" class = "input_text" rid = "project_name" />
			<input type = "hidden" value ="0" rid = "project_id" />
		</div>
	</div>
	
	<div class ="field">
		<div class = "field_label">
			Description: (<i>optional</i>)
		</div>
		<div class = "field_input">
			<input type = "text" class = "input_text" rid = "project_description" />
		</div>
	</div>
	<div class ="field">
		<div class = "field_label">
			Primary Template: (<i>optional</i>)
		</div>
		<div class = "field_input">
			<input type = "text" class = "input_text" rid = "project_primary_template" />
		</div>
	</div>
	<div class ="field">
		<div class = "field_label">
			Control Task: (<i>optional</i>)
		</div>
		<div class = "field_input">
			<input type = "text" class = "input_text" rid = "project_control_task" />
		</div>
	</div>
	<div style ="width:25%;float:left;">
		<div class="title">Tasks</div>
		<div ___pointer_marker = "never" component = "wideselect" rid = "project_tasks_list">
		</div>
	</div>
	<div style ="width:25%;float:left;">
		<div class="title">Templates</div>
		<div ___pointer_marker = "never" component = "wideselect" rid = "project_templates_list">
		</div>
	</div>
	<div style ="width:25%;float:left;">
		<div class="title">Fragments</div>
		<div ___pointer_marker = "never" component = "wideselect" rid = "project_fragments_list">
		</div>
	</div>
	<div style ="width:25%;float:left;">
		<div class="title">Components</div>
		<div ___pointer_marker = "never" component = "wideselect" rid = "project_components_list">
		</div>
	</div>
	<div style ="clear:left;">
		<input type = "button" value = "Import" onclick ="${this}.LoadProjectText()" />
		<select onchange ="${this}.BuildProject()" rid = "build_project"><option>Project XML</option><option>Deployed XML</option></select>
		<span rid ="project_model_controls" style ="display:none;">
			<select rid ="project_link_type"><option>TaskList</option><option>Template</option><option>Fragment</option><option>Component</option></select>
			<input type ="button" value ="Link" onclick ="${this}.LinkToProject()" />
		<!--
			<input type ="button" value ="Link Template" onclick ="${this}.LinkTemplateToProject()" />
			<input type ="button" value ="Link Fragment" onclick ="${this}.LinkFragmentToProject()" />
			<input type ="button" value ="Link Component" onclick ="${this}.LinkComponentToProject()" />
			<input type ="button" value ="Link Task List" onclick ="${this}.LinkTaskListToProject()" />
		-->
			<input type ="button" value ="Remove" onclick ="${this}.RemoveProjectLink()" />
		</span>
		<br />
		<textarea rid ="project_text" wrap ="off" style ="border:0px;margin:0px;width:100%;height:200px;"></textarea>
	</div>
	<embedded-script>
        <![CDATA[
		template_init : function (){
			Hemi.include("hemi.text");
			Hemi.include("hemi.app.dwac");
			this.GetElementByRID("btnPub").style.display = "";
			this.getProperties().pub_data_path = 0;
			this.scopeHandler("projecttasksclick",0,0,1);
			this.scopeHandler("projecttemplatesclick",0,0,1);
			this.scopeHandler("projectfragmentsclick",0,0,1);
			this.scopeHandler("projectcomponentsclick",0,0,1);
			this.setupButtons("project-buttons-bar");
			this.scopeHandler("project_load",0,0,1);
			this.getObjects().project_construct = 0;
			this.scopeHandler("project_picker_open",0,0,1);
			this.scopeHandler("project_picker_choose",0,0,1);
			this.GetProjectTasksList().setResultHandler( this._prehandle_projecttasksclick);
			this.GetProjectTemplatesList().setResultHandler( this._prehandle_projecttemplatesclick);
			this.GetProjectFragmentsList().setResultHandler( this._prehandle_projectfragmentssclick);
			this.GetProjectComponentsList().setResultHandler( this._prehandle_projectcomponentsclick);
			this.refreshBuilderUI();
			this.RefreshProjectBuilder();
			this.getProperties().builder_title = "Project Builder";
		},
		RefreshProjectBuilder : function(){
			var b = (this.getObjects().project_construct ? 1 : 0);
			this.GetElementByRID("project_name").disabled = (b ? false : true);
			this.GetElementByRID("project_description").disabled = (b ? false : true);
			this.GetElementByRID("build_project").disabled = (b ? false : true);
			this.GetElementByRID("project_primary_template").disabled = (b ? false : true);
			this.GetElementByRID("project_control_task").disabled = (b ? false : true);
			this.GetElementByRID("project_model_controls").style.display = (b ? "" : "none");
			
		},
		template_destroy : function(){
			this._prehandle_project_picker_open = 0;
			this._prehandle_project_picker_choose = 0;
			this._prehandle_project_load = 0;
			this._prehandle_projecttasksclick = 0;
			this._prehandle_projectfragmentsclick = 0;
			this._prehandle_projecttemplatesclick = 0;
			this._prehandle_projectcomponentsclick = 0;
		},
		PublishCurrentProject : function(bSkipRefresh){
			if(
				 !this.getObjects().project_construct
				 ||
				 this.getObjects().project_construct.id <= 0
			){				 
				 this.SetLabel("Nothing to publish");
				 return;
			}
			if(this.GetElementByRID("build_project").selectedIndex != 1){
				this.GetElementByRID("build_project").selectedIndex = 1;
			}
			this.BuildProject();
			
			var sTemp = this.GetElementByRID("project_text").value;
			this.GetElementByRID("project_text").value = escape(sTemp);
			
			var sName = this.GetElementByRID("project_name").value;
			var bAdd = 1;
			var oResp = this.GetNameExists(["project_name","project_id"], sName, "Pub");
			if(oResp.status == true){
				bAdd = 0;
			}
      
			var bOut = 0;
			var aF = ["project_name","project_description","project_text"];
			var oResp = 0;
			if(!bAdd){
				Hemi.log("Editing Project ...");
				oResp = this.EditData(aF, this.getObjects().project_construct.id, "Pub");
			}
			else{
				Hemi.log("Adding Project ...");
				oResp = this.AddData(aF, "Pub");
			}
			if(oResp.status){
				var sId = oResp.responseId;
				if(!sId || sId.length == 0){
						this.SetLabel("Error retrieving publication id");
				}
				else{
					/// if(!bSkipRefresh) this.RefreshDWACLists();
					this.SetLabel("Published!");
					Hemi.xml.setInnerXHTML(this.GetElementByRID("project_info"),"Pub Id: " + sId + " / Path: " + oResp.responsePath);
					this.getProperties().pub_data_path = oResp.responsePath;
					bOut = 1;
				}
			}
			else{
				this.SetLabel("Failed to publish '" + this.GetElementByRID("project_name").value + "'");
			}
			this.GetElementByRID("project_text").value = sTemp;
			return bOut;
		},
		RunProject : function(s){
			if(!this.getObjects().project_construct) return;
			
			var sTask = this.GetElementByRID("project_control_task").value;
			var sTemplate = this.GetElementByRID("project_primary_template").value;
			var oTemplate = this.GetProjectTemplatesList().getActiveItem();
			var oTask = this.GetProjectTasksList().getActiveItem();
			var aTemplateItems = this.GetProjectTemplatesList().getItems();

			if(!oTemplate && aTemplateItems.length != 1 && sTemplate.length == 0 && !oTask  && sTask.length == 0){
				this.SetLabel("Select or specify a template, or specify a task, to run as the primary.");
				return;
			}
			if(!sTemplate.length && aTemplateItems.length > 0) sTemplate = aTemplateItems[0].name;
			
			var aBuff = [];
			
			if(!this.PublishCurrentProject(1)){
				return;
			}
			
			this.GetElementByRID("build_project").selectedIndex = 0;
			this.BuildProject();

			var aParams = [
				this.GetElementByRID("project_name").value,
				sTask,
				sTemplate,
				"dwac:" + this.getProperties().pub_data_path,
				this.GetElementByRID("run_project_as").selectedIndex
			];
			this.RunControl("RunApplicationProject", aParams);
		},
		LoadObject : function(i){
			this.SetLabel("Loading ...");
			Hemi.xml.setInnerXHTML(this.GetElementByRID("project_info"),"[ Volatile Project ]");

			this.getProperties().project_id = i;
			this.GetElementByRID("build_project").selectedIndex = 0;
			this.ReadData(0, i, this._prehandle_project_load);
		},
		_handle_project_load : function(oService, oSubject, oRequest, oResponse){
			this.SetLabel("");
			if(!oResponse.responseData.length){
				 this.SetLabel("Failed to load project");
				 return;
			}
			var oData = oResponse.responseData[0];
			var sVal = unescape(oData.value);
			
			this.GetElementByRID("project_text").value = sVal;

			if(this.LoadProjectText()){
				this.GetElementByRID("project_name").value = oData.name;
				this.GetElementByRID("project_description").value = oData.description;

				this.getObjects().project_construct.id = this.getProperties().project_id;
				this.RefreshProjectBuilder();
				Hemi.xml.setInnerXHTML(this.GetElementByRID("project_info"),"Id: " + this.getProperties().project_id + " / Path: " + oData.path);
				this.SetDataPath(oData.path);
			}

			this.GetElementByRID("project_id").value = this.getProperties().project_id + "";
	
		},

		DeleteCurrentObject : function(){
			var id = this.getProperties().project_id;
			if(typeof id != "number"){
				 this.SetLabel("Invalid project data id");
				 return;
			}
			if(!confirm("Are you sure you want to delete this project?")) return;
			var oResp = this.DeleteData(["project_id"], id);
			if(oResp.status == true){
					this.ClearProject();
					/// this.RefreshDWACLists();
			}
			else{
				 this.SetLabel("Failed to delete data #" + id);
			}
		},
		ClearProject : function(){
				this.GetElementByRID("build_project").selectedIndex = 0;
				this.GetElementByRID("project_text").value = "";
				this.GetElementByRID("project_id").value = "0";
				this.GetElementByRID("project_name").value = "";
				this.GetElementByRID("project_control_task").value = "";
				this.GetElementByRID("project_primary_template").value = "";
				this.GetElementByRID("project_description").value = "";
				Hemi.xml.setInnerXHTML(this.GetElementByRID("project_info"),"[ Volatile Project ]");
				this.getObjects().project_construct = 0;
				this.getProperties().project_id = 0;
				this.GetProjectTasksList().clearItems();
				this.GetProjectComponentsList().clearItems();
				this.GetProjectFragmentsList().clearItems();
				this.GetProjectTemplatesList().clearItems();
				this.SetLabel("");
				this.RefreshProjectBuilder();
		},
	
		SaveCurrentObject : function(){
			if(
				 !this.getObjects().project_construct
				 ||
					(
						this.getObjects().project_construct.changes == 0
						&&
						!confirm("No changes detected.  Save anyway?")
					)
			){				 
				 this.SetLabel("Didn't save - Nothing to do");
				 return;
			}
			if(this.GetElementByRID("build_project").selectedIndex != 0){
				this.GetElementByRID("build_project").selectedIndex = 0;
			}
			this.BuildProject();
			var sName = this.GetElementByRID("project_name").value;
			var oResp = this.GetNameExists(["project_name","project_id"], sName);
			if(oResp.status == true){
				Hemi.log("Resp Status=" + oResp.status);
				this.SetLabel("The project '" + sName + "' already exists");
				return;
			}
			
			var sTemp = this.GetElementByRID("project_text").value;
			this.GetElementByRID("project_text").value = escape(sTemp);

			var aF = ["project_name","project_description","project_text","project_id"];
			oResp = 0;
			if(this.getObjects().project_construct.id > 0){
				oResp = this.EditData(aF, this.getObjects().project_construct.id);
			}
			else{
				oResp = this.AddData(aF);
			}
			if(oResp.status){

				this.getObjects().project_construct.changes = 0;
				var sId = oResp.responseId;
				if(!sId || sId.length == 0){
						this.SetLabel("Error retrieving data id");
						this.getObjects().project_construct.id = 0;
				}
				else{
					this.getObjects().project_construct.id = parseInt(sId);	
					this.GetElementByRID("project_id").value = sId;
					this.SetLabel("Saved!");
					Hemi.xml.setInnerXHTML(this.GetElementByRID("project_info"),"Id: " + sId + " / Path: " + oResp.responsePath);
					this.SetDataPath(oResp.responsePath);
				}
			}
			else{
					this.SetLabel("Failed to save project");
			}
			this.GetElementByRID("project_text").value = sTemp;
		},

		CreateNewObject : function(){
				if(this.getObjects().project_construct && this.getObjects().project_construct.changes > 0 && !confirm("Continue?  Changes will be lost.")) return;

				var oW = Hemi.app.createWindow(0,"Templates/NewProject.xml","NewProject-" + this.getObjectId(),0,0,{opener_id:this.getObjectId()});
				oW.resizeTo(500,350);
				// Destroy the window when finished
				//
				oW.setHideOnClose(0);
				//this.SetLabel("New Project - Remember to Save!");
		},

		BuildProject : function(){
			if(!this.getObjects().project_construct){
				this.SetLabel("Invalid project construct");
				return;	 
			}
			var sName = Hemi.text.trim(this.GetElementByRID("project_name").value);
			var sTask = Hemi.text.trim(this.GetElementByRID("project_control_task").value);
			var sTemplate = Hemi.text.trim(this.GetElementByRID("project_primary_template").value);
			this.getObjects().project_construct.name = sName;
			
			var aBuff = [];	
			var iIndex = this.GetElementByRID("build_project").selectedIndex;
			var bXml = 1;//(iIndex == 0 ? 1 : 0);
			var bDep = (iIndex == 1 ? 1 : 0);
			var aErrors = [];

			if(bXml){
				aBuff.push("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
				aBuff.push("<DWAC Title = \"" + sName + "\"");
				if(sTask.length > 0) aBuff.push(" DWacControlTask = \"" + sTask + "\"");
				if(sTemplate.length > 0) aBuff.push(" DWacTemplateId = \"" + sTemplate + "\"");
				aBuff.push(">\n");
			}
			
			var aItems = this.GetProjectFragmentsList().getItems();
			if(bDep){
				aBuff.push("<fragments>\n");
			}
			for(var i = 0; i < aItems.length; i++){
				if(!bDep){
					aBuff.push("<module type = \"fragment\"  uri = \"dwac:" + g_application_path + "DWAC/" + Hemi.data.io.service.getSubject().name + "/Fragments/" + aItems[i].name + "\"");
					aBuff.push(" id = \"" + aItems[i].data + "\"");
					aBuff.push(" name = \"" + aItems[i].name + "\"");
					aBuff.push(" />\n");
				}
				else{
					var oXml = Hemi.xml.getXml("dwac:" + g_application_path + "DWAC/" + Hemi.data.io.service.getSubject().name + "/Fragments/" + aItems[i].name + "?ts=" + (new Date()).getTime(),0,0,0,0);
					aBuff.push("<fragment id = \"" + aItems[i].name + "\">\n");
					aBuff.push(this.BuildProjectModule(oXml, oXml.documentElement, aErrors));
					var s = Hemi.xml.serialize(oXml.documentElement);
					aBuff.push("</fragment>");
				}
			}
			if(bDep){
				aBuff.push("</fragments>\n");
			}
			aItems = this.GetProjectTasksList().getItems();
			var bCheck = 0;
			if(bDep){
				aBuff.push("<tasks>\n");
			}
			for(var i = 0; i < aItems.length; i++){
				if(!bDep){
					aBuff.push("<module type = \"tasklist\"  uri = \"dwac:" + g_application_path + "DWAC/" + Hemi.data.io.service.getSubject().name + "/Tasks/" + aItems[i].name + "\"");
					aBuff.push(" id = \"" + aItems[i].data + "\"");
					aBuff.push(" name = \"" + aItems[i].name + "\"");
					aBuff.push(" />\n");
				}
				else{
					var oXml = Hemi.xml.getXml("dwac:" + g_application_path + "DWAC/" + Hemi.data.io.service.getSubject().name + "/Tasks/" + aItems[i].name + "?ts=" + (new Date()).getTime(),0,0,0,0);
					for(var c = 0; c < oXml.documentElement.childNodes.length; c++){
						if(oXml.documentElement.childNodes[c].nodeType == 1){
							var oTask = oXml.documentElement.childNodes[c];
							if(sTask.length > 0 && oTask.getAttribute("id") == sTask) bCheck = 1;
							aBuff.push(this.BuildProjectModule(oXml, oTask, aErrors));
						}
					}
				}
			}
			if(bDep){
				if(!bCheck && sTask.length > 0) aErrors.push("Task warning: The primary task is not defined in any linked task list.");
				aBuff.push("</tasks>\n");
			}
			
			bCheck = 0;
			aItems = this.GetProjectComponentsList().getItems();
			if(bDep){
				aBuff.push("<application-components>");
			}
			for(var i = 0; i < aItems.length; i++){
				if(!bDep){
					aBuff.push("<module type = \"component\"  uri = \"dwac:" + g_application_path + "DWAC/" + Hemi.data.io.service.getSubject().name + "/Components/" + aItems[i].name + "\"");
					aBuff.push(" id = \"" + aItems[i].data + "\"");
					aBuff.push(" name = \"" + aItems[i].name + "\"");
					aBuff.push(" />\n");
				}
				else{
					var oXml = Hemi.xml.getXml("dwac:" + g_application_path + "DWAC/" + Hemi.data.io.service.getSubject().name + "/Components/" + aItems[i].name + "?ts=" + (new Date()).getTime(),0,0,0,0);
					for(var c = 0; c < oXml.documentElement.childNodes.length; c++){
						if(oXml.documentElement.childNodes[c].nodeType == 1){
							aBuff.push(this.BuildProjectModule(oXml, oXml.documentElement.childNodes[c], aErrors));
						}
					}
				}
			}
			if(bDep){
				aBuff.push("</application-components>");
			}

			
			bCheck = 0;
			aItems = this.GetProjectTemplatesList().getItems();
			if(bDep){
				aBuff.push("<Templates>\n");
			}
			for(var i = 0; i < aItems.length; i++){
				if(!bDep){
					aBuff.push("<module type = \"template\"  uri = \"dwac:" + g_application_path + "DWAC/" + Hemi.data.io.service.getSubject().name + "/Templates/" + aItems[i].name + "\"");
					aBuff.push(" id = \"" + aItems[i].data + "\"");
					aBuff.push(" name = \"" + aItems[i].name + "\"");
					aBuff.push(" />\n");
				}
				else{
					var oXml = Hemi.xml.getXml("dwac:" + g_application_path + "DWAC/" + Hemi.data.io.service.getSubject().name + "/Templates/" + aItems[i].name + "?ts=" + (new Date()).getTime(),0,0,0,0);
					var sId = Hemi.text.trim(oXml.documentElement.getAttribute("id"));
					if(sId.length > 0 && sTemplate.length > 0 && sId == sTemplate) bCheck = 1;
					//aBuff.push("<Template id = \"" + aItems[i].name + "\">\n");
					aBuff.push(this.BuildProjectModule(oXml, oXml.documentElement, aErrors));
					var s = Hemi.xml.serialize(oXml.documentElement);
					//aBuff.push("</Template>");
				}
			}
			if(bDep){
				if(!bCheck && sTemplate.length > 0) aErrors.push("Template warning: The primary template does not match any linked template");
				aBuff.push("</Templates>\n");
			}
			
			if(bXml){
				aBuff.push("</DWAC>\n"); 
			}
			var sText = aBuff.join("");

			this.GetElementByRID("project_text").value = sText;
			if(bDep){
				if(aErrors.length) alert(aErrors.join("\n"));
				var oTestXml = Hemi.xml.parseXmlDocument(sText);
				if(oTestXml == null || oTestXml.documentElement == null || oTestXml.documentElement.nodeName == "parsererror"){
					this.SetLabel("Failed to parse DWAC Build");
					return 0;
				}
				else{
					this.SetLabel("Successfully parsed DWAC Build");
				}
			}
		},
		BuildProjectModule : function(oXml, oNode, aMessages){
			var sDWACPath = g_application_path + "DWAC/" + Hemi.data.io.service.getSubject().name;
			//var regFrag = /Fragments\/([\S\s]+)/;
			var regComp = new RegExp("appcomp_path\\s*=\\s*\"(dwac:)+" + g_application_path + "DWAC/" + Hemi.data.io.service.getSubject().name + "/Components/([^\"]*)\"","g");
			var regFrag = new RegExp("(dwac:)+" + g_application_path + "DWAC/" + Hemi.data.io.service.getSubject().name + "/Fragments/([\\S\\s]+)");
			var oM;
			var aBuff = [];
			var oImports = oNode.getElementsByTagName("import-xml");
			for(var i = oImports.length - 1; i>=0; i--){
				var sHref = Hemi.text.trim(oImports[i].getAttribute("src"));
				//alert("'" + sHref + "\n" + regFrag + "\n" + sHref.match(regFrag));
				if((oM = sHref.match(regFrag)) && oM.length > 1){
					var oSwap = oXml.createElement("import-dxml");
					var bUseKey = oM[2].match(/^ID\-/) ? 1 : 0;
					var sVal = oM[2].replace(/^ID\-/,"");
					var sIdHint = Hemi.text.trim(oImports[i].getAttribute("id"));
					//if(bUseKey){
						var sMatch = "";
						var aFrags = this.GetProjectFragmentsList().getItems();
						for(var f = 0; f < aFrags.length; f++){
							if((bUseKey && aFrags[f].data == sVal) || aFrags[f].data == sVal){
								sMatch = aFrags[f].name;
								break;
							}
						}
						sVal = sMatch;
						if(sVal.length == 0){
							aMessages.push("Unlinked fragment warning: Missing Fragment Link to " + sHref + (sIdHint ? " ('" + sIdHint + "')" : ""));
						}
					//}
					
					oSwap.setAttribute("context-path","/DWAC/fragments/fragment[@id = '" + sVal + "']/span");// + oNode.nodeName);
					oSwap.setAttribute("src","${dwac.path}");
					//oImports[i].parentNode.appendChild(oSwap);
					Hemi.xml.swapNode(oImports[i],oSwap);
					//oImports[i].parentNode.removeChild(oImports[i]);
					// <import-dxml context-path = "/DWAC/fragments/fragment/div" src = "${dwac.path}" />
				}
			}
			//var regComp = new RegExp("appcomp_path\\s*=\\s*" + g_application_path + "/DWAC/" + Hemi.data.io.service.getSubject().name + "/Components/([\\S\\s]+[^\"])");
			var sText = Hemi.xml.serialize(oNode);
			/// alert(sText + "\n" + sText.match(regComp));
			sText = sText.replace(regComp,"appcomp_path = \"${dwac.path}\"");
			sText = sText.replace(/<html\-fragment>/,"<span>");
			sText = sText.replace(/<\/html\-fragment>/,"</span>");
			aBuff.push(sText);
			//alert(sText);
			//(/appcomp_path\s*=\s*"\/DWAC\/steve\/Components\/([\S\s]+)/)
			return aBuff.join("");
		},
		GetProjectTemplatesList : function(){
			return this.GetComponentByRID("project_templates_list").GetWideSelect();
		},
		GetProjectFragmentsList : function(){
			return this.GetComponentByRID("project_fragments_list").GetWideSelect();
		},
		GetProjectComponentsList : function(){
			return this.GetComponentByRID("project_components_list").GetWideSelect();
		},
		GetProjectTasksList : function(){
			return this.GetComponentByRID("project_tasks_list").GetWideSelect();
		},
		_handle_projectfragmentsclick : function(sEvent,oItem){

			
		},
		_handle_projectcomponentsclick : function(sEvent,oItem){

			
		},
		_handle_projecttemplatesclick : function(sEvent,oItem){

			
		},
		_handle_projecttasksclick : function(sEvent,oItem){

			
		},

		LinkToProject : function(){
			this.getProperties().expect_content = 1;
			this["Link" + this.GetElementByRID("project_link_type").options[this.GetElementByRID("project_link_type").selectedIndex].text + "ToProject"]();
		},
		LinkTaskListToProject : function(){
			this.getProperties().link_target = "Tasks";
			Hemi.app.createWindow("Task List Picker","Templates/Picker.xml","ProjectPicker",0,0,{opener_id:this.getObjectId()},this._prehandle_project_picker_open);
		},
		LinkComponentToProject : function(){
			this.getProperties().link_target = "Components";
			Hemi.app.createWindow("Component Picker","Templates/Picker.xml","ProjectPicker",0,0,{opener_id:this.getObjectId()},this._prehandle_project_picker_open);
		},
		LinkFragmentToProject : function(){
			this.getProperties().link_target = "Fragments";
			Hemi.app.createWindow("Fragment Picker","Templates/Picker.xml","ProjectPicker",0,0,{opener_id:this.getObjectId()},this._prehandle_project_picker_open);
		},
		LinkTemplateToProject : function(){
			this.getProperties().link_target = "Templates";
			Hemi.app.createWindow("Template Picker","Templates/Picker.xml","ProjectPicker",0,0,{opener_id:this.getObjectId()},this._prehandle_project_picker_open);
		},
		_handle_project_picker_open : function(oW){
			oW.setCanResize(0);
			oW.resizeTo(200,300);
			Hemi.app.getWindowManager().CenterWindow(oW);
			oW.setIsModal(1);
			oW.GetPickerList().addItem("Obtaining " + this.getProperties().link_target + " options ...");
			oW.SetPickerHandler(this._prehandle_project_picker_choose);
			oW.setHideOnClose(0);
			this.getObjects().current_picker = oW;
			var oResp = this.ListData(this.getProperties().link_target);
			oW.GetPickerList().clearItems();
			for(var i = 0; i < oResp.responseData.length; i++){
				/// Hemi.log("Adding to picker: " + oResp.responseData[i].name + " (" + oResp.responseData[i].id + ")");
				oW.GetPickerList().addItem(oResp.responseData[i].name,"" + oResp.responseData[i].id);
			}
		},
		_handle_project_picker_choose : function(oPicker, oItem){
			oPicker.Close();
			var oList;
			switch(this.getProperties().link_target){
				case "Templates":
					oList = this.GetProjectTemplatesList();
					break;
				case "Tasks":
					oList = this.GetProjectTasksList();
					break;
				case "Components":
					oList = this.GetProjectComponentsList();
					break;
				case "Fragments":
					oList = this.GetProjectFragmentsList();
					break;
			}
			var aCurrent = oList.getItems();
			var bCheck = 0;
			for(var i = 0; i < aCurrent.length; i++){
				if(aCurrent[i].data == oItem.data){
						bCheck = 1;
						break;
				}
			}
			if(bCheck){
				 this.SetTemplateLabel(this.getProperties().link_target + " '" + oItem.data + "' is already linked");
			}
			else{
				oList.addItem(oItem.name,oItem.data);
				oList.commitBuffer();
				this.getObjects().project_construct.changes++;
				this.BuildProject();
			}
		},
		LoadProjectText : function(){

			var bXml = (this.GetElementByRID("build_project").selectedIndex == 0 ? 1 : 0);
			if(this.getObjects().project_construct && this.getObjects().project_construct.changes > 0 && !confirm("Continue?  Changes will be lost.")) return 0;

			if(!bXml){
				this.SetLabel("Parsing only available for full html-fragment XML file format.  Select Project XML.");
				return 0;
			}
			
			var oXml = null;

			var sFragText = this.GetElementByRID("project_text").value;
			try{
				 oXml = Hemi.xml.parseXmlDocument(sFragText);
			}
			catch(e){
				 //alert("Error parsing XML");
				 //return 0;
			}
			
			if(oXml == null || oXml.documentElement == null){
				this.SetLabel("Failed to parse Project XML");
				return 0;
			}

			var oComp = oXml.documentElement;

			if(oComp.nodeName != "DWAC"){
				this.SetLabel("No DWAC definitions found");
				return 0;
			}
			var n;
			
			this.GetProjectTemplatesList().clearItems();
			this.GetProjectComponentsList().clearItems();
			this.GetProjectTasksList().clearItems();
			this.GetProjectFragmentsList().clearItems();
			
			var aModules = oComp.getElementsByTagName("module");
			for(var i = 0; i < aModules.length; i++){
				var sType = Hemi.text.trim(aModules[i].getAttribute("type"));
				var sId = Hemi.text.trim(aModules[i].getAttribute("id"));
				var sName = Hemi.text.trim(aModules[i].getAttribute("name"));
				var oList;
				switch(sType){
					case "fragment":
						oList = this.GetProjectFragmentsList();
						break;
					case "tasklist":
						oList = this.GetProjectTasksList();
						break;
					case "component":
						oList = this.GetProjectComponentsList();
						break;
					case "template":
						oList = this.GetProjectTemplatesList();
						break;
				}
				if(oList){
					oList.addItem(sName,sId);
				}
			}
			this.GetElementByRID("project_name").value = "";
			this.GetElementByRID("project_description").value = "";
			this.GetElementByRID("project_control_task").value = Hemi.text.trim(oComp.getAttribute("DWacControlTask"));
			this.GetElementByRID("project_primary_template").value = Hemi.text.trim(oComp.getAttribute("DWacTemplateId"));
			
			
			this.getObjects().project_construct = this.ParseConstruct();
			this.getObjects().project_construct.xhtml_content = "";
			this.RefreshProjectBuilder();
 		 return 1;
		},
		RemoveProjectLink : function(){
			var a = [this.GetProjectTasksList(), this.GetProjectFragmentsList(),this.GetProjectComponentsList(),this.GetProjectTemplatesList()];
			var k = 0;
			for(var i = 0; i < a.length; i++){
				var o = a[i].getActiveItem();
				if(o){
					a[i].clearItem(o.index);
					k++;
				}
			}
			var oConstruct = this.getObjects().project_construct;
			if(k){
				oConstruct.changes++;
				this.BuildProject();
				this.SetLabel("Removed Member - Remember to Save!");
			}
		}

	]]></embedded-script>
</Template>
