/*
 * The Apache Software License, Version 1.2
 *
 * Copyright (c) 2003 Institute of Information Science, 
 * Academia Sinica, Taiwan. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer. 
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:  
 *       "This product includes software developed by Jenghan Hsieh,
 *        Bike Hsu. (http://axeframework.sourceforge.net/) during
 *        2002-04/12 and by Jenghan Hsieh at Academia Sinica, Taiwan
 *        during 2003-01/07."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "AXEFramework", and "Academia Sinica, Taiwan" must not 
 *    be used to endorse or promote products derived from this software
 *    without prior written permission. For written  permission, please
 *    contact jenghan@iis.sinica.edu.tw.
 *
 * 5. Products derived from this software may not be called "AXEFramework"
 *    nor may "AXEFramework" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals.  For more information on the Architectural XML Editing
 * Framework, please see
 * <http://axeframework.sourceforge.net/>.
 *
 * ====================================================================
 
    This library can be divided into three parts, including :

      1. Helper functions for DOM Maipulation
      2. Helper functions for AXEF Template Manipulation
      3. Miscellanious helper functions.

// [2003-10-10 bear] - Better support for nested templates.

// [2003-10-16 bear] - modify _C(), I break the backward compatibility since this is considered
                                 a major revision.
                                 
   [2003-10-16 bear] - added a _t_add2(), while _t_add() is autonomous, it must be used 
								within the template fragment. _t_add2() can be used anywhere in the
								document. But the linking should be provided manually.

   [2003-10-16 bear] - try to add a "_f()" for retrieving following match. similar to _a, _d.

   [2003-10-16 bear] - modifying FillTable (bug) add variable declaration for loops within function. 
   
   [2003-11-18 bike] absTop, absLeft, added
   					 __d modified : not getAttribute in non-element node.
   					 
   ----- V1.2 -------------------
	
  [2005-03-20 max] add SetOptionDefault is called by _t_addx , select can support default setting. " <select default="value" ... >
  	
  [2005-03-20 max] add plugin SetAssignValue.
 
 =======================
  Template Manipulation
 =======================
*/
// max add ; 2005/03/20
var AssignValue = AssignValue_Normal;

function SetAssignValue( fnAssignValue )
{
	AssignValue = fnAssignValue;
}
//---------------------------------------

function absTop(_rObject)
{
	var _oT = _rObject.offsetTop;
	var _oP = _rObject.offsetParent;
	
	
	
	while(_oP != null)
	{
		_oT += _oP.offsetTop;
		_oP = _oP.offsetParent;
	}
	
	return _oT;
}

function absLeft(_rObject){
	var _oL = _rObject.offsetLeft;
	var _oP = _rObject.offsetParent;
	while(_oP != null){
		_oL += _oP.offsetLeft;
		_oP = _oP.offsetParent;
	}
	return _oL;
} 
 
 
/*
   Create Entry
   ---------------
*/
function _C( oPrototype, oDestination, oSelf )
{

	var oTarget = oPrototype.cloneNode(true); 
	
	if( oSelf ){	
		if (oSelf.nextSibling) {
			return oSelf.parentNode.insertBefore( oTarget, oSelf.nextSibling );
		} else {
			return oSelf.parentNode.appendChild( oTarget );
		}
	}
	else
	{
		if(oDestination.childNodes.length > 1){
			return oDestination.insertBefore( oTarget, oDestination.childNodes[0]);
		}
		
		return oDestination.appendChild( oTarget );
	}
}

/*
   Delete Entry
   ------------
*/
function _D( cID ){
 	var oTemp; 
 	    oTemp = _a( window.event.srcElement, cID );
	    oTemp.removeNode(true);
}

function _Dc( cClass ){
 	var oTemp; 
 	    oTemp = _ac( window.event.srcElement, cClass );
	    oTemp.removeNode(true);
}

