/**
 * 控件类的相关函数。
 * @create   2004-10-14 source0
 * @author   source0 source0@hotmail.com
 * @copyright 版权所有（C） 2004  source0
 *                这一程序是自由软件，你可以遵照自由软件基金会出版的GNU通用
 *            公共许可证条款来修改和重新发布这一程序。或者用许可证的第二版，
 *            或者（根据你的选择）用任何更新的版本。
 *                发布这一程序的目的是希望它有用，但没有任何担保。甚至没有
 *            适合特定目的的隐含的担保。更详细的情况请参阅GNU通用公共许可证。
 *                你应该已经和程序一起收到一份GNU通用公共许可证的副本。如果
 *            还没有，写信给：
 *                The Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
 *                MA02139, USA
 */

var DEFAULT_BORDER_SIZE = 9;
var DEFAULT_BORDER_IMAGE_PATH = "common/image/border";


/**
 * 得到指定单选按钮的值。
 * @param name 单选按钮的名字。
 * @return 如果指定的单选按钮存在并且有被选中的项目，返回该项目的索引号（从0开始），否则返回 -1。
 */
function getRadioCtlValue(name){
    for ( var key in document.forms ){
        var ctl = eval("document.forms[key]." + name);
        if ( ctl ){
            for ( var index = 0; index < ctl.length; index++ ){
                if ( ctl[index].checked == true ){
                    return index;
                }
            }
            return -1;
        }
    }
    return -1;
}
/**
 * 设置指定单选按钮的选中项目。
 * @param name 单选按钮的名字。
 * @param index 被选中的单选项目的索引号。
 */
function setRadioCtlValue(name, index){
    for ( var key in document.forms ){
        var ctl = eval("document.forms[key]." + name );
        if ( ctl ){
            if ( ctl[index] ){
                ctl[index].checked = true;
            } else {
                return;
            }
        }
    }
}

/**
 * 得到列表控件的选中项目。
 * @param name 列表控件的名字。
 * @return 被选中的列表控件项目的值。
 */
function getSelectCtlValue(name){
    var ctl = null;
    for ( var key in document.forms ){
        ctl = eval("document.forms[key]." + name);
       if ( ctl ){
            return ctl.options[ctl.selectedIndex].text;
        }
    }
    return -1;
}

/**
 * 设置列表控件的选中项目。
 * @param name 列表控件的名字。
 * @param value 被选中的列表控件项目的值。
 */
function setSelectCtlValue(name, value){
    var ctl = null;
    for ( var key in document.forms ){
        ctl = eval("document.forms[key]." + name);
        if ( ctl ){
            for ( var i = 0; i < ctl.options.length; i++ ){
                if ( ctl.options[i].text == value ){
                    ctl[i].selected = true;
                }
            }
        }
    }
}
/**
 * 设置活动提示区的位置。
 * @param e 鼠标事件（仅对Mozilla）。
 */
function setToolTipAreaPos(e){
    setFloatAreaPos("TOOL_TIP_COMPONENT", e);
}
/**
 * 设置活动提示区的位置。
 * @param e 鼠标事件（仅对Mozilla）。
 */
function setFloatAreaPos(areaId, e){
    var areaObj = document.getElementById(areaId);
    var scrollSize = 40;
    var mouseOffset = 20;
    if(e != null)
    {//mozilla
        /* 设置X。*/
        if (( areaObj.offsetWidth + e.clientX + scrollSize ) > document.body.clientWidth){
            areaObj.style.left = document.body.scrollLeft + e.clientX - areaObj.offsetWidth;
        } else {
            areaObj.style.left = window.scrollX + e.clientX + mouseOffset;
        }
        /* 设置Y。*/
        if (( (areaObj.offsetHeight + mouseOffset)> (document.body.clientHeight - e.clientY))
            && (e.clientY > (areaObj.offsetHeight + scrollSize)))
        {
            areaObj.style.top = document.body.scrollTop + e.clientY - areaObj.offsetHeight;
        }else {
            areaObj.style.top = window.scrollY + e.clientY + mouseOffset;
        }
    }
    else
    {//MSIE
        /* 设置X。*/
        if (( areaObj.offsetWidth + event.clientX + scrollSize ) > document.body.clientWidth){
            areaObj.style.left = document.body.scrollLeft + event.clientX - areaObj.offsetWidth;
        } else {
            areaObj.style.left = document.body.scrollLeft + event.clientX + mouseOffset;
        }

        /* 设置Y。*/
        if (( (areaObj.offsetHeight + mouseOffset) > (document.body.clientHeight - event.clientY))
            && (event.clientY > (areaObj.offsetHeight + scrollSize)))
        {
            areaObj.style.top = document.body.scrollTop + event.clientY - areaObj.offsetHeight;
        } else {
            areaObj.style.top = document.body.scrollTop + event.clientY + mouseOffset;
        }
    }
}
/** 隐藏活动提示区。 */
function hideToolTipArea(){
    var toolTipArea = document.getElementById("TOOL_TIP_COMPONENT");
    toolTipArea.style.display = 'none';
}

