var dontGrowlErrorsForUrls = [];

function blockDialog(dialogId) {
    block("[aria-labelledby='ui-dialog-title-" + dialogId + "']");
}

function unblockDialog(dialogId){
    unblock("[aria-labelledby='ui-dialog-title-" + dialogId + "']");
}

function block(id, hideimage) {
    var message = '<img src="/resources/img/ajax-loader.gif" />';
    if (hideimage) {
        message = '';
    }
    if ($(".blockUI", $(id)).length === 0) {
        $(id).block({ message: message,
            css: {
                padding: 0,
                margin: 'auto auto',
                width: '100%',
                textAlign: 'center',
                color: '#fff',
                border: 'none',
                backgroundColor: 'Transparent',
                cursor: 'wait',
                zIndex: 999999999
            },
            // styles for the overlay
            overlayCSS: {
                backgroundColor: '#fff',
                opacity: '0.777',
                zIndex: 999999999
            }
        });
    }

    return true;
}

function unblock(id) {
    if (id == null) {
        $.unblockUI();
    }
    else {
        $(id).unblock();
    }
}

var growlAnimateInProgress = false;
function growlAfterOpen(element, message, options) {
    if (growlAnimateInProgress)
    { return; }
    growlAnimateInProgress = true;
//    var heightValues = [40, 20, 10, 5];
//    $.each(heightValues, function() {
//        $(element).animate({ top: '-=' + this + 'px' }, 175).animate({ top: '+=' + this + 'px' }, 120);
//    });
    $(element).animate({ top: '-=' + 40 + 'px' }, 175).animate({ top: '+=' + 40 + 'px' }, 120).animate({ top: '-=' + 20 + 'px' }, 175).animate({ top: '+=' + 20 + 'px' }, 120).animate({ top: '-=' + 10 + 'px' }, 175).animate({ top: '+=' + 10 + 'px' }, 120).animate({ top: '-=' + 5 + 'px' }, 175).animate({ top: '+=' + 5 + 'px' }, 120, null, function() { growlAnimateInProgress = false; });
}

function growl(message, header, theme, sticky, timeout, growlId, ajaxRequest) {
    if (timeout === null)
    { timeout = 10000; }
    
    if (theme)
    { $.jGrowl(AddPTag(message), { theme: theme, header: header, sticky: sticky, life: timeout, afterOpen: growlAfterOpen, growlId: growlId, ajaxRequest: ajaxRequest }); }
    else
    { $.jGrowl(AddPTag(message), { header: header, sticky: sticky, life: timeout, afterOpen: growlAfterOpen, growlId: growlId, ajaxRequest: ajaxRequest }); }
}

function growlError(message, header, growlId, ajaxRequest) {
    growl(AddPTag(message), header, "ui-state-error", true, undefined, growlId, ajaxRequest);
}

function AddPTag(message) {
    var test = $("<div></div>").append(message);
    if (test.children().length == 0)
        return "<p>" + message + "</p>";
    else
        return message;
}

function growlClose(growlId) {
    $("div#" + growlId).trigger("jGrowl.close").remove();    
}

function growlAjaxResult(result, header) {
    if (result === null) { return false; }
    
    if (result.Success) {
        if (result.Message)
        { growl(result.Message, header); }
        else
        { growl("Request successful.", header); }

        return true;
    }

    if (header === null || header === "")
    { header = "Error"; }

    if (result.Message)
    { growlError(result.Message, header); }
    else
    { growlError("An unknown error occured.", header); }

    return false;
}

function growlCloseAll() {
    $('div.jGrowl-close').trigger('click');    
}

function growlAjaxError(this_AjaxRequest, XMLHttpRequest, textStatus, errorThrown) {
    var description = 'A request to the server';
    if (this_AjaxRequest.actiondescription) { description = this_AjaxRequest.actiondescription; }

    var error = description + ' failed due to an unknown error.';
    if (textStatus == 'timeout') { error = description + ' timed out.'; }
    if (textStatus == 'notmodified') { error = description + ' failed with the error "Response was not modified".'; }
    if (textStatus == 'parsererror') { error = description + ' failed due to a parser error.'; }
    if (textStatus == 'error') {
        error = description + ' failed with error ' + XMLHttpRequest.status + '.';
    }
    if (!this_AjaxRequest.allowRetry) {
        error += '<br /><br />Please try again later.';
    }
    growlError(error, "Ajax Error @ " + new Date().toUTCString(), undefined, this_AjaxRequest);
    if (window.console) { console.log("ajax error: " + error); }
}