function _Dt( oTarget, cTemplateName ){

	if( cTemplateName ){
 		var oTemp; 
 		    oTemp = _at( oTarget, cTemplateName );
 		    oTemp.parentNode.removeChild(oTemp);
	} else {
		var oTemp = _template( oTarget );
		oTemp.parentNode.removeChild(oTemp);
	}
}

/*
   Move Entry Up
   -------------
*/
function _up( oTarget )
{
	var oPrevious = oTarget.previousSibling;
	if( oPrevious.className == oTarget.className )
	{
		oPrevious = oTarget.insertAdjacentElement("afterEnd",  oPrevious );
	}
}

/*
   Move Entry Down
   ---------------
*/
function _dn( oTarget )
{
	var oNext = oTarget.nextSibling;
	if( oNext != null )
	{
		if( oNext.className == oTarget.className )
		{
			oNext.insertAdjacentElement("afterEnd",  oTarget);
		}
	}
}

/*
   decendent : retrieve using ID and Index#
   ----------------------------------------
*/
function _d( oTarget, cID, iIndex )
{

  	if( ! iIndex ) iIndex = 0 ;
 	
  	var retVal = __d( oTarget, "id", cID, iIndex );
  	  	
	return( retVal );

}

function _dt( oTarget, cFragmentID, iIndex )
{

  	if( ! iIndex ) iIndex = 0 ;
  	var retVal = __d( oTarget, "_TEMPLATE", cFragmentID, iIndex );
	return( retVal );

}

	function __d( oTarget, cAttributeName, cAttributeValue, iIndex )
	{

		iIndex ? iIndex : iIndex = 0 ;

		try
		{
			if(oTarget.nodeType == 1)
			{
				if( cAttributeValue )
				{
					if(oTarget.getAttribute(cAttributeName) == cAttributeValue)
					{
						iIndex --;
						if( iIndex == -1)
						{ 
							return (oTarget); 
						}
					}
				}
				else
				{
					if(oTarget.getAttribute(cAttributeName).length > 0)
					{
						iIndex --;
						if( iIndex == -1)
						{ 
							return (oTarget); 
						}
					}
				}
			}
		} 
		catch(e)
		{};

		if(oTarget.nodeType == 1 || oTarget.nodeType == 9)
		{
			var oChildrenList = oTarget.childNodes;
			if( oChildrenList.length > 0 )
			{
				for( var i = 0; i < oChildrenList.length; i++ )
				{
					var tResult = __d( oChildrenList[i], cAttributeName, cAttributeValue, iIndex );
					if( typeof(tResult) == "object" )
					{
						return( tResult );
					}
					else
					{
						iIndex = tResult;
					}
				}
			} 
		}
		
		return( iIndex );
	}
	
	
	function __d2( oTarget, cAttributeName, cAttributeValue)
	{
		var queue = new Array();
		var node;
		queue.push(oTarget);
		
		while(queue.length > 0)
		{
			node = queue.shift();
			
			try{
				if(node.nodeType == 1)
				{
					if(node.getAttribute(cAttributeName) == cAttributeValue)
					{
						return (node); 
					}
					
					var oChildrenList = node.childNodes;
					if( oChildrenList.length > 0 )
					{
						for( var i = 0; i < oChildrenList.length; i++ )
						{
							queue.push(oChildrenList[i]);
						}
					}
				
				}
			} catch(e){ alert("error") };
		}
		return( null );
	}
	
	
/*
   ancestor : retrieve using ID
   ----------------------------
*/
function _a( oTarget, cID ){
	try{
		var oParent; oParent = oTarget.parentNode;
	} catch (e) {
		alert("Error Fetching Ancestor for class(" + inClass + ")!");
		return(null);
	}
	if( typeof(oParent) == "object" ){
		if( oParent.id == cID ){
			return( oParent);
		    alert( oParent.tagName );
		}else{
			try{
				return( _a( oParent, cID ));
			} catch(e) { 
//			    alert( "no valid ancestor !!");
			    return(null) }
		}
	};
}


