﻿function mapidArray(entity, table) {
    var mapids = [];
    if (table == 'selector')
        mapids = ['ph1', 'ph2', 'ph3', 'mad', 'em1', 'typ', 'cid', 'aid', 'entity'];
    if (entity == 'contact') {
        if (table == 'contactmain') {
            mapids = [11222, 11223, 11230, 11248, 11227, 11235, 11237, 11238, 11236, 11224, 11225, 11226, 11229, 11228, 11233, 11231, 11232, 11234, 11239, 11240, 11241];
        }
        else if (table == 'contactname') {
            mapids = [11250, 11251, 11252, 11253, 11254, 11255, 11256]; // full,first,middle,last,prefix,suffix,fileas
        }
        else if (table == 'address') {
            mapids = [11201, 11201, 11201, 11202, 11203, 11205, 11204, 11214, 11214, 11214, 11215, 11216, 11218, 11217, 11258, 11258, 11258, 11259, 11260, 11262, 11261];
        }
        else if (table == 'phone') {
            mapids = [11265, 11266, 11267, 11268, 11269, 11270, 11271, 11272, 11273, 11274, 11275, 11276, 11277, 11278, 11279, 11280, 11281, 11282, 11283];
        }
        else if (table == 'email') {
            mapids = [11208, 11210, 11212];
        }
    }
    else if (entity == 'account') {
        if (table == 'contactmain') {
            mapids = [12248, 12247, 12230, 12227, 12233, 12231, 12232, 12234, 12224, 12225, 12226, 12229, 12228, 12239, 12240, 12241];
        }
        else if (table == 'contactname') {
            mapids = [12250, 12251, 12252, 12253, 12254, 12255, 12256]; // full,first,middle,last,prefix,suffix,fileas
        }
        else if (table == 'address') {
            mapids = [12201, 12201, 12201, 12202, 12203, 12205, 12204, 12214, 12214, 12214, 12215, 12216, 12218, 12217, 12258, 12258, 12258, 12259, 12260, 12262, 12261];
        }
        else if (table == 'phone') {
            mapids = [12265, 12266, 12267, 12268, 12269, 12270, 12271, 12272, 12273, 12274, 12275, 12276, 12277, 12278, 12279, 12280, 12281, 12282, 12283];
        }
        else if (table == 'email') {
            mapids = [12208, 12210, 12212];
        }
    }
    else if (entity == 'opportunity') {
        if (table == 'opportunitymain') {
            mapids = [13201, 13225, 13248, 13247, 13203, 13202, 13229, 13226, 13224, 13227, 13252, 13241, 13206];
        }
    }
    else if (entity == 'appointment') {
        if (table == 'appointment') {
            mapids = [15217, 15223, 15215, 15216, 15208, 15209, 15201, 15202, 15224, 15222, 15261];
        }
    }
    else if (entity == 'support') {
        if (table == 'supportmain') {
            mapids = [17201, 17225, 17248, 17226, 17230, 17202, 17205, 17203, 17204, 17224, 17247, 17229, 17241, 17206];
        }
    }
    return mapids;
}
function smartListService(entity) {
    if (entity == 'contact' || entity == 1)
        return "/Services/DefaultService.svc/ContactiSmartList";
    else if (entity == 'account' || entity == 2)
        return "/Services/DefaultService.svc/AccountiSmartList";
    else if (entity == 'opportunity' || entity == 3)
        return "/Services/DefaultService.svc/OpportunityiSmartList";
    else if (entity == 'support' || entity == 4)
        return "/Services/DefaultService.svc/SupportiSmartList";
    else if (entity == 'appointment' || entity == 5)
        return "/Services/DefaultService.svc/AppointmentiSmartList";
    else if (entity == 'task' || entity == 6)
        return "/Services/DefaultService.svc/TaskiSmartList";
    else if (entity == 'support' || entity == 7)
        return "/Services/DefaultService.svc/SupportiSmartList";
}
function entityPanel() {

}
function jsonSerializeDefaults(jsonData)
{
jsonData['smartid']= $('#usercred').data('sid');
jsonData['modifiedby'] = $('#usercred').data('uan');
jsonData['modifiedid'] = $('#usercred').data('uid');

return jsonData;
}

