﻿// JScript File

function ValidatorEnabledStateChange(whichElement, enableIt) 
{
	if (whichElement)
	{
		whichElement.enabled = enableIt; // changed from ValidatorEnable since it calls ValidatorValidation as a side-effect
	}
}

// formats a numerical value as a currency in USD: $dddd.ff
function formatAsCurrency( value )
{
	if( typeof(value) !== 'number' ) value = Number(0.0);
	value = value.toFixed(2);
	parts = value.split('.');
	whole = parts[0];
	frac  = parts.length > 1 ? '.' + parts[1] : '';
	var regex = /(\d+)(\d{3})/;
	while (regex.test(whole)) {
		whole = whole.replace(regex, '$1' + ',' + '$2');
	}
	return '$' + whole + frac;
}

// Gets everything between the scheme:// and the end of the domain name from a FTP/HTTP[S] URI
function hostNameFromUri(uri) 
{
	var regex = new RegExp('^(?:f|ht)tp(?:s)?\://([^/:]+)', 'im');
	matches = uri.match(regex);
	if( matches && matches.length > 1 )
		return matches[1].toString();
	else
		return '';
}

// encode each character of a string into its hex equivalent
function hexEncode( str )
{
	var ret = '';
	for( var i = 0; i < str.length; ++i )
	{
		ret += toBase( str.charCodeAt(i), '0123456789ABCDEF' );
	}
	return ret;
}

// convert a string of hex codes (2 bytes each) to the equivalent ASCII characters
function hexDecode( str )
{
	var ret = '';
	str = str.toUpperCase();
	for( var i = 0; i < str.length; i+=2 )
	{
		var hex = str.substr( i, 2 );
		ret += String.fromCharCode( fromBase( hex, '0123456789ABCDEF' ));
	}
	return ret;
}

// convert from a decimal number into any other base
// e.g., for hex, base='0123456789ABCDEF'
function toBase( dec, base )
{
	var len = base.length;
	var ret = '';
	while( dec > 0 ) 
	{
		ret = base.charAt( dec%len ) + ret;
		dec = Math.floor( dec/len );
	}
	
	return ret;
}

// convert from any base to decimal
// e.g., for hex, base='0123456789ABCDEF'
function fromBase( num, base )
{
	var len = base.length;
	var ret = 0;
	for(var x = 1; num.length > 0; x *= len)
	{
		ret += base.indexOf( num.charAt( num.length - 1 ) ) * x;
		num = num.substr( 0, num.length - 1 );
	}
	return ret;
}