// JavaScript Document
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 XhtmlToDOMParser(xml){
	var xmlTagName=xml.tagName.toLowerCase();
	var contextNode=document.getElementById(xml.getAttribute("contextNodeID"));
		this.startParsing = function(){
        switch (xmlTagName) {
            case "taconite-append-as-children":
                getReplaceChildren(contextNode,xml,false);
				break;
            case "taconite-delete":
                getDelete(contextNode,xml);
                break;
            case "taconite-append-as-first-child":
                getAppendAsFirstChild(contextNode,xml);
                break;				
            case "taconite-insert-after":
                getInsertAfter(contextNode,xml);
                break;
            case "taconite-insert-before":
                getInsertBefore(contextNode,xml);
                break;				
            case "taconite-replace-children":
                getReplaceChildren(contextNode,xml,true);
                break;
            case "taconite-replace":
               getReplace(contextNode,xml);
                break;				
        }			
	}
	function isIE() {
		if(document.uniqueID) {
			
			return true;
		}
		else {
			return false;
		}
	}	
    function isRadioButton(node) {
        if(!node.tagName.trim().toLowerCase() == "input") {
            return false;
        }

        var attrs = node.attributes;
        var attr = null;
        for(var a = 0; a < attrs.length; a++) {
            attr = attrs[a];
           
            if(attr.name.toLowerCase().trim() == "type") {
                if(attr.value.toLowerCase().trim() == "radio") {
                    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);
				break;
			case 3:  //TEXT_NODE
			   return handleTextNode(xmlNode);
			   break;
			case 4:  //CDATA_SECTION_NODE
				break;
		}	
		return null;
	}
	function handleElement(xmlNode){
		var domElemNode=null;
		if(isIE() && isRadioButton(xmlNode)){
			domElemNode=document.createElement("<INPUT " + handleAttributes(domElemNode,xmlNode,true) + ">");
		}
		if(domElemNode==null){
			domElemNode=document.createElement(xmlNode.tagName.toLowerCase());
			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() && xmlNode.tagName.toLowerCase() == "script"){
				if(xmlNode.childNodes.length==1){
					var text=xmlNode.childNodes[0].nodeValue;
					var regExp = /\s+/g; 
					text = text.replace(regExp, " "); 
					domElemNode.text=text;
				}
	
			}				
			else{
				for(var z = 0; z < xmlNode.childNodes.length; z++) {
				   var domChildNode=handleNode(xmlNode.childNodes[z]);
				   if(domChildNode!=null)
					   	domElemNode.appendChild(domChildNode);
				}
			}
		}	
	
		return domElemNode;
	}
	function handleTextNode(xmlNode){
		var text = xmlNode.nodeValue;
		//Replace all consecutive whitespace chars with a single space
		var regExp = /\s+/g; 
		text = text.replace(regExp, " "); 	
		var textNode=document.createTextNode(text);
		return textNode;
	}
	function handleAttributes(domNode,xmlNode) {
		var attr = null;
		var attrString = "";
		var name = "";
		var value = "";
		var returnAsText=false;
		if(arguments.length==3)
			returnAsText=true;

		for(var x = 0; x < xmlNode.attributes.length; x++) {
			attr = xmlNode.attributes[x];
			name = 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);
					eval("domNode." + name.trim().toLowerCase() + "=function(){" + value + "}");
				}
				else {
					/* Standards compliant */
					domNode.setAttribute(name,value);
					
				}
					/* class attribute workaround for IE */
				if(name == "class") {
					domNode.setAttribute("className",value);
				}
			}else{
				attrString = attrString + name + "='" + value + "' " ;
			}
		}
		return attrString;
	}
    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){
					domNode.appendChild(domChildNode);
					firstNode=domChildNode;
				}
				else
					domNode.insertBefore(domChildNode,firstNode);
				
			}
		}		
	}

	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(nextSibling!=null)
				domNode.parentNode.insertBefore(domChildNode,nextSibling);
			else
				domNode.parentNode.appendChild(domChildNode);
			
		}		
	}
	function getInsertBefore(domNode,xml){
		var domChildNode=null;
		for(var i=0;i<xml.childNodes.length;i++){
			domChildNode=handleNode(xml.childNodes[i]);
			if(domChildNode!=null)
				domNode.parentNode.insertBefore(domChildNode,domNode);
		}		
	}	
	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(contextNode.childNodes.length >0){
				contextNode.removeChild(contextNode.childNodes[0]);
			}	
		}
		for(var i=0;i<xml.childNodes.length;i++){
			domChildNode=handleNode(xml.childNodes[i]);
			if(domChildNode!=null)
				domNode.appendChild(domChildNode);
		}		
	}
	
}
