{
  "includeFiles":[
  ],
  "metadata":{
    "amiScriptMethods":[
      "{\n",
      "  /*\n",
      "   * Helper method to add a number and operation to the calculator history.\n",
      "   \n",
      "   * param: operation - a string representing the operation performed. \n",
      "   * param: operand - the value associated with the operand. \n",
      "   * returns: true\n",
      "   */\n",
      "  boolean addToCalculatorHistory(String operation, Number operand){\n",
      "    \n",
      "    // Get history\n",
      "    List history = getHistory();\n",
      "    \n",
      "    // Add the new calculation\n",
      "    history.add(operation);\n",
      "    history.add(operand);\n",
      "    \n",
      "    // Call the history fields callback to redraw\n",
      "    getHistoryField().getCallback(\"onChange\").invoke(null);\n",
      "    return true;\n",
      "  };\n",
      "  \n",
      "  boolean clearHistory(){\n",
      "    \n",
      "    // Get history\n",
      "    List history = getHistory();\n",
      "    \n",
      "    // Clear it\n",
      "    history.clear();\n",
      "    \n",
      "    // Update history field\n",
      "    getHistoryField().getCallback(\"onChange\").invoke(null);\n",
      "  };\n",
      "  \n",
      "  /*\n",
      "   * Helper method to get the history list from the session.\n",
      "   * If it doesnt exist creates an empty list, stores in the session and returns that\n",
      "   * returns: history list\n",
      "   */\n",
      "  List getHistory() {\n",
      "    \n",
      "    // Get calculator history from session\n",
      "    List history = session.getValue(\"CALC_HISTORY\");\n",
      "    \n",
      "    // Create new history if not present\n",
      "    if (null == history) {\n",
      "      history = new List();\n",
      "      session.setValue(\"CALC_HISTORY\", history);\n",
      "    }\n",
      "    \n",
      "    return history;\n",
      "  };\n",
      "  /*\n",
      "   * Helper method to find the history text area in the calculator window. \n",
      "   * First checks the session to see if it has been cached else performs a search\n",
      "   * returns: The history FormTextareaField or null if not found\n",
      "   */\n",
      "  FormTextareaField getHistoryField() {\n",
      "    // Try to get the history field from the session\n",
      "    FormTextareaField historyField = session.getValue(\"CALC_HIST_FIELD\");\n",
      "    \n",
      "    // if there isnt one we need to go find it\n",
      "    if (historyField == null){\n",
      "      \n",
      "      // Loop through all the windows looking for our Calculator \n",
      "      for (Window window : session.getWindowsMap().getValues()){\n",
      "        \n",
      "        // If we find the calculator window\n",
      "        if (window.getName() == \"Calculator\") {\n",
      "          \n",
      "          // Look through all the fields for the history textarea\n",
      "          for (FormField fField : window.getInnerPanel().getChildren()) {\n",
      "            \n",
      "            // If we find the textarea we want\n",
      "            if (fField.getVariableName() == \"history\") {\n",
      "              \n",
      "              // Store the field in the session to avoid re-searching every time.\n",
      "              session.setValue(\"CALC_HIST_FIELD\", fField);\n",
      "              \n",
      "              // Return the field\n",
      "              return fField;\n",
      "            }\n",
      "          }\n",
      "        }\n",
      "      }\n",
      "    } else {\n",
      "      return historyField;\n",
      "    }\n",
      "    \n",
      "    // If we get here we either couldnt find the textarea in the window or\n",
      "    // couldnt find the calculator window. Either way tell the user and return null\n",
      "    session.alert(\"Could not find a textarea in the calculator window whose variable name is 'history'. If you have made changes to the layout consider reverting them or reload the original tutorial layout.\");\n",
      "    return null;\n",
      "  };\n",
      "  \n",
      "}"
    ],
    "customPrefsImportMode":"reject",
    "dm":{
    },
    "fileVersion":3,
    "menubarPosition":"top",
    "rt":{
      "processors":[
      ]
    },
    "stm":{
      "styles":[
        {
          "id":"LAYOUT_DEFAULT",
          "lb":"Layout Default",
          "pt":"DEFAULT"
        }
      ]
    },
    "transientIdPrefix":"TRANSIENT_"
  },
  "portletConfigs":[
    {
      "portletBuilderId":"amidesktop",
      "portletConfig":{
        "amiPanelId":"@DESKTOP",
        "amiStyle":{
          "pt":"LAYOUT_DEFAULT"
        },
        "windows":[
          {
            "header":true,
            "heightDflt":318,
            "hidden":false,
            "leftDflt":73,
            "portlet":"Html1",
            "pos":0,
            "stateDflt":"flt",
            "title":"Calculator",
            "topDflt":100,
            "widthDflt":640,
            "zindexDflt":1
          },
          {
            "header":true,
            "heightDflt":589,
            "hidden":false,
            "leftDflt":80,
            "portlet":"Html2",
            "pos":1,
            "stateDflt":"flt",
            "title":"Window",
            "topDflt":100,
            "widthDflt":782,
            "zindexDflt":2
          }
        ]
      }
    },
    {
      "portletBuilderId":"amiform",
      "portletConfig":{
        "amiPanelId":"Html1",
        "amiStyle":{
          "pt":"LAYOUT_DEFAULT",
          "vl":{
            "form":{
              "bgCl":"#2d2d2d"
            }
          }
        },
        "fields":[
          {
            "bgColor":"#707070ff",
            "borderColor":"#b5b4b3",
            "borderRadius":10,
            "borderWidth":3,
            "callbacks":{
              "entries":[
                {
                  "amiscript":[
                    "input.setValue(0);\n",
                    "resultField.setValue(0);\n",
                    "clearHistory();"
                  ],
                  "linkedVariables":[
                    {
                      "ari":"FIELD:Html1?input",
                      "varName":"input"
                    },
                    {
                      "ari":"FIELD:Html1?result",
                      "varName":"resultField"
                    }
                  ],
                  "name":"onChange"
                }
              ]
            },
            "fieldFontSize":50,
            "fontColor":"#ffff00",
            "heightPx":80,
            "l":"C",
            "labelHidden":true,
            "leftPosPx":280,
            "n":"clear",
            "t":"buttonField",
            "topPosPx":120,
            "widthPx":100,
            "zidx":7
          },
          {
            "bgColor":"#707070ff",
            "borderColor":"#b5b4b3",
            "borderRadius":7,
            "borderWidth":3,
            "callbacks":{
              "entries":[
                {
                  "amiscript":[
                    "// Get the index that the user wants to delete\n",
                    "int deleteIdx = searchInput.getValue();\n",
                    "\n",
                    "// If it is valid\n",
                    "if (deleteIdx != null){\n",
                    "  \n",
                    "  // Get the history\n",
                    "  List history = getHistory();\n",
                    "  \n",
                    "  // Delete the operand and value\n",
                    "  history.remove(deleteIdx * 2);\n",
                    "  history.remove(deleteIdx * 2);\n",
                    "  \n",
                    "  // Ask the history textarea to re-draw\n",
                    "  historyField.getCallback(\"onChange\").invoke(null);\n",
                    "\n",
                    "}"
                  ],
                  "linkedVariables":[
                    {
                      "ari":"FIELD:Html1?history",
                      "varName":"historyField"
                    },
                    {
                      "ari":"FIELD:Html1?searchInput",
                      "varName":"searchInput"
                    }
                  ],
                  "name":"onChange"
                }
              ]
            },
            "fieldFontSize":30,
            "fontColor":"#ffff00",
            "heightPx":80,
            "l":"Delete Entry",
            "labelColor":"#ffff00",
            "labelHidden":true,
            "leftPosPx":700,
            "n":"deleteHistory",
            "t":"buttonField",
            "topPosPx":220,
            "widthPx":180,
            "zidx":9
          },
          {
            "bgColor":"#707070ff",
            "borderColor":"#b5b4b3",
            "borderRadius":10,
            "borderWidth":3,
            "callbacks":{
              "entries":[
                {
                  "amiscript":[
                    "int userInput = (int)(inputField.getValue());\n",
                    "int currentResult = (int)(resultField.getValue());\n",
                    "int result = currentResult / userInput;\n",
                    "\n",
                    "if (userInput == null) {\n",
                    "  string message = \"The input '\" + inputField.getValue() + \"' is not a valid number\";\n",
                    "  session.alert(message);\n",
                    "} else if (userInput == 0) {\n",
                    "  session.alert(\"You cannot divide by zero.\");\n",
                    "} else {\n",
                    "  resultField.setValue(result);\n",
                    "\n",
                    "  addToCalculatorHistory(\"/\", userInput);\n",
                    "}"
                  ],
                  "linkedVariables":[
                    {
                      "ari":"FIELD:Html1?input",
                      "varName":"inputField"
                    },
                    {
                      "ari":"FIELD:Html1?result",
                      "varName":"resultField"
                    }
                  ],
                  "name":"onChange"
                }
              ]
            },
            "fieldFontSize":50,
            "fontColor":"#ffff00",
            "heightPx":80,
            "l":"/",
            "labelHidden":true,
            "leftPosPx":400,
            "n":"divide",
            "t":"buttonField",
            "topPosPx":220,
            "widthPx":100,
            "zidx":4
          },
          {
            "bgColor":"#707070ff",
            "borderColor":"#b5b4b3",
            "borderRadius":7,
            "borderWidth":3,
            "disabled":true,
            "fieldFontFamily":"Courier",
            "fieldFontSize":13,
            "fontColor":"#fdfd02",
            "heightPx":180,
            "l":"History",
            "labelBoldSetting":false,
            "labelColor":"#ff7f00",
            "labelFontSize":50,
            "labelHidden":true,
            "labelItalicSetting":false,
            "labelPadding":3,
            "labelSide":1,
            "labelSideAlignment":1,
            "labelUnderlineSetting":false,
            "leftPosPx":640,
            "n":"history",
            "t":"textArea",
            "topPosPx":20,
            "widthPx":240,
            "zidx":8
          },
          {
            "bgColor":"#726a6aff",
            "borderColor":"#ffff00",
            "borderRadius":10,
            "borderWidth":2,
            "dme":"0",
            "fieldFontSize":45,
            "fontColor":"#ffffff",
            "heightPx":80,
            "l":"Input:",
            "labelColor":"#ffffff",
            "labelFontSize":48,
            "labelPadding":6,
            "labelSide":0,
            "labelSideAlignment":0,
            "leftPosPx":20,
            "n":"input",
            "t":"textField",
            "topPosPx":220,
            "widthPx":359,
            "zidx":6
          },
          {
            "bgColor":"#707070ff",
            "borderColor":"#b5b4b3",
            "borderRadius":10,
            "borderWidth":3,
            "callbacks":{
              "entries":[
                {
                  "amiscript":[
                    "int userInput = (int)(inputField.getValue());\n",
                    "int currentResult = (int)(resultField.getValue());\n",
                    "int result = currentResult - userInput;\n",
                    "\n",
                    "if (userInput == null) {\n",
                    "  string message = \"The input '\" + inputField.getValue() + \"' is not a valid number\";\n",
                    "  session.alert(message);\n",
                    "} else {\n",
                    "  resultField.setValue(result);\n",
                    "\n",
                    "  addToCalculatorHistory(\"-\", userInput);\n",
                    "}\n",
                    "\n"
                  ],
                  "linkedVariables":[
                    {
                      "ari":"FIELD:Html1?input",
                      "varName":"inputField"
                    },
                    {
                      "ari":"FIELD:Html1?result",
                      "varName":"resultField"
                    }
                  ],
                  "name":"onChange"
                }
              ]
            },
            "fieldFontSize":50,
            "fontColor":"#ffff00",
            "heightPx":80,
            "l":"-",
            "labelHidden":true,
            "leftPosPx":520,
            "n":"minus",
            "t":"buttonField",
            "topPosPx":120,
            "widthPx":100,
            "zidx":3
          },
          {
            "bgColor":"#707070ff",
            "borderColor":"#b5b4b3",
            "borderRadius":10,
            "borderWidth":3,
            "callbacks":{
              "entries":[
                {
                  "amiscript":[
                    "int userInput = inputField.getValue();\n",
                    "int currentResult = resultField.getValue();\n",
                    "\n",
                    "int result = userInput * currentResult;\n",
                    "\n",
                    "if (userInput == null) {\n",
                    "  string message = \"The input '\" + inputField.getValue() + \"' is not a valid number\";\n",
                    "  session.alert(message);\n",
                    "} else {\n",
                    "  resultField.setValue(result);  \n",
                    "\n",
                    "  addToCalculatorHistory(\"*\", userInput);\n",
                    "}"
                  ],
                  "linkedVariables":[
                    {
                      "ari":"FIELD:Html1?history",
                      "varName":"historyField"
                    },
                    {
                      "ari":"FIELD:Html1?input",
                      "varName":"inputField"
                    },
                    {
                      "ari":"FIELD:Html1?result",
                      "varName":"resultField"
                    }
                  ],
                  "name":"onChange"
                }
              ]
            },
            "fieldFontSize":50,
            "fontColor":"#ffff00",
            "heightPx":80,
            "l":"x",
            "labelHidden":true,
            "leftPosPx":520,
            "n":"mult",
            "t":"buttonField",
            "topPosPx":220,
            "widthPx":100,
            "zidx":5
          },
          {
            "bgColor":"#707070ff",
            "borderColor":"#b5b4b3",
            "borderRadius":10,
            "borderWidth":3,
            "callbacks":{
              "entries":[
                {
                  "amiscript":[
                    "int userInput = (int)(inputField.getValue());\n",
                    "int currentResult = (int)(resultField.getValue());\n",
                    "int result = userInput + currentResult;\n",
                    "\n",
                    "if (userInput == null) {\n",
                    "  string message = \"The input '\" + inputField.getValue() + \"' is not a valid number\";\n",
                    "  session.alert(message);\n",
                    "} else {\n",
                    "  resultField.setValue(result);  \n",
                    "\n",
                    "  addToCalculatorHistory( \"+\", userInput);\n",
                    "}\n",
                    "\n"
                  ],
                  "linkedVariables":[
                    {
                      "ari":"FIELD:Html1?input",
                      "varName":"inputField"
                    },
                    {
                      "ari":"FIELD:Html1?result",
                      "varName":"resultField"
                    }
                  ],
                  "name":"onChange"
                }
              ]
            },
            "fieldFontSize":50,
            "fontColor":"#ffff00",
            "heightPx":80,
            "l":"+",
            "labelHidden":true,
            "leftPosPx":400,
            "n":"plus",
            "t":"buttonField",
            "topPosPx":120,
            "widthPx":100,
            "zidx":1
          },
          {
            "bgColor":"#6d6b6bff",
            "borderColor":"#ffff00",
            "borderRadius":10,
            "borderWidth":2,
            "disabled":true,
            "dme":"0",
            "fieldFontSize":45,
            "fontColor":"#ffff00",
            "heightPx":80,
            "l":"",
            "leftPosPx":20,
            "n":"result",
            "t":"textField",
            "topPosPx":20,
            "widthPx":600,
            "zidx":2
          },
          {
            "bgColor":"#707070ff",
            "borderColor":"#ffff00",
            "borderRadius":7,
            "borderWidth":2,
            "fieldFontSize":28,
            "fontColor":"#ffffff",
            "heightPx":80,
            "l":"",
            "labelColor":"#ffffff",
            "leftPosPx":640,
            "n":"searchInput",
            "t":"textField",
            "topPosPx":220,
            "widthPx":60,
            "zidx":9
          }
        ],
        "snap":20,
        "upid":"Html1"
      }
    },
    {
      "portletBuilderId":"amiform",
      "portletConfig":{
        "amiPanelId":"Html2",
        "amiStyle":{
          "pt":"LAYOUT_DEFAULT"
        },
        "fields":[
          {
            "callbacks":{
              "entries":[
                {
                  "amiscript":[
                    "boolean premiumUser = false;\n",
                    "double lifetimeSpend = 1000.0;\n",
                    "double discount = (premiumUser == true) ? 20.0 : (lifetimeSpend > 500.0 ? 10.0 : 2.5);\n",
                    "\n",
                    "if (premiumUser == true) {\n",
                    "  discount = 20.0;\n",
                    "} else {\n",
                    "  if (lifetimeSpend > 500.0) {\n",
                    "    discount = 10.0;\n",
                    "  } else {\n",
                    "    discount = 2.5;\n",
                    "  }\n",
                    "}\n"
                  ],
                  "linkedVariables":[
                  ],
                  "name":"onChange"
                }
              ]
            },
            "heightPx":20,
            "l":"Button",
            "labelHidden":true,
            "leftPosPx":200,
            "n":"button",
            "t":"buttonField",
            "topPosPx":80,
            "widthPx":200,
            "zidx":1
          }
        ],
        "snap":20,
        "upid":"Html2"
      }
    }
  ]
}