function logAjaxError(ajaxRequest, XMLHttpRequest, textStatus, errorThrown) {
    var errorData = { textStatus: textStatus, errorThrown: errorThrown, errorUrl: ajaxRequest.url };
    if (XMLHttpRequest) {
        try {
            errorData.xhrstatus = XMLHttpRequest.status;
            errorData.xhrstatusText = XMLHttpRequest.statusText;
            errorData.xhrtext = XMLHttpRequest.responseText;
            if (XMLHttpRequest.responseXML) {
                errorData.xhrxml = XMLHttpRequest.responseXML.xml;
                errorData.xhrurl = XMLHttpRequest.responseXML.url;
            }
        } catch (ex) {
        if (window.console) { console.log("Exception gathering XMLHttpRequest error information: " + ex); }
        }
    }

    $.ajax({
        cache: false,
        data: errorData,
        error: function (XMLHttpRequest, textStatus, errorThrown) { if (window.console) { console.log("Error logging error! " + textStatus); } },
        global: false,
        type: "POST",
        url: "/a/ajaxerror"
    });    
}

// To use this:
//
//    error: function(XMLHttpRequest, textStatus, errorThrown) {
//            ajaxErrorHandler(this, XMLHttpRequest, textStatus, errorThrown);
//            // (Insert your other error handling here)
//        }
//
// If you are just unblocking the UI or something, do that in the complete event instead.
function ajaxErrorHandler(this_AjaxRequest, XMLHttpRequest, textStatus, errorThrown) {
    var loggedOut = false;
    try {
        if (XMLHttpRequest && XMLHttpRequest.status == 403) {
            loggedOut = true;
        }
    } catch (ex) { }

    if (!loggedOut && (dontGrowlErrorsForUrls[this_AjaxRequest.url] === undefined || dontGrowlErrorsForUrls[this_AjaxRequest.url] === false)) {
        growlAjaxError(this_AjaxRequest, XMLHttpRequest, textStatus, errorThrown);
    }

    logAjaxError(this_AjaxRequest, XMLHttpRequest, textStatus, errorThrown);

    if (loggedOut) {
        window.location = '/a/Login?ReturnUrl=' + encodeURIComponent(window.location.href);
    }
}

function checkForEnter(event) {
    if (event.keyCode == 13) {
        return false;
    }
}

var isSafariNotChrome = $.browser.safari && !/chrome/.test(navigator.userAgent.toLowerCase());
var jsondateregex = /^\\?\/Date\(([0-9]{13})(-([0-9]{4}))?\)\\?\/$/;

function parseJsonDate(date) {

    if (date === null || date === undefined) { return false; }

    var dateString;
    if (date.DateTimeOffset && date.DateTimeOffset !== undefined)
    { dateString = date.DateTimeOffset; }
    else
    { dateString = date; }

    var match = dateString.match(jsondateregex);
    if(match) {
        return new Date(parseInt(match[1], 10));
    }
}

function isWeekend(date) {

    var day = date.getDay();
    if (day === 0 || day == 6)
    { return [true, 'ui-state-disabled', 'Weekend']; }

    return [true, ''];
}

function formatDateTextToDate(element) {
    if (element === null || element === "") { return false; }

    try {
        var separator;
        if ($(element).val().indexOf("/") > 0)
        { separator = "/"; }
        else if ($(element).val().indexOf("-") > 0)
        { separator = "-"; }

        var date = $.datepicker.parseDate("mm" + separator + "dd" + separator + "yy", $(element).val());
        return date;
    } catch (e) {
//        var errordate = $(element).val();
//        if (errordate === undefined || errordate === "") { return false; }

//        var month = errordate.substring(0, errordate.indexOf("/"));
//        var day = errordate.substring(errordate.indexOf("/") + 1, errordate.lastIndexOf("/"));
//        var year = errordate.substring(errordate.lastIndexOf("/") + 1);

//        return new Date(year, month - 1, day);
        return null;
    }
}

