/**
$Id: pint_commonDEBUG.js,v 1.15 2003/07/03 17:25:16 cducker Exp $

Description:
	PINT Commonly used JavaScript functions and constants.

Dependencies:
	Inward:
		pint_initcleanup.js
	
	Outward:
		none	
	
Usage:
	n/a		
*/

/* ******** Constants *********************************** */
rootDirectory = "/br";
//rootDirectory = "/clients/viewsonic/site/brazil"; 
windowStatus = "";
/* ******** Common Window Settings ********************** */
defaultStatus = "";

/**
 * PINT_GetEventSource()
 * Takes as an argument the first argument to an event handler, and 
 * returns a reference to the object that generated the event
 *
 * @param e - first argument to an event handler
 *
 * @return reference to object that triggered event
 */
function PINT_GetEventSource(e)
{
	return ( //figure out where in the dom events come in on this browser
		(e && e.target) || 
		(window && window.event && window.event.srcElement)
	);	
}

/**
 * PINT_GetElementById()
 * Tries to find an element in the document 
 * by its id or name
 *
 * @param idname - id of element to locate
 */
function PINT_GetElementById(idname)
{
	var handle;

	if (document.getElementById) {
		handle = document.getElementById(idname);
		if (handle) return handle;
	}

	if (document.getElementByName) {
		handle = document.getElementByName(idname)[0];
		if (handle) return handle;
	}

	handle = document[idname];
	if (handle) return handle;

	if (document.all) {
		handle = document.all[idname];
		if (handle) return handle;
	}
	
	if (document.anchors) {
		handle = document.anchors[idname];
		if (handle) return handle;
	}
	
	if (document.links) {
		handle = document.links[idname];
		if (handle) return handle;
	}
	
	if (document.images) {
		handle = document.images[idname];
		if (handle) return handle;
	}
	
	if (document.embeds) {
		handle = document.embeds[idname];
		if (handle) return handle;
	}

	return handle;
}

/**
 * PINT_GetIdByElement()
 * Inverse of PINT_GetElementById, returns the id, 
 * or name, of a given element
 *
 * @param element - object whose id to retrieve
 */
function PINT_GetIdByElement(element)
	{
	if (!(element)) return undefined;
	if (element.id) return element.id;
	if (element.name) return element.name;
	return undefined;
	}

/**
 * PINT_ChangePageTitle()
 * Change title of current page. Use when initial title
 * tag value is optimized for Search Engines, but you 
 * want the title to be more descriptive for the visitor.
 *
 * @param   pageTitle  - new page title
 */	
function PINT_ChangePageTitle( pageTitle )
	{
	if(document.title.readOnly == true) document.title = pageTitle;
	} 	
	
/**
 * PINT_GetCurrentFileName()
 * Get name of current file from path name
 */
function PINT_GetCurrentFileName()
	{
	var URL = unescape( window.location.pathname );
	var start = URL.lastIndexOf( "/" ) + 1;
	var end = ( URL.indexOf( "?" ) > 0 ) ? URL.indexOf( "?" ) : URL.length;
	return( URL.substring( start, end ) );
	}	
/**
 * PINT_GetCurrentFilePath()
 * Get path to current file from path name
 */
function PINT_GetCurrentFilePath()
	{
	var URL = unescape( window.location.pathname );
	var start = URL.lastIndexOf( "/" );
	return( URL.substring( 0, start ) );
	}

/**
 * PINT_GetCurrentDirectory()
 * Get name of current directory from path name
 */			
function PINT_GetCurrentDirectory()
	{
	var filePath = PINT_GetCurrentFilePath();
	var directories = filePath.split("/");
	return directories.length && directories[ directories.length-1 ] != "" ? directories[ directories.length-1 ] : "";
	}

/**
 * PINT_GetBaseDirectory()
 * Get name of current directory from path name
 */			
