var KEY_BACKSPACE = 8;
var KEY_ZERO = 48;
var KEY_NINE = 57;
var KEY_ENTER = 13;
var WHICH_TAB = 0;
var KEY_TAB = 9;  
var dKEY_ZERO = 96;
var dKEY_NINE = 105; 
var ARROW_KEY_L = 37;
var ARROW_KEY_U = 38;
var ARROW_KEY_R = 39;
var ARROW_KEY_D = 40;
var DELETE_KEY = 46;

document.onkeypress = HandleKeyPresses;
if (document.layers)
	document.captureEvents(Event.KEYPRESS);

function GetXmlHttpObject()
{
	var xmlHttpObj = null;
	
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttpObj = new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try	{
			xmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e) {
			xmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	
	return xmlHttpObj;
}

//Focused element --------------------------------------------------------
function GetFocusedElement(e)
{
	var focussedElement = null;
	var agt = navigator.userAgent.toLowerCase();

	//Get the focussed element.
	if(agt.indexOf("ie") > -1) //IE
	{
		focussedElement =  event.target ? event.target : event.srcElement;
	}
	else //Mozilla
	{
		focussedElement =  e.target ? e.target : e.srcElement;
	}
	
	return focussedElement;
}

//Key pressed -------------------------------------------------------------
function GetKeyPressed(e)
{
	var pKeyPressed = null;
	var agt = navigator.userAgent.toLowerCase();

	//Get the focussed element.
	if(agt.indexOf("ie") > -1) //IE
	{
		pKeyPressed = document.all? window.event.keyCode:e.which;
	}
	else //Mozilla
	{
		pKeyPressed = document.all? e.keyCode : e.which;
	}
	
	return pKeyPressed;
}

//Enter key ---------------------------------------------------------------
function HandleKeyPresses(e) 
{
	var pKeyPressed = GetKeyPressed(e);
	var focussedElement = GetFocusedElement(e);
	var agt = navigator.userAgent.toLowerCase();
		
	//Handle certain keys.
	switch(pKeyPressed)
	{
		case KEY_ENTER:
		{
			//Do we have the hidden field with our default button?
			if( g_hfEnterKeyBtn != null )
			{
				//Cancel the enter key event. 
				//(so submit buttons, such as ImageButton, will not postback twice)
				if(agt.indexOf("ie") > -1) //IE
				{
					document.all ? window.event.returnValue=false : e.returnValue=false;
					document.all ? window.event.cancel=true : e.cancel = true;
				}
				else //Mozilla
				{
					e.returnValue=false;
					e.cancel = true;
				}

                if( g_hfEnterKeyBtn != '' ) {
				    var defBtn = document.getElementById(g_hfEnterKeyBtn);
				    //Does the value of the default button actually exist as an element?
				    if(defBtn != null)
				    {
					    //if a button other than the default button has focus, 
					    //then execute it's click instead.
					    if(focussedElement != null && (focussedElement.id != defBtn.id) )
					    {
						    //Make sure it's a button before we try to use click.
						    if(focussedElement.type == "image" || focussedElement.type == "button")
							    focussedElement.click()
						    else
							    defBtn.click();
					    }
					    else
						    defBtn.click();
				    }
				}
				else //no default button specified, so use the focused one.
				{
					//Make sure it's a button before we try to use click.
					if(focussedElement.type == "image" || focussedElement.type == "button")
						focussedElement.click()
				}
			}
		    break;
		}
	    default:
	        break;
	}
}


//Sets the focus to a pre-determined input field on the form.
function SetInputFocus()
{
    var focusElement = null;

	//Do we have the hidden field with our custom focus element?
	if( g_hfInputFocus != null )
	{
	    if( g_hfInputFocus != '' ) {
		    focusElement = document.getElementById(g_hfInputFocus);
    		
		    //Does the value of the custom focus actually exist as an element?
		    if(focusElement != null)
		    {
			    try {
				    focusElement.focus();
				    return;
			    }
			    catch(e){}
		    }
		}
	}
	
    if( focusElement == null ) {
        try {        
            //Set focus to the first input/select element on form.
            var divContent = document.getElementById('MainBoxContent');
            var inputs = divContent.getElementsByTagName('INPUT');

            for (var i = 0; i < inputs.length; i++)	{
                var elem = inputs[i];

                //Set the first input/select that is visible or not hidden input.
                if( elem.type == 'text' ) { 
                    if ((elem.style.display != null && elem.style.display == 'none') || 
                    (elem.style.visibility != null && elem.style.visibility == 'hidden') || 
                    (elem.className != null && elem.className == 'MCDate') ) {
                        continue;
                    }
        		    else {
                        elem.focus();
                        break;
                    }
                }
            }
        }
        catch (e){}
    }
}

