/*
该函数将判断某一变量是否为空字符串
该函数将被函数is_textbox_null调用
str:被测试的字符串变量
*/
function is_only_space(str){
	for(i=0;i<=str.length-1;i++){
		if (str.charAt(i) != " ") 
			return false;
	}
	return true;
}
/*
function check_multiple_item (objForm , Item , ItemCaption, AllowNum);
该函数用于检验某些多选表单元素
objForm:  表单名称
Item:  将被检验的多选表单元素名称
ItemCaption:  Item的说明
AllowNum:  Item允许被选择的元素个数
*/
function check_multiple_item (objForm , Item , ItemCaption, AllowNum)
{
	selected_num = 0;
	is_selected = false;
	if (isNaN(AllowNum))
		AllowNum = 1;

	for (i=0;i<objForm.elements.length;i++)
	{
		if (objForm.elements[i].name == Item)
		{
			if (objForm.elements[i].options[0].selected)
			{
				alert(ItemCaption + "不能选择\“请选择\”");
				objForm.elements[i].focus();
				return false;
			}

			for (j=0;j<objForm.elements[i].length;j++)
			{
				if (objForm.elements[i].options[j].selected)
				{
					selected_num++;
					is_selected = true;
				}
			}
			if (selected_num > AllowNum)
			{
				alert(ItemCaption + "选择的数目过多");
				objForm.elements[i].focus();
				return false;
			}
			if (!is_selected)
			{
				alert(ItemCaption + "不能为空");
				objForm.elements[i].focus();
				return false;
			}
			else
				return true;
		}
	}
	alert("Err, I can't find the item " + Item + ", you can kick me");
	return false;
}
/*
function is_item_not_null(Item, ItemCaption);
该函数将判断表单中元素值是否为空
该函数将调用is_only_space
Item:  表单元素名称
ItemCaption:  Item的说明
*/
function is_item_not_null(Item, ItemCaption)
{
	if ((Item.value == "") || is_only_space(Item.value))
	{
		alert(ItemCaption + "不能为空！");
		Item.focus();
		return false;
	}
	return true;
}

function is_item_not_null_nof(Item, ItemCaption)
{
	if ((Item.value == "") || is_only_space(Item.value))
	{
		alert(ItemCaption + "不能为空！");
		return false;
	}
	return true;
}



