﻿// JScript File

// $: get a set of objects
function $()
{
    var arrElements = new Array ();
    for (var v = 0; v < arguments.length; v++)
    {
        var objElement = arguments [v];
        if (typeof objElement == "string")
        {
            objElement = document.getElementById (objElement);
        }
        if (arguments.length == 1)
        {
            return (objElement);
        }
        arrElements.push (objElement);
    }
    return (arrElements);
}

//getAttribute (strElementID, strAttributeName)
//strElementID: ID of the Element to Access
//strAttributeName: the Name of the Attribute to Access
//Doesn't Work in Firefox
function getAttribute (strElementID, strAttributeName)
{
    var objCommand = new StringBuffer ();
    objCommand.append ("$(");
    objCommand.append (strElementID);
    objCommand.append (").");
    objCommand.append (strAttributeName);
    return (eval (objCommand.toString ()));
}

//getMouseCoordinates
//eventArgs = eventArgs || window.event
//eventArgs must be passed by declaring control.onAction = Some_Method
//  and taking the arguments to Some_Method
function getMouseCoordinates (eventArgs)
{
    alert (eventArgs);
    if (eventArgs.pageX || eventArgs.pageY)
    {
        //in FF
        return {x: eventArgs.pageX, 
                y: eventArgs.pageY};
     }
     //in IE
     return {x: eventArgs.clientX + document.body.scrollLeft - document.body.clientLeft,
             y: eventArgs.clientY + document.body.scrollTop - document.body.clientTop};
}
        
//getPosition (strElementID)
function getPosition (strElementID)
{
    var theElement = $(strElementID);
    if (theElement)
    {
        var xPosition = theElement.offsetLeft;
        var yPosition = theElement.offsetTop;
        theElement = theElement.offsetParent;
        while (theElement)
        {
            xPosition += theElement.offsetLeft;
            yPosition += theElement.offsetTop;
            theElement = theElement.offsetParent;
        }
        return ({x: xPosition, y: yPosition});
    }    
}

//hideElement (strElementID)
function hideElement (strElementID)
{
    var theElement = $(strElementID);
    if (theElement)
    {
        theElement.style.display = ("none");
    }
}

//preloadImage (strImageSrc)
function preloadImage (strImageSrc) 
{
    var imgTemp = new Image ();
    imgTemp.src = strImageSrc;
}

//getStyleAttribute (strElementID, strAttributeName)
//strElementID: ID of the Element to Access
//strStyleAttributeName: the Name of the Style Attribute to Access
//Doesn't Work in Firefox
function getStyleAttribute (strElementID, strStyleAttributeName)
{
    var objCommand = new StringBuffer ();
    objCommand.append ("$(");
    objCommand.append (strElementID);
    objCommand.append (").style.");
    objCommand.append (strStyleAttributeName);
    alert (objCommand.toString ());
    return (eval (objCommand.toString ()));
}

//setAttribute (strElementID, strAttributeName, strAttributeValue)
//strElementID: ID of the Element to Modify
//strAttributeName: the Name of the Attribute to Modify
//strAttributeValue: the Value to Set the Attribute To
function setAttribute (strElementID, strAttributeName, strAttributeValue)
{
    var objCommand = new StringBuffer ();
    objCommand.append ("$(");
    objCommand.append (strElementID);
    objCommand.append (").");
    objCommand.append (strAttributeName);
    objCommand.append (" = ('");
    objCommand.append (strAttributeValue);
    objCommand.append ("');");
    eval (objCommand.toString ());
}

//setPosition (strElementID, objLocation)
//strElementID: the ID of the Element to Position
//objLocation: an Object with Properties x and y Giving the Coordinates of the Element
function setPosition (strElementID, objLocation)
{
    var theElement = $(strElementID);
    theElement.style.position = ("absolute");
    theElement.style.left = (objLocation.x + "px");
    theElement.style.top  = (objLocation.y + "px");
}

//setStyleAttribute (strElementID, strAttributeName, strAttributeValue)
//strElementID: ID of the Element to Modify
//strStyleAttributeName: the Name of the Style Attribute to Modify
//strAttributeValue: the Value to Set the Attribute To
function setStyleAttribute (strElementID, strStyleAttributeName, strAttributeValue)
{
    var objCommand = new StringBuffer ();
    objCommand.append ("document.getElementById('");
    objCommand.append (strElementID);
    objCommand.append ("').style.");
    objCommand.append (strStyleAttributeName);
    objCommand.append (" = ('");
    objCommand.append (strAttributeValue);
    objCommand.append ("');");
    eval (objCommand.toString ());
}

//showElement (strElementID)
function showElement (strElementID)
{
    var theElement = $(strElementID);
    if (theElement)
    {
        theElement.style.display = ("");
    }
}

//StringBuffer Object
//StringBuffer (strInitial): Creates a New String Buffer with Initial Text strInitial
//append (strAppend): Adds the String strAppend to the Buffer
//toString (): Returns a String of the Buffer
//clear (): Clears the Buffer
function StringBuffer (strInitial)
{
    this.arrBuffer = new Array ();
    this.append (strInitial);
}
StringBuffer.prototype.append = function (strAppend)
{
    if (strAppend)
    {
        this.arrBuffer.push (strAppend);
    }
};
StringBuffer.prototype.toString = function ()
{
    return (this.arrBuffer.join (""));
};
StringBuffer.prototype.clear = function ()
{
    this.arrBuffer.length = 0;
};