/* =================================================================== */

/* 	Global Variable */
var MENU_TAG 			= 'menu';
var MENUITEM_TAG		= 'menuitem';
var SUBMENU_TAG    		= 'submenu';
var TITLE_TAG			= 'title';
var URL_TAG			= 'url';
var CHILDITEM_TAG		= 'childitem';
var CSSCLASS_TAG		= 'css_class';
var H2_TAG			= 'h2_enclosed';
var LEVEL_TAG                   = 'level';
var ALIAS_TAG                   = 'alias';
var SHOW_IN_TOP_MENU            = 'top_menu';
	

function loadXML(xmlfile) {
    if (window.XMLHttpRequest){
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }else{
        // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET",xmlfile,false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML;
	
    return xmlDoc;
}

/*
	use parseTopMenuElement function to parse and generate html code 
	for HDC top navigation menu. This function is really tight couples
	with HDC css, so actually this function doesn't meet any general menu
	generator by now :).
 */
function parseTopMenuElement(fromelement){	
    /* 	wrap the menu list with menu div css */
    document.write('<div class=\"grid_10 alpha\">');
    document.write('<div class=\"primary_navigation\">');
	
    if(fromelement.nodeName == MENU_TAG){
        var topmenu = getElByName(SUBMENU_TAG, getElByName(MENUITEM_TAG, fromelement));
        if(topmenu.hasChildNodes()) {
            document.write('<ul id=\"topnav\">');
            for(var i=0; i < topmenu.childNodes.length; i++) {
                if(getValueOfElement(SHOW_IN_TOP_MENU, topmenu.childNodes[i]) == 'true'){
                    buildTopMenuItem(topmenu.childNodes[i]);
                }                
            }
            document.write('</ul>');
        }
    }else{
        document.write('UNKNOWN FORMAT XML');
        return;
    }
	
    /* 	end wrap the menu list with menu div css */
    document.write('</div>'); /* end div primary_navigation */
    document.write('</div>'); /* end div grid_10 alpha */
}



function buildTopMenuItem(element){
    if(element.nodeType == 1){
        if(getValueOfElement(SHOW_IN_TOP_MENU, element) != 'false'){
            if(getValueOfElement(CSSCLASS_TAG, element) == 'business'){
                /* 		document.write('<ul>'); */
                document.write('<li>');
                document.writeln('<a href=\"'+
                    getValueOfElement(URL_TAG, element)+
                    '\" class=\"'+
                    getValueOfElement(CSSCLASS_TAG, element)+
                    '\">'+
                    getValueOfElement(TITLE_TAG, element) + '</a>');
							
                document.write('<div class=\"sub sub_business\" id=\"sub_business\" style=\"width: 750px;\">');
		
                for(var k = 0; k < element.childNodes.length; k++) {
                    if(element.childNodes[k].tagName == SUBMENU_TAG){
                        var submenuelm = element.childNodes[k];
                        for(var l = 0; l < submenuelm.childNodes.length; l++){
                            if(submenuelm.childNodes[l].tagName == MENUITEM_TAG){
                                if(getValueOfElement(SHOW_IN_TOP_MENU, submenuelm.childNodes[l]) != 'false'){
                                    document.write('<ul>');
                                    buildTopMenuItem(submenuelm.childNodes[l]);
                                    document.write('</ul>');
                                }
                            }
                        }
                    }
                }
                document.write('</div>');
                document.write('</li>');

            }else if(getValueOfElement(CSSCLASS_TAG, element) == 'consumer'){
                /* 		document.write('<ul>'); */
                document.write('<li>');
                document.writeln('<a href=\"'+
                    getValueOfElement(URL_TAG, element)+
                    '\" class=\"'+
                    getValueOfElement(CSSCLASS_TAG, element)+
                    '\">'+
                    getValueOfElement(TITLE_TAG, element) + '</a>');
							
                document.write('<div class=\"sub sub_consumer\" id=\"sub_consumer\" style=\"width: 600px;\">');
		
                for(var k = 0; k < element.childNodes.length; k++) {
                    if(element.childNodes[k].tagName == SUBMENU_TAG){
                        var submenuelm = element.childNodes[k];
                        for(var l = 0; l < submenuelm.childNodes.length; l++){                        
                            if(submenuelm.childNodes[l].tagName == MENUITEM_TAG){
                                if(getValueOfElement(SHOW_IN_TOP_MENU, submenuelm.childNodes[l]) != 'false'){
                                    document.write('<ul>');
                                    buildTopMenuItem(submenuelm.childNodes[l]);
                                    document.write('</ul>');
                                }
                            }                        
                        }
                    }
                }
                document.write('</div>');
                document.write('</li>');
				
            }else if(getValueOfElement(CSSCLASS_TAG, element) == 'government'){
		
                /* 		document.write('<ul>'); */
                document.write('<li>');
                document.writeln('<a href=\"'+
                    getValueOfElement(URL_TAG, element)+
                    '\" class=\"'+
                    getValueOfElement(CSSCLASS_TAG, element)+
                    '\">'+
                    getValueOfElement(TITLE_TAG, element) + '</a>');
							
                document.write('<div class=\"sub sub_government\" id=\"sub_government\" style=\"width: 450px;\">');
		
                for(var k = 0; k < element.childNodes.length; k++) {
                    if(element.childNodes[k].tagName == SUBMENU_TAG){
                        var submenuelm = element.childNodes[k];
                        for(var l = 0; l < submenuelm.childNodes.length; l++){
                        
                            if(submenuelm.childNodes[l].tagName == MENUITEM_TAG){
                                if(getValueOfElement(SHOW_IN_TOP_MENU, submenuelm.childNodes[l]) != 'false'){
                                    document.write('<ul>');
                                    buildTopMenuItem(submenuelm.childNodes[l]);
                                    document.write('</ul>');
                                }
                            }
                        
                        }
                    }
                }
                document.write('</div>');
                document.write('</li>');
            }else{
            
                document.write('<li>');
            
                if(getValueOfElement(H2_TAG, element) == 'true'){
                    document.write('<h2>');
                }
			
                document.writeln('<a href=\"'+
                    getValueOfElement(URL_TAG, element)+'\">'+
                    getValueOfElement(TITLE_TAG, element) + '</a>');
						
							
                if(getValueOfElement(H2_TAG, element) == 'true'){
                    document.write('</h2>');
                }
							
                for(var k = 0; k < element.childNodes.length; k++) {
                    if(element.childNodes[k].tagName == SUBMENU_TAG){
                        var submenuelm = element.childNodes[k];
                        for(var l = 0; l < submenuelm.childNodes.length; l++){
                            if(getValueOfElement(SHOW_IN_TOP_MENU, submenuelm.childNodes[l]) != 'false'){
                                document.write('<ul>');
                                buildTopMenuItem(submenuelm.childNodes[l]);
                                document.write('</ul>');
                            }
                        
                        }
                    }
                }
                document.write('</li>');
            }
        }
    }
}

function findElmByPageAlias(alias, element){
    if(element.hasChildNodes()){
        for (var i = 0; i < element.childNodes.length; i++) {
            var node = element.childNodes.item(i);
            if(node.nodeType == 1){                
                if (getValueOfElement(ALIAS_TAG, node) == alias) {
                    if (getValueOfElement(LEVEL_TAG, node) == '2') {
                        buildDirectSideMenu(node, alias);
                    } else if (getValueOfElement(LEVEL_TAG, node) == '3') {
                        buildDirectSideMenu(node.parentNode.parentNode, alias);
                    } else if (getValueOfElement(LEVEL_TAG, node) == '4') {
                        buildDirectSideMenu(node.parentNode.parentNode.parentNode.parentNode, alias);
                    } else if (getValueOfElement(LEVEL_TAG, node) == '5') {
                        buildDirectSideMenu(node.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode, alias);
                    } 
                }else{
                    if(getElByName(SUBMENU_TAG, node) != null){
                        findElmByPageAlias(alias, getElByName(SUBMENU_TAG, node));
                    }
                }
            }        
        }        
    }
}


function findElmSideMenuKC(alias, element){
    if(element.hasChildNodes()){
        for (var i = 0; i < element.childNodes.length; i++) {
            var node = element.childNodes.item(i);
            if(node.nodeType == 1){
                if (getValueOfElement(ALIAS_TAG, node) == alias) {
                    if (getValueOfElement(LEVEL_TAG, node) == '1') {
                        buildDirectSideMenu(node, alias);
                    } else if (getValueOfElement(LEVEL_TAG, node) == '2') {
                        buildDirectSideMenu(node.parentNode.parentNode, alias);
                    } else if (getValueOfElement(LEVEL_TAG, node) == '3') {
                        buildDirectSideMenu(node.parentNode.parentNode.parentNode.parentNode, alias);
                    } else if (getValueOfElement(LEVEL_TAG, node) == '4') {
                        buildDirectSideMenu(node.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode, alias);
                    }
                }else{
                    if(getElByName(SUBMENU_TAG, node) != null){
                        findElmSideMenuKC(alias, getElByName(SUBMENU_TAG, node));
                    }
                }
            }
        }
    }
}



/***
 * Create breadcrumb navigation from page alias.
 * page = inside/main
 */
function createBreadCrumb(alias, element){
    if(element.hasChildNodes()){
        for (var i = 0; i < element.childNodes.length; i++) {
            var node = element.childNodes.item(i);
            if(node.nodeType == 1){
                if (getValueOfElement(ALIAS_TAG, node) == alias) {
                    buildBreadCrumb(node);
                }else{
                    if(getElByName(SUBMENU_TAG, node) != null){
                        createBreadCrumb(alias, getElByName(SUBMENU_TAG, node));
                    }
                }
            }
        }
    }
}

/***
 * page = inside/main
 */
function buildBreadCrumb(node){    
    if (getValueOfElement(LEVEL_TAG, node) == '1') {
        var parent = node.parentNode.parentNode;
        document.write('<a href=\"');
        document.write(getValueOfElement(URL_TAG, parent)+'\">');
        document.write(getValueOfElement(TITLE_TAG, parent));
        document.write('</a>');
        document.write(' / ');
        document.write(getValueOfElement(TITLE_TAG, node));
    }else if (getValueOfElement(LEVEL_TAG, node) == '2') {
        var parent = node.parentNode.parentNode;
        var grandParent = node.parentNode.parentNode.parentNode.parentNode;
        document.write('<a href=\"');
        document.write(getValueOfElement(URL_TAG, grandParent)+'\">');
        document.write(getValueOfElement(TITLE_TAG, grandParent));
        document.write('</a>');
        document.write(' / ');
        document.write('<a href=\"');
        document.write(getValueOfElement(URL_TAG, parent)+'\">');
        document.write(getValueOfElement(TITLE_TAG, parent));
        document.write('</a>');
        document.write(' / ');
        document.write(getValueOfElement(TITLE_TAG, node));
    } else if (getValueOfElement(LEVEL_TAG, node) == '3') {
        var parent = node.parentNode.parentNode;
        var grandParent = node.parentNode.parentNode.parentNode.parentNode;
        var superGrandParent = node.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
        document.write('<a href=\"');
        document.write(getValueOfElement(URL_TAG, superGrandParent)+'\">');
        document.write(getValueOfElement(TITLE_TAG, superGrandParent));
        document.write('</a>');
        document.write(' / ');
        document.write('<a href=\"');
        document.write(getValueOfElement(URL_TAG, grandParent)+'\">');
        document.write(getValueOfElement(TITLE_TAG, grandParent));
        document.write('</a>');
        document.write(' / ');
        document.write('<a href=\"');
        document.write(getValueOfElement(URL_TAG, parent)+'\">');
        document.write(getValueOfElement(TITLE_TAG, parent));
        document.write('</a>');
        document.write(' / ');
        document.write(getValueOfElement(TITLE_TAG, node));
    }else if (getValueOfElement(LEVEL_TAG, node) == '4') {
        var parent = node.parentNode.parentNode;
        var grandParent = node.parentNode.parentNode.parentNode.parentNode;
        var superGrandParenta = node.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
        var superGrandParentb = superGrandParenta.parentNode.parentNode;
	 document.write('<a href=\"');
        document.write(getValueOfElement(URL_TAG, superGrandParentb)+'\">');
        document.write(getValueOfElement(TITLE_TAG, superGrandParentb));
        document.write('</a>');
	 document.write(' / ');
        document.write('<a href=\"');
        document.write(getValueOfElement(URL_TAG, superGrandParenta)+'\">');
        document.write(getValueOfElement(TITLE_TAG, superGrandParenta));
        document.write('</a>');
        document.write(' / ');
        document.write('<a href=\"');
        document.write(getValueOfElement(URL_TAG, grandParent)+'\">');
        document.write(getValueOfElement(TITLE_TAG, grandParent));
        document.write('</a>');
        document.write(' / ');
        document.write('<a href=\"');
        document.write(getValueOfElement(URL_TAG, parent)+'\">');
        document.write(getValueOfElement(TITLE_TAG, parent));
        document.write('</a>');
        document.write(' / ');
        document.write(getValueOfElement(TITLE_TAG, node));
    }
}

/***
 * Build side menu from 'menuitem' tag
 */
function buildDirectSideMenu(element, alias){
    if(element.hasChildNodes()){
        if(element.nodeType == 1){
            document.write('<ul>');
            document.write('<h5>');
            document.write('<a href=\"');
            document.write(getValueOfElement(URL_TAG, element)+'\">');
            document.write(getValueOfElement(TITLE_TAG, element));
            document.write('</a>');
            document.write('</h5>');
            if (getElByName(SUBMENU_TAG, element) != null) {
                buildSideMenuItem(getElByName(SUBMENU_TAG, element), alias);
            }
            document.write('<ul>');
        }        
    }
}

/***
 * Build side menu from 'menu' tag..
 *
 */
function buildSideMenu(element){
    if(element.hasChildNodes()){
        for (var i = 0; i < element.childNodes.length; i++) {
            var node = element.childNodes.item(i);
            if(node.nodeType == 1){
                document.write('<ul>');
                document.write('<h5>');
                document.write('<a href=\"');
                document.write(getValueOfElement(URL_TAG, node)+'\">');
                document.write(getValueOfElement(TITLE_TAG, node));
                document.write('</a>');
                document.write('</h5>');
                if (getElByName(SUBMENU_TAG, node) != null) {
                    buildSideMenuItem(getElByName(SUBMENU_TAG, node), '');
                }
                document.write('<ul>');
            }            
        }
    }
}

function buildSideMenuItem(element, selectedItem){
    var submenu = element;
    for(var j = 0; j < submenu.childNodes.length; j++){
        var subNode = submenu.childNodes.item(j);
        if (subNode.nodeType == 1) {
            document.write('<li>'+'<a href=\"');
            document.write(getValueOfElement(URL_TAG, subNode)+'\">');
            if(selectedItem == getValueOfElement(ALIAS_TAG, subNode)){
                document.write('<strong>');
            }
            document.write(getValueOfElement(TITLE_TAG, subNode));
            if(selectedItem == getValueOfElement(ALIAS_TAG, subNode)){
                document.write('</strong>');
            }
            document.write('</a>');
            if (getElByName(SUBMENU_TAG, subNode) != null) {
                var subNodeSubMenu = getElByName(SUBMENU_TAG, subNode);
                document.write('<ul>');
                buildSideMenuItem(subNodeSubMenu, selectedItem);
                document.write('</ul>');
            }
            document.write('</li>');
        }
    }

}

/* -------- */


function parseMenuByElementName(elname, fromelement){
    if(fromelement.hasChildNodes()) {
        for(var i=0; i < fromelement.childNodes.length; i++) {
            if(fromelement.childNodes[i].nodeType == 1){	// if nodeType = ELEMENT_TYPE
                if(fromelement.childNodes[i].tagName == SUBMENU_TAG){
                    var submenufromelm = fromelement.childNodes[i];
                    //                if(fromelement.childNodes[i].tagName == MENUITEM_TAG){
                    for(var y = 0; y < submenufromelm.childNodes.length; y++){
                        if(submenufromelm.childNodes[y].tagName == MENUITEM_TAG){
                            if(submenufromelm.childNodes[y].getAttribute("id") == elname){
                                document.write('<ul>');
                                document.write('<h5>'+ getValueOfElement(TITLE_TAG, submenufromelm.childNodes[y]) +'</h5>');

                                var flist = submenufromelm.childNodes[y];

                                if(flist.hasChildNodes()){
                                    for(var j=0; j < flist.childNodes.length; j++){
                                        if(flist.childNodes[j].nodeType == 1){	// if nodeType = ELEMENT_TYPE
                                            //                                    if(flist.childNodes[j].tagName == MENUITEM_TAG){
                                            //                                        buildSideMenuItem(flist.childNodes[j]);
                                            //                                    }

                                            if(flist.childNodes[j].tagName == SUBMENU_TAG){
                                                var submenuelm = flist.childNodes[j];
                                                for(var l = 0; l < submenuelm.childNodes.length; l++){
                                                    if(submenuelm.childNodes[l].tagName == MENUITEM_TAG){
                                                        document.write('<ul>');
                                                        buildSideMenuItem(submenuelm.childNodes[l], '');
                                                        document.write('</ul>');
                                                    }
                                                }
                                            }

                                        }
                                    } // end for
                                }

                                document.write('</ul>');
                            }//RIFAI MARK
                        }
                        else{ 	// if element has a child then probe it to find
                            if(submenufromelm.childNodes[y].hasChildNodes()){
                                parseMenuByElementName(elname, submenufromelm.childNodes[y]); // recursive search
                            }
                        }
                    }
                } // end if == MENUITEM_TAG
            }
        } // end for
    }
}



/*function buildSideMenuItem(element){
    document.write('<li>');
    document.writeln('<a href='+
        getValueOfElement(URL_TAG, element)+'>'+
        getValueOfElement(TITLE_TAG, element) + '</a>');
    for(var k = 0; k < element.childNodes.length; k++) {
        if(element.childNodes[k].tagName == MENUITEM_TAG){
            document.write('<ul>');
            buildSideMenuItem(element.childNodes[k]);
            document.write('</ul>');
        }
    }
    document.write('</li>');
}*/



/* ---------- */





function getElByName(name, element){
    for(var i=0; i < element.childNodes.length; i++) {
        if(element.childNodes[i].nodeType == 1){
            if(element.childNodes[i].tagName == name){
                return element.childNodes[i];
            }
        }
    }
    // if not found return NULL
    return null;
}



function getValueOfElement(name, element){
    //document.writeln('<br><i>'+element.tagName+'</i><br>');
    for(var i=0; i < element.childNodes.length; i++) {
        if(element.childNodes[i].nodeType == 1){
            if(element.childNodes[i].tagName == name){
                //document.writeln('<br>ELEMENT '+ name + ' = '+ element.childNodes[i].firstChild.nodeValue +' ]');
                if(element.childNodes[i].firstChild == null){
                    return '&nbsp;';
                }else{
                    return element.childNodes[i].firstChild.nodeValue;
                }
            }
        }
    }
    
    // if not found return NULL
    return null;
}







/* =================================================================== */