/*
   ancestor : retrieve using ClassName
   ------------------------------------
*/
function _ac( oTarget, cClassName ){
	try{
		var oParent; oParent = oTarget.parentNode;
	} catch (e) {
		alert("Error Fetching Ancestor for class(" + inClass + ")!");
		return(null);
	}

	if( typeof(oParent) == "object" ){
		if( oParent.className == cClassName ){
			return( oParent);
		}else{
			try{
				return( _ac( oParent, cClassName ));
			} catch(e) { 
//			    alert( "no valid ancestor !!");
			    return(null) }
		}
	};
}

/*
   ancestor : retrieve using TemplateName
   -----------------------------------------------
*/

/*
 * Old Version remarked by bike.
 *
 
function _at( oTarget, cTemplateName ){
	try{
		var oParent; oParent = oTarget.parentNode;
	} catch (e) {
		alert("Error Fetching Ancestor for template(" + cTemplateName + ")!");
		return(null);
	}

	if( typeof(oParent) == "object" ){
		if( oParent.getAttribute("_TEMPLATE") == cTemplateName ){
			return( oParent);
		}else{

//			try{
				return( _ac( oParent, cTemplateName ));
//			} catch(e) { 
//			    alert( "no valid ancestor !!");
//			    return(null) 
}
		}
	};
}

*/


function _at( oTarget, cTemplateName ){
	var oParent;
	
	while(true){
		try{
			oParent = oTarget.parentNode;
		} catch (e) {
			alert("Error Fetching Ancestor for template(" + cTemplateName + ")!");
			return(null);
		}
		
		if(oParent.nodeType == 1)
			if( oParent.getAttribute("_TEMPLATE") == cTemplateName )
				return( oParent);
			
		oTarget = oParent;
	}
}


/*
   template : retrieve nearest template
   -----------------------------------------------
*/
function _template( oTarget  ){
	try{
		var oParent; oParent = oTarget.parentNode;
	} catch (e) {
		return(null);
	}

	if( typeof(oParent) == "object" )
	{
		if( oParent.getAttribute("_TEMPLATE")  )
		{
			return( oParent);
		}
		else{
			try
			{
				return( _template( oParent  ));
			}
			catch(e)
			{ 
			    return(null) }
		}
	};
}

function GetEntry( inClass, inSrcElement )
{
	var cClassName;

	try
	{
		var oTarget; oTarget = inSrcElement.parentNode;
	}
	catch (e)
	{
		alert("Error Fetching Ancestor for class(" + inClass + ")!");
		return(null);
	}
	
	if( typeof(oTarget) == "object" )
	{
		if( oTarget.className == inClass )
		{
			return( oTarget );
		}
		else
		{
			return( GetEntry( inClass, oTarget ));
		}
	};
}
//2005-6-17 flysnow add a parameter iIndex,it used for control maxlength of  the new line
function _t_add( oTarget, oFragments ,iIndex)
{
	iIndex ? iIndex : iIndex = 0 ;
	
	var oCurrentEntry = _template( oTarget );
	var cTemplateName = oCurrentEntry.getAttribute("_TEMPLATE");
	var oTargetFragment = __d( oFragments, "_TEMPLATE", cTemplateName, 0 ).cloneNode(true);
	var _ref_number=0;
	for( i=0; i<oCurrentEntry.parentNode.childNodes.length; i++)
	{
		if( oCurrentEntry.parentNode.childNodes.item(i).getAttribute("_REF") != null )
			_ref_number+=1;
	}
	
	var length = oCurrentEntry.parentNode.childNodes.length - _ref_number;
	//alert( length + '/'+iIndex);
	
	if( iIndex==0 || length<(iIndex+1) )
	{
	FillTable( oTargetFragment, oFragments );
	
	var node = _C( oTargetFragment, oTarget.parentNode, oCurrentEntry );
	
	// check select	default	
	SetOptionDefault( node );

	return node;
	}
	else
	{
		alert("The max number allow is "+iIndex);
	}
}

