// (c) Copyright 2002, TIBCO Software Inc.  All rights reserved.
// LEGAL NOTICE:  This source code is provided to specific authorized end
// users pursuant to a separate license agreement.  You MAY NOT use this
// source code if you do not have a separate license from TIBCO Software
// Inc.  Except as expressly set forth in such license agreement, this
// source code, or any portion thereof, may not be used, modified,
// reproduced, transmitted, or distributed in any form or by any means,
// electronic or mechanical, without written permission from  TIBCO
// Software Inc.
//---------------------------------------------------------------------------
// THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND,
// EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT
// THE PROGRAM IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR
// PURPOSE OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND
// PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD ANY PART OF THE PROGRAM
// PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT TIBCO) ASSUME THE COST OF ANY
// NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY
// CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF THE PROGRAM IS
// AUTHORIZED HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
//---------------------------------------------------------------------------


// APDX.js
// 
// These functions are used by the TIBCO ActivePortal "drill-across"
// and "picker" features. 
// 


//_____________________
//
//        APDX
//_____________________


// ____________ 
// APDXAddField: Add a field to a headline in a category.
//
// This function is invoked while rendering the headline.
// It checks whether the headline has an associated 
// drill-across link, and renders the field either with
// the link or without the link.
//
// fieldname: name of field being added
// fieldvalue: the actual value, which is typically
//     %%_headline(fieldname, etc.)%%
// hypertextlink: the drill-across link associated with
//     the field, e.g. %%_hyperlink(fieldname, etc.)%%
//
function APDXAddField(fieldname, fieldvalue, hypertextlink)
{
    if(hypertextlink == '' || hypertextlink == "")  {
	document.write(fieldvalue); 
    }    
    else  { 
	document.write("<a href = \"" + hypertextlink + "\">" + fieldvalue + "</a>"); 
    } 
    return false;
}


// _____________ 
// APDXDoNothing: Do nothing
//
function APDXDoNothing()
{
}


// ________________ 
// APDXLaunchPicker: Open a Picker window
//
// This function is invoked when a user clicks on a link
// that needs to launch a Picker window. In most cases,
// we expect this function to be called directly when
// the form with which the picker is associated lives
// in a category, and the entire Picker URL has been
// specified in the category configuration.
//
// pickerURL: the URL of a portal page that contains
// a Picker category, which is used to pick one of a list
// of headlines (records). It then sets the values of
// some elements of the source form = to the values 
// of certain fields in the selected record.
//
function APDXLaunchPicker(pickerURL) 
{
    var offset = document.cookie.indexOf("APDXURL=");
    if(offset != -1) {
	var len = document.cookie.length;

	firstFragment = document.cookie.substring(0, offset);

	var offsetEnd = document.cookie.substring(offset, len).indexOf(";");

	thirdFragment = document.cookie.substring(offsetEnd, len);

	document.cookie = firstFragment.concat(thirdFragment);
    }

    document.cookie = ("APDXURL=" + escape(pickerURL));
    
    //drillDown = window.open(pickerURL, 'APDXPickerWindow'); 
    drillDown = window.open(pickerURL, 'APDXPickerWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=600,height=400');

    return false;
}


// __________________ 
// APDXLaunchPickerEZ: Construct a Picker category URL and 
// open a Picker window
// 
// This function constructs the necessary Picker URL from
// its component parts and then invokes APDXLaunchPicker.
//
// pickerPageID: a portal page containing a category that
//   will be used as a Picker
// sourceFormID: the id in the "source" page of the form
//   whose elements we will update with field values from a 
//   headline selected in the picker
// elemID0: ID of the first element in "source form" that 
//   we want the Picker to update
// pickerHeadlineField0: the name of the headline field 
//   in the Picker category whose value we want to use to
//   update elemID0
// elemID1: ID of the 2nd element in "source form" that 
//   we want the Picker to update
// pickerHeadlineField1: the name of the headline field 
//   in the Picker category whose value we want to use to
//   update elemID1
// elemID2: ID of the 3rd element in "source form" that 
//   we want the Picker to update
// pickerHeadlineField1: the name of the headline field 
//   in the Picker category whose value we want to use to
//   update elemID2
// otherRequestParameters: additional request parameters
//   for the Picker category's use
//
function APDXLaunchPickerEZ(pickerPageID, sourceFormID, 
			    elemID0, pickerHeadlineField0,
			    elemID1, pickerHeadlineField1,
			    elemID2, pickerHeadlineField2,
			    otherRequestParameters)
{		  
    pickerURL = pickerPageID + "?APPickSource=" + sourceFormID +
	"&APPickFor0=" + elemID0 + "&APPickFld0=" + pickerHeadlineField0;
    if(elemID1 != '') {
	pickerURL += ("&APPickFor1=" + elemID1 + "&APPickFld1=" + pickerHeadlineField1);
	if(elemID2 != '') {
	    pickerURL += ("&APPickFor2=" + elemID2 + "&APPickFld2=" + 
			  pickerHeadlineField2);
	}		
    }
    
    if(otherRequestParameters != '') {
	pickerURL += (otherRequestParameters); 
    }

    pickerURL += "&APDXRefresh=All";

    APDXLaunchPicker(pickerURL); 
    return false; 
}


// ______________ 
// APDXPickRecord: In a Picker window, pick one record,
// set the values of the appropriate form elements in the
// parent page to the appropriate fields in the selected
// record, and close the picker window.
//
function APDXPickRecord(pickerCat, recordNum)
{
    var elem = recordNum - 1;
    var idx;
 
    for(idx = 0; idx < pickerCat.elementsToSet.length; idx++) {

	if(pickerCat.elementsToSet[idx].pickFor != '') {

	   var fldName = pickerCat.elementsToSet[idx].hlField;
	   var fldString = "pickerCat.headlines[" + elem + "]." + fldName;

	   var fldVal = eval(fldString);

	   fullString = "opener.document." + pickerCat.sourceFormID + 
	             "." + pickerCat.elementsToSet[idx].pickFor + 
		     ".value";

//	   document.write(fullString);

           eval(fullString + " = \"" + fldVal + "\";");
        }
    }
    window.close();
}

//_____________________
//
// APDXPickerCategory
//_____________________


// __________________ 
// APDXPickerCategory: Constructor
//
function APDXPickerCategory(srcFormID)
{
    this.sourceFormID = srcFormID;
}


//_____________________
//
//   APDXElementToSet
//_____________________


// ________________ 
// APDXElementToSet: Constructor
//
function APDXElementToSet(pickValueFor, getValueFrom)
{
    this.pickFor = pickValueFor;
    this.hlField = getValueFrom;
}			   


