(function(cloudStack, testData) {
  cloudStack.sections.templates = {
    title: 'Templates',
    id: 'templates',
    sectionSelect: {
      label: 'Select view'
    },
    sections: {
      templates: {
        type: 'select',
        title: 'Templates',
        listView: {
          id: 'templates',
          label: 'Templates',          
          filters: {
            mine: { label: 'Mine' },
            featured: { label: 'Featured' },
            community: { label: 'Community' }
          },          
          fields: {
            name: { label: 'Name' },
            id: { label: 'ID' },
            zonename: { label: 'Zone' },
            hypervisor: { label: 'Hypervisor' }
          },
          actions: {
            add: {
              label: 'Create template',
              messages: {
                confirm: function(args) {
                  return 'Are you sure you want to create a template?';
                },
                success: function(args) {
                  return 'Your new template is being created.';
                },
                notification: function(args) {
                  return 'Creating new template';
                },
                complete: function(args) {
                  return 'Template has been created successfully!';
                }
              },
              createForm: {
                title: 'Create template',
                desc: 'Please fill in the following data to create a new template.',
                preFilter: cloudStack.preFilter.createTemplate,
                fields: {
                  name: {
                    label: 'Name',
                    validation: { required: true }
                  },
                  description: {
                    label: 'Description',
                    validation: { required: true }
                  },
                  url: {
                    label: 'URL',
                    validation: { required: true }
                  },
                  zone: {
                    label: 'Zone',
                    select: function(args) {
                      $.ajax({
                        url: createURL("listZones&available=true"),
                        dataType: "json",
                        async: true,
                        success: function(json) {
                          var zoneObjs = json.listzonesresponse.zone;
                          var items = [];
                          if (isAdmin())
                            items.push({id: -1, description: "All Zones"});
                          $(zoneObjs).each(function() {
                            items.push({id: this.id, description: this.name});
                          });
                          args.response.success({data: items});
                        }
                      });
                    }
                  },
                  hypervisor: {
                    label: 'Hypervisor',
                    dependsOn: 'zone',
                    select: function(args) {
                      if(args.zone == null)
                        return;
                      var apiCmd;
                      if(args.zone == -1)
                        apiCmd = "listHypervisors&zoneid=-1";
                      else
                        apiCmd = "listHypervisors&zoneid=" + args.zone;
                      $.ajax({
                        url: createURL(apiCmd),
                        dataType: "json",
                        async: false,
                        success: function(json) {
                          var hypervisorObjs = json.listhypervisorsresponse.hypervisor;
                          var items = [];
                          $(hypervisorObjs).each(function(){
                            items.push({id: this.name, description: this.name});
                          });
                          args.response.success({data: items});
                        }
                      });
                    }
                  },
                  format: {
                    label: 'Format',
                    dependsOn: 'hypervisor',
                    select: function(args) {
                      var items = [];
                      if(args.hypervisor == "XenServer") {
                        //formatSelect.append("");
                        items.push({id:'VHD', description: 'VHD'});
                      }
                      else if(args.hypervisor == "VMware") {
                        //formatSelect.append("");
                        items.push({id:'OVA', description: 'OVA'});
                      }
                      else if(args.hypervisor == "KVM") {
                        //formatSelect.append("");
                        items.push({id:'QCOW2', description: 'QCOW2'});
                      }
                      else if(args.hypervisor == "BareMetal") {
                        //formatSelect.append("");
                        items.push({id:'BareMetal', description: 'BareMetal'});
                      }
                      else if(args.hypervisor == "Ovm") {
                        //formatSelect.append("");
                        items.push({id:'RAW', description: 'RAW'});
                      }
                      args.response.success({data: items});
                    }
                  },
                  osTypeId: {
                    label: 'OS Type',
                    select: function(args) {
                      $.ajax({
                        url: createURL("listOsTypes"),
                        dataType: "json",
                        async: true,
                        success: function(json) {
                          var items = json.listostypesresponse.ostype;
                          args.response.success({data: items});
                        }
                      });
                    }
                  },
                  isExtractable: {
                    label: "Extractable",
                    isBoolean: true
                  },
                  isPasswordEnabled: {
                    label: "Password Enabled",
                    isBoolean: true
                  },
                  isPublic: {
                    label: "Public",
                    isBoolean: true,
                    isHidden: true
                  },
                  isFeatured: {
                    label: "Featured",
                    isBoolean: true,
                    isHidden: true
                  }
                }
              },
              action: function(args) {
                var array1 = [];
                array1.push("&name=" + todb(args.data.name));
                array1.push("&displayText=" + todb(args.data.description));
                array1.push("&url=" + todb(args.data.url));
                array1.push("&zoneid=" + args.data.zone);
                array1.push("&format=" + args.data.format);
                array1.push("&isextractable=" + (args.data.isExtractable=="on"));
                array1.push("&passwordEnabled=" + (args.data.isPasswordEnabled=="on"));
                array1.push("&osTypeId=" + args.data.osTypeId);
                array1.push("&hypervisor=" + args.data.hypervisor);
                if(args.$form.find('.form-item[rel=isPublic]').css("display") != "none")
                  array1.push("&ispublic=" + (args.data.isPublic == "on"));
                if(args.$form.find('.form-item[rel=isFeatured]').css("display") != "none")
                  array1.push("&isfeatured=" + (args.data.isFeatured == "on"));
                $.ajax({
                  url: createURL("registerTemplate" + array1.join("")),
                  dataType: "json",
                  success: function(json) {
                    var items = json.registertemplateresponse.template;  //items might have more than one array element if it's create templates for all zones.
                    args.response.success({data:items[0]});
                    /*
                     if(items.length > 1) {
                     for(var i=1; i 1) {
                     for(var i=1; i