function PINT_GetBaseDirectory()
	{
	var filePath = PINT_GetCurrentFilePath();
	filePath = filePath.substring( rootDirectory.length );
	var directories = filePath.split("/");
	return directories.length && directories[ 1 ] != "" ? directories[ 1 ] : "";
	}
	
/**
 * PINT_IsRootDirectory()
 * Determine if specified directory matches root directory
 *
 * @param directory - directory to check
 */
function PINT_IsRootDirectory( directory )
	{
	return directory == rootDirectory ? true : false;
	}

/**
 * PINT_FirstFocus()
 * Set cursor focus to first available form field
 * 
 * @param field - optional: reference to form input, otherwise defaults to the first element of the first form on the page
 */				
function PINT_FirstFocus()	
	{
	var elementref;
	var i=0;
	if (!(elementref = PINT_FirstFocus.arguments[0]))
		{
		if (!(document.forms[0])) return false;
		while ((elementref = document.forms[0].elements[i++]) && (elementref.type == 'hidden')) {};
		}
	if (!(elementref)) return false;
	elementref.focus();
	return true;
	}

/**
 * PINT_OnMouseOverHandler()
 * Handler for all onmouseover events. Must be explicitly set as 
 * the function handler.
 * 
 * @param e		event
 * @return		True.
 *
 */
function PINT_OnMouseOverHandler(e) 
	{
	e = (e) ? e : ((window.event) ? window.event : "")
	if (e) 
		{
		var eventsource = PINT_GetEventSource(e);

		/*
		if( eval( 'typeof(VS_ROtriggers)' ) != 'undefined' &&  
		    eval( 'typeof(VS_ROtriggers[eventsource.id])' ) != 'undefined' )
			VS_RORollover(e);
		*/	
		
		if( eval( 'typeof(PINT_MenuTriggers)' ) != 'undefined' && 
		    eval( 'typeof(PINT_MenuTriggers[eventsource.id])' ) != 'undefined' )
			PINT_MenuPopUp(e);

		if( eval( 'typeof(PINT_ROtriggers)' ) != 'undefined' &&  
		    eval( 'typeof(PINT_ROtriggers[eventsource.name])' ) != 'undefined' )
			PINT_RORollover(e);
			
		PINT_SetWindowStatus();	
		}
	return true;	
	}
	
/**
 * PINT_OnMouseOutHandler()
 * Handler for all onmouseout events. Must be explicitly set as 
 * the function handler.
 * 
 * @param e		event
 * @return		True.
 *
 */	
function PINT_OnMouseOutHandler(e) 
	{
	e = (e) ? e : ((window.event) ? window.event : "")
	if (e) 
		{
		var eventsource = PINT_GetEventSource(e);
		
		if( eval( 'typeof(PINT_MenuTriggers)' ) != 'undefined' && 
		    eval( 'typeof(PINT_MenuTriggers[eventsource.id])' ) != 'undefined' )
			PINT_MenuPopDown(e);

		if( eval( 'typeof(PINT_ROtriggers)' ) != 'undefined' &&  
		    eval( 'typeof(PINT_ROtriggers[eventsource.name])' ) != 'undefined' )
			PINT_RORollout(e);
	
		/*
		if( eval( 'typeof(VS_ROtriggers)' ) != 'undefined' &&  
	    	eval( 'typeof(VS_ROtriggers[eventsource.id])' ) != 'undefined' )
			VS_RORollout(e);
		*/	
		}
	return true;
	}

/**
 * PINT_SetWindowStatus()
 * Set status bar message from parameter or global variable.
 * 
 * @param e		event
 * @return		True.
 *
 */	
function PINT_SetWindowStatus()
	{
	// if no arguments are passed, look for global windowStatus varible
	if( PINT_SetWindowStatus.arguments.length == 0 )
		{
		if( typeof(windowStatus) != 'undefined' && windowStatus != "" )
			{
			window.status = windowStatus;
			windowStatus = "";
			}
		}	
	else
		window.status = PINT_SetWindowStatus.arguments[0];
	return true;
	}	
	
