/*
	Resource.js
	
	JavaScript routines developed by ResourcePark, Inc.
	
	2/20/03RGS V002 Correct name of rpImageDefine, rpImageSwap in function and documentation
	1/9/03 RGS V001 Cobble into single module, add VanFin mods, Single window action.
					- Add timing routines, create detail examples.
					- Single source module.
					- Javascript calls define targets. No more coding in Java
					
	1/9/03 GW  V000 Create from WebSi, Rampant, etc.  Working prototype
	
	Incorporates confidential and proprietary information and software under license
	agreement with named sources.  Use does not constitute rights or ownership.  All
	rights and ownership rest with named parties below.  Unauthorized use
	prohibited.  Use only by prior written agreement with named parties below.
		Copyright 2003 ResourcePark, Inc. All rights reserved.
		Coypright 1995-2003 Rampant, Inc. All rights reserved.
		Copyright 1995-2003 Websitement, Inc. All rights reserved.
*/
/* ***************************************************************************************** 
  Technical Notes:
	To manipulate information between windows functions must have a target location or name.
	That information usually can't be determined reliably after a window is launched.
	So, Parent windows must save their identifying information in the children before
	they release let go of the child.
	
	See rpLaunchPopup for an example of setting the popup's "creator".  

	Once the popup is alive it can move data into objects in the parent (i.e. a select box)
	or even force the parent to jump to another location (i.e. change the creator.location) 
	
  Examples & Notes

	Multiple commands
		- Combine several commands in one call.  
		- This example updates a field and closes the current window.
		<a onclick="JavaScript:riUpdateParentForm('field1');rpCloseMe()">newvalue</a>
		
	
	Use ONCLICK vs HREF 
		- Advantage: Doesn't display "JAVASCRIPT..." in browser status window when you mouse over it
  		<a onclick="JavaScript:rpParentJump('/home.asp')"> Home </a>
  		- Note: Don't use the HREF.  If you do, a second page will open (see open multiple windows if desired)
  		
  	How to open multiple windows at once.  (From a popup)
  		- The following opens Rampant.com in window "Rampant"
  			AND repoints the parent page elsewhere at the same time!

  		<a href="http://www.rampant.com" target="rampant"
				 onclick="JavaScript:rpParentJump('/db/onlinebuy.asp')">

	TODO - make into routine
		Nice code snippit, loads a value from the form we are in. 
		Assumes you've set popup window's creator from caller
			creator.location=document.LinkMain.go.options[document.LinkMain.go.selectedIndex].value

			<Select Name="go">
			<Option Selected Value="http://www.ResourcePark.com">PickMe</option>
			</Select>(which is the target url)
			
*/

/*---------------------------------------------------------------------------------
	rpUpdateParentField

	In the Parent define a field to be updated...
		<form method="post" action="somewhere.asp" name="SampleForm">	<!--must name the form-->
		<input type="text" value="oldvalue"        name="SampleField">	<!--must name the field-->
	In the Popup/Child 
		<a onclick="JavaScript:riUpdateParentForm('samplefield')">newvalue</a>
	Action: when the link in the popup is cilcked, this routine inserts the value

	Source: Vantage/MiniDB.asp

	UNTESTED, UNTESTED, UNTESTED	UNTESTED, UNTESTED, UNTESTED
	UNTESTED, UNTESTED, UNTESTED	UNTESTED, UNTESTED, UNTESTED
	UNTESTED, UNTESTED, UNTESTED	UNTESTED, UNTESTED, UNTESTED
	UNTESTED, UNTESTED, UNTESTED	UNTESTED, UNTESTED, UNTESTED
*/
function rpUpdateParentField(parentForm, parentField){
var targetForm = window.opener.document.forms[parentForm];
	targetForm.elements[parentField].value = argValue;
}


/*---------------------------------------------------------------------------------
	OPEN A POPUP WINDOW AND SPECIFY SIZE
	Current window becomes parent
	
	TESTED example
		<a onclick="JavaScript:rpLaunchPopupSized('/popup.asp',200,300)"> Popup </a>
*/
function rpLaunchPopupSized(targetURL,width,height)
{
var popupWindow
	popupWindow = window.open(targetURL,"","width=" + width +",height=" + height)
	popupWindow.creator = self		// Insert PARENT identity into the popup window so it can find us later */
}

/*---------------------------------------------------------------------------------
	OPEN A POPUP WINDOW.
	Current window becomes parent
	
	TESTED example
		<a onclick="JavaScript:rpLaunchPopup('/popup.asp')"> Popup </a>
*/
function rpLaunchPopup(targetURL){
var popupWindow
	popupWindow = window.open(targetURL,"","width=220,height=180")
	popupWindow.creator = self		// Insert PARENT identity into the popup window so it can find us later */
}

