﻿var lastKey;

function justNumbers(e, strID)
{
    var keynum;
    var keychar;
    var numcheck;
    var strText;
    
    if (window.event) // IE
    {
        keynum = e.keyCode;
    }
    else if (e.which) // Netscape/Firefox/Opera
    {
        keynum = e.which;
    }

    if ((keynum == 8) || (keynum == 9) || (keynum == 46) || (keynum == 16) || (keynum == 13)) { // Let the backspace and tab through.
        return true;
    }
    else {
        keychar = String.fromCharCode(keynum);
        lastKey = keychar;
        numcheck = /\d/;
        return numcheck.test(keychar);
    }
}

function justNumbersAndDot(e, strID)
{
    var keynum;
    var keychar;
    var numcheck;
    var strText;
    
    if (window.event) // IE
    {
        keynum = e.keyCode;
    }
    else if (e.which) // Netscape/Firefox/Opera
    {
        keynum = e.which;
    }
    
    if ((keynum == 189) || (keynum == 8) || (keynum == 9) || (keynum == 190) || (keynum == 110) || ((keynum >= 35) && (keynum <= 40)) || (keynum == 46) || (keynum == 16) || (keynum == 13) || ((keynum >= 96) && (keynum <= 105))) { // Let the backspace and tab through.
        return true;
    }
    else {
        keychar = String.fromCharCode(keynum);
        //alert(keynum);
        numcheck = /[\d\.\-]/;
        return numcheck.test(keychar);
    }
}

function justTime(e)
{
    var keynum;
    var keychar;
    var numcheck;
    var strText;
    
    if (window.event) // IE
    {
        keynum = e.keyCode;
    }
    else if (e.which) // Netscape/Firefox/Opera
    {
        keynum = e.which;
    }
    
    keychar = String.fromCharCode(keynum);
    numcheck = /[\d\.\:\-]/;
    return numcheck.test(keychar);
}

function formatPhone(e, strID) {
    var numcheck;
    var strText;
    
    numcheck = /\d/;
    //alert(lastKey);
    
    if (numcheck.test(lastKey)) {
        strText = new String(document.getElementById(strID).value); 
        strText = strText.replace("(", "");
        strText = strText.replace(")", "");
        strText = strText.replace(" ", "");
        strText = strText.replace("-", "");

        if (strText.length < 1) {
            // Do nothing
        } 
        else if (strText.length < 3) {
            document.getElementById(strID).value = '(' + strText;
        }
        else if (strText.length == 3) {
            document.getElementById(strID).value = '(' + strText + ') ';
        }
        else if (strText.length < 6) {
            document.getElementById(strID).value = '(' + strText.substr(0, 3) + ') ' + strText.substr(3);
        }
        else if (strText.length == 6) {
            document.getElementById(strID).value = '(' + strText.substr(0, 3) + ') ' + strText.substr(3) + '-';
        }
        else {
            document.getElementById(strID).value = '(' + strText.substr(0, 3) + ') ' + strText.substr(3, 3) + '-' + strText.substr(6, 4);
        }
    }
    
    lastKey = '';
}

function formatSIN(e, strID) {
    var numcheck;
    var strText;
    
    numcheck = /\d/;
    
    if (numcheck.test(lastKey)) {
        strText = new String(document.getElementById(strID).value); 
        strText = strText.replace(/ /g, "");

        if (strText.length < 3) {
            document.getElementById(strID).value = strText;
        }
        else if (strText.length == 3) {
            document.getElementById(strID).value = strText + ' ';
        }
        else if (strText.length < 6) {
            document.getElementById(strID).value = strText.substr(0, 3) + ' ' + strText.substr(3);
        }
        else if (strText.length == 6) {
            document.getElementById(strID).value = strText.substr(0, 3) + ' ' + strText.substr(3) + ' ';
        }
        else {
            document.getElementById(strID).value = strText.substr(0, 3) + ' ' + strText.substr(3, 3) + ' ' + strText.substr(6);
        }
    }
    
    lastKey = '';
}

function checkLikeRadio(strClickedID, strOtherID) {
    if (document.getElementById(strClickedID).checked) {
        document.getElementById(strOtherID).checked = false;
    }
}

function checksLikeRadio(strClickedID, strOtherID, strOtherID2) {
    if (document.getElementById(strClickedID).checked) {
        document.getElementById(strOtherID).checked = false;
        document.getElementById(strOtherID2).checked = false;
    }
}

function checkTextFill(strClickedID, strTextID, strText) {
    if (document.getElementById(strClickedID).checked) {
        document.getElementById(strTextID).value = strText;
    }
}

function CheckLinkedTextBoxClicked(strClickedID, strTextID) {
    if (document.getElementById(strClickedID).checked) {
        document.getElementById(strTextID).disabled = false;
    }
    else {
        document.getElementById(strTextID).value = '';
        document.getElementById(strTextID).disabled = true;
    }
}

function GetConfirmation(strConfirm, strHiddenID) {
    if (confirm(strConfirm)) {
        document.getElementById(strHiddenID).value = 'yes';
        return true;
    }
    else {
        document.getElementById(strHiddenID).value = 'no';
        return false;
    }    
}

function setDirty(blnValue) {
    blnDirty = blnValue;
}

function openCalendar(strTextId, strNextMonth, strTop, strLeft, strPostBack) {
    window.open('CalendarPopup.aspx?DestField=' + strTextId + '&NextMonth=' + strNextMonth + '&VisibleDate=' + document.getElementById(strTextId).value + '&PostBack=' + strPostBack, 'winCal', 'toolbar=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=270,height=165,top=' + strTop + ',left=' + strLeft);
}

function generateControlName(intRow, strName) {
    if (intRow < 10) {
        return 'dg_ctl0' + intRow + '_' + strName;
    }
    else {
        return 'dg_ctl' + intRow + '_' + strName;
    }
}
        
function convertMinutes(intRow, strName) {
    convertMinutesWithID(generateControlName(intRow, strName));
}

function convertMinutesWithID(strID) {
    var strValue = new String(document.getElementById(strID).value);
    
    if (strValue.indexOf(':') >= 0) {
        var intMinutes = strValue.substr(strValue.indexOf(':') + 1);
        intMinutes = intMinutes / 60;
        var intHours = strValue.substr(0, strValue.indexOf(':'));
        document.getElementById(strID).value = Number(intHours) + Number(intMinutes);
    }
}


