//
// +--------------------------------------------------------------------------+
// | phpMyChat version 0.15.0                                                 |
// +--------------------------------------------------------------------------+
// | Copyright (c) 2000-2001 The phpHeaven-team                               |
// +--------------------------------------------------------------------------+
// | License:  GNU/GPL - http://www.gnu.org/copyleft/gpl.html                 |
// +--------------------------------------------------------------------------+
// | Set of JavaScript functions used by the starting form of the chat.       |
// |                                                                          |
// | This library is called by the 'chat/lib/index_libs/main_index.lib.php'  |
// | script.                                                                  |
// +--------------------------------------------------------------------------+
// | From the phpMyChat project:                                              |
// |    http://www.phpheaven.net/projects/phpMyChat/                          |
// |                                                                          |
// | Authors: the phpHeaven-team <team@phpheaven.net>                         |
// +--------------------------------------------------------------------------+
//
// $Id: start_page.lib.js,v 1.16 2001/12/08 17:48:19 loic1 Exp $
//
// Set of JavaScript functions used by the starting form of the chat.
//



/**
 * Get the with and the height of the phpMyChat window and the size in pixels
 * of a column of an input field, then update the starting form with these
 * values
 *
 * @access	public
 */
function pmcSetSizes()
{
	// Get the screen size
	if (typeof(document.body) != 'undefined'
		&& typeof(document.body.clientWidth) != 'undefined'
		&& document.body.clientWidth)
	{
		var theWinWidth		= document.body.clientWidth;
		var theWinHeight	= document.body.clientHeight;
	}
	else if (typeof(window.innerWidth) != 'undefined')
	{
		var theWinWidth		= window.innerWidth;
		var theWinHeight	= window.innerHeight;
	}

	// Get the size of a text input field column
	if (jsIsDOM)
	{
		var aCell		= document.getElementById('testSize');
		// IE emphasizes a bit the column with -> handled in a special way
		var aColWidth	= (typeof(aCell.clientWidth) != 'undefined' && aCell.clientWidth)
						? Math.floor(aCell.clientWidth / aCell.size)
						: (aCell.offsetWidth / aCell.size);
		var aRowHeight	= Math.floor(aCell.offsetHeight);
	}
	else if (jsIsIE4)
	{
		var aCell		= document.all('testSize');
		var aColWidth	= Math.floor(aCell.clientWidth / aCell.size);
		var aRowHeight	= Math.floor(aCell.offsetHeight);
	}
	else if (jsIsNS4)
	{
		// TODO (can't find a way to compute this value :()
		// var aCell		= document.layers['testSize'];
		// var aColWidth	= Math.floor(aCell.document.width / 11);
		// var aRowHeight	= Math.floor(aCell.document.height);
	}

	// Update the starting form
	if (typeof(aColWidth) != 'undefined' && aColWidth)
	{
		with (document.forms['startingForm'])
		{
			elements['colWidth'].value	= aColWidth;
			elements['rowHeight'].value	= aRowHeight;
		}
	}
} // end of the 'pmcSetSizes()' function


/**
 * Updates the "jsVersion" field in the starting form according to the
 * JavaScript abilities of the browser
 *
 * @access	public
 */
function pmcDefineVerField()
{
	// js1.1 but not 1.2 enabled browser
	if (typeof(document.images) != 'undefined' && jsWhichVersion == 'low')	
	{
		document.forms['startingForm'].elements['jsVersion'].value = 'medium';
	}
	// other cases
	else
	{
		document.forms['startingForm'].elements['jsVersion'].value = jsWhichVersion;
	}
} // end of the 'pmcDefineVerField()' function


/**
 * Launches the tutorial
 *
 * @access	public
 */
function pmcTutorialPopup()
{
	var tutorialUrl	= jsChatPath + 'tutorial_popup.' + jsPhpExt
					+ '?' + 'lang=' + jsLang
					+ jsUrlArgSeparator + 'jsVersion=' + jsWhichVersion;

	// Launches the tutorial window only if it doesn't already exists
	if (!jsTutorialWin)
	{
		if (jsIsJs11)
			window.focus();
		jsTutorialWin = window.open(tutorialUrl, 'tutorial_popup', 'resizable=yes,scrollbars=yes,toolbar=yes,menubar=yes,status=yes');
	}

	if (jsIsJs11)
		jsTutorialWin.focus();
} // end of the 'pmcTutorialPopup()' function


/**
 * Launches the users popup according to the DHTML capacities of the browser
 *
 * @param	dummy	used to ensure the popup name is unique 
 *
 * @access	public
 */
