/// <reference path="dpr.global.js"/>

/// <summary>commonly used (static) methods.</summary>
dpr.util = {};


dpr.util.formatCurrency = function (inMoney)
{
	/// <summary>
	/// returns number formatted to 2 decimal places (exluding currency symbol)
	/// </summary>
	
	var output = String(Math.round(inMoney * 100)/100);
	if (output.indexOf(".") == -1) output += '.';
	while (output.indexOf(".") > output.length - 3) output += '0';
	return output; 
}


dpr.util.getCookie = function (name) 
{
	/// <summary>
	/// returns the value of the cookie with the specified name, null if it does not exist.
	/// </summary>
	var cookies = document.cookie.split(';');
	for(var i=0;i < cookies.length;i++) 
	{
		var cookie = cookies[i];
		while (cookie.charAt(0)==' ') cookie = cookie.substring(1,cookie.length); // trim leading space(s)
		if (cookie.indexOf(name + "=") == 0) return cookie.substring(name.length+1,cookie.length); // return value
	}
	return null;
}


dpr.util.hideAllChildrenExcept = function (parentElementId, newVisibleChildElementId)
{
	var parentElement = document.getElementById(parentElementId);
	// hide all children of parentElement except for the chosen one
	for (i = 0; i < parentElement.childNodes.length; i++)
	{
		parentElement.childNodes[i].style.display = (parentElement.childNodes[i].id == newVisibleChildElementId) ? 'block' : 'none';
	}
}


dpr.util.getRecentMilestoneFormatted = function (intervalHours, offsetMinutes)
{
	var d = new Date();
	var lastUtcMilestone = Math.round(intervalHours * Math.floor(d.getUTCHours() / intervalHours));
	d.setUTCHours(lastUtcMilestone, 0, 0, 0);
	d.setMinutes(d.getMinutes() + offsetMinutes, 0, 0);
	return d.getUTCFullYear() + dpr.util.padZeroesLeft(d.getUTCMonth() + 1, 2) + dpr.util.padZeroesLeft(d.getUTCDate(), 2) + '_' + dpr.util.padZeroesLeft(d.getUTCHours(), 2) + dpr.util.padZeroesLeft(d.getMinutes(), 2);
}


dpr.util.padZeroesLeft = function(theInt, desiredLength)
{
	var output = String(theInt);
	while (output.length < desiredLength) output = '0' + output;
	return output; 
}


dpr.util.readQueryString = function (parameterKey)
{
	/// <summary>
	/// returns the value for the specified querystring parameter key (case insensitive)
	/// </summary>
	
	var output = null;
	var entireQueryString = window.location.search.substring(1);
	if (entireQueryString != null && entireQueryString != '')
	{
		var nameValuePairs = entireQueryString.split("&");
		for (var i=0; i<nameValuePairs.length; i++)
		{
			var tokens = nameValuePairs[i].split("=")
			if (tokens.length > 1 && tokens[0].toLowerCase() == parameterKey.toLowerCase() && tokens[1] != '')
			{
				output = tokens[1];
				break;
			}
		}
	}
	return output;
}


dpr.util.displayErrorInPage  = function (theError, elementToShowErrorWithin, title)
{
	if (elementToShowErrorWithin) elementToShowErrorWithin.innerHTML = '<span style="color: #f00;">[' + title + ': ' + theError.message + ']</span>'
}


dpr.util.logError = function (title, theError)
{
	/// <summary>
	/// attempts to inform dpreview of client-side javascript error
	/// </summary>
	/// <param name="title">the message to send</param>
	/// <param name="theError">Optional. The error to log</param>
	
	/*
	var errorLogger = document.createElement('img');
	errorLogger.src = '/shop/nojs.asp?' + message;
	errorLogger.style.display = 'none';
	document.getElementsByTagName('body')[0].appendChild(errorLogger);
	*/
	
	// inform the user (confirm box)
	/*
	if (confirm('An error occured loading the page. Would you like to send a report to dpreview.com?'))
	{
		var message = '';
		message = 'page: ' + window.location + ',  ';
		message += 'title: ' + title + ',  ';
		if (theError && theError.description) message += 'detail: ' + theError.description;
		window.location = '/misc/feedback.asp?comments=' + message;
	}
	*/
}


dpr.util.GetXmlHttpRequest = function ()
{
	/// <summary>
	/// Creates an XmlHttpRequest in the appropriate manner for the current browser. Returns 
	/// null if no XmlHttpRequest object could be created.
	/// </summary>
	
	var request = null;
	try 
	{
		request = new XMLHttpRequest();
	} 
	catch (trymicrosoft) 
	{
		try 
		{
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (othermicrosoft) 
		{
			try 
			{
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (failed) 
			{
				request = null;
			}
		}
	}
	return request;
}