// Author: Nicholas Livaditis
// Purpose: A set of useful functions for common situations with
// web page development that come up
// - If you use these functions for your own purpose please give
// credit where credit it due and give reference to my name.

// Date functions - GMT format: Sat, 12 Aug 2000 00:25:03 GMT
var now = (new Date()).toGMTString()

// Purpose: Gets the FULL 4 digit year while using only JS 1.0 functions
// Input: none
// Returns: The current 4 digit year
// JS level: 1.0
function getYear() {
	for ( var i = 0, counter = 0, Char = ""; i < now.length; i++ ) {
		Char = now.charAt(i)
		if ( Char >= '0' && Char <= '9' ) {
			counter++
			if ( counter == 4 )
				return parseInt(now.substring(i-4, i+1))
		}
		else
			counter = 0
	}
	return ""
}
// Purpose: Gets today's day in the 3 letter format
// Input: none
// Returns: string of today's day
// JS level: 1.0
function getDay() {
	return now.substring(0,3)
}
// Purpose: Gets today's date
// Input: none
// Returns: string of today's date
// JS level: 1.0
function getDate() {
	for (var i = 0, Char; i < now.length; ++i) {
		Char = now.charAt(i)
		if ( Char >= '0' && Char <= '9' )
			return parseInt(now.substring(i,i+2))
	}
	return ""
}
// Purpose: Gets current month in 3 letter format
// Input: none
// Returns: string of current month
// JS level: 1.0
function getMonth() {
	for (var i = 0, Char; i < now.length; ++i) {
		Char = now.charAt(i)
		if ( !(Char < '0' || Char > '9') ) {
			do Char = now.charAt(++i)
			while ( Char < 'A' || Char > 'Z' )
			return now.substring(i,i+3)
		}
	}
	return ""
}
// Purpose: Trims out space characters to the left and right of a string
// Input: Any user-victimized string with extraneous blanks thrown in!
// Returns: A string trimmed of trailing spaces both on the left and right sides
// JS level: 1.0
function Trim( strVictim ) {
	var LeftIdx = -1, RightIdx = strVictim.length
	if ( RightIdx == 0 ) 
		return ""
	while ( LeftIdx < RightIdx && strVictim[++LeftIdx] == " " )
		;// Move past blanks on the left side
	while ( RightIdx >= LeftIdx && strVictim[--RightIdx] == " " )
		;// Move past blanks on the right side
	return strVictim.substring(LeftIdx, ++RightIdx)
}
// Purpose: Format a string so that all character after a space and the
// first character, are uppercased while all other characters are lowercased
// Input: Any unformatted "user victimized" string
// Returns: The formatted string
// JS level: 1.0
function Initcap(strVictim) {
	var i = 0, j = 0, Max = strVictim.length, Char = ''
	if ( Max == 1 )
		return strVictim.toUpperCase()
	else if ( Max > 1 ) {
		strVictim = strVictim.toLowerCase()
		strVictim = strVictim.charAt(0).toUpperCase() + strVictim.substring(1)
		while ( true )  {
			i = strVictim.indexOf(" ", j)
			if ( i >= 0 && j < Max ) {
				j = i + 1
				Char = strVictim.charAt(j)
				if ( Char >= 'a' && Char <= 'z' )
					strVictim = strVictim.substring(0, i+1) + Char.toUpperCase() + strVictim.substring(j+1)
			}
			else
				break
		}
	}
	return strVictim
}
function toLower(strVictim){
return strVictim.toLowerCase();
}
// Purpose:  formats a number into the currency format(i.e. $1,234,567.00)
function formatCurrency(strVictim) {
	strVictim += ""
	var Pos = strVictim.lastIndexOf(".")
	var Max = strVictim.length
	var fracpart = ""
	if ( Pos > 0 ) {
		fracpart = strVictim.substring(Pos,Pos+3)
		strVictim = strVictim.substring(0,Pos)
		Max = strVictim.length
	}
	if ( Max > 3 )
		for ( var i=parseInt(Max/3); i > 0; i-- ) {
			j = i * 3 - 2
			strVictim = strVictim.substring(0,j) + "," + strVictim.substring(j)
		}
	if ( fracpart.length > 0 )
		strVictim += fracpart
	return "$" + strVictim
}

// JScript 1.0 string replace function
// Performs a basic Find and Replace operation on a string
function Replace(Expression, Find, Replace)
{
	var pos = 0
	while ( (pos = Expression.indexOf(Find,pos)) >= 0 )
		Expression = Expression.substring(0, pos) + Replace + Expression.substring(pos+Find.length)
	return Expression
}

// To Handle the event of a the Enter key being pressed in a document
function SubmitOnEnterKey(e) {
	var keyPressed = 0
	if (window.Event) // For Netscape 4 up
		keyPressed = e.which
	else if (document.all) // For IE 4 up
		keyPressed = window.event.keyCode
	if (keyPressed == 10 || keyPressed == 13)
		document.forms[0].submit()
}

// Submits the first form on Enter, for both IE/Netscape 4 and up
function ActivateSubmitOnEnter() {
	if (window.Event)
		document.captureEvents(Event.KEYPRESS)
	document.onkeypress = SubmitOnEnterKey;
}

function net4reload() {
	if ( document.layers ) document.location.reload()
}
/*
Purpose: Creates and returns an request object, that is, an object with name+value
         pairs of based off the query string that arrived with the page request
Inputs:  The Location object's search property
Returns: An object, with each property being a name and equal to it's value(i.e.)
         if the query string is ?uri=main.html&code=u the object if effect
		 looks like this:
		 this.uri == 'main.html'
		 this.code == 'u'
Comment: Just beautiful */
function Request() {
	var Char, Name, Value, i = 0
	this.query = self.location.href // Get the query string
	this.query = this.query.substring(this.query.indexOf("?")+1)
	var max = this.query.length
	while ( i < max ) {
	// Extract name from name=value pair
		for (Name = ""; i < max; ++i) {
			Char = this.query.charAt(i)
			if ( Char != "=" && Char != "&" )
				Name += Char
			else {
				++i
				break
			}
		}
	// Extract value from name=value pair
		for (Value = ""; i < max; ++i) {
			Char = this.query.charAt(i)
			if ( Char != "&" )
				Value += Char
			else {
				++i
				break
			}
		}
		if ( Name.length > 0 )
			eval("this." + unescape(Name) + "=unescape('" + Value + "')")
	}
}