/*---------------------------------------------------------------------------------
	OPEN A NEW WINDOW.  Defaults for size, name, etc
	
	TESTED example
		<a onclick="JavaScript:rpLaunchWindow('/moreinfo.asp')"> MoreInfo </a>
*/
function rpLaunchWindow(targetURL)
{
var popupWindow
	popupWindow = window.open(targetURL)
	popupWindow.creator = self		// Insert PARENT identity into the popup window so it can find us later
}

/*---------------------------------------------------------------------------------
	OPEN A NEW WINDOW.  
		Set window NAME, URL, height, width and other information
		Setting width or height to 0 interpreted as use default
	
	TESTED example
		<a onclick="JavaScript:rpOpenWindow('BugList','/buglist.asp', 0,0)"> BugList </a>
		<a href="javascript:rpOpenWindow('','/',0,0,'height=380,width=630,left=100,top=100,scrollbars=yes,location=no,directories=no,status=yes,menubar=no,toolbar=yes,resizable=yes')"> Complicated Example </a>
*/
function rpOpenWindow(windowName, targetURL, width, height, other)
{
var popupWindow
var	paramString
	paramString = ""
	if (width != null) {
		if (width!=0) {
			if (paramString != "") paramString = paramString + ","
			paramString = paramString + "width=" + width;
		}
	}
	if (height != null) {
		if (height!=0) {
			if (paramString != "") paramString = paramString + ","
			paramString = paramString + "height=" + height;
		}
	}
	if (other != null) {
		if (other!=0) {
			if (paramString != "") paramString = paramString + ","
			paramString = paramString + other
		}
	}
	popupWindow = window.open(targetURL, windowName, paramString);
	popupWindow.creator = self;		// Insert PARENT identity into the popup window so it can find us later
//	alert("creator = "+popupWindow.creator.name);

/* debug: problem updating after external page linked (permission denied)
*/
//var targetWindow;
//		targetWindow = window.opener.document[windowName];
//		alert("targetname = "+targetWindow.name);
}

/*---------------------------------------------------------------------------------
	FORCE PARENT TO JUMP	

	TESTED example
		<a onclick="JavaScript:rpParentJump('/home.asp')"> Home </a>

*/
function rpParentJump(targetURL)
{
	var parentwin;
	var childwin;
	var lclException;
	try {

		parentwin = window.opener.document;
		parentwin.location = targetURL;

	}	catch(lclException)	{

		try {
		//Future: figure out where to save child window
			childwin = rpOpenWindow('',targetURL,0,0,'');

		} catch (lclException) {

		}
	}

//OLD	// Old method: set "creator" from caller.  But..clicking/refreshing pop looses creator.
//OLD	//	parentwin = window.creator;
// See if original window information is present
//	if ((parentwin == undefined) ||(parentwin == null) || (parentwin == '')) {
//	alert('No parent! Opening new window.');
//	} else if ((parentloc == undefined) ||(parentloc == null) || (parentloc == '')) {
//	alert('No parent! Opening new window.');
//	} else {
//	alert("x=" + window.creator.location)
//	}

}

/*---------------------------------------------------------------------------------
	FORCE CURRENT PAGE TO JUMP	

	TESTED example
		<a onclick="JavaScript:rpSelfJump('/home.asp')"> Home </a>

	Why? We want to open a popup AND jump to a new location.
	TESTED example
		<a onclick="JavaScript:rpLaunchPopupSized('/popup.asp',100,500);rpSelfJump('/home.asp')"> Home </a>

*/
function rpSelfJump(targetURL)
{
	self.location=targetURL
}

/*---------------------------------------------------------------------------------
	CLOSE CURRENT WINDOW

	TESTED example
		<a onclick="JavaScript:rpCloseMe()"> Close this form </a>

*/
function rpCloseMe()
{
	window.close()
}
function rpCloseWindow(targetWin)
{
	window.close(targetWin)
}

/*---------------------------------------------------------------------------------
	MAKE POPUP REAPPEAR AFTER A SPECIFIED PERIOD OF TIME
	(recommended for popups)

	TESTED example
		<body onBlur="rpRestoreWindowOnTopTimer(5000)">	

	On blur occurs at the time a window is hidden.  
	Example causes window to reappear 5 seconds after it is minimized (or partially obscured)

*/
function rpRestoreWindowOnTopTimer(milliseconds)
{
    setTimeout("self.focus()",milliseconds);
}

/*---------------------------------------------------------------------------------
	FORCE POPUP TO OPEN ON TOP
	(obnoxious)

	TESTED example
		<body onload="rpForceWindowOnTop()">
	
	To force popup to open on top and prevent other instances of browsers from getting on top
	(reasserts itself every time it is blured)
	TESTED example
		<body onload="rpForceWindowOnTop()" onBlur="rpForceWindowOnTop()">	

*/
function rpForceWindowOnTop(){
	window.focus();
	window.alwaysRaised = true;
}


/*---------------------------------------------------------------------------------
	DISPLAY MESSAGE
		Incredibly useful for debugging too!
	
	TESTED example
		<a onmouseover="JavaScript:rpAlert('You moved the mouse!')"> MouseOver Message </a>
		
*/
function rpAlert(argMessage)
{
	alert(argMessage);
}