/**
 * 显示活动提示区并根据鼠标位置进行定位。
 * @param e 鼠标事件（仅对mozilla）。
 */
function showToolTipArea(e){
    var toolTipArea = document.getElementById("TOOL_TIP_COMPONENT");
    toolTipArea.style.display = '';
    setToolTipAreaPos(e);
}
/**
 * @param id 按钮组件的ID。
 * @param defaultCss 默认风格。
 * @create 2004-10-15 source0
 */
function createImageButton(id, defaultCss){
    var btn = document.getElementById(id);
    btn.style.border = 0;
    //btn.style.width = 49;
    //btn.style.height = 48;
    if ( defaultCss ){
        btn.className = defaultCss;
        btn.defaultClassName = defaultCss;
    } else {
        btn.className = "normalButton";
    }
}

function createImageButtonTop(id, defaultCss){
    var btn = document.getElementById(id);
    btn.style.border = 0;
    //btn.style.width = 38;
    //btn.style.height = 35;
    if ( defaultCss ){
        btn.className = defaultCss;
        btn.defaultClassName = defaultCss;
    } else {
        btn.className = "normalButtonTop";
    }
}
/**
 * 给指定的对象添加边框。
 * @param containerId 对象的容器对象。
 * @param objId 对象。
 * @create 2004-10-14 source0
 */
function borderContent(containerId, objId){
    var cnt = new String();
    var path = DEFAULT_BORDER_IMAGE_PATH;
    var size = DEFAULT_BORDER_SIZE;
    cnt += "<table cellspacing=0 cellpadding=0 width='100%' height='100%'>";
    cnt += "<tr>";
    cnt += "<td width=" + size + " height=" + size + " style='background:url(" + path + "/nwCorner.gif)'>";
    cnt += "<td style='background:url(" + path + "/nBorder.gif)'></td>";
    cnt += "<td width=" + size + " height=" + size + " style='background:url(" + path + "/neCorner.gif)'>";
    cnt += "</tr>";
    cnt += "<tr>";
    cnt += "<td style='background:url(" + path + "/wBorder.gif)'>";
    cnt += "<td>";
    cnt += getOuterHTML(document.getElementById(objId));
    cnt += "</td>";
    cnt += "<td style='background:url(" + path + "/eBorder.gif)'>";
    cnt += "</tr>";
    cnt += "<tr>";
    cnt += "<td width=" + size + " height=" + size + " style='background:url(" + path + "/swCorner.gif)'>";
    cnt += "<td style='background:url(" + path + "/sBorder.gif)'></td>";
    cnt += "<td width=" + size + " height=" + size + " style='background:url(" + path + "/seCorner.gif)'>";
    cnt += "</tr>";
    cnt += "</table>";
    setInnerHTML(document.getElementById(containerId), cnt);

}
/**
 * 给指定的对象添加边框。
 * @param containerId 对象的容器对象。
 * @param objId 对象。
 * @create 2004-10-14 source0
 */
function borderContentInput(containerId, objId, imagePath, borderSize){
    var cnt = new String();
    var path = imagePath;
    var size = borderSize;
    cnt += "<table border=0 cellspacing=0 cellpadding=0>";
    cnt += "<tr>";
    cnt += "<td width=" + size + " height=" + size + " style='background:url(" + path + "/nwCorner.gif)'>";
    cnt += "<td style='background:url(" + path + "/nBorder.gif)'></td>";
    cnt += "<td width=" + size + " height=" + size + " style='background:url(" + path + "/neCorner.gif)'>";
    cnt += "</tr>";
    cnt += "<tr>";
    cnt += "<td style='background:url(" + path + "/wBorder.gif)'>";
    cnt += "<td style='background-color:#000000'>";
    cnt += getOuterHTML(document.getElementById(objId));
    cnt += "</td>";
    cnt += "<td style='background:url(" + path + "/eBorder.gif)'>";
    cnt += "</tr>";
    cnt += "<tr>";
    cnt += "<td width=" + size + " height=" + size + " style='background:url(" + path + "/swCorner.gif)'>";
    cnt += "<td style='background:url(" + path + "/sBorder.gif)'></td>";
    cnt += "<td width=" + size + " height=" + size + " style='background:url(" + path + "/seCorner.gif)'>";
    cnt += "</tr>";
    cnt += "</table>";
    setInnerHTML(document.getElementById(containerId), cnt);

}
/**
 * 给指定的浮动显示对象添加边框。
 * @param id 区域对象的ID。
 * @param contentId 显示内容的对象的id。
 * @param cssName CSS样式。
 * @create 2004-10-25 source0
 */
