﻿function addEvent(elm, evType, fn, useCapture)  
// addEvent and removeEvent  
// cross-browser event handling for IE5+,  NS6 and Mozilla  
// By Scott Andrew  
{
    if (elm.addEventListener) {
        elm.addEventListener(evType, fn, useCapture);
        return true;
    } else if (elm.attachEvent) {
        var r = elm.attachEvent("on" + evType, fn);
        return r;
    } else {
        alert("Handler could not be removed");
    }
}

function prepareTextBoxes() {
    if (!document.getElementsByTagName) return;
    var oi = 0;
    var thisObj;
    var objs = document.getElementsByTagName("input");
    for (oi = 0; oi < objs.length; oi++) {
        thisObj = objs[oi];
        if (thisObj.getAttribute('type') == 'text' || thisObj.getAttribute('type') == 'password') {
            //            thisObj.className = 'text ' + thisObj.className;
            if (thisObj.className == '') {
                thisObj.className = 'text';
            }
        } else if (thisObj.getAttribute('type') == 'checkbox') {
            thisObj.className = 'checkbox';
        }
    }
}

addEvent(window, "load", prepareTextBoxes);

function allowUploadFileExt(fileName) {
    var isAllowed = false;
    fileName = fileName.toLowerCase();
    var fileExt = fileName.substring(fileName.lastIndexOf('.'));
    switch (fileExt) {
    case '.jpg':
    case '.jpeg':
    case '.gif':
    case '.png':
    case '.bmp':
    case '.pdf':
    case '.wmv':
    case '.avi':
    case '.mpg':
    case '.mpeg':
    case '.mov':
    case '.mp4':
    case '.flv':
    case '.mp3':
    case '.doc':
    case '.txt':
    case '.xls':
    case '.rtf':
    case '.ppt':
    case '.rar':
    case '.zip':
    case '.htm':
    case '.html':
        isAllowed = true;
        break;         
    default:
        isAllowed = false;
        break;
    }
    return isAllowed;
}

function findPosX(obj) {
    var curleft = 0;

    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curleft += obj.offsetLeft;
            obj = obj.offsetParent;
        }
    } else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curtop += obj.offsetTop;
            obj = obj.offsetParent;
        }
    } else if (obj.y)
        curtop += obj.y;
    return curtop;
}

function getViewPortHeight() {
    var viewPortHeight;

    // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight

    if (typeof window.innerWidth != 'undefined') {
        viewPortHeight = window.innerHeight;
    }

        // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

    else if (typeof document.documentElement != 'undefined'
        && typeof document.documentElement.clientWidth !=
            'undefined' && document.documentElement.clientWidth != 0) {
        viewPortHeight = document.documentElement.clientHeight;
    }
        // older versions of IE
    else {
        viewPortHeight = document.getElementsByTagName('body')[0].clientHeight;
    }

    return viewPortHeight;
}

function showUrlPopup(strURL, w, h) {
    var left, top, setting;
    left = (screen.width - w) / 2;
    top = (screen.height - h) / 2;

    setting = "width=" + w + ",height=" + h + ", scrollbars=1, toolbar=0,titlebar=0,"
        + "dependent=0, menubar=0,location=0,status=0, directories=0, resizable=1, "
            + "top=" + top + ", left=" + left;
    var popupWin = window.open(strURL, "popup_id", setting);
    popupWin.focus();
}

function getIndexByValue(strValue, lst) {
    var index = -1;
    if (lst.options.length > 0) {
        var lstLen = lst.options.length;
        strValue = strValue.toLowerCase();
        var opt = null;
        for (i = 0; i < lstLen; i++) {
            currentValue = lst.options[i].value.toLowerCase();
            if (currentValue == strValue) {
                index = i;
                break;
            }
        }
    }

    return index;
}


function drawHtmlChart(totalWidth, currentPercent, unit, textColor, bgColor, chartHeight) {
    var htmlStr = '';
    if (currentPercent > 0) {
        var percentInt = eval(currentPercent);
        var step = eval(totalWidth / 100);
        var currentWidth = Math.round(step * percentInt);

        var paddingTop = chartHeight > 14 ? (chartHeight - 14) / 2 + 'px' : '0px';
        var htmlStr = '<div style="position:absolute;top:0px;left:0px;text-align:center;width:' + totalWidth + 'px;height:' + chartHeight + 'px;color:' + textColor + '"><div style="padding-top:' + paddingTop + '">' + currentPercent + ' ' + unit + '</div></div>';
        htmlStr += '<div style="top:0px;left:0px;width:' + currentWidth + 'px;background-color:' + bgColor + '"><img src="' + jsCurrentSkinPath + 'images/member/trans.gif" width=1 height=' + chartHeight + ' border=0 /></div>';
    }

    return htmlStr;
}

function showCalendar(sender) {
    var selectedDate = sender.value;
    var x = findPosX(sender);
    var y = findPosY(sender) - 85;
    var nextUrl = jsPathPrefix + 'member/common_calendar.aspx?selectedDate=' + selectedDate + '&returnID=' + sender.id;
    createPopup(nextUrl, 'Calendar', 300, 190, y, x);
}

function setSelectedDate(selectedDate, controlID) {
    eval('f.' + controlID).value = selectedDate;
}
