﻿String.prototype.trim =function(){return this.replace(/(^\s+)|(\s+$)/g,"");}
String.prototype.ltrim=function(){return this.replace(/^\s+/,"");}
String.prototype.rtrim=function(){return this.replace(/\s+$/g,"");}

function $id(s){return document.getElementById(s);}
//显示日期
function fixZero(i)
{
   if (i < 10 ) return '0'+i;
   else return i;
}

function showTime()
{
	var o = document.getElementById('time');
	if (!o) return;
	var d = new Date();
	var w = ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
    o.title = w[d.getDay()];
	o.innerHTML = d.getFullYear() + '-' + (d.getMonth()+1) + '-'+d.getDate()+'&nbsp;'+ fixZero(d.getHours())+':'+fixZero(d.getMinutes())+':'+ fixZero(d.getSeconds());
	setTimeout(arguments.callee,1000);
}


function setHome(obj)
{
   var url="http://"+document.location.host+"/";
   obj.style.behavior="url(#default#homepage)";
   obj.setHomePage(url);
}

function addToFav(name) {
    var url = "http://" + document.location.host + "/";
    try {
        window.external.AddFavorite(url, name);
    }
    catch (e) {
        alert('操作失败,请手动添加至收藏夹!');
        throw e;
    }
}

function CInt(s) {
    if (s.length < 1) return 0;
    //修复全角数字
    var b = '０１２３４５６７８９';
    for (var i = 0; i < b.length; i++) {
        s = s.replace(eval('/' + b[i] + '/g'), i);
    }
    s = s.replace(/\.\w+$/, ''); //去掉小数点
    s = s.replace(/[^\d]/g, ''); //除去非数字
    var n = parseInt(s);
    return !isNaN(n) ? n : 0;
}

function fontSize(n)
{
    var o = document.getElementById("content");
    if (!o) return;
    o.style.fontSize = n;
}

function writeCookie(name, value, hours)
{
  var expire = "";
  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire;
}

function readCookie(name)
{
   var cookieValue = "";
   var search = name + "=";
   if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}

function isEmail(str)
{
    return /^[\w-]+@[\w-]+(\.[a-z]{2,3}){1,2}$/i.test(str);
}
function isTel(str)
{
    return /^(\d{2,6}-){0,2}\d{4,16}$/.test(str);
}

function GetTimeKey() {
    var d = new Date();
    return d.getHours() + "_" + d.getMinutes() + "_" + d.getSeconds();
}

function cgVercode(o) {
    var key = GetTimeKey();
    o.src=o.src.replace(/(\?t=)[\d\.\_]+$/,'$1'+key);
}

function doSel(name,act)
{
	if (!arguments[1]){act = name;name = 'ids';}
	var o = document.getElementsByName(name);
	if (o.length <1) return;
	var f;
	switch(act)
	{
		case "all":
		    f = function(c){c.checked = true;};break;
		case "other":
		    f = function(c){c.checked = (c.checked)?false:true;};break;
		case "none":
		    f = function(c) { c.checked = false; }; break;
		default:return;
	}
	for (var i=0;i<o.length;i++) f(o[i]);
}

function getFlash(src,w,h,alpha,vars)
{
	var tmp = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="{$w}" height="{$h}"><param name="movie" value="{$src}" /><param name="quality" value="high" />{$attach1}<embed src="{$src}" quality="high" {$attach2}  pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="{$w}" height="{$h}"></embed></object>';
	tmp = tmp.replace(/\{\$src\}/g,src).replace(/\{\$w\}/g,w).replace(/\{\$h\}/g,h);
	var attach = '',attach2 = '';
	if (alpha == true){
		attach += '<param name="wmode" value="transparent" />';
		attach2+= ' wmode="transparent" ';
	}
	if (vars && vars != ''){
		attach += '<param name="FlashVars" value="'+vars+'" />';
		attach2+= ' FlashVars="'+vars+'" ';
	}
	tmp = tmp.replace('{$attach1}',attach).replace('{$attach2}',attach2);
	document.writeln(tmp);
}