function getSmartForm(entity) {
    var $smartForm;
    
    if (entity == 1 || entity == 'contact')
        $smartForm = $('body').find('.sd-dlg-contact');
    else if (entity == 2 || entity == 'account')
        $smartForm = $('body').find('.sd-dlg-account');
    else if (entity == 3 || entity == 'opportunity')
        $smartForm = $('body').find('.sd-dlg-opportunity');

    else if (entity == 5 || entity == 'appointment')
        $smartForm = $('body').find('.sd-dlg-appointment');
    else if (entity == 7 || entity == 'support')
        $smartForm = $('body').find('.sd-dlg-support');

    return $smartForm;
}

function serializeContactAddress($frm,entity) {
    var $jsonData, $addressForm, mapids, mapidBools, mapidDate, $body, $smartForm;
    $body = $('body');
    $jsonData = {};
    mapids = mapidArray(entity, 'address');

    $smartForm = getSmartForm(entity);

    if ($frm != undefined) {
        for (var i = 0; i < mapids.length; i++) {
    var field,value,addrArray;
            var map = mapids[i].toString();
            if ($body.data(map).json == 'string') {
                var fld = $body.data(map).field;
                
                var addrType = '';
                if (fld.charAt(0) == 'b') addrType = 'business';
                else if (fld.charAt(0) == 'h') addrType = 'home';
                else addrType = 'other';

                addrArray = $smartForm.data(addrType);
                if (addrArray == undefined) {
                    $smartForm.data(addrType, new Array());
                    addrArray = $smartForm.data(addrType);
                }
                if (fld == 'businessstreet' || fld == 'homestreet' || fld == 'otherstreet') {
                    value = '';
                    for (var j = 1; j < 4; j++) {
                        var val = $frm.find('.sd-id-' + map + '_' + j + ':input[type="text"]').val();
                        if (val.length > 0)
                            value += val + ';';

                        addrArray = $smartForm.data(addrType);
                        addrArray['street' + (j).toString()] = val;
                        $smartForm.data(addrType, addrArray);
                    }
                    while (value.charAt(value.length - 1) == ';'){
                        value = value.substr(0, value.length - 1);
                    }
                    field = $body.data(map).field;
                    $jsonData[field] = value;
                }
                else {
                    field = $body.data(map).field;
                    value = $frm.find('.sd-id-' + map + ':input[type="text"]').val();
                    $jsonData[field] = value;

                    addrArray = $smartForm.data(addrType);
                    addrArray[addrType.substr(addrType.length)] = value;
                    $smartForm.data(addrType, addrArray);
                }
            }
        }
        var businessaddress = '';
        businessaddress += ($jsonData.businessstreet.length > 0?$jsonData.businessstreet+';':'');
        businessaddress += ($jsonData.businesscity.length > 0 ? $jsonData.businesscity + ';' : '');
        businessaddress += ($jsonData.businessregion.length > 0 ? $jsonData.businessregion + ';' : '');
        businessaddress += ($jsonData.businesspostcode.length > 0 ? $jsonData.businesspostcode + ';' : '');
        businessaddress += ($jsonData.businesscountry.length > 0 ? $jsonData.businesscountry + ';' : '');
        while (businessaddress.charAt(businessaddress.length - 1) == ';') { businessaddress = businessaddress.substr(0, businessaddress.length - 1); }

        $jsonData['businessaddress'] = businessaddress;

        var homeaddress = '';
        homeaddress += ($jsonData.homestreet.length > 0 ? $jsonData.homestreet + ';' : '');
        homeaddress += ($jsonData.homecity.length > 0 ? $jsonData.homecity + ';' : '');
        homeaddress += ($jsonData.homeregion.length > 0 ? $jsonData.homeregion + ';' : '');
        homeaddress += ($jsonData.homepostcode.length > 0 ? $jsonData.homepostcode + ';' : '');
        homeaddress += ($jsonData.homecountry.length > 0 ? $jsonData.homecountry + ';' : '');
        while (homeaddress.charAt(homeaddress.length - 1) == ';') { homeaddress = homeaddress.substr(0, homeaddress.length - 1); }

        $jsonData['homeaddress'] = homeaddress;
        
        var otheraddress = '';
        otheraddress += ($jsonData.otherstreet.length > 0 ? $jsonData.otherstreet + ';' : '');
        otheraddress += ($jsonData.othercity.length > 0 ? $jsonData.othercity + ';' : '');
        otheraddress += ($jsonData.otherregion.length > 0 ? $jsonData.otherregion + ';' : '');
        otheraddress += ($jsonData.otherpostcode.length > 0 ? $jsonData.otherpostcode + ';' : '');
        otheraddress += ($jsonData.othercountry.length > 0 ? $jsonData.othercountry + ';' : '');
        while (otheraddress.charAt(otheraddress.length - 1) == ';') { otheraddress = otheraddress.substr(0, otheraddress.length - 1); }

        $jsonData['otheraddress'] = otheraddress;

        $jsonData['contactserviceid'] = $smartForm.data('cid');

        $jsonData = jsonSerializeDefaults($jsonData);
    }
    return $jsonData;
}

