﻿
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Activity</title>

    <script type="text/javascript" src="../../ClientGlobalContext.js.aspx"></script>
    <script type="text/javascript" src="../../../../_common/global.ashx"></script>

    <script src="../Scripts/jquery.1.4.4.min.js" type="text/javascript"></script>
    <script src="../Scripts/json2.js" type="text/javascript"></script>
    <script src="../Scripts/xml2json.js" type="text/javascript"></script>
    <script src="../Scripts/crm.common.js" type="text/javascript"></script>
    <script src="../Scripts/SDK.MetaData.js" type="text/javascript"></script>
    <script src="../Scripts/jquery.ui.datepicker.js" type="text/javascript"></script>
    <script src="../Scripts/datefunction.js" type="text/javascript"></script>

    <link href="../Styles/dialog.css" rel="stylesheet" type="text/css" />
    <link href="../Styles/jquery.ui.datepicker.css" rel="stylesheet" type="text/css" />

    <style type="text/css">
        #selectActivity
        {
            width: 292px;
        }
        #selectRegarding
        {
            width: 288px;
        }
    </style>
    <script type="text/javascript">

        function Search() {

            if(selectActivity.value=='task')
                var filter = "ActivityTypeCode eq '" + selectActivity.value + "' and StatusCode/Value ne 5 ";
            else if (selectActivity.value == 'phonecall')
                var filter = "ActivityTypeCode eq '" + selectActivity.value + "' and StatusCode/Value ne 2 ";
            else if (selectActivity.value == 'email')
                var filter = "ActivityTypeCode eq '" + selectActivity.value + "' and StatusCode/Value ne 6 ";
            else if (selectActivity.value == 'appointment')
                var filter = "ActivityTypeCode eq '" + selectActivity.value + "' and StatusCode/Value ne 3 ";
            else if (selectActivity.value == 'fax')
                var filter = "ActivityTypeCode eq '" + selectActivity.value + "' and StatusCode/Value ne 2 ";
            else if (selectActivity.value == 'serviceappointment')
                var filter = "ActivityTypeCode eq '" + selectActivity.value + "' and StatusCode/Value ne 8 ";
            else if (selectActivity.value == 'letter')
                var filter = "ActivityTypeCode eq '" + selectActivity.value + "' and StatusCode/Value ne 4 ";
            else if (selectActivity.value == 'campaignresponse')
                var filter = "ActivityTypeCode eq '" + selectActivity.value + "' and StateCode/Value ne 1 ";

            var regarding = selectRegarding.value;
            //and RegardingObjectId/Id eq (guid'" + uid + "')";
            //385A00E3-182A-E111-8D90-1CC1DE6E2B2D
            //var obj = Crm.GetRecords('ActivityPointer', null, null, null, null, null);
            var records = Crm.GetRecords('ActivityPointer', null, filter, null, null, null);
            var HTML = "";
            HTML += "<table id='tblActivity' style='table-layout: fixed; width: 100%; height: 32px' class='ms-crm-FormSection' class='#somecell' cellspacing='0' cellpadding='3' align='left' >";
            HTML += "<colgroup><col style='width:5%'/><col style='width:25%'/><col style='width:20%'/><col style='width:15%'/><col style='width:10%'/><col style='width:10%'/><col style='width:12%'/></colgroup>";
            HTML += "<tbody>";
            HTML += "<tr><td class='ms-crm-Form-Section ms-crm-Form-SectionBar' colspan='8'></td></tr>";
            HTML += "<tr>";
            HTML += "<td style='width:10%' align='left'><input id='chkActivity' type='checkbox' onClick='chkAll(" + records.results.length + ")'/></td>";
            HTML += "<td style='width:25%'>Subject</td>";
            HTML += "<td style='width:20%'>Regarding</td>";
            HTML += "<td style='width:15%'>Due Date</td>";
            HTML += "<td style='width:10%'>Priority</td>";
            HTML += "<td style='width:10%'>Duration</td>";
            HTML += "<td style='width:10%'>Completed/closed</td>";
            HTML += "</tr>";
            HTML += "<tr><td class='ms-crm-Form-Section ms-crm-Form-SectionBar' colspan='8'></td></tr>";
            for (var i = 0; i < records.results.length; i++) {
                if (records.results[i].ScheduledEnd == null)
                    var ScheduledEnd = "";

                else
                    ScheduledEnd = new Date(parseInt(records.results[i].ScheduledEnd.replace("/Date(", "").replace(")/", ""), 10)).format("defaultdate");
                if (records.results[i].RegardingObjectId.Name == null)
                    var RegardingName = "";
                else
                    var RegardingName = records.results[i].RegardingObjectId.Name;
                
                if (records.results[i].Description == null)
                    var Description = "";
                else
                    var Description = records.results[i].Description;
                if (selectActivity.value == "email" && Description.startsWith('<FONT'))
                 {
                    var string=records.results[i].Description;
                    var string1 = string.split('>');
                    var string2 = string1[1];
                    var string3 = string2.split('<');
                    var Description = string3[0];
                  }
                    

                if (records.results[i].RegardingObjectId.LogicalName == regarding || (regarding == "" && records.results[i].RegardingObjectId.LogicalName == null)) {
                    HTML += "<tr style='height:32px'>";
                    HTML += "<td style='width:10%' align='left'><input id='chkRecords" + eval(i + 1) + "' type='checkbox' /><img id='imgHide" + eval(i + 1) + "' src='../Images/hide.jpg' alt='Hide Description' onClick='hideDescription(" + i + ")'/><img id='imgShow" + eval(i + 1) + "' src='../Images/show.jpg' alt='Show Description' onClick='showDescription(" + i + ")'/></td>";
                    HTML += "<td style='width:25%'><input style='width: 220px; height:19px;font-size:11px;font-family:Segoe UI, Tahoma, Arial;' id='txtSubject" + eval(i + 1) + "' type='text' value='" + records.results[i].Subject + "'></input></td>";
                    HTML += "<td style='width:20%'><input style='width: 180px; height:19px;font-size:11px;font-family:Segoe UI, Tahoma, Arial;' id='txtRegarding" + eval(i + 1) + "' type='text' value='" + RegardingName + "' readonly='true'></input></td>";
                    HTML += "<td style='width:15%'><table style='width: 100%' class='ms-crm-DateTime'><colgroup><col/></colgroup><tbody><tr>"
                    HTML += "<td><input style='width:130px' id='date_DATE" + eval(i + 1) + "' value='" + ScheduledEnd + "'/></td>";
                    HTML += "</tr></tbody></table></td>";
                    HTML += "<td style='width:10%'><select class='ms-crm-SelectBox' id='selectPriority" + eval(i + 1) + "'></select></td>";
                    HTML += "<td style='width:10%'><select class='ms-crm-SelectBox' id='selectDuration" + eval(i + 1) + "'><option value=''></option><option value='1'>1 minute</option><option value='5'>5 minutes</option><option value='15'>15 minutes</option><option value='30'>30 minutes</option><option value='45'>45 minutes</option><option value='60'>1 hour</option><option value='90'>1.5 hours</option><option value='120'>2 hours</option><option value='150'>2.5 hours</option><option value='180'>3 hours</option><option value='210'>3.5 hours</option><option value='240'>4 hours</option><option value='270'>4.5 hours</option><option value='300'>5 hours</option><option value='330'>5.5 hours</option><option value='360'>6 hours</option><option value='390'>6.5 hours</option><option value='420'>7 hours</option><option value='450'>7.5 hours</option><option value='480'>8 hours</option><option value='1440'>1 day</option><option value='2880'>2 days</option><option value='4320'>3 days</option>";
                    HTML += "</select></td>"
                    HTML += "<td style='width:10%' align='left'><input id='chkActivityClosed" + eval(i + 1) + "' type='checkbox' /></td>";
                    HTML += "</tr>";
                    HTML += "<tr id='rowDescription" + eval(i + 1) + "'><td></td><td colspan='5'><textarea id='txtDescription" + eval(i + 1) + "' rows='2' cols='20' colspan='5'>" + Description + "</textarea></td><td></td></tr>";
//                    document.getElementById('imgShow'+ (i + 1)).style.display = 'none';
                }

               
            }
            
            HTML += "</tbody></table>";
            divActivityTable.innerHTML = HTML;

            document.getElementById('chkActivity').style.display = 'none';

            for (var i = 0; i < records.results.length; i++) {
                if (records.results[i].RegardingObjectId.LogicalName == regarding || (regarding == "" && records.results[i].RegardingObjectId.LogicalName == null)) {
                    document.getElementById('imgShow' + (i + 1)).style.display = 'none';
                    document.getElementById('chkRecords' + (i + 1)).style.display = 'none';
                }
            }
            
            SDK.MetaData.RetrieveAttributeRequest("task", "prioritycode", null, false, PopulatePriority, null);
            for (var i = 0; i < records.results.length; i++) {
                if (records.results[i].RegardingObjectId.LogicalName == regarding || (regarding == "" && records.results[i].RegardingObjectId.LogicalName == null)) {
                    document.getElementById('selectDuration' + (i + 1)).value = records.results[i].ActualDurationMinutes;
                }
            }
           function PopulatePriority(data) {
               var res = data;
               var regarding = selectRegarding.value;
                    for (var i = 0; i < records.results.length; i++) {
                        if (records.results[i].RegardingObjectId.LogicalName == regarding || (regarding == "" && records.results[i].RegardingObjectId.LogicalName == null)) {
                            for (j = 0; j < res.OptionSet.Options.length; j++) {
                                document.getElementById('selectPriority' + (i + 1))[j] = new Option(res.OptionSet.Options[j].OptionMetadata.Label.LocalizedLabels[0].LocalizedLabel.Label, res.OptionSet.Options[j].OptionMetadata.Value);

                            }
                            document.getElementById('selectPriority' + (i + 1)).value = records.results[i].PriorityCode.Value;
                        }
                }
            }
            for (var i = 0; i < records.results.length; i++) {
                if (records.results[i].RegardingObjectId.LogicalName == regarding || (regarding == "" && records.results[i].RegardingObjectId.LogicalName == null)) {

                    $("#date_DATE" + (i + 1)).datepicker();
                    $("#date_IMG" + eval(i + 1)).click(function () {
                        $("#date_DATE" + (i + 1)).datepicker("show");
                    });
                }
            }


        }
        
        function hideDescription(i) {

            document.getElementById('rowDescription' + (i + 1)).style.display = 'none';
            document.getElementById('imgShow' + (i + 1)).style.display = '';
            document.getElementById('imgHide' + (i + 1)).style.display = 'none';
        }
        function showDescription(i) {
            document.getElementById('rowDescription' + (i + 1)).style.display = '';
            document.getElementById('imgShow' + (i + 1)).style.display = 'none';
            document.getElementById('imgHide' + (i + 1)).style.display = '';
        }

        function chkAll(length) {
            var regarding = selectRegarding.value;
            if (selectActivity.value == 'task')
                var filter = "ActivityTypeCode eq '" + selectActivity.value + "' and StatusCode/Value ne 5 ";
            else if (selectActivity.value == 'phonecall')
                var filter = "ActivityTypeCode eq '" + selectActivity.value + "' and StatusCode/Value ne 2 ";
            else if (selectActivity.value == 'email')
                var filter = "ActivityTypeCode eq '" + selectActivity.value + "' and StatusCode/Value ne 6 ";
            else if (selectActivity.value == 'appointment')
                var filter = "ActivityTypeCode eq '" + selectActivity.value + "' and StatusCode/Value ne 3 ";
            else if (selectActivity.value == 'fax')
                var filter = "ActivityTypeCode eq '" + selectActivity.value + "' and StatusCode/Value ne 2 ";
            else if (selectActivity.value == 'serviceappointment')
                var filter = "ActivityTypeCode eq '" + selectActivity.value + "' and StatusCode/Value ne 8 ";
            else if (selectActivity.value == 'letter')
                var filter = "ActivityTypeCode eq '" + selectActivity.value + "' and StatusCode/Value ne 4 ";
            else if (selectActivity.value == 'campaignresponse')
                var filter = "ActivityTypeCode eq '" + selectActivity.value + "' and StateCode/Value ne 1";

            var records = Crm.GetRecords('ActivityPointer', null, filter, null, null, null);
            for (var i = 0; i < length; i++) {
                if (records.results[i].RegardingObjectId.LogicalName == regarding || (regarding == "" && records.results[i].RegardingObjectId.LogicalName == null)) {
                    if (document.getElementById('chkActivity').checked == true) {

                        document.getElementById('chkRecords' + (i + 1)).checked = true;
                    }
                }
            }
            for (var i = 0; i < length; i++) {
                if (records.results[i].RegardingObjectId.LogicalName == regarding || (regarding == "" && records.results[i].RegardingObjectId.LogicalName == null)) {
                    if (document.getElementById('chkActivity').checked == false) {

                        document.getElementById('chkRecords' + (i + 1)).checked = false;
                    }
                }
            }


        }


        function applyChanges() {

            if (selectActivity.value == 'task')
                var filter = "ActivityTypeCode eq '" + selectActivity.value + "' and StatusCode/Value ne 5 ";
            else if (selectActivity.value == 'phonecall')
                var filter = "ActivityTypeCode eq '" + selectActivity.value + "' and StatusCode/Value ne 2 ";
            else if (selectActivity.value == 'email')
                var filter = "ActivityTypeCode eq '" + selectActivity.value + "' and StatusCode/Value ne 6 ";
            else if (selectActivity.value == 'appointment')
                var filter = "ActivityTypeCode eq '" + selectActivity.value + "' and StatusCode/Value ne 3 ";
            else if (selectActivity.value == 'fax')
                var filter = "ActivityTypeCode eq '" + selectActivity.value + "' and StatusCode/Value ne 2 ";
            else if (selectActivity.value == 'serviceappointment')
                var filter = "ActivityTypeCode eq '" + selectActivity.value + "' and StatusCode/Value ne 8 ";
            else if (selectActivity.value == 'letter')
                var filter = "ActivityTypeCode eq '" + selectActivity.value + "' and StatusCode/Value ne 4 ";
            else if (selectActivity.value == 'campaignresponse')
                var filter = "ActivityTypeCode eq '" + selectActivity.value + "' and StateCode/Value ne 1 ";

            var regarding = selectRegarding.value;
          
            var records = Crm.GetRecords('ActivityPointer', null, filter, null, null, null);
            var activity = new Object();


           
            for (var i = 0; i < records.results.length; i++) {

                if (records.results[i].RegardingObjectId.LogicalName == regarding || (regarding == "" && records.results[i].RegardingObjectId.LogicalName == null)) {
                    var date = document.getElementById('date_Date' + (i + 1)).value;
                    var subject = document.getElementById('txtSubject' + (i + 1)).value;
                    var description = document.getElementById('txtDescription' + (i + 1)).value;

                    if (subject.length > 200)
                    {
                        alert('Subject Field Length Exceeds')
                        return (false);
                    }
                    if (description.length > 2000) {
                        alert('Description Field Length Exceeds')
                        return (false);
                    }

                    if (date != "") {
                        var dt = new Date(date);
                        var str = date.split('/');
                        var Mn = str[0];
                        var Day = str[1];
                        var Yr = str[2];
                        if (dt.getDate() != Day) {
                            alert('Invalid Date');
                            return (false);
                        }
                        else if (dt.getMonth() != Mn - 1) {
                            //this is for the purpose JavaScript starts the month from 0

                            alert('Invalid Date');
                            return (false);
                        }
                        else if (dt.getFullYear() != Yr) {
                            alert('Invalid Date');
                            return (false);
                        }

                        
                    }
                }
            }


            for (var i = 0; i < records.results.length; i++) {

                if (records.results[i].RegardingObjectId.LogicalName == regarding || (regarding == "" && records.results[i].RegardingObjectId.LogicalName == null))
                 {
                     
                    activity.Subject = document.getElementById('txtSubject' + (i + 1)).value;
                    
                    if (document.getElementById('date_Date' + (i + 1)).value == "")
                        activity.ScheduledEnd = null;
                    else
                        activity.ScheduledEnd = document.getElementById('date_Date' + (i + 1)).value;
                    activity.PriorityCode = { Value: document.getElementById('selectPriority' + (i + 1)).value };
                    if (document.getElementById('selectDuration' + (i + 1)).value == "")
                        activity.ActualDurationMinutes = null;
                    else
                        activity.ActualDurationMinutes = document.getElementById('selectDuration' + (i + 1)).value;

                    if (document.getElementById('txtDescription' + (i + 1)).value == "")
                        activity.Description = null;
                    else
                        activity.Description = document.getElementById('txtDescription' + (i + 1)).value;
                    
                    var GUID = records.results[i].ActivityId;

                    if (selectActivity.value == 'task')

                        Crm.UpdateRecord('Task', GUID, activity, null, null);
                    else if (selectActivity.value == 'email')

                        Crm.UpdateRecord('Email', GUID, activity, null, null);
                    else if (selectActivity.value == 'phonecall')

                        Crm.UpdateRecord('PhoneCall', GUID, activity, null, null);
                    else if (selectActivity.value == 'letter')

                        Crm.UpdateRecord('Letter', GUID, activity, null, null);
                    else if (selectActivity.value == 'fax')

                        Crm.UpdateRecord('Fax', GUID, activity, null, null);
                    else if (selectActivity.value == 'appointment')

                        Crm.UpdateRecord('Appointment', GUID, activity, null, null);
                    else if (selectActivity.value == 'serviceappointment')

                        Crm.UpdateRecord('ServiceAppointment', GUID, activity, null, null);
                    else if (selectActivity.value == 'campaignresponse')

                        Crm.UpdateRecord('CampaignResponse', GUID, activity, null, null);




                    if (document.getElementById('chkActivity').checked == true) {
                        if (selectActivity.value == 'task')
                            Crm.CompleteRecord('task', GUID, "1", "5", test, null);

                        else if (selectActivity.value == 'email')
                            Crm.CompleteRecord('email', GUID, "1", "6", test, null);

                        else if (selectActivity.value == 'phonecall')
                            Crm.CompleteRecord('phonecall', GUID, "1", "2", test, null);

                        else if (selectActivity.value == 'letter')
                            Crm.CompleteRecord('letter', GUID, "1", "4", test, null);

                        else if (selectActivity.value == 'fax')
                            Crm.CompleteRecord('fax', GUID, "1", "2", test, null);

                        else if (selectActivity.value == 'appointment')
                            Crm.CompleteRecord('appointment', GUID, "1", "3", test, null);

                        else if (selectActivity.value =='serviceappointment')
                            Crm.CompleteRecord('serviceactivity', GUID, "1", "8", test, null);

                        else if (selectActivity.value == 'campaignresponse')
                            alert('You can not close campaign activities');

                     }


                    if (document.getElementById('chkActivityClosed' + (i + 1)).checked == true) {

                        if (selectActivity.value == 'task')

                            Crm.CompleteRecord('task', GUID, "1", "5", null, null);
                        else if (selectActivity.value == 'email')

                            Crm.CompleteRecord('email', GUID, "1", "6", null, null);
                        else if (selectActivity.value == 'phonecall')

                            Crm.CompleteRecord('phonecall', GUID, "1", "2", null, null);
                        else if (selectActivity.value == 'letter')
                            Crm.CompleteRecord('letter', GUID, "1", "4", null, null);

                        else if (selectActivity.value == 'fax')
                            Crm.CompleteRecord('fax', GUID, "1", "2", null, null);

                        else if (selectActivity.value == 'appointment')

                            Crm.CompleteRecord('appointment', GUID, "1", "3", null, null);
                        else if (selectActivity.value == 'serviceappointment')
                            Crm.CompleteRecord('serviceappointment', GUID, "1", "8", null, null);

                        else if (selectActivity.value == 'campaignresponse') {
                            alert('You can not close campaign activities');
                            return (false);
                        }
                    }
                }
            }
            alert('Records Updated');
            document.getElementById('tblActivity').style.display = 'none';
        }

    </script>
    
    
