﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
<link rel="stylesheet" type="text/css" href="../Styles/Crmstyle.css" /> 
<script type="text/javascript" src="../Scripts/jquery1.9.1.js"></script>
<script type="text/javascript" src="../Scripts/SDK.MetaData.js"></script>
<script src="../../../ClientGlobalContext.js.aspx"></script>
<script type="text/javascript">

    var ORGANIZATION_SERVICE_URL = Xrm.Page.context.prependOrgName("/XRMServices/2011/Organization.svc");
    var FORM_CREATE = 1;
    var FORM_QUICK_CREATE = 5;
    window.onload = function () {
        RetrieveOptionSetAttributeMetadata("incident", "CaseTypeCode");
        RetrieveOptionSetAttributeMetadata("incident", "CaseOriginCode");
    }

    //////////////***************Retrieve & Load Case Optionset Values Start********************///////////////////

    function RetrieveOptionSetAttributeMetadata(entityName, attributeName) {
        SDK.Metadata.RetrieveAttribute(entityName, attributeName, null, true, successRetrieveEntityAttribute, errorRetrieveEntityAttribute, null);
    }

    function successRetrieveEntityAttribute(attributeMetadataCollection) {
        LoadSelectedEntityAttributeList(attributeMetadataCollection);
    }

    function errorRetrieveEntityAttribute(error) {
        //message.innerText = error.message;
    }

    function LoadSelectedEntityAttributeList(data) {

        for (var i = 0; i < data.OptionSet.Options.length; i++) {

            if (data.SchemaName == "CaseTypeCode") {
                $('#SelectCaseType').append('<option value="' + data.OptionSet.Options[i].Value + '">' + data.OptionSet.Options[i].Label.UserLocalizedLabel.Label + '</option>')
            }

            if (data.SchemaName == "CaseOriginCode") {
                $('#SelectCaseOrigin').append('<option value="' + data.OptionSet.Options[i].Value + '">' + data.OptionSet.Options[i].Label.UserLocalizedLabel.Label + '</option>')
                
                //Set the CaseOrigin to E-mail by default on Form Create
                if (window.parent.Xrm.Page.ui.getFormType() == FORM_CREATE || window.parent.Xrm.Page.ui.getFormType() == FORM_QUICK_CREATE) {
                    if (data.OptionSet.Options[i].Value == 2)
                        $('#SelectCaseOrigin').val(data.OptionSet.Options[i].Value);
                }

            }
            
            if (window.parent.Xrm.Page.getAttribute('iotap_casetype').getValue() != null) {
                $('#SelectCaseType').val(window.parent.Xrm.Page.getAttribute('iotap_casetype').getValue().toLowerCase());
            }

            if (window.parent.Xrm.Page.getAttribute('iotap_caseorigin').getValue() != null) {
                $('#SelectCaseOrigin').val(window.parent.Xrm.Page.getAttribute('iotap_caseorigin').getValue().toLowerCase());
            }
        }

    }
    //////////////***************Retrieve & Load Case Optionset Values End********************///////////////////
    function UpdateAttribute() {
        var selectedCaseType = SelectCaseType.value;
        var selectedCaseOrigin = SelectCaseOrigin.value;
        var casetype = "iotap_casetype";
        var caseorigin = "iotap_caseorigin";
        window.parent.Xrm.Page.getAttribute(casetype).setValue(selectedCaseType);
        window.parent.Xrm.Page.getAttribute(caseorigin).setValue(selectedCaseOrigin);
    }


</script>
    <style type="text/css">
        .label
        {
            font-family: Segoe UI, Tahoma, Arial;
            font-size: 11px;
            font-weight: normal;
        }
        .label1
        {
            font-family: Segoe UI, Tahoma, Arial;
            font-size: 11px;
            font-weight: normal;
            color: Red;
        }
        .body
        {
            margin-left: 0px;
            margin-top: 0px;
            margin-right: 0px;
            margin-bottom: 0px;
            background-color: White;
            padding: 0px;
        }
        .style1
        {
            width: 8%;
        }
        .style2
        {
            width:42%;
        }
        .style3
        {
            padding-left:10px;
            width: 8%;
        }
    </style>
</head>
<body style="width:100%;margin:0 0 0 0;padding:0 0 0 0;">
<table style="width: 100%; border-style: none; table-layout: fixed;" cellpadding="0"
        cellspacing="0">
<colgroup>
            <col width="108">
            <col>
            <col width="123">
            <col>
            <tbody>
        <tr>
            <td align="left"style="width:8%">
                <label class="label">Case Type</label></td>
            <td align="right" width="42%" >
                <select id="SelectCaseType" class="ms-crm-SelectBox" name="D1" onchange="UpdateAttribute()" >
                    <option ></option>
                </select></td>
            <td align="left"style="padding-left:15px; width:8%;">
            <label class="label">Case Origin</label></td>
            <td align="right" style=" width:42%;">
                <select id="SelectCaseOrigin" class="ms-crm-SelectBox" name="D2" onchange="UpdateAttribute()" >
                    <option ></option>
                </select></td>
        </tr>
        </tbody>
        </table>
</body>
</html>
