﻿
/// <summary>
/// Length of string which is allowable.
/// </summary>
var MAX_STRING_LENGTH = 256;	//Strings aren't allowed to be longer than 256 characters in length.
var DISSALOWED_CHARS = "'<>";	//Strings aren't allowed to contain these characters. Input elements will not allow them to be entered.

/// <summary>
/// Converts a value from the QueryString to an integer. Throws an error if the value is not a string
/// representation of an integer. Note: length of the input string is checked as a side-effect.
/// </summary>
/// <param name="input">string -- The value you wish to convert to an integer</param>
/// <returns>The integer represented by the input string</returns>
function ConvertStringToInteger(input)
{
	var retVal = -1;

	if (input.Length > 11)
	{
		//"The string is too long to be a valid integer"
		retVal = 0;
	}
	else if ((input.Length > 10) && (!input.StartsWith("-")))
	{
		//"The string is too long to be a valid positive integer"
		retVal = 0;
	}
	else
	{
		//Can't determine validity by length alone, must try to convert the string to an integer.
		try
		{
			retVal = parseInt(input);
		}
		catch (e)
		{
			retVal = 0;
		}
	}

	return retVal;
}

/// <summary>
/// Ensures that the input string does not exceed the max string length (the length of a string representing the
/// max value would actually exceed this length, but numbers of that size are beyond the scope of this application), 
/// then converts to a double. If the conversion throws an exception, returns 0.
/// </summary>
/// <param name="input">string -- The value you wish to converto to a float</param>
/// <returns>The float representation of the input string</returns>
function ConvertStringToFloat(input)
{
	var retVal = 0.0;

	if (input.Length > 40)
	{
		//The string is too long to be a valid float
		retVal = 0.0;
	}
	else if ((input.Length > 39) && (!input.StartsWith("-")))
	{
		//"The string is too long to be a valid positive float"
		retVal = 0.0;
	}
	else
	{
		//can't determine validity by length. try to convert. if it fails, return 0.0
		try
		{
			retVal = parseFloat(input);
		}
		catch (e)
		{
			retVal = 0.0;
		}
	}

	return retVal;
}

/// <summary>
/// Operates on the string passed in, removing any dissalowed characters. If a dissalowed character is found
/// returns false, otherwise returns true;
/// </summary>
/// <param name="input">The string whose length we are interested in.</param>
/// <returns>
/// If the check passed, returns a copy of the input string, otherwise returns a string truncated to the
/// appropriate length. If the input is null, returns an empty string.
/// </returns>
function CheckString(input)
{
	var retVal = true;
	if (input != null)
	{
		var re = new RegExp("[" + DISSALOWED_CHARS + "]");
		retVal = input.match(re);
	}
	return retVal;
}

function CleanString(input)
{
	var retVal = input;
	if (retVal != null)
	{
		var re = new RegExp("[" + DISSALOWED_CHARS + "]");
		retVal = retVal.replace(re, "");
		if (retVal.length > MAX_STRING_LENGTH)
		{
			retVal = retVal.substr(0, MAX_STRING_LENGTH);
		}
	}
	return retVal;
}