//---------------------------------------------------------------------------------------------------------------------------------
// standard.js global variables.
//---------------------------------------------------------------------------------------------------------------------------------
// The document area/element where the "processing..." message appears. See function ShowExecWait().
var standard_js_waitArea;
  
//---------------------------------------------------------------------------------------------------------------------------------
// Checks if a value is numeric.
// <param="num">The value to evaluate as numeric or not.</param>
// <returns>True if the value is numeric, false otherwise.</returns>
//---------------------------------------------------------------------------------------------------------------------------------
function IsNumeric(num)
{
    if ( isNaN(num) )
    {
        return false;
    }
    return true;
}

//---------------------------------------------------------------------------------------------------------------------------------
// Deletes all leading spaces from the beginning of a string value.
// <param="inString">The string to trim.</param>
// <returns>The input string with leading spaces removed.</returns>
//---------------------------------------------------------------------------------------------------------------------------------
function TrimStart(inString)
{

    var outString = "";
    var firstPos = 0;
    var charArr = inString.split("");
    
    if ( inString == "" )
    {
        return "";
    }
    
    for(i = 0; i < charArr.length; i++)
    {
        if ( charArr[i] != " " )
        {
            firstPos = i;
            break;
        }
    }
    
    for(i = firstPos; i < charArr.length; i++)
    {
        outString += charArr[i];
    }
    
    return outString;
    
}

//---------------------------------------------------------------------------------------------------------------------------------
// Deletes all occurences of the specified character from the beginning of a string value.
// <param="inString">The string to trim.</param>
// <param="character">The character to remove from the front of the string.</param>
// <returns>The input string with the specified characters removed from the beginning of the string.</returns>
//---------------------------------------------------------------------------------------------------------------------------------
function TrimCharStart(inString, character)
{

    var outString = "";
    var firstPos = 0;
    var charArr = inString.split("");
    
    if ( inString == "" )
    {
        return "";
    }
    
    for(i = 0; i < charArr.length; i++)
    {
        if ( charArr[i] != character )
        {
            firstPos = i;
            break;
        }
    }
    
    for(i = firstPos; i < charArr.length; i++)
    {
        outString += charArr[i];
    }
    
    return outString;
    
}

//---------------------------------------------------------------------------------------------------------------------------------
// Deletes all trailing spaces from the end of a string value.
// <param="inString">The string to trim.</param>
// <returns>The input string with trailing spaces removed.</returns>
//---------------------------------------------------------------------------------------------------------------------------------
function TrimEnd(inString)
{

    var outString = "";
    var lastPos = 0;
    var charArr = inString.split("");
    
    if ( inString == "" )
    {
        return "";
    }
    
    for(i = charArr.length-1; i > 0; i--)
    {
        lastPos = i;
        if ( charArr[i] != " ")
        {
            break;
        }
    }
    
    for(i = 0; i <= lastPos; i++)
    {
        outString += charArr[i];
    }
    
    return outString;
    
}

//---------------------------------------------------------------------------------------------------------------------------------
// Deletes all instances of the specified character from the end of a string value.
// <param="inString">The string to trim.</param>
// <param="character">The character to trim from the string.</param>
// <returns>The input string with the characters removed from the end of the string.</returns>
//---------------------------------------------------------------------------------------------------------------------------------
function TrimCharEnd(inString, character)
{

    var outString = "";
    var lastPos = 0;
    var charArr = inString.split("");
    
    if ( inString == "" )
    {
        return "";
    }
    
    for(i = charArr.length-1; i > 0; i--)
    {
        lastPos = i;
        if ( charArr[i] != character)
        {
            break;
        }
    }
    
    for(i = 0; i <= lastPos; i++)
    {
        outString += charArr[i];
    }
    
    return outString;
    
}

//---------------------------------------------------------------------------------------------------------------------------------
// Deletes all leading and trailing spaces from a string value.
// <param="inString">The string to trim.</param>
// <returns>The input string with leading and trailing spaces removed.</returns>
//---------------------------------------------------------------------------------------------------------------------------------
function Trim(inString)
{
    return TrimStart(TrimEnd(inString));
}


