﻿/// <reference name="MicrosoftAjax.js"/>
Type.registerNamespace("foodpoints");

foodpoints.common = function() {
    foodpoints.common.initializeBase(this);
    
    this._formElements = "input:text,input:checkbox,input:radio,select,input:hidden";  
}

foodpoints.common.prototype = {

    initialize: function() {
        foodpoints.common.callBaseMethod(this, 'initialize');
    },
    
    dispose: function() {        
        //Add custom dispose actions here
        foodpoints.common.callBaseMethod(this, 'dispose');
    },
    
    loadForm : function(parentId, data){
        var parent = $get(parentId);
        var fields = $(this._formElements, parent);
        
        fields.val('');
        
        if(data != null){
            jQuery.each(fields, function(index, field){
                if(field.attributes["prop"] != null){
                    var value = data[field.attributes['prop'].nodeValue];
                    
                    if(value != null){
                        if(Date.isInstanceOfType(value)){
                            value = value.format('MM/dd/yyyy');
                        }
                        field.value = value;
                    }
                    
                    if(field.type == 'checkbox') field.checked = value;
                }  
            });
        }
    },
    
    getFormData : function(parentId){
        var parent = $get(parentId);
        var fields = $(this._formElements, parent);
        var json = new Object();
        
        jQuery.each(fields, function(index, field){
            if(field.attributes["prop"] != null){
                var key = field.attributes['prop'].nodeValue;
                var value = null;
                if(field.value != undefined && field.value != null && field.value != '') value = field.value; else value = field.defaultValue;
                if(field.type == 'checkbox') value = field.checked;
                json[key] = value;
            }  
        });
        
        return json;
    }
}

var common = new foodpoints.common();

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