function callFunction(funct, data) {
    if (funct !== undefined && $.isFunction(funct)) {
        if (data === undefined)
        { funct(); }
        else
        { funct(data); }
    }
}

function formatDateFromText(element) {
    if (element === null || element === "") { return false; }
    var dayInt;
    var yearInt;
    var dateRetVal;
    var months = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"];

    try {

        var text = $(element).val();
        var month = text.substring(0, text.indexOf(" ")).toLowerCase();
        var year = text.substring(text.lastIndexOf(" ") + 1);
        var day = text.substring(text.indexOf(" ") + 1);
        day = day.substring(0, day.indexOf(text.indexOf(",") == -1 ? " " : ","));

        if (isNaN(parseInt(day, 10)) || isNaN(parseInt(year, 10)) || $.inArray(month, months) == -1)
        { return false; }

        if (year.length == 2)
        { year = "20" + year; }

        dateRetVal = new Date(year, $.inArray(month, months), day);
    } catch (e) {
        return false;
    }

    return dateRetVal;
}

function replaceAll(text, strA, strB) {
    return text.replace(new RegExp(strA, "g"), strB);
}

// Simulates PHP's date function
Date.prototype.format = function(format) { var returnStr = ''; var replace = Date.replaceChars; for (var i = 0; i < format.length; i++) { var curChar = format.charAt(i); if (replace[curChar]) { returnStr += replace[curChar].call(this); } else { returnStr += curChar; } } return returnStr; };  Date.replaceChars = { shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], longMonths: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], shortDays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], longDays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], d: function() { return (this.getDate() < 10 ? '0' : '') + this.getDate(); }, D: function() { return Date.replaceChars.shortDays[this.getDay()]; }, j: function() { return this.getDate(); }, l: function() { return Date.replaceChars.longDays[this.getDay()]; }, N: function() { return this.getDay() + 1; }, S: function() { return (this.getDate() % 10 == 1 && this.getDate() != 11 ? 'st' : (this.getDate() % 10 == 2 && this.getDate() != 12 ? 'nd' : (this.getDate() % 10 == 3 && this.getDate() != 13 ? 'rd' : 'th'))); }, w: function() { return this.getDay(); }, z: function() { return "Not Yet Supported"; }, W: function() { return "Not Yet Supported"; }, F: function() { return Date.replaceChars.longMonths[this.getMonth()]; }, m: function() { return (this.getMonth() < 9 ? '0' : '') + (this.getMonth() + 1); }, M: function() { return Date.replaceChars.shortMonths[this.getMonth()]; }, n: function() { return this.getMonth() + 1; }, t: function() { return "Not Yet Supported"; }, L: function() { return "Not Yet Supported"; }, o: function() { return "Not Supported"; }, Y: function() { return this.getFullYear(); }, y: function() { return ('' + this.getFullYear()).substr(2); }, a: function() { return this.getHours() < 12 ? 'am' : 'pm'; }, A: function() { return this.getHours() < 12 ? 'AM' : 'PM'; }, B: function() { return "Not Yet Supported"; }, g: function() { return this.getHours() % 12 || 12; }, G: function() { return this.getHours(); }, h: function() { return ((this.getHours() % 12 || 12) < 10 ? '0' : '') + (this.getHours() % 12 || 12); }, H: function() { return (this.getHours() < 10 ? '0' : '') + this.getHours(); }, i: function() { return (this.getMinutes() < 10 ? '0' : '') + this.getMinutes(); }, s: function() { return (this.getSeconds() < 10 ? '0' : '') + this.getSeconds(); }, e: function() { return "Not Yet Supported"; }, I: function() { return "Not Supported"; }, O: function() { return (this.getTimezoneOffset() < 0 ? '-' : '+') + (this.getTimezoneOffset() / 60 < 10 ? '0' : '') + (this.getTimezoneOffset() / 60) + '00'; }, T: function() { return "Not Yet Supported"; }, Z: function() { return this.getTimezoneOffset() * 60; }, c: function() { return "Not Yet Supported"; }, r: function() { return this.toString(); }, U: function() { return this.getTime() / 1000; } };

