﻿function saveContactMain($frm, entity) {
    var jsonData;
    var uri = '/Services/DefaultService.svc/SetContactMain';
    if (entity == 'opportunity')
        uri = '/Services/DefaultService.svc/SetOpportunity';
    else if (entity == 'support')
        uri = '/Services/DefaultService.svc/SetSupport';
    //if (entity == 'contact' || entity == 'account') {
        jsonData = serializeContactMain(entity);
    //}
        $.ajax({
            type: "POST",
            url: uri,
            data: JSON.stringify(jsonData),
            
            dataType: "json",
            success: function(msg) {
                if (msg.d != null) {
                    createListEvent(msg.d.id, msg.d.flagstatus, msg.d.groupby, getToeName(entity), msg.d.title, true, true);
                    var oid = $frm.smartForm('option', 'owner');
                    if(oid != undefined) {
                    //var oFrm = 
                    }
                }
            }
        });
    $frm.dialog('close');
}
function saveSelectedAddress(selectedTab, entity) {
    var jsonData, $smartForm;
    var uri = "/Services/DefaultService.svc/SetMailingAddress";
    $smartForm = getSmartForm(entity);
    
    if ($smartForm != undefined) {
        jsonData = { 'mad': selectedTab };
        jsonData['contactserviceid'] = $smartForm.data('cid')
        jsonData = jsonSerializeDefaults(jsonData);
        $smartForm.data('mad', selectedTab.toString());

        $.ajax({
            type: "POST",
            url: uri,
            data: JSON.stringify(jsonData),
            
            dataType: "json",
            success: function(msg) {
                return msg.d;
            }
        });
    }
}
function saveSelectedPhone(entity) {
    var jsonData, $smartForm;
    var uri = "/Services/DefaultService.svc/SetPhoneSelector";
    $smartForm = getSmartForm(entity);

    if ($smartForm != undefined) {
        jsonData = { 'contactserviceid': $smartForm.data('cid') };
        jsonData['ph1'] = contactPhoneIndex($smartForm.find('select.sd-ph1').val(), entity);
        jsonData['ph2'] = contactPhoneIndex($smartForm.find('select.sd-ph2').val(), entity);
        jsonData['ph3'] = contactPhoneIndex($smartForm.find('select.sd-ph3').val(), entity);
        jsonData = jsonSerializeDefaults(jsonData);

        $.ajax({
            type: "POST",
            url: uri,
            data: JSON.stringify(jsonData),
            
            dataType: "json",
            success: function(msg) {
                return msg.d;
            }
        });
    }
}
function saveSelectedEmail(entity) {
    var jsonData, $smartForm;
    var uri = "/Services/DefaultService.svc/SetEmailSelector";
    $smartForm = getSmartForm(entity);

    if ($smartForm != undefined) {
        jsonData = { 'contactserviceid': $smartForm.data('cid') };
        jsonData['em1'] = contactEmailIndex($smartForm.find('select.sd-select-email').val(), entity);
        jsonData = jsonSerializeDefaults(jsonData);

        $.ajax({
            type: "POST",
            url: uri,
            data: JSON.stringify(jsonData),
            
            dataType: "json",
            success: function(msg) {
                return msg.d;
            }
        });
    } 
}
function saveContactCategory(entity, $frm) {
    var jsonData;
    var uri = "/Services/DefaultService.svc/SetContactCategory";

    jsonData = serializeContactCategories(entity)

    $.ajax({
        type: "POST",
        url: uri,
        data: JSON.stringify(jsonData),
        
        dataType: "json",
        success: function(msg) {
        $('.sd-id-1' + getToeName(entity) + '302:input[type="text"]').val(msg.d);
        }
    });
    $frm.dialog('close');
}
function saveContactNote(entity) {
    var jsonData;
    var uri = "/Services/DefaultService.svc/SetContactNote";

    jsonData = serializeContactNote(entity)

    $.ajax({
        type: "POST",
        url: uri,
        data: JSON.stringify(jsonData)
    });
}
function saveEmail(entity){
    var jsonData;
    var uri = '/Services/DefaultService.svc/SetContactEmail';
    jsonData = serializeContactEmail(entity);
    $.ajax({
        type: "POST",
        url: uri,
        data: JSON.stringify(jsonData)
    });
}
function savePhone(entity) {
    var jsonData;
    var uri = "/Services/DefaultService.svc/SetContactPhone";

    jsonData = serializeContactPhone(entity);

    $.ajax({
        type: "POST",
        url: uri,
        data: JSON.stringify(jsonData)
    });
}
function saveAddress($frm, entity) {
    var jsonData, $smartForm;

    $smartForm = getSmartForm(entity);
    if ($smartForm != undefined) {
        var uri = "/Services/DefaultService.svc/SetContactAddresses";
        jsonData = serializeContactAddress($frm,entity);

        $.ajax({
            type: "POST",
            url: uri,
            data: JSON.stringify(jsonData)
        });

        var selected = $smartForm.data('mad'); var mailing = '';
        if (selected == 0) { mailing = jsonData.businessaddress.replace(/;/gi, '\n<br>'); }
        else if (selected == 1) { mailing = jsonData.homeaddress.replace(/;/gi, '\n<br>'); }
        else if (selected == 2) { mailing = jsonData.otheraddress.replace(/;/gi, '\n<br>'); }

        $smartForm.find('.sd-address').html(mailing);
    }
    $frm.dialog('close');
}
function saveContactName($frm, entity) {
    var jsonData = serializeContactName($frm,entity);

    var uri = "/Services/DefaultService.svc/SetContactName";

    $.ajax({
        url: uri,
        data: JSON.stringify(jsonData)
    });

    var toe = getToeName(entity);
    var $smartForm = getSmartForm(entity);
    $smartForm.dialog('option', 'title', jsonData.fileas);

    $smartForm.data('1' + toe + '250', jsonData.fullname);
    $smartForm.data('1' + toe + '251', jsonData.irstname);
    $smartForm.data('1' + toe + '252', jsonData.middlename);
    $smartForm.data('1' + toe + '253', jsonData.lastname);
    $smartForm.data('1' + toe + '254', jsonData.prefix);
    $smartForm.data('1' + toe + '255', jsonData.suffix);
    $frm.dialog('close');
    createListEvent(jsonData['contactserviceid'], null, jsonData.fileas.substr(0, 1).toUpperCase(),
    toe, jsonData.fileas, true, true);
    $smartForm.find('input.sd-id-1' + toe + '250:text').val(jsonData.fullname);
}
function saveLinkedEntity($frm, entity, cid, isParent, setParent) {
    var jsonData = serializeLinkedEntity($frm, entity, cid, isParent, setParent);

    var uri = "/Services/DefaultService.svc/SetLinkedEntity";

    $.ajax({
        url: uri,
        data: JSON.stringify(jsonData),
        success: function(msg) {
            var $smartForm = getSmartForm(entity);
            if (entity == 'contact') {
                $smartForm.find('.sd-id-11301:input:text').val(jsonSdString(msg.d.fullname));
            }
            else if (entity == 'account') {
                $smartForm.find('.sd-id-12246:input:text').val(jsonSdString(msg.d.fullname));
            }
            else
            {
                $smartForm.find('.sd-id-1' + getToeName(entity) + '246:input:text').val(jsonSdString(msg.d.fullname));
                $smartForm.find('.sd-id-11208:input:text').val(jsonSdString(msg.d.email));
                $smartForm.find('.sd-id-11276:input:text').val(jsonSdString(msg.d.mobile));
            }
            $frm.dialog('close');
        }
    });
}
function saveOwner($frm, entity, cid) {
    var $jsonData, $smartForm;
    $jsonData = {};
    $smartForm = getSmartForm(entity);
    $jsonData['cid'] = $smartForm.data('cid');
    $jsonData['oid'] = cid;
    $jsonData['entity'] = entity;
    $jsonData = jsonSerializeDefaults($jsonData);

    var uri = "/Services/DefaultService.svc/SetOwner";

    $.ajax({
        url: uri,
        data: JSON.stringify($jsonData),
        success: function(msg) {
            if (msg.d == null) return false;
            var $smartForm = getSmartForm(entity);
            $smartForm.find('.sd-id-1' + getToeName(entity) + '207:input:text').val(jsonSdString(msg.d.fullname));
            $frm.dialog('close');
        }
    });
}
function saveNewAppointment(start, minutes, $anyresource, $allresource, $frm, $options) {
    var jsonData = serializeNewAppointment(start, minutes, $anyresource, $allresource,$frm, $options);

    var uri = "/Services/DefaultService.svc/SetNewAppointment";

    $.ajax({
        type: "POST",
        url: uri,
        data: JSON.stringify(jsonData),
        
        dataType: "json",
        success: function(msg) {
        if (msg.d.length > 0) {

                $sd = $smartdiary.smartdiary({
                    editable: true,
                    eventClick: function(calEvent, $event) {
                        loadCalAppointment(calEvent, $event);
                    }
                });
                $smartdiary.smartdiary("removeEvent", msg.d[0].id);
                var $event;
                for (var i = 0; i < msg.d.length; i++) {
                    var ap = msg.d[i];
                    $event = { "id": ap.id, "start": ap.start, "end": ap.end, "title": ap.title, "resourceId": ap.resourceId, "bgColor": ap.bgColor, "bsColor": ap.bsColor };
                    $smartdiary.smartdiary("createEvent", $event);
                    createListEvent(ap.id, null, JsonToJavaDate(ap.start).toDateString(), 5, JsonToJavaDate(ap.start).toTimeString().substr(0, 5) + ' - ' + ap.title, true, true);
                }
            }        
        }
    });

}
function saveAppointment($frm) {
    var jsonData = serializeAppointment('appointment',$frm);

    var uri = "/Services/DefaultService.svc/SetAppointment";

    $.ajax({
        url: uri,
        data: JSON.stringify(jsonData),
        success: function(msg) {

            if (msg.d.length > 0) {

                $sd = $smartdiary.smartdiary({
                    editable: true,
                    eventClick: function(calEvent, $event) {
                        loadCalAppointment(calEvent, $event);
                    }
                });
                
                //$sd.smartdiary("removeEvent", msg.d[0].id);

//                var options = $smartdiary.smartdiary('options');
//                $smartdiary.find(".sd-cal-event").each(function() {
//                    if ($(this).data("calEvent").id === msg.d[0].id) {
//                        $(this).remove();
//                    }
//                });


                //var $smartdiary = $('#diaryPanel');
                //$smartdiary.smartdiary("loadCalendarWithData");
                $smartdiary.smartdiary("removeEvent", msg.d[0].id);

                var $event;
                for (var i = 0; i < msg.d.length; i++) {
                    var ap = msg.d[i];
                    $event = { "id": ap.id, "start": ap.start, "end": ap.end, "title": ap.title, "resourceId": ap.resourceId, "bgColor": ap.bgColor, "bsColor": ap.bsColor };
                    $smartdiary.smartdiary("createEvent", $event);
                    createListEvent(ap.id, null, JsonToJavaDate(ap.start).toDateString(), 5, JsonToJavaDate(ap.start).toTimeString().substr(0, 5) + ' - ' + ap.title, true, true);
                }
            }
            $frm.dialog('close');
        }
    });
}
function saveAddContactToAppointment(appointmentid, cid, $frm, isNew, start) {
    var jsonData = serializeAddContactToAppointment(appointmentid, cid);

    var uri = "/Services/DefaultService.svc/AddContactToAppointment";

    $.ajax({
        type: "POST",
        url: uri,
        data: JSON.stringify(jsonData),
        
        dataType: "json",
        success: function(msg) {
            if (isNew == true && msg.d < 1) {
                GetCategoryEvents(msg.d, appointmentid, cid, isNew, start)
            }
            if ($frm != undefined)
                $frm.dialog('close');
        }
    });
}



                 //  { "id": ap.id, "start": ap.start, "end": ap.end, "title": ap.title, "resourceId": ap.resourceid, "bgColor": ap.bgColor, "bsColor": ap.bsColor };

