<?xml version="1.0" encoding="utf-8" ?>
<html-fragment>
	<embedded-script>
			<![CDATA[
		embedded_init : function(){
      Hemi.include("hemi.text");
			if(this.getStatus().preferred_title && typeof this.setTitle == "function"){
				this.setTitle(this.getStatus().preferred_title);
			}
			this.setupButtons("buttons-bar");
			// Clear any status
			//
			if(typeof this.setStatus == "function") this.setStatus("");
		},
		FormatStringIntoHtml : function(oNode, sData){
			if(!oNode || !sData) return;
			var aDat = sData.split("\n");
			for(var i = 0; i < aDat.length; i++){
				var oD = document.createElement("p");
				var sLine = Hemi.text.trim(aDat[i]);
				if(sLine.length == 0) oD.appendChild(document.createElement("br"));
				else oD.appendChild(document.createTextNode(sLine));
				oNode.appendChild(oD);
			}
		},
		GetElementByRID : function(n){
			var o = this.getTemplateEngine().getObjectByName(n);
			if(!o) return 0;
			if(o.object){
				if(typeof o.object.getContainer == 'function') return o.object.getContainer();
				return o.object;
			}
			return o;
		},
		// TODO: Temporary fix until form set value is fixed for selects
		//
		SetSelectValueByIndex : function(n,v){
			var o = this.GetElementByRID(n);
			if(!o || typeof o.selectedIndex != "number") return;
			for(var i = 0; i < o.options.length; i++){
				if(o.options[i].value == v || (o.options[i].value == "" && o.options[i].text == v)){
					o.selectedIndex = i;
					break;
				}
				
			}
		},
		// TODO: There is a bug in the setValue function in ocjxf.setValue where select options
		// are not being selected
		//
		SetFormValue : function(n, v){
			return org.cote.js.xhtml.form.XHTMLFormComponent.setValue(n,v, this.getTemplateEngine().engine_id)
		},
		GetFormValue : function(n){
			return org.cote.js.xhtml.form.XHTMLFormComponent.getValue(n,this.getTemplateEngine().engine_id)
		},
		GetForm : function(){
			return org.cote.js.xhtml.form.XHTMLFormComponent.getFormByName(this.getTemplateEngine().engine_id);
		},
		ClearForm : function(){
			return org.cote.js.xhtml.form.XHTMLFormComponent.clearForm(this.getTemplateEngine().engine_id,0,0);
		},
		GetElementText : function(oParent, sName){
			var aN = oParent.getElementsByTagName(sName);
			if(aN.length == 0) return ""; // was 0;
			return org.cote.js.xml.getInnerText(aN[0]);
		},
		SetText : function(sName, sValue){
			var o = this.GetElementByRID(sName);
			if(!o) return;
			org.cote.js.xml.setInnerXHTML(o,sValue);
		},
		Register : function(){
    		var oX = this.SerializeForm();
				var oR = org.cote.js.xml.postXml("Registration.aspx?is-xml=1&NewRegistration=1",oX);
				if(oR == null || oR.documentElement == null){
					this.setStatus("Error sending/receiving registration request.");
				}
				else if(this.IsSuccess(oR)){
					this.setStatus("Submitted Registration");
					this.loadTemplate("Templates/RegistrationCompleted.xml");
				}
				else{
					this.setStatus("Failed to submit registration request");
				}
		},
		IsSuccess : function(oX){
		  if(!oX || !oX.documentElement) return 0;
		  return (oX.getElementsByTagName("Success").length > 0 ? 1 : 0);
		},
		IsLoginRequired : function(oX){
		  if(!oX || !oX.documentElement) return 0;
		  return (oX.getElementsByTagName("Login-Required").length > 0 ? 1 : 0);
		},
		ShowLogin : function(){
			var oW = GetWindowManager().GetWindowByName("LoginWindow");
			if(oW){
				oW.open();
				oW.restore();
				GetSession().Refresh(1);
				oW.setStatus("Logged out");
				oW.loadTemplate("ActionForms/Login.xml");
			}
		},
		SetElementParam : function(oX, name, val){
		  if(!oX || !oX.documentElement) return 0;
		  var oParams = oX.documentElement.getElementsByTagName("Params");
		  if(oParams.length == 0){
			oParams = oX.createElement("Params");
		  }
		  else{
			oParams = oParams[0];
		  }
	      
		  var oParam = oX.createElement("Param");
				oParams.appendChild(oParam);
		  oParam.setAttribute("Name",name);
				oParam.setAttribute("Value","#cdata");
				oParam.appendChild(oX.createCDATASection(val));
		},
		GetDataNameExists : function(sSubAppPath,aFields){
			var oForm = this.SerializeForm(0,aFields);
			var oX = org.cote.js.xml.postXml(sSubAppPath + "/CheckName?is-xml=1",oForm);
			if(oX == null || oX.documentElement == null){
				alert("Error retrieving response");
				return 1;
			}
			if(oX.getElementsByTagName("True").length > 0) return 1;
			return 0;
		},
		SerializeForm : function(oX,aLimit){
			var eid = this.getTemplateEngine().engine_id;
			
			if(!oX) oX = org.cote.js.xml.newXmlDocument("Request");
			var oForm = oX.createElement("Form");
			oForm.setAttribute("cid",this.getObjectId());
			oForm.setAttribute("eid",eid);
			
			oX.documentElement.appendChild(oForm);				
			var aElements = this.GetForm().getElements();
			for(var e = 0; e < aElements.length;e++){
				var name = aElements[e].getName();
				if(typeof aLimit == "object" && aLimit.length){
					var b = 0;
					for(var m = 0; m < aLimit.length; m++){
						if(aLimit[m] == name){
							b = 1;
							break;
						}
					}
					if(!b) continue;
				}

				/*
					use the getValue() method to ensure that the value is taken from the visible element first, then the cache value
					otherwise, the value of the element object - not the Form Element HTML node - may not be synchronized, and thus not the expected value
				*/
				var val = org.cote.js.xhtml.form.XHTMLFormComponent.getValue(name,eid);
				if(typeof val == "boolean") val = val.toString();
				var sType = aElements[e].getType();
				
				if(!val || val.length == 0 || !sType) continue;
				 
				var oElement = oX.createElement("Element");
				oForm.appendChild(oElement);
				var oName = oX.createElement("Name");
				oName.appendChild(oX.createCDATASection(name));
				oElement.appendChild(oName);
				var oValue = oX.createElement("Value");
				oValue.appendChild(oX.createCDATASection(val));
				oElement.appendChild(oValue);
				oElement.setAttribute("Type",sType);
			}
			return oX;
		},
		ValidateForm : function(b){
			var oF = this.GetForm();
			return org.cote.js.xhtml.form.XHTMLFormComponent.validateForm(oF.i, b);
		},
		ValidateForPattern : function(n, p){
			var oF = this.GetForm();
			if(!oF) return 0;
			var oE = oF.getElementByName(n);
			if(!oE) return 0;
			return org.cote.js.xhtml.validator.XHTMLValidator.validateField(oE.getElement(),p);
		},
		DiagramStep : function(sLabel, iMark, iTotal){
			var oLabel = this.GetElementByRID("step_label");
			var oDiag = this.GetElementByRID("step_diagram");
			if(!oLabel || !oDiag) return;
			org.cote.js.xml.setInnerXHTML(oLabel,sLabel);
			for(var i = 0; i < iTotal; i++){
				var oI = document.createElement("img");
				oI.setAttribute("src","Graphics/" + (iMark > i ? "star_hover.jpg" : "star_empty.jpg"));
				oI.setAttribute("width","20");
				oI.setAttribute("width","20");
				oDiag.appendChild(oI);
			}
		},
		EnableButton : function(n){
			this.SwitchButton(n,false);
		},
		DisableButton : function(n){
			this.SwitchButton(n,true);
		},
		SwitchButton : function(n, b){
			var o = this.GetElementByRID(n),a,ac=" ";
			if(!o) return;
			a = o.getAttribute("button-action");
			if(a) ac += a.toLowerCase() + "_button";
			o.disabled = b;
			o.className = "designer_button" + ac;
		},
		setupButtons : function(sName){
			var o = this.getTemplateObjectByName(sName);
			if(!o) return;
			var aC = o.childNodes,a;
			this.createHandler("toggle_highlight",0,0,1);
			this.createHandler("exec_button",0,0,1);
			for(var i = 0; i < aC.length; i++){
				
				if(aC[i].nodeType == 1){
					a = aC[i].getAttribute("button-action");
					if(!a || a.length == 0) continue;
					aC[i].onmouseover = this._prehandle_toggle_highlight;
					aC[i].onmouseout = this._prehandle_toggle_highlight;
					if(a != "avoid" && typeof this._handle_exec_button == "function" && !aC[i].onclick) aC[i].onclick = this._prehandle_exec_button;
				}
			}
		},
		_handle_toggle_highlight : function(e){
			var o = org.cote.js.dom.event._gevt_src(e),a,ac = " ";
			e = org.cote.js.dom.event._gevt(e);
			a = o.getAttribute("button-action");
			if(a) ac += a.toLowerCase() + "_button";
			if(e.type == "mouseover")
				o.className = "designer_button designer_button_highlight" + ac;
			else
				o.className = "designer_button" + ac;
		},
		
		get_frame : function(n){
			var aF = this.getContainer().getElementsByTagName("iframe");
			for(var i = 0; i < aF.length; i++){
				if(aF[i].getAttribute("rid") == n){
					o = aF[i];
					break;
				}
			}
			if(o) o = (o.contentWindow || o.contentDocument);
			if(!o) return null;
			//if(o.document) o = o.document;
			if(!o.document || !o.document.body) return null;
			return o;
		},
		
		GetComponentByRID : function(sRid){
			var o = this.getTemplateEngine().getObjectByName(sRid);
			if(!o || !o.object) return 0;
			return o.object.getApplicationComponent();

		},
		ResizeFrame : function(n,iModWidth,iModHeight){
			try{
				var d = this.getTemplateObjectByName(n);
				
				var iT = d.offsetTop;
				// the parentNode x 3 is a bug
				// where Engine is not correctly swapping out the html-fragment element
				//
				var iB = d.parentNode.parentNode.parentNode.clientHeight;
							//(typeof this.getBody == "function" ? this.getBody() : d.parentNode).clientHeight;
							//n.parentNode.parentNode.parentNode.clientHeight;
				var iH = (iB - iT );
				if(iModHeight > 0) iH -= iModHeight;
				
				if(iH < 1) iH = 1;
				
				var iW = this.getContainer().clientWidth - 20;
				if(iModWidth > 0) iW -= iModWidth;
				if(iW < 1) iW = 1;
				
				d.style.height = iH + "px";
				d.style.width = iW + "px";
			}
			catch(e){
				this.setStatus("Unexpected UI Error: " + (e.message ? e.message : e.description));
			}
		},
		TrimSafe : function(s){
			if(typeof s != "string") return "";
			s = s.replace(/^\s*/gi,"");
			s = s.replace(/\s*$/gi,"");
			return s;
		}
	]]></embedded-script>
</html-fragment>
