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();} 
}

function formatDate(Date){
    if (Date != null){return (Date.getMonth() + 1) + "/" + Date.getDate() + "/" + Date.getUTCFullYear();}
    else{return "";}
}

function num2money(n_value) {// validate input
    if (isNaN(Number(n_value)))
        return 'ERROR';

    // save the sign
    var b_negative = Boolean(n_value < 0);
    n_value = Math.abs(n_value);

    // round to 1/100 precision, add ending zeroes if needed
    var s_result = String(Math.round(n_value * 1e2) % 1e2 + '00').substring(0, 2);

    // separate all orders
    var b_first = true;
    var s_subresult;
    while (n_value > 1) {
        s_subresult = (n_value >= 1e3 ? '00' : '') + Math.floor(n_value % 1e3);
        s_result = s_subresult.slice(-3) + (b_first ? '.' : ',') + s_result;
        b_first = false;
        n_value = n_value / 1e3;
    }
    // add at least one integer digit
    if (b_first)
        s_result = '0.' + s_result;

    // apply formatting and return
    return b_negative
	? '($' + s_result + ')'
	: '$' + s_result;
}

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){
        if (document.getElementById(Row) != null)
            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="";
            if (document.getElementById(VerifyCkBx).parentNode.disabled)
                document.getElementById(VerifyCkBx).parentNode.disabled = "";}
    }
    else{
        if (document.getElementById(Row) != null)
            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";}
   }
}

function OpenPrint(querystring)
{
    var sURL = '../ReportViewer.aspx?' + querystring;
    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) 
{    
    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 PrintNotes(qs) {OpenPopup("../Common/NotePrintView.aspx?" + qs, "600", "600");}

function DisableButton(button,valgroup) {
    if (valgroup != null){Page_ClientValidate(valgroup);}
    if (valgroup == null || Page_IsValid)
        window.setTimeout("disableButton('" + button.id + "')", 0);
}

function disableButton(buttonID) {document.getElementById(buttonID).disabled = true;}

function ToggleTextEntry(List, Panel, Validator, ItemArray){
    Panel.style.display = "none";
    ValidatorEnable(Validator,false);
    var iSelected = List.options[List.selectedIndex].value;
    for (var i=0; i < ItemArray.length; i++){
        if (iSelected == ItemArray[i]){
            Panel.style.display = "";
            ValidatorEnable(Validator,true);
            break;}
    }
}

