//==============================================
certificate.xfer = {
    records: [],
    research: function(orderid) {
        certificate.v.theOrderId = orderid;
        $('body').append(certificate.v.loader);
        $('body').append('<form id="research"><input type="hidden" id="researchid" name="researchid"  value="'+orderid+'"></form>')
        $.post("xferAudit.php", $("#research").serialize())
            .done(function(data) {
                console.log(data);
                certificate.xfer.researchResults(data);
                $('#loading').remove();
                $('body').remove('#research');
            });
        //certificate.xfer.research(6382);
    }, //==============================================
    researchResults: function(data) {
        // try {
            console.log(  certificate.v.researchData  );
            $('#loading').remove();
            var orderid = certificate.v.theOrderId;
            certificate.v.researchData = JSON.parse(data);
            $('#selectsomething').remove();
            
            $('body').append('<div id="selectsomething"><span class="close" onClick="$(\'#selectsomething\').remove();">X</span><div id="selectsomethinginner"><span id="selectheader">'+ orderid +'</span><hr /><div class="items"></div></div></div>');

            for (var i = 0; i < certificate.v.researchData.length; i++) {
                if (certificate.v.researchData[i].used === '1' || certificate.v.researchData[i].used === 1) {
                    var used = certificate.v.researchData[i].create_date;
                } else {
                    var used = 'Not Used';
                }
                if (certificate.xfer.isParent(certificate.v.researchData[i].guid)) {
                    if (certificate.v.researchData[i].transferid === null) {
                        $('#selectsomething .items').append('<div class="item" >\
                            <div>Certificate: '+certificate.v.researchData[i].label+'</div>\
                            <div>Created on: '+certificate.v.researchData[i].cert_date+'</div>\
                            <div>Original Owner: <span style="font-weight: bolder;">'+certificate.v.researchData[i].first+' '+certificate.v.researchData[i].last+'</span></div>\
                            <div>Used on: '+used+'</div>\
                            </div>');
                    } else {
                        $('#selectsomething .items').append('<div class="item" >\
                            <div>Certificate: '+certificate.v.researchData[i].label+'</div>\
                            <div>Created on: '+certificate.v.researchData[i].cert_date+'</div>\
                            <div>Original Owner: <span style="font-weight: bolder;">'+certificate.v.researchData[i].first+' '+certificate.v.researchData[i].last+'</span></div>\
                            <div id="transferArea_'+i+'"></div>\
                            </div>');
                        $('#transferArea_'+i).html(certificate.xfer.transferData(certificate.v.researchData[i].transferid,certificate.v.researchData[i].cert_date));
                    }
                }
            }
            $('.item').css('text-align','left');
            $('#selectsomething').append('<div class="button" onclick="$(\'#selectsomething\').draggable({  cursorAt: { top: -5, left: -5 } });$(this).remove();">double click to move this box</div>');
            

        // } catch (err) {
        //     alert('Error');
        //     console.log('Error');
        // }
        
        //certificate.xfer.researchResults(data);
    }, //==============================================
    transferData: function(guid,xDate) {
        var result = '';
        for (var i = 0; i < certificate.v.researchData.length; i++) {
            if (certificate.v.researchData[i]['guid'] === (guid)) {


                if (certificate.v.researchData[i].used === '1' || certificate.v.researchData[i].used === 1) {
                    var used = certificate.v.researchData[i].create_date;
                } else {
                    var used = 'Not Used';
                }

                result += '<div>Transferred on: '+xDate+'</div>';
                result += '<div>Transferred to: <span style="font-weight: bolder;">'+certificate.v.researchData[i].first+' '+certificate.v.researchData[i].last+'</span></div>';
                if (certificate.v.researchData[i].transferid === null) {
                    result += '<div>Used on: '+used+'</div>';
                } else {
                    result += certificate.xfer.transferData(certificate.v.researchData[i].transferid,certificate.v.researchData[i].cert_date);
                }
                
            }
        }
        return result;
        //certificate.xfer.transferData(guid);
    }, //==============================================
    isParent: function(guid) {
        var result = true;
        for (var i = 0; i < certificate.v.researchData.length; i++) {
            if (certificate.v.researchData[i]['transferid'] === (guid)) {
                var result = false;
            }
        }
        return result;
        //certificate.xfer.isParent(guid);
    }, //==============================================
    nullFunction: function() {
        //certificate.xfer.log('null');
    } //==============================================
}