﻿// JScript 文件

var useragent	= navigator.userAgent.toLowerCase();
var webtv		= useragent.indexOf('webtv') != -1;
var kon			= useragent.indexOf('konqueror') != -1;
var mac			= useragent.indexOf('mac') != -1;
var saf			= useragent.indexOf('applewebkit') != -1 || navigator.vendor == 'Apple Computer, Inc.';
var opera		= useragent.indexOf('opera') != -1 && opera.version();
var moz			= (navigator.product == 'Gecko' && !saf) && useragent.substr(useragent.indexOf('firefox') + 8, 3);
var ns			= useragent.indexOf('compatible') == -1 && useragent.indexOf('mozilla') != -1 && !opera && !webtv && !saf;
var ie			= (useragent.indexOf('msie') != -1 && !opera && !saf && !webtv) && useragent.substr(useragent.indexOf('msie') + 5, 3);
function g(eid) { return document.getElementById(eid); }
function htmlencode(str) {
	str = str.replace(/&/g, '&amp;');
	str = str.replace(/</g, '&lt;');
	str = str.replace(/>/g, '&gt;');
	str = str.replace(/(?:\t| |\v|\r)*\n/g, '<br />');
	str = str.replace(/  /g, '&nbsp; ');
	str = str.replace(/\t/g, '&nbsp; &nbsp; ');
	str = str.replace(/\x22/g, '&quot;');
	str = str.replace(/\x27/g, '&#39;');
	return str;
}

