﻿/// <reference path="~/desktopmodules/intevista/js/jquery-1.3.2.min.vsdoc.js"/>

Date.fromJson = function (jsonDate) {
    /// <summary>
    /// Converts a json date into a javascript Date object.
    /// </summary>
    /// <param name="jsonDate">A json formatted date.</param>
    return eval(jsonDate.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));
}

Date.prototype.formatDate = function (format) {

    var date = this;

    if (!format)

        format = "MM/dd/yyyy";



    var month = (date.getMonth() + 1).toString();
    
    var year = date.getFullYear();



    format = format.replace("MM", month);



    format = format.replace("yyyy", year.toString());


    var day = date.getDate().toString();
    format = format.replace("dd", day);



    var hours = date.getHours();

    if (format.indexOf("t") > -1) {

        if (hours > 11)

            format = format.replace("t", "PM")

        else

            format = format.replace("t", "AM")

    }

    if (format.indexOf("HH") > -1)

        format = format.replace("HH", hours);

    if (format.indexOf("hh") > -1) {

        if (hours > 12) hours = hours - 12;

        if (hours == 0) hours = 12;

        format = format.replace("hh", hours);

    }

    var minutes = date.getMinutes().toString();
    if (format.indexOf("mm") > -1)
        format = format.replace("mm", minutes);

    var seconds = date.getSeconds().toString();
    if (format.indexOf("ss") > -1)
        format = format.replace("ss", seconds);

    return format;

}



// Returns the parsed version of the current browser as a JSON object.
jQuery.browser.parsedVersion = function() {
	var values = jQuery.browser.version.split('.');
	var major = parseInt(values[0]) || 0;
	var minor = parseInt(values[1]) || 0;
	var build = parseInt(values[2]) || 0;
	var revision = parseInt(values[3]) || 0;
	
	return {
		major: major,
		minor: minor,
		build: build,
		revision: revision
	}
};


function insertAtCaret(areaId, text) {
    var txtarea = document.getElementById(areaId);
    var scrollPos = txtarea.scrollTop;
    var strPos = 0;
    var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ?
		"ff" : (document.selection ? "ie" : false));
    if (br == "ie") {
        txtarea.focus();
        var range = document.selection.createRange();
        range.moveStart('character', -txtarea.value.length);
        strPos = range.text.length;
    }
    else if (br == "ff") strPos = txtarea.selectionStart;

    var front = (txtarea.value).substring(0, strPos);
    var back = (txtarea.value).substring(strPos, txtarea.value.length);
    txtarea.value = front + text + back;
    strPos = strPos + text.length;
    if (br == "ie") {
        txtarea.focus();
        var range = document.selection.createRange();
        range.moveStart('character', -txtarea.value.length);
        range.moveStart('character', strPos);
        range.moveEnd('character', 0);
        range.select();
    }
    else if (br == "ff") {
        txtarea.selectionStart = strPos;
        txtarea.selectionEnd = strPos;
        txtarea.focus();
    }
    txtarea.scrollTop = scrollPos;
}



$(document).ready(function () {

    // Fade all messages
    $('div[class*=iv_message_]').fadeTo(5000, 1).fadeOut(5000, function () {
        $(this).remove();
    });

    // Support :hover in IE6
    if ($.browser.msie && $.browser.parsedVersion.major <= 6) {
        try {
            $.fixIEHover();
        }
        catch (err) { }
    }

    $(".iv_hrefbutton").click(function () {
        window.location = $(this).attr("href");
    });
});

function htmlEncode(value) { 
  return $('<div/>').text(value).html(); 
} 

function htmlDecode(value) { 
  return $('<div/>').html(value).text();
}

function isScrolledIntoView(elem) {
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();

    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();

    return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom)
      && (elemBottom <= docViewBottom) && (elemTop >= docViewTop));
}