function serializeContactMain(entity) {
    var $jsonData, $smartForm, mapids, mapidBools, mapidDate, $body;

    $body = $('body');

    $jsonData = {};

    $smartForm = getSmartForm(entity);
    if (entity == 'contact' || entity == 'account')
        mapids = mapidArray(entity, 'contactmain');
    else if (entity == 'opportunity')
        mapids = mapidArray(entity, 'opportunitymain');
    else if (entity == 'support')
        mapids = mapidArray(entity, 'supportmain');

    if ($smartForm != undefined) {
        for (var i = 0; i < mapids.length; i++) {
        var field,value;
            var map = mapids[i].toString();

            if ($body.data(map).json == 'datetime') {
                field = $body.data(map).field;
                value = $smartForm.find('.sd-id-' + map + '.dtp:input[type="text"]').val();
                var tpk = $smartForm.find('.sd-id-' + map + '.tpk:input[type="text"]');
                if (tpk.length > 0)
                    value += ' '+tpk.val()+':00';
                $jsonData[field] = value;
            }
            else if ($body.data(map).json == 'string') {
                field = $body.data(map).field;
                value = $smartForm.find('.sd-id-' + map + ':input[type="text"]').val();
                $jsonData[field] = value;
            }
            else if ($body.data(map).json == 'guid') {
                field = $body.data(map).field;
                value = $smartForm.find('select.sd-id-' + map).val();
                $jsonData[field] = value;
            }
            else if ($body.data(map).json == 'byte') {
                field = $body.data(map).field;
                value = $smartForm.find('select.sd-id-' + map).val();
                $jsonData[field] = value;
            }
            else if ($body.data(map).json == 'bool') {
            field = $body.data(map).field;
            value = $smartForm.find('.sd-id-' + map + ':input[type="checkbox"]').attr('checked');
            $jsonData[field] = value;
            }
            else if ($body.data(map).json == 'short') {
            field = $body.data(map).field;
            var $slider = $smartForm.find('.sd-id-' + map + '.sd-slider');
            value = $slider.slider('option', 'value');
            $jsonData[field] = value;
            }
        }
    }

    $jsonData['contactserviceid'] = $smartForm.data('cid');

    $jsonData['type'] = getToeName(entity);

    $jsonData = jsonSerializeDefaults($jsonData);
    
    return $jsonData;
}

