/*
* jQuery 1.2.3 - New Wave Javascript
*
* Copyright (c) 2008 John Resig (jquery.com)
* Dual licensed under the MIT (MIT-LICENSE.txt)
* and GPL (GPL-LICENSE.txt) licenses.
*
* $Date: 2008-02-06 00:21:25 -0500 (Wed, 06 Feb 2008) $
* $Rev: 4663 $
*/

/* Extending jQuery selectors */
jQuery.extend(jQuery.expr[':'], {
	text:	"(a.nodeName=='INPUT' && (a.type=='text' || a.type=='password')) || a.nodeName=='TEXTAREA'",
	button:	"(a.nodeName=='INPUT' && 'button,submit,reset'.indexOf(a.type) >= 0) || a.nodeName=='BUTTON'",
	submit:	"a.nodeName=='INPUT' && a.type=='submit'"
});

/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

/* Clear form function */
$.fn.clearForm = function() {
    return this.each(function() {
    var type = this.type, tag = this.tagName.toLowerCase();
    if (tag == 'form')
        return $(':input',this).clearForm();
    if (type == 'text' || type == 'password' || tag == 'textarea')
        this.value = '';
    else if (type == 'checkbox' || type == 'radio')
        this.checked = false;
    else if (tag == 'select')
        this.selectedIndex = 0;
    });
};




      

       var MAX_DUMP_DEPTH = 10;

      

       function dumpObj(obj, name, indent, depth) {

              if (depth > MAX_DUMP_DEPTH) {

                     return indent + name + ": <Maximum Depth Reached>\n";

              }

              if (typeof obj == "object") {

                     var child = null;

                     var output = indent + name + "\n";

                     indent += "\t";

                     for (var item in obj)

                     {

                           try {

                                  child = obj[item];

                           } catch (e) {

                                  child = "<Unable to Evaluate>";

                           }

                           if (typeof child == "object") {

                                  output += dumpObj(child, item, indent, depth + 1);

                           } else {

                                  output += indent + item + ": " + child + "\n";

                           }

                     }

                     return output;

              } else {

                     return obj;

              }

       }

      


/* Popup AJAX form function */
function popupForm(link){
	var popupLink = $(link);
    $('.popup,.popup-content,.tip,.tooltip').css('z-index','1');
    $('.tip').addClass('none');
	$('.popup-content').remove()
	$('<div class="popup-content"><h3>Loading...</h3></div>').insertBefore(link);
    popupLink.parent().css('z-index','999');
	$('.popup-content').load(popupLink.attr('href'),function(){
		$('#property_id').attr('value', popupLink.attr('id').substring(5));
		$('.popup-close').click(function(){
            $('.popup-content').remove();popupLink.removeClass('none');
            $('.popup,.popup-content,.tip,.tooptip').css('z-index','1');
        });
		$(".popup-content form").validate({
			submitHandler: function(form){
				myForm = $(form);
				var pdata = myForm.serialize();
				var url = popupLink.attr('href');
				$.ajax({
					url: url,
					data: pdata,
					beforeSend: function(){
						myForm.html('<h3>Loading...</h3>');
					},
					beforeSend: function(){
						myForm.html('<h3>Saving...</h3>');
					},
					success: function(){
						$('.popup-content').remove()
					}
				});
				return false;
			}
		});
	});
return false;
}

/* Bookmark page */
function bookmark(url,title){
	if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
		window.external.AddFavorite(url,title);
	} else if (navigator.appName == "Netscape") {
		window.sidebar.addPanel(title,url,"");
	}
}

/* Allow only numbers in input */
function isNumberKey(evt){
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57))
	return false;
	return true;
}

/* Format numbers by adding commas */
function numberFormat(nStr,prefix){
    var prefix = prefix || '';
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1))
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    return prefix + x1 + x2;
}

/*
 * Copyright (c) 2007 Josh Bush (digitalbush.com)
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE. 
 */
(function($) {

    //Helper Function for Caret positioning
    $.fn.caret=function(begin,end){ 
        if(this.length==0) return;
        if (typeof begin == 'number') {
            end = (typeof end == 'number')?end:begin;  
            return this.each(function(){
                if(this.setSelectionRange){
                    this.focus();
                    this.setSelectionRange(begin,end);
                }else if (this.createTextRange){
                    var range = this.createTextRange();
                    range.collapse(true);
                    range.moveEnd('character', end);
                    range.moveStart('character', begin);
                    range.select();
                }
            });
        } else {
            if (this[0].setSelectionRange){
                begin = this[0].selectionStart;
                end = this[0].selectionEnd;
            }else if (document.selection && document.selection.createRange){
                var range = document.selection.createRange();           
                begin = 0 - range.duplicate().moveStart('character', -100000);
                end = begin + range.text.length;
            }
            return {begin:begin,end:end};
        }       
    };
})(jQuery);