//---------------------------------------------------------------------------------------------------------------------------------
// Partially encode (just "<"'s and "&"'s) the content to prevent transmitting harmful content.
//---------------------------------------------------------------------------------------------------------------------------------
function PartiallyEncode(inString)
{

    var outString = inString;
    var splitString = "";
    
    if ( inString == "" )
    {
        return "";
    }
    
    // Change all "&" to "&amp;".
    splitString = outString.split("");
    outString = "";
    for(i = 0; i < splitString.length; i++)
    {
        if ( splitString[i] == "&" )
        {
	        outString = outString + "&amp;";
	    }
	    else
	    {
	        outString = outString + splitString[i];
	    }
    }
    
    // Change all "<" to "&lt;".
    splitString = outString.split("");
    outString = "";
    for(i = 0; i < splitString.length; i++)
    {
        if ( splitString[i] == "<" )
        {
	        outString = outString + "&lt;";
	    }
	    else
	    {
	        outString = outString + splitString[i];
	    }
    }
    
    return outString;
    
}

//---------------------------------------------------------------------------------------------------------------------------------
// Encodes the input string to use in the URL as parameters (text after the "?").
//---------------------------------------------------------------------------------------------------------------------------------
function URLEncode(inString)
{

    var outString = inString;
    var splitString = "";
    var currChar = "";
    
    if ( inString == "" )
    {
        return "";
    }
    
    splitString = outString.split("");
    outString = "";
    
    for(i = 0; i < splitString.length; i++)
    {
        currChar = splitString[i]
        if ( currChar == "&" )
        {
	        currChar = "%26";
	    }
	    else if (currChar == "$")
	    {
	       currChar = "%24";
	    }
	    else if (currChar == "+")
	    {
	       currChar = "%2B";
	    }
	    else if (currChar == ",")
	    {
	       currChar = "%2C";
	    }
	    else if (currChar == "/")
	    {
	       currChar = "%2F";
	    }
	    else if (currChar == ":")
	    {
	       currChar = "%3A";
	    }
	    else if (currChar == ";")
	    {
	       currChar = "%3B";
	    }
	    else if (currChar == "=")
	    {
	       currChar = "%3D";
	    }
	    else if (currChar == "?")
	    {
	       currChar = "%3F";
	    }
	    else if (currChar == "@")
	    {
	       currChar = "%40";
	    }
	    else if (currChar == " ")
	    {
	       currChar = "%20";
	    }
	    else if (currChar == "\"")
	    {
	       currChar = "%22";
	    }
	    else if (currChar == "<")
	    {
	       currChar = "%3C";
	    }
	    else if (currChar == ">")
	    {
	       currChar = "%3E";
	    }
	    else if (currChar == "#")
	    {
	       currChar = "%23";
	    }
	    else if (currChar == "%")
	    {
	       currChar = "%25";
	    }
	    else if (currChar == "{")
	    {
	       currChar = "%7B";
	    }
	    else if (currChar == "}")
	    {
	       currChar = "%7D";
	    }
	    else if (currChar == "|")
	    {
	       currChar = "%7C";
	    }
	    else if (currChar == "\\")
	    {
	       currChar = "%5C";
	    }
	    else if (currChar == "^")
	    {
	       currChar = "%5E";
	    }
	    else if (currChar == "~")
	    {
	       currChar = "%7E";
	    }
	    else if (currChar == "[")
	    {
	       currChar = "%5B";
	    }
	    else if (currChar == "]")
	    {
	       currChar = "%5D";
	    }
	    else if (currChar == "`")
	    {
	       currChar = "%60";
	    }
	    outString = outString + currChar;
    }
    
    return outString;
    
}

//---------------------------------------------------------------------------------------------------------------------------------
// Checks for a duplicate content name.
//---------------------------------------------------------------------------------------------------------------------------------
function IsDupeName(contentId)
{
   if ( document.getElementById(contentId) == null )
   {
        return false;
   }
   return true;
}

