/* $id : utils.js 5052 2007-02-03 10:30:13z weberliu $ */ var browser = new object(); browser.ismozilla = (typeof document.implementation != 'undefined') && (typeof document.implementation.createdocument != 'undefined') && (typeof htmldocument != 'undefined'); browser.isie = window.activexobject ? true : false; browser.isfirefox = (navigator.useragent.tolowercase().indexof("firefox") != - 1); browser.issafari = (navigator.useragent.tolowercase().indexof("safari") != - 1); browser.isopera = (navigator.useragent.tolowercase().indexof("opera") != - 1); var utils = new object(); utils.htmlencode = function(text) { return text.replace(/&/g, '&').replace(/"/g, '"').replace(//g, '>'); } utils.trim = function( text ) { if (typeof(text) == "string") { return text.replace(/^\s*|\s*$/g, ""); } else { return text; } } utils.isempty = function( val ) { switch (typeof(val)) { case 'string': return utils.trim(val).length == 0 ? true : false; break; case 'number': return val == 0; break; case 'object': return val == null; break; case 'array': return val.length == 0; break; default: return true; } } utils.isnumber = function(val) { var reg = /^[\d|\.|,]+$/; return reg.test(val); } utils.isint = function(val) { if (val == "") { return false; } var reg = /\d+/; return !reg.test(val); } utils.isemail = function( email ) { var reg1 = /([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-za-z]{2,4}|[0-9]{1,3})(\]?)/; return reg1.test( email ); } //互亿无线代码 utils.ismobile = function( mobile ) { var reg1 = /^1[3456789][\d]{9}$/; var reg2 = /^0[\d]{10,11}$/; if (reg1.test( mobile )) return true; if (reg2.test( mobile )) return true; return false; } //互亿无线代码 utils.istel = function ( tel ) { var reg = /^[\d|\-|\s|\_]+$/; //只允许使用数字-空格等 return reg.test( tel ); } utils.fixevent = function(e) { var evt = (typeof e == "undefined") ? window.event : e; return evt; } utils.srcelement = function(e) { if (typeof e == "undefined") e = window.event; var src = document.all ? e.srcelement : e.target; return src; } utils.istime = function(val) { var reg = /^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}$/; return reg.test(val); } utils.x = function(e) { //当前鼠标x坐标 return browser.isie?event.x + document.documentelement.scrollleft - 2:e.pagex; } utils.y = function(e) { //当前鼠标y坐标 return browser.isie?event.y + document.documentelement.scrolltop - 2:e.pagey; } utils.request = function(url, item) { var svalue=url.match(new regexp("[\?\&]"+item+"=([^\&]*)(\&?)","i")); return svalue?svalue[1]:svalue; } utils.$ = function(name) { return document.getelementbyid(name); } function rowindex(tr) { if (browser.isie) { return tr.rowindex; } else { table = tr.parentnode.parentnode; for (i = 0; i < table.rows.length; i ++ ) { if (table.rows[i] == tr) { return i; } } } } document.getcookie = function(sname) { // cookies are separated by semicolons var acookie = document.cookie.split("; "); for (var i=0; i < acookie.length; i++) { // a name/value pair (a crumb) is separated by an equal sign var acrumb = acookie[i].split("="); if (sname == acrumb[0]) return decodeuricomponent(acrumb[1]); } // a cookie with the requested name does not exist return null; } document.setcookie = function(sname, svalue, sexpires) { var scookie = sname + "=" + encodeuricomponent(svalue); if (sexpires != null) { scookie += "; expires=" + sexpires; } document.cookie = scookie; } document.removecookie = function(sname,svalue) { document.cookie = sname + "=; expires=fri, 31 dec 1999 23:59:59 gmt;"; } function getposition(o) { var t = o.offsettop; var l = o.offsetleft; while(o = o.offsetparent) { t += o.offsettop; l += o.offsetleft; } var pos = {top:t,left:l}; return pos; } function cleanwhitespace(element) { var element = element; for (var i = 0; i < element.childnodes.length; i++) { var node = element.childnodes[i]; if (node.nodetype == 3 && !/\s/.test(node.nodevalue)) element.removechild(node); } }