﻿//fm_ini()
function fm_ini(color){
	var fm,i,j
	for(i=0;i<document.forms.length;i++)
	{
		fm=document.forms[i]
		for(j=0;j<fm.length;j++)
		{
			if((fm[j].alt+"").indexOf(":")==-1)
				continue;
				
			if (color){
				oo("chk_"+fm[j].name).style.color=color;
			}
			else{
				oo("chk_"+fm[j].name).style.color="#FF0000";
			}
						
			fm[j].onblur = function(){this.style.background=(tx_chk(this)==true)?'#FFFFFF':'#FFFFFF'}
			fm[j].onfocus = function(){this.style.background='#FFFF00'}
		}
	}
}
function fm_chk(fm,subname)
{
	if(subname){
		document.getElementById(subname).disabled=true;
	}
	var isPass=true
	for(var i=0;i<fm.length;i++)
	{
		if((fm[i].alt+"").indexOf(":")==-1)
			continue;
			
		if(!tx_chk(fm[i]))
		{
			isPass=false;
			
			if(subname){
				document.getElementById(subname).disabled=false;
			}
	
			fm[i].style.background='#FFFFFF';//提交后错误文本框颜色
		}
	}
	if(isPass)
	{
		return true;
	}
	else
	{
		alert("您填写的信息有误，请根据提示更改！")
		return false
	}
}

