function urlEncode( _url )
{
	SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";			// RFC2396 Mark characters

	HEX = "0123456789ABCDEF";

	plaintext = _url;
	encoded = "";
	for ( a = 0; a < plaintext.length; a++ )
	{
		ch = plaintext.charAt( a );
		if ( ch == " " ) {
			encoded += "+";				// x-www-urlencoded, rather than %20
		}
		else if ( SAFECHARS.indexOf( ch ) != -1 )
		{
			encoded += ch;
		}
		else
		{
	   		charCode = ch.charCodeAt( 0 );
			if ( charCode > 255 )
			{
				alert( "Unicode Character '" 
                			       	+ ch 
		   			+ "' cannot be encoded using standard URL encoding.\n"
					+ "(URL encoding only supports 8-bit characters.)\n"
					+ "A space (+) will be substituted." );
				encoded += "+";
			}
			else
			{
				encoded += "%";
				encoded += HEX.charAt( ( charCode >> 4 ) & 0xF );
				encoded += HEX.charAt( charCode & 0xF );
			}
		}
	}
	return encoded;
}

function trim( myWord )
{
	var beginning =- 1;
	var end =- 1;
	var returnVal = "";

	for( i = 0; i <= myWord.length; i++ )
		if( myWord.charAt( i ) != ' ' && beginning == -1 ) beginning = i;

	if( beginning == -1 ) return returnVal;

	for( i = ( myWord.length - 1 ); i >= 0; i-- )
		if( myWord.charAt( i ) != ' ' && end == -1 ) end = i;

	for( i = beginning; i <= end; i++ )
		returnVal += myWord.charAt( i );

	return returnVal;
}

function checkForm( myForm )
{
	errs = 0;
	eMsg = "";
	radioCount = 0;

	for( j = ( myForm.length - 1 ); j >= 0; j-- )
	{
		field = myForm.elements[ j ];
		
		//if( field.name.length > 0 ) alert( field.name );
		
		if( field.name.length > 0 && document.getElementById( field.name ).getAttribute( 'checkName' ) != null )
		{
			fieldAlert = document.getElementById( field.name ).getAttribute( 'checkName' );

			if( field.type == 'text' || field.type == 'password' || field.type == 'textarea' || field.type == 'file' )
			{
				field.value = trim( field.value );
				if( field.value.length == 0 )
				{
					errs++;
					eMsg = "\n  - " + fieldAlert + eMsg;
					if( field.type != 'file' ) field.focus();
				}
			}
			else if( field.type == 'select-one' )
			{
				if( field.options[ field.selectedIndex ].value == '' )
				{
					errs++;
					eMsg = "\n  - " + fieldAlert + eMsg;
					field.focus();
				}
			}
			else if( field.type == 'select-multiple' )
			{
				if( field.selectedIndex == -1 )
				{
					errs++;
					eMsg = "\n  - " + fieldAlert + eMsg;
					field.focus();
				}
			}
			else if( field.type == 'radio' || field.type == 'checkbox' )
			{
				radioCount++;
				if( ( j - 1 ) < 0 || myForm.elements[ ( j - 1 ) ].name != field.name )
				{
					found = -1;
					for( x = 0; x < radioCount; x++ )
						if( myForm.elements[ ( j + x ) ].checked )
							found = x;

					radioCount = 0;

					if( found == -1 )
					{
						errs++;
						eMsg = "\n  - " + fieldAlert + eMsg;
						field.focus();
					}
				}
			}
		}
	}
	if( errs > 0 )
	{
		eMsg = "Errors occured in the following fields:" + eMsg;
		alert( eMsg );
		return false;
	}
	return true;
}

function updateCart( num )
{
	empty=0;
	at=0;
	do
	{
		at++;
		document.getElementById( 'quantity' + at ).value = trim( document.getElementById( 'quantity' + at ).value );
		if( document.getElementById( 'quantity' + at ).value.length == 0 )
			empty++;
	} while( at < num );
	if( empty > 0 )
		alert( 'Errors occurred in the following fields:\n  - One or more items is missing a quantity' );
	else
	{
		document.cartForm.action='index.cfm?fuseaction=updateCart';
		document.cartForm.submit();
	}
}
