
/* Add trim() function for String. */
String.prototype.trim = function (){
	return this.replace(/^\s+|\s+$/, '');
};

jQuery.noConflict();
jQuery(function(){
    jQuery('body').css('visibility', 'visible');
    //jQuery('div.PageButton').setWidth();
});

(function($) {
    $.fn.setWidth = function() {
        return this.each(function() {
            var myWidth = 0;
            $(this).children().each(function(){
                myWidth += $(this).outerWidth(true);
            });
            $(this).width(myWidth);
        });
    };    
})(jQuery);

function includeHtml(target, url, order, callback) {
    var html;
    html = jQuery.ajax({url: url, async: false}).responseText;
    if(order==null)
        jQuery(target).append(html);
    else 
        jQuery(target).prepend(html);

    if(typeof(callback) != "undefined" && typeof(callback) == "function") {
        callback();
    }    
}

function buttonSubmitForm(button) {
    if (!button || !button.form) {
        alert('button or form is not available!');
        return;
    }
    
    var input = document.createElement('input');
    input.type = 'hidden';
    input.name = button.name;
    input.value = button.value;
    button.form.appendChild(input);
    button.form.submit();
}
/*Misc function for Table: ajust tbody height due to Max Height for FireFox*/
function ajustTBodyHeight () {
    try {
        var tbody = document.getElementsByTagName("tbody");
        for ( var i = 0 ; i < tbody.length ; i ++ ) {
            var tbodyHeight = tbody[i].clientHeight;
            var maxHeight = parseInt( document.defaultView.getComputedStyle(tbody[i], null).getPropertyValue("max-height") );

            if ( tbodyHeight > maxHeight ) {
                tbody[i].style.height = maxHeight + "px";
            }
        }
    }
    catch ( err ) {
        //handle error
    }
}

/*Remove heading and trailing spaces of a string */
function trim(value) {
	return value.replace(/^\s+|\s+$/, '');
}

/*Remove all spaces of a string (include spaces in the middle of the string) */
function trimAll(value) {
	return value.replace(/\s+/g, '');
}

function trimFieldValue(fieldName) {
	fieldName.val(trim(fieldName.val()));
}

function trimAllFieldValue(fieldName) {
	fieldName.val(trimAll(fieldName.val()));
}

function openPopupWindow(url, windowName, width, height) {
	if(windowName == null) {
		windowName = 'newWindow';
	}
	
	if(width == null) {
		width = 900;
	}
	
	if(height == null) {
		height = 700
	}
		
	var iLeft = (screen.width  - width) / 2 ;
	var iTop  = (screen.height - height) / 2 ;

	var sOptions = "toolbar=no,scrollbars=yes,status=no,resizable=no,dependent=yes" ;
	sOptions += ",width=" + width;
	sOptions += ",height=" + height ;
	sOptions += ",left=" + iLeft ;
	sOptions += ",top=" + iTop ;

	var oWindow = window.open( url, windowName, sOptions );
	oWindow.focus();
}

function setDefaultValue(id, defaultValue) {
	var selector = "#" + id;
	if((jQuery(selector).val().trim()) == "") {
		jQuery(selector).val(defaultValue);
	}
}

function clearDefaultValue(id, defaultValue) {
	var selector = "#" + id;
	if((jQuery(selector).val().trim()) == defaultValue) {
		jQuery(selector).val('');
	}
}

function getJsonDataValue(jsonData) {
	var elementId = jsonData.Element;
	var value = jQuery("#" + elementId).val();
	return value;
}
