///////////////////CascadedDropdown Menu/////////////////
//Date : 08/09/2007               						//
//Version : 1.1        					  //
//Author Dr J.G. "Koos" Prins		  //
//EMail: kprins@cxsconsulting.com 	  //		
/////////////////////////////////////////////////////////
// Modifications on this code is not recomended
// Suggestions are welcome

//
// Call be an onClick event to display a HRef
//
function onGoto(href)
{
	try
	{
		if ((href == null) || (href == ""))
			return true;
		
		var pWindow = window;
		if ((pWindow.frameElement != null) && (pWindow.frameElement.ownerDocument != null))
			pWindow = pWindow.frameElement.ownerDocument.parentWindow;

		pWindow.location = href;
	}
	catch(ex)
	{}		
		
	return true;
}
//
// Call be an onClick event to display a HRef in teh same browser window as its parent docuement
//
function onGotoMain(href)
{
	try
	{
		if ((href == null) || (href == ""))
			return true;
		
		var pWindow = window;
		while ((pWindow.frameElement != null) && (pWindow.frameElement.ownerDocument != null)) {
			pWindow = pWindow.frameElement.ownerDocument.parentWindow;
		}

		pWindow.location = href;
	}
	catch(ex)
	{}		
		
	return true;
}
//************************************************************************************
// Open a print Window with sUrl 
//************************************************************************************
this.onGotoNew = function(sUrl,sFeatures)
{
	try
	{
		if (sFeatures == null)
			sFeatures = "dependent=no";
			
		window.open(sUrl,"Child_Window",sFeatures);
	}
	catch(ex)
	{}
	return true;
}

//
// Call be a MouseOver event to display the obj.title in the status bar
//
function onHoover(obj)	
{
	try
	{
		if (obj != null)
		{
			if (obj.title != null)
				window.defaultstatus = obj.title;
			
			var pStyle = obj.style;
			if (pStyle != null)
				pStyle.cursor = "url('handpoint.cur') ,hand, auto;";
		}
	}
	catch(ex)
	{}
		
	return true;
}

//
// Call be a Mouseout event to clear the status bar
//
function onExit(obj)	
{
	try
	{
		window.defaultstatus = "";
	}
	catch(ex)
	{}
		
	return true;
}

// Opens a new window showing sUrl with spciaifed features
function OpenBrowserWindow(theURL,winName,features) 
{ 
	window.open(theURL,winName,features);
}

// Opens a Topic Page and display sSrc in an IFrame
function ShowTopic(sParentFolder,sCaption,sSrc,bDoPrint,bDoExpand)
{
	try
	{
		if ((sSrc == null) || (sSrc == ""))
			throw new Error("Topic Link is undefined");
				
		var sUrl = GetRelativePath("_borders", sParentFolder);	
		if (sUrl == null)
			throw new Error("Unable to extract the relative path to Page[topicpage]");
		sUrl += "/topicpage.html";
		
		var sPars = "";
		if ((sCaption == null) || (sCaption == ""))
			sCaption = "Page["+sSrc+"]";
			
		var pUrl = new Url();
		sPars = "title="+pUrl.encode(sCaption);			
				
		if ((sSrc != null) && (sSrc != ""))
			sPars = (sPars=="")? "src="+sSrc: sPars +"&src="+sSrc;
		
		if ((bDoPrint != null) && (bDoPrint == true))
			sPars = (sPars=="")? "print=true": sPars +"&print=true";
		
		if ((bDoExpand != null) && (bDoExpand == true))
			sPars = (sPars=="")? "expand=true": sPars +"&expand=true";
		
		if (sPars != "")
			sUrl += "?"+sPars;
			
		var sFeatures = "'menubar=no,toolbar=no,status=no,directories=no,location=0,resizeable=no,height=580,width=580'";
		OpenBrowserWindow(sUrl,"Topic_Window",sFeatures);
	}
	catch(ex)
	{
		alert("Show Topic Error: "+ex.message);
	}
}

function ArrayToPath(aArray)
{
  var sPath = "";
  if ((aArray == null) || (aArray.length == 0))
  return sPath;
  
  for(i=0;i<aArray.length;i++)
  {
		var sFolder = aArray[i];
		if ((sFolder != null) && (sFolder != ""))
		{
		    if (sPath == "")
		     	sPath = sFolder;
		    else
		    	sPath = sPath+"/"+sFolder;	
		}
  }
  return sPath;
}

