﻿var jQueryImageRoot;

function Trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}
function LTrim(stringToTrim) {
    return stringToTrim.replace(/^\s+/, "");
}
function RTrim(stringToTrim) {
    return stringToTrim.replace(/\s+$/, "");
}

function SetupToolTipBehaviorForHelp(target, help) {
    $($(target)).bt($(help).html(),
        {
            trigger: 'click',
            fill: 'rgba(244,244,244,.8)',
            spikeLength: 20,
            cornerRadius: 10,
            strokeWidth: 1,
            shadow: true,
            shadowOffsetX: 3,
            shadowOffsetY: 3,
            shadowBlur: 8,
            shadowColor: 'rgba(0,0,0,.8)',
            shadowOverlap: false,
            noShadowOpts: { strokeStyle: '#999', strokeWidth: 2 },
            positions: ['left', 'top']
        });
}
//To Check To date greater than from date
function CheckFromDateGreaterThanToDate(from, to) {

    var FromDate = $(from).val();

    var ToDate = $(to).val();

    var format = "M/dd/yyyy";
    var Greater = compareDates(FromDate, format, ToDate, format);

    if (FromDate != "" && ToDate != "") {
        //If Greater=1 FromDate is greater ; If Greater=0 ToDate is greater ; If Greater=-1 Date Format error   
        return Greater != 0;
    }

    return false;
}

//To Check Valid date
function ValidateDate(target) {
    return isDate($(target).val(), 'M/dd/yyyy');
}

//To Check From date greater than Today's date
function CheckFromDateLEToday(target) {
    if (CheckDateAgainstToday(target)) {
        alert("From date should be less than or equal to Today's Date");
        $(target).val('');
        return false;
    }
}

//To Check To date greater than Today's date
function CheckToDateLEToday(target) {
    if (CheckDateAgainstToday(target)) {
        alert("To date should be less than or equal to Today's Date");
        $(target).val('');
        return false;
    }
}

//To Check date greater than Today's date
function CheckDateAgainstToday(target) {
    if (!$(target)[0])
        return;

    var FromDate = new Date($(target).val());
    var currentTime = new Date()
    return FromDate > currentTime;
}

//Setup datepicker for generic dates with button and text focus events
function SetupDatePickerForGenericCalendar() {
    $('.genericDatePicker').datepicker({
        changeMonth: true,
        changeYear: true,
        showOtherMonths: true,
        selectOtherMonths: true,
        showWeek: true,
        firstDay: 1,
        showOn: 'both',
        //showOn: 'focus',
        //showOn: 'button',
        buttonImage: '../Styles/jQuery/css/redmond/images/calendar.gif',
        buttonImageOnly: true,
        showButtonPanel: true
    });
}

//Setup the DATE picker formats and defaults
function SetDatePickerAlwaysFor(item, properties) {
    if ($(item).hasClass('hasDatepicker')) {
        return;
    }

    var defaults = {
        inline: false,
        clickInput: true,
        createButton: false,
        showAnim: "fadeIn",
        showOptions: { direction: 'up' },
        speed: "slow",
        showWeeks: true,
        dateFormat: 'mm/dd/yy',
        firstDay: 1,
        changeFirstDay: true,
        mandatory: true,
        closeAtTop: false,
        showStatus: true,
        defaultdate: null
    };
    $.extend(defaults, properties || {});

    $(item).each(function() {
        $(this).datepicker(defaults);
    });
}

$(document).ready(function() {
    var jQueryImageRoot = $('#GetjQueryImageRootUrl').val();
    SetupDatePickerForGenericCalendar();
})

//Minimize area when clicking   ---------------------------------------------

//Collapse and Expand any fieldset container.
//Only need to specify class name of "dynamic"
//Will collapse and expand on click for expand/collapse image as well as legend itself
//It also has a fading affect on the legend when the legend is clicked
//It also has a fading affect on the label when the image for collapse or expand is clicked
$(document).ready(function() {
    var $dynamicFieldset = $('fieldset.dynamic');
    if ($dynamicFieldset[0]) {
        $dynamicFieldset.addClass('maxFieldset')
        .find('legend')
        .hover(function() {
            $(this).addClass('hover');
        }, function() {
            $(this).removeClass('hover');
        });
        $dynamicFieldset.find('legend').css({ cursor: 'help' });

        $dynamicFieldset.find('legend')
        .toggle(
    function() {
        $(this).fadeOut('slow').fadeIn('slow'); //Fade the legend out and back in
        $(this).attr({ title: "Click to expand the content" });
        $(this).parent().removeClass('maxFieldset').addClass('minFieldset');
        $(this).parent().find('span.dynamicButton.collapse').hide();
        $(this).parent().find('span.dynamicButton.expand').show();
    },
    function() {
        $(this).fadeOut('slow').fadeIn('slow'); //Fade the legend out and back in
        $(this).attr({ title: "Click to collapse the content" });
        $(this).parent().removeClass('minFieldset').addClass('maxFieldset');
        $(this).parent().find('span.dynamicButton.expand').hide();
        $(this).parent().find('span.dynamicButton.collapse').show();
        $('fieldset.auditLogView', $(this).parent('fieldset.auditLog:first')).hide();

    });

    }
})//-----------------------------------------------------------------------------