/* simple redirect for drop downs */
function redirect(urlobject) 
	{
	var urlint = urlobject.selectedIndex;
	var numurls = urlobject.length;
	if( typeof( urlobject.options[urlint].value ) == 'string' && urlobject.options[urlint].value != "" )
		location = urlobject.options[urlint].value;
	}	

var PINTPW_capablePopupWin = true; // assume popup window capable
var PINTPW_popAnchors;
PINTPW_capablePopupWin = ((PINTPW_popAnchors = new Array()) ? true : false );

var PINTPW_featureBasic = 'alwaysLowered=no,alwaysRaised=no,dependent=yes,directories=no,hotkeys=no,'; // features that all popup windows will have
var PINTPW_featureNoChrome = 'location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,'; // features that no chrome popup windows will have

//enumerated constant types for poptype
var PINTPW_FULLCHROME = 5;
var PINTPW_NOCHROME = 6;

//enumerated constant types for positioning
var PINTPW_SCREENCENTER = 10;
var PINTPW_WINDOWCENTER = 11;
var PINTPW_FREE = 12;

/************************************* public methods ****************************/


function PINTPW_InitPopupWinFullChrome(anchorid, newurl, windowname, featurestring)
{
	PINTPW_InitPopupWin(anchorid, newurl, windowname, FULLCHROME, featurestring);
}


/*********************************************************
function PINTPW_InitPopupWin
Initializes an anchor to popup a new browser window onclick. 
To make popup windows re-use themselves, give them all the same name.
Arguments:
	1. name/id of an anchor element
	2. url to open in new browser window
	3. name of new browser window, or '' (null string)
	4. extra feature string as passed to window.open, or '' (null string)
Returns: false, to cancel the anchor href
*/
function PINTPW_InitPopupWin(anchorid, newurl, windowname, poptype, popWidth, popHeight, positioning, featurestring)
{
	if (!(PINTPW_capablePopupWin)) return false;
	if (!(window)) return false;	

	if (PINTPW_popAnchors[anchorid]) return false; // already a popup window attatched to this anchor

	PINTPW_popAnchors[anchorid] = new Array();
	PINTPW_popAnchors[anchorid]['newurl'] = newurl;
	PINTPW_popAnchors[anchorid]['windowname'] = windowname;
	PINTPW_popAnchors[anchorid]['poptype'] = poptype;
	PINTPW_popAnchors[anchorid]['popWidth'] = popWidth;
	PINTPW_popAnchors[anchorid]['popHeight'] = popHeight;
	PINTPW_popAnchors[anchorid]['positioning'] = positioning;
	PINTPW_popAnchors[anchorid]['featurestring'] = featurestring;

	alert( anchorid );
	document.anchors[anchorid].onclick = PINTPW_Pop;
	//document.forms['formname'][anchorid].onclick = PINTPW_PopH;
	return false;
}


/*************************************************************
PINTPW_InitPopupWinNoChrome
Initializes an anchor element that, onclick, pops up a no-chrome window to specification. 
To make popup windows re-use themselves, give them all the same name.
Arguments:
	1. name/id of an anchor element
	2. url to open in window
	3. name of new browser window, or '' (null string)
	4. horizontal size of new window in pixels
	5. vertical size of new window in pixels
	6. constant indicating the positioning of the new window. Possible values:
		PINTPW_SCREENCENTER	centered in screen
		PINTPW_WINDOWCENTER	centered in parent browser window
		PINTPW_FREE		pops up wherever the client feels like it
Returns: always false
*/
function PINTPW_InitPopupWinNoChrome(anchorid, newurl, windowname, popWidth, popHeight, positioning)
{
	if (!(PINTPW_capablePopupWin)) return false;
	if (!(window)) return false;	

	return PINTPW_InitPopupWin(anchorid, newurl, windowname, PINTPW_NOCHROME, popWidth, popHeight, positioning, '');
}

