// This file contains any Javascript functions that are used by the entire site and do not fall into any discernable category
var string_ContentPlaceHolderMain_Prefix = 'ctl00_contentPlaceHolderMain_';

function form_GeneralUtilities_GetForm()
{
    var form_currentForm = document.getElementById('aspnetForm');
    
    if (form_currentForm == null)
        form_currentForm = document.getElementById('form1');
        
    return form_currentForm;
}

function object_GeneralUtilities_GetObject(string_ID)
{
    var object_currentObject;
    
    if (string_ID != "")
        object_currentObject = document.getElementById(string_ID);
    
    if (object_currentObject == null)
        object_currentObject = document.getElementById(string_ContentPlaceHolderMain_Prefix + string_ID);

    return object_currentObject;
}

function object_GeneralUtilities_GetObjectByIDMatch(string_ID)
{
    var form_currentForm = form_GeneralUtilities_GetForm();

    for (var i = 0; i< form_currentForm.elements.length; i++)
    {
        if (form_currentForm.elements[i].Id.IndexOf(string_ID) == (form_currentForm.elements[i].Id.length - string_ID.length))
        {
            return form_currentForm.elements[i];   
        }
    }
    
    return null;
}

// Returns the current site prefix; necessary for all popups
function string_GeneralUtilities_SitePrefix()
{
    var string_fullURL = window.location.href;
    var string_prefix;
    
    string_prefix = location.protocol + '//' + location.host + "/";
    
    if (string_fullURL.indexOf("cswina") > -1 || string_fullURL.indexOf("localhost") > -1)
    {
        string_prefix += string_fullURL.substring(string_prefix.length, string_fullURL.indexOf("/", string_prefix.length) + 1);
    }
    
    return string_prefix;
}

function void_GeneralUtilities_GoToAnchor(string_anchorName)
{
    var string_currentURL = (document.URL.substring(0,document.URL.indexOf('#')));
    document.location = string_currentURL + '#' + string_anchorName;
}

// Clean bad HTML text from textboxes
function void_GeneralUtilities_RemoveHTMLFromTextAreas()
{
    var textBoxes = new Array();
    textBoxes = document.getElementsByTagName('textarea') // store collection of all <input/> elements
    
    for ( i = 0; i < textBoxes.length; i++ )
        textBoxes[i].value = string_GeneralUtilities_RemoveHTMLFromString(textBoxes[i].value);
        
    var textBoxes = new Array();
    textBoxes = document.getElementsByTagName('input') // store collection of all <input/> elements
    
    for ( i = 0; i < textBoxes.length; i++ )
        if (textBoxes[i].type == "text" || textBoxes[i].type == "hidden")
            textBoxes[i].value = string_GeneralUtilities_RemoveHTMLFromString(textBoxes[i].value);
}

// Replace any '<' symbols with '&lt;' to prevent HTML injection
function string_GeneralUtilities_RemoveHTMLFromString(string_input)
{
    return string_input.replace(/</g, "&lt;");
}

// Sets the contents of the specified text box to be empty
function void_GeneralUtilities_clearTextFromTextBox(textBox_clickedTextBox)
{
    textBox_clickedTextBox.value = "";
}

// Sets the contents of a 'date' drop down menu based on a selected 'month' drop down menu (so that only legal dates are able to be chosen, i.e. NOT February 31)
function void_GeneralUtilities_setDateDropDownOptionsByMonthDropDownSelection(string_dateDropDownName, string_monthDropDownName)
{
    var selectone_dateDropDown = document.getElementById(string_dateDropDownName);
    var selectone_monthDropDown = document.getElementById(string_monthDropDownName);
    var int_numberOfDays = 31;
    var int_currentDayIndex = selectone_dateDropDown.selectedIndex;
    
    if (selectone_monthDropDown.options[2].selected)
    {    
        int_numberOfDays = 29;
    }
    else if (selectone_monthDropDown.options[4].selected || selectone_monthDropDown.options[6].selected || selectone_monthDropDown.options[9].selected || selectone_monthDropDown.options[11].selected)
    {
        int_numberOfDays = 30;
    }
    
    for(i = 0; i < 31; i++)
    {
        selectone_dateDropDown.options[i]=null;
    }
    
    selectone_dateDropDown.options[0] = new Option("Day", "-1");
    
    for(var i = 0; i < int_numberOfDays ; i++)
	{
	    var x = String(i+1);
	    selectone_dateDropDown.options[i+1] = new Option(x,x);		
	}
	
	selectone_dateDropDown.selectedIndex = int_currentDayIndex;
}

function void_GeneralUtilities_PutDropDownValueInTextBox(string_dropDownName, string_textBoxName)
{
    var dropDown = document.getElementById(string_dropDownName);
    var textBox = document.getElementById(string_textBoxName);
    
    textBox.value = dropDown.options[dropDown.selectedIndex].value;
}

function string_GeneralUtilities_AttemptToGetArticleForWord(string_input)
{
    if (string_input != null && string_input.length > 0)
    {
        if (string_input.charAt(0).toLowerCase() == 'a' || string_input.charAt(0).toLowerCase() == 'e' || string_input.charAt(0).toLowerCase() == 'i' || string_input.charAt(0).toLowerCase() == 'o' || string_input.charAt(0).toLowerCase() == 'u')
            return "an";
        else
            return "a";        
    }
    else
        return "";
}

function bool_GeneralUtilities_ArrayContains(a, obj)
{
    for(var i = 0; i < a.length; i++)
    {
        if(a[i] == obj)
        {
          return true;
        }
    }
    return false;
}

// Returns true if string is made up only of digits
// Passes: '432', '5431', '2'
// Fails: '-2432', '5423k', 'hello'
function bool_GeneralUtilities_StringIsValidPositiveInteger(string_input)
{
    var filter=/^[\d]+$/;
    
    return filter.test(string_input);
}

// Returns true if string is made up only of digits and possible negative sign
// Passes: '432', '-2432', '0'
// Fails: 'x', '54"32', '32.31'
function bool_GeneralUtilities_StringIsValidInteger(string_input)
{
    var filter=/^-?[\d]+$/;
    
    return filter.test(string_input);
}

// Returns true if string is made up only of digits, possible negative sign, and possible decimal extension
// Passes: '353', '354.433', '-2432.994', '0.24'
// Fails: '.345', '2343.', '2432.k'
function bool_GeneralUtilities_StringIsValidDecimal(string_input)
{
    var filter=/^-?[\d]+(?:\.[\d]+)?$/;
    
    return filter.test(string_input);
}

// Fucntions for trimming whitespace from ends of strings
String.prototype.trim = function()
{
	return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.ltrim = function()
{
	return this.replace(/^\s+/,"");
}

String.prototype.rtrim = function()
{
	return this.replace(/\s+$/,"");
}

function string_GeneralUtilities_AddLeadingZerosToInteger(int_number, int_digits)
{   
    var str = '' + int_number;
    while (str.length < int_digits)
        str = '0' + str;
   
    return str;
}

//// COMODO
//var cot_loc0=(window.location.protocol == "https:")? (string_GeneralUtilities_SitePrefix() + "JavaScripts/cot.js") :
//(string_GeneralUtilities_SitePrefix() + "JavaScripts/cot.js");
//document.writeln('<scr' + 'ipt language="JavaScript" src="'+cot_loc0+'" type="text\/javascript">' + '<\/scr' + 'ipt>');