//---------------------------------------------------------------------------------------------------------------------------------
// Clears a dropdown list of all its list items.
//---------------------------------------------------------------------------------------------------------------------------------
function ClearDropDown(dropDownId) 
{	
		
    var dropDown = document.getElementById(dropDownId);			
    var listCount;
    
    if ( dropDown == null )
    {
        alert("Error in function ClearDropDown(): Invalid dropDownId.");
        return;
    }
    
    listCount = dropDown.options.length;
    for (i=0; i<listCount; i++) 
    {				 
        dropDown.remove(0);	// Always remove(0) and NOT remove(i)		
    }
    
    return;
    
}

//---------------------------------------------------------------------------------------------------------------------------------
// Adds a list item to a dropdown list.
//---------------------------------------------------------------------------------------------------------------------------------
function AddDropDownItem(dropDownId,text,value)
{
    var dropDown = document.getElementById(dropDownId);
    if ( dropDown == null )
    {
        alert("Error in function AddDropDownItem(): Invalid dropDownId.");    
        return;
    }			
    dropDown.options.add(new Option(text, value));
    return;
}

//---------------------------------------------------------------------------------------------------------------------------------
// Sets the seleted item in a dropdown by the option's value.
//---------------------------------------------------------------------------------------------------------------------------------
function SetDropDownSelectedByValue(dropDownId,targetValue)
{
    var index = 0;
    var returnVal = false;
    var dropDown = document.getElementById(dropDownId);
    if ( dropDown == null )
    {
        alert("Error in function SetDropDownSelectedByValue(): Invalid dropDownId.");    
        return;
    }
    for(iii=0;iii<dropDown.length;iii++) // use var iii which is less likely used by client code
    {
        if ( dropDown.options[iii].value == targetValue )
        {
            index = iii;
            returnVal = true;
            break;
        }
    }
    dropDown.selectedIndex = index;			
    return returnVal;
}

//---------------------------------------------------------------------------------------------------------------------------------
// Resets the target client window width and height based on:
// 1. The starting client window dimentions.
// 2. The current local screen resolution.
// 3. The starting screen resolution from which to re-adjust (currently 1024x768).
// Returns a new pop window size in the form of [width]X[height].
//---------------------------------------------------------------------------------------------------------------------------------
function ResolutionResize(startingWidth, startingHeight)
{

    var gaugeWidth = 1024;
    var gaugeHeight = 768;
    var resizingWFactor = 1;
    var resizingHFactor = 1;
    var newWidth = gaugeWidth;
    var newHeight = gaugeHeight;
    var returnSize = newWidth + "x" + newHeight;            
    
    // Use the built in "screen" object to get local screen resolution and set a resizing factor.
    if ( screen.width == gaugeWidth && screen.height == gaugeHeight )
    {
        resizingWFactor = 1;
        resizingHFactor = 1;
    }
    else if ( screen.width == 1600 && screen.height == 1200 )
    {
        resizingWFactor = 1.2;
        resizingHFactor = 1.3;
    }
    else if ( screen.width == 1440 && screen.height == 900 )
    {
        resizingWFactor = 1.2;
        resizingHFactor = 1.17;
    }
    else if ( screen.width == 1400 && screen.height == 1050 )
    {
        resizingWFactor = 1.2;
        resizingHFactor = 1.3;
    }
    else if ( screen.width == 1280 && screen.height == 1024 )
    {
        resizingWFactor = 1.1;
        resizingHFactor = 1.3;
    }
    else if ( screen.width == 1280 && screen.height == 800 )
    {
        resizingWFactor = 1.1;
        resizingHFactor = 1;
    }
    else if ( screen.width == 1280 && screen.height == 768 )
    {
        resizingWFactor = 1.1;
        resizingHFactor = .95;
    }
    else if ( screen.width == 1280 && screen.height == 720 )
    {
        resizingWFactor = 1.1;
        resizingHFactor = .9;
    }
    else if ( screen.width > gaugeWidth || screen.height > gaugeHeight )
    {
        resizingWFactor = 1.2;
        resizingHFactor = 1.2;
    }
    else if ( screen.width < gaugeWidth || screen.height < gaugeHeight )
    {
        resizingWFactor = .8;
        resizingHFactor = .8;
    }
    
    // Set a new width and height based on the resizing factor.
    newWidth = startingWidth * resizingWFactor;
    newHeight = startingHeight * resizingHFactor;
    
    // Set the return value as a string.
    returnSize = newWidth + "x" + newHeight;            
    
    return returnSize;

}
        
        
//---------------------------------------------------------------------------------------------------------------------------------
// Hides the "waiting" message.
//---------------------------------------------------------------------------------------------------------------------------------
function HideExecWait()
{
    try
    {
        if ( standard_js_waitArea != null )
        {
            standard_js_waitArea.style.visibility = "hidden";
        }
    }
    catch(e)
    {
    }
    return;
}

