/* CONFIDENTIAL AND PROPRIETARY SOURCE CODE OF yet2.com

Copyright (c) 1999-2003 by yet2.com, Inc.  All Rights Reserved.
The copyright notice(s) in this Source Code does not indicate actual
or intended publication of this Source Code.
File: client.js

---------------------------------------------------------------------------
js_function:	newWindow
input:			url - the URL of the page to display in the new window.
				windowName - the name of the new window for reference. y2_child
				is the default.
				width - number of pixels wide to make the window. 500 is default.
				height - number of pixels high to make the window. 400 is default.
				center - when true the window is centered on the screen.
				resizeable - when true the window is resizable.
				scrollbars - when true the window has scrollbars.
				focusWindow - when true the window is brought to the front, default
					is true.
				statusBar - when true the window has a status bar
returns:		none
description:	Opens a new browser window with no menubar, toolbar, location bar,
				directories bar or status bar.
---------------------------------------------------------------------------*/
function newWindow( url, windowName, width, height, center, resizeable, scrollbars, focusWindow, statusBar )
{
	var child;
	var windowFeatures = "";
	var top;
	var left;

	// If the opening window doesn't have a name then give it one.
	if ( window.name == "" ) window.name = "y2_parent";
	
	// Set a default window name if it isn't defined.
	if ( windowName == null ) windowName = "y2_child";
	
	// If the opening window doesn't have a name then give it one.
	if ( window.name == "" ) window.name = "y2_parent";

	// Set a default window name if it isn't defined.
	if ( windowName == null ) windowName = "y2_child";

	// Set a default width and height when they are not defined.
	if ( height == null ) height = 480;
	if ( width == null ) width = 640;

	// Add the width and height to the window features string.
	windowFeatures = "width=" + width + ",height=" + height;

	// Make the window centered if requested.
	if ( ( center != null ) && center )
		{
		top = ( screen.height - height ) / 2;
		left = ( screen.width - width ) / 2;
		windowFeatures += ",top=" + top + ",left=" + left;
		}

	// Make the window resizable if requested.
	if ( ( resizeable != null ) && resizeable ) windowFeatures += ",resizable=yes";
	
	// Make the window have scrollbars if requested.
	if ( ( scrollbars != null ) && scrollbars ) windowFeatures += ",scrollbars=yes";
	
	if ( ( statusBar != null ) && statusBar )  windowFeatures += ",status=yes";
	
	// Open the window.
	child = window.open( url, windowName, windowFeatures );

	// Make sure the child's opener is set correctly.
	if ( ( child != null ) && ( child.opener == null ) ) child.opener = window;
	
	// If the window should be focused then tell the child to focus, default to true.
	if ( focusWindow == null )  focusWindow = true;
	if ( focusWindow && ( child != null ) ) child.focus( );

} // newWindow
function helpWindow( url, windowName )
{
	var child;
	var windowFeatures = "directories=yes,hotkeys=yes,location=yes,menubar=yes,status=yes,toolbar=yes,width=640,height=480,resizable=yes,scrollbars=yes";;
	var top;
	var left;
	var bookmark;
	var parentURL;

	// If the opening window doesn't have a name then give it one.
	if ( window.name == "" ) window.name = "y2_parent";
	
	// Set a default window name if it isn't defined.
	if ( windowName == null ) windowName = "y2_help";
	
	// Create the url.
	parentURL = "?parent=" + escape( document.URL );
	index = url.indexOf( "#" );
	if ( index == -1 )
		{
		url += parentURL;
		}
	else
		{
		bookmark = url.substring( index + 1 );
		url = url.substring( 0, index ) + parentURL + "#" + bookmark;
		}
	
	top = ( screen.height - 480 ) / 2;
	left = ( screen.width - 640 ) / 2;
	windowFeatures += ",top=" + top + ",left=" + left;

	// Open the window.
	child = window.open( url, windowName, windowFeatures );
	child.opener = window;
	child.focus( );
}

