// StripChars(theField, theChars)
//		Removes the characters contained in 'theChars' from the string 'theField'.

// NumbersOnly(number, addChars [optional])
//		Removes everything except numeric digits from 'number' with the exception of characters contained in 'addChars'.

// AlphaOnly(string, addChars [optional])
//		Removes all non-alphabetic characters from 'string' with the exception of characters contained in 'addChars'.

// IsInRange(number, min_value, max_value)
//		Returns a true if 'number' is between 'min_value' and 'max_value' and a false otherwise.

// IsDate(theDate)
//		Returns a true if the 'theDate' is a valid date in mm/dd/yyyy form and a false otherwise.

// compareDates(d1, d2)
//		Compares d1 against d2 and returns a 1 if d1 is greater than d2, a -1 if d1 is less than d2, and a 0
//        if d1 and d2 are equal. 

// IsInteger(number)
//		Returns a true if 'number' is an integer and a false otherwise.

// IsNumber(number)
//		Returns a true of 'number' is a number defined as having an optional leading + or -., having at most 1 decimal 
// 		point, and otherwise containing only the characters 0-9.  Returns a false otherwise.

// IsNull(theField)
//		Returns a true if 'theField' has a zero length or is equal to "" and a false otherwise.

// IsEmail(theField)
//		Returns a true if 'theField' matches the pattern x@x.xx representing the minimum for a valid email address.

// IsPhoneNumber(theField)
//		Returns a true if 'theField' matches the pattern nnn-nnn-nnnn representing a valid phone number.

// IsIPAddress(theField)

// trackKeys(theText, theField, limit)
//		Checks to see if the length of the text passed in 'theText' is equal to or greater than 'limit'.  If so it changes the focus
// 		to the field 'theField'.  If 'theField' already has a value, the value is selected.

// normalizeIPAddress(ipaddress)
//		Returns the ip address padded with 0s.  For example, 1.1.1.1 is returned as 001.001.001.001.

// compareIPAddresses(ip1, ip2)
//		Compares ip1 against ip2 and returns a 1 if ip1 is greater than ip2, a -1 if ip1 is less than ip2, and a 0
//        if ip1 and ip2 are equal.

// compareDates(d1, d2)

// IsCurrency(number)
//		Returns a true of 'number' is a number defined as having an optional leading + or -., having at most 1 decimal 
// 		point, and otherwise containing only the characters 0-9.  Returns a false otherwise.

// ----------------------------------------------------------------------------------------------------------------------------------

function StripChars(theField, theChars, replace)
{

	replaceableChars = new Array ("<", ">", "\"", "'");
	replacementChars = new Array ("&lt;", "&gt;", "&quot;", "&rsquo;");

	var newStr   = "";
	var charOkay = null;
	var ch       = null;

	for (var i = 0; i < theField.length; i++)
	{
		charOkay = false;
		ch = theField.charAt(i);
		
		if (theChars.indexOf(ch) == -1)
			charOkay = true;				
		else
		{
			charOkay = false;
			
			if (replace)
			{
				for (var j = 0; j < replaceableChars.length; j++)
				{
					if (ch == replaceableChars[j])
					{
						ch = replacementChars[j];
						charOkay = true;
						break;
					}
				}
			}

		}

		if (charOkay) newStr = newStr + ch;
	}
	
	return newStr;
	
}

function NumbersOnly()
{
		
	switch (arguments.length)
	{
		case 0  : checkOk = "1234567890"; theField = ""; break;
		case 1  : checkOk = "1234567890"; theField = arguments[0]; break;
		default : checkOk = "1234567890" + arguments[1]; theField = arguments[0]; break;
	}
	
    var newStr = "";
	var ch     = null;

    for (i = 0;  i < theField.length; i++)
    {
    	ch = theField.charAt(i);
		if (checkOk.indexOf(ch) != -1)
        	newStr = newStr + ch;
    }
  
    return newStr;
}

function AlphaOnly()
{
		
	switch (arguments.length)
	{
		case 0  : checkOk = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; theField = ""; break;
		case 1  : checkOk = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; theField = arguments[0]; break;
		default : checkOk = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" + arguments[1]; theField = arguments[0]; break;
	}
	
    var newStr = "";
	var ch     = null;

    for (i = 0;  i < theField.length; i++)
    {
    	ch = theField.charAt(i);
		if (checkOk.indexOf(ch) != -1)
        	newStr = newStr + ch;
    }
  
    return newStr;
}