// 2003-10-16 bear
function _t_add2( oEntryPoint, oTargetFragment, oFragments )
{
	FillTable( oTargetFragment, oFragments );
	
	var node = _C( oTargetFragment, oEntryPoint );
	
	// check select	default	
	SetOptionDefault( node );
	
	return node;
}


//2003-12-02 Bike
// Insert a "strTemplateName" from "oFragments" in "strParentID" after "strEntryID"
//2005-6-17 flysnow add a parameter iIndex,it used for control maxlength of  the new line
function _t_add3(oRef, strParentID, strEntryID, strTemplateName, oFragments, iIndex)
{
	iIndex ? iIndex : iIndex = 0 ;
	var oCurrentEntry = _d(_a(oRef, strParentID), strEntryID);
	var oTargetFragment = __d( oFragments, "_TEMPLATE", strTemplateName, 0 ).cloneNode(true);
	var _ref_number=0;
	for( i=0; i<oCurrentEntry.parentNode.childNodes.length; i++)
	{
		if( oCurrentEntry.parentNode.childNodes.item(i).getAttribute("_REF") != null )
			_ref_number+=1;
	}
	
	var length = oCurrentEntry.parentNode.childNodes.length - _ref_number;
	//alert( length + '/'+iIndex);
	
	if( iIndex==0 || length<(iIndex+1) )
	{
		
	FillTable( oTargetFragment, oFragments );
	
	var node = _C( oTargetFragment, _a(oRef, strParentID), oCurrentEntry);
	
	// check select	default	
	SetOptionDefault( node );
	return node;
	}
	else
	{
		alert("The max number allow is "+iIndex);
	}
}


// max 2003-03-20, add default select
function SetOptionDefault( oTargetFragment )
{
	var collObjects = oTargetFragment.getElementsByTagName("SELECT");
	
	for( i=0; i< collObjects.length; i++ )
	{	
		var oSelect = collObjects[i];
		var value = oSelect.getAttribute("default");
		var index=0;
				
		if( value != "" )
		{
			//oSelect.value = value;					
			for( var j=0; j<oSelect.options.length; j++ )
			{	if( oSelect.options[j].value == value )
				{	index=j;								
					break;
				}	
			}
		}
				
		oSelect.selectedIndex = index;
		oSelect.options[index].selected = true;
	}
}


function _t_del( oTarget, cTemplateName  )
{
	_Dt( oTarget, cTemplateName );
}

// 2003-12-1 Bike
// Delete template unless last one
function _t_del2( oTarget, cTemplateName  )
{	
	var oTemp; 
 	if( cTemplateName ){
 		    oTemp = _at( oTarget, cTemplateName );
 	} else {
		var oTemp = _template( oTarget );
	}
	
	if(_templateCount(oTemp) > 1){
		oTemp.parentNode.removeChild(oTemp);
	}
}

// 2003-12-1 Bike
// Count the same template
function _templateCount(oTemplate)
{
	var oChildList = oTemplate.parentNode.childNodes;
	var strName = oTemplate.getAttribute("_TEMPLATE");
	//alert(strName + " " + );
	var count = 0;
	for(var i = 0; i < oChildList.length; i++){
		if(oChildList[i].nodeType == 1){
			if(oChildList[i].getAttribute("_TEMPLATE") == strName){
				count ++;
			}
		}
	}
	return count;
}


/*
 * Expend an template entry in table.
 * objTAble: table to be expended.
 * objFragments: template source
 *
 * in template block:
 * _TEMPLATE: name of template to search
 * _COUNT: number of copies
 *
 */
 
// [2003-10-10 bear] -  Better support for nested templates.

