String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/, "");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/, "");
}


//javascript auto tab to next field at phone # field
function PhoneKeyUp(CurrentField, MaxLength, NextFieldID) {
    // default code to 9 so we don't jump on unsupported browsers
    var keycode = 9;
    if (window.event) {
        keycode = window.event.keyCode;
    }

    //if tabbing, or shifting or field is not full, retain current field focus
    if (keycode == 9 || keycode == 16 || CurrentField.value.length < MaxLength) {
        return true;
    }
    else {
        //otherwise jump to next field
        document.getElementById(NextFieldID).focus();
    } //end if
} //end function

function formatDate(Date) {
    if (Date != null) {
        return (Date.getMonth() + 1) + "/" + Date.getDate() + "/" + Date.getUTCFullYear();
    }
    else {
        return "";
    }
}

function msgForDatePick(noteID) {
    document.getElementById(noteID).innerHTML = "<br />Please use date picker for date field";
}

function ShowProgress(Button, ReplaceText) {
    if (Button.getAttribute("clicked") == null) {
        Button.className = "InputProcessing";
        if (ReplaceText != null) { Button.value = ReplaceText; }
        Button.setAttribute("clicked", true);
        return true;
    }
    else {
        return false;
    }
}

function DefaultButton(keyEvent, btn) {
    if (document.all) {
        if (keyEvent.keyCode && keyEvent.keyCode == 13) {
            keyEvent.returnValue = false;
            keyEvent.cancel = true;
            btn.click();
        } //if 
    } //if
    else if (document.getElementById) {
        if ((keyEvent.which && keyEvent.which == 13) || (keyEvent.charCode && keyEvent.charCode == 13)) {
            keyEvent.returnValue = false;
            keyEvent.cancel = true;
            btn.click();
        } //if
    } //else if
    else if (document.layers) {
        if (keyEvent.which == 13) {
            keyEvent.returnValue = false;
            keyEvent.cancel = true;
            btn.click();
        } //if
    } //else if
} //function


function toggleRow(Show, Row, RFV) {
    //Enable/disable validator
    if (typeof (Page_Validators) != "undefined" && Page_Validators != null && document.getElementById(RFV) != null) {
        ValidatorEnable(document.getElementById(RFV), Show);
    }

    if (Show) {
        //Show Row
        document.getElementById(Row).style.display = "";
    }
    else {
        //Hide Row
        document.getElementById(Row).style.display = "none";
    }
}

function SelectRow(CheckBox, Row, Textbox, Validator, VerifyCkBx) {
    if (CheckBox.checked) {
        document.getElementById(Row).style.fontWeight = "bold";

        if (document.getElementById(Textbox) != null) {
            document.getElementById(Textbox).disabled = "";
        }

        if (document.getElementById(Validator) != null)
            ValidatorEnable(document.getElementById(Validator), true);

        if (document.getElementById(VerifyCkBx) != null) {
            document.getElementById(VerifyCkBx).disabled = "";
            document.getElementById(VerifyCkBx).parentNode.attributes["disabled"].value = "";
        }
    }
    else {
        document.getElementById(Row).style.fontWeight = "normal";

        if (document.getElementById(Textbox) != null) {
            document.getElementById(Textbox).disabled = "disabled";
        }

        if (document.getElementById(Validator) != null)
            ValidatorEnable(document.getElementById(Validator), false);

        if (document.getElementById(VerifyCkBx) != null) {
            document.getElementById(VerifyCkBx).disabled = "disabled";
            document.getElementById(VerifyCkBx).parentNode.attributes["disabled"].value = "";

        }
    }
}

function OpenPrint(reportName, reportParms, useViewer) {
    var sURL = '../ReportViewer.aspx';
    if (reportName != null) {
        sURL += '?reportName=' + reportName;
    }
    if (reportParms != null) {
        sURL += '&reportParms=' + reportParms;
    }
    if (useViewer != null) {
        sURL += '&useViewer=' + useViewer;
    }

    var sName = '_blank';
    var iHeight = document.documentElement.clientHeight;
    var iWidth = document.documentElement.clientWidth;

    var sOptions = 'modal=no,width=' + iWidth + ',height=' + iHeight + ',resizable=yes,modal=yes,status=yes';
    window.open(sURL, sName, sOptions);
}


function OpenPopup(URL, Height, Width) {
    //    if (window.showModalDialog) {
    //        window.showModalDialog(URL, "Modal",
    //            "dialogWidth:" + Width + "px;dialogHeight:" + Height + "px");
    //    } 
    //    else {
    window.open(URL, "Modal",
            'height=' + Height + ',width=' + Width + ',toolbar=no,directories=no,status=no,' +
            'menubar=no,scrollbars=yes,resizable=no,modal=yes');
    //    }
}

function ToggleModalPopUp(BehaviorID, Show) {
    var mpBehavior = $find(BehaviorID);
    if (Show == true)
        mpBehavior.show();
    else
        mpBehavior.hide();
}


//selects a specified value in a drop down list
//returns true if the value was found
//returns false if the value was not found
function SelectListItem(List, StringToSelect) {
    var bSelected = false;
    if (List != null) {

        for (var i = 0; i < List.options.length; i++) {
            var compareString = StringToSelect;

            if (isNaN(parseInt(StringToSelect)))
                compareString = StringToSelect.toUpperCase();

            if (List.options[i].value.toUpperCase() == compareString) {
                List.selectedIndex = List.options[i].index;
                bSelected = true;
                break;
            }
        }
    }
    return bSelected;
}

//Updates a running total of hours for a gridview
//Used by Core Knowledge Area, CDA, and Topic gridviews on course and consulting event entry
function UpdateRunningTotal(TotalLabel, GridView, Decimals) {
    if (TotalLabel != null && GridView != null) {
        var arrInput = GridView.getElementsByTagName("input")
        var dblTotal = 0;
        var iShowDecimals = 0;

        if (Decimals != null)
            iShowDecimals = Decimals;

        try {
            for (var i = 0; i < arrInput.length; i++) {
                if (!arrInput[i].disabled && arrInput[i].attributes["sum"] != null) {
                    if (arrInput[i].value.replace(/^\s+|\s+$/g, "") != "" && !isNaN(arrInput[i].value))
                        dblTotal += parseFloat(arrInput[i].value);
                }
            }
        }
        catch (err) {
            dblTotal = 0;
        }

        TotalLabel.innerHTML = dblTotal.toFixed(iShowDecimals);
    }
}

function InitNoteEntry(ID, Category, Activity, RefNbr, StatusText, Cmd) {
    OpenPopup("../Common/NoteEntry.aspx?id=" + ID + "&c=" + Category + "&a=" + Activity + "&ref=" + RefNbr + "&cmd=" + Cmd + "&s=" + StatusText, "350", "600");
}

function PrintNotes(Type, Category, RefNbr) {
    OpenPopup("../Common/NotePrintView.aspx?t=" + Type + "&c=" + Category + "&ref=" + RefNbr, "600", "600");

}