function tx_chk(obj)
{
	var name, key, val = obj.value, oShow=oo("chk_"+obj.name);
	name = obj.alt.slice(0,obj.alt.indexOf(":"));
	key = "/"+obj.alt.slice(obj.alt.indexOf(":")+1)+"/";
	oo("chk_"+obj.name).style.display = "none";
	
	if(key.indexOf("/Nothing/")>-1&&val==""){
		//name = (name=="u_password")?"Password":name;
		if(name=="u_password"){
			name = "密码";
		}
		else if(name=="u_user"){
			name = "用户名";
		}
		
		if(name=="Country"||name=="Card Type"){
			oShow.innerHTML="请选择"+name;
		}
		else{
			oShow.innerHTML="请输入"+name;
		}
		oShow.style.display="";
		return false;
	}
	if(key.indexOf("/b15/")>-1&&!(strLen(val)==15)){
		oShow.innerHTML="必须是15字节"
		oShow.style.display=""
		return false
	}
	if(key.indexOf("/b16/")>-1&&!(strLen(val)==16)){
		oShow.innerHTML="必须是16字节"
		oShow.style.display=""
		return false
	}
	if(key.indexOf("/4-16/")>-1&&(strLen(val)<4||strLen(val)>16)){
		oShow.innerHTML="必须是4-16字节"
		oShow.style.display=""
		return false
	}
	if(key.indexOf("/6-16/")>-1&&(strLen(val)<6||strLen(val)>16)&&strLen(val)!=0){
		oShow.innerHTML="必须是6-16字节"
		oShow.style.display=""
		return false
	}
	if(key.indexOf("/Strange-characters/")>-1&&(/>|<|,|\[|\]|\{|\}|\?|\/|\+|=|\||\'|\\|\"|:|;|\~|\!|\@|\#|\*|\$|\%|\^|\&|\(|\)|`/i).test(val)){
		oShow.innerHTML="请勿使用特殊字符"
		oShow.style.display=""
		return false
	}
	if(key.indexOf("/Strange-characters-pass_word/")>-1&&(/>|<|\+|,|\[|\]|\{|\}|\/|=|\||\'|\\|\"|:|;|\~|\!|\@|\#|\*|\$|\%|\^|\&|\(|\)|`/i).test(val)){
		oShow.innerHTML="请勿使用特殊字符"
		oShow.style.display=""
		return false
	}
	if(key.indexOf("/Strange-characters-phone/")>-1&&(/>|<|,|\[|\]|\{|\}|\?|\/|\+|=|\||\'|\\|\"|:|;|\~|\!|\@|\$|\%|\^|\&|\(|\)|`/i).test(val)){
		oShow.innerHTML="请勿使用特殊字符"
		oShow.style.display=""
		return false
	}	
	
	if(key.indexOf("/Strange-characters-mail/")>-1&&(/>|<|\+|,|\[|\]|\{|\}|\/|=|\||\'|\\|\"|:|;|\~|\!|\#|\*|\$|\%|\^|\&|\(|\)|`/i).test(val)){
		oShow.innerHTML="请勿使用特殊字符"
		oShow.style.display=""
		return false
	}
	
	if(key.indexOf("/Spaces/")>-1&&val.indexOf(" ")>-1){//有空格
		oShow.innerHTML="不能有空格"
		oShow.style.display=""
		return false
	}
	//if( key.indexOf("/首尾不能是空格/")>-1&&val!="")
	//{
		//var pat_s = /^(\s)/;
		//var pat_e = /(\s)$/;

		//if ( pat_s.test(val) || pat_e.test( val))
		//{
			//oShow.innerHTML="首尾不能是空格"
			//oShow.style.display=""
			//return false
		//}
	//}
	if(key.indexOf("/All-digital/")>-1&&val!=""){//全数字
		if ( /^[0-9]+$/.test(val)){
			oShow.innerHTML="不能全为数字"
			oShow.style.display=""
			return false;
		}
	}
	if(key.indexOf("/H_capital/")>-1&&/[A-Z]/.test(val)){//有大写
		oShow.innerHTML="不能有大写"
		oShow.style.display=""
		return false
	}
	if(key.indexOf("/Mail domain/")>-1 && val != "" &&!(/([0-9a-z]+(([0-9a-z]*)|([0-9a-z-]*[0-9a-z]))+\.)+[a-z]{2,4}$/i).test(val)){//邮箱域
		oShow.innerHTML="邮箱域错误";
		oShow.style.display="";
		return false;
	}
	if(key.indexOf("/bmuncs/")>-1&&!/^[0-9*]*$/.test(val)){
		oShow.innerHTML="必须为数字"
		oShow.style.display=""
		return false
	}
	if(key.indexOf("/bmun/")>-1&&!/^[0-9]*$/.test(val)){
		oShow.innerHTML="必须为数字"
		oShow.style.display=""
		return false
	}
	/*if(key.indexOf("/英文数字/")>-1&&!/^[a-zA-Z0-9_]*$/.test(val)){
		oShow.innerHTML="只能为英文和数字"
		oShow.style.display=""
		return false
	}*/
	
	/*if(key.indexOf("/Mail all/")>-1 && val != "" &&!(/([0-9a-z]+(([0-9a-z]*)|([0-9a-z-]*[0-9a-z]))+\.)+[a-z]{2,4}$/i).test(val)){//邮箱
		oShow.innerHTML="Mail error";
		oShow.style.display="";
		return false;
	}*/
	
	if(key.indexOf("/Mail all/")>-1 && val != "" &&!(/^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.){1,4}[a-z]{2,4}$/i).test(val)){//邮箱
		oShow.innerHTML="请使用正确的邮箱格式";
		oShow.style.display="";
		return false;
	}
	
	if(key.indexOf("/Full-width/")>-1&&/[ａ-ｚＡ-Ｚ０-９！＠＃￥％＾＆＊（）＿＋｛｝［］｜：＂＇；．，／？＜＞｀～　]/.test(val))
	{//有全角
		oShow.innerHTML="不能有全角"
		oShow.style.display=""
		return false
	}
	if(key.indexOf("/H_Chinese/")>-1&&escape(val).indexOf("%u")>-1){//有汉字
		oShow.innerHTML="不能有汉字"
		oShow.style.display=""
		return false
	}
	if(key.indexOf("/Last-underline/")>-1&&val.slice(val.length-1)=="_"){//最后是不能下划线
		oShow.innerHTML="不能下划线结尾"
		oShow.style.display=""
		return false
	}
	if(key.indexOf("/Confirmusername/")>-1){
		if(obj.form[name].value!=val){
			oShow.innerHTML="用户名不一致"
			oShow.style.display=""
			return false
		}
	}
	if(key.indexOf("/Confirmpassword/")>-1){
		if(obj.form[name].value!=val){
			oShow.innerHTML="密码不一致"
			oShow.style.display=""
			return false
		}
	}
	/*if(key.indexOf("/必选/")>-1){
		var ol=obj.form[obj.name],isSel=false
		for(var i=0;i<ol.length;i++){
			if(ol[i].checked)
				isSel=true
		}
		if(!isSel){
			oShow.innerHTML=name+"必须选择"
			oShow.style.display=""
			return false
		}
	}*/
	if(key.indexOf("/Strange-characters-name/")>-1&&(/>|<|,|\_|\[|\]|\{|\}|\?|\/|\+|=|\||\'|\\|\"|:|;|\~|\!|\@|\#|\*|\$|\%|\^|\&|\(|\)|`/i).test(val)){
		oShow.innerHTML="请勿使用特殊字符"
		oShow.style.display=""
		return false
	}
	/*if(key.indexOf("/协议/")>-1){
		var ol=obj.form[obj.name],isSel=false
		if(ol.checked){
			isSel=true
		}
		if(!isSel){
			oShow.innerHTML=name+"必须选择"
			oShow.style.display=""
			return false
		}
	}*/
	/*if ( name=="u_password" || name=="Password"){
		ShowStrong('pswstrong',checkStrong(val),1);
	}*/
	return true
}
function strLen(key){
	var l=escape(key),len
	len=l.length-(l.length-l.replace(/\%u/g,"u").length)*4
	l=l.replace(/\%u/g,"uu")
	len=len-(l.length-l.replace(/\%/g,"").length)*2
	return len
}
function oo(obj){
	return document.getElementById(obj)
}

//密码强度
function CharMode(iN){
		if (iN>=65 && iN <=90) //大写字母
		return 2;
		if (iN>=97 && iN <=122) //小写
		return 4;
		else
		return 1; //数字
}
//计算出当前密码当中有多少种模式
function bitTotal(num){
		modes=0;
		for (i=0;i<3;i++){
				if (num & 1) modes++;
				num>>>=1;
		}
		return modes;
	}
//checkStrong函数 返回密码的强度级别
/*function checkStrong(sPW){
		Modes=0;
		for (i=0;i<sPW.length;i++){
				//测试每一个字符的类别并统计一共有多少种模式.
				Modes|=CharMode(sPW.charCodeAt(i));
		}
		var btotal = bitTotal(Modes);
		if (sPW.length >= 10) btotal++;
		switch(btotal) {
				case 1:
						return "<table width='130' class='f12'><tr><td bgcolor='#FE707E'><strong>弱</strong></td><td><span style='color:#666;'>中</span></td><td><span style='color:#666;'>强</span></td></tr></table>";
						break;
				case 2:
						return "<table width='130' class='f12'><tr><td bgcolor='#FCFA93'><span style='color:#666;'>弱</span></td><td bgcolor='#FCFA93'><strong>中</strong></td><td><span style='color:#666;'>强</span></td></tr></table>";
						break;
				case 3:
						return "<table width='130' class='f12'><tr><td bgcolor='#BDFEA6'><span style='color:#666;'>弱</span></td><td bgcolor='#BDFEA6'><span style='color:#666;'>中</span></td><td bgcolor='#BDFEA6'><strong>强</strong></td></tr></table>";
						break;
				default:
						return "<font color='#33CC00'>强</font>";
		}
}

function ShowStrong(obj,oMsg,status){
		var obj = oo(obj);
				if(status==1){
				obj.innerHTML = oMsg
		}else{
				obj.innerHTML = ""
		}
}*/

function setCurrency(s){
	s = String(s);
	if(s.indexOf('-')==0){
	   //计算负数
	   s= s.substring(1,s.lenght);
	   //alert("ddddd"+s);
	   if(/[^0-9\.\-]/.test(s)) return "invalid value";
	   s=s.replace(/^(\d*)$/,"$1.");
	
	   s=(s+"00").replace(/(\d*\.\d\d)\d*/,"$1");//取小数点后两位
	   s=s.replace(".",",");
	   var re=/(\d)(\d{3},)/;
	   while(re.test(s))
		s=s.replace(re,"$1,$2");
		s=s.replace(/,(\d\d)$/,".$1");//取小数点后两位
	   
	   return '-'+s.replace(/^\./,"0.")
	}else{
	   //计算正数
	   if(/[^0-9\.\-]/.test(s)) return "invalid value";
	   s=s.replace(/^(\d*)$/,"$1.");
	
	   s=(s+"00").replace(/(\d*\.\d\d)\d*/,"$1");//取小数点后两位
	   s=s.replace(".",",");
	   var re=/(\d)(\d{3},)/;
	   while(re.test(s))
		s=s.replace(re,"$1,$2");
		s=s.replace(/,(\d\d)$/,".$1");//取小数点后两位
	   
	   return s.replace(/^\./,"0.")
	}
}

function GY_Server(){
	var xmlHttp;
    if (window.ActiveXObject) {
        try {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e1) {
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
    }
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
	return xmlHttp;
}

/*
check(方式：get/post,页面及参数，接收返回赋值变量)
check("get","checkuser.php?username=user","u_user_c")
*/
function check(otype,topage,spanid){
	var obj = GY_Server();
	obj.open(otype,topage,true);
	obj.onreadystatechange = function()
	{
		if (obj.readyState == 4)
		{
			if (obj.status == 200)
			{
				document.getElementById(spanid).innerHTML = obj.responseText;
			}
		}
	}
	obj.send(null);
}

function promot(otype,topage){
	//alert('aa');
	var obj = GY_Server();
	obj.open(otype,topage,true);
	obj.onreadystatechange = function()
	{
		if (obj.readyState == 4)
		{
			if (obj.status == 200)
			{
				var total_y=Number((document.getElementById("total_y").innerHTML).replace(",",""));
				var yf=Number((document.getElementById("yf").innerHTML).replace(",",""));
				var PD;
				var promot_sqt = obj.responseText.split("|");
				if (obj.responseText){
					PD = Number(total_y-total_y*promot_sqt[1]/10);
					document.getElementById("PD").innerHTML = setCurrency(PD);
					document.getElementById("code_zt").innerHTML = "";
				}
				else{
					PD = 0;
					document.getElementById("PD").innerHTML = "0.00";
					document.getElementById("code_zt").innerHTML = "促销码不正确或超过使用次数";
				}
				document.cart.pr_code_ok.value = promot_sqt[0];
				var HM = Number((document.getElementById("HM").innerHTML).replace(",",""));
				var Ice = Number((document.getElementById("Ice").innerHTML).replace(",",""));
				
				var total_z = Number(total_y+yf+HM+Ice-PD);
				document.getElementById("total_z").innerHTML = setCurrency(total_z);
				//document.getElementById("total_z").innerHTML = obj.responseText;
			}
		}
	}
	obj.send(null);
}
