var lastFocusedControlId = "";

// Ajax: Field Focus Helper
// Application Init
function appInit() 
{
    if (typeof(window.addEventListener) !== "undefined") {
        window.addEventListener("focus", focusHandler, true);
    }
    Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(pageLoadingHandler);
    Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoadedHandler);
}

// Ajax: Field Focus Helper
// Focus Control Set
function focusControl(targetControl) 
{
    if (Sys.Browser.agent === Sys.Browser.InternetExplorer) {
        var focusTarget = targetControl;
        if (focusTarget && (typeof(focusTarget.contentEditable) !== "undefined")) {
            oldContentEditableSetting = focusTarget.contentEditable;
            focusTarget.contentEditable = false;
        }
        else {
            focusTarget = null;
        }
        doSelect(targetControl);
        if (focusTarget) {
            focusTarget.contentEditable = oldContentEditableSetting;
        }
    } else {
        doSelect(targetControl);
    }
}

// Ajax: Field Focus Helper
// Do Select on IE and FF (different approach)
function doSelect(who) 
{
   try { who.focus(); } 
   catch(e) {}

   who.select();
}

// Ajax: Field Focus Helper
// Focus Handler
function focusHandler(e) 
{
    document.activeElement = e.originalTarget;
}

// Ajax: Field Focus Helper
// Page Loading Handler
function pageLoadingHandler(sender, args) 
{
    lastFocusedControlId = typeof(document.activeElement) === "undefined" 
        ? "" : document.activeElement.id;
}

// Ajax: Field Focus Helper
// Page Loaded Handler
function pageLoadedHandler(sender, args) 
{
    if (typeof(lastFocusedControlId) !== "undefined" && lastFocusedControlId != "") {
        var newFocused = $get(lastFocusedControlId);
        if (newFocused) {
            focusControl(newFocused);
        }
    }
}

Sys.Application.add_init(appInit);

// Clear background style
function clearValColor(e)
{
    e.style.backgroundColor = '';
}

// Confirm Regional Revenue if is more than 1 Billion
function confirmRevenue()
{
    var e = document.getElementById('ctl00_cphMain_MyRegionalForm_txtRRWP2K7_Total');

    if (e == null) 
        var e = document.getElementById('ctl00_cphMain_MyNationalForm_txtContractRevenue2K7');

    if (e == null) return true;

    if (toDecimal(e) != '') 
    {
        e.value = e.value.replace('$', '');
        e.value = e.value.replace(',', '');
        
        if (e.value >= 1000) {
            return confirm('You have said your firm is greater than $1 Billion dollars worth of Specialty Contractor Revenue (click OK to continue or Cancel to correct).');
        }
    }
}

// Convert Value to Decimal Format
function toDecimal(e)
{
    e.value = e.value.replace('$', '');
    e.value = e.value.replace(',', '');
    e.value = (isNaN(parseFloat(e.value))) ? '' : formatNumber(parseFloat(e.value),2,',','.','$','','(',')');
}

// Convert Value to Percent Format
function toPercent(e)
{
    e.value = e.value.replace('%', '');
    e.value = (isNaN(parseFloat(e.value))) ? '' : formatNumber(parseFloat(e.value),2,'','.','','%','','');
}

// Number Formatter
// num = Number value to format (int, float or double)
// dec = Decimal places (int)
// tho = Thousands separator (string)
// pnt = Decimal point (string)
// cuL = Left currency symbol (string)
// cuR = Right currency symbol (string)
// ngL = Left negative symbol (string)
// ngR = Right negative symbol (string)
function formatNumber(nbr, dec, tho, pnt, cuL, cuR, ngL, ngR) 
{
    var x = Math.round(nbr * Math.pow(10, dec));
    if (x >= 0) ngL=ngR='';

    var y = ('' + Math.abs(x)).split('');
    var z = y.length - dec;

    if (z<0) z--;

    for(var i = z; i < 0; i++) y.unshift('0');

    y.splice(z, 0, pnt);

    if (y[0] == pnt) y.unshift('0');

    while (z > 3) 
    {
        z -= 3;
        y.splice(z, 0, tho);
    }

    var r = cuL + ngL + y.join('') + ngR + cuR;

    return r;
}

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();