/*---------------------------------------------------------------------------
js_function:	openChildWin
input:			windowURL - url of the page to open, passed window.open().
				windowName - name of the window passed to window.open().
returns:		result of window.open() call
description:	Used to open standard child window going to a third party site.
---------------------------------------------------------------------------*/
function openChildWin( windowURL, windowName ) 
{ 
	return window.open( windowURL, windowName, 'width=790, height=490, toolbar=1, location=1, directories=0, status=0, menuBar=0, scrollBars=1, resizable=1' ); 
}

/*---------------------------------------------------------------------------
js_function:	formCheckAndSubmit
input:			form - the form the submit is being done through.
returns:		false to avoid navigation on href.
description:	Only submits if form.isSubmitting is not already true.
---------------------------------------------------------------------------*/
function formCheckAndSubmit( form )
{
	if ( !form.isSubmitting )
		{
		form.isSubmitting = true;
		form.submit();
		}
	return false;
}

/*---------------------------------------------------------------------------
js_function:	submitCommand
input:			command - the command value to set in the command field.
				formName - the name of the form that owns the field.
returns:		false to avoid navigation on href.
description:	Sets the value in the specified form's command field and
				submits the form.
---------------------------------------------------------------------------*/
function submitCommand( commandValue, formName )
{
	return submitValue( formName, "command", commandValue );

} // submitCommand
/*---------------------------------------------------------------------------
js_function:	submitValue
input:			formName - the name of the form that owns the field.
				field - the name of the field whose value to set.
				value - the new field value.
returns:		false to avoid navigation on href.
description:	Sets the value in the specified form's field and
				submits the form.
---------------------------------------------------------------------------*/
function submitValue( formName, field, value )
{
	var theForm;

	theForm = document[ formName ];
	theForm.elements[ field ].value = value;
	theForm.submit( );

	return false;

} // submitValue
/*---------------------------------------------------------------------------
js_function:	reportError
input:			msg - the error message.
				url - the name of the page that caused the error.
				line - the script line that caused the error.
returns:		none
description:	Displays an alert with error information when a client side
				script error occurs.
---------------------------------------------------------------------------*/
function reportError( msg, url, line )
{
	var errString;

	errString = "Err: " + msg + " on line: " + line + "\nURL: " + url  + "\nWeb: " +
			navigator.appName + " " + navigator.appName + " " + navigator.appVersion;

	alert( errString );

	return true;

} // reportError
/*---------------------------------------------------------------------------
js_function:	dataChanged
input:			formName - the name of the form the contains the control.
				changedName - the name of the control that holds the changes.
				controlName - the name of the control that changed.
				documentObject - optional document object so another window
				can trigger a change on its parent.
returns:		none
description:	Adds the control name to the changed control value if it
				is not already there. The changed control holds a string
				of control names that is comma separated so the server can
				determine which inputs have changed.
---------------------------------------------------------------------------*/
function dataChanged( formName, changedName, controlName, documentObject )
{
	var changes;
	var changesControl;

	// Find the changes control on the form when the document object is
	// null otherwise it uses the document object to find the control.
	if ( documentObject == null )
		changesControl = document[ formName ][ changedName ];
	else
		changesControl = documentObject[ formName ][ changedName ];

	if ( changesControl != null )
		{
		// Get the current list of changed controls.
		changes = changesControl.value;

		// If the control name is not in the string add it.
		if ( changes.indexOf( controlName, 0 ) == -1 )
			{
			if ( changes == "" )
				changes = controlName;
			else
				changes +=  ( "," + controlName );
			changesControl.value = changes;
			}
		}

} // dataChanged
/*---------------------------------------------------------------------------
js_function:	getTaxoValue
input:			DescString - Description containing text and values
returns:		Comma-delimited string of values
description:	Restores taxonomy points value string on back or error
---------------------------------------------------------------------------*/
function getTaxoValue( DescString)
{
	var valueString = "";
	var offset = 1;
	while ( DescString.length > 0 )
	{
		if (DescString.indexOf("\r") > -1) offset = DescString.indexOf("\r")-6;
		else offset = DescString.length - 6;
		if (offset < 0) break;
		valueString += valueString.length > 0 ? "," : "";
		//alert( "offset:"+offset );
		valueString +=  DescString.substr( offset, 6 );
		//alert( "valueString:"+valueString );
		offset = Math.max( DescString.indexOf("\r")+1, offset + 6 );
		DescString = DescString.substr( offset );
		//alert( "DescString:"+DescString );
	}
	return( valueString);
} //getTaxoValue
/*---------------------------------------------------------------------------
	js_function:	TaxoPick
	input:			FormName - Name of the form containing the inputs
					DescInputName - (optional) Name of input within <FormName> 
					to write description string.
					Link - (boolean) true = single select, otherwise multi-select
					URL - (optional) If your base is not PSUser like for JADE, pass
						the relative path needed to get to PSUser.
	returns:		none - sets value into opener inputs
	
	description:	Opens the taxonomy picker window with passed values checked.
					On close, it writes values into opener inputs.
---------------------------------------------------------------------------*/
function TaxoPick(FormName, DescInputName, Mode, URL)
{	
	var Desc = document[ FormName ][ DescInputName ].value;
	var Values = getTaxoValue( Desc );
	var urlString = ((URL) ? URL : "") + "y2_taxonomy.htm?FORMNAME=";
	urlString += FormName;
	urlString += "&DESCNAME=" + DescInputName;
	urlString += "&VALUE=" + Values;
	urlString += (Mode) ? "&MODE="+Mode : "" ;
	document[ FormName ][ DescInputName ].blur();
	newWindow( urlString , "Categories", 500, 400, true, true, true );
} // TaxoPick
/*---------------------------------------------------------------------------
js_function:	calWindow
input:			url - the URL of the page to display in the new window.
				windowName - the name of the new window for reference. y2_child
				is the default.
				width - number of pixels wide to make the window. 500 is default.
				height - number of pixels high to make the window. 400 is default.
				center - when true the window is centered on the screen.
				resizeable - when true the window is resizable.
				scrollbars - when true the window has scrollbars.
returns:		none
description:	Opens a new browser window with no menubar, toolbar, location bar,
				directories bar or status bar.
---------------------------------------------------------------------------*/
function calWindow( url, windowName, width, height, center, resizeable, scrollbars, focusWindow )
{
	var child;
	var windowFeatures = "";
	var top;
	var left;

	// If the opening window doesn't have a name then give it one.
	if ( window.name == "" )
		window.name = "y2_parent";

	// Set a default window name if it isn't defined.
	if ( windowName == null )
		windowName = "y2_child";

	// Set a default width and height when they are not defined.
	if ( width == null )
		width = 500;
	if ( height == null )
		height = 400;

	// Add the width and height to the window features string.
	windowFeatures = "width=" + width + ",height=" + height;

	// Make the window centered if requested.
	if ( ( center != null ) && center )
		{
		top = ( screen.height - height ) / 2;
		left = ( screen.width - width ) / 2;
		windowFeatures += ",top=" + top + ",left=" + left;
		}

	// Make the window resizable if requested.
	if ( ( resizeable != null ) && resizeable )
		windowFeatures += ",resizable=yes";

	// Make the window have scrollbars if requested.
	if ( ( scrollbars != null ) && scrollbars )
		windowFeatures += ",scrollbars=yes";

	// Open the window.
	child = window.open( url, windowName, windowFeatures );

	// Make sure the child's opener is set correctly.
	if ( ( child != null ) && ( child.opener == null ) )
		child.opener = window;
	
	// If the window should be focused then tell the child to focus.
	if ( ( focusWindow != null ) && ( child != null ) )
		child.focus( );

	// Return the window object for calendar
	return( child);

} // calWindow
/*---------------------------------------------------------------------------
js_function:	gotoListPage
input:			formName - the HTML name of the list form.
				pageIndexName - the HTML name of the list page index input.
				direction - the direction that the user wants to navigate.
				clearCommand - optional boolean when true will clear the
				command input value.
returns:		none
description:	Sets the page index input value and clears the command
				value if there is an command field.
---------------------------------------------------------------------------*/
function gotoListPage( formName, pageIndexName, direction, clearCommand )
{
	var pageIndexField;
	var pageIndex;
	var commandField;
	
	// Get the page index input field and value.
	pageIndexField = document[ formName ].elements[ pageIndexName ];
	pageIndex = pageIndexField.value;
	
	// Determine the page index based on the direction specified.
	switch ( direction )
		{
		case "0" :
			pageIndex = 1;
			break;

		case "-1" :
			if ( pageIndex > 1 )
				pageIndex--;
			else
				pageIndex = 1;
			break;

		case "1" :
			pageIndex++;
			break;

		case "n" :
			pageIndex = "n";
			break;

		default :
			pageIndex = 1;
		}

	// Set the specified page index in the page input field.
	pageIndexField.value = pageIndex;
	
	// Get the command hidden field and clear it when requested and it exists.
	if ( ( clearCommand == null ) || clearCommand )
		{
		commandField = document[ formName ].elements[ "command" ];
		if ( commandField != null )
			commandField.value = "";
		}

} // gotoListPage
/*---------------------------------------------------------------------------
js_function:	checkSpelling
input:			none
returns:		none
description:	Loops through all text & textarea fields in the form,
				calling the SpellCheckApplet to check the spelling of the
				data in those fields.  Note that this routine assumes 
				that the form contains the applet,
				SpellCheckApplet, as an element.
---------------------------------------------------------------------------*/
function checkSpelling( textObject ) 
{
	var foundOne = false;
    if( !navigator.javaEnabled( ) ) 
	{
        alert( "Java must be enabled in your browser to use the spelling checker." );
        return;
    }
	if( textObject )
	{
		if( textObject.value )
		{
			document.SpellCheckApplet.text = textObject.value;
    		if ( document.SpellCheckApplet.check() == 1 ) textObject.value = document.SpellCheckApplet.text;
		}
		else
			alert( "Warning! Spell checking was skipped because there was no text to check." );
		return;
	}
	for ( var i = 0; i < document.forms[0].elements.length; i++ ) 
	{
		if (document.forms[0].elements[ i ].type.indexOf('text') > -1)
		{
			if ( document.forms[0].elements[ i ].value )
			{
				foundOne = true;
				document.SpellCheckApplet.text = document.forms[0].elements[ i ].value;
	    		if ( document.SpellCheckApplet.check() == 1 ) 
					document.forms[0].elements[ i ].value = document.SpellCheckApplet.text;
			}
		}
	} 
	if( !foundOne )
		alert( "Warning! Spell checking was skipped because there was no text to check." );
	
} // checkSpelling
/*---------------------------------------------------------------------------
js_function:	tbInitImages
input:			none
returns:		none
description:	Initializes images required by the toolbar.
---------------------------------------------------------------------------*/
function tbInitImages( isMember, isAdmin, isYet2Admin, path )
{
	var imageNames;
	var name;
	var imageList;
	var index;

	if ( document.images )
		{
		// Set the default path if necessary.
		if ( path == null )
			path = "/en/images/toolbar/";

		// Create the base array of names for the guest toolbar.
		imageNames = new Array( "on_find_technology", "on_find_technology_text", 
								"on_list_technology", "on_list_technology_text", 
								"on_insight", "on_insight_text", 
								"on_using_site", "on_using_site_text", 
								"on_about_us", "on_about_us_text", 
								"on_dummy_text", "on_home", "on_help" );

		// Add image names for member and admin buttons.
		if ( isMember )
			{
			imageNames[ imageNames.length ] = "on_myyet2";
			imageNames[ imageNames.length ] = "on_myyet2_text";
			imageNames[ imageNames.length ] = "on_support";
			imageNames[ imageNames.length ] = "on_my_profile";
			imageNames[ imageNames.length ] = "on_logout";
			if ( isAdmin )
				imageNames[ imageNames.length ] = "on_admin";
			if ( isYet2Admin )
				imageNames[ imageNames.length ] = "on_yet2_admin";
			}

		// Create an array of images.
		imageList = new Array( );
		for ( index in imageNames )
			{
			imageList[ index ] = new Image( );
			imageList[ index ].src = path + imageNames[ index ] + ".gif";
			}
		}

} // tbInitImages
/*---------------------------------------------------------------------------
js_function:	tbMouseOver
input:			imageName - name of image that mouse is over.
returns:		none
description:	Sets the toolbar image to the on image.
---------------------------------------------------------------------------*/
function tbMouseOver( imageName, path )
{
	if ( document.images )
		{
		if ( path == null )
			path = "/en/images/toolbar/";
		document.images[ imageName + "_tb" ].src = path + "on_" + imageName + ".gif";
		}

} // tbMouseOver
/*---------------------------------------------------------------------------
js_function:	tbPrimaryMouseOver
input:			imageName - name of image that mouse is over.
returns:		none
description:	Sets the toolbar image to the on image and changes the secondary rollover image.
---------------------------------------------------------------------------*/
function tbPrimaryMouseOver( imageName, isHomePage, path )
{
	if ( document.images )
		{
		if ( path == null )
			path = "/en/images/toolbar/";
		document.images[ imageName + "_tb" ].src = path + "on_" + imageName + ".gif";
		}

} // tbPrimaryMouseOver
/*---------------------------------------------------------------------------
js_function:	tbMouseOut
input:			imageName - name of image that mouse is leaving.
returns:		none
description:	Sets the toolbar image to the off image.
---------------------------------------------------------------------------*/
function tbMouseOut( imageName, path )
{
	if ( document.images )
		{
		if ( path == null )
			path = "/en/images/toolbar/";
		document.images[ imageName + "_tb" ].src = path + "off_" + imageName + ".gif";
		}

} // tbMouseOut

