/** 
 * functions.js (c) 2007 spikeframework
 * Author: Chris Kasprowicz 
 **/

/*
 * Generic Cancel function. Sends browser back 1 page.
 */
function cancel()
{
	history.back(1);
}

// GRAPHICAL

/**
 * Fades out div with id using opacity.
 *
 * @param id - id of the element to fade.
 * @param t  - duration of the fade (in seconds).
 * @param o  - initial opacity of the element (0 to 1).
 */
function fadeIn(id, t, o)
{
 	var speed = (t) ? t * 100 : 100;
	var opacity = (o) ? o * 100 : 100;
	setOpacity(id, 0);
	for (var i = 0; i <= speed; i++)
    {
    	setTimeout("setOpacity('" + id + "'," + (i * (opacity / speed)) + ");", i * 10);
    }
}

/*
 * Fades in div with id using opacity.
 * 
 * @param cntlId - id of control to fade in.
 * @param t      - duration of fade (in s).
 * @param o      - initial opacity (0-1).
 */
function fadeOut(id, t, o)
{
 	var speed = (t) ? t * 100 : 100;
	var opacity = (o) ? o * 100 : 100;
 	setOpacity(id, opacity);
 	for (var i = speed; i >= 0; i--)
 	{
    	setTimeout("setOpacity('" + id + "'," + (i * (opacity / speed)) + ");", (speed - i) * 10);
    }
}

/*
 * Returns the full height (including scroll) of the document.
 */
function getDocumentHeight()
{
	if (window.innerHeight && window.scrollMaxY)
	{
		return window.innerHeight + window.scrollMaxY;
	}
	else if (document.body.scrollHeight > document.body.offsetHeight)
	{
		return document.body.scrollHeight;
	}
	else
	{
		return document.body.offsetHeight;
	}
}

/*
 * Returns the full width (including scroll) of the document.
 */
function getDocumentWidth()
{
	if (window.innerWidth && window.scrollMaxX)
	{
		return window.innerWidth + window.scrollMaxX;
	}
	else if (document.body.scrollWidth > document.body.offsetWidth)
	{
		return document.body.scrollWidth;
	}
	else
	{
		return document.body.offsetWidth;
	}
}

/**
 * Move element to specified position.
 *
 * @param id - id of the element to move.
 * @param x  - new x-axis value to move to.
 * @param y  - new y-axis value to move to.
 * @param t  - duration of the move (in seconds).
 **/
function moveTo(id, x, y, t)
{
	var speed = (t) ? t * 100 : 100;
	var control = document.getElementById(id);
	var _x = (control.style.left) ? (control.style.left).replace("px", "") : 0;
	var _y = (control.style.top) ? (control.style.top).replace("px", "") : 0;
	var dx = x - _x;
	var dy = y - _y;
	
	//alert("(" + _x + "," + _y + "), (" + dx + ", " + dy + ")");
	for (var i = 0; i <= speed; i++)
	{
		if (dx != 0)
		{
			setTimeout("document.getElementById('" + id + "').style.left = '" + 
					(parseInt(_x) + parseInt(i * (dx / speed))) + "px';", i * 10);
		}
		if (dy != 0)
		{
			setTimeout("document.getElementById('" + id + "').style.top = '" + 
					(parseInt(_y) + parseInt(i * (dy / speed))) + "px';", i * 10);
		}
  }
}

/**
 * Set the opacity/transparency of the specified element.
 * 
 * @param id   - the id of the element to set the opacity for.
 * @param opac - new opacity of the element.
 **/
function setOpacity(id, opac)
{
	var ff = opac * (1 / 100);
	document.getElementById(id).style.opacity = ff;
	document.getElementById(id).style.filter = 
			"progid:DXImageTransform.Microsoft.Alpha(opacity=" + opac + ")";
}

/**
 * Resize element to specified dimensions.
 *
 * @param id - id of the element to move.
 * @param w  - new width value for the element.
 * @param h  - new height value for the element.
 * @param t  - duration of the move (in seconds).
 **/
