/* $Id: common.js,v 1.2 2005/12/27 00:37:47 nio Exp $ */
//  通用 JS 函数。

/**
 * 去掉字符串首尾空字符。
 *
 * @param   string  sInput  需要去掉首尾空字符的字符串。
 * @return  string  返回去掉首尾空字符的字符串。
 */
function _GetTrimStr(sInput) 
{
    var sOutput;

    if (typeof (sInput) != "string")
        return false;

    sOutput = sInput.replace (/^(\s+)/, "");     //去掉左边空字符
    sOutput = sOutput.replace (/(\s+)$/, "");   //去掉右边空字符

    return sOutput;
}   //end function _GetTrimStr


/**
 * 检测是否为数字，包括小数。
 *  
 * @param   string  sInput      需要检测的字符串。
 * @param   boolean bCanBeNull  被检测的字符串是否允许为空，默认值为 false（不允许为空）。
 * @return  boolean 符号条件返回 true，否则返回 false。
 */
function _IsNumber(sInput, bCanBeNull) 
{
    if (!bCanBeNull && !sInput)
        return false;

    if (isNaN(sInput))
        return false;
    else
        return true;
}   //end function _IsNumber


/**
 * 检测是否为整数。
 *      
 * @param   string  sInput      需要检测的字符串。
 * @param   boolean bCanBeNull  被检测的字符串是否允许为空，默认值为 false（不允许为空）。
 * @return  boolean 符号条件返回 true，否则返回 false。
 */
function _IsInteger(sInput, bCanBeNull) 
{
    var reExp=/[.]/g;

    if (_IsNumber(sInput, bCanBeNull) == false)    //调用函数 _IsNumber() 判断字符串是否为数字
        return false;

    if (reExp.exec (sInput))
        return false;
    else
        return true;
}   //end function _IsInteger


/**
 * 检测字符串是否为字母、数字(不包括中文)的组合。
 * 
 * @param   string  sInput      需要检测的字符串。
 * @param   boolean bCanBeNull  被检测的字符串是否允许为空，默认值为 false（不允许为空）。
 * @return  boolean 符号条件返回 true，否则返回 false。
 */
function _IsLetterNum(sInput, bCanBeNull) 
{
    var reExp=/[^A-Za-z0-9]/g;      //查找非字母、数字字符的正则表达式
    
    if (!bCanBeNull && !sInput)
        return false;

    if (reExp.exec (sInput))
        return false;
    else
        return true;
}   //end funciton _IsLetterNum


/**
 * 检测字符串是否合法，是否含有（导致错误的）特殊字符即为不合法。
 *        
 * @param   string  sInput      需要检测的字符串。
 * @param   boolean bCanBeNull  被检测的字符串是否允许为空，默认值为 false（不允许为空）。
 * @return  boolean 符号条件返回 true，否则返回 false。
 */ 
