if (jQuery) {
	/*
	OPTIONS:
	baseClass:
		Javascript will alter additional class to it's target table. So, <table class="table3_test-at-glance"> will formated to <table class="table3_test-at-glance tables">
	applyOddEven:
		1: alter each TR inside TBODY with 'oddrow' and 'evenrow' class.
		0: turn off (we used this on no_frills.html)
		2: 'odd' and 'even' are reversed (currently there is no tables using this)
	formatBorder:
		true: to remove border of header row and table's outline border,
		false: turn off (no_frills.html)
	firstColHeader:
		true: first column will be formated as header, by adding 'header' class and scope attribute
	firstRowHeader:
		true: will generate THEAD section based on first row of TBODY.
	   
	tableFormat:
		set of advanced format.
	*/
	
	$.fn.alterTable = function(opt) {
		var opt = $.extend({
			baseClass:'tables',
			applyOddEven: 1,
			formatBorder:true,
			firstColHeader:true,
			firstRowHeader:true,
			tableFormat:null
		},opt);
		
		return this.each(function() {
			var table = this;
			var options = opt;
			
			var initBorder = function() {
				if (options.formatBorder){
					$("tr",table).find("td:first,th:first").addClass('firstCol');
					$("tr",table).find("td:last,th:last").addClass('lastCol');
				}
			};
			
			var initEvenOdd = function() {
				if (options.applyOddEven == 1) {
					$("tbody>tr:even", table).addClass("oddRow");
					$("tbody>tr:odd", table).addClass("evenRow");
				} else if (options.applyOddEven == 2) {
					$("tbody>tr:even", table).addClass("evenRow");
					$("tbody>tr:odd", table).addClass("oddRow");
				}
			};
			
			var makeSelector = function(str) {
				var s = "";
				var isCell = false;
				var isRow = false;
				var isCol = false;
				if (m = /^row-(\d+|n)$/i.exec(str)) {
					if (m[1] == 'n') {
						s = "tr";
					} else {
						s = "tr:eq("+m[1]+")";
					}
					isRow = true;
				} else if (m = /^col-(\d+|n)$/i.exec(str)) {
					if (m[1] == 'n') {
						s = "tr>td";
					} else {
						s = "tr>td:eq("+m[1]+")";
					}
					isCol = true;
				} else if (m = /^rowcol-(\d+|n)-(\d+|n)$/i.exec(str)) {
					if (m[1] == 'n' && m[2] == 'n') {
						s = "tr>td";
						isCol = true;
					} else if (m[1] == 'n' && m[2] != 'n') {
						s = "tr>td:eq("+m[2]+")";
						isCol = true;
					} else if (m[1] != 'n' && m[2] == 'n') {
						s = "tr:eq("+m[1]+")>td";
						isCol = true;
					} else {
						s = "tr:eq("+m[1]+")>td:eq("+m[2]+")";
						isCell = true;
					}
				}
				return { s:s,isCell:isCell,isRow:isRow,isCol:isCol };
			};
			
			var init = function() {
			
				if (options.baseClass) {
					$(table).addClass("tables");
				}
				
				if (options.firstRowHeader) {
					
					$thead=$('<thead></thead>');
					$tr=$('<tr></tr>');
					$("tr:first>td", table).each(function() {
						h = $(this).html();
						// DMC
						if (isNaN($(this).attr("colspan"))) {
    						t = $('<th>'+h+'</th>');
                        }
                        else {
    						t = $('<th colspan=' + $(this).attr("colspan") + '>'+h+'</th>');
                        }                        
						t.addClass("header");
						if (h != "") {
							t.attr("scope","col");
						}
						$tr.append(t);
					});
					
					$thead.append($tr);
					
					// DMC: fix for embedded tables
					$("tbody:first",table).before($thead);
										
					$("tbody>tr:first",table).remove();
					
				}
				
				if (options.firstColHeader) {
					$("tr",table).find("td:first").addClass("header").attr("scope","row");
				}
				
				initEvenOdd();
				initBorder();
				
				
				if (options.tableFormat) {
					tf = options.tableFormat;
					//alert(tf.width);
					//width
					if (tf.width) {
						
						for(i=0;i<tf.width.length;i++) {
							w=tf.width[i];
							if (w.length > 1) {
								s = makeSelector(w[0]);
								
								if (s.s!=""){
									$(s.s,table).css("width",w[1]);
								}
							}
						}
					}
					//height
					if (tf.height) {
						for(i=0;i<tf.height.length;i++) {
							w=tf.height[i];
							if (w.length > 1) {
								s = makeSelector(w[0]);
								if (s.s!=""){
									$(s.s,table).css("height",w[1]);
								}
							}
						}
					}
					//add class
					if (this.addClass) {
						for(i=0;i<this.addClass.length;i++){
							w=this.addClass[i];
							if (w.length>1) {
								s = makeSelector(w[0]);
								if (s.s!=""){
									$(s.s,table).addClass(w[1]);
								}
							}
						}
					}
					//wrap HTML
					if (tf.wrapHTML) {
						for(i=0;i<tf.wrapHTML.length;i++){
							w=tf.wrapHTML[i];
							if (w.length>1) {
								s = makeSelector(w[0]);
								if (s.s!="" && (s.isCol || s.isCell)){
									$(s.s,table).wrapInner(w[1]);
								}
							}
						}
					}
					
				}
			};
			
			
			init();
		});
	};
	
	alterTableFormat = function(opt) {
		if (opt) {
			return $.extend({
				table: "",
				width:null,
				height:null,
				addClass:null,
				wrapHTML:null
			},opt);
		}
		return null;
	};
	
	$(document).ready(function() {
		//demo table_demo.html
		/*
		format = new alterTableFormat({ 
			width: [ ["col-1","100px"] ],
			height: [ ["row-1","50px"] ],
			addClass: [ ["col-0","header"],["rowcol-2-2","alignCenter"] ],
			wrapHTML: [ ["row-1","<span></span>"], ["rowcol-2-2","<span style='font-size:15px;color:red;'></span>"] ]
		});
		*/
		//$("table.table_demo").alterTable();
		
		if ($("table.table1_course-comparisons")!=null)
		{
		    $("table.table1_course-comparisons").alterTable();
		}
		
		if ($("table.table2_course-descriptions")!=null)
		{
		    $("table.table2_course-descriptions").alterTable({
			    // DMC 5/15
			    //firstRowHeader:false
			    firstColHeader:false,
			    firstRowHeader:true
		    });
		}
		
		if ($("table.table3_test-at-glance")!=null)
		{
		    $("table.table3_test-at-glance").alterTable({
			    firstRowHeader:false
		    });
		}
		
		if ($("table.table4_comparison-chart")!=null)
		{
		    $("table.table4_comparison-chart").alterTable();
		}
		
		if ($("table.table6_what-you-get")!=null)
		{
		    $("table.table6_what-you-get").alterTable({
			    firstRowHeader:false,
			    firstColHeader:false
		    });
		}
		
		if ($("table.table8_test-dates")!=null)
		{
		    $("table.table8_test-dates").alterTable();
		}
		
		if ($("table.table9_noFrills")!=null)
		{
		    $("table.table9_noFrills").alterTable({
			    firstColHeader:false,
			    formatBorder:false,
			    applyOddEven:0
		    });
		}
		
	});
}