</head>
<body>
    <div style="overflow: auto; margin-bottom: 0px;">
        <table style="width:100%;background-color:#ffffff;" cellspacing="0" cellpadding="0">
            <tr>
                <td colspan="2" class="ms-crm-Dialog-Header" valign="middle">
                  
                    <div class="ms-crm-Dialog-Header-Title" id="DlgHdTitle">
                    <img src="../Images/tiny_logo_mf.gif" alt="Mindfire Solutions"/>
                        Microsoft Dynamic CRM - Quick Activity Editor
                    </div>
                </td>
            </tr>
       
    
    <tr>
                <td colspan="2" >
                    <!--<div class="ms-crm-Dialog-Main" >-->
  <div style="overflow: auto; margin-bottom: 0px;BACKGROUND-COLOR: #e9edf1">
        <table style="width: 100%;" cellspacing="0" cellpadding="3" class="ms-crm-FormSection">
            <colgroup>
                <col width="115" />
                <col />
                <col width="115" />
                <col />
                <col/>
            </colgroup>
            <tbody>
                <tr>
                    <td>&nbsp
                    </td>
                </tr>
                <tr>
                    <td>
                    </td>
                </tr>
                <tr valign="top">
                    <td class="ms-crm-FieldLabel-LeftAlign ms-crm-Field-Required">
                        <label for="lblActivityType">
                            Activity Type :</label>
                    </td>
                    <td>
                        <select class="ms-crm-SelectBox" id="selectActivity">
                            <option value="task">Task</option>
                            <option value="email">E-mail</option>
                            <option value="phonecall">Phone call</option>
                            <option value="letter">Letter</option>
                            <option value="fax">Fax</option>
                            <option value="appointment">Appointment</option>
                            <option value="serviceappointment">Service Activity</option>
                            <option value="campaignresponse">Campaign Response</option>
                        </select>
                    </td>
                    <td class="ms-crm-FieldLabel-LeftAlign ms-crm-Field-Required">
                        <label for="lblRegarding">
                            Regarding :</label>
                    </td>
                    <td>
                        <select class="ms-crm-SelectBox" id="selectRegarding">
                            <option value=""></option>
                            <option value="account">Account</option>
                            <option value="incident">Case</option>
                            <option value="contact">Contact</option>
                            <option value="contract">Contract</option>
                            <option value="invoice">Invoice</option>
                            <option value="quote">Quote</option>
                            <option value="order">Order</option>
                            <option value="lead">Lead</option>
                            <option value="opportunity">Opportunity</option>
                            <option value="campaign">Campaign</option>
                        </select>
                    </td>
                    <td align="left" valign="top">
                        <button class="ms-crm-Button" id="Button2" onclick="Search();" type="button">
                            Search</button>
                    </td>
                </tr>
                <tr valign="top">
                    <td class="ms-crm-Form-Section ms-crm-Form-SectionBar" colspan="8">&nbsp
                    </td>
                </tr>
                <tr valign="top">
                    <td>
                    </td>
                    
                </tr>
               
            </tbody>
        </table>
    </div>
    </td>
    </tr>
   <tr>
    <td colspan="2" >
    <div id="divActivityTable" class="ms-crm-Dialog-Main" style="width: 100%">
        <!--<table style="TABLE-LAYOUT: fixed  ;width: 100%" class="ms-crm-FormSection" cellspacing="0" cellpadding="3">
            <colgroup>
                <col style="width:5%"/>
                <col style="width:25%"/>
                <col style="width:20%"/>
                <col style="width:15%"/>
                <col style="width:10%"/>
                <col style="width:10%"/>
                <col style="width:15%"/>
               
            </colgroup>
            <tbody>
           
                <tr style="width: 100%">
                    <td style="width:5%">-->
                       <!-- <input id="chkActivity" type="checkbox" />-->
                    <!--/td>
                    <td style="width:25%">
                        Subject
                    </td>
                    <td style="width:20%">
                        Regarding
                    </td>
                    <td style="width:15%">
                        Due Date
                    </td>
                    <td style="width:10%">
                        Priority
                    </td>
                    <td style="width:10%">
                        Duration
                    </td>
                    <td style="width:15%">
                        Completed/Closed
                    </td>
                </tr>          
            </tbody>
        </table>-->
   </div>
        </td>
        </tr>
    <tr id="divSaveButton" class="ab-dialog-footer">
            <td style="width:100%"class="ms-crm-Dialog-Footer ms-crm-Dialog-Footer-Center" align="center">
                <button class="ms-crm-Button" id="cmdDialogSave" onclick="applyChanges();" type="button">Save</button>
            </td>
    </tr>
   <!--<!-- </div>-->
   <!-- <div id="divSaveButton">

    </div>
   -->
    <!-- </div>
               </td>
            </tr>
-->
    <!-- </table> 
         
    </div>
-->
</table>
       
 </div>

</body>
</html>