function _IsChrValid(sInput, bCanBeNull) 
{
    var reExp=/[\'\"\/\\]/g;    //查找特殊字符的正则表达式
    
    if (!_GetTrimStr(sInput)) {
        if (!bCanBeNull)    //不允许为空的情况
            return false;
        if (bCanBeNull)     //允许为空的情况
            return true;    
    } else {
        if (reExp.test(sInput))
            return false;
        else
            return true;
    }
}   //end function _IsChrValid


/**
 * 检测字符串是否合法日期，格式为：YYY-MM-DD，如：2002-07-07。
 * 
 * @param   string  sInput      需要检测的字符串。
 * @return  boolean 符号条件返回 true，否则返回 false。
 */ 
function _IsDateValid(sInput)
{
    var reExp=/^(19|20)\d{2}[\-](0[1-9]|1[0-2])[\-](0[1-9]|[12][0-9]|3[01])$/;

    if (reExp.exec(sInput))
        return true;
    else
        return false;

}   //end function _IsDateValid


/**
 * 检测字符串是否合法日期，格式为：YYYY-MM-DD，如：2002-07-07。
 * 
 * @param   string  sInput      需要检测的字符串。
 * @param   boolean bCanBeNull  被检测的字符串是否允许为空，默认值为 false（不允许为空）。
 * @return  boolean 符号条件返回 true，否则返回 false。
 */ 
function _IsEmailValid(sInput)
{
    var sFilter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    
    if (sFilter.test(sInput)) 
        return true;
    else 
        return false;
}   //end function _IsEmailValid

//===============================================================
//  用于显示提示信息的函数
//===============================================================
var g_tooltip_elm = null;
var g_tooltip_showing = 0;
var g_tooltip_link_elm = null;
var g_tooltip_previous_click = null;
var g_tip_elm = null;

function init_tooltip(){
    g_tooltip_elm = document.createElement('DIV');
    g_tooltip_elm.className = 'ToolTip';
    g_tooltip_elm.style.display = 'none';
    document.body.appendChild(g_tooltip_elm);
}

function show_tooltip(link, text, len, left){
    var cw, ch;
    var pos_left;

    cw = document.body.clientWidth;
    ch = document.body.clientHeight;
    
    if (!g_tooltip_elm){
        init_tooltip();
    }
    if (g_tooltip_showing){
        if (g_tooltip_link_elm == link){
            hide_tooltip();
            return;
        }
        hide_tooltip();
    }

    var x = tooltip_findPosX(link);
    var y = tooltip_findPosY(link);

    if (len < 0)
        len = 200;
    
    

    g_tooltip_elm.style.width = len + 'px';
    if (arguments.length == 4){
        g_tooltip_elm.style.left = left + 'px';
    } else {
        pos_left = (x + len > cw - 20) ? (x - len) : x;
        g_tooltip_elm.style.left = pos_left + 'px';
    }   //end if
    g_tooltip_elm.style.top = (y + 20) +'px';

    g_tip_elm = document.getElementById(text);

    move_children(g_tip_elm, g_tooltip_elm);


    g_tooltip_showing = 1;
    g_tooltip_elm.style.display = 'block';
    g_tooltip_link_elm = link;

    document.onmousedown = doc_mousedown;
}

function doc_mousedown(e){
    if (getEventSrc(e) == g_tooltip_link_elm){
        document.onmousedown = function(){};
    }else{
        hide_tooltip();
    }
}

function hide_tooltip(){

    document.onmousedown = function(){};

    if (!g_tooltip_elm){
        return false;
    }

    g_tooltip_showing = 0;
    g_tooltip_elm.style.display = 'none';
    g_tooltip_link_elm = 'null';

    move_children(g_tooltip_elm, g_tip_elm);

    return false
}

function move_children(e_from, e_to){

    while(e_from.childNodes.length){
        e_to.appendChild(e_from.removeChild(e_from.childNodes[0]));
    }
}

function tooltip_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 tooltip_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 getEventSrc(e){
    if (e){ return e.target; }
    if (window.event){ return window.event.srcElement; }
    return null;
}
//===============================================================
//  End Tip
//===============================================================

function showUserInfo(sUserId) 
{
    var win = window.open('./index.php?t=gu_user_info&user_id=' + sUserId, 'k12gu_userinfo', 'toolbar=no,location=no,status=yes,resizable=yes,scrollbars=yes,width=360,height=400');
    win.focus();
}   //end function
/*-------------------------- table --------------------------*/
var stripeTable = function() {
    var tables = document.getElementsByTagName("table");    
    for (var x=0; x!=tables.length; x++){
        var table = tables[x];
        if (!table || table.className.indexOf('common') == -1 ) { return; }
        var tbodies = table.getElementsByTagName("tbody");
        for (var h = 0; h < tbodies.length; h++) {
            var even = true;
            var trs = tbodies[h].getElementsByTagName("tr");
            
            for (var i = 0; i < trs.length; i++) {
                if (table.className.indexOf('label') != -1) {
                    var tds = trs[i].getElementsByTagName("td");
                    if (tds.length > 1) {
                        tds[0].className += " key";
                    }
                }
                trs[i].onmouseover=function(){
                    this.className += " ruled"; return false
                }
                trs[i].onmouseout=function(){
                    this.className = this.className.replace("ruled", ""); return false
                }                
                if(even && trs[i].className.indexOf('thead') == -1)
                    trs[i].className += " even";
                even = !even;
            }
        }
    }
}   ///:~

/*-------------------------- form --------------------------*/
var sFocus = null;
var focusField = function() {
	var oFocus = null;
	if (sFocus) {
		oFocus = $(sFocus);
	}
    if (document.forms.length > 0) {
        var els = document.forms[0].elements;
		for (var i = 0; i < els.length; i++) {
			var el = els[i];
			if (el.type == 'text' || el.type == 'textarea' || el.type == 'password') {
				if (!oFocus)
					oFocus = el;
                if (document.all) {
				    el.onmouseover = el.onfocus = function() { this.className += ' hover' };
				    el.onmouseout  = el.onblur = function() {this.className = this.className.replace('hover', '')};
                }
			}
		}
    }
	try {
        if (oFocus) oFocus.focus();
    } catch (ex) {
        //do noting!
    }
}

/*-------------------------- round --------------------------*/
var roundDiv = function(el) {
    var opt = {border:'#C6D2E5',color:'white',bgColor:'#E8EEF7'};
    if (el) {
        Rico.Corner.round(el, opt);
    } else {
        new Effect.Round('div', 'round', opt);
    }
}

var init = function() {
	//focusField();
    roundDiv();
    stripeTable();
}
window.onload = focusField;