var loaded=true;
if(MlsPageButtons == null){
	var MlsPageButtons = new Array();
	loaded=false;
}

function Button()
{					  
    var name;
    var isNotIE = false;
    var id;
    var text;
    var spanText;
    var onclick;
    var onmouseoverclass;
    var onmouseoutclass;
    var classname;
    var buttonImage;
    var objButtonImage;
    var container;
    var containerLevel;
    var buttonElement;
    var imageElement;
    var baseImagePath;
    var enableAutoClick = false;
    var leftBorder;
    var rightBorder;
    var okToClean = true;
    
    if(Button.arguments.length > 0)
	{
		var oArg = Button.arguments[0];
		
		// get id of button.
		if(oArg.id != null)
		{
			// Get ID button.
			id = oArg.id;
		}// if(oArg.id != null)
		
		// get name of button.
		if(oArg.name != null)
		{
			// Get name of button.
			name = oArg.name;
		}// if(oArg.id != null)
		
		// get name of button.
		if(oArg.text != null)
		{
			// Get name of button.
			text = oArg.text;
		}// if(oArg.id != null)
		
		// get name of button.
		if(oArg.buttonImage != null)
		{
			// Get name of button.
			buttonImage = oArg.buttonImage;
		}// if(oArg.id != null)
		
		// get if we need to use auto click instead of onclick event.
		if (oArg.enableAutoClick)
		{
			enableAutoClick = oArg.enableAutoClick == '1';			
		}

		// get onclick event
		if (oArg.onclick != null)
		{
		    onclick = oArg.onclick;
		}
		// get onmouseover class name
		if (oArg.onmouseoverclass != null)
		{
		    if (getIsIE())
		    {
		        onmouseoverclass = oArg.onmouseoverclass + "Bar";
		    }
		    else if (getIsMOZ())
		    {
		        onmouseoverclass = oArg.onmouseoverclass + "BarNoIE";
		    }
		    else if (getIsWK())
		    {
		        onmouseoverclass = oArg.onmouseoverclass + "BarWK";
		    }
		}
		// get onmouseout class name
		if (oArg.onmouseoutclass != null)
		{
            if(getIsIE())
		    {
		        onmouseoutclass = oArg.onmouseoutclass + "Bar";
		    }
		    else if(getIsMOZ())
		    {
		        onmouseoutclass = oArg.onmouseoutclass + "BarNoIE";
		    }
		    else if(getIsWK())
		    {
		        onmouseoutclass = oArg.onmouseoutclass + "BarWK";
		    }
		}
		// get  class name
		if (oArg.classname != null)
		{
		    classname = oArg.classname + "Bar";
		}
		// get  class name
		if (oArg.baseImagePath != null)
		{
		    baseImagePath = oArg.baseImagePath;
		}
		//get container id
		if (oArg.container != null)
		{
		    container = oArg.container;
		}
		
		// get level
		if (oArg.containerLevel != null)
		{
		    containerLevel = oArg.containerLevel;
		}
    }
    
    // Define Properties
    this.ID = id;
    this.OnClick = onclick;
    this.Text = text;
    this.SpanText = spanText;
    this.ButtonImage = buttonImage;
    this.ObjButtonImage = objButtonImage;
    this.ButtonElement = buttonElement;
    this.BaseImagePath = baseImagePath;
    this.Container = container;
    this.ContainerLevel = containerLevel;
    this.ClassName = classname;
    this.LeftBorder = leftBorder;
    this.RightBorder = rightBorder;
    this.OkToClean = okToClean;
    
    // Define Methods
    this.Init = Init;
    this.Add = Add;
    this.Remove = Remove;
    this.GetContainerFrame = GetContainerFrame;
    this.GetBorder = GetBorder;
    this.SetText = SetText;
    this.GetText = GetText;
    this.Hide = Hide;
    this.Show = Show;
    this.SetGraphic = SetGraphic;
    this.SetOnClick = SetOnClick;
    this.SetDisable = SetDisable;
    this.SetUnloadBlock = SetUnloadBlock;
    
    // Events
    this.OnClickRaise = OnClickRaise;
    this.OnMouseOverRaise = OnMouseOverRaise;
    this.OnMouseOutRaise = OnMouseOutRaise;
    this.AutoClick = AutoClick;
    
    // Initialize button
    function Init()
    {
        var containerFrame = this.GetContainerFrame();
        if (!getIsWK()) {
            buttonElement = containerFrame.document.createElement("button");
        }
        else {
            buttonElement = containerFrame.document.createElement("input");
        }
        try
        {
            if(!getIsIE())
            {
                //Firefox is seeing every button as a submit button by default.
                //This has been fixed so that the form is not submitted every time you click a button
                buttonElement.type = "button";
            }
        }
        catch (e)
        {
            //loading common cross browser library failed.
            //Solution: add references to jquery.rapattoni.min.js and CrossBrowserLibrary.js (see MLSPageHeader.htm) 
        }
    
        buttonElement.id = id;

        if (getIsMOZ()) 
        {
            this.ClassName = this.ClassName + "NoIE";
        }
        else if (getIsWK())
        {
            if (IsTouchDevice())
            {
                this.ClassName = this.ClassName + "IP";
            }
            else
            {
                this.ClassName = this.ClassName + "WK";
            }
        }

        buttonElement.className = this.ClassName;
        
        if (enableAutoClick)
        {
			buttonElement.onclick = this.AutoClick;
        }
        else
        {
			buttonElement.onclick = this.OnClickRaise;
        }
        
        buttonElement.onmouseover = this.OnMouseOverRaise;
        buttonElement.onmouseout = this.OnMouseOutRaise;
        var objButtonSpan = containerFrame.document.createElement("span");

        var objContentDiv = containerFrame.document.createElement("div");

		// Add button image       
        if(this.ButtonImage != null && !getIsWK())
		{
			var span = containerFrame.document.createElement("span");
			var img = containerFrame.document.createElement("img");
			img.src = this.BaseImagePath + this.ButtonImage;
			img.style.marginRight = "2px";
			img.style.marginLeft="2px"
			img.style.height = "23px";
			img.style.width="28px";
        }

        if (!getIsWK()) {
            // Add button text.
            objButtonSpan.style.background = "transparent url(" + this.BaseImagePath + this.ButtonImage + ") no-repeat 0px -2px";
            objButtonSpan.innerHTML = this.Text;
            objContentDiv.appendChild(objButtonSpan);
            buttonElement.appendChild(objContentDiv);
        }
        else {
            buttonElement.value = this.Text;
        }
        
        this.ButtonElement = buttonElement;
        this.SpanText = objButtonSpan;
    }
    
    // On click raise.
    function OnClickRaise() {

        try {
            eval(onclick + "(this)");
        }
        catch (ex)
        { }
    }
    
    // On mouse over raise.
    function OnMouseOverRaise() {
        if (!IsTouchDevice()) {
            eval("this.className='" + onmouseoverclass + "'");
        }
    }
    
    // On mouse out raise.
    function OnMouseOutRaise() {
        if (!IsTouchDevice()) {
            eval("this.className='" + onmouseoutclass + "'");
        }
    }
    
    // auto click on button with same id in child frame.
    function AutoClick()
    {
		var eventHandleButton = document.getElementById(id);
		
		if (eventHandleButton != null)
		{
			eventHandleButton.click();	
		}
		else
		{
			alert('Unable to find button in child frame with id: ' + id + '. Please look at id passed to button object.');
		}
		
    }
    
    // Add the button.
    function Add()
    {
        var containerFrame = this.GetContainerFrame();
        var containerObj = null;
        
        if (containerFrame != null)
        {
            containerObj = containerFrame.document.getElementById(this.Container);

            if (containerObj != null) {
                //Add Left Border if it exists
                if (this.LeftBorder != null && !getIsWK()) {
                    containerObj.appendChild(this.LeftBorder);
                }

                if (getIsWK()) {
                    var a = containerFrame.document.createElement("a");
                    a.href = 'javascript:document.getElementById(\'' + id + '\').click();';
                    var img = containerFrame.document.createElement("img");
                    img.src = this.BaseImagePath + this.ButtonImage;
                    img.style.position = "absolute";
                    img.style.paddingLeft = "5px";
                    img.style.paddingTop = "6px";
                    img.style.zIndex = "1";
                    imageElement = img;
                    //img.onclick = onclick;
                    //img.style.hover = "pointer";
                    a.appendChild(img);
                    containerObj.appendChild(a);
                }                                

                containerObj.appendChild(this.ButtonElement);

                //Add Right Border is it exists
                if (this.RightBorder != null && !getIsWK()) {
                    containerObj.appendChild(this.RightBorder);
                }
                CheckResolution(containerObj);
            }
        }
    }
    
    function GetBorder(position, total)
	{
		var containerFrame = this.GetContainerFrame();
        var containerObj = containerFrame.document.getElementById(this.Container);
		
		//Add left and right borders (General Case)
		leftBorder = containerFrame.document.createElement("img");
		leftBorder.src = this.BaseImagePath + "/toolbarseparator.png";
		this.LeftBorder = leftBorder;
	
		rightBorder = containerFrame.document.createElement("img");
		rightBorder.src = this.BaseImagePath + "/toolbarseparator.png";
		this.RightBorder = rightBorder;
		
		if (position != 0)
		{
			this.LeftBorder = null;
		}
	}

    
    // Remove the button.
    function Remove()
    {
        var containerFrame = this.GetContainerFrame();
        var containerObj = null;
        
        if (containerFrame != null)
        {
            containerObj = containerFrame.document.getElementById(this.Container);
            
            if(containerObj.children.length > 0)
            {
				//Remove left border if it exists
					try{
						containerObj.removeChild(this.LeftBorder);
					}catch(err){}
				containerObj.removeChild(this.ButtonElement);
				//Remove right border if it exists
				try
				{
					containerObj.removeChild(this.RightBorder);
				}catch(err){}
            }
        }
    }
    
    // Returns the container.
    function GetContainerFrame()
    {
        var containerFrame = self; 
        
        for (var counter = 0; counter < this.ContainerLevel; counter++)
        {
            containerFrame = containerFrame.parent;
        }
        
        return containerFrame;
    }
    
	// Mutator/Accessor Methods
	function SetText(newText)
	{
		this.Text = newText;      
		var containerFrame = this.GetContainerFrame();
        var containerObj = null;
        
        if(this.ButtonElement!=null){
			// Remove old text. 
			if (containerFrame != null)
			{
				containerObj = containerFrame.document.getElementById(this.Container);
				if (this.ButtonElement.childNodes.length > 0)
				    this.SpanText.innerHTML = newText;
				else if(getIsWK())
				    this.ButtonElement.value = newText;
			}
		}
	}    
    
    function GetText()
	{
		return this.SpanText.innerHTML;
    }
    
    function Hide()
    {
        this.ButtonElement.style.display = "none";
        this.RightBorder.style.display = "none";

        if (getIsWK()) {
            imageElement.style.display = "none";
        }
    }
    
    function Show()
    {
        this.ButtonElement.style.display = "";
        this.RightBorder.style.display = "";

        if (getIsWK()) {
            imageElement.style.display = "";
        }
    }
    
    function SetGraphic(imgPath)
    {
		this.ButtonImage = imgPath;
		var containerFrame = this.GetContainerFrame();
        var containerObj = null;
		
		// Remove old image. 
        if (containerFrame != null)
        {
            containerObj = containerFrame.document.getElementById(this.Container);
        
			if(this.ButtonElement.children.length > 0 && !getIsWK())
            {
				this.ButtonElement.removeChild(this.ObjButtonImage);
            }
        }
		
		// Add button image.       
        if(this.ButtonImage != null)
        {
			var objButtonImage = containerFrame.document.createElement("img");
			objButtonImage.src = this.BaseImagePath + this.ButtonImage;
			objButtonImage.style.marginRight = "2px";
			objButtonImage.style.marginLeft = "2px";
			//this.ButtonElement.appendChild(objButtonImage);
			if (!getIsWK()) {
			    buttonElement.style.background = "transparent url(" + this.BaseImagePath + this.ButtonImage + ") no-repeat 5px 5px";
			}
			this.ObjButtonImage = objButtonImage;
        }
        this.SetText(this.Text);
    }
    
    function SetOnClick(fcnName)
    {
		this.OnClick = fcnName;
    }
}


