// Form Builder v3.0 TFA Edition (Dec. 2006) - FormAssembly.com
// Copyright (c) 2005-2006 Cedric Savarese - Veer West LLC (veerwest.com)
// All Rights Reserved - do not reuse without permission.
// For licensing information visit: http://www.formassembly.com/about-form-builder.php

// hXUL library customization

hXUL.disableXULRendering = true;

hXUL.AssetManager.dataTransformer = function(data) {
	var doc = data.document.documentElement;
	var data = this.dataTransformerLoop(doc); 	
	data = [{id: "outline@form" ,content: ["<a href='#'>"+doc.getAttribute("name")+"</a>"], rowClass: "anchored", children: data}];
	
	if(data[0].children.length > 0) {
		data.push({id:"separator_1", content: ["&nbsp;"], rowClass: "inert undroppable"}, 
		          {id:"copier", content: ["Copier <small>- Drag & drop here to duplicate</small>"], rowClass: "inert"});	
		data.push({id:"separator_2", content: ["&nbsp;"], rowClass: "inert undroppable"}, 
		          {id:"trash", content: ["Trash <small>- Drag & drop here to delete</small>"], rowClass: "inert"});
	}

	return  data;
}
hXUL.AssetManager.dataTransformerLoop = function(element) {
	var data = [];
	for(var i=0;i<element.childNodes.length;i++) {
		var n = element.childNodes[i];
		if(n.nodeType==1) {
			var item = {};
			switch(n.tagName) {
				case "field":
					var textNode  = n.getElementsByTagName("label")[0].firstChild;
					if(textNode && textNode.nodeValue!="") 
						var text = textNode.nodeValue;
					else {
						var choiceNode = n.getElementsByTagName("choice")[0];
						if(choiceNode) {
							var valueNode = choiceNode.getElementsByTagName("label")[0];
							if(valueNode && valueNode.firstChild && valueNode.firstChild.nodeValue!="") {
								var text  = valueNode.firstChild.nodeValue;							
							} else
								var text  = "no label";
						} else
							var text  = "no label";
					}
					if(text.length>25)
						text = text.substring(0,22)+ "...";
					text = "<a href='#'>"+text+"</a>";
					item.id = "outline@"+n.getAttribute("id");
					extra = "";
					if(n.getAttribute("required")=="required")
						extra += "<img src='images/icon_validate_required.gif' alt='required' title='required field' /> ";
					if(n.getAttribute("validation")=="validate-email")
						extra += "<img src='images/icon_validate_email.gif' alt='email'  title='email field' />";
					if(n.getAttribute("validation")=="validate-alpha")
						extra += "<img src='images/icon_validate_alpha.gif' alt='alpha' title='text only field' />";
					if(n.getAttribute("validation")=="validate-alphanum")
						extra += "<img src='images/icon_validate_alphanum.gif' alt='alpha-numeric' title='text and numbers only field' />";
					if(n.getAttribute("validation")=="validate-date")
						extra += "<img src='images/icon_validate_date.gif' alt='date'  title='date field' />";
					if(n.getAttribute("validation")=="validate-float")
						extra += "<img src='images/icon_validate_float.gif' alt='decimal number' title='numeric field' />";
					if(n.getAttribute("validation")=="validate-integer")
						extra += "<img src='images/icon_validate_numeric.gif' alt='number' ' title='numeric field' />";

					item.content  = [text]; // , extra
					item.rowClass = n.getAttribute("type");
					
					break;
				case "fieldset":
					var text = n.getAttribute("name")
					if(!text) {
						if(n.getAttribute("type")=="inlinediv")
							text = "inline section";
						else
							text = "section";
					}
					item.id       = "outline@"+n.getAttribute("id");
					item.content  = ["<a href='#'>"+text+"</a>",""]; //n.getAttribute("type")
					item.rowClass = n.getAttribute("type");
					item.collapse = n.getAttribute("treeview");
					item.children = this.dataTransformerLoop(n);
					break;
				case "htmlsection":					
					if(n.firstChild) {
						var text = n.firstChild.nodeValue.replace(/<[^>]*>/gi," ");
						text = text.replace(/&....?;/g," ");
						if(text.length>35)
							var text = text.substring(0,33) + "...";
					} else 
						text = "text and image";
					item.id       = "outline@"+n.getAttribute("id");
					item.content  = ["<a href='#'>"+text+"</a>",""]; //n.getAttribute("type")
					item.rowClass = "htmlsection";
					break;
			}	
			if(item.content)	
				data.push(item);
		}
	}
	return data;
}
hXUL.TreeView.prototype.select = function(id, preventOnSelectEvent) {	
	return this._renderer.selectRow("outline@"+id, preventOnSelectEvent);
}