﻿

// this is a small helper extension i stole from
// http://www.texotela.co.uk/code/jquery/reverse/
// it merely reverses the order of a jQuery set.
$.fn.reverse = function() {
    return this.pushStack(this.get().reverse(), arguments);
};

// create two new functions: prevALL and nextALL. they're very similar, hence this style.
$.each( ['prev', 'next'], function(unusedIndex, name) {
    
	$.fn[ name + 'ALL' ] = function(matchExpr) {
        // get all the elements in the body, including the body.
        var $all = $('body').find('*').andSelf();

        // slice the $all object according to which way we're looking
        $all = (name == 'prev')
             ? $all.slice(0, $all.index(this)).reverse()
             : $all.slice($all.index(this) + 1)
        ;
        // filter the matches if specified
        if (matchExpr) $all = $all.filter(matchExpr);
        return $all;
    };
});


/*** 
* This function return parent tr.
* .get(0).tagName;
* @param {Object} domElement
*/
jQuery.fn.moveToParent = function(domElement) {
    var parentEl = $(this).parents().map(function() {
        if (this.tagName == domElement
			|| this.tagName.toUpperCase() == domElement.toUpperCase()) {
            return this;
        }
    });

    return parentEl;
}


/*
 * Function check value if is null or empty
 * If is value null or empty return false
 * @param {Object} value
 */
jQuery.stringIsNullOrEmpty = function(value) {
	if(value == ''
	|| value == 'undefined' 
	|| value == typeof undefined)
		return false;
	else
		return true;
};

/***
 * Function fiiled select box
 * @param {Object} data
 */
$.fn.addItems = function(data){
	return this.each(function(){
		var list = this;
		$.each(data, function(index, itemData){
			var option = new Option(itemData.code, itemData.name);
			list.add(option);
		});
	});
};


/*
jQuery.htmlentities = function(str) {
	var i, output = '', len, chr = '';
    len = str.length;
    for(i=0;i<len;i++){
        char = str[i].charCodeAt(0);
        if( (char>47 && char<58)||(char>62 && char<127) ){
            output += str[i];
        }else{
            output += "&#" + str[i].charCodeAt(0) + ";";
        }
    }
    return output;
};
*/

/*
 * Function use for display log message
 * @param {Object} message
 */
jQuery.debug = {
	displayLog: function(message){
		
		if (window.console) {
			console.debug(message);
		}
		else {
			alert(message);
		}
	}	
};

/*
 * Function displays error message.
 * @param {Object} message
 * @param {Object} exception - if sent as string or XMLHttpRequest, will be appended to parameter message
 */
jQuery.log = {
    error: function(message, exception) {
        if (exception == undefined) {
            $.modalDialog.showError(message, 'Error');
        } else if (exception instanceof XMLHttpRequest) {
            if (!$.stringIsNullOrEmpty(exception.responseText)) {
                //alert(exception.responseText);
                try {
                    var respMessage = eval('(' + exception.responseText + ')').Message
                    $.modalDialog.showError(message + ': ' + respMessage, 'Error');
                } catch (e) {
                    //on server error it may happen, that responseText is not JSON object
                    $.modalDialog.showError(message, 'Error');
                }
            } else {
                $.modalDialog.showError(message, 'Error');
            }
        } else {
           $.modalDialog.showError(message + ': ' + exception, 'Error');
        }
    }
};




/**
 * Function convert string boolen on boolean
 * Examples: ("True").bool();
 */
String.prototype.bool = function() {
    return (/^true$/i).test(this);
};

/**
 * This helper methods to validate arguments.
 */
jQuery.argUtil = {
	
	notNullNorEmpty: function(arg, argName) {
		if($.stringIsNullOrEmpty(arg) || typeof(arg) == "undefined") {
			$.log.error(argName + ' should not be empty.');
		}
	}
};





