/**
 * Utility functions that can be reused on other pages.
 * Functions should be self-contained (no global variables).
 */

/**
 * Use to enable/disable a button or any other input control on the page
 * id = ID of the control
 * enable = True to enable, false to disable the control
 * setFocus = (optional) True to set focus on the control when enabling
 * Example: onclick="enableControl('HiaBtnContinue', true)"
 * Example: onclick="enableControl('HiaTxtBodyType', true, true)"
 */
function enableControl(id, enable, setFocus)
{
	var cntrl = document.getElementById(id);
	if (cntrl != null)
	{
		cntrl.disabled = !enable;
		if (enable && setFocus != null && setFocus == true) cntrl.focus();
	}
}

/**
 * Executes a print of the current web page which should bring up the
 * print dialog.
 */
function printPage()
{
	window.print();
}

/**
 * Shows the specified message if a checkbox is not checked and returns state of the checkbox.
 * id = Id of the checkbox control
 * msg = Message to display if checkbox not checked
 * Example: onsubmit="return showErrorIfNotChecked('HiaCkxAgree', 'Please check the agree checkbox')"
 */
function showErrorIfNotChecked(id, msg)
{
	var cntrl = document.getElementById(id);
	if (cntrl != null)
	{
		if (!cntrl.checked) alert(msg);
	}
	return cntrl.checked;
}

function navigateToURL(href)
{
	window.location.href = href;
}

function popUp(URL, w, h)
{
	day = new Date();
	id = day.getTime();
	l = (screen.width) ? (screen.width-w)/2 : 0;
	t = (screen.height) ? (screen.height-h)/2 : 0;
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width='+w+',height='+h+',left='+l+',top='+t);");
}

/*
    Append to the onKeyDown function of an input control to force
    numeric input only.
*/

function ForceNumericInput(This, AllowDot, AllowMinus)
{
	if(arguments.length == 1)
	{
    	var s = This.value;
    	// if "-" exists then it better be the 1st character
    	var i = s.lastIndexOf("-");
    	if(i == -1)
        	return;
    	if(i != 0)
       		This.value = s.substring(0,i)+s.substring(i+1);
       	return;
    }

    var code = event.keyCode;
    switch(code)
    {
        case 8:     // backspace
        case 9:     // tab
        case 35:    // end
        case 36:    // home
        case 37:    // left arrow
        case 39:    // right arrow
        case 46:    // delete
        case 188:   // comma
            event.returnValue=true;
            return;
    }

    if(code == 189)     // minus sign
    {
    	if(AllowMinus == false)
    	{
            event.returnValue=false;
            return;
        }

        // wait until the element has been updated to see if the minus is in the right spot
        var s = "ForceNumericInput(document.getElementById('"+This.id+"'))";
        setTimeout(s, 250);
        return;
    }

    if(AllowDot && code == 190)
    {
        if(This.value.indexOf(".") >= 0)
        {
        	// don't allow more than one dot
            event.returnValue=false;
            return;
        }
        event.returnValue=true;
        return;
    }

    // allow character of between 0 and 9
    if ((code >= 48 && code <= 57) || (code >= 96 && code <= 105))
    {
        event.returnValue=true;
        return;
    }
 
    event.returnValue=false;
}