function PINTPW_Pop(newurl, windowname, poptype, popWidth, popHeight, positioning, featurestring)
	{
	var winLeft, winTop, winWidth, winHeight;
	var newXlocation, newYlocation;
	var allfeature = PINTPW_featureBasic;

	if (popWidth && popHeight)
		{
		allfeature += 
		'innerWidth=' + popWidth + ',' +
		'innerHeight=' + popHeight + ',' +
		'width=' + popWidth + ',' +
		'height=' + popHeight + ',';

		if( positioning == PINTPW_SCREENCENTER )
			{
			if (screen)
				{
				winWidth = screen.availWidth;// only in netscape?
				winHeight = screen.availHeight;
				winLeft = 0;
				winTop = 0;

				if (winWidth)
					{
					newXlocation = Math.floor(((winWidth - popWidth) / 2) + winLeft);
					newYlocation = Math.floor(((winHeight - popHeight) / 2) + winTop);
					allfeature += 'left=' + newXlocation + ','; // for ie
					allfeature += 'top=' + newYlocation + ',';
					allfeature += 'screenx=' + newXlocation + ','; //for netscape
					allfeature += 'screeny=' + newYlocation + ',';
					}					
				}
			}
		else if( positioning ==	PINTPW_WINDOWCENTER )
			{
			if (window.innerWidth)// only netscape can do window centering
				{
				winLeft = (window.screenLeft || window.screenX);
				winTop = (window.screenTop || window.screenY);
				winWidth = (window.innerWidth || window.width ); 
				winHeight = (window.innerHeight || window.height);
				}
			else // so do screen centering instead
				{
				winWidth = screen.availWidth;
				winHeight = screen.availHeight;
				winLeft = 0;
				winTop = 0;
				}

			if (winWidth)
				{
				newXlocation = Math.floor(((winWidth - popWidth) / 2) + winLeft);
				newYlocation = Math.floor(((winHeight - popHeight) / 2) + winTop);
				allfeature += 'left=' + newXlocation + ','; // for ie
				allfeature += 'top=' + newYlocation + ',';
				allfeature += 'screenx=' + newXlocation + ','; //for netscape
				allfeature += 'screeny=' + newYlocation + ',';
				}					
			}
		}
	if (poptype == PINTPW_NOCHROME)
		{
		allfeature += PINTPW_featureNoChrome;
		}
	allfeature += featurestring;
	return window.open(newurl, windowname, allfeature);
	}


/********************************** private methods ****************************/



function PINTPW_PopH(e)
{
	if (!(PINTPW_capablePopupWin)) return false;

	var eventSource;
	if (!(eventSource = PINT_GetEventSource(e))) return false;
//	alert(eventSource.name);
	var anchorname;
	if (!(anchorname = eventSource.name)) return false;

	return (PINTPW_popAnchors[anchorname] ?
		PINTPW_Pop(
			PINTPW_popAnchors[anchorname]['newurl'], 
			PINTPW_popAnchors[anchorname]['windowname'], 
			PINTPW_popAnchors[anchorname]['poptype'], 
			PINTPW_popAnchors[anchorname]['popWidth'], 
			PINTPW_popAnchors[anchorname]['popHeight'], 
			PINTPW_popAnchors[anchorname]['positioning'], 
			PINTPW_popAnchors[anchorname]['featurestring']
		) : false );


/*	if (PINTPW_popAnchors[anchorname])
	{
		alert(PINTPW_popAnchors[anchorname]['featurestring']);
		window.open(PINTPW_popAnchors[anchorname]['newurl'], PINTPW_popAnchors[anchorname]['windowname'], PINTPW_popAnchors[anchorname]['featurestring']);
	}

	return false;*/
}
	
// old school popup for projector calculator //

function pop_up(location,width,height) {	
 window.open( location , 'pop_up_window' , eval("'status=no,location=no,menubar=no,toolbar=no,resizable=no,scrollbars=no," + "width=" + width + ",height=" + height + "'"));
}