function FillTable(objTable, objFragments, cDepth )
{	
	var intXX;
	
	if(objTable.getAttribute("_TEMPLATE") == "classList")
	{
		intXX = 0;
	}

	if( !cDepth ) cDepth = 1;
	
	if( cDepth < 5 )
	{
		var arrREFs = GetREF(objTable, 0);
			
		for(var i = 0; i < arrREFs.length; i++)
		{
			var objP = arrREFs[i].parentNode; // Parent Node
			intXX = arrREFs[i].getAttribute("_COUNT"); // number of copies
			//alert(intXX);

			if(intXX == null)
				intXX = 1;
			for(var j = 0; j < intXX; j++)
			{
				var objNewNode = __d(objFragments, "_TEMPLATE", arrREFs[i].getAttribute("_REF")).cloneNode(true);

				FillTable( objNewNode, objFragments, cDepth + 1 );
				
				if(objNewNode.getAttribute("_TEMPLATE") == "class")
				{
					intXX = intXX;
				}
				
				
				objP.insertBefore(objNewNode, arrREFs[i]);
			}

			objP.removeChild(arrREFs[i]);
		}
	}
}

function GetREF(objTable, level)
{
	var arrREFs = new Array();
	var arrChilds = objTable.childNodes;
		
	for(var i = 0; i < arrChilds.length; i++)
	{
		if(arrChilds[i].nodeType == 1)
		{
			if(arrChilds[i].getAttribute("_REF") != null)
			{
				arrREFs.push(arrChilds[i]);
			}
			else
			{
				arrREFs = arrREFs.concat(GetREF(arrChilds[i], level + 1));
			}
		}
	}
	return arrREFs;
}

function _hasChildElement( oXML )
{
	var childNodes = oXML.childNodes;
	for(i = 0; i < childNodes.length; i++)
	{
		if(childNodes[i].nodeType == 1)
			return true;
	}
	return false;
}

function GetChildElements(objTable)
{
	var arrChildElements = new Array();
	var arrChilds = objTable.childNodes;
		
	for(var i = 0; i < arrChilds.length; i++)
	{
		if(arrChilds[i].nodeType == 1)
		{
			arrChildElements.push(arrChilds[i]);
		}
	}		
	return arrChildElements;
}

function InsertAfter(oSelf, oNewNode)
{
	if (oSelf.nextSibling)
	{
		oTemp = oSelf.parentNode.insertBefore( oNewNode, oSelf.nextSibling );
	}
	else
	{
		oTemp = oSelf.parentNode.appendChild( oNewNode );
	}
}

/*
 ===================
  DOM Helper Functions
 ===================
 
   1. Dynamic selections
   ------------------------
   1.1. InitSelections
   1.2. InitSelection
   1.3. InitSelctionWithValues
   1.4. InitRemoteSelections

*/

function InitSelections( oSelectionList, vArray )
{
	alert("InitSelections");
	
	var i;
	var j;
	var oOption;
	
	if( oSelectionList.length == 1)
	{
		// if the oSelectionList is single <select>
		InitSelection( oSelectionList, vArray );

	} else {

		for( j = 0 ; j < oSelectionList.length; j ++)
		{
			InitSelection( oSelectionList[j], vArray );
		}
	}
}

function InitSelection( oSelection, vArray )
{
	var i; var oOption; 
	var errNo;
	var cOriginalValue = '';
	
	try{
		if( oSelection.options.length > 0 )
		{
			try
			{
				cOriginalValue = oSelection.options[ oSelection.selectedIndex ].innerText ;
			}
			catch (e)
			{}
			
			try{
				while( !oSelection.options.length == 0 ){
					oSelection.options.remove(0);
				}
			}catch(e){}	
		}
	} catch (e){
		// the selection may be empty that oSelection.options will cause an error.
		// proceed anyway.
	}
	
	for( i = 0 ; i < vArray.length ; i++ ){
		oOption = document.createElement("option");
		oSelection.options.add( oOption );
		oOption.appendChild( document.createTextNode( vArray[i] ));
	}

	if( cOriginalValue != '' ){
		AssignValue( oSelection, cOriginalValue );
	}else{
		oSelection.options[0].selected = true;
	}

	return( oSelection );
}

