/*********************************************************************************************  

jQuery: 		jQuery HTML List - Calculated column
Filename:		jQueryHTMLList.js
Description: 	This jQuery file loops through all TD elements on the page. If it finds a TD
				element that contains a text substring matching "<DIV" then it will take the 
				internal text string and write it out as html to be rendered by the browser.
				
				This file also contains the original javascript for this process, courtesy of:
				Christophe@PathToSharePoint.com
				
Author: 		Drew Frisk
Version:		v1.0
Created:		7.22.2011
Last Update: 	8.26.2011


******************************************************************************************/

/**** FUNCTION:******************** *************

	....see file description....
	
*************************************************/
$( document ).ready( function () {	

	// TDContent will contain the html from the calulated column field
	var TDContent = " ";

	// loop through each 'td' element on the page
	$( 'TD' ).each( function() {
	
		// set TDContent to contain the html inside the current enumeration of 'TD'
		TDContent = $( this ).text();
		
		// if the substring index of "<DIV" is 0, then the current TD contains the value 
		// of a caluclated column with html in it
		if( ( TDContent.indexOf( "<DIV" ) == 0 ) && ( TDContent.indexOf( "</DIV>" ) >= 0 ) ) {
		
			// then set the html of the TD to be the value of the string 
			$( this ).html( TDContent );
			
		}
	} );

} );

/******************************************************************************
	This is the start of a rewrite to create html in from a calculated column 
	in a collapsable group it currently is not functioning code.
	
	See below for the original code to this function.
******************************************************************************
function ExpGroupRenderData( htmlToRender, groupName, isLoaded ) {
	//alert( groupName );
	var tbody = $( "#tbod" + groupName + "_" );
	var wrapDiv = $( "<div></div>" );


	//alert( wrapDiv );
	$( tbody + " td.ms-vb2" ).each( function(){
		try {
			var TDContent = $( this ).text();
			if( ( TDContent.indexOf( "<DIV" ) == 0 ) && ( TDContent.indexOf( "</DIV>" ) >= 0 ) ) {
				//$( this ).html( TDContent );
			}
		} 
		catch( err ){}
	} );

	wrapDiv.html( "<TABLE><TBODY id='tbod" + groupName + "_' isLoaded='" + isLoaded + "'>" + htmlToRender + "</TBODY></TABLE>" );
	//tbody.html( wrapDiv );	
}
******************************************************************************

************ ORIGINAL TEXT TO HTML SCRIPT ***********************************

// Text to HTML
// Feedback and questions: Christophe@PathToSharePoint.com
//
var theTDs = document.getElementsByTagName("TD");
var i=0;
var TDContent = " ";
while (i < theTDs.length) {
	try {
	TDContent = theTDs[i].innerText || theTDs[i].textContent;
		if ((TDContent.indexOf("<DIV") == 0) && (TDContent.indexOf("</DIV>") >= 0)) {
			theTDs[i].innerHTML = TDContent;
		}
	}
	catch(err){}
	i=i+1;
}
//
// ExpGroupRenderData overwrites the default SharePoint function
// This part is needed for collapsed groupings
//
function ExpGroupRenderData(htmlToRender, groupName, isLoaded) {
	var tbody=document.getElementById("tbod"+groupName+"_");
	var wrapDiv=document.createElement("DIV");
	wrapDiv.innerHTML="<TABLE><TBODY id=\"tbod"+ groupName+"_\" isLoaded=\""+isLoaded+ "\">"+htmlToRender+"</TBODY></TABLE>";
	var theTBODYTDs = wrapDiv.getElementsByTagName("TD"); var j=0; var TDContent = " ";
	while (j < theTBODYTDs.length) {
		try {
		TDContent = theTBODYTDs[j].innerText || theTBODYTDs[j].textContent;
			if ((TDContent.indexOf("<DIV") == 0) && (TDContent.indexOf("</DIV>") >= 0)) {
				theTBODYTDs[j].innerHTML = TDContent;
			}
		}
		catch(err){}
		j=j+1;
	}
tbody.parentNode.replaceChild(wrapDiv.firstChild.firstChild,tbody);
}
***************************************************************************************/
