function ClientScripting_SetFieldValue(element, value)
{		
	if (element != null)
	{			
		if (!element.type && element.length)
		{
			//we have an array of radio or checkbox values
			for(var i=0; i<element.length; i++)
			{
				if (element[i].value == value)
				{
					element[i].checked = true;
					break;
				}
			}
		}
		else
		{
			switch(element.type)
			{
				case "text":
				case "textarea":
				case "hidden":
					element.value = value;
					break;	
				case "select-one":
					for (var i=0; i<element.length; i++)
					{	
						if (element.options[i].value == value)
						{
							element.selectedIndex = i;
							break;
						}
					}
					break;
			}
		}
	}
}
function ClientScripting_GetFieldValue(element)
{
	if (element == null)
		return "";
							
	if (!element.type && element.length)
	{
		//we have an array of radio or checkbox values
		for(var i=0; i<element.length; i++)
		{
			if (element[i].checked)
			{
				return element[i].value;
				break;
			}
		}
	}	
	else
	{
		switch(element.type)
		{
			case "text":
			case "textarea":
			case "hidden":
				return element.value;
				break;
			case "checkbox":
			case "radio":
				if (element.checked)
					return element.value;
				break;	
			case "select-one":
				if (element.selectedIndex >= 0)
					return element.options[element.selectedIndex].value;
				break;
		}
	}
				
	return "";
}
function ClientScripting_FieldIsEmpty(element)
{
	return ClientScripting_StripWhitespace(ClientScripting_GetFieldValue(element)) == "";
}
function ClientScripting_StripNonNumeric(val)
{	
	return val.replace(/\D/g, "");
}
function ClientScripting_StripWhitespace(val)
{	
	return val.replace(/\s/g, "");
}
function ClientScripting_Trim(val)
{	
    var m = val.match(/^\s*(\S+(\s+\S+)*)\s*$/);
    return (m == null) ? "" : m[1];
}
function ClientScripting_FormatCurrency(value)
{
	return ClientScripting_FormatNumber(value, 2);
}
function ClientScripting_FormatNumber(value, decimals)
{
	var suffix = "";
	for ( var i=0; i<decimals; i++ )
		suffix += "0";

	var num = parseFloat(value);
	if (isNaN(num))
		num = 0;
	else
		num = Math.round(num*Math.pow(10, decimals))/Math.pow(10, decimals);
	
	num = String(num);
	var decimalIndex = num.indexOf(".");
	if (decimalIndex < 0)
		return num += (decimals>0) ? "."+suffix : "";
	else if (num.length - (decimalIndex+1) >= decimals)
		return num.substring(0, decimalIndex+decimals+1);
	else
		return num + suffix.substring(0, decimals - (num.length - (decimalIndex+1)));
}
function ClientScripting_IsValidPostalCode(countryCode, value)
{
	var strippedValue;
	switch (countryCode)
	{
		case "":
		case "000":
			//validate usa zip
			strippedValue = ClientScripting_StripNonNumeric(value);
			return (strippedValue.length == 5 || strippedValue.length == 9)
		case "100":
			//validate canadian zip
			//The canadian postal code is a six-character alpha-numeric code in the format "ANA NAN", 
			//where "A" represents an alphabetic character and "N" represents a numeric 
			//character.
			strippedValue = ClientScripting_StripWhitespace(value);
			return (strippedValue.length == 6)
		default:
			//some countries do not have postal codes
			return true;
	}
	
}
function ClientScripting_IsValidPhoneNumber(value)
{
	var strippedValue = ClientScripting_StripNonNumeric(value);
	return (strippedValue.length == 10);
}
function ClientScripting_CountryRequiresPostalCode(countryCode)
{
	//some countries don't have postal codes
	switch (countryCode)
	{
		case "":
		case "000": /*USA*/
		case "100": /*CANADA*/
			return true;
		default:
			return false;
	}
}
function ClientScripting_CountryRequiresCity(countryCode)
{
	//some countries don't have cities
	switch (countryCode)
	{
		case "250": /*HONG KONG*/
		case "345": /*LUXEMBURG*/
		case "347": /*MACAO*/
		case "505": /*SINGAPORE*/
			return false;
		default:
			return true;
	}	
}