//Method to add page buttons.
function AddPageButtons()
{
    if (document.readyState == "complete" && !isNotIE)
    {
		var pageButtonObj;
		var position = 0;
		var count = 0;
		
		for(var pageButton in MlsPageButtons)
        {
			count = count + 1;
        }

        for(var pageButton in MlsPageButtons)
        {
            pageButtonObj = MlsPageButtons[pageButton];
		    pageButtonObj.GetBorder(position, count);
            pageButtonObj.Init();
            pageButtonObj.Add();
            position = position + 1;
        }

        document.body.onbeforeunload = RemovePageButtons;
    }else if(isNotIE)
    {
        var pageButtonObj;
		var position = 0;
		var count = 0;
		
		for(var pageButton in MlsPageButtons)
        {
			count = count + 1;
        }

        for(var pageButton in MlsPageButtons)
        {
            pageButtonObj = MlsPageButtons[pageButton];
            
		    pageButtonObj.GetBorder(position, count);
            pageButtonObj.Init();
            pageButtonObj.Add();
            position = position + 1;
        }
        //to change this for Firefox
        document.body.onbeforeunload = RemovePageButtons;
    }
}

// Method to remove page buttons.
function RemovePageButtons()
{
    CheckResolution();
	if(this.OkToClean){
		for(var pageButton in MlsPageButtons)
		{
			var pageButtonObj = MlsPageButtons[pageButton]
			pageButtonObj.Remove();
		}
	}
}

