// JavaScript Document

/*
====================================================================================================================
#	Date			Developer	Brief Description of Change and Reason for Change
====================================================================================================================
1.	7-Mar-2007		ph			Add support for HTML (non-XHTML) text.
								Usage: Put HTML text between "<![CDATA[|HTML|" and "]]>" tags.
2.	8-Mar-2007		ph			Add handler for IE to handle "script" tag.
								Every "script" tag in IE will be moved out from its parent tag if any.
								This is to enable the script to be executed in IE.
3.	13-Mar-2007		ph			Fixed IE bug which gets error when IE proccess the "style" tag.
4.	21-Mar-2007		ph			For "<![CDATA[|HTML|" and "]]>", "|HTML|" will be removed after convert to XHTML.
								This is to prevent "script" tag being eliminated when it is put as 1st child in 
								innerHTML in IE.
5.	31-May-2007		ph			Added fade in and fade out effects for taconite-replace-children.
								Usage: Include SpryEffects.js
								<taconite-replace-children contextNodeID="target_div" parseInBrowser="true" fadeOut="true" fadeIn="true" clearLoadingID="my_loading">
								fadeOut = true : fade out the content before replacing the content.
								fadeIn = true : fade in the content after replacing the content.
								clearLoadingID = {ID} : Clear loading container after fade in. 
								Normally for ajaxRequest.setLoadingAutoRemove(false).
6.	04-Jun-2009		ph			Handle IE8: Set the input value at last, otherwise IE8 will reset the value to 
								"Submit Query" when it set the type to "Submit" after setting the value.
7.	04-Oct-2010		ph			2 major handling changes:
								1. For taconite-append-as-children, taconite-append-as-first-child and
								taconite-replace-children, if there is no element (nodeType = 1) inside the tag,
								then use innerHTML method to write the content. This is for non-IE only, because
								IE cannot set innerHTML if it is called before the page is finish loaded.
								2. Totally change on handleHTMLNodes function (added at #1), where no more using
								HTML to XHTML converter, straight create a DIV and set the innerHTML, after that
								remove that DIV.
								3. Removed previous "script_tag" handling for IE (#2), it's fine already in IE.
								4. The text inside an option tag must be trim for IE9.
===================================================================================================================
*/

var taconite_parser_version=1.502;
var ajax_fadeOut_duration = 800; // #5
var ajax_fadeIn_duration = 800; // #5
var isIE=document.uniqueID;
String.prototype.trim = function() {
    //skip leading and trailing whitespace
    //and return everything in between
    var x=this;
    x=x.replace(/^\s*(.*)/, "$1");
    x=x.replace(/(.*?)\s*$/, "$1");
    return x;
};

function requiresContextNode(xmlTagName) {
    return !(xmlTagName == "taconite-execute-javascript" || xmlTagName == "taconite-redirect");
}