function InitSelectionWithValues( oSelection, vArrayText, vArrayValues )
{
	var i; var oOption; 
	var errNo;
	var cOriginalValue = '';
	if( oSelection.options.length > 0 )
	{
		try{
			cOriginalValue = oSelection.options[ oSelection.selectedIndex ].innerText ;
		}
		catch (e) {}
		try{
			while( !oSelection.options.length == 0 ){
				oSelection.options.remove(0);
			}
		}catch(e){}	
	}
	
	for( i = 0 ; i < vArrayText.length ; i++ ){
		oOption = document.createElement("option");
		oSelection.options.add( oOption );
		oOption.appendChild( document.createTextNode( vArrayText[i] ));
		oOption.value = vArrayValues[i];
	}
	
	if( cOriginalValue != '' )
	{
		//SetupSelectionDefault( oSelection, cOriginalValue );
	}else{
		oSelection.options[0].selected = true;
	}
	return( oSelection );
}

function AssignValue_Normal( oSelection, cValue )
{
	var i; var oOption; var oSelected;
	var bSelected = false;

	for( i = 0 ; i < oSelection.options.length; i ++ )
	{
		oOption = oSelection.options[i];
		if( oOption.value == cValue || oOption.text == cValue ) {
			oOption.selected = true;
			oSelection.selectedIndex = i;
			return(oOption);
		}
	}

	// not found, Create a new one
	oOption = document.createElement("option");
	oSelection.options.add(oOption);
	oOption.selected = true;
	oOption.appendChild(document.createTextNode (cValue));
	oOption.value =  cValue;
	oSelection.selectedIndex = i;
	
	return( oOption );
}

function CustomEntry(){

	var srcElm = window.event.srcElement;
	var oTargetOption = srcElm.options[srcElm.selectedIndex];	
	var pt = _getXY( srcElm );

	switch( oTargetOption.innerText )
	{
		case '**NEW**' :
			popCal.style.top = pt.y + srcElm.offsetHeight + 1;
			popCal.style.left = pt.x;
			popCal.style.visibility = 'visible';
			frCustomService.formInit( popCal, srcElm, AssignValue, oTargetOption.innerText );
			break;
	};
}

function valueOf( oNode, cXPath )
{
	var oTargetList = oNode.selectNodes( cXPath ) ;
	
	if( oTargetList.length > 0 ){
		return( _interop_GetTextValue( oTargetList(0) ) );
	} else {
		return( new String("") );
	}
}

function toggleDisplay( oTarget )
{
    if( oTarget.style.display == "block" )
    {
	    oTarget.style.display = "none";
    }
    else
    {
	    oTarget.style.display = "block";
    }
}

function _SelectedText( inSelection )
{
	if( inSelection.options.length > 0 ) {
		return( _interop_getTextValue( inSelection.options[ inSelection.selectedIndex ] ));
	} else {
		return( "" );
	}
}

function DisableAllInput(oTable)
{
	switch(oTable.tagName)
	{
	    	case "INPUT" :
		case "TEXTAREA":
		case "SELECT":
		case "BUTTON" :
		    	oTable.disabled = true;
			break;
		default :
			if(oTable.hasChildNodes)
			{
			    var oChilds = oTable.childNodes;
				for(var i = 0; i < oChilds.length; i++)
				    DisableAllInput(oChilds[i]);
			}
	}
}	