/*---------------------------------------------------------------------------
js_function:	tbPrimaryMouseOut
input:			imageName - name of image that mouse is leaving.
returns:		none
description:	Sets the toolbar image to the off image and hides the secondary rollover image.
---------------------------------------------------------------------------*/
function tbPrimaryMouseOut( imageName, isHomePage, path)
{
	if ( document.images )
		{
		if ( path == null )
			path = "/en/images/toolbar/";
		document.images[ imageName + "_tb" ].src = path + "off_" + imageName + ".gif";
		}

} // tbPrimaryMouseOut
/*---------------------------------------------------------------------------
js_function:	checkLength
input:			controlToCheck - the control whose value needs its length checked.
				maxLength - maximum number of characters allowed in the field
				fieldName - Descriptive name of the field, used in an error
							message if too many characters entered.
				truncate - Boolean to control truncation of the data
returns:		none
description:	Checks the length of the specified field and alerts the user
				if too many characters entered.  Note that you can use the HTML
				maxlength property on an input text field to check the field
				length, so it is not necessary to use this routine with input
				text fields.  
	
				There are two ways to call this routine:
					truncate=true: usually used with onblur or onkeyup events.
						Data will be truncated.
					truncate=false: usually used as a test before form submit
						is done.  Data is not truncated.
---------------------------------------------------------------------------*/
function checkLength( controlToCheck, maxLength, fieldName, truncate )
{ // checkLength
	if ( controlToCheck != null )
		{
		// Place the value of the control into a string variable.
		// You must have a string value in order to use the length property.
		var stringValue = controlToCheck.value + "";
		
		// Get the length
		var actualLength = stringValue.length;

		// Check the length
		if ( actualLength > maxLength )
			{
			var message = "";

			if( truncate )
				{
				controlToCheck.value = stringValue.substring( 0, maxLength );
				message = "Warning!  The " + 
					fieldName + " field has been truncated.  You entered " + 
					actualLength + " characters, but the maximum number is " +
					maxLength + ".";
				}
			else
				{
				message = "Warning!  Your action has not been processed.  " +
					"You entered " + actualLength + " characters in the " +
					fieldName + " field, but the maximum number is " +
					maxLength + ".";
				}
							
			// Notify the user
			alert( message );
							
			// Return false
			return false;
			}
		}

	return true;
} // checkLength
/*---------------------------------------------------------------------------
js_function:	IndustryPicker
input:		formname - the name of the form in the document
			fieldname - the name of the field in the form to be updated when the
				operation is successful.
			remove - boolean when true the taxonomy choice dialog will display
					otherwise the industry will be cleared.
			changed - boolean when true indicates that a hidden field called "changed" will be
				updated
			multi - boolean, determines if the picker allows multiple selections
returns:		none
description:	Displays the taxonomy choice dialog or clears the industry
				field.
---------------------------------------------------------------------------*/
function IndustryPicker( formname, fieldname, remove, changed, multipick )
{
	// If the user is selecting an industry, display the taxonomy choice list
	// otherwise just clear the industry name field on the form.
	if ( remove )
		document[ formname ].elements[ fieldname ].value = "";
	else
		if ( multipick ) TaxoPick( formname, fieldname );
		else TaxoPick( formname, fieldname, 'LINK' );

	// Mark the field as changed.
	if ( changed ) dataChanged( formname, "changed", fieldname );
	
} // IndustryPicker
/*---------------------------------------------------------------------------
js_function:	formNav
input:			formname - the name of the form
				page - the section name of the form to be
				displayed.
returns:		none
description:	Saves the form then displays the specified section of the
				form.
---------------------------------------------------------------------------*/
function formNav( formname, page )
{
	// Set the specified page in the page input field.
	document[ formname ].elements[ "page" ].value = page;
	
	// Submit the document.
	document[ formname ].submit();

} //formNav