function IsInRange(number, min_value, max_value)
{

    if (number.length == 0)
        return true;

    if (min_value != null)
	{
        if (number < min_value)
			return false;
	}

    if (max_value != null)
	{
		if (number > max_value)
			return false;
	}

	return true;

}

function DayInMonth(checkYear, checkMonth, checkDay)
{

	var maxDay = 31;
	
	if (checkMonth == 4 || checkMonth == 6 || checkMonth == 9 || checkMonth == 11)
		maxDay = 30;
	else if (checkMonth == 2)
	{
		if (checkYear % 4 > 0)
			maxDay = 28;
		else if (checkYear % 100 == 0 && checkYear % 400 > 0)
			maxDay = 28;
		else
			maxDay = 29;
	}

	if (checkDay > 0 && checkDay <= maxDay)
		return true;

	return false;
	
}

function IsDate( theDate )
{

    if (theDate.length == 0)
        return true;

	isplit = theDate.indexOf('/');

	if (isplit == -1 || isplit == theDate.length)
		return false;

    theMonth = theDate.substring(0, isplit);

	if (theMonth.length == 0)
        return false;

	isplit = theDate.indexOf('/', isplit + 1);

	if (isplit == -1 || (isplit + 1) == theDate.length)
		return false;

    theDay = theDate.substring((theMonth.length + 1), isplit);

	if (theDay.length == 0)
        return false;

	theYear = theDate.substring(isplit + 1);

	if (!IsInteger(theMonth))                   return false;
	if (!IsInRange(theMonth, 1, 12))            return false;
	if (!IsInteger(theYear))                    return false;
	if (!IsInRange(theYear, 1500, 3000 ))       return false;
	if (theYear.length != 4)				    return false;
	if (!IsInteger(theDay))	                   return false;
	if (!DayInMonth(theYear, theMonth, theDay)) return false;

	return true;
	
}

function IsInteger(number)
{
	
	if (number.length == 0)
		return true;
		
    if (number.indexOf(".") < 1)
		return IsNumber(number);

	return false;	

}

function IsNumber(number)
{
    //Returns true if value is a number or is NULL
    //otherwise returns false	

    if (number.length == 0)
        return true;

    //Returns true if value is a number defined as
    //   having an optional leading + or -.
    //   having at most 1 decimal point.
    //   otherwise containing only the characters 0-9.
	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;

    //The first character can be + - .  blank or a digit.
	check_char = start_format.indexOf(number.charAt(0))
    //Was it a decimal?
	if (check_char == 1)
	    decimal = true;
	else if (check_char < 1)
		return false;
        
	//Remaining characters can be only . or a digit, but only one decimal.
	for (var i = 1; i < number.length; i++)
	{
		check_char = number_format.indexOf(number.charAt(i))
		if (check_char < 0)
			return false;
		else if (check_char == 1)
		{
			if (decimal)		// Second decimal.
				return false;
			else
				decimal = true;
		}
		else if (check_char == 0)
		{
			if (decimal || digits)	
				trailing_blank = true;
        // ignore leading blanks

		}
	        else if (trailing_blank)
			return false;
		else
			digits = true;
	}	
    //All tests passed, so...
    return true;
}

function IsNull( value )
{
	return ( value == "" || value.length == 0 )
}

function IsEmail( value )
{
	return ( /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test( value ) );
}

function IsPhoneNumber ( value )
{
	return ( /^(\d{3}\-\d{3}\-\d{4}){1}/.test( value ) ); 
}

function IsTime ( value )
{
	var parts = value.split( ":" );
     return ( parts.length == 2 ) ? ( ( parts[ 0 ] >= 0 && parts[ 0 ] < 13 ) && ( parts[ 1 ] >= 0 && parts[ 1 ] < 60 ) ) : false;
}

