/*****************************************************************************
 * LocalizeClient.js
 *
 * Functions to determine localization settings of client machine.
 *****************************************************************************/

/*****************************************************************************
 * PopulateForm
 *
 * Given a reference to a form, populates the following fields:
 *
 * DFS= date format string
 * NFD= numeric format decimal symbol
 * NFG= numeric format grouping symbol
 * TZA= time zone abbreviation
 * TZD= time zone difference in hours
 *****************************************************************************/
function PopulateForm(oForm)
{
	oForm.DFS.value = DateFormat();
	oForm.NFD.value = NumberFormatDecimal();
	oForm.NFG.value = NumberFormatGroup();
	oForm.TZA.value = TimeZoneAbbreviation();
	oForm.TZD.value = TimeZoneDifference();
}

/*****************************************************************************
 * DateFormat
 *
 * Returns three-letter string representing the presentation order, in which:
 * D = day
 * M = month
 * Y = year
 * An example would be: MDY (month-day-year).
 *
 * This function always returns a valid date format string, or an
 * empty string if the date format cannot be determined.
 *****************************************************************************/
function DateFormat()
{
	var d = new Date();
	var s = "";

	d.setFullYear(1999);
	d.setMonth(11);
	d.setDate(30);

	var s1 = d.toLocaleDateString();
	var t1 = s1.replace(/,/g,"");
	var a1 = t1.split(' ');

	d.setDate(31);

	var s2 = d.toLocaleDateString();
	var t2 = s2.replace(/,/g,"");
	var a2 = t2.split(' ');

	for (i=0; i < a1.length; i++)
	{
		var v1 = a1[i];
		var v2 = a2[i];

		if (v1 == 12)
		{
			// month as number
			s = s + "M";
			continue;			
		}
		
		if (v1 == 30)
		{
			// day
			s = s + "D";
			continue;			
		}
		
		if (v1 == 1999)
		{
			// year
			s = s + "Y";
			continue;			
		}

		if (v1 == v2)
		{
			// month as word
			s = s + "M";
			continue;
		}
	}

	// result must have all three parts, in any order
	switch (s)
	{
		case "DMY": break;
		case "DYM": break;
		case "MDY": break;
		case "MYD": break;
		case "YMD": break;
		case "YDM": break;
		default   : return "";
	}

	return s;
}

/*****************************************************************************
 * NumberFormatDecimal
 * 
 * Get local number format decimal point character.
 * Returns decimal character, or an empty string if not available.
 *****************************************************************************/
function NumberFormatDecimal()
{
	var n = new Number(1.23);
	var s = n.toLocaleString();
	var t = (s.length == 4) ? s.substring(1,2) : "";
	return t;
}

/*****************************************************************************
 * NumberFormatGroup
 * 
 * Get local number format grouping character..
 * Returns grouping character, or an empty string if not available.
 *****************************************************************************/
function NumberFormatGroup()
{
	var n = new Number(1234.56);
	var s = n.toLocaleString();
	var t = (s.length == 8) ? s.substring(1,2) : "";
	return t;
}

/*****************************************************************************
 * TimeZoneAbbreviation
 *
 * Returns time zone abbreviation, or an empty string if not available.
 *****************************************************************************/ 
function TimeZoneAbbreviation()
{
	var d = new Date();
	var t = d.toTimeString();
	var i = t.lastIndexOf(' ');
	var s = (i < 0) ? "" : t.substr(i+1);
	return s;
}

/*****************************************************************************
 * TimeZoneDifference
 * 
 * Get number of hours local time zone is different from GMT.  Returns 
 * difference value as a string.
 *****************************************************************************/
function TimeZoneDifference()
{
	var d = new Date();
	var h = d.getTimezoneOffset() / 60;
	return h.toString();
}