function getFlv(url,w,h,auto,autoload) {
    var tmp = '<object type="application/x-shockwave-flash" data="swf/vcastr3.swf" width="@w@" height="@h@" id="vcastr3"><param name="movie" value="swf/vcastr3.swf"/><param name="allowFullScreen" value="true" /><param name="FlashVars" value="xml=<vcastr><channel><item><source>@url@</source><duration></duration><title></title></item></channel><config><bufferTime>4</bufferTime><contralPanelAlpha>0.75</contralPanelAlpha><controlPanelBgColor>0x9d0612</controlPanelBgColor><controlPanelBtnColor>0xffffff</controlPanelBtnColor><contralPanelBtnGlowColro>0xffd8da</contralPanelBtnGlowColro><controlPanelMode>float</controlPanelMode><defautVolume>0.7</defautVolume><isAutoPlay>@auto@</isAutoPlay><isLoadBegin>@autoload@</isLoadBegin><isShowAbout>false</isShowAbout><scaleMode>showAll</scaleMode><isRepeat>false</isRepeat></config><plugIns></plugIns></vcastr>"/></object>';
    w = w ? w : 480;
    h = h ? h : 360;
    auto = auto ? 'true' : 'false';
    autoload = autoload ? 'true' : 'false';
    tmp = tmp.replace(/@url@/, url).replace(/@w@/, w).replace(/@h@/, h).replace(/@auto@/, auto).replace(/@autoload@/, autoload);
    document.write(tmp);
}

function cgVercode(o) {
    var key = GetTimeKey();
    o.src = o.src.replace(/(\?t=)[\d\.\_]+$/, '$1' + key);
}

function chkVcode(o, s) {
    var s = !s ? 'vcode' : s;
    var v = o[s];
    if (!v) return false;
    if (v.value.length < 4) { alert('请填写验证码!'); v.focus(); return false; }
    if (/^[a-z0-9]{4}$/i.test(v.value) == false) { alert('验证码有误!'); v.focus(); return false; }
    return true;
}

function SetVerCode(name, id) {
    var name = name ? name : 'vcode';
    var id = id ? id : 'vercode';
    document.write('<input type="text" maxlength="4" class="ipt vcode" name="', name, '" onfocus="GetVerCode(this,\'', id, '\');" onkeyup="ajax_chkcode(this);" autocomplete="off" chk="vcode" /><span id="', id, '"></span><span id="vcodechk"><span></span></span>');
}

function GetVerCode(o, id) {
    o.onfocus = null;
    var m = document.getElementById(id);
    if (!m) return;
    m.innerHTML = '<img src="Inc/VerCode.aspx?t=1" onclick="cgVercode(this);" alt="看不清楚,点击更换" class="vcodepic" />';
}

function ajax_chkcode(o) {
    var p = document.getElementById('vcodechk');
    if (!p) return;
    if (/^[ABCDEFGHJKLMNPQRSTUVWXYZ23456789]{4}$/i.test(o.value) == false) {
        p.className = 'fail';
        return;
    }
    p.className = '';
    jQuery.get('/inc/do.ashx', { 'act': 'chkcode', 'code': o.value, 't': GetTimeKey() }, function (d) {
        p.className = (d == '1' ? 'ok' : 'fail');
    });
}
function ajax_chkuser(o) {
    var ipt = document.getElementById('uid');
    ipt.value = ipt.value.trim().replace("'", "");
    if (ipt.value == '') return;
    o.disabed = 'disabed';
    o.value = '检测中...';
    jQuery.post('/inc/do.ashx?act=chkuser', { 'uid': ipt.value, 't': GetTimeKey() }, function(d) {
        alert(d == '1' ? "恭喜,用户名可用!" : '抱歉,用户名已被注册!');
        o.disabled = '';
        o.value = '检测';
    });
}


function HtmlEncode(text) {
    return text.replace(/&/g, '&amp').replace(/\"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}

function HtmlDecode(text) {
    return text.replace(/&amp;/g, '&').replace(/&quot;/g, '\"').replace(/&lt;/g, '<').replace(/&gt;/g, '>');
}

function highNav(i) {
    var i = !i ? 1 : i;
    var o = document.getElementById('n' + i);
    if (!o) return;
    o.className += ' now';
    if (i != 1) document.getElementById('n1').className = '';
}
function GetRndArr(arr, n) {
    if (!arr || arr.length < 1) return null;
    n = n ? n : arr.length;
    var a = new Array(), b, c = arr.join('##');
    var x;
    while (a.length < n) {
        b = c.split('##');
        for (var i = 0; i < n, a.length < n; i++) {
            x = b.length > 1 ? Math.floor(Math.random() * b.length) : 0;
            a.push(b.splice(x, 1).toString());
            if (b.length < 1) break;
        }
    }
    return a;
}
