/// <reference path="common.js" />
/// <reference path="MessageBox.js" />

//Attach Event Handlers.
if  (window.attachEvent) //IE exclusive method for attaching an event
{
    window.attachEvent("onload", mainPageLoad);
}
else
{
    window.addEventListener("load", mainPageLoad, false);
}

document.onkeypress = keyPress;

//--------------------------------------------------------------------------
//Global script variables.
//--------------------------------------------------------------------------
var oActiveDefaultButton;       //default button that is currently active.
var oPageDefaultButton;	        //default button for the page.
var oEditDefaultButton;         //default button for the Edit Pop-Up box.
var oPageDefaultFocus;          //default focus location.

//--------------------------------------------------------------------------
//  Page Initialization Event handlers
//--------------------------------------------------------------------------
// Page Load
function mainPageLoad()
{
    try
    {
        if (window.detachEvent)  //IE exclusive method for detaching an event
        {
            window.detachEvent("onload", mainPageLoad);
        }
        else
        {
            window.removeEventListener("load", mainPageLoad, false);
        }
        
        if  (window.attachEvent) //IE exclusive method for attaching an event
        {
            window.attachEvent("onunload", mainPageUnload);
        }
        else
        {
            window.addEventListener("unload", mainPageUnload, false);
        }
         
		if (oPageDefaultButton)	{this.setActiveDefaultButton(oPageDefaultButton);};  //register the button as the active default button.
    }
    catch (err){alert("pageLoad: " + err.message)};
}


// Page Unload
function mainPageUnload()
{
    try
    {
		//Minimize memory leaks.
		if (window.detachEvent)  //IE exclusive method for detaching an event
        {
            window.detachEvent("onunload", mainPageUnload);
        }
        else
        {
            window.removeEventListener("unload", mainPageUnload, false);
        }
		document.onkeypress = null;
		oActiveDefaultButton = null;
		oPageDefaultButton = null;
		oEditDefaultButton = null;
		oPageDefaultFocus = null;
    }
    catch (err){alert("main_v3.pageUnload: " + err.message)};
}

//--------------------------------------------------------------------------
//  Page Event handlers
//--------------------------------------------------------------------------
function keyPress(e)
{
    var evtobj=window.event? event : e

    checkEnterKeyPressed(oActiveDefaultButton, evtobj)
}

//--------------------------------------------------------------------------
//  Button Event handlers
//--------------------------------------------------------------------------
//Register the button argument as the Default button object for the page.
function setDefaultButton(oButton) 
{
    this.oPageDefaultButton = oButton;
}

//Register the button argument as the Active Default button object for the page.
function setActiveDefaultButton(oButton, changeImage) 
{
    if (changeImage == true)
    {
        this.setButtonFace(oActiveDefaultButton , "normal");    //set the current Active Default button to Normal
        this.setButtonFace(oActiveDefaultButton , "default");   //set the appearance of the new Active Default button
    }
    
    this.oActiveDefaultButton = oButton;                    //register the button as the Active Default button.
}

//Returns the Id of an Infragistics Button control or HTML button control.
function getButtonId(oButton){
    try{
        var igButton = ig_getWebControlById(oButton._id);   //Determine if the object is an Infragistics button.
        if (igButton)
            return oButton._id;    //return the Infragistics button control Id
        else
            return oButton.id;     //return the HTML button control Id
    }
    catch(err){return oButton.id};  //no Infragistics controls are used, return the HTML button control Id
}


//Fires the click event of the button object when the enter key is pressed.
function checkEnterKeyPressed(oButton,event) 
{
    try
    {
       var keyCode = (event.which) ? event.which : event.keyCode; 
       
        if (oButton && keyCode == 13) 
        {
            oButton.click();
            event.returnValue = false;
            event.cancel = true;
        }
    }
    catch (err){alert("checkEnterKeyPressed: " + err.message)};
}