/*
function check_email(Email)
该函数用于检验Email
Email: 表单中Email元素名称，或者Email变量名
*/
function check_email(Email)
{
	if (!is_item_not_null(Email, "Email地址"))
		return false;

	var pattern = /^([.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/; 
	flag = pattern.test(Email.value); 
	if(!flag)
	{
		alert("您填写的Email地址格式不符。"); 
		Email.focus();
		return false;
	}
	else
		return true;
}
/*
function is_digital(Item, ItemCaption)
该函数用于检验某一Item是否为数字
Item: 表单中元素名称，或者变量名
isObj:确定变量Email代表一个Item对象还是一个仅仅变量，if true,Item是一个表单中元素名称，否则……
ItemCaption:仅当IsObj = ture时有效,用于出错提示
*/
function is_digital(Item, ItemCaption)
{
	var pattern = /^([0-9]|.)+$/;
	flag = pattern.test(Item.value); 
	if(!flag)
	{
		alert(ItemCaption + "不是数字。"); 
		Item.focus();
		return false;
	}
	else
		return true;
}

/*
function check_length(Item, MinLength, MaxLength, ItemCaption)
该函数用于检验某一Item是否长度符合要求
Item: 表单中元素名称，或者变量名
MinLength:最短长度
MaxLength:最大长度
ItemCaption:仅当IsObj = ture时有效,用于出错提示
*/
function check_length(Item, MinLength, MaxLength, ItemCaption)
{
   //该函数使用实际字符长度
	if ((MinLength == 0) && (MaxLength == 0))
		return true;

	if (MaxLength < MinLength)
	{
		alert("\"check_length\"函数调用错误。");
		return false;
	}
	//alert(Item.value.Tlength());
	var str = replace_qj(Item.value);
	var str2 = str.search(/\S/);
	if(MinLength>0 && str2 == -1)
	{
		alert(ItemCaption + "内容不许为空。"); 
		Item.focus();
		return false;
	}
	if ((Item.value.Tlength() < MinLength) || (Item.value.Tlength() > MaxLength))
	{
		alert(ItemCaption + "长度不符合要求。"); 
		Item.focus();
		return false;
	}
	else
		return true;
}

function check_length_asc(Item, MinLength, MaxLength, ItemCaption)
{
   //该函数使用字符的长度
	if ((MinLength == 0) && (MaxLength == 0))
		return true;

	if (MaxLength < MinLength)
	{
		alert("\"check_length_asc\"函数调用错误。");
		return false;
	}
	//alert(Item.value.Tlength());
	if ((StrLen(Item.value) < MinLength) || (StrLen(Item.value) > MaxLength))
	{	
		alert(ItemCaption + "长度不符合要求\n\n至少需要"+MinLength/2+"个汉字或"+MinLength+"个字母\n\n最多不超过"+MaxLength/2+"个汉字或"+MaxLength+"个字母。"); 
		Item.focus();
		return false;
	}
	else
		return true;
}
/*
function backup1(objForm)
备用函数：可以限定表单元素的取值范围和长度
*/
function backup1(objForm)
{
	var pattern = /^[a-zA-Z][a-zA-Z0-9_-]{4,9}$/; 
	flag = pattern.test(objForm.text1.value); 
	if(!flag){ 
	  alert("文本不对!"); 
	  objForm.text1.focus();
	  return false;
	}
}
/*
function check_postcode(PostCode)
该函数用于检验邮政编码
*/
function check_postcode(PostCode)
{
	if (!check_length(PostCode, 6, 6, "邮政编码"))
		return false;

	if (!is_digital(PostCode, "邮政编码"))
		return false;
	return true;
}
/*
function check_password(Pass1, Pass2, MinLength, MaxLength)
该函数用于检验密码
*/
function check_password(Pass1, Pass2, MinLength, MaxLength,ItemCaption)
{
	ItemCaption=(ItemCaption==null)?"密码":ItemCaption;
	if (!check_text(Pass1, MinLength, MaxLength,ItemCaption))
		return false;

	if (!check_text(Pass2, MinLength, MaxLength, "第二次输入的"+ItemCaption))
		return false;

	if (Pass1.value != Pass2.value)
	{
		alert(ItemCaption+"两次输入的密码不一致。");
		Pass1.value = "";
		Pass2.value = "";
		Pass1.focus();
		return false;
	}
	return true;

}
/*
function check_idcard(IDCard)
该函数用于检验身份证
*/
function check_idcard(IDCard)
{
	if (!is_item_not_null(IDCard, "身份证号码"))
		return false;

	var re = /\d*x?/g; 
	if (!re.test(IDCard))
	{
		alert("格式不对！");
		IDCard.focus();
		return false;
		
	}

	//if ((IDCard.value.length != 15) && (IDCard.value.length != 18))
	//{
	//	alert("身份证号码长度不对。");
	//	IDCard.focus();
	//	return false;
	//}
	return true;
}
/*
function check_text(Item, MinLength, MaxLength, ItemCaption)
该函数用于检验文本框
*/
function check_text(Item, MinLength, MaxLength, ItemCaption)
{
	if (!is_item_not_null(Item, ItemCaption))
		return false;
	if (!check_length_asc(Item, MinLength, MaxLength, ItemCaption))
		return false;
	return true;
}
/*
function check_radio(Item, ItemCaption)
该函数用于检验Radio框
Item:表单元素名，比如 objForm.username
ItemCaption:表单元素显示给用户看的文字名，比如 "用户名"
当Item元素不存在或者尚未选择相应选项的时候，将报出错提醒信息给用户。
*/
function check_radio(Item, ItemCaption)
{
	if (Item)
	{
		for (i = 0; i < Item.length;i++ )
		{
			if (Item[i].checked == true)
				return true;
		}
		alert(ItemCaption + "不能为空");
		Item[0].focus();
		return false;
	}
	else
	{
		alert("表单元素\"" + ItemCaption + "\"不存在");
		return false;
	}
}
//禁止全角
function isQj(elem){
  //[\u4E00-\u9FA5]汉字﹐[\uFE30-\uFFA0]全角字符
  var pattern=/[\uFE30-\uFFA0]/gi;
  if(pattern.test(elem)){
    //不为汉字
	//alert("不能用全角");
	//elem="";
	return false;
  }else{
    //输入正常
    return true;
  }
}
//checkUsername函数
function checkUsername(username)
{
    var tempUsername = username.replace(/(^[\s,，‘'　]*)|([\s,，'’　]*$)/g, "");
    if (tempUsername=="")
    {
    	return false;	
    }
    tempUsername = tempUsername.replace(/[\u4E00-\u9FA5]/gi, "");
    tempUsername = tempUsername.replace(/^([a-zA-Z0-9])+$/, "");
    //tempUsername = tempUsername.replace(/^[0-9]+$/;, "");
    if (tempUsername!="")
    {
    		return false;	
    }
    else
    {
    	return true;	
    }
}

//由a-z和A-Z组成的字符串
function isStr(elem){
	var pattern=/^[a-zA-Z]+$/;
	if(pattern.test(elem)){
		return true;
	}else{
		return false;
	}
}
//由字符串和数字字符串组成
function isCharAndNumber(str) {
	var ValidChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	if(str.length>15) return false;
	for (var i =0; i<str.length; i++) {
		if (ValidChars.indexOf(str.charAt(i)) == -1) {
			return false;
			break;
		} 
	} 
	return true;
}


//判断字符由字母和数字，下划线,点号组成.且开头的只能是下划线和字母
function isStrin(elem){
  var pattern=/^(\b[a-zA-Z]|\b_)([a-zA-Z0-9]+(_|\.)?[a-zA-Z0-9]*)+$/;
  if(pattern.test(elem)){
    return true;
  }else{
    return false;
  }
}
//将全角字符转换为半角（目前只转换０,１,２,３,４,５,６,７,８,９,，）
function replace_qj(str)
{   
	Arryqj=new Array('０','１','２','３','４','５','６','７','８','９','，','　');
	Arrybj=new Array('0','1','2','3','4','5','6','7','8','9',',','');
    for(i=0;i<Arryqj.length;i++)
		{
		var re = eval("/"+Arryqj[i]+"/g");
		str=str.replace(re,Arrybj[i]);
		}
    return str;
}
//该函数用来检测是否是标准日期格式2004-10-12 12:33
function checkdatetime(strdatetime)
{
  if(!/^20[0-1][0-9]-\d{1,2}-\d{1,2} [0-2]{0,1}[0-9]:[0-5]{0,1}[0-9]$/g.test(strdatetime))
	return false;
  else
	return true;
}
//该函数用于检验某一个变量是不是数字，包括小数
function is_number(strvalue)
{
  if(!/^[.0-9]*$/g.test(strvalue)) 
	return false;  
 else 
   return true;
}
//设置cookie
function writecookie(sName,sValue)
	{
		//var ndate = new Date()
		//ndate = new Date(ndate.getTime()+1*(24*60*60*1000))
		try{
		parent.document.cookie=  sName + "=" + escape(sValue) + ";path=/member;";
		}
		catch(e)
		{
		document.cookie=  sName + "=" + escape(sValue) + ";path=/member;";
		}
		
		 
	}

//trim函数
String.prototype.trim = function()
{
    return this.replace(/(^[\s,，‘'　]*)|([\s,，'’　]*$)/g, "");
}
//取字符串实际长度
String.prototype.Tlength = function(){var arr=this.match(/[^\x00-\xff]/ig);return this.length+(arr==null?0:arr.length);}

//字符串左取
String.prototype.left = function(num,mode){if(!/\d+/.test(num))return(this);var str = this.substr(0,num);if(!mode) return str;var n = str.Tlength() - str.length;num = num - parseInt(n/2);return this.substr(0,num);}

//字符串右取
String.prototype.right = function(num,mode){if(!/\d+/.test(num))return(this);var str = this.substr(this.length-num);if(!mode) return str;var n = str.Tlength() - str.length;num = num - parseInt(n/2);return this.substr(this.length-num);}

/*
替换用户名中的特殊字符“+”,新系统不存在非法字符或特殊字符，主要是以前的老用户有此情况
*/
function Replace_username(username)
{
	var regex=/\+/g;	//username中含有+号
	var usertmp;
	usertmp=username;
	if(username.indexOf("+")>0)
	{
		usertmp=username.replace(regex,"%2b");
	}
	
	regex=/\=/g			//username中含有=号
	if(username.indexOf("=")>0)
	{
		usertmp=usertmp.replace(regex,"%3d");
	}	
	return usertmp;
}


//含有非法字符~!@%^&*();'\"?><[]{}\\|,:/=+-""'   \$|\(|\)|\*|\+|\-|\.|\[|]|\?|\\|\^|\{|\||}|~|`|!|@|#|%|&|_|=|<|>|/|,
function havenotallow(elem){
  var str = "$()*+-.[]?\^{\|}~`!@#%&_=<>/\",';";
  for(i=0;i<elem.length;i++)
   if (str.indexOf(elem.charAt(i)) !=-1){
          return false;
	}
     return true;
}

//用户名中禁止包含的字符
function check_username(UserStr){	
	if (UserStr.indexOf("大师")>=0)
	{
		alert("用户名中不得包含大师、安华等字符");
		return false;
	}
	if (UserStr.indexOf("安华")>=0)
	{
		alert("用户名中不得包含大师、安华等字符");
		return false;
	}
	if (UserStr.indexOf("0x")>=0)
	{
		alert("用户名中包含有非法字符");
		return false;
	}
	return true;
}

function isKeyNumber(){
  
  if ( (event.keyCode == 46) ||  // DEL
       (event.keyCode == 8)  ||  // backspace
       (event.keyCode == 9)  ||  // tab
       (event.keyCode == 37) ||  // ＄ key
       (event.keyCode == 38) ||  // ¤ key
       (event.keyCode == 39) ||  // ℃ key
       (event.keyCode == 40) ||  // ￠ key
       (event.keyCode == 35) ||  // HOME key
       (event.keyCode == 36) ||  // END key
       (event.keyCode == 13) ||  // Enter key
       (event.keyCode == 189) ||  // - key
       ((event.keyCode >= 48) && (event.keyCode <= 57 )) || // 0 ~ 9
       ((event.keyCode >= 96) && (event.keyCode <= 105 ))||   // 0 ~ 9 in 
		 !isNaN(String.fromCharCode(event.keyCode))
     )
  {

		event.returnValue=true;
 
  }
  else{
		event.returnValue=false;
  }
}
function isMoney(){
  
  if ( (event.keyCode == 46) ||  // DEL
       (event.keyCode == 8)  ||  // backspace
       (event.keyCode == 9)  ||  // tab
       (event.keyCode == 37) ||  // ＄ key
       (event.keyCode == 38) ||  // ¤ key
       (event.keyCode == 39) ||  // ℃ key
       (event.keyCode == 40) ||  // ￠ key
       (event.keyCode == 35) ||  // HOME key
       (event.keyCode == 36) ||  // END key
       (event.keyCode == 13) ||  // Enter key
       (event.keyCode == 189) ||  // - key
       (event.keyCode == 110) ||  // . key
       (event.keyCode == 190) ||  // . key
       ((event.keyCode >= 48) && (event.keyCode <= 57 )) || // 0 ~ 9
       ((event.keyCode >= 96) && (event.keyCode <= 105 ))||   // 0 ~ 9 in 
		 !isNaN(String.fromCharCode(event.keyCode))
     )
  {
		event.returnValue=true;
 
  }
  else{
		event.returnValue=false;
  }
}
//文本框提示信息函数
function checkCode(n,formid,mstr) {
	var code = document.getElementById(formid)
	if (n == 0 && code.value == mstr){code.value="";code.style.cssText = "color:#000000"}
	if (n == 1 && code.value == ""){code.value = mstr;code.style.cssText = "color:#aaaaaa"}
}

//计算字符串的长度
function StrLen(sString)
{
	var sStr,iCount,i,strTemp ; 
	
	iCount = 0 ;
	sStr = sString.split("");
	for (i = 0 ; i < sStr.length ; i ++)
	{
	strTemp = escape(sStr[i]); 
	if (strTemp.indexOf("%u",0) == -1) // 表示是汉字
	{ 
	iCount = iCount + 1 ;
	} 
	else 
	{
	iCount = iCount + 2 ;
	}
	}
	return iCount ;
}
function compare2Time(dAt,dBt,value){
	var dA=new Date(dAt.replace(/-/,"/"));
	var dB=new Date(dBt.replace(/-/,"/"));
	var dAV=Date.parse(dA)
	var dBV=Date.parse(dB)
	var minuValue=value*1000*60*60;
	if(dAV==isNaN){
		dAV=0;
	}
	if(dBV==isNaN){
		dBV=0;
	}
	
	if((dAV-dBV)<0 || (dAV-dBV)>minuValue)
	   return false;
	else
	   return true;
}

function openModalDialog(theURL,theWidth,theHeight) {
  window.showModalDialog(theURL,window,"help: No; resizable: No; status: No;scrollbars:yes;center: Yes;dialogWidth:"+theWidth+"px;dialogHeight:"+theHeight+"px;");
}

function checkisNum(obj) {
	var str=document.getElementById(obj).value;
	var ValidChars = "0123456789.";
	var err=0;
	if(str.length>20) err++;;
	for (var i =0; i<str.length; i++) {
		if (ValidChars.indexOf(str.charAt(i)) == -1) {
			err++;
		} 
		if (i==0&&((str.charAt(i)=='0'&&str.charAt(i)!='.')||str.charAt(i)=='.')) {
			err++;
		}
		if (str.charAt(i)=='.') {
			err++;
		}
	} 
	if(err>0){
		alert("对不起，您输入的不是数字！");
		document.getElementById(obj).value="";
		document.getElementById(obj).focus();
		return false;
	}
}

document.write("<iframe width=\"0\" height=\"0\"></iframe>");

function checkIsInt(obj){
	var str=document.getElementById(obj).value;
	var ValidChars = "0123456789";
	var err=0;
	for (var i =0; i<str.length; i++) {
		if (ValidChars.indexOf(str.charAt(i)) == -1) {
			err++;
		} 
	} 
	if(err>0){
		alert("对不起，您输入的不是整数！");
		document.getElementById(obj).value="";
		document.getElementById(obj).focus();
		return false;
	}else{
	return true;
	}
}

function IsIntLen(obj,minValue, maxValue)
{
	checkIsInt(obj);
	var str=document.getElementById(obj).value;
   
   //该函数使用实际字符长度
	if ((minValue == 0) && (maxValue == 0))
		return true;

	if (maxValue < minValue)
	{
		alert("\"IsIntLen\"函数调用错误。");
		return false;
	}
	
	if (str < minValue || str > maxValue)
	{
		alert("对不起，您只能输入"+minValue+"到"+maxValue+"的整数。"); 
		document.getElementById(obj).value="";
		document.getElementById(obj).focus();
		return false;

	}
	return true;

}
