// Record object

function Record(){
    this.className = 'Record';
    this.fields = new Object();

    this.set = function(fields){
        var value;
        for(var fieldName in fields){
            value = fields[fieldName];
            if (this.fields[fieldName]){
                this.fields[fieldName].value = value;
            }
            this[fieldName] = value;
        }
    }

    /**
     * assign vars to template
     **/
    this.assign = function(tpl, prefix, forForm){
        prefix = formatPrefix(prefix);

        var varArray = new Array();
        var value;
        for(var varName in this){
            if ((typeof this[varName]) == "function") continue;
            value = new String(this[varName]);

            varArray[prefix + varName] = value;
        }

        for(var i in this.fields){
            var field = this.fields[i];

            if (field.is('bool')){
                this.assignBool(tpl, i, prefix);
            }
        }

        tpl.assign(varArray);
    }

    /**
     * assignDate
     * assigns MDY, MDYHIS formats for a date field
     **/
    this.assignDate = function(tpl, fieldName, prefix){
        prefix = formatPrefix(prefix);

        if (this[fieldName]){
            var value = new String(this[fieldName]);
            var year = value.substring(0, 4);
            var month = value.substring(5, 7);
            var day = value.substring(8, 10);

            var mdyName = fieldName + 'MDY';
            var varArray = new Object();
            varArray[prefix + mdyName] = month + '/' + day + '/' + year;
            tpl.assign(varArray);
        }
    }

    this.getMDY = function(fieldName){
        if (this[fieldName]){
            var value = new String(this[fieldName]);
            var year = value.substring(0, 4);
            var month = value.substring(5, 7);
            var day = value.substring(8, 10);
	  
            return month + '/' + day + '/' + year;
        }
        return '';
    }

    this.getYesNo = function(fieldName){
        if (this[fieldName]){
            if (this[fieldName] == 1 || (this.fields[fieldName] && this.fields[fieldName].value == 1)){
                return 'Yes';
            }
            if (this[fieldName] == 0){
                return 'No';
            }
        }
        return '';
    }



    /**
     * assignBool
     * assigns yes no values etc.
     **/
    this.assignBool = function(tpl, fieldName, prefix){
        prefix = formatPrefix(prefix);

        if (this[fieldName] != undefined){
            var yesNoName = fieldName + 'YesNo';
            var vals = new Object();

            if (this[fieldName] == 1 || (this.fields[fieldName] && this.fields[fieldName].value == 1)){
                vals[prefix + yesNoName] = 'Yes';
                vals[prefix + fieldName + 'Checked'] = 'CHECKED';

            }
            else if (this[fieldName] == 0){
                vals[prefix + yesNoName] = 'No';
                vals[prefix + fieldName + 'Checked'] = '';
            }
            else {
                vals[prefix + yesNoName] = '';
                vals[prefix + fieldName + 'Checked'] = '';
            }
            tpl.assign(vals);
        }
    }

    /** 
     * assignConcat
     * concats related records (if one to many) into a comma separated list
     **/
    this.assignConcat = function(tpl, fieldName, subFieldName, assignTo, separator){
        var value = "";
        if (this[fieldName]){
            if (typeof this[fieldName] == 'object'){
                for(var i in this[fieldName]){
                    var subRecord = this[fieldName][i];
                    if (subRecord[subFieldName]){
                        value += subRecord[subFieldName] + separator;
                    }
                }
            }
        }
        if (value.length > 0){
            value = value.substring(0, value.length - separator.length);
        }
        var varArray = new Object();
        varArray[assignTo] = value;
        tpl.assign(varArray);
    }

    /**
     *
     **/
    function formatPrefix(prefix){
        if (!prefix) prefix = '';
        if (prefix != ''
            && prefix.substring(0, prefix.length - 1) != '.'){
            prefix += '.';
        }
        return prefix;
    }



    /**
     *
     * this doesn't check node name
     * makes for easy casting, but could let unintended
     * behavior slip.
     **/
    this.fromXML = function(xmlNode){
        var field;
        if (!xmlNode){
            alert("No XML Node in Record.fromXML()");
            return false;
        }

        var haveAttributes = false;

        for(var i = 0; i < xmlNode.attributes.length; i++){
            var attribute = xmlNode.attributes[i];
            this[attribute.name] = attribute.value;
            haveAttributes = true;
        }

        if (!haveAttributes && !xmlNode.childNodes) {
            alert ("No child nodes Record.fromXML() and no attributes.");
            return false;
        }

        for(var j = 0; j < xmlNode.childNodes.length; j++){
            field = xmlNode.childNodes.item(j);
            // this sets up recursion so nested objects can be parsed.
            if (field.nodeName == 'Array'
                || field.nodeName == 'Relation') {
                var newVarName = field.getAttributeNode('name').value;

                // one to one
                if (field.nodeName == 'Relation'){
                    var relation = new Record();
                    relation.fromXML(field.childNodes.item(0));
	  
                    this[newVarName] = relation;

                }
                // one to many
                else {
                    this[newVarName] = new Array();
                    for (var k = 0; k < field.childNodes.length; k++){
                        // would like to be able to do variable class instantiation.
                        var newObj = new Record();
                        newObj.fromXML(field.childNodes.item(k));
                        this[newVarName].push(newObj);
                    }
                }
            }
            // look at the value attribute first
            else if (field.attributes && field.attributes['value']){
                this[field.nodeName] = field.attributes['value'];
                var ourField = new Field();

                ourField.value = field.attributes['value'];
                if (field.getAttribute('type') == 'b'){
                    ourField.addFlag('bool');
                }
                this.fields[field.nodeName] = ourField;

            }
            // then look at the firstchild in a text way
            else if (field.firstChild) {
                this[field.nodeName] = field.firstChild.nodeValue;

                var ourField = new Field();
                ourField.value = field.firstChild.nodeValue;

                if (field.getAttribute('type') == 'b'){
                    ourField.addFlag('bool');
                }
                this.fields[field.nodeName] = ourField;

            }
            else this[field.nodeName] = "";
        }
    }

    /**
     *
     **/
    this.fromXMLString = function(xmlString){
        var domParser = new DOMParser();
        var doc = domParser.parseFromString(xmlString, "text/xml");
        // will be a document object, but childNodes still exists
        // hooray for no-strong-typing
        if (doc.firstChild){
            this.fromXML(doc.firstChild);
        }
    }

    /**
     *
     **/
    this.toXML = function(doEscape){
        var xml = '<' + this.className + '>';
    
        for(var varName in this){
            var objVar = this[varName];
            
            if ((typeof objVar) == 'undefined'
                || (typeof objVar) == "function"
                || (typeof objVar) == 'object'
                || varName.substring(0, 1) == '$') continue;
            
            xml += "<" + varName + ">";
            if (doEscape){
                xml += escape(objVar);
            }
            else {
                xml += objVar;
            }
            xml += "</" + varName + ">";
        }
        xml += "</" + this.className + ">";
        return xml;
    }
    
    this.toParamString = function (){
        for(var varName in this){
            var objVar = this[varName];
            if ((typeof objVar) == "function") continue;
            paramString += varName + "=" + objVar + "&";
        }
    }
}

// end Tag definition
