/***********************
*                      *
*    AJAX Functions    *
*                      *
***********************/

var RequestObject = null;

function GetXMLHttpRequestObject()
{
    var requestObject = null;
    try
    {
        requestObject = new XMLHttpRequest();
    }
    catch (TryMicrosoft)
    {
        try
        {
            requestObject = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (TryMicrosoftOther)
        {
            try
            {
                requestObject = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (Failure)
            {
                requestObject = null;
            }
        }
    }
    if (requestObject == null)
    {
        alert("Your browser does not support XMLHTTP.");
    }
    return requestObject;
}

/**************************
*                         *
*    Utility Functions    *
*                         *
**************************/

var RegularExpressionEMailAddress = new RegExp(/^[a-zA-Z0-9._-]+[@][a-zA-Z0-9._-]+[.][a-zA-Z0-9_-]+$/);
var RegularExpressionInjectionAttack = new RegExp(/([>])|([<])|([(])|([)])|([;])/);

var InputBackgroundColorInvalid = "#FA8072";
var InputBackgroundColorValid = "#FFFFFF";

function IsDateValue(value)
{
	return IsDate(value, 1753, 9999, "/");
}

function IsDate(text, yearMin, yearMax, delim)
{
    var isDigits = new RegExp(/\d{1,}/);
    //Parse and store the month, day, and year values as strings.
    var sMonth = new String(text.split(delim)[0]);
    var sYear = new String(text.split(delim)[2]);
    var sDay = new String(text.split(delim)[1]);
    //Confirm that these parsed values consist of digits only.
    if (!sMonth.match(isDigits)) {return false;}
    if (!sYear.match(isDigits)) {return false;}
    if (!sDay.match(isDigits)) {return false;}
    //Confirm that none of the parsed values contain more than 2 or 4 digits.
    if (sMonth.length > 2) {return false;}
    if (sYear.length > 4) {return false;}
    if (sDay.length > 2) {return false;}
    //Convert the parsed string values to numbers.
    var iMonth = new Number(parseFloat(sMonth));
    var iYear = new Number(parseFloat(sYear));
    var iDay = new Number(parseFloat(sDay));
    //Perform an initial check on the numerical range of the parsed values.
    if ((iYear < yearMin) || (iYear > yearMax)) {return false;}
    if ((iMonth < 1) || (iMonth > 12)) {return false;}
    if ((iDay < 1) || (iDay > 31)) {return false;}
    //Determine and store the number of days in February for the given year.
    var x = new Number(((iYear % 4 == 0) && ((!(iYear % 100 == 0)) || (iYear % 400 == 0))) ? 29 : 28);
    //Perform the final check on the numerical range of the day value.
    if (((iMonth ==  4) && (iDay > 30)) ||
        ((iMonth ==  6) && (iDay > 30)) ||
        ((iMonth ==  9) && (iDay > 30)) ||
        ((iMonth == 11) && (iDay > 30)) ||
        ((iMonth ==  2) && (iDay >  x))) {return false;}
    //At this point, the text argument contains a valid date value.
    return true;
}

function UrlEncode(value)
{
    return escape(value).replace(/\+/g, "%2B");
}

function TrimWhitespaceCharacters(value)
{
    var b = false;
    var n = "";
    var s = "";
    value = value.toString();
    for (var i = 0; i < value.length; i++)
    {
        if ((value.charCodeAt(i) != 32) && (value.charCodeAt(i) != 9))
        {
            if (s != "")
            {
                n += s;
                s = "";
            }
            n += value.charAt(i);
            if (!b) b = true;
        }
        else if (b)
        {
            s += value.charAt(i);
        }
    }
    return n;
}

function TrimTextBoxLength(textBox, length)
{
	if (textBox.value.length > length)
	{
		textBox.value = textBox.value.substr(0, length);
	}
}