//******************************************************************
//*************************** Form输入限制  ************************
//******************************************************************
//登陆校验
function validateForm(){
	var form0=document.forms[0];
	//var ssn=form0.username.value.toLowerCase();
	//document.forms[0].password.value = ignoreSpaces(document.forms[0].password.value);
	if (isEmpty(form0.username))
	{
		alert("用户名不能为空.");
		form0.password.value="";
		form0.username.focus();
		return false;			
	}
	if (isEmpty(form0.password))
	{
		alert("密码不能为空.");
		form0.password.value="";
		form0.password.focus();
		return false;	
	}	
	document.forms[0].username.value = form0.username.value.toLowerCase();
	document.forms[0].submit();
}
//只能输入字符allowInput指定范围内字符,在文本域调用示例onkeypress="return restrictInput('1234567890');"
function restrictInput(allowInput){
	var keyCode=window.event.keyCode;
	if (keyCode==13)
		return true;
	for (var i=0;i<allowInput.length;i++){
		if (allowInput.charCodeAt(i)==keyCode)
			return true;
	}
	return false;
}
//只能输入正整数,在文本域调用示例onkeypress="return restrictIn('1234567890');"
function restrictInt(){
	return restrictInput("1234567890");
}
//只能输入正负浮点数,在文本域调用示例onkeypress="return restrictFloat(document.form1.amount);"
function restrictFloatNumber(obj){
	var xx=restrictInput("+-1234567890.");
	if (xx==false)
		return false;
	var value=obj.value;
	var pos=value.indexOf(".");
	if (pos>=0 && window.event.keyCode==46)//小数点最多只能出现一次
		return false;
	//if (value.length>0 && (window.event.keyCode==45 || window.event.keyCode==43))
	//	return false;
	return true;
}
//输入的小写字符自动转换为大写字符,调用示例onkeypress="return restrictUpperCase();"
function restrictUpperCase() {
	if (window.event.keyCode>=97 & window.event.keyCode<=122 )
		window.event.keyCode = window.event.keyCode - 32;
}
//输入的大写字符自动转换为小写字符,调用示例onkeypress="return restrictLowerCase();"
function restrictLowerCase() {
	if (window.event.keyCode>=65 & window.event.keyCode<=90 )
		window.event.keyCode = window.event.keyCode + 32;
}
//只能输入日期,格式两种yyyy-mm-dd或yyyymmdd,在文本域调用示例onkeypress="return restrictDate(document.form1.date,'yyyymmdd');"
//简单的判断,yyyymmdd格式必须全是数字,yyyy-mm-dd必须全为数字或中划线,且中划线只能在第5、8位出现
function restrictDate(obj,dateStyle){
	var style=1;
	if (equalsIgnoreCase(dateStyle,"yyyy-mm-dd"))
		style=2;
	var xx=null;
	if (style==1)
		xx=restrictInput("1234567890");
	else
		xx=restrictInput("1234567890-");
	if (xx==false)
		return false;
	var keyCode=window.event.keyCode;
	var value=obj.value;
	if (value==null)
		value="";
	var len=value.length;
	if (style==1){//格式1
		if (len==8)
			return false;
	}else{//日期格式2
		if (len==10)
			return false;
	}
	return true;
}
//只能输入日期,格式为yyyymmdd,在文本域调用示例onkeypress="return restrictDate(document.form1.date);"
//简单的判断,yyyymmdd格式必须全是数字,yyyy-mm-dd必须全为数字或中划线,且中划线只能在第5、8位出现,JavaScript不支持重载
//function restrictDate(obj){
//	return restrictDate(obj,"yyyymmdd");
//}
//是否全为英文字符(只能为大小写字母),调用方式onKeyPress="return onlyEng();"
function restrictEng(){
	if(!(event.keyCode>=65 && event.keyCode<=90) && !(event.keyCode>=97 && event.keyCode<=122))
		return false;
	return true;
}
//输入限制，数字及两位小数
function regInput(obj, reg, inputStr)
{
var docSel = document.selection.createRange();
if (docSel.parentElement().tagName != "INPUT") return false;
oSel = docSel.duplicate();
oSel.text = "";
var srcRange = obj.createTextRange();
oSel.setEndPoint("StartToStart", srcRange);
var str = oSel.text + inputStr + srcRange.text.substr(oSel.text.length);
return reg.test(str);
}
//******************************************************************
//*************************** Form输入校验  ************************
//******************************************************************
//判断是否为空(包括全角或半角空格),obj为文本域
function isEmpty(obj){
	if (gtrim(obj.value)=="")	
		return true;
	return false;
}
//判断checkbox或radio是否至少有一个被选中)
function isChecked(obj){
	found=false;
	//单个
	if (typeof(obj.length)== "undefined"){
		found=obj.checked;
	}else{//多个
	    for (var i=0;i<obj.length;i++){
			if (obj[i].checked){
			   found=true;
			   break;
			}
		}
	}
	return found;
}
//邮件地址是否有效,为空返回true
function validateEmail(obj) {
	if (isEmpty(obj))
		return true;
	if (obj.value.search(/^\w+(([-\/]\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
		return true;
	return false;
}
//判断电话号码是否有效,电话号码只能是数字、中划线和空格,为空返回true
function validateTel(obj){
	return validateInput(obj,"0123456789- ");
}
//校验邮政编码,obj为邮政编码的值
/**
function validatePostalCode(obj)
{
var patrn=/^[a-zA-Z0-9 ]{3,12}$/;
if (!patrn.exec(obj)) return false;
return true;
}
**/
//校验邮政编码,obj为邮政编码的值
function validatePostalCode(value)
{
var patrn=/^[0-9 ]{6,6}$/;
if (!patrn.exec(value)) return false;
return true;
}
//判断obj文本域的字符是否在allowInput字符串内,若obj文本域值为空,返回true
function validateInput(obj,allowInput){
	if (isEmpty(obj))
		return true;
	return isInChars(obj.value,allowInput);
}
//判断日期格式是否正确,年份必须为4位,月份、日期必须各2位,dateStyle只能为yyyymmdd或yyyy-mm-dd
//obj为日期文本输入域,若为空返回true
function validateDate(obj,dateStyle){
	if (isEmpty(obj))
		return true;
	var source=obj.value;
	var style=1;
	if (equalsIgnoreCase(dateStyle,"yyyy-mm-dd"))
		style=2;
	var len=source.length;
	if (style==1 && len!=8){
		return false
	}else if (style==2 && len!=10){
		return false;
	}
	var year,month,day;
	if (style==1){
		year=source.substring(0,4);
		month=source.substring(4,6);
		day=source.substring(6);
	}else{
		year=source.substring(0,4);
		month=source.substring(5,7);
		day=source.substring(8);
	}
	if (!isInChars(year,"1234567890") || !isInChars(month,"1234567890") || !isInChars(day,"1234567890") || month==0 || day==0)
		return false;
	if (month>12)	
		return false;
	if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12){
		if (day>31)
			return false;
	}else if (month==4 || month==6 || month==9 || month==11){
		if (day>30)
			return false;
	}else if (month==2){
		if (!isLeapYear(year)){
			if (day>28)
				return false;
		}else{
			if (day>29)
				return false;
		}
	}
	return true;
}

//******************************************************************
//*************************** Form输入其它操作  ************************
//******************************************************************
/**
  * copy数据到粘贴板,srcObject为文本域
  */
function copy(srcObject){
    clipboardData.setData("text",srcObject.value);
}
/**
  * 剪切数据到粘贴板,srcObject为文本域
  */
function cut(srcObject){
    clipboardData.setData("text",srcObject.value);
    srcObject.value="";
}
/**
  * 从粘贴板获取数据copy到dstObject文本域控件
  */
function paste(dstObject){
    dstObject.value=clipboardData.getData("text")==null?"":clipboardData.getData("text");
}
//取得文件扩展名,若无扩展名返回空字符串,否则返回扩展名
function getFileExt(path){
	var tmp = path;
	var pos=tmp.lastIndexOf(".");
	if (pos<0)
		return "";
	return tmp.substring(pos+1);
}
//选择文本域示例:当鼠标移动到文本框时选中文本框
//onMouseOver(document.form1.name.select());

//******************************************************************
//*************************** 字符串常用函数  **********************
//******************************************************************
//去除字符串前后导空格
function trim(source){
	return replace(source,/(^\s*)|(\s*$)/g,"");
}
//去除字符串中的所有空格,包括全角空格
function gtrim(source){
	if (source.length==0)
		return "";
	var dest="";
	for (var i=0;i<source.length;i++){
		var char0=source.charAt(i);
		if (char0!=' ' && char0!='　')
			dest=dest+char0;
	}
	return dest;
}
//获取字符串字节长度
function lengthOfByte(source){
	if (source==null || source=="")
		return 0;
	var count=0;
	for (var i=0;i<source.length;i++){
		var char0=source.charAt(i);
		if (char0<255)
			count=count+1;
		else
			count=count+2;
	}
	return count;			
}
//source字符串中的字符是否包含在字符串集合chars中,若source为空,返回false
function isInChars(source,chars){
	if (source==null || source=="")
		return false;
	for (var i=0;i<source.length;i++){
		var char0=source.charAt(i);
		var found=false;
		for (var j=0;j<chars.length;j++){
			if (char0==chars.charAt(j)){
				found=true;
				break;
			}
		}
		if (found==false)
			return false;
	}
	return true;			
}
//是否相等，不区分大小写
function equalsIgnoreCase(source,dest){
	return source.toUpperCase()==dest.toUpperCase();
}
/*由于在Js中Replace只能替换一次,下面函数能全局替换2003.5.13 -sundy
as_expression --包含要替换的字符串
as_find --搜索的子字符串
as_replacement --要替换的字符串
替换字符串函数,如:"'"转换为"''" 
*/
function replace(as_expression,as_find,as_replacement){
	var fs_expression = as_expression;

	var fs_find = as_find;
	var fs_replacement = as_replacement;
	if (fs_expression == "") return "";
	if (fs_find == "") return "";
	//构造正则表达式\,$,(,),*,+,.,[,?,{,^,|为特殊字符,必须转义后替换
	fs_regx1 = /\\/gi;
	fs_find = fs_find.replace(fs_regx1,"\\\\");
	fs_replacement = fs_replacement.replace(fs_regx1,"\\");
	fs_regx1 = /\$/gi;
	fs_find = fs_find.replace(fs_regx1,"\\\$");
	fs_replacement = fs_replacement.replace(fs_regx1,"\$");         
	fs_regx1 = /\(/gi;
	fs_find = fs_find.replace(fs_regx1,"\\\(");
	fs_replacement = fs_replacement.replace(fs_regx1,"\(");         
	fs_regx1 = /\)/gi;
	fs_find = fs_find.replace(fs_regx1,"\\\)");
	fs_replacement = fs_replacement.replace(fs_regx1,"\)");         
	fs_regx1 = /\*/gi;
	fs_find = fs_find.replace(fs_regx1,"\\\*");
	fs_replacement = fs_replacement.replace(fs_regx1,"\*");         
	fs_regx1 = /\+/gi;
	fs_find = fs_find.replace(fs_regx1,"\\\+");
	fs_replacement = fs_replacement.replace(fs_regx1,"\+");
	fs_regx1 = /\./gi;
	fs_find = fs_find.replace(fs_regx1,"\\\.");
	fs_replacement = fs_replacement.replace(fs_regx1,"\.");
	fs_regx1 = /\[/gi;
	
	fs_find = fs_find.replace(fs_regx1,"\\\[");
	fs_replacement = fs_replacement.replace(fs_regx1,"\[");
	fs_regx1 = /\?/gi;
	fs_find = fs_find.replace(fs_regx1,"\\\?");
	fs_replacement = fs_replacement.replace(fs_regx1,"\?");
	fs_regx1 = /\^/gi;
	fs_find = fs_find.replace(fs_regx1,"\\\^");
	fs_replacement = fs_replacement.replace(fs_regx1,"\^");
	fs_regx1 = /\{/gi;
	
	fs_find = fs_find.replace(fs_regx1,"\\\{");
	fs_replacement = fs_replacement.replace(fs_regx1,"\{");
	fs_regx1 = /\|/gi;
	fs_find = fs_find.replace(fs_regx1,"\\\|");

	fs_replacement = fs_replacement.replace(fs_regx1,"\|");
	fs_find = "/" + fs_find + "/gi";
	//返回替换后的值
	return fs_expression.replace(eval(fs_find),fs_replacement);
	
}
//******************************************************************
//*************************** 其它常用函数  ************************
//******************************************************************
//取得客户端的信息
function clientInfo(){
  strClientInfo="availHeight=   "+window.screen.availHeight+"\n"+
    "availWidth=   "+window.screen.availWidth+"\n"+
    "bufferDepth=   "+window.screen.bufferDepth+"\n"+
    "colorDepth=   "+window.screen.colorDepth+"\n"+
    "colorEnable=   "+window.navigator.cookieEnabled+"\n"+
    "cpuClass=   "+window.navigator.cpuClass+"\n"+
    "height=   "+window.screen.height+"\n"+
    "javaEnable=   "+window.navigator.javaEnabled()+"\n"+
    "platform=   "+window.navigator.platform+"\n"+
    "systemLanguage=   "+window.navigator.systemLanguage+"\n"+
    "userLanguage=   "+window.navigator.userLanguage+"\n"+
    "width=   "+window.screen.width;
  return strClientInfo;
}
//获取操作系统
function getOsName(){
	var useragent = navigator.userAgent; 
	var os = ''; 
	var indx, indx2 = 0; // OS 
	indx = useragent.indexOf('Linux'); 
	if (indx > -1) os = "Linux"; 
	indx = useragent.indexOf('Mac OS X'); 
	if (indx > -1) os = "Mac OS X"; 
	indx = useragent.indexOf('Mac_PowerPC'); 
	indx2 = useragent.indexOf('PPC'); 
	if (indx > -1 || indx2 > -1) os = "Mac Power PC"; 
	indx = useragent.indexOf('Sun OS'); 
	indx2 = useragent.indexOf('SunOS'); 
	if (indx > -1 || indx2 > -1) os = "Sun OS"; 
	indx = useragent.indexOf('HP-UX'); 
	if (indx > -1) os = "HP-UX"; 
	indx = useragent.indexOf('Windows 95'); 
	indx2 = useragent.indexOf('Win95'); 
	if (indx > -1 || indx2 > -1) os = "Windows 95"; 
	indx = useragent.indexOf('Windows 98'); 
	indx2 = useragent.indexOf('Win98'); 
	indx3 = useragent.indexOf('Windows98'); 
	if (indx > -1 || indx2 > -1 || indx3 > -1) os = "Windows 98"; 
	indx = useragent.indexOf('Windows ME'); 
	if (indx > -1) os = "Windows ME"; 
	indx = useragent.indexOf('Windows NT 4.0'); 
	indx2 = useragent.indexOf('WinNT'); 
	if (indx > -1 || indx2 > -1) os = "Windows NT"; 
	indx = useragent.indexOf('Windows NT 5.0'); 
	indx2 = useragent.indexOf('Windows 2000'); 
	if (indx > -1 || indx2 > -1) os = "Windows 2000"; 
	indx = useragent.indexOf('XP'); 
	indx = useragent.indexOf('Windows NT 5.1'); 
	if (indx > -1 || indx2 > -1) os = "Windows XP";
	return os;
}
//获取浏览器版本
function browserVer(){
	var useragent=navigator.userAgent;
	var msiePos = useragent.indexOf('MSIE'); 
	if (msiePos > -1) { 
		var Ver = ""; 
		if(useragent.indexOf('MSIE 4') != -1) 
			Ver = '4.0'; 
		else if(useragent.indexOf('MSIE 5.0') != -1)
			Ver = '5.0'; 
		else if(useragent.indexOf('MSIE 5.1') != -1) 
			Ver = '5.1'; 
		else if(useragent.indexOf('MSIE 5.5') != -1) 
			Ver = '5.5'; 
		else if(useragent.indexOf('MSIE 6.0') != -1) 
			Ver = '6.0'; 
		return 'Microsoft Internet Explorer '+Ver;
	}
	var pos = useragent.indexOf('Opera'); 
	if (pos > -1) { 
		var Ver = useragent.substring(pos + 6); 
		var pos = Ver.indexOf(' '); 
		Ver = Ver.substring(0, pos); 
		Ver = Ver + 'testop'; ///// 
		return 'Opera '+Ver;
	} 
	// Get Netscape version before Gecko (Netscape < 6.0) 
	if (Name == "Netscape") { 
		var Ver = useragent.substring(8); 
		var pos = Ver.indexOf(' '); 
		Ver = Ver.substring(0, pos); 
		Ver = Ver + 'testns5u'; ///// 
	} 
	if (Name == "Netscape" && parseInt(navigator.appVersion) >= 5) { 
		var pos = useragent.lastIndexOf('/'); 
		var Ver = useragent.substring(pos + 1); 
		Ver = Ver + 'testns5p'; ////// 
	} 
	return 'Netscape '+Ver;
}
//取得当前系统日期时间字符串,月日不足两位补零,格式:yyyy-mm-dd hh24:mi:ss
function now() {
	var fs_now = new Date();
	var fs_return="";
	fs_return = fs_now.getFullYear() + "-" + appendZero(fs_now.getMonth() + 1) + "-" + appendZero(fs_now.getDate());
	fs_return +=" " + appendZero(fs_now.getHours()) + ":" + appendZero(fs_now.getMinutes()) + ":" + appendZero(fs_now.getSeconds());
	return fs_return;
} 
//日期自动补零函数
function appendZero(n){return(("00"+ n).substr(("00"+ n).length-2));}
//取得当前系统日期
function today() {
	var fs_now = new Date();
	var fs_return="";
	fs_return = fs_now.getFullYear() + "-" + appendZero(fs_now.getMonth() + 1) + "-" + appendZero(fs_now.getDate());
	return fs_return;
} 
//判断是否闰年,year为年份(包括世纪)
function isLeapYear(year){
	if (year%4!=0)
		return false;
	if (year%100==0 && year%400!=0)
			return false;
	return true;
}
//产生m到n之间的随机整数,
function getRandom(m,n){
	return Math.round(Math.random()*(n-m))+m;	
}
//打开一个全屏或者居中的窗口
function openwindow( url, winName,width,height,otherproperty)
{
   //width,height
   //otherproperty
   xposition=0; yposition=0;
   if ((parseInt(navigator.appVersion) >= 4 ))
   {
        if(width == 1)
        {
            width=screen.width - 10;
            height=screen.height - 55;
            xposition = 0;
            yposition = 0;
        }
        else
        {
            if (width < 1)
            {
                width=screen.width*width;
                height=screen.height*height;
            }
            xposition = (screen.width - width) / 2;
            yposition = (screen.height - height) / 2 - 15;
        }

      if (yposition < 0 )
      {
            yposition = 0;
      }
    }
    theproperty= "width=" + width + ", " + "height=" + height + ", "
        + "screenX=" + xposition + ", " //Netscape
        + "screenY=" + yposition + ", " //Netscape
        + "left=" + xposition + ", " //IE
        + "top=" + yposition + ", "; //IE

    theproperty = theproperty + "location=0, "
        + "menubar=0, "
        + "resizable=0, "
        + "scrollbars=1, "
        + "status=0, "
        + "toolbar=0, "
        + "hotkeys=0, ";
    theproperty = theproperty + ', ' + otherproperty;
    winobj=window.open( url,winName,theproperty );
    if (url == "about:blank")
    {
        winobj.document.writeln("<font face='Arial' color='red' size='4'>Loading.....</font>");
    }
    winobj.focus();
    return winobj;
}

function createTxtLink(){//给选中的文本创建链接
  var sText=document.selection.createRange();//sText.text获取选中的文本
  if(sText.text!="")
  {
    document.execCommand("CreateLink");
    if(sText.parentElement().tagName=="A")
    {
      sText.parentElement().innerText=sText.parentElement().href;
      document.execCommand("ForeColor",false,"#ffff00");
    }
  }
}

//验证身份证号码是否有效
function  IsValidId(number){
	var date, Ai;
	var verify = "10x98765432";
	var Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
	var area = ['','','','','','','','','','','','北京','天津','河北','山西','内蒙古','','','','','','辽宁','吉林','黑龙江','','','','','','','','上海','江苏','浙江','安微','福建','江西','山东','','','','河南','湖北','湖南','广东','广西','海南','','','','重庆','四川','贵州','云南','西藏','','','','','','','陕西','甘肃','青海','宁夏','新疆','','','','','','台湾','','','','','','','','','','香港','澳门','','','','','','','','','国外'];
	var re = number.match(/^(\d{2})\d{4}(((\d{2})(\d{2})(\d{2})(\d{3}))|((\d{4})(\d{2})(\d{2})(\d{3}[x\d])))$/i);
	if(re == null) return false;
	if(re[1] >= area.length || area[re[1]] == "") return false;
	if(re[2].length == 12){
		Ai = number.substr(0, 17);
		date = [re[9], re[10], re[11]].join("-");
	}
	else{
		Ai = number.substr(0, 6) + "19" + number.substr(6);
		date = ["19" + re[4], re[5], re[6]].join("-");
	}
	if(!this.IsDate(date, "ymd")) return false;
	var sum = 0;
	for(var i = 0;i<=16;i++){
		sum += Ai.charAt(i) * Wi[i];
	}
	Ai +=  verify.charAt(sum%11);
	return (number.length ==15 || number.length == 18 && number == Ai);
}

//判断字串是否为一个合法满足某条件的日期字符
function  IsDate(op, formatString){
   formatString = formatString || "ymd";
   var m, year, month, day;
   switch(formatString){
      case "ymd" :
	m = op.match(new RegExp("^((\\d{4})|(\\d{2}))([-./])(\\d{1,2})\\4(\\d{1,2})$"));
	if(m == null ) return false;
	day = m[6];
	month = m[5]*1;
	year =  (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10));
	break;
      case "dmy" :
	m = op.match(new RegExp("^(\\d{1,2})([-./])(\\d{1,2})\\2((\\d{4})|(\\d{2}))$"));
	if(m == null ) return false;
	day = m[1];
	month = m[3]*1;
	year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10));
	break;
      default :
	break;
     }
     if(!parseInt(month)) return false;
     month = (month==0)?12:month;
     var date = new Date(year, month-1, day);
     return (typeof(date) == "object" && year == date.getFullYear() && month == (date.getMonth()+1) && day == date.getDate());
} 

//产生完整年份
function GetFullYear(y){
   return ((y<30?"20" : "19") + y)|0;
}
function mhHover(tbl, idx, cls)
{
	var t, d;
	if (document.getElementById)
		t = document.getElementById(tbl);
	else
		t = document.all(tbl);
	if (t == null) return;
	if (t.getElementsByTagName)
		d = t.getElementsByTagName("TD");
	else
		d = t.all.tags("TD");
	if (d == null) return;
	if (d.length <= idx) return;
	d[idx].className = cls;
}
function SetCwinHeight(obj,addheight)
{
  var cwin=obj;
  if (document.getElementById)
  {
    if (cwin && !window.opera)
    {
      if (cwin.contentDocument && cwin.contentDocument.body.offsetHeight)
        cwin.height = cwin.contentDocument.body.offsetHeight + addheight; 
      else if(cwin.Document && cwin.Document.body.scrollHeight)
        cwin.height = cwin.Document.body.scrollHeight + addheight;
    }
  }
}

function restrictAmount(obj){
	var xx=restrictInput("1234567890.");
	if (xx==false)
		return false;
	var value=obj.value;
	var pos=value.indexOf(".");
	if (pos>=0 && window.event.keyCode==46)//
		return false;
	//if (value.length>0 && (window.event.keyCode==45 || window.event.keyCode==43))
	//	return false;
	return true;
}
function iFrameHeight() {   
		var ifm= document.getElementById("iframepage");   
		var subWeb = document.frames ? document.frames["iframepage"].document : ifm.contentDocument;   
		if(ifm != null && subWeb != null) {
		   ifm.height = subWeb.body.scrollHeight;
		}   
}   