function PathToArray(sPath)
{
	var aArray = new Array();

	if ((sPath != null) && (sPath.length != 0))
	aArray = sPath.split("/");

	return aArray;
}

function GetRelativePath(sPath, sBasePath)
{
	var sRelPath = null;
	if ((sBasePath == null) || (sBasePath.length==0))
		return sPath;

	var aArrPath = PathToArray(sPath);
	var aArrBase = PathToArray(sBasePath);
	var aArrRel = new Array();
	var iLevel = 0;
	if (aArrBase.Length = 0)
		sRelPath = sPath;
	else if (aArrBase.length > aArrPath.length)
	{
		for (i=0;i<aArrBase.length;i++)
		{
			if (i >=  aArrPath.length)
			{
				if (iLevel < 2)
				{
					aArrRel.unshift("..");
					iLevel++;
				}
				else
					break;
			}
			else if (iLevel > 0)
			{
				if (iLevel > 2)
					break;
				
			 	aArrRel.unshift("..");
				iLevel++;
			}
			else if (aArrBase[i] != aArrPath[i])
			{
				iLevel++;
				aArrRel.push("..");
				if (i < (aArrPath.length))
				{
				    	for(j=i;j<aArrPath.length;j++)
				    {
				       	aArrRel.push(aArrPath[j]);
				    }
			    	}
			}
		}
		
		if (aArrRel.length > 0)
			sRelPath  =  ArrayToPath(aArrRel);
	}
	else
	{
		for (i=0;i<aArrPath.length;i++)
		{
			if ((iLevel >0) || (i >= aArrBase.length))
			{
				aArrRel.push(aArrPath[i]);
			}
			else if (aArrPath[i] != aArrBase[i])
			{
				while ((iLevel < 2) && ((i+iLevel) < aArrBase.length))
				{
				    aArrRel.unshift("..");
				    iLevel++;
				}
				aArrRel.push(aArrPath[i]);
			}
		}
		
		if (aArrRel.length > 0)
		{
			sRelPath  =  ArrayToPath(aArrRel);
		}     	
	}
	return sRelPath;
}

//
// Extract the value of parameter sPar from the page's URL.
// Return null if not assigned.
//
function GetUrlParameter(sPar)
{			
		var sHRef = window.location.search;
		var sVal = null;
		var iIndex = -1;
		if (sHRef != null) 
			iIndex = sHRef.indexOf(sPar);
		
		if (iIndex >= 0)
		{
			var iEnd = sHRef.indexOf("&",iIndex);
			var sSubStr = sHRef;
			if (iEnd > iIndex)
				sSubStr = sHRef.substring(iIndex,iEnd);
		
			var sValStr = "";
			if (sSubStr != "")
			{
				var iEqual = sSubStr.indexOf("=");
				if ((iEqual >= 0) && ((iEqual+1) < (sSubStr.length-1)))
					sValStr = sSubStr.substring(iEqual+1);
			}
			
			if (sValStr.length > 0)
			{
				var pUrl = new Url();
				sVal = pUrl.decode(sValStr);
			}
		}
		return sVal;
}


//
// Return true if the Pages Url include the string sPar
//
function HasUrlParameter(sPar)
{			
		var sHRef = window.location.search;
		return ((sHRef != null)  && (sHRef.indexOf(sPar) >= 0))
}

function Url()
{
	// public method for url encoding
	this.encode = function (string) 
	{
		return escape(this._utf8_encode(string));
	}

	// public method for url decoding
	this.decode = function (string) 
	{
		return this._utf8_decode(unescape(string));
	}

	// private method for UTF-8 encoding
	this._utf8_encode = function (string) 
	{
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) 
		{
			var c = string.charCodeAt(n);

			if (c < 128) 
			{
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) 
			{
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else 
			{
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
		}

		return utftext;
	}

	// private method for UTF-8 decoding
	this._utf8_decode = function (utftext) 
	{
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) 
		{
			c = utftext.charCodeAt(i);

			if (c < 128) 
			{
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) 
			{
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else 
			{
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		}
		return string;
	}
}
