dennisshen7 / Change WO last update date

// ==UserScript==
// @name        Change WO last update date
// @namespace   --
// @include       http*://*Include/workorder.aspx?*updateWorkOrder*
// @include       http*://infotracker.csitech.com/Home.aspx*
// @include       http*://10.1.1.19/Home.aspx*
// @include       http*://*aspsoft/dispatcher.aspx?nextPID=listMy*
// @include       http*://*aspsoft/dispatcher.aspx?nextPID=listWorkOrderSearch*
// @grant none
// @version     1.8
// ==/UserScript==
//this.$ = this.jQuery = jQuery.noConflict(true);
var $ = unsafeWindow.jQuery;

function dateRebuild(inputD) {
    return inputD.getFullYear() + '/' + ('0' + (inputD.getMonth() + 1)).slice(-2) + '/' + ('0' + inputD.getDate()).slice(-2) + ' ' +
        (inputD.getHours() % 12 > 0 ? ('0' + (inputD.getHours() % 12)).slice(-2) : 12) + ':' +
        (inputD.getMinutes() >= 10 ? inputD.getMinutes() : '0' + inputD.getMinutes()) +
        (inputD.getHours() >= 12 ? ' PM ' : ' AM ')
}


Date.prototype.stdTimezoneOffset = function() {
    var jan = new Date(this.getFullYear(), 0, 1);
    var jul = new Date(this.getFullYear(), 6, 1);
    return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
}

Date.prototype.dst = function() {
    return this.getTimezoneOffset() < this.stdTimezoneOffset();
}

function isWinterDay(inputD) {
    var milliSeconds = 0;
    /*
    var offSet1 = new Date(inputD.getFullYear(), (inputD.getMonth() + 1), inputD.getDate(), 24, 0, 0, milliSeconds).getTimezoneOffset();
    var offSet2 = new Date(inputD.getFullYear(), (inputD.getMonth() + 1), inputD.getDate(), 24, 0, 0, milliSeconds - 1).getTimezoneOffset(); // Check the offset 1 ms before
    */
    var offSet1 = new Date(inputD.getFullYear(),0,1).getTimezoneOffset();
    var offSet2 = new Date(inputD.getFullYear(),6,1).getTimezoneOffset();
    var max = Math.max(offSet1, offSet2);


    return (inputD.dst() ? 12 : 13);
}