function sizeTo(id, w, h, t)
{
	// TODO Take into account padding and specified width/height;
	var speed = (t) ? t * 100 : 100;
	var control = document.getElementById(id);
	
	if (navigator.userAgent.indexOf("MSIE") != -1 ||
			navigator.userAgent.indexOf("Safari") != -1)
	{
		var _w = control.offsetWidth;
		var _h = control.offsetHeight;
	}
	else
	{
		var aPad = ((control.style.padding).replace(/px/g, "")).split(" ");
		var _w = control.offsetWidth - (parseInt(aPad[1]) + parseInt(aPad[3]));
		var _h = control.offsetHeight - (parseInt(aPad[0]) + parseInt(aPad[2]));
	}
	var dx = w - _w;
	var dy = h - _h;
	
	for (var i = 0; i <= speed; i++)
	{
		setTimeout("document.getElementById('" + id + "').style.width = '" + 
				(_w + (i * (dx / speed))) + "px';", i * 10);
		setTimeout("document.getElementById('" + id + "').style.height = '" + 
				(_h + (i * (dy / speed))) + "px';", i * 10);
	}
}

/** REAL TIME CHECKING **/

/**
 *
 **/
function checkNumeric(c, e, min, max)
{
	var key, keyChar;
	
	if (window.event)
		key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;
	
	keyChar = String.fromCharCode(key);
	
	if ((key == null) || (key == 0) || (key == 8) || 
			(key == 9) || (key == 13) || (key == 27) )
		return true;
		
	var result = true;
	
	if (("0123456789").indexOf(keyChar) == -1)
	{		
		showError(c, "this field only accepts numbers");
		result = false;
	}
	
	if (parseInt(c.value + keyChar) < min || parseInt(c.value + keyChar) > max)
	{
		showError(c, "the value of this field must be between " + min + " and " + max);
		result = false;
	}
	
	return result;
}

var isErr = false;

function showError(c, s)
{
	if (!isErr)
	{
		var msg = document.getElementById(c.id + "_msg");
		if (msg != null)
		{
			msg.innerHTML = s;
			msg.style.opacity = 1;
			isErr = true;
			setTimeout("fadeOut('" + msg.id + "');", 2000);
			setTimeout("isErr=false;", 3000);
		}
	}
}

function showFlash(src, w, h, colour, play, loop)
{
	document.write("<object type='application/x-shockwave-flash'");
	document.write("	codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\"");
	document.write("	  data='" + src + "'");
	document.write("	  width='" + w + "' height='" + h + "'>");
	document.write("  <param name='allowScriptAccess' value='sameDomain'\/>");
	document.write("  <param name='movie' value='" + src + "'\/>");
	document.write("  <param name='loop' value='" + loop + "'\/>");
	document.write("  <param name='menu' value='false'\/>");
	document.write("  <param name='quality' value='high'\/>");
	document.write("  <param name='play' value='" + play + "'\/>");
	document.write("  <param name='bgcolor' value='" + colour + "'\/>");
	document.write("<\/object>");
}


/** FORM VALIDATION **/

function validateField(field)
{
	var input = document.getElementById(field);
	if (input.value == "")
	{
		alert("Please enter a value for '" + field.replace("_", " ") + "'");
		input.focus();
		return false;
	}
	return true;
}

function validateSameFields(field1, field2)
{
	var input1 = document.getElementById(field1);
	var input2 = document.getElementById(field2);
	if (input1.value != input2.value)
	{
		alert("Please enter the same values for '" + field1.replace("_", " ") + 
				"' and '" + field2.replace("_", " ") + "'");
		input1.focus();
		return false;
	}
	return true;
}

/**
 * JavaScript interpretation of a StringTokenizer.
 *
 * @param str - The String to be tokenized.
 * @param delim - The delimiter to be used when tokenizing the String.
 * @param returnDelims - Determines whether delimiters should be returned when returning tokens.
 */
function StringTokenizer(str, delim, returnDelims)
{
	this.index = 0;
	this.str = str;
	this.delim = (delim != null) ? delim : " ";
	this.returnDelims = (returnDelims != null) ? returnDelims : false;
}

StringTokenizer.prototype.hasMoreTokens = function()
{
	return this.str.length > this.index;	
};

StringTokenizer.prototype.nextToken = function()
{
	var result = "";
	if (this.hasMoreTokens())
	{
		if (this.str.indexOf(this.delim, this.index) != -1)
		{
			var newIndex = this.str.indexOf(this.delim, this.index);
			result = this.str.substring(this.index, ((this.returnDelims) ? newIndex + this.delim.length : newIndex));
			this.index = newIndex + this.delim.length ; 
		}
		else
		{
			result = this.str.substring(this.index);
			this.index = this.str.length;
		}
		
	}
	return result;
};
