function hideDiv(e, prop)
{
    $(e).parents(prop).css({"top":"0","left":"-9999px"}); return false;
}

function Close(e, container)
{
    $(container).hide(); return false;
}

function addTitleAndRealClose(prop, titleText, closeText)
{
     addTitleAndCloseProto(prop, titleText, closeText, true);
}


function addTitleAndCloseProto(prop, titleText, closeText, isRealClose)
{
    $(prop).prepend("<table width='100%' border='0' cellpadding='0' cellspacing='0' class='titleRow'><tr><td align='left'><strong>" + titleText + 
            "</strong></td><td align='right' valign='top' style='padding:0px'><a title='" + closeText + "' href='javascript:void(0);' onclick="+(isRealClose?"Close":"hideDiv")+"(this,'" + prop + "');><img src='/i/close.gif' border='0' width='15px' height='15px' /></a></td></tr></table>");
}

function addTitleAndClose(prop, titleText, closeText)
{
    addTitleAndCloseProto(prop, titleText, closeText, false);
}

function assign_confirm(prop){
    $(prop).each(function(i){
    var evnt = $(this).attr("href");
	evnt = escape(evnt.replace(/\"/g, "'"));
    $(this).attr("href",'javascript:confirmation("'+this.id+'", "'+evnt+'");');
 });
}

function confirmation(sender, action)
{
    var offset = $('#' + sender).offset();
     if (offset != null){
         $('#divConfirmation').css({"top":(offset.top + 10),"left":(offset.left - 150)});
     }
     $('#divConfirmationText').text($('#' + sender).attr("ConfirmationText"));
     $('#btnConfirmationSubmit').attr("href", unescape(action));
     if ($('#' + sender).text() != '')
        $('#btnConfirmationSubmit').text($('#' + sender).text());
 }

 (function ($) {
     $.fn.defaultButton = function (buttonId) {
         return this.each(function () {
             var $this = $(this);
             $this.keydown(
                function (event) {
                    if (event.keyCode == '13' && $this.val() != '') {
                            eval($('#' + buttonId).attr('href'));
                    }
                }
            );
         });

     };

     $.fn.initialTextFocus = function() {
         var $this = $(this);
         $this.css({"color":"black","font-style":"normal"});
                        if ($this.attr("InitialText") == $this.val())
	                        $this.val("");
         return $this;
     };

     $.fn.initialTextBlur = function() {
         var $this = $(this);
         if ($this.val() == "" || $this.val() == $this.attr("InitialText")) {
             $this.val($this.attr("InitialText"));
             $this.css({ "color": "gray", "font-style": "italic" });
         }
     };

     $.fn.initialText = function () {
         return this.each(function () {
             var $this = $(this);
             $this.focus($this.initialTextFocus);
             $this.blur($.fn.initialTextBlur);
             $this.initialTextBlur();
         });

     };
     
     $.fn.getCursorPosition = function() {
         var pos = 0;
         var el = $(this).get(0);
         // IE Support
         if (document.selection) {
             el.focus();
             var Sel = document.selection.createRange();
             var SelLength = document.selection.createRange().text.length;
             Sel.moveStart('character', -el.value.length);
             pos = Sel.text.length - SelLength;
         }
             // Firefox support
         else if (el.selectionStart || el.selectionStart == '0')
             pos = el.selectionStart;

         return pos;
     };
     
     $.fn.setCursorPosition = function(position) {
         if (this.lengh == 0) return this;
         return $(this).setSelection(position, position);
     };
     
     $.fn.setSelection = function(selectionStart, selectionEnd) {
         if (this.lengh == 0) return this;
         var input = this[0];

         if (input.createTextRange) {
             var range = input.createTextRange();
             range.collapse(true);
             range.moveEnd('character', selectionEnd);
             range.moveStart('character', selectionStart);
             range.select();
         } else if (input.setSelectionRange) {
             input.focus();
             input.setSelectionRange(selectionStart, selectionEnd);
         }

         return this;
     };

     $.fn.focusEnd = function() {
         this.setCursorPosition(this.val().length);
     };
 })(jQuery);

 (function() {

	var fieldSelection = {

		getSelection: function() {

			var e = this.jquery ? this[0] : this;

			return (

				/* mozilla / dom 3.0 */
				('selectionStart' in e && function() {
					var l = e.selectionEnd - e.selectionStart;
					return { start: e.selectionStart, end: e.selectionEnd, length: l, text: e.value.substr(e.selectionStart, l) };
				}) ||

				/* exploder */
				(document.selection && function() {

					e.focus();

					var r = document.selection.createRange();
					if (r == null) {
						return { start: 0, end: e.value.length, length: 0 }
					}

					var re = e.createTextRange();
					var rc = re.duplicate();
					re.moveToBookmark(r.getBookmark());
					rc.setEndPoint('EndToStart', re);

					return { start: rc.text.length, end: rc.text.length + r.text.length, length: r.text.length, text: r.text };
				}) ||

				/* browser not supported */
				function() {
					return { start: 0, end: e.value.length, length: 0 };
				}

			)();

		},

		replaceSelection: function() {

			var e = this.jquery ? this[0] : this;
			var text = arguments[0] || '';

			return (

				/* mozilla / dom 3.0 */
				('selectionStart' in e && function() {
					e.value = e.value.substr(0, e.selectionStart) + text + e.value.substr(e.selectionEnd, e.value.length);
					return this;
				}) ||

				/* exploder */
				(document.selection && function() {
					e.focus();
					document.selection.createRange().text = text;
					return this;
				}) ||

				/* browser not supported */
				function() {
					e.value += text;
					return this;
				}

			)();

		}

	};

	jQuery.each(fieldSelection, function(i) { jQuery.fn[i] = this; });

})();
