
function PCSButton (doc, sImgPath)
{
  
 	PCSButton.prototype.loadImages = function (sPath, bIgnoreDelay)
	{
          
		if (this.m_Image == null)
		{
			this.m_sBasePath = sPath;
			return;
		}		
		
		if (typeof (this.m_Image) == "undefined")
			return;

		if (bIgnoreDelay || typeof (this.m_Image.onerror) == "undefined" || typeof (this.m_Image.onload) == "undefined")
		{
			for (var i = 0; i < this.m_exts.length; i++)
			{
				if (!this.stateAvailable (this.m_exts[i]))
					continue;
					
				var sName = this.buildName (sPath, this.m_exts[i]); 
				this.m_Images[i] = new Image;
				this.m_Images[i].onerror = new Function ("document.m_PCSBtns[" + this.m_nBtnIndex + "].m_Images[" + i + "] = null;");
				this.m_Images[i].src = sName;
			}
		}
		else
		{
			this.delayLoad (sPath, 0);
		}
	};
	

	PCSButton.prototype.delayLoad = function (sPath, nIdx)
	{
		if (nIdx >= this.m_exts.length)
		{
			this.updateImage ();
			return;
		}

		if (!this.stateAvailable (this.m_exts[nIdx]))
		{
			this.delayLoad (sPath, nIdx + 1);
			return;
		}

		var sName                  = this.buildName (sPath, this.m_exts[nIdx]);
		var sErrorFcn              = "document.m_PCSBtns[" + this.m_nBtnIndex + "].m_Images[" + nIdx + "] = null;";
		var sFcnCallOnError        = "document.m_PCSBtns[" + this.m_nBtnIndex + "].delayLoad ('" + sPath + "', " + (nIdx + 1) + ");";

		this.m_Images[nIdx]                         = new Image;
		this.m_Images[nIdx].onerror                 = new Function (sErrorFcn + ";" + sFcnCallOnError);
		this.m_Images[nIdx].onload	            = new Function (sFcnCallOnError);
		this.m_Images[nIdx].src                     = sName;
	};
             
	PCSButton.prototype.stateAvailable = function (sExt)
	{
		var nItems = this.m_arrAvailStates.length;
		for (var i = 0; i < nItems; i++)
		{
			if (this.m_arrAvailStates[i] == sExt)
				return (true);
		}

		return (false);
	};
                      
	PCSButton.prototype.buildName = function (sPath, sExt)
	{
		var szLookup = this.m_LookupState[sExt];
		if (szLookup != null)
		{
			szLookup = this.getParam (szLookup);
			if (szLookup != "")
				return (szLookup);
		}

		return (sPath.replace (/(\.gif|\.jpg|\.png)/, ("_" + sExt + "$1")));
	};
                   
	PCSButton.prototype.over = function (bMouseOver)
	{
		var a_szFuncName = ["onmouseout", "onmouseover"];
		if (this.m_bEnable && this.m_bInside != bMouseOver)
		{
			if (!bMouseOver)		// can't be pressed if not over!
				this.m_bPress = false;
			this.m_bInside = bMouseOver;
			this.updateImage ();
			this.execParam (a_szFuncName[bMouseOver ? 1 : 0]);
		}
	};
                            
	PCSButton.prototype.press = function (i_bPressed)
	{
		var a_szFuncName = ["onmouseup", "onmousedown"];
		if (this.m_bEnable && this.m_bInside && this.m_bPress != i_bPressed)
		{
			this.m_bPress = i_bPressed;
			this.updateImage ();
			this.execParam (a_szFuncName[i_bPressed ? 1 : 0]);
		}
	};
	             
	PCSButton.prototype.keyPress = function (event)
	{
		bReturn = false;
		
		if (this.m_bEnable)
		{
			if (event.keyCode == 13)
			{
				bReturn = this.click();
			}
		}
		
		return bReturn;
	};
	                     
	PCSButton.prototype.click = function ()
	{
		if (this.m_bEnable)
		{
			this.m_bPress = true;
			this.updateImage ();
			this.execParam ("onclick");
			this.m_bPress = false;
			this.updateImage ();
		}

		var szHref = this.getParam ("href");
		return (this.m_bEnable && szHref != "");
	};
                           
	PCSButton.prototype.enable = function (bEnable)
	{
		if (this.m_bEnable != bEnable)
		{
			this.m_bEnable = bEnable;
			this.updateImage ();
		}
	};
                    
	PCSButton.prototype.updateImage = function ()
	{
		var pNewImage = this.m_Image;

		if (typeof (this.m_Images[0]) != "undefined" && this.m_Images[0] != null)
			pNewImage = this.m_Images[0];

		if (this.m_bVisible == false)
		{
			pNewImage = this.m_Images[4];
		}
		else if (this.m_bEnable)
		{
			if (this.m_bPress)
			{
				if (typeof (this.m_Images[1]) != "undefined" && this.m_Images[1] != null)
					pNewImage = this.m_Images[1];
			}
			else if (this.m_bInside)
			{
				if (typeof (this.m_Images[3]) != "undefined" && this.m_Images[3] != null)
					pNewImage = this.m_Images[3];
			}
		}
		else
		{
			if (typeof (this.m_Images[2]) != "undefined" && this.m_Images[2] != null)
			{
				pNewImage = this.m_Images[2];
			}
		}

		if (pNewImage != null)
		{
			this.m_Image.src = pNewImage.src;
		}
	};
                            
	PCSButton.prototype.getParam = function (sParamName)
	{
		var szVal = this.m_aParams[sParamName];
		if (szVal == null)
			szVal = new String ("");

		return (szVal);
	};
                            
	PCSButton.prototype.execParam = function (sParamName)
	{
		var bResult = true;
		var szCmd = this.getParam (sParamName);
		if (szCmd != "")
		{
			var pFunc = new Function (szCmd);
			bResult = pFunc ();
		}

		return (bResult);
	};
                                 
	PCSButton.prototype.setParam = function (sParamName, sFcn)
	{
		this.m_aParams[sParamName] = sFcn;
	};
             
	PCSButton.prototype.parseParams = function (arrParams, nFirstIdx)
	{
		if (arrParams == null || typeof (arrParams) != "object")
			return;

		if (nFirstIdx == null || nFirstIdx < 0)
			nFirstIdx = 2;

		for (var i = nFirstIdx; i < arrParams.length; i++)
		{
			var sTemp = arrParams[i];
			var nIndex;
			if ((nIndex = sTemp.indexOf ("=")) >= 0)
			{
				var sVarName = sTemp.substr (0, nIndex).toLowerCase ();
				var sParam = sTemp.substr (nIndex +1);

				if (sParam.charAt (0) == sParam.charAt (sParam.length -1))
				{
					if (sParam.charAt (0) == "'" || sParam.charAt (0) == "\"")
						sParam = sParam.substr (1, sParam.length -2);
				}
				this.setParam (sVarName, sParam);
			}
			else
			{
				switch (sTemp.toLowerCase ())
				{
					case "disabled":
						this.m_bEnable = false;
						break;
						
					case "enabled":
						this.m_bEnable = true;
						break;

					case "hidden":
						this.m_bVisible = false;
						break;
				}
			}
		}
	};

                  
	PCSButton.prototype.beginImageLoad = function ()
	{
		if (this.m_bLoaded)
			return;

		var szStates = this.getParam ("states");
		if (szStates != "")
		{
			this.m_arrAvailStates = szStates.split (",");
		}
		this.m_bLoaded = true;
		this.loadImages (this.m_sBasePath, false);
		this.updateImage ();
	};
                    
	PCSButton.prototype.buildAnchor = function ()
	{
                 
		if (typeof (this.m_document.m_PCSBtns) == "undefined")
			this.m_document.m_PCSBtns = new Array ();
		this.m_nBtnIndex = this.m_document.m_PCSBtns.length;
		this.m_document.m_PCSBtns[this.m_nBtnIndex] = this;

		var szHref = this.getParam ("href");
		var szTarget = this.getParam ("target");
		var sName = this.getParam ("name");
		var szWidth = this.getParam ("width");
		var szHeight = this.getParam ("height");
		var szAlt = this.getParam ("alt");
		var szStyle = this.getParam ("style");

		szTarget = (szTarget == "" ? "" : "target='" + szTarget + "'");

		szHref = (szHref == "" ? "href='#'" : "href='" + szHref + "' ");
		sName = (sName == "" ? "" : "name='" + sName + "' ");
		szWidth = (szWidth == "" ? "" : "width='" + szWidth + "' ");
		szHeight = (szHeight == "" ? "" : "height='" + szHeight + "' ");
		szAlt = (szAlt = "" ? "" : "alt='" + szAlt + "' ");
		szStyle = (szStyle = "" ? "" : "style='" + szStyle + "' ");

		this.m_document.write ("<a " + szHref + sName +
				"onMouseOver='document.m_PCSBtns[" + this.m_nBtnIndex + "].over (true); return (true);' " +
				"onMouseOut='document.m_PCSBtns[" + this.m_nBtnIndex + "].over (false);' " +
				"onMouseDown='document.m_PCSBtns[" + this.m_nBtnIndex + "].press (true);' " +
				"onMouseUp='document.m_PCSBtns[" + this.m_nBtnIndex + "].press (false);' " +
				"onKeyPress='document.m_PCSBtns[" + this.m_nBtnIndex + "].keyPress (event);' " +
				"onClick='return (document.m_PCSBtns[" + this.m_nBtnIndex + "].click ());' " + szTarget + ">");

		var szUpName = this.buildName (this.m_sBasePath, this.m_bEnable ? "u" : "x");
		

		this.m_document.write ("<img src='" + szUpName + "' border='0' " + sName  + szWidth + szHeight + szAlt + szStyle +
			" onLoad='this.m_Button=document.m_PCSBtns[" + this.m_nBtnIndex + "]; document.m_PCSBtns[" + this.m_nBtnIndex + "].m_Image = this; document.m_PCSBtns[" + this.m_nBtnIndex + "].beginImageLoad ();'"
			 + ">");
		this.m_document.write ("</a>");
         };
         

    this.m_exts = ["u", "d", "x", "o", "h"];
    this.m_arrAvailStates = ["u","o","d"];

    // store the various states
    this.m_LookupState = new Array (this.m_exts.length);
    this.m_LookupState["u"] = "img_up";
    this.m_LookupState["o"] = "img_over";
    this.m_LookupState["d"] = "img_press";
    this.m_LookupState["x"] = "img_disable";
    this.m_LookupState["h"] = "img_hidden";

    this.m_Image = null;

    this.m_document    = null;
    this.m_sBasePath   = "";

    // init state
    this.m_bEnable     = true;
    this.m_bVisible    = true;
    this.m_bPress      = false;
    this.m_bInside     = false;
    this.m_bLoaded     = false;

    if (sImgPath != "")
    {
            //m_Images[0] = Disabled State
            //m_Images[1] = Down State
            //m_Images[2] = Mouse Over State
            //m_Images[3] = Up State
            //m_Images[4] = Hidden State

        this.m_Images = new Array (this.m_exts.length);
        this.m_aParams = new Object ();

        this.m_document = doc;
        this.m_sBasePath = sImgPath;
        this.parseParams(arguments, 2);
        this.buildAnchor();
    }
         
};
/*

        Usage:
		var x = new PCSButton (document, 'images/btn.gif', ['disabled',] ['href=something.php',] ['onClick=document.forms['mine'].submit();']);

        Required Images( must be .gif or .jpg):
        DO NOT include the _? part of the name when calling
		For the above example the images must be named as follows:
            web_u.gif - up image
            web_o.gif - mouse over image
            web_d.gif - down image
            web_x.gif - disabled image
            web_h.gif - hidden image


        Usage 2:
        var btn = new PCSButton (document, 'images/web.gif', 'onClick=document.forms['mine'].submit ()');

        Required Arguments:
		The first argument is the document to place the	button on.
		The second argument is the relative path of the image to be drawn.

        Optional Arguments:
        Optional you can include the following after the image parameter, in any order.
        onClick        - handler which will be called when the click occrurs
        onMouseUp        - hander which will be called when the mouse is released over the item
        onMouseOver    - hander which will be called when the mouse rolls over the item
        onMouseOut    - hander which will be called when the mouse rolls out of the item
        onMouseDown    - hander which will be called when the mouse is pressed on the item
        href			- href of a URL to link to upon clicking on the button
        target		- the target property for the anchor tag
        name			- name assigned to the anchor AND image tags
        img_up		- relative path for image to use for default up state (overrides the default)
        img_over		- relative path for image to use for rollover state (overrides the default)
        img_press		- relative path for image to use for depressed state (overrides the default)
        img_disable	- relative path for image to use for disabled state (overrides the default)
        img_hidden	- relative path for image to use for hidden state (overrides the default)
        alt			- alt tag for image
        disabled      - to disable the button on creation (no argument)
        states		- states to load (comma separated string of r, u, d, x, h)

\*/

