/////////////////////////////////////////////////////////////////////////////
// Function : nodelink
// Comments :
/////////////////////////////////////////////////////////////////////////////
function craftnodelink(nodeId,siteId)
{
	var newUrl = "";
	newUrl += g_httpCgiUrl;

	if (typeof nodeId != _U && nodeId != 0)
	{
		newUrl += "nodeId=" + nodeId;
	}

	if( typeof siteId != _U && siteId != null )
	{
		newUrl += "&siteId=" + siteId;
	}

	var currentUrl = "" + window.location;

	// Continue propagation of the "SSContributor" parameter
	if( SSContributor )
	{
		var paramName = "SSContributor=";
		var pos = currentUrl.indexOf( paramName );
		if( pos != -1 )
		{
			var subStr = currentUrl.substring( pos+paramName.length, pos+paramName.length+1 );
			if( isTrue( subStr ) )
			{
				newUrl += "&" + paramName + "true";
			}
		}
	}

	// Continue propagation of the "previewId" parameter
	var previewId = "previewId=";
	var posStart = currentUrl.indexOf( previewId );
	if( posStart != -1 )
	{
		var posEnd = currentUrl.indexOf("&", posStart);
		var value = "";
		if (posEnd == -1)
		{
			value = currentUrl.substring(posStart+previewId.length, currentUrl.length);
		}
		else
		{
			value = currentUrl.substring(posStart+previewId.length, posEnd);
		}
		newUrl += "&" + previewId + value;
	}

	// We now always add the node that the link is on as part of the URl also.
	if( typeof g_ssSourceNodeId != _U && g_ssSourceNodeId != null )
		newUrl += "&ssSourceNodeId=" + g_ssSourceNodeId;

	if( typeof g_ssSourceSiteId != _U && g_ssSourceSiteId != null )
		newUrl += "&ssSourceSiteId=" + g_ssSourceSiteId;

	if( typeof ssUrlPrefix != _U && ssUrlPrefix != null )
		newUrl += "&ssUrlPrefix=" + ssUrlPrefix;

	// return the constructed url
	return newUrl;
}


function DCLNAV_Change(dclListChanged, fragid, id)
{
	try{
		// Declare variables, note there is a real possiblity these could be null
		var dclNavFormObject = document.forms['DCLNavForm_' + fragid];
		var dclListNext = dclNavFormObject.elements['DCLNAV_' + fragid + '_' + (id + 1)];
	
		var dclNavChangedGoImg = document.getElementById('DCLNAV_GO_' + fragid + '_' + (id));	
	//var dclNavNextGoImg = document.getElementById['DCLNAV_GO_' + fragid + '_' + (id + 1)];
//alert(dclNavChangedGoImg);
//alert(dclNavChangedGoImg.src);
//alert(dclNavNextGoImg.src);
	// Before doing anything with the list that changed lets make sure it isn't null, JUST in case
		if (dclListChanged != null)
		{
			// If the list that changed was put back to the first item in the list then turn off the go button
			if (dclListChanged.selectedIndex < 1)
			{
				// Turing off the go button
				dclNavChangedGoImg.src = DCLNAV_GoOff;
				
			//alert(dclNavChangedGoImg.src);
				// Attempting to turn off all sublists as well
				while (dclListNext != null)
				{
					dclListNext.options.length = 0;
					var dclNavTempGo = eval("document.DCLNAV_GO_" + (id + 1));
					dclNavTempGo.src = DCLNAV_GoOff;
					id += 1;
					dclListNext = dclNavFormObject.elements['DCLNAV_' + (id + 1)];
				}			
			}
			else
			{   	
				dclNavChangedGoImg.src = DCLNAV_GoOn;
			
				if (dclListNext != null  && dclListChanged.selectedIndex > 0)
				{				
					var targetList = null;
					var targetText = null;
	
					try
					{ 
						targetList = eval("DCLNAV_list_" + dclListChanged.options[dclListChanged.selectedIndex].value);
						targetText = eval("DCLNAV_text_" + dclListChanged.options[dclListChanged.selectedIndex].value);
		
					}
					catch (err)
					{
						targetList = null;
						targetText = null;
					}
					
					if (targetList != null)
					{ 
						dclListNext.options.length = 0;                    
						dclListNext.options[dclListNext.options.length] = new Option("Select Sub-Category", "");
						for (var i = 0; i < targetList.length; i++)
						{                         
							dclListNext.options[dclListNext.options.length] = new Option(targetText[i], targetList[i]);		
						}
	
						//dclNavNextGoImg.src = DCLNAV_GoOff;
						
						DCLNAV_FlushLists(dclNavFormObject, id + 1, fragid);
					}
					else
					{
						DCLNAV_FlushLists(dclNavFormObject, id, fragid);
					}
					
				}
			}
		}
		else
		{
			alert("The list that changed and called the DCLNAV_Change function is now null, how did this happen?  Contact support.");
		}
	}catch(err){}
}

