// Applies onfocus and onblur events onto an element to add a class on focus and remove it on blur
function applyElementOnFocusAndOnBlurEvents( element )
{
	element.onfocus = function() 
	{
		this.className += " withFocus";
		this.select();
	}
	element.onblur = function() 
	{
		this.className = this.className.replace( new RegExp( " withFocus\\b" ), "" );
	}
}

// Applies onfocus and onblur events to all text box elements on the page
function applyPageOnFocusAndOnBlurEvents()
{
	var elements = document.getElementsByTagName( "INPUT" );
	for ( var i = 0; i < elements.length; i++ ) 
	{
		if( elements[ i ].type == "text" || elements[ i ].type == "password" )
		{
			applyElementOnFocusAndOnBlurEvents( elements[ i ] );
		}
	}
	
	elements = document.getElementsByTagName( "TEXTAREA" );
	for ( var i = 0; i < elements.length; i++ ) 
	{
		applyElementOnFocusAndOnBlurEvents( elements[ i ] );
	}
}

// Input Mask to put slashes "/" into a date field: result = dd/mm/yyyy
function validDateMask(obj, e)
{
   // find the last key pressed - for IE or Firefox
    var pressedKey = e.keyCode || e.which;

    // Fixed for IE9.
    if (pressedKey == undefined)
      return true;
   
    if (pressedKey == 8) 
    {
        // if it is a backspace, then return
        event.cancelBubble = true;
        event.returnValue = false;
        return false;
    }
  
    // otherwise check the date format
   var date = obj.value;
    if (/[^\d/]|(\/\/)/g.test(date))  {obj.value=obj.value.replace(/[^\d/]/g,'');obj.value=obj.value.replace(/\/{2}/g,'/'); return; }
    if (/^\d{2}$/.test(date)){obj.value=obj.value+'/'; return; }
    if (/^\d{2}\/\d{2}$/.test(date)){obj.value=obj.value+'/'; return; }
    if (!/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(date)) return;

   obj.focus();
   return false;
}

// Input Mask to put slashes "/" into a short date field: result = mm/yy
function validShortDateMask(obj, e)
{
   // find the last key pressed - for IE or Firefox
    var pressedKey = e.keyCode || e.which;

    // Fixed for IE9.
    if (pressedKey == undefined)
      return true;
   
    if (pressedKey == 8) 
    {
        // if it is a backspace, then return
        event.cancelBubble = true;
        event.returnValue = false;
        return false;
    }
  
    // otherwise check the date format
   date = obj.value;
    if (/[^\d/]|(\/\/)/g.test(date))  {obj.value=obj.value.replace(/[^\d/]/g,'');obj.value=obj.value.replace(/\/{2}/g,'/'); return }
    if (/^\d{2}$/.test(date)){obj.value=obj.value+'/'; return }
    if (/^\d{1,2}\/\d{1,2}$/.test(date)) return

   obj.focus();
   return false;
}

// Showing tooltip message
function showToolTip(controlName)
{
    var oDiv = document.getElementById(controlName);
    oDiv.style.visibility = "visible";
    oDiv.style.display="block";
}

// Hiding tooltip message
function hideToolTip(controlName)
{
    var oDiv = document.getElementById(controlName);
    oDiv.style.visibility = "hidden";
    oDiv.style.display="none";
}

// Set the timers that track a user's inactive time.
function SetupAutoLogout(signoutPage)
{
   window.setTimeout(function() { AutoLogout(signoutPage); }, 1800000); // 30 minute timout.
}

// Auto logout the user after the timer expires.
function AutoLogout(signoutPage)
{
   window.location = signoutPage;
}