function InitRemoteSelections()
{
	var collObjects = document.getElementsByTagName("SELECT") ;
	var oCount = collObjects.length ;

	for( var i = 0; i < collObjects.length; i++ )
	{
		var sItem="" ;
		tempObject = collObjects[i];

		// ---------------------------------------------------------------
		// if( "option" length == 0 ) 
		//		do remote entries lookup wiht the 'href' attribute
		// ---------------------------------------------------------------
		if( tempObject.getElementsByTagName("OPTION").length == 0 )
		{
			tempURL = tempObject.getAttribute("href") ;
			if( tempURL )
			{
				// -------------
				// split the URL 
				// -------------
	 			var urlParts = tempURL.split( "#" );
	 			sLocation = urlParts[0];
 			
				if( sLocation == "" )
				{
					// ------------
					// local idRef 
					// ------------
					_generateOptions( tempObject , document.getElementById( urlParts[1] ), urlParts[1] );
				}
				else
				{
					// ------------
					// remote idRef 
					// ------------

					if( urlParts.length > 0 ){
		 				sItem = urlParts[1];
					}

					var remoteXML = GetXML( sLocation );

					if( remoteXML.parseError != 0 )
					{
						alert( "Error : " + remoteXML.parseError.reason );
					}
					else
					{
						if( sItem )
						{
							_generateOptions( tempObject , _d(remoteXML, sItem, 0) );
						}
						else
						{
							_generateOptions( tempObject , _d(remoteXML, "", 0) );
						}
					}
				}
 			}
		}
	}
}

/*
'------------------------------------------------
'名稱 : _generateOptions
'功能 : 依據傳入的項目清單, 動態建立下拉式選單.
'------------------------------------------------
' [2003-10-23 bear] optional third parameter "cNodeID" is added for debugging purpose.
*/
function _generateOptions( objTargetSelection, objList, cNodeID ) {

	var strDefault = objTargetSelection.getAttribute("default") ;
	
	if(objList == null )
	{
		if( !cNodeID ) cNodeID = "" ;
		alert("Null List !!\nnode id--" + cNodeID );
		return;
	}
	
	if( objList.childNodes.length > 0 )
	{
		for( var i = 0; i < objList.childNodes.length; i++ )
		{
			var oCurrentItem = objList.childNodes[i];
			alert(oCurrentItem);
			if( oCurrentItem.nodeType == 1 )
			{
				strOptionValue = _interop_getTextValue( oCurrentItem );
 				strOptionSelected = oCurrentItem.getAttribute("selected");

				oOption = document.createElement("option");

				if( strOptionSelected != "" ) {
					oOption.selected = true ;
				}

				if( strDefault != "" ) {
					if( strDefault == strOptionValue) {
						oOption.selected = true ;
					}
				}

				_interop_setTextValue( oOption, trim( strOptionValue ) ) ;
				oOption.value = strOptionValue ;
			
				try{ 
					objTargetSelection.appendChild( oOption ) 
				} catch (e) {
					objTargetSelection.add( oOption, null );
				}
			}
		}
	} else {
		alert( "載入資源中應包含<ul>\n" + objList.innerHTML );
	}
}


function _nc_removeChildren( oTarget )
{	
	var oChildren = oTarget.childNodes;
		
	for( var i = 0; i < oChildren.length; i++ ){
		oTarget.removeChild( oChildren[i] );
	}
}

/*
 ===================
  Miscellanious Functions
 ===================
*/

function _point(iX, iY)
{
	this.x = iX; this.y = iY;
}

function _getXY(aTag)
{
  var oTmp = aTag;
  var pt = new _point(0,0);
  do {  	
  	pt.x += oTmp.offsetLeft;
 	pt.y += oTmp.offsetTop;
  	oTmp = oTmp.offsetParent;  	
  } while(  oTmp.tagName!="BODY" );
  return pt;
}

function GetCurrentTimestamp( d )
{

    if(!d) var d = new Date();
	var cMonth = _ConvertDateString(d.getMonth() + 1);
	var cDate = _ConvertDateString(d.getDate());
	var cHour = _ConvertDateString(d.getHours());
	var cMinute = _ConvertDateString(d.getMinutes());
	var cSecond = _ConvertDateString(d.getSeconds());
	
	return( d.getYear() + "-" + cMonth  + "-" + cDate + "T" + cHour + ":" + cMinute + ":" + cSecond );

	function _ConvertDateString( cInput ){

		if( (cInput < 10) && (cInput >= 0) ){
			return( "0" + cInput );
		} else {
			return( cInput );
		}
	}

}