function serializeContactName($frm, entity) {
    var $jsonData, $smartForm, mapids, mapidBools, mapidDate, $body;

    $body = $('body');

    $jsonData = {};

    $smartForm = getSmartForm(entity);

    mapids = mapidArray(entity, 'contactname');

    if ($frm != undefined) {
        for (var i = 0; i < mapids.length; i++) {
            var map = mapids[i].toString();

            if ($body.data(map).json == 'string') {
                var field = $body.data(map).field;
                var value = $frm.find('.sd-id-' + map + ':input[type="text"]').val();
                if (value == undefined)
                    value = $frm.find('select.sd-id-' + map).val();
                $jsonData[field] = value;
                $smartForm.data(map, value);
            }
        }
    }

    $jsonData['contactserviceid'] = $smartForm.data('cid');

    $jsonData = jsonSerializeDefaults($jsonData);

    if (entity == 'contact') {
        var firstname = $frm.find('.sd-id-11251:input').val();
        var middle = $frm.find('.sd-id-11252:input').val();
        var last = $frm.find('.sd-id-11253:input').val();
        var prefix = $frm.find('select.sd-id-11254').val();
        var suffix = $frm.find('.sd-id-11255:input').val();
        var fullname = prefix + (prefix.length > 0 ? ' ' : '');
        fullname += firstname + (firstname.length > 0 ? ' ' : '');
        fullname += middle + (middle.length > 0 ? ' ' : '');
        fullname += last + (last.length > 0 ? ' ' : '');
        fullname += suffix + (suffix.length > 0 ? ' ' : '');

        var fileas = last.length > 0 ? last + ' ,' : '';
        fileas += firstname;
        if (fileas.charAt(fileas.length - 1) == ',') { fileas = fileas.slice(0, fileas.length - 1); }

        $jsonData['fullname'] = fullname;
        $jsonData['fileas'] = fileas;

        $smartForm.data('11250', fullname);
        $smartForm.data('11256', fileas);
    }
    else {
        $jsonData['fileas'] = $jsonData['fullname'];
        $smartForm.data('12250', fullname);
        $smartForm.data('12256', fileas);
    }

    return $jsonData;
}
function serializeContactEmail(entity) {
    var $jsonData, $body, $smartForm;
    $smartForm = getSmartForm(entity);
    $body = $('body');
    $jsonData = {};

    mapids = mapidArray(entity, 'email');

    if ($smartForm != undefined) {
        for (var i = 0; i < mapids.length; i++) {
            var map = mapids[i].toString();

            var field = $body.data(map).field;
            var value = $smartForm.data(map);
            $jsonData[field] = value;
        }

        $jsonData['cid'] = $smartForm.data('cid')

        $jsonData = jsonSerializeDefaults($jsonData);
    }
    return $jsonData;
}
function serializeContactPhone(entity) {
    var $jsonData, $body, $smartForm;
    $smartForm = getSmartForm(entity);
    $body = $('body');
    $jsonData = {};

    mapids = mapidArray(entity, 'phone');

    if ($smartForm != undefined) {
        for (var i = 0; i < mapids.length; i++) {
            var map = mapids[i].toString();

            var field = $body.data(map).field;
            var value = $smartForm.data(map);
            $jsonData[field] = value;
        }

        $jsonData['contactserviceid'] = $smartForm.data('cid')

        $jsonData = jsonSerializeDefaults($jsonData);
    }
    return $jsonData;
}
function serializeContactCategories(entity) {
    var $categoryDialog, $jsonData, $smartForm;

    $categoryDialog = $('#categoryDialog');
    var value = '';
    $jsonData = {};

    $smartForm = getSmartForm(entity);

    if ($smartForm != undefined) {
        $categoryDialog.find('input[type="checkbox"]').each(function() {

            var val = $(this).attr('checked');

            if (val == true) {
                var c = $(this).attr('class').split(' ');

                for (var i = 0; i < c.length; i++) {
                    if (c[i].match('^sd-cat-')) {
                        value += (c[i]).replace('sd-cat-', '') + ';';
                    }
                }
            }
        });
        while (value.charAt(value.length - 1) == ';') {
            value = value.substr(0, value.length - 1);
        }
        $jsonData['categories'] = value;

        $jsonData['contactserviceid'] = $smartForm.data('cid')
        $jsonData = jsonSerializeDefaults($jsonData);
    }
    return $jsonData;
}
function serializeContactNote(entity) {
    var $jsonData, $smartForm;

    $jsonData = {};

    $smartForm = getSmartForm(entity);

    if ($smartForm != undefined) {
        $jsonData['note'] = $smartForm.find('.sd-textarea-comments').val();

        $jsonData['contactserviceid'] = $smartForm.data('cid')
        $jsonData = jsonSerializeDefaults($jsonData);
    }
    return $jsonData;
}
function serializeLinkedEntity($frm, entity, cid, isParent, setParent) {
    var $jsonData, $smartForm;
    $jsonData = {};

    $smartForm = getSmartForm(entity);

    if (isParent == true) {
        $jsonData['parentcontactserviceid'] = $smartForm.data('cid');
        $jsonData['contactserviceid'] = cid;
    }
    else {
        $jsonData['parentcontactserviceid'] = cid;
        $jsonData['contactserviceid'] = $smartForm.data('cid');
    }
    $jsonData['setparent'] = setParent;
    $jsonData['isparent'] = isParent;
    $jsonData = jsonSerializeDefaults($jsonData);

    return $jsonData;
}
function serializeAppointment(entity, $frm) {
    var $jsonData, $smartForm, mapids;

    $body = $('body');

    $jsonData = {};

    $smartForm = getSmartForm(entity);

    mapids = mapidArray(entity, 'appointment');

    var startDate;
    var startTime;
    var endDate;
    var endTime;
    var resourceids;
    
    if ($smartForm != undefined) {
        for (var i = 0; i < mapids.length; i++) {
        var field,value;
            var map = mapids[i].toString();

            var dt = $body.data(map).json;

            if (dt == 'datetime') {
                field = $body.data(map).field;
                var dtp = $smartForm.find('.sd-id-' + map + ':input[type="text"]');
                if (dtp.hasClass('dtp')) {
                    if (startDate == undefined) {
                        startDate = dtp.val();
                    }
                    else {
                        endDate = dtp.val();
                    }
                }
                else if (dtp.hasClass('tpk')) {
                    if (startTime == undefined) {
                        startTime = dtp.val();
                    }
                    else {
                        endTime = dtp.val();
                    }
                }
                
                //var value = $smartForm.find('.sd-id-' + map + ':input[type="text"]').val();
                //$jsonData[field] = value;
            }
            else if (dt == 'string') {
            field = $body.data(map).field;
//            var value;
//            if (map == 15223) {
//                value = '';
//                var $resource = $smartForm.find('select.sd-id-' + map);
//                $resource.find('option').each(function() {
//                    var sel = $(this).attr('selected');
//                    if (sel == true) {
//                        value += $(this).val()+';';
//                    }
//                });
//            }
//            else {
            value = $smartForm.find('.sd-id-' + map + ':input[type="text"]').val();
            if (value == undefined)
                value = $smartForm.find('textarea.sd-id-' + map).val();
            //}
                $jsonData[field] = value;
            }
            else if (dt == 'int') {
                field = $body.data(map).field;
                value = $smartForm.find('select.sd-id-' + map).val();
                $jsonData[field] = value;
            }
            else if (dt == 'guid') {
                field = $body.data(map).field;
                value = $smartForm.find('select.sd-id-' + map).val();
               $jsonData[field] = value;
            }
            else if (dt == 'bool') {
                field = $body.data(map).field;
                value = $smartForm.find('.sd-id-' + map + ':input[type="checkbox"]').attr('checked');
                $jsonData[field] = value;
            }
        }
    }
    //$jsonData['location'] = $smartForm.find('select.sd-id-15224').val();
    //$jsonData['notes'] = $smartForm.find('textarea.sd-id-15261').val();
    $jsonData['start'] = startDate + ' ' + startTime;
    $jsonData['end'] = endDate + ' ' + endTime;
    $jsonData['resource'] = $frm.data('selectedResource').join(';');

    $jsonData['appointmentid'] = $smartForm.data('aid');

    $jsonData = jsonSerializeDefaults($jsonData);
    return $jsonData;
}
function serializeNewAppointment(start, minutes, $anyresource, $allresource, $frm, $options) {
    var $jsonData, $frmData;

    $jsonData = {};
    
    $jsonData['start'] = start;
    $jsonData['duration'] = minutes;
    $jsonData['anyresource'] = $anyresource.join(';');
    $jsonData['allresource'] = $allresource.join(';');
    $jsonData = jsonSerializeDefaults($jsonData);
    $jsonData['appointmentid'] = $options.appointmentid;
    $jsonData['contactserviceid'] = $options.cid;
    $jsonData['category'] = $frm.data('category');
    $jsonData['busystate'] = $frm.data('busystate');
    $jsonData['subject'] = $frm.data('subject');

    return $jsonData;
}
function serializeTimespanIsAvailable(start, minutes, anyresource, allresource) {
    var $jsonData = {};
    $jsonData['searchstart'] = start;
    $jsonData['smartid'] = $('#usercred').data('sid');
    $jsonData['durationminutes'] = minutes;
    $jsonData['anyresource'] = anyresource.join(';');
    $jsonData['allresource'] = allresource.join(';');

    return $jsonData;
}
function serializeFindAppointmentByBusy(start, busy, cid) {
    var $jsonData = {};
    $jsonData['searchstart'] = start;
    $jsonData['busystate'] = busy;
    $jsonData['contactserviceid'] = cid;
    $jsonData['smartid'] = $('#usercred').data('sid');
    return $jsonData;
}
function serializeAddContactToAppointment(appointmentid, cid) {
    var $jsonData = {};
    $jsonData['contactserviceid'] = cid;
    $jsonData['appointmentid'] = appointmentid;
    $jsonData = jsonSerializeDefaults($jsonData);
    return $jsonData;
}