//Command Button Event Handler.
var omainButtonWithFocus;
function buttonEventHandler(oButton, oEvent){
    try{
        var eventType, buttonId, igControl;
        
        //Determine if the Event was generated from an Infragistics button control.
        if (oEvent.type){
            eventType = oEvent.type;         //HTML control generated event
            buttonId = oButton.id;
            igControl = false;
            }
        else {
            eventType = oEvent.event.type;   //Infragistics control generated event
            buttonId = oButton._id;
            igControl = true;
        };
            
        switch (eventType) {
            case "mouseover":
                this.setButtonFace(oButton, "hover");
                break;
            case "mouseout":
                //if the button has focus, restore to focus style
                if(omainButtonWithFocus && omainButtonWithFocus.id == buttonId){this.setButtonFace(oButton, "focus"); break;};

                //if it's the Default button and no other button has focus, restore the Default style
                if(oActiveDefaultButton && getButtonId(oActiveDefaultButton) == buttonId && !omainButtonWithFocus)
                    this.setButtonFace(oButton, "default");                
                else
                    this.setButtonFace(oButton, "normal");
                break;
            case "focus":
                omainButtonWithFocus = oButton;
                if (!igControl) this.setButtonFace(oButton, "focus");
                
                //Change the Active Default button image to a Normal button.
                if (oActiveDefaultButton && getButtonId(oActiveDefaultButton) != buttonId){
                    this.setButtonFace(oActiveDefaultButton, "normal");
		        };
                break;
            case "blur":
                omainButtonWithFocus = null;
                if(oActiveDefaultButton && getButtonId(oActiveDefaultButton) == buttonId)
                    this.setButtonFace(oButton, "default");                
                else
                    this.setButtonFace(oButton, "normal");

                //Change the Active Default button image to a Default button.
                this.setButtonFace(oActiveDefaultButton, "default");
                break;
            case "keydown":
                this.checkEnterKeyPressed(oButton);
                break;
            };
    }
    catch (err){alert("main_v3.buttonEventHandler: " +  err.message)};
}

function setButtonFace(oButton, buttonState){
    try{
        if (!oButton) return;
        if (oButton.type == 'image' || oButton.type == '' || oButton.tagName == "IMG") return;
        
        var igBtnImage, btnClass;
        switch (buttonState) {
            case "normal":
                igBtnImage = "Button-Up.png";
                break;
            case "default":
                igBtnImage = "Button-Up.png";
                break;
            case "hover":
                igBtnImage = "Button-Over.png";
                break;
            case "focus":
                igBtnImage = "Button-Up.png";
                break;
            case "pressed":
                igBtnImage = "Button-Down.png";
                break;
            case "disabled":
                igBtnImage = "Button-Disabled.png";
                break;
        };

        try
        {
            var igButton = ig_getWebControlById(oButton._id);   //check if the object is an Infragistics button.
            if (igButton)
                igButton.setRoundedImageAt("../../Images/Buttons/" + igBtnImage, 0, false); 
        }
        catch(err){};  //no Infragistics controls are used, set the CSS classname.
    }
    catch (err){alert("main_v3.setButtonFace: " + err.message)};
}

//--------------------------------------------------------------------------
//  Print Event handlers
//--------------------------------------------------------------------------
// Print this page
function printWindow(){
   bV = parseInt(navigator.appVersion);
   if (bV >= 4) window.print();
}

//Trims leading and trailing spaces from the value.
function trim(val){return val.replace(/^\s+|\s+$/g, '');}

//-------------------------------------------------------------------------
//  I-Frame Sizing Functions
//-------------------------------------------------------------------------
function adjustMyFrameHeight(Iframe)
{
    var frameDoc;
    
    if (Iframe.contentDocument)
        frameDoc = Iframe.contentDocument;
    else // bad IE  ;)
        frameDoc = document.frames[0].document;
    
    Iframe.height = frameDoc.body.scrollHeight;
}