/*---------------------------------------------------------------------------------
	DISPLAY MOUSE POSITION IN STATUS LINE
		Incredibly useful for debugging too!
	
	TESTED example
		<li><a onmousemove="rpReportMove('Hello Mouse')"><img src="http://www.rampant.com/art/earth.jpg"></a>
		
*/
function rpReportMove(argMessage)
{
	var	infomsg;
	infomsg = null;
	if (argMessage == null) {		// undefined may be a keyword too
		infomsg = "You moved the mouse to "
	} else {
		infomsg = argMessage;
	}
	infomsg = infomsg + " X=" + window.event.x + " Y=" + window.event.y;
	window.status = infomsg;
//	alert(infomsg);
//	document.write('<li>'+infomsg);
}


/*---------------------------------------------------------------------------------
	Created by RGS 3/5/03 to shorten the amount of junk in Java script includes
	imagerollover  = rpImageDefine('/web/art/1.gif')
	... onmouseover(
*/

function rpImageDefine(in_path)
{ 
	var newimg;
	var lclException;
	try {
		newimg=new Image;
		newimg.src=in_path;
		return newimg; 

	}	catch(lclException)	{
	}
}

/*---------------------------------------------------------------------------------
	Image Rollover.. Copies to target from any previoulsy defined image
	
	imagenorm  = rpImageDefine('/web/art/normal.gif')
	imageroll  = rpImageDefine('/web/art/fancy.gif')
	<body...  (definitions must happen before body command)
	... src="/web/art/normal.gif" name="nameofhtmlIMGcommand"
	... onmouseover="rpImageSwap('nameofhtmlIMGcommand','imageroll')" 
	... onmouseout ="rpImageSwap('nameofhtmlIMGcommand','imagenorm')"

	Created by RGS 3/5/03 to shorten the amount of junk in Java script includes
		
*/
function rpImageSwap(targetname,sourcename)
{ 
	var	previousimage;
	var lclException;
	try {
		previousimage = document.images[targetname].src;
		document.images[targetname].src=eval(sourcename + ".src"); 
		return previousimage;

	}	catch(lclException)	{
		return '';
	}
}


/*---------------------------------------------------------------------------------
  ---------------------------------------------------------------------------------
  Samples: not yet used, but copied from elsewhere
  ---------------------------------------------------------------------------------
  ---------------------------------------------------------------------------------
*/

/*---------------------------------------------------------------------------------
	VanFin: demoheader.js
*/
function codesnippid()
{
	alert('0');
	here = this.location.toString();
	len = here.length;
	value = here.substring(len-8,len-4);
	ID = value;
	alert(ID);
	document.write('<PARAM NAME="ID" VALUE=' + ID + '>');

//	sample: appletID = Math.floor((ID-(productID*1000))/100)
}

/*---------------------------------------------------------------------------------
	Our own invention to disable a control at certain times
		<a onmouseover="rpDisableControl('ProductHomeForm','dropdownproducts')">Hide dropdownproducts</a>
	
	Currently used for test, is able to change the value in a select box
		<form name="targetform">
			<select name="targetfield">
				<option value="1">Value 1</option>
				<option value="2">Value 2</option>
			</select>
			
			<a href="javascript:rpSetControl('targetform','targetfield','1')">Set to 1</a>
			<a href="javascript:rpSetControl('targetform','targetfield','2')">Set to 2</a>
		</form>
	
*/
function rpSetControl(parentForm, parentField, newvalue){
//		alert('has forms');
	if (document.forms != null) {
		/* DEBUG
				var enumidx;
				alert(document.location + ' has ' + document.forms.length + ' forms');
				for (enumidx=0; enumidx<document.forms.length; enumidx++) {
					alert('Form ' + (enumidx+1) + ' is named ' + document.forms(enumidx).name);
				}
				alert('Requested form is named ' + document.forms(parentForm).name);
				alert('Requested field is named ' + targetForm.elements[parentField].name);
		*/

	// get target form
		var targetForm = document.forms(parentForm);

//		targetForm.elements[parentField].selectedIndex = newvalue;
		targetForm.elements[parentField].value = newvalue;	// this must match the xxx in <option value="xxx">

	// Disable control
		//		targetForm.elements[parentField].disabled = true;
		
	// Display off (doesn't work)
		//		alert(parentField+'.display ' + targetForm.elements[parentField].display);
		//		targetForm.elements[parentField].display = false;
		//		targetForm.elements[parentField].face = 'arial';
		//		targetForm.elements[parentField].hidden = true;


	}
}


//var targetForm = window.opener.document.forms[parentForm];
//var targetForm = window.forms[parentForm];
//	alert(targetForm.elements[parentField].name);
//	targetForm.elements[parentField].visible = 0;
//}


/*---------------------------------------------------------------------------------
	End Resource.JS			ResourcePark, Inc.				www.ResourcePark.com
  ---------------------------------------------------------------------------------*/