function XhtmlToDOMParser(){
    this.parseXhtml = function(xml){
        var xmlTagName=xml.tagName.toLowerCase();
        var contextNode=document.getElementById(xml.getAttribute("contextNodeID"));
        if(contextNode == null && requiresContextNode(xmlTagName)){
            return false;
        }
        switch (xmlTagName) {
            case "taconite-append-as-children":
                /* #7 getReplaceChildren(contextNode,xml,false);*/
				/* #7 */
				if( isFullHTMLContent(xml) ){
					getReplaceChildrenHTML(contextNode,xml,false);
				}
				else{
					getReplaceChildren(contextNode,xml,false);
				}
				/* end #7 */
                break;
            case "taconite-delete":
                getDelete(contextNode,xml);
                break;
            case "taconite-append-as-first-child":
                /* #7 getAppendAsFirstChild(contextNode,xml);*/
				/* #7 */
				if( isFullHTMLContent(xml) ){
					getAppendAsFirstChildHTML(contextNode,xml);
				}
				else{
					getAppendAsFirstChild(contextNode,xml);
				}
				/* end #7 */
                break;                         
            case "taconite-insert-after":
                getInsertAfter(contextNode,xml);
                break;
            case "taconite-insert-before":
                getInsertBefore(contextNode,xml);
                break;                         
            case "taconite-replace-children":
				// #5 Check the fadeIn & fadeOut value.
				var fadeOut = xml.getAttribute("fadeOut");
				if( fadeOut != null ){
					if( fadeOut.toLowerCase() == "true" )
						fadeOut = true;
					else
						fadeOut = false;
				}
				else {
					fadeOut = false;
				}
				var fadeIn = xml.getAttribute("fadeIn");
				if( fadeIn != null ){
					if( fadeIn.toLowerCase() == "true" )
						fadeIn = true;
					else
						fadeIn = false;
				}
				else {
					fadeIn = false;
				}
				// end #5 Check the fadeIn & fadeOut value.
				
				// #5 Function to check & clear loading container
				var clearLoadingContainer = function(){
					var loadingContainer = document.getElementById(xml.getAttribute("clearLoadingID"));
					if( loadingContainer != null ){
			            while(loadingContainer.childNodes.length >0){
			                loadingContainer.removeChild(loadingContainer.childNodes[0]);
			            }      
			        }
				}
				// end #5 Function to check & clear loading container
				
				// #5 Function to clear opacity, so it won't looks weird in IE
				var clearOpacity = function(){
					if (window.ActiveXObject)
						contextNode.style.filter = "";
					else
						contextNode.style.opacity = "";
				}
				// end #5 Function to clear opacity, so it won't looks weird in IE
				
				// #5 Replace/Update content with fade in and fade out effects base on request.
				if( fadeOut ){
					var fade_stop = function() {
						 try {
						 	/* #7 getReplaceChildren(contextNode,xml,true);*/
							/* #7 */
							if( isFullHTMLContent(xml) ){
								getReplaceChildrenHTML(contextNode,xml,true);
							}
							else{
								getReplaceChildren(contextNode,xml,true);
							}
							/* end #7 */
						 }
						 catch(exception){}
						 if( fadeIn ){ // If fadeIn & fadeOut, then fade out content, replace content, fade in content.
							var ajaxFade = new Spry.Effect.Fade(xml.getAttribute("contextNodeID"), {finish:clearOpacity, duration: ajax_fadeIn_duration, from: 0, to: 100, toggle:false});
							ajaxFade.start();
						}
						else{ // If fadeOut only, then fade out content, replace content, show the content.
							clearOpacity();
						}
						clearLoadingContainer();
					}
					var ajaxFade = new Spry.Effect.Fade(xml.getAttribute("contextNodeID"), {finish:fade_stop, duration: ajax_fadeOut_duration, from: 100, to: 0, toggle:false});
					ajaxFade.start();
				}
				else if( fadeIn ){ // If fadeIn only, then hide content, replace content, fade in content.
						var fade_stop = function() {
							try {
								/* #7 getReplaceChildren(contextNode,xml,true);*/
								/* #7 */
								if( isFullHTMLContent(xml) ){
									getReplaceChildrenHTML(contextNode,xml,true);
								}
								else{
									getReplaceChildren(contextNode,xml,true);
								}
								/* end #7 */
							}
						 	catch(exception){}
							var ajaxFade = new Spry.Effect.Fade(xml.getAttribute("contextNodeID"), {finish:clearOpacity, duration: ajax_fadeIn_duration, from: 0, to: 100, toggle:false});
							ajaxFade.start();
							clearLoadingContainer();
						}
						var ajaxFade = new Spry.Effect.Fade(xml.getAttribute("contextNodeID"), {finish:fade_stop, duration: 0, from: 100, to: 0, toggle:false});
						ajaxFade.start();
				}
				else{ // If no fadeIn & no fadeOut, then just do nothing on fading.
					/* #7 getReplaceChildren(contextNode,xml,true);*/
					/* #7 */
					if( isFullHTMLContent(xml) ){
						getReplaceChildrenHTML(contextNode,xml,true);
					}
					else{
						getReplaceChildren(contextNode,xml,true);
					}
					/* end #7 */
					clearLoadingContainer();
				}				
				// end #5 Replace/Update content with fade in and fade out effects base on request.                
                break;
            case "taconite-replace":
                getReplace(contextNode,xml);
                break;                         
            case "taconite-set-attributes":
                xml.removeAttribute("contextNodeID");
                xml.removeAttribute("parseInBrowser");
                handleAttributes(contextNode,xml);
                break;
            case "taconite-redirect":
                handleRedirect(xml);
                break;
            case "taconite-execute-javascript":
                executeJavascript(xml);
                break;
        }  
        return true;
    };
    
    function isInlineMode(node) {
        var attrType;
        if(!node.tagName.toLowerCase() == "input") {
            return false;
        }
        attrType=node.getAttribute("type");
        
        if(attrType=="radio" || attrType=="checkbox") {
            return true;
        }
        return false;
    }  
    this.getJavaScript= function() {
        return "var dummy_taconite_variable=0";
    }; 
    function handleNode(xmlNode){
        var nodeType = xmlNode.nodeType;               
        switch(nodeType) {
            case 1:  //ELEMENT_NODE
                return handleElement(xmlNode);
            case 3:  //TEXT_NODE
           // case 4:  //CDATA_SECTION_NODE
                var textNode = document.createTextNode(xmlNode.nodeValue);
                if(isIE) {
                    textNode.nodeValue = textNode.nodeValue.replace(/\n/g, '\r'); 
                }
                return textNode;
			case 4:  //CDATA_SECTION_NODE	// Edited by ph 7-Mar-2007
				if( xmlNode.data.substring(0, 6) == "|HTML|" ){	// HTML (NON-XHTML) TEXT			
					return handleHTMLNodes(xmlNode);					
				}
				else{  // SAME WITH CASE 3
					var textNode = document.createTextNode(xmlNode.nodeValue);
	                if(isIE) {
	                    textNode.nodeValue = textNode.nodeValue.replace(/\n/g, '\r'); 
	                }
	                return textNode;
				}
				
        }      
        return null;
    }
    function handleElement(xmlNode){
        var domElemNode=null;
        var xmlNodeTagName=xmlNode.tagName.toLowerCase();
        if(isIE){
            if(isInlineMode(xmlNode)) {
                return document.createElement("<INPUT " + handleAttributes(domElemNode,xmlNode,true) + ">");
            }
            if(xmlNodeTagName == "style"){
                //In internet explorer, we have to use styleSheets array.		
                var text,rulesArray,styleSheetPtr;
                var regExp = /\s+/g;
                text=xmlNode.text.replace(regExp, " ");
                rulesArray=text.split("}");
                
                domElemNode=document.createElement("style");
				
				// Added by ph 13-Mar-2007
				// If "style" tag not found, create one on document body
				if( document.styleSheets.length == 0 ){
					var document_style = document.createElement("style");
					window.document.body.appendChild(document_style);
				}
				
                var styleSheetPtr=document.styleSheets[document.styleSheets.length-1];
                for(var i=0;i<rulesArray.length;i++){
                    rulesArray[i]=rulesArray[i].trim();
                    var rulePart=rulesArray[i].split("{");
                    if(rulePart.length==2) {//Add only if the rule is valid
                    	rulePart[0] = rulePart[0].trim();
						rulePart[1] = rulePart[1].trim();
					    if( rulePart[1] == "" )	// Added by ph 13-Mar-2007 to prevent error when it is empty value
							rulePart[1] = " ";
						styleSheetPtr.addRule(rulePart[0],rulePart[1],-1);//Append at the end of stylesheet.
                    }
                }							
                return domElemNode;			
            }
            
        }
        if(domElemNode == null){
            if(useIEFormElementCreationStrategy(xmlNodeTagName)) {
                domElemNode = createFormElementsForIEStrategy(xmlNode);
            }
            else {
				/* #7 No longer use.
				if(isIE && xmlNodeTagName == "script"){  // Added by ph 8-Mar-2007
					// script tag will be handled separately for IE
					domElemNode = document.createElement("script_tag");
				}
				else{*/
					domElemNode = document.createElement(xmlNodeTagName);
				/*}*/
            }
            
            handleAttributes(domElemNode,xmlNode);
            //Fix for IE Script tag: Unexpected call to method or property access error
            //IE don't allow script tag to have child
            if(isIE && (!domElemNode.canHaveChildren) ){
                if(xmlNode.childNodes.length > 0){
                    domElemNode.text=xmlNode.text;
                }
                
            }                              
            else{
                for(var z = 0; z < xmlNode.childNodes.length; z++) {
                    var domChildNode=handleNode(xmlNode.childNodes[z]);
                    if(domChildNode!=null) {
                        domElemNode.appendChild(domChildNode);
						/* #7 Handling for IE9 */
						if( xmlNodeTagName == "option" ){
							if( isIE ){
								domElemNode.innerHTML = domElemNode.innerHTML.trim();
							}
						}
						/* end #7 Handling for IE9 */
                    }
                }
            }
        }  
		       
        return domElemNode;
    }
	
	// #7 Totally changed by ph 6-Oct-2010
	function handleHTMLNodes(cdataNode){  // Parameter: CDATA node
		// Set into a DIV innerHTML, mark the DIV as attribute taconite_ref="HTMLWrapper", and return it
		var HTML_wrapper = document.createElement("div");
		HTML_wrapper.setAttribute("taconite_ref", "HTMLWrapper");
		HTML_wrapper.innerHTML = ( isIE ? "<div taconite_ref=\"AddHandlerDiv\" style=\"display:none;\">&nbsp;</div>" : "" ) + cdataNode.data.substring(6) + ( isIE ? "<div taconite_ref=\"AddHandlerDiv\" style=\"display:none;\">&nbsp;</div>" : "" ); // Remove "|HTML|"
		return HTML_wrapper;
	}
    
    function useIEFormElementCreationStrategy(xmlNodeTagName) {
        var useIEStrategy = false;
        
        if (isIE && ( xmlNodeTagName.toLowerCase() == "form" ||
              xmlNodeTagName.toLowerCase() == "input" ||
              xmlNodeTagName.toLowerCase() == "textarea" ||
              xmlNodeTagName.toLowerCase() == "select" ||
              xmlNodeTagName.toLowerCase() == "a" ||
              xmlNodeTagName.toLowerCase() == "applet" ||
              xmlNodeTagName.toLowerCase() == "button" ||
              xmlNodeTagName.toLowerCase() == "img" ||
              xmlNodeTagName.toLowerCase() == "link" ||
              xmlNodeTagName.toLowerCase() == "map" ||
              xmlNodeTagName.toLowerCase() == "object")) {
                      
            useIEStrategy = true;
        }
        
        return useIEStrategy;
    }
    
    function createFormElementsForIEStrategy(xmlNode) {
        var attr = null;
        var name = "";
        var value = "";
        for (var x = 0; x < xmlNode.attributes.length; x++) {
            attr = xmlNode.attributes[x];
            name = attr.name.trim();
            if (name == "name") {
                value = attr.value.trim();
            }
        }

        domElemNode = document.createElement("<" + xmlNode.tagName + " name='" + value + "' />"); // e.g. document.createElement("<input name='slot2'>");
        
        return domElemNode;
    }
    
    function handleAttributes(domNode, xmlNode) {
        var attr = null;
        var attrString = "";
        var name = "";
        var value = "";
        var returnAsText = false;
        if(arguments.length == 3) {
            returnAsText = true;
        }
		var has_value_of_value = false; // #6
		var value_of_value = ""; // #6
        
        for(var x = 0; x < xmlNode.attributes.length; x++) {
            attr = xmlNode.attributes[x];
            name = cleanAttributeName(attr.name.trim());
            value = attr.value.trim();
            if(!returnAsText){
                if(name == "style") {
                    /* IE workaround */
                    domNode.style.cssText = value;
                    /* Standards compliant */
                    domNode.setAttribute(name, value);
                }
                else if(name.trim().toLowerCase().substring(0, 2) == "on") {
                    /* IE workaround for event handlers */
                    //domNode.setAttribute(name,value);							
					// Added by ph 9-Mar-2007.			
					value = getValidEventString(value);
                    eval("domNode." + name.trim().toLowerCase() + "=function(){" + value + "}");
                }
                else if(name == "value") {
                    /* IE workaround for the value attribute -- makes form elements selectable/editable */
                    // #6 Commented. domNode.value = value;
					has_value_of_value = true; // #6
					value_of_value = value; // #6
                }
                else if(useIEFormElementCreationStrategy(xmlNode.tagName) && name == "name") {
                    //Do nothing, as the "name" attribute was handled in the createFormElementsForIEStrategy function
                    continue;
                }
                else {
                    /* Standards compliant */
                    domNode.setAttribute(name,value);
                }
                /* class attribute workaround for IE */
                if(name == "class") {
                    domNode.setAttribute("className",value);
                }
            }else{
               	if(name.trim().toLowerCase().substring(0, 2) == "on") {
					// Added by ph 21-Mar-2007.			
					value = getValidEventString(value);
				}
			    attrString = attrString + name + "=\"" + value + "\" " ;
            }
        }
		// #6
		if( has_value_of_value ){
			domNode.setAttribute("value",value_of_value);
		}
		// end #6
        return attrString;
    }
	// Added by ph 9-Mar-2007. Main objective is for handling in IE. 
	// If found value = "function anonym(){ my_function() }"
	// then change it becomes value = "my_function()" only.
	function getValidEventString(value){
		value = value.trim();
		if( value.toLowerCase().substring(0, 9) == "function " ){
			var openSymbol = value.indexOf("{");
			var closeSymbol = value.lastIndexOf("}");
			if( openSymbol != -1 && closeSymbol != -1 && openSymbol < closeSymbol ) {
				value = value.substring(openSymbol+1, closeSymbol);
			}
			value = getValidEventString(value);
		}
		return value;
	}
    function getAppendAsFirstChild(domNode,xml){
        var firstNode=null;
        if(domNode.childNodes.length > 0) {
            firstNode=domNode.childNodes[0];
        }
        
        for(var i=0;i<xml.childNodes.length;i++){
            domChildNode=handleNode(xml.childNodes[i]);
            if(domChildNode!=null){
                if(firstNode==null){
					var script_array = handleHTMLWrapper1(domChildNode); /* #7 */
					if( isIE ){  // Added by ph 8-Mar-2007					
						var script_nodes = getIEScriptNodes(domChildNode);										
						domNode.appendChild(domChildNode);
						for(var k=0; k < script_nodes.length; k++){
							domNode.appendChild(script_nodes[k]);
						}
					}
					else{
						domNode.appendChild(domChildNode);
					}
					handleHTMLWrapper2(domChildNode); /* #7 */
					appendScriptTags(script_array, domNode); /* #7 */
                }
                else {
					var script_array = handleHTMLWrapper1(domChildNode); /* #7 */
					if( isIE ){  // Added by ph 8-Mar-2007		
						var script_nodes = getIEScriptNodes(domChildNode);										
						domNode.insertBefore(domChildNode,firstNode);	
						for(var k=0; k < script_nodes.length; k++){
							domNode.insertBefore(script_nodes[k],firstNode);
						}
					}
					else{
						domNode.insertBefore(domChildNode,firstNode);
					}
					handleHTMLWrapper2(domChildNode); /* #7 */
					appendScriptTags(script_array, domNode); /* #7 */
                }
                
            }
        }              
    }
    
    function getInsertAfter(domNode,xml){
        var domChildNode=null;
        var nextSibling=domNode.nextSibling;
        for(var i=0;i<xml.childNodes.length;i++){
            domChildNode=handleNode(xml.childNodes[i]);
            if(domChildNode!=null){
                if(nextSibling!=null) {
					var script_array = handleHTMLWrapper1(domChildNode); /* #7 */
					if( isIE ){  // Added by ph 8-Mar-2007
						var script_nodes = getIEScriptNodes(domChildNode);										
						domNode.parentNode.insertBefore(domChildNode,nextSibling);
						for(var k=0; k < script_nodes.length; k++){
							domNode.parentNode.insertBefore(script_nodes[k],nextSibling);
						}
					}
					else{
						domNode.parentNode.insertBefore(domChildNode,nextSibling);
					}
					handleHTMLWrapper2(domChildNode); /* #7 */
					appendScriptTags(script_array, domNode); /* #7 */
                }
                else {
					var script_array = handleHTMLWrapper1(domChildNode); /* #7 */
					if( isIE ){  // Added by ph 8-Mar-2007
						var script_nodes = getIEScriptNodes(domChildNode);									
						domNode.parentNode.appendChild(domChildNode);
						for(var k=0; k < script_nodes.length; k++){
							domNode.parentNode.appendChild(script_nodes[k]);
						}				
					}
					else{
						domNode.parentNode.appendChild(domChildNode);
					}
					handleHTMLWrapper2(domChildNode); /* #7 */
					appendScriptTags(script_array, domNode); /* #7 */
                }
            }
        }              
    }
    function getInsertBefore(domNode,xml){
        var domChildNode=null;
        for(var i=0;i<xml.childNodes.length;i++){
            domChildNode=handleNode(xml.childNodes[i]);
            if(domChildNode!=null) {
				var script_array = handleHTMLWrapper1(domChildNode); /* #7 */
				if( isIE ){  // Added by ph 8-Mar-2007
					var script_nodes = getIEScriptNodes(domChildNode);							
					domNode.parentNode.insertBefore(domChildNode,domNode);
					for(var k=0; k < script_nodes.length; k++){
						domNode.parentNode.insertBefore(script_nodes[k],domNode);
					}	
				}
				else{
					domNode.parentNode.insertBefore(domChildNode,domNode);
				}
				handleHTMLWrapper2(domChildNode); /* #7 */
				appendScriptTags(script_array, domNode); /* #7 */
            }
        }              
    }      
    function getReplace(domNode,xml){
        getInsertAfter(domNode,xml);
        domNode.parentNode.removeChild(domNode);
    }
    function getDelete(domNode) {
        domNode.parentNode.removeChild(domNode);
    }
    function getReplaceChildren(domNode,xml,doRemoveChildren) {
        var domChildNode=null;
        if(doRemoveChildren){
            while(domNode.childNodes.length >0){
                domNode.removeChild(domNode.childNodes[0]);
            }      
        }
        for(var i=0;i<xml.childNodes.length;i++){
            domChildNode=handleNode(xml.childNodes[i]);
            if(domChildNode!=null) {		
				var script_array = handleHTMLWrapper1(domChildNode); /* #7 */
				if( isIE ){  // Added by ph 8-Mar-2007					
					var script_nodes = getIEScriptNodes(domChildNode);								
					domNode.appendChild(domChildNode);	
					for(var k=0; k < script_nodes.length; k++){
						domNode.appendChild(script_nodes[k]);
					}				
				}
				else{
					domNode.appendChild(domChildNode);
				}
				handleHTMLWrapper2(domChildNode); /* #7 */
				appendScriptTags(script_array, domNode); /* #7 */
            }
        }              
    }
	
	// For IE only. Get the "script_tag" tags, convert to "script" tags, return as an array.
	// Remove the "script_tag" tags since no longer be used.
	function getIEScriptNodes(domChildNode){ // Added by ph 8-Mar-2007
		var script_nodes = new Array();
		var k = 0;
		for(var j=0; j<domChildNode.childNodes.length; j++){
			var script_nodes2 = getIEScriptNodes(domChildNode.childNodes[j]);
			script_nodes = script_nodes.concat(script_nodes2);
			k = script_nodes.length;
		}
		return script_nodes;
	}
    
    function handleRedirect(xmlNode) {
        var targetUrl = xmlNode.getAttribute("targetUrl");
        window.location.replace(targetUrl);
    }
    
    function executeJavascript(xmlNode) {
        var scripts = xmlNode.getElementsByTagName("script");
        for (var i = 0; i < scripts.length; i++) {
            var script = scripts[i];
            if (script.getAttribute("type") == "text/javascript") {
                var js = script.firstChild.nodeValue;
                eval(js);
            }
        }
    }

    function cleanAttributeName(name) {
        if(isIE == false) {
            return;
        }
        
        // IE workaround to change cellspacing to cellSpacing, etc
        var cleanName = name.toLowerCase();
        if(cleanName == "cellspacing") {
            cleanName = "cellSpacing";
        }
        else if(cleanName == "cellpadding") {
            cleanName = "cellPadding";
        }
        else if(cleanName == "colspan") {
            cleanName = "colSpan";
        }
        else if(cleanName == "tabindex") {
            cleanName = "tabIndex";
        }
        else if(cleanName == "readonly") {
            cleanName = "readOnly";
        }
        return cleanName;
    }
	
	/* #7 */
	function deleteElement(domNode){
		domNode.parentNode.removeChild(domNode);
	}

	function isFullHTMLContent(TaconiteNode){
		if( isIE ){ /* not for IE */
			return false;
		}
		if( TaconiteNode.childNodes.length > 0 ){
			for( var i=0; i<TaconiteNode.childNodes.length; i++ ){
				if( TaconiteNode.childNodes[i].nodeType == 1 ){
					return false;
				}
			}
			return true;
		}
		else{
			return true;
		}
	}
	
	function populateHTMLContent(xml){
		var contentHTML = "";
		for( var i=0; i<xml.childNodes.length; i++ ){
			switch( xml.childNodes[i].nodeType ){
				case 3:	contentHTML += xml.childNodes[i].nodeValue;
						break;
				case 4:	if( xml.childNodes[i].data.substring(0, 6) == "|HTML|" ){
							contentHTML += ( isIE ? "<div taconite_ref=\"AddHandlerDiv\" style=\"display:none;\">&nbsp;</div>" : "" ) + xml.childNodes[i].data.substring(6) + ( isIE ? "<div taconite_ref=\"AddHandlerDiv\" style=\"display:none;\">&nbsp;</div>" : "" );
						}
						else{
							var data_temp = xml.childNodes[i].data.replace(/&/g, "&amp;");
							data_temp = data_temp.replace(/</g, "&lt;");
							data_temp = data_temp.replace(/>/g, "&gt;");
							data_temp = data_temp.replace(/"/g, "&quot;");
							contentHTML += data_temp;
						}
			}
		}
		return contentHTML;
	}
	
	function checkNSetAttribute(ori_ele, target_ele, attr){
		if( ori_ele.getAttribute(attr) ){
				if( ori_ele.getAttribute(attr) != "" ){
				target_ele.setAttribute(attr, ori_ele.getAttribute(attr));
			}
		}
	}
	
	function getReplaceChildrenHTML(domNode,xml,doRemoveChildren){
		var HTMLContent_temp = populateHTMLContent(xml);
		var processedHTML = processHTMLContent(HTMLContent_temp);
		if( doRemoveChildren ){
			domNode.innerHTML = processedHTML.HTMLContent;
		}
		else{
			domNode.innerHTML += processedHTML.HTMLContent;
		}
		appendScriptTags(processedHTML.scriptArray, domNode);
	}
	
	function getAppendAsFirstChildHTML(domNode,xml){
		var HTMLContent_temp = populateHTMLContent(xml);
		var processedHTML = processHTMLContent(HTMLContent_temp);
		domNode.innerHTML = contentHTML + processedHTML.HTMLContent;
		appendScriptTags(processedHTML.scriptArray, domNode);
	}
	
	/* Get script tags and HTMLContent without script tags. */
	function processHTMLContent(contentHTML){
		
		var result = new Object();
		result.scriptArray = new Array();
		result.HTMLContent = "";
		
		var div_temp = document.createElement("div");
		div_temp.innerHTML = contentHTML;
		
		result.scriptArray = extractScriptTag(div_temp);
		if( isIE ){
			delete_AddHandlerDiv(div_temp);
		}
		result.HTMLContent = div_temp.innerHTML;
		
		return result;
	}
	
	/* Take script tag out from the element, and store into an array. */
	function extractScriptTag(div_temp){
	
		var x = div_temp.getElementsByTagName("script");
		var total_ele = x.length;
		var scriptArray = new Array();
		
		for( var i=0; i < total_ele; i++){
			scriptArray[i] = document.createElement("script");
			checkNSetAttribute(x[i], scriptArray[i], "type");
			checkNSetAttribute(x[i], scriptArray[i], "language");
			checkNSetAttribute(x[i], scriptArray[i], "src");
			scriptArray[i].text = x[i].text;
			
		}
		for( var i=0; i < total_ele; i++){
			deleteElement(x[0]);
		}
		return scriptArray;
	}
	
	/* Remove the additional handler DIVs which are only for IE. */
	function delete_AddHandlerDiv(div_temp){
		var x = div_temp.getElementsByTagName("div");
		var total_ele = x.length;
		for( var i=(total_ele-1); i >= 0; i--){
			if( x[i].nodeType == 1 ){
				if( x[i].getAttribute("taconite_ref") == "AddHandlerDiv" ){
					div_temp.removeChild(x[i]);
				}
			}
		}
	}
	
	function appendScriptTags(script_array, append_JS_ele){
		if( script_array ){
			for( var i=0; i < script_array.length; i++ ){
				append_JS_ele.appendChild(script_array[i]);
			}
		}
	}
	
	/* Check if it's HTMLWrapper, take script tag out from HTMLWrapper, and store into an array. */
	function handleHTMLWrapper1(wrapperNode){

		if( wrapperNode.nodeType == 1 ){
			if( wrapperNode.getAttribute("taconite_ref") == "HTMLWrapper" ){
				var scriptArray = extractScriptTag(wrapperNode);
				return scriptArray;
			}
		}
		return null;
	}
	
	/* Check if it's HTMLWrapper, move out all the elements inside the HTMLWrapper, and delete the HTMLWrapper. */
	function handleHTMLWrapper2(wrapperNode){
		if( wrapperNode.nodeType == 1 ){
			if( wrapperNode.getAttribute("taconite_ref") == "HTMLWrapper" ){
				if( isIE ){
					delete_AddHandlerDiv(wrapperNode);
				}
				while(wrapperNode.childNodes.length > 0){
					wrapperNode.parentNode.insertBefore(wrapperNode.childNodes[0], wrapperNode);
				}
				deleteElement(wrapperNode);
			}
		}
	}
	/* end #7 */
    
}