//---------------------------------------------------------------------------------------------------------------------------------
// Hides the "waiting" message.
//---------------------------------------------------------------------------------------------------------------------------------
function ShowExecWait(waitText, waitAreaId, imageUrl, top, left, height, width)
{

    var popHtml = "";
    
    try
    {
        standard_js_waitArea = document.getElementById(waitAreaId); // the area where the "processing..." message appears.
        if ( standard_js_waitArea == null )
        {
            throw 'Invalid waitAreaId.'
        }
        standard_js_waitArea.style.top = top;
        standard_js_waitArea.style.left = left;
        standard_js_waitArea.style.height = height;
        standard_js_waitArea.style.width = width;
    }
    catch(e)
    {
        alert("Error in function ShowExecWait(): " + e);
        return;
    }
    
    // Set up the wait area text.            
    popHtml = "<br /><center>";
    if ( waitText != "" )
    {
        popHtml += waitText;
    }
    if ( imageUrl != "" )
    {
        popHtml += "<br /><br /><img src='" + imageUrl + "' alt='' />";
    }
    popHtml += "</center>";
    
    standard_js_waitArea.innerHTML = popHtml;
    
    // Show the wait area.
    standard_js_waitArea.style.visibility = "visible";
    
    return;
    
}

//---------------------------------------------------------------------------------------------------------------------------------
// Returns the current date in format mm/dd/yyyy.
//---------------------------------------------------------------------------------------------------------------------------------
function CurrentDate()
{
    var dt = new Date();
    var mon = dt.getMonth() + 1;
    return mon + '/' + dt.getDate() + '/' + dt.getFullYear() ;
}

//---------------------------------------------------------------------------------------------------------------------------------
// Returns the current time in format HH:mm.
//---------------------------------------------------------------------------------------------------------------------------------
function CurrentTime()
{
    var dt = new Date();
    var min = '0' + dt.getMinutes();
    if ( min.length > 2 )
    {
        min = min.substring(1);
    }
    return dt.getHours() + ':' + min;
}

//---------------------------------------------------------------------------------------------------------------------------------
// Sets the current date and time into the specified text controls.
//---------------------------------------------------------------------------------------------------------------------------------
function SetCurrentDateTime(dateTextControl, timeTextControl)
{
    var currDateCtl = document.getElementById(dateTextControl);
    var currTimeCtl = document.getElementById(timeTextControl);
    try
    {
        if ( currDateCtl == null )
        {
            throw "Date text control " + dateTextControl + " is not valid.";
        }
        if ( currTimeCtl == null )
        {
            throw "Time text control " + timeTextControl + " is not valid.";
        }
        currDateCtl.value = CurrentDate();
        currTimeCtl.value = CurrentTime();
    }
    catch(ex)
    {
        alert("An error occured in function SetCurrentDateTime(): " + ex);
    }
    return true;
}


//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------//
//                                                                          End of Document                                                                              //
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------//
