
/*
 * @version Id: myHTMLParser.js 09/apr/07
 * @package Parcelle
 * @copyright Copyright (C) 2007 Andrea Bizzotto - Tecnobit S.R.L. All rights reserved.
 * @license 
 * Created on 09/apr/07
 */
 
function getTagById(text, tag, id) {

	var idPos = text.indexOf("id=\"" + id + "\"");
	if (idPos == -1) {
		idPos = text.indexOf("id='" + id + "'");
		if (idPos == -1)
			return -1;
	}
	
	var startDivPos = text.lastIndexOf("<" + tag, idPos);
	if (startDivPos == -1)
		return false;

	var closeTagPos = text.indexOf(">", idPos);
	if (closeTagPos == -1)
		return false;
	
	closeTagPos++;
	
	var endDivPos;	
	var endDivPos1 = text.indexOf("/>", idPos);
	var endDivPos2 = text.indexOf("</" + tag + ">", idPos);
	if (endDivPos1 == -1) {
		if (endDivPos2 == -1)
			return false;
			
		endDivPos = endDivPos2 + 6;
	}
	else {
		if (endDivPos2 == -1 || endDivPos1 < endDivPos2)
			endDivPos = endDivPos1 + 2;
		else
			endDivPos = endDivPos2 + 6;
	}
	return startDivPos + "-" + closeTagPos + "-" + endDivPos;
}

/*
 * This function works as following:
 * returns -1 if there isn't tag that matches the id correspondence
 *
 * if the tag format is on the form:
 *   <tag_name [attributes] id=$id [attributes] />
 *   the function returns a triplet as this: "pos1-pos2-pos3" where
 *   - pos1 is the index of the first character of the tag ('<')
 *   - pos2 and pos3 are equal to the index of the first character that follows the tag (the character after '/>')
 * else if the tag format is on the form:
 *   <tag_name [attributes] id=$id [attributes]>[content]</tag>
 *   the function returns a triplet as this: "pos1-pos2-pos3" where
 *   - pos1 is the index of the first character of the tag ('<')
 *   - pos2 is equal to the index of the first character of the tag content (the character after '>')
 *   - pos3 is equal to the index of the first character that follows the tag (the character after '</div>')
 * returns false if an error occurs
 */
function getParcelDivById(html, id) {
	var idPos = html.indexOf("id=\"" + id + "\"");
	if (idPos == -1) {
		idPos = html.indexOf("id='" + id + "'");
		if (idPos == -1)
			return -1;
	}
	
	var startDivPos = html.lastIndexOf("<div", idPos);
	if (startDivPos == -1)
		return false;

	var closeTagPos = html.indexOf(">", idPos);
	if (closeTagPos == -1)
		return false;
	
	closeTagPos++;
	
	var endDivPos;	
	var endDivPos1 = html.indexOf("/>", idPos);
	var endDivPos2 = html.indexOf("</div>", idPos);
	if (endDivPos1 == -1) {
		if (endDivPos2 == -1)
			return false;
			
		endDivPos = endDivPos2 + 6;
	}
	else {
		if (endDivPos2 == -1 || endDivPos1 < endDivPos2)
			endDivPos = endDivPos1 + 2;
		else
			endDivPos = endDivPos2 + 6;
	}
	return startDivPos + "-" + closeTagPos + "-" + endDivPos;
}

function normalizeHTML(html) {

	var indexes = getParcelDivById(html, "sections");
	if (indexes != false && indexes != -1) {
		
		var index = indexes.split("-");
		
		// if the case is: <div id="sections" ...  /><div id="totals">...
		// break the tag into: <div id="sections" ... ></div><div id="totals">...
		if (index[1] == index[2])
			html = html.substring(0, index[1] - 2) + "></div>" + html.substring(index[2]);

		//alert("\"sections\" indexes = " + indexes + "\nHTML:\n" + html);
		return html;
	}
	else {
		// if the "sections" tag does not exist (TinyMCE deletes it if it has no content), simply add it
		var sectionsDiv =
		   "<div id='sections'></div>";

		var indexes = getParcelDivById(html, "totals");
		if (indexes != false && indexes != -1) {
			var index = indexes.split("-");
			//alert("left:\n" + html.substring(0, index[0]) + "\nsection\n" + sectionsDiv + "\nright\n" + html.substring(index[0]));
			html = html.substring(0, index[0]) + sectionsDiv + html.substring(index[0]);

			return html;
		}
	}
	return false;
}
