/*
该函数将判断某一变量是否为空字符串
该函数将被函数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 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 ((Item.value.length < MinLength) || (Item.value.length > MaxLength))
	{
		alert(ItemCaption + "长度不符合要求。"); 
		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{15}|\d{18}|\d{17}(X|x))$/; 
	if (!re.test(IDCard.value))
	{
		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;
  }
}
//含有非法字符~!@%^&*();'\"?><[]{}\\|,:/=+-""'   \$|\(|\)|\*|\+|\-|\.|\[|]|\?|\\|\^|\{|\||}|~|`|!|@|#|%|&|_|=|<|>|/|,
function havegg(elem){
  var str = "$()*+-.[]?\^{\|}~`!@#%&_=<>/\",';";
  for(i=0;i<elem.length;i++)
   if (str.indexOf(elem.charAt(i)) !=-1){
          return false;
	}
     return true;
}

//由a-z和A-Z组成的字符串
function isStr(elem){
	var pattern=/^[a-zA-Z]+$/;
	if(pattern.test(elem)){
		return true;
	}else{
		return false;
	}
}
//由字符串和数字字符串组成
function isStrAndInt(elem){
    var patten=/^[a-zA-Z0-9]+$/;
	if(pattern.test(elem)){
		return true;
	}else{
		return false;
	}
}
//判断字符由字母和数字，下划线,点号组成.且开头的只能是下划线和字母
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=/b_trade;";
		}
		catch(e)
		{
		document.cookie=  sName + "=" + escape(sValue) + ";path=/b_trade;";
		}
		
		 
	}

//改函数用于弹出模块窗口
function openModalDialog(theURL) { //v2.0
  window.showModalDialog(theURL,window,"help: No; resizable: No; status: No;scrollbars:yes;center: Yes;dialogWidth:740px;dialogHeight:600px;");
}
//该函数用于弹出式协议的显示
function openModalDialogAgree(theURL) { //v2.0
  window.showModalDialog(theURL,window,"help: No; resizable: No; status: No;scrollbars:yes;center: Yes;dialogWidth:550px;dialogHeight:550px;");
}
//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 check_username(username)
{	
	if(username.indexOf("大师")>=0)
	{
		alert("用户名中不得包含'易讯、500WAN'等字符");
		return false;
	}
	
	if(username.indexOf("易讯")>=0)
	{
		alert("用户名中不得包含'易讯、500WAN'等字符");
		return false;
	}
	
	if(username.indexOf("500WAN")>=0)
	{
		alert("用户名中不得包含'易讯、500WAN'等字符");
		return false;
	}
	if(username.indexOf("0x")>=0)
	{
		alert("用户名中包含有非法字符");
		return false;
	}
	
	var pattern=/^[A-Za-z0-9\u4e00-\u9fa5]+$/;	
	if(!pattern.test(username))
	{	
		alert("用户名中包含有非法字符!");
		return false;
	}
	
	return true;
}