﻿
/************************************************************************
 *** isInteger
 ************************************************************************/

function isInteger(s) {
    return (s.toString().search(/^-?[0-9]+$/) == 0);
}


/*********************************************************************************************
****  Return X & Y
*********************************************************************************************/

function ReturnY(o) {
    var y = 0;

    y += o.offsetTop;
    y += o.offsetHeight;
    while (o.offsetParent != null) {
        o = o.offsetParent;
        y += o.offsetTop;
    }

    return y;
}

function ReturnX(o) {
    var x = 0;

    x += o.offsetLeft;
    while (o.offsetParent != null) {
        o = o.offsetParent;
        x += o.offsetLeft;
    }

    return x - 1;
}

/************************************************************************
 *** Do Nothing
 ************************************************************************/

function DoNothing() {

}

/************************************************************************
 *** Format Phone Number
 ************************************************************************/

function FormatPhoneNumber(pn) {
    if (pn == '') {
        return '';
    }
    var r = '';
    r += pn.substring(0, 3) + '.'
    r += pn.substring(3, 6) + '.'
    r += pn.substring(6, 10);
    return r;
}

/************************************************************************
 *** Strip Phone Number
 ************************************************************************/

function StripPhoneNumber(pn) {
    if (pn == '') {
        return '';
    }
    var r = '';
    for (var i = 0; i < pn.length; i++) {
        switch (pn.substring(i, i + 1)) {
            case '0':
                r += '0';
                break;
            case '1':
                r += '1';
                break;
            case '2':
                r += '2';
                break;
            case '3':
                r += '3';
                break;
            case '4':
                r += '4';
                break;
            case '5':
                r += '5';
                break;
            case '6':
                r += '6';
                break;
            case '7':
                r += '7';
                break;
            case '8':
                r += '8';
                break;
            case '9':
                r += '9';
                break;
        }
    }
    
    
    return r;
}

/************************************************************************
 *** Set One Moment
 ************************************************************************/


function SetOneMoment(divName,PathToRoot,Margin) {
    var r = '';
    var d = document.getElementById(divName);
    var id = d.id + '_Moment';
    var TopMargin = 0;
    var Path = ''

    if (Margin != null) {
        TopMargin = Margin
    }
    if (PathToRoot != null) {
        Path = PathToRoot
    }

    if (TopMargin == 0) {
//        if (d.offsetHeight < 58) {
//            TopMargin = d.offsetParent.offsetHeight;
//        } else {
//            TopMargin = d.offsetHeight;
//        }
        TopMargin = (TopMargin - 58) / 2;
    }
    
    
    
    if (id == null || id == '') {
        id = 'dOneMoment'
    }

    r += '<div id="' + id +'" style="width:100%;text-align:center;margin-top:' + TopMargin + 'px;">';
    r += '<div class="OneMoment">';
    r += '<img src="' + Path + 'images/one-moment.gif" class="OneMoment" alt="One Moment" />';
    r += '<span id="sMessage">Loading . . .</span>';
    r += '</div>';
    r += '</div>';

    d.innerHTML = r;
}



/*****************************************************************************************
 *** Cookies
 *****************************************************************************************/

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}


/*****************************************************************************************
 *** SelectItem
 *****************************************************************************************/

function SelectItem(o, id) {
    for (var i = 0; i < o.length; i++) {
        if (o[i].value == id) {
            o.selectedIndex = i;
            return;
        }
    }
}

/*****************************************************************************************
*** Selected Text
*****************************************************************************************/

function SelectedText(o) {
    return o[o.selectedIndex].text;
}

/*****************************************************************************************
*** Selected Value
*****************************************************************************************/

function SelectedValue(o) {
    return o[o.selectedIndex].value;
}



/*****************************************************************************************
 *** Format Currency
 *****************************************************************************************/

function FormatCurrency(str, decimals) {
    
    if (decimals == null) {
        decimals = 2;
    }

    // Convert to String    
    str = str.toString();

    // Ensure that it is a number
    if (str == 'NaN') {
        return '';
    }

    // Get rid of any commas
    str = str.replace(',', '');

    // round to the proper decimal places    
    str = parseFloat(str).toFixed(decimals);

    // Split the whole and decimal numbers into array
    var cols = str.split('.');
    var num = '';
    var count = 0;

    // Remove any negatives
    cols[0] = cols[0].replace('-', '');

    // Place a comman every three digits of the whole number
    for (var i = cols[0].length; i >= 0; i--) {
        if (IsNumericStrict(cols[0].charAt(i))) {
            num = cols[0].charAt(i) + num;
            count += 1;
            if (count == 3 && i > 0) {
                num = ',' + num;
                count = 0;
            }
        }
    }

    // Add decimals back to string
    if (cols[1] != null) {
        num += '.' + cols[1];
    }

    // check and see if orginal number was negative    
    if (str.indexOf('-') >= 0) {
        num = '-' + num;
    }

    return num
}

/*****************************************************************************************
 *** IsNumeric Strict
 *****************************************************************************************/

function IsNumericStrict(s) {
    var IsNumber = false;
    var Char;

    switch (s) {
        case '0':
            IsNumber = true;
            break;
        case '1':
            IsNumber = true;
            break;
        case '2':
            IsNumber = true;
            break;
        case '3':
            IsNumber = true;
            break;
        case '4':
            IsNumber = true;
            break;
        case '5':
            IsNumber = true;
            break;
        case '6':
            IsNumber = true;
            break;
        case '7':
            IsNumber = true;
            break;
        case '8':
            IsNumber = true;
            break;
        case '9':
            IsNumber = true;
            break;
    }

    return IsNumber;
}
