﻿function findArtFormData(parentNames) {
	var names = parentNames.split(',');
	for (var c = 0; c < names.length; c++) {
		var parentElement = $ntb(names[c]);
		if (parentElement != null)
			return getArtFormData(parentElement);
	}
}
function getArtFormData(parentElement, el) {
    if (el == null) {
        var doc = nitobi.xml.createXmlDoc('<ArtForm />');
        el = doc.documentElement;
    }
    addFormElements(parentElement, el);
    return el.xml;
}

function addFormElements(parentElement, docEl)
{
    if (parentElement.hasChildNodes())
    {
        var children = parentElement.childNodes;
        for (var i = 0; i < children.length; i++)
        {
            var child = children[i];
            if (child.nodeName.toUpperCase() == "INPUT") {
                var title = child.title;
                //docEl.setAttribute("InputType", child.type.toLowerCase());
                if (child.type.toLowerCase() == "checkbox") {
                    if (child.checked)
                        docEl.setAttribute(child.name, "true");
                    else
                        docEl.setAttribute(child.name, "false");

                }
                else if (child.type.toLowerCase() == "radio") {
                    if (child.checked)
                        docEl.setAttribute(child.name, child.value);
                }
                else
                    docEl.setAttribute(child.name, child.value);
                docEl.setAttribute(child.name + ".title", title);
            }
            else if (child.nodeName.toUpperCase() == "TEXTAREA") {
                //docEl.setAttribute("InputType", child.nodeName.toLowerCase());
                docEl.setAttribute(child.name, child.value);
            }
            else if (child.nodeName.toUpperCase() == "SELECT") {
                var v = child.value;
                if((v == null || v == "") && child.selectedIndex > 0)
                    v = child.options[child.selectedIndex].text;

                docEl.setAttribute(child.name, v);
            }
            else
                addFormElements(child, docEl);
        }
    }

}

function showArtFormData(parentElement, xml) {
    if (xml == null || xml.length == 0)
        return;
    var doc = nitobi.xml.createXmlDoc(xml);
    var docEl = doc.documentElement;
    setupFormElements(parentElement, docEl);
    if(typeof setupFormFields == 'function')
    	setupFormFields();
}

function setupFormElements(parentElement, docEl)
{
    if (parentElement.hasChildNodes())
    {
        var children = parentElement.childNodes;
        for (var i = 0; i < children.length; i++)
        {
            var child = children[i];
            var attrib = findXmlAttributeFromBaseName(child.name, docEl);
            // if(child.name != null && child.name != "" && docEl.getAttribute(child.name) != null)
            if (attrib != null)
            {
                if (child.nodeName.toUpperCase() == "INPUT") {
                    if (child.type.toLowerCase() == "checkbox") {
                        if (attrib.value == "true")
                            child.checked = true;
                        else
                            child.checked = false;
                        //child.checked = docEl.getAttribute(child.name)
                    }
                    else if (child.type.toLowerCase() == "radio") {
                        if (attrib.value == child.value)
                            child.checked = true;
                    }
                    else
                        child.value = attrib.value;
                }
                else if (child.nodeName.toUpperCase() == "TEXTAREA")
                    child.value = attrib.value;
                else if (child.nodeName.toUpperCase() == "SELECT")
                    for (var p = 0; p < child.options.length; p++)
                       if (child.options[p].value == attrib.value || child.options[p].text == attrib.value) {
                           child.options[p].selected = true;
                           child.selectedIndex = p;
                           break;
                        }
                else
                    setupFormElements(child, docEl);
            }
            else
                setupFormElements(child, docEl);
        }
    }

}

function findXmlAttributeFromBaseName(baseName, docEl) {
    if (baseName != null && baseName.length > 0)
    {
        var attribs = docEl.attributes;
        for (var p = 0; p < attribs.length; p++)
            if (attribs[p].name == baseName || (attribs[p].name.length < baseName.length && baseName.substr(0, attribs[p].name.length) == attribs[p].name))
            return attribs[p];
    }
    return null;
   }

function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