function pmcUsersPopup(dummy)
{
	var whichFile	= (jsWhichVersion == 'low') ? '_low' : '';
	var usersUrl	= jsChatPath + 'users_popup' + whichFile + '.' + jsPhpExt
					+ '?' + jsDbSessionSID;

	if (jsIsJs11)
		window.focus();
	var usersPopup	= window.open(usersUrl, 'users_popup_' + dummy, 'width=180,height=300,resizable=yes,scrollbars=yes');
	if (jsIsJs11)
		usersPopup.focus();
} // end of the 'pmcUsersPopup()' function


/**
 * Launches the popups for the registration stuffes
 *
 * @param	name	the name of the script to launch
 *
 * @access	public
 */
function pmcRegPopups(name)
{
	var regUrl		= jsChatPath + name + '.' + jsPhpExt
					+ '?' + jsDbSessionSID;
	var popWidth	= (name != 'admin') ? 350 : 510;
	var popHeight	= (name != 'profile_del' && name != 'profile_remind') ? 470 : 190;
	var popParams	= 'width=' + popWidth
					+ ',height=' + popHeight
					+ ',resizable=yes,scrollbars=yes';

	if (jsIsJs11)
		window.focus();
	jsRegPopupWin = window.open(regUrl, name + '_popup', popParams);
	if (jsIsJs11)
		jsRegPopupWin.focus();
} // end of the 'pmcRegPopups()' function


/**
 * Allows to ensure an unique choice among rooms
 *
 * @param	choiceType	type of the room that has been selected
 *
 * @access	public
 */
function pmcResetRoomBox(choiceType)
{
	var resetR0	= document.forms['startingForm'].elements['enterDefaultRoomName'].options[0];
	var resetR1	= document.forms['startingForm'].elements['enterOtherRoomName'].options[0];
	var resetT	= document.forms['startingForm'].elements['createRoomType'].options[0];
	var resetR2	= document.forms['startingForm'].elements['createRoomName'];

	// A default public chat room has been selected
	if (choiceType == 'default')
	{
		resetR1.selected	= true;
		resetT.selected		= true;
		resetR2.value		= '';
	}
	// An 'other' public chat room has been selected
	else if (choiceType == 'otherPub')
	{
		resetR0.selected	= true;
		resetT.selected		= true;
		resetR2.value		= '';
	}
	else
	{
		resetR0.selected	= true;
		resetR1.selected	= true;
	}
} // end of the 'pmcResetRoomBox()' function


/**
 * Validates briefly the starting form
 *
 * @param	doCheckPaswd	whether to ensure there is a password or not
 *
 * @access	public
 */
function pmcIndexValidate(doCheckPswd)
{
	// The replace function (js1.2) isn't supported -> no js tests are done
	if (typeof(document.forms['startingForm'].elements['pmcNick'].value.replace) == 'undefined')
		return true;

	var nickField		= document.forms['startingForm'].elements['pmcNick'];
	var pswdField		= document.forms['startingForm'].elements['pmcPassword'];
	var roomToCreate	= document.forms['startingForm'].elements['createRoomName'];
	var	reEmpty			= new RegExp(' ', 'g');
	var	reNick			= new RegExp('[\\\\,]');
	var	reRoom			= new RegExp('[\\\\,]');

	// Brief nick validation
	if (nickField.value.replace(reEmpty, '') == '')	// nick is empty
	{
		nickField.value = '';
		nickField.focus();
		alert(jsErrorNick1);
		return false;
	}
	else if (reNick.test(nickField.value))			// invalid characters in
	{												// the nick
		nickField.focus();
		alert(jsErrorNick2);
		return false;
	}

	// Brief password validation
	if (doCheckPswd 
		&& pswdField.value.replace(reEmpty, '') == '')	// registration is required
	{													// but password is empty
		pswdField.value = '';
		pswdField.focus();
		alert(jsErrorPswd1);
		return false;
	}

	// Ensure there is a password if the user wants to create room
	if (typeof(roomToCreate) != 'undefined' && roomToCreate.value != ''
		&& pswdField.value.replace(reEmpty, '') == '')
	{
		pswdField.focus();
		alert(jsErrorPswd2);
		return false;
	}

	// Brief room name validation
	if (typeof(roomToCreate) != 'undefined' && roomToCreate != '')
	{
		if (roomToCreate.value.replace(reEmpty, '') == '')	// only space chars
		{
			roomToCreate.value = '';
		}
		else if (reRoom.test(roomToCreate.value))	// invalid characters in
		{											// the name of the room to
			roomToCreate.focus();					// create
			alert(jsErrorRoom);
			return false;
		}
	}

	// All the tests have been succesfully passed -> submit the from
	document.forms['startingForm'].elements['isJsValidated'].value = 1;
	return true;
} // end of the function 'pmcIndexValidate()'
