// This fades an element on the page before hiding it
var hideElementTimeout = null; // houses the id of the last hideElement timeout from the fadeElement function
var valueElementTimeout = null; // houses the id of the last setValue timeout from the fadeElement function
var idElementTimeout = null; // houses the id of the last elementID timeout from the fadeElement function
function fadeElement(_elementID, _fadeLevel, _resetText, _hideAfterCompletion)
{
    // Step 1.  Get the object
    if(!_elementID) { return; }
    var objElement = getElement(_elementID);
    if(!objElement || !objElement.style) { return; }
    if(!_fadeLevel) { _fadeLevel = 0; } // reset fade level to instant fade
	if(_hideAfterCompletion == null || typeof(_hideAfterCompletion) == "undefined") { _hideAfterCompletion = 1; } // reset the hide after completion to true
    else { _fadeLevel = parseInt(_fadeLevel); } // get the fade level as an integer
    // If we have timeouts going from a previous operation
    if(idElementTimeout && idElementTimeout == _elementID)
    {
        if(hideElementTimeout)
        {
            clearTimeout(hideElementTimeout);
        }
        if(valueElementTimeout)
        {
            clearTimeout(valueElementTimeout);
        }
    }
    
    // Step 2.  Determine the background color
    var szBackgroundColor = "#fffbcf";
    switch(_fadeLevel)
    {
        case 3:
            szBackgroundColor = "#fffbcf";
            break;
        case 2:
            szBackgroundColor = "#fffbe5";
            break;
        case 1:
            szBackgroundColor = "#fffbf3";
            break;
        case 0:
            // We're done fading, show a standard white background and set the element to hide
            szBackgroundColor = "#fff";
            idElementTimeout = _elementID;
			if(_hideAfterCompletion == 1)
			{
            	hideElementTimeout = setTimeout("hideElement('"+_elementID+"')", 3000);
			}
            if(_resetText)
            {
               valueElementTimeout = setTimeout("setValue('"+_elementID+"', '"+_resetText+"')", 3050);
            }
            break;
        default:
            // We don't understand this value, so we'll reset it and start at 3
            szBackgroundColor = "#fffbcf";
            _fadeLevel = 3;
            break;
    }
    // Update the _fadeLevel for the next pass
    _fadeLevel = _fadeLevel - 1;
    
    // Step 3.  Change the background color of the element
    objElement.style.backgroundColor = szBackgroundColor;
    
    // Step 4.  Call this same method again with the new fade level
    if(_fadeLevel >= 0)
    {
        if(_resetText)
        {
            setTimeout("fadeElement('"+_elementID+"','"+_fadeLevel+"','"+_resetText+"', '"+_hideAfterCompletion+"')", 200);
        }
        else
        {
            setTimeout("fadeElement('"+_elementID+"','"+_fadeLevel+"',null,'"+_hideAfterCompletion+"')", 200);
        }
    }
}

// Checks if a user submits a form by pressing enter
function checkSubmitForm(e)
{    
	var keyCode = 0;
	if(window.event) keyCode = e.keyCode;
	else if(e.which) keyCode = e.which;
	if(keyCode == 13) return true;
	return false;
}

// Clears a dropdown of all its entries
function clearDropDown(objDropDown)
{
    if(!objDropDown || !objDropDown.length || !objDropDown.options)
    {
        return;
    }
    
    for(var j = objDropDown.length; j >= 0; j--)
    {
        objDropDown.options[j] = null;
    }
}

// Populates a dropdown using a string with format Value~Name|Value2~Name2|Value3~Name3
function populateDropDown(objDropDown, _delimitedString)
{
    if(!_delimitedString || !objDropDown)
    {
        return;
    }
    // Split the response (Value~Name|Value~Name|Value~Name) up into the individual brands
    var a_AllItems = _delimitedString.split('|');
    if(!a_AllItems || !a_AllItems.length)
    {
        // Unable to parse the delimited string
        return;
    }
    
    for(var i = 0; i < a_AllItems.length; i++)
    {
        // Make sure this row has information
        if(!a_AllItems[i])
        {
            continue;
        }
        
        // Split up the Value~Name into individual elements
        var a_ItemInfo = a_AllItems[i].split('~');
        if(a_ItemInfo.length < 2)
        {
            continue;
        }
                
        // Create a new option element for the dropdown
        var objDropDownOption = document.createElement("option");
        
        // Add the value to the drop down option
        if(a_ItemInfo[0]) { objDropDownOption.setAttribute('value', a_ItemInfo[0]); }
        else { objDropDownOption.setAttribute('value', ''); }
        
        // Add the text to the drop down option
        objDropDownOption.appendChild(document.createTextNode(a_ItemInfo[1]));
        
        // Append the new option to the dropdown
        objDropDown.appendChild(objDropDownOption);
    }
}

// Removes leading whitespaces
function LTrim( value ) {
	if(!value || !value.replace) { return value; }
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	if(!value || !value.replace) { return value; }
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	if(!value || !value.replace) { return value; }
	return LTrim(RTrim(value));
	
}

// Gets an element by it's id on the page
function getElement(id)
{
    if (document.getElementById)
        var returnVar = document.getElementById(id);
    else if (document.all)
        var returnVar = document.all[id];
    else if (document.layers)
        var returnVar = document.layers[id];
    return returnVar;
}

// This function hides an element on the page using its ID
function hideElement(_elementID)
{
	// Parameters:
	//		_elementID = the ID of the element to hide
	
	// Make sure we have valid parameters
	if(!_elementID) { return; }
	
	// Get the object
	var objElement = getElement(_elementID);
	
    // Make sure we have valid objects
    if(!objElement || !objElement.style) { return; }
    
    // Hide the object
    objElement.style.display = 'none';
}

// This function shows an element on the page
function showElement(_elementID)
{
   	// Parameters:
	//		_elementID = the ID of the element to show
	
	// Make sure we have valid parameters
	if(!_elementID) { return; }
	
	// Get the object
	var objElement = getElement(_elementID);
	
    // Make sure we have valid objects
    if(!objElement || !objElement.style) { return; }
    
    // Show the object
    objElement.style.display = 'block';
}

// Opens a URL in a popup window
function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width=640,height=480');");
}
