
function OpenWindow(URL, winHeight, winWidth, scrollbars, resizeable, menus, toolbars, status, e) {

    if (toolbars == null) toolbars = menus;
    if (status == null) status = 1;
    var widthOverhead = 10; // don’t change these values, they are used to 
    var heightOverhead = 38; // compensate for the browser’s border and title bar
    var winLeft;
    var winTop;
    if (e == null) {
        if (winWidth > screen.availWidth) winWidth = -2;
        if (winHeight > screen.availHeight) winHeight = -2;
        switch (winWidth) {
            case -2:  //this means set to max
                winWidth = screen.availWidth - 25;
                winLeft = screen.width - screen.availWidth;
                break;
            case -1:
                winLeft = -1;
                break;
            default:
                winLeft = (screen.width - winWidth) / 2;
                break;
        }
        switch (winHeight) {
            case -2:
                winHeight = screen.availHeight - 120;
                winTop = 0;
                break;
            case -1:
                winTop = -1;
                break;
            default:
                winTop = (screen.height - winHeight) / 2;
                if (menus == 1)
                    winTop = winTop - 45;
                if (toolbars == 1)
                    winTop = winTop - 49;
                break;
        }
    } else {
        winLeft = e.screenX;
        winTop = e.screenY;
        if ((winLeft + winWidth) > (screen.width - 50))
            winLeft = e.screenX - winWidth - widthOverhead;
        if ((winTop + winHeight) > (screen.height - 50))
            winTop = e.screenY - winHeight - heightOverhead;
    }
    var parms = 'menubar=' + menus + ',location=' + toolbars + ',toolbar=' + toolbars + ',scrollbars=' + scrollbars + ',resizable=' + resizeable + ',Height=' + winHeight + ',Width=' + winWidth + ',left=' + winLeft + ',top=' + winTop + ',status=' + status;
    var win = window.open('' + URL + '', '', parms);
}
function Trim(str) {
    var newStr = RTrim(LTrim(str));
    while (newStr.indexOf("  ") > 0)
        newStr = newStr.replace("  ", " ");
    return newStr;
}
function LTrim(str) {
    var i = 0;
    while (str.charAt(i) == " ") i++;
    return str.substring(i, str.length);
}
function RTrim(str) {
    var j = str.length;
    while (str.charAt(j) == ' ') j--;
    return str.substring(0, j + 1);
}
function EndsWith(str, val) {
    var strLen = str.length;
    var valLen = val.length;
    if (str.lastIndexOf(val) == (strLen - valLen))
        return true;
    return false;
}

//couldn't find a "Standard Time" property for the date object, so convert it myself from Military time
function DisplayLocalDateTime() {
    var today = new Date();
    var standardTime = new Date();

    var ampm = "AM";
    var tempDate = new Date();
    if (today.getHours() >= 12) {
        ampm = "PM";
        if (today.getHours() >= 13)
            standardTime.setHours(today.getHours() - 12);
    } else {
        standardTime = today;
    }
    //seperate the Time and Timezone, and inject ampm
    var tmp = standardTime.toTimeString().split(' ');
    return tmp[0] + ' ' + ampm + ' ' + tmp[1];
}
function AutoTab(input, len, e) {
    var keyCode = e.keyCode;
    var filter = [0, 8, 9, 16, 17, 18, 37, 38, 39, 40, 46];
    if (input.value.length >= len && !containsElement(filter, keyCode)) {
        input.value = input.value.slice(0, len);
        var input = input.form[(getIndex(input) + 1) % input.form.length]
        if (input != null) {
            input.focus();
            if (input.type == 'text')
                input.select();
        }
    }
    function containsElement(arr, ele) {
        var found = false, index = 0;
        while (!found && index < arr.length)
            if (arr[index] == ele)
            found = true;
        else
            index++;
        return found;
    }
    function getIndex(input) {
        var index = -1, i = 0, found = false;
        while (i < input.form.length && index == -1)
            if (input.form[i] == input) index = i;
        else i++;
        return index;
    }
    return true;
}

