﻿/*
	File：基本函数库
	Author：elvis@dotop
	Update：3:53 2008-5-12
*/

var agt		= navigator.userAgent.toLowerCase();
var is_op	= (agt.indexOf("opera") != -1);
var is_ie	= (agt.indexOf("msie") != -1) && document.all && !is_op;
var is_ie5	= (agt.indexOf("msie 5") != -1) && document.all && !is_op;
var is_mac	= (agt.indexOf("mac") != -1);
var is_gk	= (agt.indexOf("gecko") != -1);
var is_sf	= (agt.indexOf("safari") != -1);
var is_saf	= ((agt.indexOf('applewebkit') != -1) || (navigator.vendor == 'Apple Computer, Inc.'));
var is_ie4	= ((is_ie) && (agt.indexOf('msie 4.') != -1));
var is_moz	= ((navigator.product == 'Gecko') && (!is_saf));
var is_kon	= (agt.indexOf('konqueror') != -1);
var is_ns	= ((agt.indexOf('compatible') == -1) && (agt.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_saf));
var is_ns4    = ((is_ns) && (parseInt(navigator.appVersion) == 4));
var is_opera  = ((agt.indexOf('opera') != -1) || (typeof(window.opera) != 'undefined'));
var is_webtv  = (agt.indexOf('webtv') != -1);

String.prototype.length2 = function()
{
	var cArr = this.match(/[^\x00-\xff]/ig);
	return this.length + (cArr == null ? 0 : cArr.length);
}

String.prototype.trim = function()
{
	var tStr = this;
	while(' ' == tStr.substr(0, 1)) tStr = tStr.substr(1);
	while(' ' == tStr.substr(tStr.length - 1, 1)) tStr = tStr.substr(0, tStr.length - 1);
	return tStr;
}

function $(str)
{
	if (typeof str == 'string' && document.getElementById(str) != null ) return document.getElementById(str);
	return null;
}

function writeDebug(str)
{
	var tO = $('CM_DEBUG');
	if(tO)
	{
		tO.value = str + '\n' + tO.value;
	}
}

function addEvent(obj, type, fn)
{
	if(obj.addEventListener) obj.addEventListener(type, fn, false);
	else if(obj.attachEvent)
	{
		obj["e" + type + fn] = fn;
		obj[type + fn] = function(){obj["e" + type + fn](window.event);}
		obj.attachEvent("on" + type, obj[type + fn]);
	}
}

function addOption(tObj, value, text, isValue)
{
	var tO = new Option;
	tO.value = value;
	tO.text = text;
	tObj.options.add(tO);
	if(isValue == value) return (tObj.options.length - 1);
	else return 0;
}

function IsNumeric(oNum)
{
	if('number' == typeof(oNum)) return true;
	var strP = /^\d+(\.\d+)?$/;
	if(!strP.test(oNum)) return false;
	try{
		if(parseFloat(oNum) != oNum) return false;
	}catch(ex){
		return false;
	}
	return true;
}

function set_div_style(obj,id,top,left,width,height,position,border,cursor,background)
{
	var obj = obj;
	obj.id = id ? id : null;
	obj.style.top = top?top:'0px';
	obj.style.left = left?left:'0px';
	if(width) obj.style.width = width ? width : '0px';
	if(height) obj.style.height = height ? height : '0px';
	obj.style.position = position?position:"static";
	if(border) obj.style.border = border?border:"1px #000 solid";
	obj.style.cursor = cursor?cursor:"default";
	if(background) obj.style.background = background?background:"";
	return obj;
}

function isNumber(sText)
{
	var ValidChars = "0123456789.";
	var IsNumber = true;
	if(sText.length < 1) IsNumber = false;
	var Char;
	
	for(i = 0; i < sText.length && IsNumber == true; i++)
	{
   		Char = sText.charAt(i);
		if (ValidChars.indexOf(Char) == -1)
		{
			IsNumber = false;
		}
	}
	return IsNumber;
}