function DetectTimeZone(callback) {
    var url = '/a/user/detecttimezone';
    var thisYear = new Date().getFullYear();
    var january1ms = new Date(thisYear, 0,  1, 0, 0, 0, 0).getTime();
    var march31ms  = new Date(thisYear, 2, 31, 0, 0, 0, 0).getTime();
    var june1ms    = new Date(thisYear, 5,  1, 0, 0, 0, 0).getTime();
    var data = { m:"detect", january1ms:january1ms, march31ms:march31ms, june1ms:june1ms };
    // Override the standard error handler to only log the error but not display it.
    $.ajax({type:"POST", url:url, data:data, dataType:"json", success:callback, error: function(XMLHttpRequest, textStatus, errorThrown) { logAjaxError(this, XMLHttpRequest, textStatus, errorThrown); } });
}

function formatCurrency(num) {

    num = num.toString().replace(/\$|\,/g, '');
    var CurrencySymbol = "$";

    if (isNaN(num))
    { num = "0"; }
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
    { cents = "0" + cents; }
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
    { num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3)); }

    return (((sign) ? '' : '-') + CurrencySymbol + num + '.' + cents);
}

Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--) {
        if (this[i] === obj) {
            return true;
        }
    }
    return false;
};

function setWatermark(element, defaultText) {
    $(element).addClass("watermarkOn");
    $(element).attr("value", defaultText);
    $(element).emptyonclick();
}

function destroyWatermark(element) {
    $(element).removeClass("watermarkOn");
}

$(document).ready(function() {
    $.jGrowl.defaults.position = 'center-right';
    $.jGrowl.defaults.closeTemplate = "close";
    $('.ui-state-default').hover(
       function() { $(this).addClass('ui-state-hover'); },
       function() { $(this).removeClass('ui-state-hover'); }
    );
    $.ajaxSetup({
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            ajaxErrorHandler(this, XMLHttpRequest, textStatus, errorThrown);
        }
    });

    $(".buttonPlus").button({ icons: { primary: 'ui-icon-plusthick'} });
});

function getUrlParameterValue(jParam) {
    if (jParam === undefined || jParam.length < 1)
    { return null; }

    jParam = jParam + "=";

    if (window.location.search.substr(window.location.search.indexOf(jParam), jParam.length) == jParam) {
        var startFromIndex = window.location.search.indexOf(jParam) + jParam.length;
        var endIndex = window.location.search.substring(startFromIndex).indexOf("&");

        if (endIndex == -1)
        { return decodeURI(window.location.search.substr(startFromIndex)); }

        return decodeURI(window.location.search.substr(startFromIndex, endIndex));
    }

    return null;
}

function decodeURI(decodedTxt) {
    // first we replace any + chars with single space as javascript built in functions do not replace + with space character
    var tempTxt = decodedTxt.replace(/\+/g, ' ');
    return decodeURIComponent(tempTxt);
}

function getGridParams(cookieName, defaults) {
    var cookieValue = $.cookie(cookieName);
    if (cookieValue !== null && cookieValue.length > 0) {
        return eval('(' + cookieValue + ')');
    } else {
        return defaults;
    }
}

function jqGridColumnDateTimeOffsetFormatter(cellvalue, options, rowObject) {

    if (cellvalue === null || cellvalue.length === 0) {
        return "";
    }

    var date = parseJsonDate(cellvalue);
    if (date === undefined) {
        return "";
    }

    var month = date.getMonth() + 1;
    var day = date.getDate();
    var year = date.getFullYear();

    return String('0' + month).slice(-2) + "/" + String('0' + day).slice(-2) + "/" + year;
}

function loadDatePickerClearButton() {
    $.datepicker._generateHTML_Old = $.datepicker._generateHTML; $.datepicker._generateHTML = function (inst) {
        res = this._generateHTML_Old(inst); res = res.replace("_hideDatepicker()", "_clearDate('#" + inst.id + "')"); return res;
    }
}