function IsIPAddress(theField)
{
	if (/^(\d{1,3}\.){3}\d{1,3}$/.test(theField))
	{
		var mask = theField.split('.');
		
		if (parseInt(parseFloat(mask[0])) == 0)
			return false;
		
		for (var i = 0; i < mask.length; i++)
			if (parseInt(parseFloat(mask[i])) > 255)
				return false;
			
		return true;
	}
	else
		return false;
}


function trackKeys(theText, nextField, limit)
{
	if (theText.length >= limit)
	{
		if (nextField.value.length > 0)
			nextField.select();
		else
			nextField.focus();
	}
}

function normalizeIPAddress(ipaddress)
{	
	var mask  = ipaddress.split('.');
	var ipstr = '';
	
	for (var i = 0; i < mask.length; i++)
	{
		for (var x = 3; x > mask[i].length; x--)
			ipstr += '0';
		
		ipstr += mask[i] + ((i < 3) ? '.' : '');
	}
	
	return ipstr;
}

function compareIPAddresses(ip1, ip2)
{
	var x = ip1.split('.');
	var y = ip2.split('.');
	
	for (var i = 0; i < x.length; i++)
		if (x[i] > y[i])
			return 1;
		else if (x[i] < y[i])
			return -1;
	
	return 0;			
}

function compareDates(d1, d2)
{
	// First, check to make sure the two values passed are valid dates in mm/dd/yyyy form.
	if (IsDate(d1) && IsDate(d2))
	{
		// Create an array holding the components of eahc date for comparison.  Since the dates have already been 
		// checked for validity, it can be safely assumed that each array will have exactly 3 elements.
		var d1 = d1.split('/');
		var d2 = d2.split('/');
		
		// Create a temporary variable for array element swapping purposes
		var t  = null;
		
		// Swap the month and day in the first date
		t = d1[0];
		d1[0] = d1[1];
		d1[1] = t;
		
		// Swap the month and day in the second date
		t = d2[0];
		d2[0] = d2[1];
		d2[1] = t;

		// Traverse the the date arrays backwards and compare the year, month, and day in that order.  If the values
		// being compared are not equal, return a 1 or -1 to indicate the the first date is greater or less than the 
		// second respectively.
		for (var i = d1.length; i > 0; --i)
			if (d1[i] > d2[i])
				return 1;
			else if (d1[i] < d2[i])
				return -1;
		
		// The two dates are exactly the same, so pass a 0
		return 0;
	}
	else return;  // One or more of the dates passed is invalied, so just return
}

function IsCurrency( number )
{
    //Returns true if value is a number or is NULL
    //otherwise returns false	

    if (number.length == 0)
        return true;

    //Returns true if value is a number defined as
    //   having an optional leading + or -.
    //   having at most 1 decimal point.
    //   otherwise containing only the characters 0-9.
	var start_format = " .0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;

	//Remaining characters can be only . or a digit, but only one decimal.
	for (var i = 1; i < number.length; i++)
	{
		check_char = number_format.indexOf(number.charAt(i))
		if (check_char < 0)
			return false;
		else if (check_char == 1)
		{
			if (decimal)		// Second decimal.
				return false;
			else
				decimal = true;
		}
		else if (check_char == 0)
		{
			if (decimal || digits)	
				trailing_blank = true;
        // ignore leading blanks

		}
	        else if (trailing_blank)
			return false;
		else
			digits = true;
	}	
    //All tests passed, so...
    return true;
}

function PadSingleDigits ( value )
{
	return ( ( ( value - 0 ) < 10 ) ? '0' : '' ) + ( value - 0 );
} 

function FormatCurrency ( value )
{
	value = value.toString().replace( /\$|\,/g, '' );

	if( isNaN( value ) )
		value = "0";
		
	var sign = ( value == ( value = Math.abs( value ) ) );
	
	value = Math.floor( value * 100 + 0.50000000001 );
	
	var cents = value % 100;

	value = Math.floor( value / 100 ).toString();
	
	for ( var i = 0; i < Math.floor( ( value.length - ( 1 + i ) ) / 3 ); ++i )
		value = value.substring( 0, value.length - ( 4 * i + 3 ) ) + ',' + value.substring( value.length - ( 4 * i + 3 ) );

	return ( ( ( sign ) ? '' : '-' ) + '$' + value + '.' + PadSingleDigits( cents ) );
}