function ConvertSharePointRestrictedEncodedDivs() {
	//look for divs that need to be converted
	
	//Get the div tags from the file
	var docDivs = document.getElementsByTagName('div');
	
	//if we have some divs
	if (docDivs.length > 0) {

		//look thru each one
		for (i = 0; i < docDivs.length; i++) {

			//this div is an encoded embed (class="{restrictedTag}_spEncoder")
			if (docDivs[i].className.search(/_spEncoder/) > -1) {
				var encodedDiv = docDivs[i];
				//if so, let's decode this div
				DecodeAndReplace(encodedDiv);
				//DecodeDiv(encodedDiv);
			}
		}
	}
}

function DecodeAndReplace(encodedDiv) {
	//let's create a new element to hold the information
	var newElement = CreateNewElement(encodedDiv);
	
	//and replace the encodedDiv with the new element
	var parentNode = encodedDiv.parentNode;
	parentNode.replaceChild(newElement, encodedDiv);

	//now lets move the children
	if (encodedDiv.firstChild != null) {
		DecodeAndAppendChildren(newElement, encodedDiv.firstChild);
	}

}

function DecodeAndAppendChildren(newElement, oldChild) {
	//and let's see if we need to decode any of the child nodes
	var newChild;

	if (oldChild.className.search(/_spEncoder/) > -1) {
		newChild = CreateNewElement(oldChild);
	}
	else {
		newChild = oldChild.cloneNode(false);
	}

	try {

		newElement.appendChild(newChild);

		//now lets move the children of the old child
		if (oldChild.firstChild != null) {
			DecodeAndAppendChildren(newChild, oldChild.firstChild);
		}

		//now lets process the next sibling of this current child
		if (oldChild.nextSibling != null) {
			DecodeAndAppendChildren(newElement, oldChild.nextSibling);
		}

	} catch (err) {
		// There is a very specific issue with the <object> tag that can cause an error (see function for discussion)
	wc3StandardsHack(newElement, newChild, oldChild);
	}

}


function CreateNewElement(encodedDiv) {
	var stop = encodedDiv.className.search(/_spEncoder/);
	var tagType = encodedDiv.className.substring(0, stop);
	var newElement = document.createElement(tagType);
	
	
	var idAttr = document.createAttribute("id");
	idAttr.value = encodedDiv.id;
	newElement.setAttributeNode(idAttr);

	DecodeAndAddAttributes(newElement, encodedDiv.title);

	return newElement;
}

function DecodeAndAddAttributes(newElement, encodedAttributes) {
	//get out the attributes from the encoded title string, they are delimited using a double semi-colon ;;
	var attributes = encodedAttributes.split(";;");

	//for each attribute we have let's put it on the return string
	for (k = 0; k < attributes.length; k++) {
		var attribute, attName = '', attValue = '';

		// the attributes are stored in a format of name::value, so let's pull them out
		attribute = attributes[k].split("::");
		attName = attribute[0];
		attName = attName.replace(/ /, "");

		//now just in case the encoded string has a standalone attribute
		if (attribute[1] != null) {
			attValue = attribute[1]
			attValue = attValue.replace(/ /, "");
		}

		if (attName != '') {

			var idAttr = document.createAttribute(attName);
			if (attValue != '') {
				idAttr.value = attValue;
			}
			newElement.setAttributeNode(idAttr);
			
		}
	}
}


function wc3StandardsHack(parent, newChild, encodedDiv) {
	/*==========================
	
	There is a very specific issue with manipulating the <object> tag in IE in JavaScript
	This catch is here to capture that specific problem 
	
	the problem lies in the fact that according to the wc3 spec the only valid children nodes for the <object> tag
	are <param> tags.  You can badly form your html if you so desire and push it down to the browsers, say put a 
	<embed> tag containing a youtube video to play, nested in the <object>.  All the major broswers will just show
	you the embed.  However IE, when manipulating the html in javascript, KNOWS that anything other then <param> is 
	not allowed.  Remember this whole process exists to allow SharePoint to host restricted tags.  We encode them
	then use this script on the client to decode and get back to the value we want to show the user, however if the
	user is using IE, the attempt to insert the <embed> tag within the <object> tag progmatically is not allowed, and
	an error is raised.
	
	So the work around is this, if the parent is an <object> tag and the child is anything other then a <param> tag
	we will replace the entire object tag with this child tag.
	
	The other browsers are just not standard compliant with the JavaScript and so we don't even see this error
	
	==========================*/

	try {
		if (parent.name = 'object' && newChild.name != 'param') {

			parent.parentNode.appendChild(newChild);
			parent.parentNode.removeChild(parent);
			
		}
		
	} catch (err2) {
		logToConsole(err2);
	}
	
}

ConvertSharePointRestrictedEncodedDivs();