function formNavAction( formname, page, command )
{
	// Set the specified command in the command input field.
	document[ formname ].elements[ "command" ].value = command;
	
	formNav( formname, page );

} // formNavAction

function formNavActionURI( formname, page, command, URI )
{
	// Set the specified URI in the URI input field.
	document[ formname ].elements[ "URI" ].value = URI;
	
	formNavAction( formname, page, command );
	
} // formNavActionURI


function formNavLink( formname, fieldname, item, page, command )
{
	// Make sure the item's radio button is selected.
	if ( document[ formname ][ fieldname ][ item ] == null )
		document[ formname ][ fieldname ].checked = true;
	else
		document[ formname ][ fieldname ][ item ].checked = true;
		
	formNavAction( formname, page, command );
	
} // formNavLink

function y2_forgot_password( theUsername )
{
	window.location="/PSUser/y2_forgot_password.htm?" + escape(theUsername);

} // y2_forgot_password

function switchLanguage( langValue )
{
	var docuLink = document.location.href;
	var altLang = ( langValue == "en" ) ? "ja" : "en" ;
	var offset = docuLink.indexOf( "language="+langValue );
	if ( offset  > -1 )
		docuLink = docuLink.replace(  "language="+langValue, "language=" + altLang );
	else
		{
		offset = docuLink.indexOf( "?" );
		if ( offset  > -1)
			docuLink += "&";
		else
			docuLink += "?";
		docuLink += "language="+ altLang ;
		}	
	document.location = docuLink;
}

function getValueFromRadio( radioObj )
{
	if( radioObj == null )
		return null;

	// When there is only one option in the radio list, length is null.
	// The radio button is probably selected, but we'll check anyway.
	if ( radioObj.length == null )
	{
		if( radioObj.checked ) 
			return radioObj.value;
		else 
			return null;
	}

	// Two or more options in the radio list.
	for( var i = 0; i < radioObj.length; i++)
	{
		if( radioObj[i].checked )
		{
			return radioObj[i].value;
		}
	}
	return null;
}

function submitComboCommand( commandValue, formName, previewDisplayPage, tpVersionID, documentType )
{
	if( (commandValue == 'commandPreview') && (previewDisplayPage != "") && (tpVersionID != null) ) 
	{
		var tpVersionIDArray = tpVersionID.split( "*" );		
		var tpID = tpVersionIDArray[0];
		var url = '/app/list/'+ previewDisplayPage + '?id=' + tpID + '&type=' + documentType + '&preview=on&pop=y';
		newWindow( url, 'learn', 800, 540, false, true, true ); 
		return false;
	}
	else
		return submitCommand( commandValue, formName );
}