function ClearListBox(lbName) {
    var listBox = document.getElementById(lbName);
    var count = listBox.options.length;
    for (var i = 0; i < count; i++) {
        listBox.options[i].selected = false;
    }
}
function OpenReportWindow(url, guid, height, width) {
    if (height == null) height = 550;
    if (width == null) width = 740;
    if (guid != null) {
        var myCookie = "Report=" + guid;
        if (document.cookie.indexOf(myCookie) < 0) {
            OpenWindow(url, height, width, 1, 1, 1, 1);
            document.cookie = myCookie;
        }
    } else {
        OpenWindow(url, height, width, 1, 1, 1, 1);
    }
}
//function NotifyReportScheduled(msg, guid){
//    if (guid != null){
//		var myCookie = "notify=" + guid;
//		if (document.cookie.indexOf(myCookie)<0){
//			alert(msg);
//			document.cookie = myCookie;
//		}
//		alert(document.cookie);
//	}else{
//	    alert(msg);
//	}
//} 
function FindOption(tbName, lbName, valOrText) {
    var textBox = document.getElementById(tbName);
    var listBox = document.getElementById(lbName);
    if (valOrText == null)
        valOrText = 'val';
    var textBoxValue = textBox.value.toLowerCase();
    var listBoxLen = listBox.length;
    var currentOption;
    var currentValue;
    if (textBoxValue.length > 0) {
        for (var i = 0; i < listBoxLen; i++) {
            currentOption = listBox.options[i];
            currentValue = (valOrText == 'val') ? currentOption.value : currentOption.text;
            if (currentValue.toLowerCase().indexOf(textBoxValue) == 0) {
                listBox.selectedIndex = currentOption.index; // currentOption.selected = true;
                break;
            }
        }
    }
}

function FindOptions(tbName, lbName, valOrText, regExpODs) {
    var textBox = document.getElementById(tbName);
    var listBox = document.getElementById(lbName);
    if (valOrText == null)
        valOrText = 'val';
    var textBoxText = textBox.value.toLowerCase();
    if (textBoxText.length > 0) {
        var listBoxLen = listBox.length;
        var currentOption;
        var currentValue;
        if (regExpODs != null)
            textBoxText = textBoxText.replace(regExpODs, ",")
        var values = textBoxText.split(',');
        var findCount = values.length;

        //clear all selected
        listBox.selectedIndex = -1;

        //first find the last value and set the index
        var value = Trim(values[findCount - 1]);
        var lastSelectedOption = null;
        if (value != "") {
            for (var i = 0; i < listBoxLen; i++) {
                currentOption = listBox.options[i];
                currentValue = (valOrText == 'val') ? currentOption.value : currentOption.text;
                var match = (currentValue.toLowerCase().indexOf(value) == 0);
                if (match) {
                    listBox.selectedIndex = currentOption.index; // currentOption.selected = true;
                    lastSelectedOption = currentOption;
                    break;
                }
            }
        }

        //then find all the previous ones and select them
        if (findCount > 1) {
            for (var b = 0; b < findCount - 1; b++) {
                value = Trim(values[b]);
                if (value != "") {
                    for (var i = 0; i < listBoxLen; i++) {
                        currentOption = listBox.options[i];
                        currentValue = (valOrText == 'val') ? currentOption.value : currentOption.text;
                        var match = (currentValue.toLowerCase() == value);
                        if (match) {
                            currentOption.selected = true;
                            break;
                        }
                    }
                }
            }
            if (lastSelectedOption != null)
                lastSelectedOption.selected = true;
        }
    }
}
function ChangeCount(b) {
    var count = b.options.length;
    var j = 0;
    for (var i = 0; i < count; i++) {
        var option = b.options[i];
        if (option.selected) {
            if (option.value != 0) {
                j++;
            } else {
                j = count - 1;
                break;
            }
        }
    }
    document.getElementById(b.id + '_selectedCount').innerHTML = j;
}

