﻿<!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/json.js"></script>
<script src="../../../ClientGlobalContext.js.aspx"></script>
<script type="text/javascript">

    var webApiUrl = window.parent.Xrm.Page.context.getClientUrl() + "/api/data/v8.0/";
    var FORM_CREATE = 1;
    var FORM_QUICK_CREATE = 5;

    window.onload = function () {
        RetrieveOptionSetAttributeMetadata("e3fe4ff2-a630-49bb-a1e9-debc3a076815", "1b0df300-5c7b-4dd1-900d-9fbe77dda9d2", "casetypecode");  //incident casetypecode
        RetrieveGlobalOptionSetAttributeMetadata( "08fa2cb2-e3fe-497a-9b5d-ee887f5cc3cd", "caseorigincode" ); //caseorigincode  caseorigincoe

    }

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

    function RetrieveGlobalOptionSetAttributeMetadata(attributeName, attributeSchemaName )
    {
        var filterQuery = "GlobalOptionSetDefinitions(" + attributeName + ")";
        var httpReq = new XMLHttpRequest();
        httpReq.open( "GET", webApiUrl + filterQuery, false );
        httpReq.setRequestHeader( "Accept", "application/json" );
        httpReq.setRequestHeader( "Content-Type", "application/json; charset=utf-8" );
        httpReq.setRequestHeader( "OData-MaxVersion", "4.0" );
        httpReq.setRequestHeader( "OData-Version", "4.0" );
        httpReq.send( null );

        if ( httpReq.status === 200 ) {

            var attrValueColl = JSON.parse( httpReq.responseText ).Options;
            for ( var i = 0; i < attrValueColl.length; i++ ) {

                if ( attributeSchemaName == "caseorigincode" ) {
                    $( '#SelectCaseOrigin' ).append( '<option value="' + attrValueColl[i].Value + '">' + attrValueColl[i].Label.UserLocalizedLabel.Label + '</option>' );
                }
            }
        }
    }

    function RetrieveOptionSetAttributeMetadata(entityName, attributeName, attributeSchemaName) {
       
        var filterQuery = "EntityDefinitions(" + entityName + ")/Attributes(" + attributeName + ")/Microsoft.Dynamics.CRM.PicklistAttributeMetadata/OptionSet?$select=Options";
        var httpReq = new XMLHttpRequest();
        httpReq.open( "GET", webApiUrl + filterQuery, false );
        httpReq.setRequestHeader( "Accept", "application/json" );
        httpReq.setRequestHeader( "Content-Type", "application/json; charset=utf-8" );
        httpReq.setRequestHeader( "OData-MaxVersion", "4.0" );
        httpReq.setRequestHeader( "OData-Version", "4.0" );
        httpReq.send( null );

        if ( httpReq.status === 200 ) {
            
            var attrValueColl = JSON.parse( httpReq.responseText ).Options;
            for ( var i = 0; i < attrValueColl.length; i++ ) {
                
                if ( attributeSchemaName == "casetypecode" ) {
                    $( '#SelectCaseType' ).append( '<option value="' + attrValueColl[i].Value + '">' + attrValueColl[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 ) {
        $( '#SelectCaseOrigin' ).val( 2 );
    }
    else {
        if ( window.parent.Xrm.Page.getAttribute( 'iotap_caseorigin' ).getValue() != null ) {
            $( '#SelectCaseOrigin' ).val( window.parent.Xrm.Page.getAttribute( 'iotap_caseorigin' ).getValue().toLowerCase() );
        }
    }

    if ( window.parent.Xrm.Page.getAttribute( 'iotap_casetype' ).getValue() != null ) {
        $( '#SelectCaseType' ).val( window.parent.Xrm.Page.getAttribute( 'iotap_casetype' ).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>