function DCLNAV_FlushLists(form, id, fragid)
{
	try{
		var targetImg = form.elements['DCLNAV_GO_' + fragid + '_' + (++id)];
		var targetDCL = form.elements['DCLNAV_' + fragid + '_' + (id)];
        while (targetDCL != null)
        {
            targetImg.src = DCLNAV_GoOff;
            targetDCL.options.length = 0;
            targetImg = form.elements['DCLNAV_GO_' + fragid + '_' + (++id)];
            targetDCL = form.elements['DCLNAV_' + fragid + '_' + (id)];
        }
    }catch(err){
        // catching errors. this fixes errors that were showing up in firefox.
    }
}
/*
 * This function checks to see if cp_cspDclPageNonNavigable is set to true
 * if it is, o normal processing else
 * disabled GO button image and set the url to not go anywhere.
 * id = node id for the given section
 * fragid = form's [that is currently being accessed] fragment id.
 */
function DCLNav_checkNavDrivenUrl(form, fragid, counter){
	try{
		    var id = form.value;
	    var propValue = getNavNodeObjectPropResult(g_navNode_Root, id);
	    var targetDCLhref = document.getElementById('DCLNAV_HREF_' + fragid + '_' + id);
	    var tempGoImage = document.getElementById('DCLNAV_GO_' + fragid + '_' + counter);

	    if (propValue.toLowerCase() != 'true'){
	        //enable button image
	        tempGoImage.src = DCLNAV_GoOn;
	    }else{
	        //disable button image
	        tempGoImage.src = DCLNAV_GoOff;         
	    }
    }catch(err){}
}

/*
 * this function takes a node and an node id and
 * goes through the navigation searching for the node id in the
 * passed in node's children
 */
function getRecursiveNode(node, id){
    var returnNode = null;
    try{
	    if(node.m_id == id){
	        returnNode = node;
	    }else{
	        for(var i = 0, len = node.m_subNodes.length; i<len; i++){
	            returnNode = getRecursiveNode(node.m_subNodes[i],id);
	            if(returnNode != null){
	                break;
	            }
	        }
	    }
    }catch(err){}
    return returnNode;
}
/*
 * this takes a node and and id and finds if the custom section
 * property cspDclPageNonNavigable is set to true
 */
function getNavNodeObjectPropResult(node,id){
    var node = getRecursiveNode(node,id);
    var returnValue = 'false';
    
    if(node){
        var val = node.cp_cspDclPageNonNavigable;
        returnValue = getCustomSectionProperty(val);
    }
    return returnValue;
}

function DCLNAV_Click_Go(id, fragid)
{
	try{
		// Declare variables, note there is a real possiblity these could be null
		var dclNavFormObject = document.forms['DCLNavForm_' + fragid];
		//var dclNavImg = dclNavFormObject.elements['DCLNAV_GO_' + fragid + '_' + id];
		var dclNavImg = document.getElementById('DCLNAV_GO_' + fragid + '_' + id);	
		var dclNavList = dclNavFormObject.elements['DCLNAV_' + fragid + '_' + id];
	    //alert(dclNavImg);
	    
	    
	    var imgLoc = dclNavImg.src;
	// Is the go button turned ON?
	//if (dclNavImg.src.indexOf("blueongray") > 0)
	//{
		// The go button is on
    
            if(imgLoc.indexOf(DCLNAV_GoOff) == -1){
		        var newUrl = craftnodelink(dclNavList.options[dclNavList.selectedIndex].value);
                window.location = newUrl;           
            }
        }catch(err){
            //catching errors for firefox
        }
	//}
}

function addLoadEvent(func) 
{
	var oldonload = window.onload;
	
	try{
		if (typeof window.onload != 'function') 
		{
			window.onload = func;
		} 
		else 
		{
			window.onload = function() 
			{
				if (oldonload) 
				{
					oldonload();
				}
				func();
			}
		}
	}catch(err){
		
	}
}

function getURL(fragid)
{
    try{
        var tempForm = document.forms["DCLSubForm_" + fragid];
        var dl = eval("tempForm.DCLSubMenu_" + fragid);
        var target = dl.options[dl.selectedIndex].value;
        var newWindowDirective = target.substring(0, 2);
        target = target.substring(2);
        if (newWindowDirective == "Y_")
        {
            window.open(target);	
        }
        else
        {
            window.location = target;
        }
    }catch(err){
        //just catching errors so they don't appear for the user
    }
}

function dclMasterSelectChange(form, dl, fragid)
{
	try{
		var targetID = dl.options[dl.selectedIndex].value;
		var subForm = document.forms["DCLSubForm_" + fragid];
		//var tempGoImage = subForm.elements['DCLGOIMG_' + fragid];
	    var tempGoImage = document.getElementById('DCLGOIMG_' + fragid);
		var subdl = eval("subForm.DCLSubMenu_" + fragid);
				
		if (targetID != null && targetID != "")
		{
			var urlList = eval("dclURLS" + fragid + "_" + targetID);
			
			if (urlList != null)
			{
				var displayList = eval("dclDisplay" + fragid + "_" + targetID);
				if (subdl != null)
				{
					subdl.options.length = 0;
	                tempGoImage.src = DCLNAV_GoOn;
					for (var i = 0; i < urlList.length; i++)
					{
	                    subdl.options[subdl.options.length] = new Option(displayList[i], urlList[i]);
					}
				}
	           
			}
			else
			{
				subdl.options.length = 0;
				tempGoImage.src = DCLNAV_GoOff;
			}
		}
		else
		{
			subdl.options.length = 0;
			tempGoImage.src = DCLNAV_GoOff;
		}
	}catch(err){
	 //	
	}
}