$(document).ready(function() {
    
    if (window.location.href.indexOf('/aspsoft/Dispatcher.aspx?nextPID=listWorkOrderSearch&staff_ids=')>-1){
        var order = $('img[async_grid_sort=last_update_datetime]').attr('async_sort_dir');
        if(order != 'DESC'){
            __doPostBack('ctl00$ContentPlaceHolder1$ctl00$gridview','Sort$last_update_datetime');
            return;
        }
    }
            
    //addTransUploadDetail
    $('body').on('mousemove','iframe',function(){
        $(this).contents().find("body").on('mouseenter', 'table[objectname=addTransUploadDetail]',function(event) {
           var item_log_list=$(this).find('tr[repeat=true]');
           item_log_list.each(function(){
               $(this).find('input[name$=sel30][name^=ctrl]:checkbox').remove();
               if($(this).find('input[name=sel30]:checkbox').length==0){
                     var intp = $('<input name=sel30 type=checkbox>');
                      intp.on('click',function(){
                       var isCli = $(this).prop('checked');
                       
                       var tar = $(this).closest('span').find('table span').filter(function(){
                           return $(this).css('color') == 'rgb(255, 0, 0)';
                       });
                         
                       tar.prev('input:checkbox').prop('checked',isCli);
                       
                     });
                     intp.insertAfter($(this).find('font[color=blue]'));
               }
           })
       });
    });
    
    /*
    $('iframe').load(function(){
       $(this).contents().find("body").on('mousemove', function(event) {
           var item_log_list=$(this).find('tr[repeat=true]');
           console.log(item_log_list);
       });
    });
    */
    
    //'table[objectname=addTransUploadDetail]'
    if (window.location.href.indexOf('Include/WorkOrder.aspx?nextPID=updateWorkOrder&')>-1){
       
    }
    
    $(document).off("click", "img[src*='image_id']");
    $("div[id^=Inbox]").bind("ajaxComplete", function(e, xhr, settings) {
        $(this).find('table[objectname=listInbox_MyAllWorkOrder] td>span[objectname=priority]').each(function() {
            if ($(this).text().indexOf('Very Low') > -1) {
                $(this).closest('td').css({
                    'background-color': '#00CCFF'
                });
            }
        });

        $('div#divWOStatusContent span[style="background-color:#F7F709"]').each(function() {
            $(this).css({
                'background-color': ''
            });
        });

    });

    var $listMyRecentWO = $('table[objectname=listMyRecentWO] tbody>tr, table[objectname=listMyViewedWO] tbody>tr, table[objectname=listWorkOrderSearch] tbody>tr');
    
    $listMyRecentWO.hover(
        function() {
            //$(this).attr('ori_color',$(this).css('background-color'));
            $(this).find('td').each(function(index, obj) {
                $(obj).attr('ori_color', $(obj).css('background-color'));
                $(obj).css('background-color', '#DFEFFC');
            });
            //$(this).css('background-color','yellow');
        },
        function() {
            //$(this).css('background-color',$(this).attr('ori_color'));
            $(this).find('td').each(function(index, obj) {
                $(obj).css('background-color', $(obj).attr('ori_color'));
            });
        }
    );
    
    
    var $pri_idx_obj = $listMyRecentWO.parents('table').next().find('table>thead>tr>th');
    var pri_idx=0
    
    $pri_idx_obj.each(function(index,element){
        if($(this).find('a').text().indexOf('Priority') > -1){
            pri_idx=index+1;
        }
    });
    
    
    var dateCollect= {};
    
    $listMyRecentWO.each(function(index) {
        if ($(this).find('td:nth-child('+pri_idx+')').text().indexOf('Very Low') > -1){
            $(this).find('td:nth-child('+pri_idx+')').css({
                    'background-color': '#00CCFF'
                });
        }
        
        var lDate = new Date($(this).find('td:eq(-2)>span').text());
        if(isNaN( lDate.getTime() )){
            lDate = new Date($(this).find('td:eq(-3)>span').text());
        }
        
        var today = new Date();
        var addHr = 0;

        addHr = isWinterDay(lDate);
        
        
        if (!isNaN( lDate.getTime() )) {
            lDate.setHours(lDate.getHours() + addHr);

            var lDateStd = new Date(lDate.getFullYear(), lDate.getMonth(), lDate.getDate(), 0, 0, 0, 0);
            var todayStd = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 0, 0, 0, 0);

            var day_window = 1;

            if (todayStd.getDay() === 1) {
                day_window = 3;
            }

            var dateDiff = parseInt(Math.abs((todayStd.getTime() - lDateStd.getTime()) / (24 * 60 * 60 * 1000)));
            
            
            var dateText = dateRebuild(lDate);
            
            
            if(Object.keys(dateCollect).length<3){
               dateCollect[dateText.substring(0,11)]=1
            }
                        
            
            if(Object.keys(dateCollect).length===1){
                $(this).closest('tr').find('td:eq(0),td:eq(1)').css('background-color', '#00CCFF');
            }
            else if (Object.keys(dateCollect).length===2){
                $(this).closest('tr').find('td:eq(0),td:eq(1)').css('background-color', '#66FF66');
            }
                
            
            /*
            if (dateDiff === 0) {
                $(this).closest('tr').find('td:nth-child(1),td:nth-child(2)').css('background-color', '#00CCFF');
            }

            if (dateDiff <= day_window && dateDiff>0) {
                $(this).closest('tr').find('td:nth-child(1),td:nth-child(2)').css('background-color', '#66FF66');
            }
            */
            $(this).find('td:eq(-2)>span').text(dateText);

        }
    });

    
    $("table#commentlist").bind("ajaxComplete", function(e, xhr, settings) {
        $('td.pst').each(function(index) {

            if ($(this).text().indexOf(' AM ') == -1 && $(this).text().indexOf(' PM ') == -1) {

                var idx = $(this).text().indexOf('/');

                var post_date = new Date($(this).text().substr(idx - 2, 19));
                var addHr = 0;

                addHr = isWinterDay(post_date);

                post_date.setHours(post_date.getHours() + addHr);

                $(this).text($(this).text().substr(0, idx - 2) + dateRebuild(post_date) + $(this).text().substr(idx + 17, 100));

            }
        });

        $('div[xcmd="Maximize"]').unbind('click');
        $('div[xcmd="Maximize"]').on('click', function() {
            $(this).toggleClass('bigger');

            if ($(this).hasClass('bigger')) {
                $('iframe#txtCommentRich').css('height', '650px');
            } else {
                $('iframe#txtCommentRich').css('height', '200px');
            }
        });
        
       $('img[src*="HandlerDocument.ashx?image_id"]:not([src*="thumbnail_image_id"])', e.target).each(function(){
            var url = $(this).attr('src');
            
            var image_id_reg = /\d{2,}/; //new RegExp("(\d{2,})");
            
            var image_id = url.match(image_id_reg);

            url = url.split('?')[0]+'?thumbnail_image_id='+image_id+'&show_original_image=1'; //url.replace('?image_id','?thumbnail_image_id')+'&show_original_image=1';
           
           $(this).die('click');
           
           $(this).attr('src',url);
           $(this).on('click',function(event){
              event.preventDefault();
              window.open(url);
           });
           
            /*
            $(this).load(function(){
                $('<img src="../Images/indicator.gif">').insertAfter($(this));
            });
            $(this).attr('src',url);
            $(this).unbind('load');
            if(this.complete){
                $(this).next('img[src*=".gif"]').remove();
            }
            */
        });
        
        //$('img[src*="HandlerDocument.ashx?thumbnail_image_id="])', e.target).
       
    });

});