function CaptureEnter(btnName, e) {
    if (e.keyCode == 13) {
        var btn = document.getElementById(btnName);
        e.returnValue = false;
        e.cancelBubble = true;
        btn.click();
        return false;
    }
}
function OpenSearchWindow(url) {
    OpenWindow(url, 330, 640, 0, 0, 0);
}
function OpenOptionalFilterFavorite(sFieldName, pFieldName, ofid) {
    var pField = document.getElementById(pFieldName);
    if (Trim(pField.value) != '') {
        var sId = 0;
        if (sField = document.getElementById(sFieldName))
            sId = sField.options[sField.selectedIndex].value;
        OpenFavorite(sId, '', 'OF', pFieldName, ofid);
    } else {
        alert("You can not save an empty favorite!");
        pField.focus();
    }
}
function OpenFavorite(selectedId, value, type, field, ofid) {
    var url = AppPath + "SecureSite/Reports/Filters/AddUpdateFavorite.aspx?selectedid=" + selectedId +
		"&value=" + value +
		"&type=" + type +
		"&field=" + field +
		"&ofid=" + ofid;
    OpenWindow(url, 135, 400, 0, 0, 0);
}
function LoadFavoriteValues(favList, field) {
    var selectedId = favList.options[favList.selectedIndex].value;
    if (selectedId != 0) {
        var hf = document.getElementById(field + "_" + selectedId);
        document.getElementById(field).value = hf.value.split(',').join(', ');
    }
}
function ValidateMultipleDelete(boxName, itemName) {
    var validMsg = 'Are you sure you want to delete the selected \'' + itemName + '(s)?\'';
    var invalidMsg = 'Must select at least one \'' + itemName + '\'!'
    var elements = document.forms[0].elements;
    var boxNameLen = boxName.length;
    for (var i = 0; i < elements.length; i++) {
        var element = elements[i];
        if (element.type == 'checkbox') {
            var j = element.id.length - boxNameLen
            if (element.id.indexOf(boxName) == j) {
                if (element.checked == true) {
                    return confirm(validMsg)
                }
            }
        }
    }
    alert(invalidMsg);
    return false;
}

function IsCheckBoxListChecked(name) {
    var retVal = false;
    var elements = document.forms[0].elements;
    for (var i = 0; i < elements.length; i++) {
        var cur = elements[i];
        if (cur.type == 'checkbox') {
            if (cur.id.indexOf(name) > -1)
                if (cur.checked == true) {
                retVal = true;
                break;
            }
        }
    }
    return retVal;
}

function GetXmlHttpObject() {
    try {    // Firefox, Opera 8.0+, Safari
        return new XMLHttpRequest();
    } catch (e) {    // Internet Explorer    
        try {
            return new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                return new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                alert("Your browser does not support AJAX!");
                return null;
            }
        }
    }
}

function SetVisibility(obj, visible) {
    if (!visible) {
        //obj.style.visibility = 'hidden';
        obj.style.display = 'none';
    } else {
        //obj.style.visibility = 'visible';
        obj.style.display = 'inline';
    }
}

function FormatNumber(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

function findControlPos(obj) {
    var curleftPos = 0;
    var curtopPos = 0;
    while (obj) {
        curleftPos += obj.offsetLeft;
        curtopPos += obj.offsetTop;
        obj = obj.offsetParent
    }
    var retval = new Array();
    retval[0] = curleftPos.toString() + 'px';
    retval[1] = curtopPos.toString() + 'px';
    return retval;
}

function TextAreaMax(c, len) {
    if (c.value.length > len) {
        alert('This field can not be greater than ' + len + ' characters');
        c.value = c.value.substr(0, len);
    }
}

var modalClientid;
function mpSetFocus(txtClientId) {
    modalClientid = txtClientId;
    setTimeout("mpFocus()", 400);
}

function mpFocus() {
    $get(modalClientid).focus();
}

function mpClickOK(sender, e) {
    __doPostBack(sender, e);
}

function GetItemQualifierFromControl(c) {
    var id = c.id.toString();
    return id.substring(0, id.lastIndexOf('_') + 1);
}