function saveJqGridParams(cookieName, sortName, sortOrder, page, filters, extraParams) {
    var cookieValue = new Object();
    var filterOption = "";

    cookieValue["SortName"] = sortName;
    cookieValue["SortOrder"] = sortOrder;
    cookieValue["Page"] = page;

    if (filters != undefined && filters.length > 0)
        cookieValue["Filters"] = filters;

    if (extraParams != undefined && extraParams.length > 0) {
        $.each(extraParams, function (index, filter) {
            if (filter != undefined && filter != null && filter.name != undefined && filter.value != undefined) {
                cookieValue[filter.name] = filter.value;
            }
        });
    }

    if (typeof JSON == 'undefined')
        return false;

    $.cookie(cookieName, JSON.stringify(cookieValue), { path: '/' });
}


function jsonDateToFriendlyDateTime(jsonDate, showTime) {
    if (!jsonDate) { return ""; }
    var date = eval(jsonDate.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));
    return dateToFriendlyDateTime(date, showTime, " ", true);
}

function jsonDateToFriendlyDateTime(jsonDate, showTime, dateTimeSeperator, useFullYear) {
    if (!jsonDate) { return ""; }
    var date = eval(jsonDate.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));
    return dateToFriendlyDateTime(date, showTime, dateTimeSeperator, useFullYear);
}

function dateToFriendlyDateTime(date, showTime) {
    if (!date) { return ""; }
    return dateToFriendlyDateTime(date, showTime, " ", true);
}

function dateToFriendlyDateTime(date, showTime, dateTimeSeperator, useFullYear) {
    if (!date) { return ""; }
    var dateText = (date.getMonth() + 1) + "/" + date.getDate() + "/";

    if (useFullYear) {
        dateText = dateText + date.getFullYear();
    } else {
        dateText = dateText + date.getFullYear().toString().slice(2);
    }

    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();
    var ap = "AM";

    if (hour > 11) ap = "PM";
    if (hour > 12) hour = hour - 12;
    if (hour == 0) hour = 12;
    if (minute < 10) minute = "0" + minute;
    if (second < 10) second = "0" + second;

    var secondText = ':' + second;
    if (second == "00")
        secondText = "";

    if (showTime)
        return dateText + dateTimeSeperator + hour + ':' + minute + secondText + " " + ap;

    return dateText;
}

function isValidDate(d) {
    if (Object.prototype.toString.call(d) !== "[object Date]")
        return false;
    return !isNaN(d.getTime());
}

function htmlDecode(input) { // http://stackoverflow.com/questions/2808368/
    var e = document.createElement('div');
    e.innerHTML = input;
    return e.childNodes[0].nodeValue;
}

// setCursorSelection() // select all text
// setCursorSelection(3) // put the cursor after the third character, with nothing selected
// setCursorSelection(3,5) // select the third through fifth characters
$.fn.setCursorSelection = function (startpos, endpos) {
    if (endpos === undefined) {
        endpos = startpos;
    }
    this.each(function (index, elem) {
        if (startpos === undefined) {
            startpos = 0;
            endpos = $(elem).val().length;
        }
        if (elem.setSelectionRange) {
            elem.setSelectionRange(startpos, endpos);
        } else if (elem.createTextRange) {
            var range = elem.createTextRange();
            range.collapse(true);
            range.moveEnd('character', endpos);
            range.moveStart('character', startpos);
            range.select();
        }
    });
    return this;
};

$.fn.resetParentScrollLeft = function () {
    this.each(function (index, elem) {

        $(elem).parents().each(function (i, e) {
            if ($(e).attr("scrollLeft") != undefined) {
                if ($(e).attr("scrollLeft") > 0) {
                    $(e).attr("scrollLeft", 0);
                    //growl($(e).get(0).tagName + ": " + $(e).attr("id") + ": " + $(e).attr("scrollLeft"));
                }
            }
        });
    });
    return this;
};

if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function (elt /*, from*/) {
        var len = this.length >>> 0;

        var from = Number(arguments[1]) || 0;
        from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
        if (from < 0)
            from += len;

        for (; from < len; from++) {
            if (from in this &&
          this[from] === elt)
                return from;
        }
        return -1;
    };
}
