(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: {            
            featured: { label: 'Featured' },
            community: { label: 'Community' },
            mine: { label: 'Mine' }
          },          
          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() && !(cloudStack.context.projects &&
                                             cloudStack.context.projects[0]))
                            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});
                        }
                      });
                      
                      args.$select.change(function() {
                        var $form = $(this).closest('form');                        
                        if($(this).val() == "VMware") {
                          $form.find('.form-item[rel=rootDiskControllerType]').css('display', 'inline-block');
                          $form.find('.form-item[rel=nicAdapterType]').css('display', 'inline-block');
                          $form.find('.form-item[rel=keyboardType]').css('display', 'inline-block');
                        }
                        else {
                          $form.find('.form-item[rel=rootDiskControllerType]').hide();
                          $form.find('.form-item[rel=nicAdapterType]').hide();
                          $form.find('.form-item[rel=keyboardType]').hide();
                        }                        
                      });                      
                    }
                  },
                  
                  //fields for hypervisor == "VMware" (starts here)
                  rootDiskControllerType: {
                    label: 'Root disk controller',
                    isHidden: true,
                    select: function(args) {
                      var items = []
                      items.push({id: "", description: ""});
                      items.push({id: "scsi", description: "scsi"});
                      items.push({id: "ide", description: "ide"});
                      args.response.success({data: items});
                    }
                  },
                  nicAdapterType: {
                    label: 'NIC adapter type',
                    isHidden: true,
                    select: function(args) {
                      var items = []
                      items.push({id: "", description: ""});
                      items.push({id: "E1000", description: "E1000"});
                      items.push({id: "PCNet32", description: "PCNet32"});
                      items.push({id: "Vmxnet2", description: "Vmxnet2"});
                      items.push({id: "Vmxnet3", description: "Vmxnet3"});
                      args.response.success({data: items});
                    }
                  },
                  keyboardType: {
                    label: 'Keyboard type',
                    isHidden: true,
                    select: function(args) {
                      var items = []
                      items.push({id: "", description: ""});
                      items.push({id: "us", description: "US"});  
                      items.push({id: "jp", description: "Japanese"});                                         
                      args.response.success({data: items});
                    }
                  },
                  //fields for hypervisor == "VMware" (ends here)
                  
                  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"));
                //VMware only (starts here)
                if(args.$form.find('.form-item[rel=rootDiskControllerType]').css("display") != "none" && args.data.rootDiskControllerType != "")
                  array1.push("&details[0].rootDiskController=" + args.data.rootDiskControllerType);                    
                if(args.$form.find('.form-item[rel=nicAdapterType]').css("display") != "none" && args.data.nicAdapterType != "")
                  array1.push("&details[0].nicAdapter=" + args.data.nicAdapterType);                    
                if(args.$form.find('.form-item[rel=keyboardType]').css("display") != "none" && args.data.keyboardType != "")
                  array1.push("&details[0].keyboard=" + args.data.keyboardType);  
                //VMware only (ends here)
                
                $.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