function ltrim ( s )
{
	return s.replace( /^\s*/, "" );
}

function rtrim ( s )
{
	return s.replace( /\s*$/, "" );
}

function trim ( s )
{
	return rtrim(ltrim(s));
}

function NodeList2Array( oNodeList ){

	var aArray = new Array();

	for( var i = 0; i < oNodeList.length ; i ++ )
	{
		aArray.push( oNodeList[i].nodeValue );
	}

	return( aArray );
}

// fix bug
function fetchArray( oTarget, cCriteria )
{

	var oChildList = oTarget.childNodes;
	var oArray = new Array();
	
	var aCriteria = cCriteria.split("=");
    	var cFilterTarget = aCriteria[0];
    	var cFilterValue = aCriteria[1];
 
	// note that if the criteria is met, no further traversing.
	// --------------------------------------------------------
	if( eval( "oTarget." + cFilterTarget ) == cFilterValue )
	{	
		//oArray.push( _interop_getTextValue( oTarget ) );
		oArray.push( oTarget )
	}
	else
	{	for( var i = 0; i < oChildList.length; i++ )
		{
			var oItem = oChildList[i];
			if( oItem.nodeType == 1 )
			{	var oTemp = fetchArray( oItem, cCriteria );
				for(var j = 0; j < oTemp.length; j++)
				{	oArray.push( oTemp[j] );
				}
			}
		}

	}

	return( oArray );
}

function fetchArraybyTagName( oTarget, strTagName )
{

	var oChildList = oTarget.childNodes;
	var oArray = new Array();
	
	// note that if the criteria is met, no further traversing.
	// ------------------------------------------------------------
	var name = _interop_GetLocalName(oTarget);
	if( _interop_GetLocalName(oTarget) == strTagName )
	{
		oArray.push( oTarget );
	}
	else
	{
		for( var i = 0; i < oChildList.length; i++ )
		{
			var oItem = oChildList[i];
			if( oItem.nodeType == 1 )
			{
				var oTemp = fetchArraybyTagName( oItem, strTagName );
				for(var j = 0; j < oTemp.length; j++)
				{
					oArray.push( oTemp[j] );
				}
			}
		}

	}

	return( oArray );
}

function fetchArraybyAttribute( oTarget, strAttribute, strValue )
{
	var oChildList = oTarget.childNodes;
	var oArray = new Array();

	// note that if the criteria is met, no further traversing.
	// ------------------------------------------------------------
	for( var i = 0; i < oChildList.length; i++ ){
		var oItem = oChildList[i];
		if( oItem.nodeType == 1 ){
			if( oItem.getAttribute(strAttribute) == strValue ){
				oArray.push( oItem );
			} else {
				var oTemp = fetchArraybyAttribute( oItem, strAttribute, strValue );
				for(var j = 0; j < oTemp.length; j++)
				{
					oArray.push( oTemp[j] );
				}
			}
		}
	}
	return( oArray );
}

function getFirstChildByTagName(odXML, tagName)
{
	var childs = odXML.childNodes;
	
	for (var i = 0; i< childs.length; i++)
	{
		if( childs[i].nodeType == 1 ){
			if(_interop_GetLocalName(childs[i]) == tagName){
				return(childs[i]);
			}
		}
	}
}
/*
function getChildsByTagName(odXML, tagName){
	var Flag = true;
	
	function innerRec(odXML, tagName){
		var childs = odXML.childNodes;
	
		for (var i = 0; i< childs.length; i++){
			if( childs[i].nodeType == 1 ){
				if(Flag){
					if(_interop_GetLocalName(childs[i]) == tagName){
						
					}
				}
			}
		}
	}
	
}
*/
