/**
 * This file includes global javascript functions.  Keep it clean!
 */

/**
 * Fires a native javascript event (like 'change', etc.)
 * @see http://jehiah.cz/archive/firing-javascript-events-properly
 */
function fireNativeEvent(element, event)
{
	if(document.createEventObject)
	{
		// dispatch for IE
		var evt = document.createEventObject();
		return element.fireEvent('on' + event, evt);
	}
	else
	{
		// dispatch for firefox + others
		var evt = document.createEvent("HTMLEvents");
		evt.initEvent(event, true, true); // event type, bubbling, cancelable
		return !element.dispatchEvent(evt);
	}
}

/**
 * Takes a float and returns it with two decimals, like you'd expect round(num, 2) to do.
 * 
 * @param aFloat float
 * @return float rounded to two decimals
 */
function twoDecimals(aFloat)
{
	return Math.round(aFloat * 100) / 100;
}

/**
 * Returns a random number between lower and upper inclusive.
 * 
 * @param lower int the lower bound on the random number
 * @param upper int the upper bound on the random number
 * @return int a random number in the range [lower, upper]
 */
function getRandom(lower, upper)
{
	if(lower > upper)
	{ // if the parameters came in the wrong order, swap them
		var temp = lower;
		lower = upper;
		upper = temp;
	}
	
	return Math.floor((upper - (lower - 1)) * Math.random()) + lower;
}

/**
 * Direct javascript interpretation of PHP's own urlencode() function.
 * 
 * @param clearString string
 * @return string (urlencoded, ie '/' becomes '%2F', etc.)
 * @see http://cass-hacks.com/articles/code/js_url_encode_decode/
 */
function urlencode(clearString)
{
	var output = '';
	var x = 0;
	clearString = clearString.toString();
	var regex = /(^[a-zA-Z0-9_.]*)/;
	while(x < clearString.length)
	{
		var match = regex.exec(clearString.substr(x));
		if (match != null && match.length > 1 && match[1] != '')
		{
			output += match[1];
			x += match[1].length;
		}
		else
		{
			if (clearString[x] == ' ')
			{
				output += '+';
			}
			else
			{
				var charCode = clearString.charCodeAt(x);
				var hexVal = charCode.toString(16);
				output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
			}
			x++;
		}
	}
	return output;
}

/**
 * Opens the popup window for card activation.
 */
function activateCard()
{
	var url = '/activate';
	var wind = window.open(url, 'activation', 'height=305, width=510, toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, directories=no, status=no');
	wind.focus();
}

/**
 * Opens the popup window for password recovery.
 * 
 * @param origin string 'merchant', 'cardholder', etc. specifying the in-site location of the function call (trying to guess which type of individual clicked on the link).
 */
function forgotPassword(origin)
{
	var url = '/user/forgot-password/origin/' + urlencode(origin);
	// note that this window is 300 tall (which seems like overkill at first glance) because the display-confirmation page IS taller
	var wind = window.open(url, 'forgotPassword', 'height=300, width=500, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no');
	wind.focus();
}

/**
 * Opens a guidetext popup window for guideName, with optionally specified width and height.
 * 
 * @param guideName string
 * @param width int optional, defaults to 600
 * @param height int optional, defaults to 300
 */
function guideText(guideName, width, height)
{
	if(typeof width == 'undefined')
	{
		width = 600;
	}
	if(typeof height == 'undefined')
	{
		height = 300;
	}
	
	var url = '/guidetext/' + urlencode(guideName);
	var wind = window.open(url, 'moreInfo', 'height=' + height + ', width=' + width + ', toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, directories=no, status=no');
	wind.focus();
}