function borderFloatElement(id, contentId, cssName)
{
    if ( null == cssName )
    {
        cssName = "floatArea";
    }
    var path = "common/image/borderTip";
    var cnt = new String();
    var size = "3";
    cnt += "<table id='" + id + "' style='position:absolute;display:none;'";
    cnt += " cellspacing=0 cellpadding=0 class='" + cssName + "'>";
    cnt += "<tr>";
    cnt += "<td width=" + size + " height=" + size + " style='background:url(" + path + "/nwCorner.gif)'>";
    cnt += "<td style='background:url(" + path + "/nBorder.gif)'></td>";
    cnt += "<td width=" + size + " height=" + size + " style='background:url(" + path + "/neCorner.gif)'>";
    cnt += "</tr>";
    cnt += "<tr>";
    cnt += "<td style='background:url(" + path + "/wBorder.gif)'>";
    cnt += "<td id='" + contentId + "' style='padding:5px;background-color:#000;'>";
    cnt += "</td>";
    cnt += "<td style='background:url(" + path + "/eBorder.gif)'>";
    cnt += "</tr>";
    cnt += "<tr>";
    cnt += "<td width=" + size + " height=" + size + " style='background:url(" + path + "/swCorner.gif)'>";
    cnt += "<td style='background:url(" + path + "/sBorder.gif)'></td>";
    cnt += "<td width=" + size + " height=" + size + " style='background:url(" + path + "/seCorner.gif)'>";
    cnt += "</tr>";
    cnt += "</table>";
    setOuterHTML(document.getElementById(id), cnt);
}

/**
 * 显示“正在载入”的信息。
 * @create 2004-10-19 source0
 */
function showProcess(msg, func){
	setInnerHTML(document.getElementById("PROCESS_AREA_CONTENT"), msg + "......");
	var processArea = document.getElementById("PROCESS_AREA");
	processArea.style.display = "";
	processArea.style.position = "absolute";
	processArea.style.top = document.body.clientHeight / 2;
	processArea.style.left = (document.body.clientWidth - 100) / 2;
	window.setTimeout("" + func + "", 0);
}
/**
 * 隐藏“正在载入”的信息。
 * @create 2004-10-19 source0
 */
function hideProcess(){
    document.getElementById("PROCESS_AREA").style.display = "none";
}

/**
 * 显示一个消息框。
 * @param type 类型。
 * @param title 标题。
 * @param msg 消息内容。
 */
function showMessageBox(type, title, msg){
    var cssStyle = "info";
    if ( MESSAGE_TYPE_ERROR == type ){
        cssStyle = "error";
    } else if ( MESSAGE_TYPE_WARN == type ){
        cssStyle = "warn";
    }
    var toolTipArea = document.getElementById("TOOL_TIP_AREA");
    var content = new String();
    content += "<table><tbody>";
    content += "<tr><td align='left' class='" + cssStyle + "'><span style='font-size:14px; font-weight:bold;'>" + title + "</span></td></tr>";
    content += "<tr><td height=2 class='separator'></td></tr>";
    content += "<tr><td>" + msg + "</td></tr>";
    content += "<tr><td align=center height=50>";
    content += "<input type='button' class='closeToolTip' id='CLOSE_MESSAGE_BOX' value='关闭' onClick='hideToolTipArea();'>";
    content += "</td></tr>";
    content += "</tbody</table>";
    setInnerHTML(toolTipArea, content);
    toolTipArea.style.display = '';
    toolTipArea.style.left = (document.body.clientWidth - toolTipArea.offsetWidth)/ 2;
    toolTipArea.style.top = (document.body.clientHeight - toolTipArea.offsetHeight)/ 2;
    document.getElementById("CLOSE_MESSAGE_BOX").focus();
}

/**
 * 显示一个确认输入框。
 * @param msg 提示消息。
 * @return 如果用户确认，返回 true，否则返回 false。
 */
function showConfirm(msg){
    return "OK" == prompt(msg + "\n如果需要继续，请输入大写字母的字符串 \"OK\" ", "");
}


function mouseDownButtonLink(id){
    document.getElementById(id).className = "mouseDownButtonLink";
}
function mouseUpButtonLink(id){
    var btn = document.getElementById(id);
    if ( btn.defaultClassName ){
        btn.className = btn.defaultClassName;
    } else {
        btn.className = "normalButtonLink";
    }
}


function mouseDownButton(id){
    document.getElementById(id).className = "mouseDownButton";
}
function mouseUpButton(id){
    var btn = document.getElementById(id);
    if ( btn.defaultClassName ){
        btn.className = btn.defaultClassName;
    } else {
        btn.className = "normalButton";
    }
}
function clickButton(id){
    var btn = document.getElementById(id);
    if ( btn.defaultClassName ){
        btn.className = btn.defaultClassName;
    } else {
        btn.className = "selectedButton";
    }
}