function htmldecode(str) {
	str = str.replace(/&amp;/gi, '&');
	str = str.replace(/&nbsp;/gi, ' ');
	str = str.replace(/&quot;/gi, '"');
	str = str.replace(/&#39;/g, "'");
	str = str.replace(/&lt;/gi, '<');
	str = str.replace(/&gt;/gi, '>');
	str = str.replace(/<br[^>]*>(?:(\r\n)|\r|\n)?/gi, '\n');
	return str;
}

function textencode(str) {
	str = str.replace(/&amp;/gi, '&');
	str = str.replace(/</g, '&lt;');
	str = str.replace(/>/g, '&gt;');
	return str;
}

function textdecode(str) {
	str = str.replace(/&amp;/gi, '&');
	str = str.replace(/&lt;/gi, '<');
	str = str.replace(/&gt;/gi, '>');
	return str;
}
function getOffset(el) {
	var point = { x: el.offsetLeft, y: el.offsetTop };
	//Recursion
	if (el.offsetParent) {
		var parentPoint = getOffset(el.offsetParent);
		point.x += parentPoint.x;
		point.y += parentPoint.y;
	}
	return point;
}

function getScrollOffset() {
	var point;
	if (document.body.scrollTop != 0) {
		point = { x : document.body.scrollLeft, y : document.body.scrollTop };
	} else {
		point = { x : document.documentElement.scrollLeft, y : document.documentElement.scrollTop };
	}
	return point;
}
 


function run(){}
 
function ShowTime() 
{
    d = new Date();
    document.write(d.getFullYear() +'年');
	document.write((d.getMonth()+1) + '月');
	document.write(d.getDate() + '日');
	document.write(d.getHours() +'时');
	document.write(d.getMinutes()+'分');
}
function ShowTime1() 
{
    d = new Date();
    document.write(d.getFullYear() +'-');
	document.write((d.getMonth()+1) + '-');
	document.write(d.getDate() + ' ');
	document.write(d.getHours() +':');
	document.write(d.getMinutes()+'');
}
function Welcome()
{
    now = new Date(),hour = now.getHours()
	if ( parseInt(hour)<6) {document.write("凌晨好！")}
	else if (parseInt(hour)<8) {document.write("早上好！")}
	else if (parseInt(hour)<12) {document.write("上午好！")}
	else if (parseInt(hour)<14) {document.write("中午好！")}
	else if (parseInt(hour)<17) {document.write("下午好！")}
	else if (parseInt(hour)<22) {document.write("晚上好！")}
	else {document.write("夜里好！")}

}
 
String.prototype.isEmail = function()
{
	var email = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	return email.test(this);
}
 
String.prototype.isUrl = function(){
	var url = /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
	
	return url.test(this);
}
 
String.prototype.isInteger = function()
{
 
	var _i = /^[-\+]?\d+$/;
	return _i.test(this);
}
String.prototype.isUserID = function()
{
 
   var userid=/^\s*[A-Za-z0-9_-]{3,20}\s*$/;
   return userid.test(this);
}
 String.prototype.isPassword = function()
{
 
   //var userid=/^\s*[.A-Za-z0-9_-]{3,20}\s*$/;
   var userid=/^\s*[.A-Za-z0-9_-]{6,20}\s*$/;
   return userid.test(this);
}
String.prototype.isWhiteWpace=function()
{
  var whitespace = " \t\n\r";
  var i;
  for (i = 0; i < this.length; i++){ 
  var c = this.charAt(i);
  if (whitespace.indexOf(c) >= 0) {
  return true;
  }
  }
  return false;
}
String.prototype.ltrim=new Function("return   this.replace(/^\\s+/,'')");  
String.prototype.rtrim=new Function("return   this.replace(/\\s+$/,'')");
String.prototype.trim = function()
{
  return this.ltrim().rtrim();
}
String.prototype.isEmpty = function()
{
  return (this.trim()=='');
  
}

/* add 2007/09/22 */
function gval(obj)
{
  if ( typeof(obj) == "string")
  {
    return document.getElementById(obj).value ;
  }
  else if ( typeof(obj) == "object")
  {
    return obj.value ;
  }
}
function gtext(obj)
{
  var o = null;
    if ( typeof(obj) == "string")
  {
    o = document.getElementById(obj) ;
  }
  else if ( typeof(obj) == "object")
  {
    o = obj ;
  }
  try
  {
  if ( o.selectedIndex==-1) return "";
  return o.options[o.selectedIndex].text;
  }catch(e){ return "" };
}
function isEmpty( obj )
{ 
  if ( typeof(obj) == "string")
  {
    return document.getElementById(obj).value.trim().isEmpty() ;
  }
  else if ( typeof(obj) == "object")
  {
    return obj.value.trim().isEmpty() ;
  }
}

function isUrl( obj )
{

   if ( typeof(obj) == "string")
  {
    return document.getElementById(obj).value.trim().isUrl() ;
  }
  else if ( typeof(obj) == "object")
  {
    return obj.value.trim().isUrl() ;
  }
  
}
function isInt( obj )
{
  var value = "";
   if ( typeof(obj) == "string")
  {
    value = document.getElementById(obj).value.trim();
  }
  else if ( typeof(obj) == "object")
  {
    value = obj.value.trim() ;
  }
  
  
  if (value =='0')
  {
   return true ;
  }
  else
  {
     return value.isInteger() ;
     }
}
function isEmail( obj )
{

     if ( typeof(obj) == "string")
  {
    return document.getElementById(obj).value.trim().isEmail() ;
  }
  else if ( typeof(obj) == "object")
  {
    return obj.value.trim().isEmail() ;
  }
     
}
function isPassword( obj )
{
    
     if ( typeof(obj) == "string")
  {
    return document.getElementById(obj).value.trim().isPassword() ;
  }
  else if ( typeof(obj) == "object")
  {
    return obj.value.trim().isPassword() ;
  }
}
function isUserID( obj )
{  
     if ( typeof(obj) == "string")
  {
    return document.getElementById(obj).value.trim().isUserID() ;
  }
  else if ( typeof(obj) == "object")
  {
    return obj.value.trim().isUserID() ;
  }
      
}
function InputRestrict(textareaID,inputID,num)
{
  var textarea = document.getElementById(textareaID);
  if (textarea==null) return;
  var input = document.getElementById(inputID);
  input.value = num - textarea.value.length ;
  
  textarea.onkeypress = function()
  { 
     
   
     if (textarea.value.length>num)
     {
       textarea.value = textarea.value.substring(0,num);
     }
     else
     {
       input.value = num - textarea.value.length ;
     }
  }
  textarea.onkeyup = function()
  { 
     
   
     if (textarea.value.length>num)
     {
       textarea.value = textarea.value.substring(0,num);
     }
     else
     {
       input.value = num - textarea.value.length ;
     }
  }
  textarea.onmouseup = function()
  { 
     
   
     if (textarea.value.length>num)
     {
       textarea.value = textarea.value.substring(0,num);
     }
     else
     {
       input.value = num - textarea.value.length ;
     }
  }
  
}


function winOpen(Url,width,height)
{
	ow = width;
	oh = height;
	 
	var xposition=0; 
	var yposition=0;
	if ((parseInt(navigator.appVersion) >= 4 ))
	{
	xposition = (screen.width - width) / 2;
	yposition = (screen.height - height-25) / 2;
	}
	window.open (Url,"","width ="+ow+",height="+oh+",toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no,left="+xposition+",top="+yposition );
}



function addfavorite(url,text)
{
     if (document.all)
     {
        window.external.addFavorite(url,text);
     }
     else if (window.sidebar)
     {
        window.sidebar.addPanel(text,url, "");
     }
}

//设为首页
function setHomepage(obj,url){
        try
        {
                obj.style.behavior='url(#default#homepage)';obj.setHomePage(url);
        }
        catch(e)
        {
                if(window.netscape) {
                        try {
                                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
                        }
                        catch (e) {
                                alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将 [signed.applets.codebase_principal_support]的值设置为'true',双击即可。");
                        }
                        var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
                        prefs.setCharPref('browser.startup.homepage',url);
                 }
        }
}

//设为首页2
function setHome(obj,url)
{
    obj.style.behavior="url(#default#homepage)";
    obj.sethomepage(url);
}

//加为收藏
function setFavorite(url, title) {   
    try 
    {   
        window.external.addFavorite(url, title);   
    } catch (e) 
    {   
        try 
        {   
            window.sidebar.addPanel(title, url, "");   
        } catch (e) 
        {   
            alert("加入收藏失败，请使用Ctrl+D进行添加");   
        }   
    }   
}


//复制
function FuZhi()
{
    var url=document.location.href;
    if(window.clipboardData)
    { 
        var flag=window.clipboardData.setData("Text",url); 
        if(flag)
        {
            alert("已经复制到剪贴板,发送给你的好友吧"); 
        } 
        else
        {
            alert("对不起，目前此功能只支持IE，请直接复制地址栏的地址！");
        } 
    }
} 

function form(f) {
	switch (typeof f) {
		default:
		case null:
		case '':
		case 'undefined':
			return document.forms[0];
		case 'string':
		case 'number':
			return document.forms[f];
		case 'object':
			return f;
	}	
}
function undefined(variable) {
	return typeof variable == 'undefined' ? true : false;
}
function trim(str) {
	return (str + '').replace(/(\s+)$/g, '').replace(/^\s+/g, '');
}
function inarray(obj, arr) {
	if(typeof obj == 'string') {
		for(var i in arr) {
			if(arr[i] == obj) {
					return true;
			}
		}
	}
	return false;
}

function preview(value)
{
  testwin = open("");
  testwin.document.open();
  testwin.document.write(value);
  testwin.document.close();
}
function checkbyvalue(c, v)
{	
	if (!undefined(c.options))
	{
			
		for (var i = 0; i < c.options.length; i++)
		{
			if (c.options[i].value == v)
			{
				c.options[i].selected = true;
				break;
			}
		}
	}
	
}

function checkradiobyvalue(c, v)
{
	if (!undefined(c.item))
	{
		for (var i = 0; i < c.item.length; i++)
		{
			if (c.item(i).value == v)
			{
				c.item(i).checked = true;
				break;
			}
		}
	}
	
}
function limitContent(contentid,width)
{
   var elments = $(contentid).getElementsByTagName("*");
   for(var i=0;i<elments.length;i++)
  {
    if (parseInt(elments[i].getAttribute("width"))>width)
    {
       elments[i].setAttribute("width",width -1) ;
    }
    if (elments[i].style.width)
    {
        if (parseInt(elments[i].style.width)>width)
        {
           elments[i].style.width = width -1 ;
        }
    }
  }
 
}
function setfocus(objid)
{
    if (document.getElementById(objid))
    {
        document.getElementById(objid).focus();
    }
}
function select(objid)
{
    if (document.getElementById(objid))
    {
        document.getElementById(objid).focus();
        document.getElementById(objid).select();
    }
}
function breakWords(objid,width)
{
 
    var obj = document.getElementById(objid);
    var content =obj.innerHTML ;
   
    if ( content.length<=width)
    {
        obj.innerHTML  = content;
    }
    else
    {
        var str = '' ;
        
            for(var i=0;i<content.length;i++)
            {
                if (i % width !=0)
                {
                    str+=content.substr(i,1);
                    
                }
                else
                {
                   if(i!=0)
                   {
                    str+="<br/>" + content.substr(i,1);
                   }
                }
            }
      
         obj.innerHTML=str;
    }
}

//设置cookie
function setCookie(sName,sValue,expireHours) {
	var cookieString = sName + "=" + escape(sValue);
	if (expireHours>0) {
		 var date = new Date();
		 date.setTime(date.getTime + expireHours * 3600 * 1000);
		 cookieString = cookieString + "; expire=" + date.toGMTString();
	}
	document.cookie = cookieString;
}

//获取cookie
function getCookie(sName) {
  var aCookie = document.cookie.split("; ");
  for (var j=0; j < aCookie.length; j++){
	var aCrumb = aCookie[j].split("=");
	if (escape(sName) == aCrumb[0])
	  return unescape(aCrumb[1]);
  }
  return null;
}
 
document.getElementsByClassName = function(classStr)
{
    var obj=this.getElementsByTagName("*");
    var objArr=new Array;
    for(var i=0;i<obj.length;i++){

        var classNameArr=obj[i].className.split(" ");
        for(var j=0;j<classNameArr.length;j++)
        {
            if(classNameArr[j] == classStr)
            {
                objArr.push( obj[i]);
                break;
            }
        } 
    }  
    if (objArr.length>0){
        return objArr;
    }else{
        return null;
    }
}