function SetDisable(isDisabled) {
    this.ButtonElement.disabled = isDisabled;
    if (isDisabled == false) {
            this.ButtonElement.className = this.ClassName;
    }
    else {
        if (getIsIE()) {
            this.ButtonElement.className = "DisToolBarButton";
        }
        else if(getIsMOZ()) {
            this.ButtonElement.className = "DisToolBarButtonNoIE DisToolBarButtonNoIEBg";
        }
        else {
            this.ButtonElement.className = "DisToolBarButtonWK DisToolBarButtonWKBg";
        }
    }
}

function SetUnloadBlock()
{
	this.OkToClean=false;
}

function UpdateButton(ButtonID)
{
	alert(MlsPageButtons[ButtonID]);
}

function isIE6(){
	version=10;
	if (navigator.appVersion.indexOf("MSIE")!=-1){
		temp=navigator.appVersion.split("MSIE");
		version=parseFloat(temp[1]);
	}
	if (version<7){
		return true;
	}
	return false;
}

function CheckResolution(contObj)
{
    try
    {
        if(contObj!=null && contObj.offsetWidth >= 645 && screen.width<1000)
	    {
	        if(parent.document.body != null)
	        {
	            parent.document.body.scroll = "yes";
	        }
    	    
	    }
	    else
	    {
	        if(parent.document.body != null)
	        {
	            parent.document.body.scroll = "no";
	        }
	    }
	}
	catch(problem){}
}

if(!loaded)
{
//AttachEvent(document, 'load', AddPageButtons);

	//document.attachEvent("onreadystatechange", AddPageButtons);
	if(window.addEventListener){ // Mozilla, Netscape, Firefox
	    AttachEvent(window, 'load', AddPageButtons);
	    //object.addEventListener('click', testevent, false);
	    //object.myflag = "test";
	    //object.mydata = "123";
	    isNotIE = true;
    } else { // IE
	    AttachEvent(document, 'readystatechange', AddPageButtons);
	    isNotIE = false;
    }
}

//Cross Browser AttachEvent try
function AttachEvent(obj, evt, func, useCap)
{
    if(!useCap)
    {
        useCap=false;
    }
    if(obj.addEventListener)
    {
        obj.addEventListener(evt, func, useCap)
        return true;
    }
    else if(obj.attachEvent)
    {
        return obj.attachEvent("on" + evt, func);
    }
}
