String.prototype.replaceAll = function(search, replace){
  return this.split(search).join(replace);
}

jQuery.fn.stripTags = function() { return this.html( this.html().replace(/<\/?[^>]+>/gi, '') ); };

$(document).ready(function(){
	
	if( $("#content-block table").hasClass('request-table') ){
		return;
	}
	
	$("#content-block table").addClass('grey-table').removeAttr("border").removeAttr("cellpadding").removeAttr("cellspacing").removeAttr("style").removeAttr("bgcolor");
	$("#content-block table th").removeAttr("style");

	if ($("#content-block table img").length > 0){
		return;
	}

	
	$("#content-block table th").each(function(index){
		$(this).stripTags();
		thHtml = $(this).html();
		$(this).replaceWith('<td style="font-weight: bold;">' + thHtml + '</td>');
	})
	
	$("#content-block table tr").each(function(index){
		
		modified = false;

		allBold = true;
		$(this).find("td").each(function(index2){
			bi = -1;
			if(style = $(this).attr('style')){
				bi = style.indexOf('bold');
				//$('body').prepend(bi);
			}
			
			if( -1 == bi ){
				allBold = false;
			}
		})
		if(allBold){
			$(this).addClass('table-title');
			$(this).find("*").removeAttr("style");

			$(this).find("td").each(function(index3){
				$(this).stripTags();
			})
			modified = true;
		}
		
		if($(this).find('td[colspan]').length == 1){
			td = $(this).find('td');
			colspan = td.attr('colspan');
			colspan--;
			for(i=0; i<colspan; i++){
				td.after('<td></td>')
			}
			
			td.attr('colspan', 1);
			td.removeAttr('valign');
			
			td.stripTags();

			td.html('<span class="title">' + td.html() +  '</span>');
			modified = true;
		}
		
		if(!modified){
			td = $(this).find("td:first");
			td.stripTags();
			td.html('<span>' + td.html() +  '</span>');
			
			$(this).find("td p").each(function(index4){
				$(this).replaceWith($(this).html());
			})
		}
	})
})
