//检查关键字
var theForm;
var submit_flag;
var checkEn     = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var checkNum    = "0123456789";
var checkPrice  = "0123456780.";
var checkEnNum  = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var checkPhone  = "-0123456789,";



function Location(url)
{
    document.location.href=url;
}
//设置标记符号
function SetFlag(flag)
{
    submit_flag = flag;
}
//检查是否选择
function CountCheckBox(theForm)
{
	var c = 0;
	for (var i=0;i<theForm.elements.length;i++) 
	{
		var e = theForm.elements[i];
		if (e.checked == true && e.type=='checkbox')
			c = c + 1;
	}
	return c;
}
//选择所有
function SelALL(theForm)
{
    for (var i=0;i<theForm.elements.length;i++)
    {
        var e = theForm.elements[i];
        var n = e.name;
        if (e.type == "checkbox" && e.name != "ChkAll")
        {
            e.checked = theForm.ChkAll.checked;
        }
    }
}

//检查是否带有空格
function isWhiteWpace (s)
{
  var whitespace = " \t\n\r";
  var i;
  for (i = 0; i < s.length; i++){   
     var c = s.charAt(i);
     if (whitespace.indexOf(c) >= 0) {
		  return true;
	  }
   }
   return false;
}

//去除字符串空格
function TrimStr(str)
{
	var i   = 0;
	var len = str.length;
	if ( str == '' ) return( str );
	j = len -1;
	flagbegin   = true;
	flagend     = true;
	while ( flagbegin == true && i< len)
    {
		if ( str.charAt(i) == ' ' )
        {
			i=i+1;
			flagbegin   = true;
		}
        else
        {
			flagbegin   = false;
		}
	}
	while  (flagend== true && j>=0)
    {
		if (str.charAt(j)==' ')
        {
			j=j-1;
			flagend=true;
		}
        else
        {
			flagend=false;
		}
	}
	if ( i > j ) return ('')
		trimstr = str.substring(i,j+1);
	return trimstr;
}
//去除空格
function trim(inputString)
{

    if (typeof inputString != "string")
        return inputString;
        
    var retValue 	= inputString;
    var ch 			= retValue.substring(0, 1);
    while (ch == " ") 
    { 
        //检查字符串开始部分的空格
        retValue 	= retValue.substring(1, retValue.length);
        ch 			= retValue.substring(0, 1);
    }
    ch = retValue.substring(retValue.length-1, retValue.length);
    while (ch == " ") 
    {
        //检查字符串结束部分的空格
        retValue 	= retValue.substring(0, retValue.length-1);
        ch 			= retValue.substring(retValue.length-1, retValue.length);
    }
    while (retValue.indexOf("  ") != -1)
    { 
         //将文字中间多个相连的空格变为一个空格
        retValue 	= retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); 
    }
    return retValue;
}

//检查字符串
function chkString(checkStr, checkOK) 
{
  var allValid = true;
  if (typeof(checkStr) != "string" || typeof(checkOK) != "string") return(false);
  for (i = 0; i < checkStr.length; i++) {
    ch = checkStr.charAt(i);
    if (checkOK.indexOf(ch) == -1) {
      allValid = false;
      break;
    }
  }
  return(allValid);
}

//检查邮件地址
function isEmail (email)
{
     var regu   = '^(([0-9a-zA-Z]+)|([0-9a-zA-Z]+[_.0-9a-zA-Z-]*[0-9a-zA-Z]+))@([a-zA-Z0-9-]+[.])+([a-zA-Z]{2}|net|NET|com|COM|gov|GOV|mil|MIL|org|ORG|edu|EDU|info|biz)$';
     var re     = new RegExp(regu);
     return(email.search(re) != -1);
}

//判断是否选择
function SelectSome(theForm)
{
    for (var i=0;i<theForm.elements.length;i++)
    {
        var e = theForm.elements[i];
        if (e.checked == true && e.name != "ChkAll")
        {
            return true;
            break;
        }
    }
}

//检查品牌用户名是否存在
function CheckBranderExist(theForm)
{
    if (theForm.name.value == ""||theForm.name.value == null)
    {
        alert("请使用英文或数字填写登录名!");
        theForm.name.focus();
        return false;
    }
    if (isWhiteWpace(theForm.name.value))
    {
        alert("\请输入正确的用户名,用户名中不能包含空格！");
        theForm.name.focus();
        return false;
    }
    window.open("index.php?action=checkbrander&name="+theForm.name.value,"","height=100,width=200,left=200,top=150,resizable=no,scrollbars=no,status=no,toolbar=no,menubar=no,location=no");
}

//检查经销商用户名是否存在
function CheckDealerExist(theForm)
{
    if (theForm.name.value == ""||theForm.name.value == null)
    {
        alert("请使用英文或数字填写登录名!");
        theForm.name.focus();
        return false;
    }
    if (isWhiteWpace(theForm.name.value))
    {
        alert("\请输入正确的用户名,用户名中不能包含空格！");
        theForm.name.foucs();
        return false;
    }
    window.open("index.php?action=checkdealer&name="+theForm.name.value,"","height=100,width=200,left=200,top=150,resizable=no,scrollbars=no,status=no,toolbar=no,menubar=no,location=no");
}


//品牌商注册
function CheckPperReg(theForm)
{
    var flag    = false;
    if (!(chkString(theForm.name.value,checkEnNum)))
    {
        alert("请使用英文或数字填写登录名!");
        theForm.name.focus();
        return false;
    }
    if (isWhiteWpace(theForm.name.value))
    {
        alert("\请输入正确的用户名,用户名中不能包含空格！");
        theForm.name.focus();
        return false;
    }
    if (theForm.name.value.length<4||theForm.name.value.length>10)
    {
        alert("登录名字长度要大小3而小于10位！");
        theForm.name.focus();
        return false;
    }
    if (theForm.password.value.length<6 || theForm.password.value.length>10)
    {
        alert("密码长度要大于5位而小于10位！");
        theForm.password.focus();
        return false;
    }
    if (theForm.confirmpassword.value == "")
    {
        alert ("请填写确认密码！");
        theForm.confirmpassword.focus();
        return false;
    }
    if (theForm.password.value != theForm.confirmpassword.value)
    {
        alert("密码不一致！");
        theForm.password.focus();
        return false;
    }
    if (theForm.brand.value == "")
    {
        alert("请输入品牌名称！");
        theForm.brand.focus();
        return false;
    }
    if (theForm.checknum.value == "")
    {
        alert("请填写验证码");
        theForm.checknum.focus();
        return false;
    }
    if (confirm("确认提交吗？")) flag = true;
    return flag;
}

//经销商注册
function CheckDealerReg(theForm)
{
    var flag    = false;
    if (!(chkString(theForm.name.value,checkEnNum)))
    {
        alert("请使用英文或数字填写登录名!");
        theForm.name.focus();
        return false;
    }
    if (isWhiteWpace(theForm.name.value))
    {
        alert("\请输入正确的用户名,用户名中不能包含空格！");
        theForm.name.focus();
        return false;
    }
    if (theForm.name.value.length<4||theForm.name.value.length>10)
    {
        alert("登录名字长度要大小3而小于10位！");
        theForm.name.focus();
        return false;
    }
    if (theForm.password.value.length<6 || theForm.password.value.length>10)
    {
        alert("密码长度要大于5位而小于10位！");
        theForm.password.focus();
        return false;
    }
    if (theForm.confirmpassword.value == "")
    {
        alert ("请填写确认密码！");
        theForm.confirmpassword.focus();
        return false;
    }
    if (theForm.password.value != theForm.confirmpassword.value)
    {
        alert("密码不一致！");
        theForm.password.focus();
        return false;
    }
    if (theForm.checknum.value == "")
    {
        alert("请填写验证码");
        theForm.checknum.focus();
        return false;
    }
    if (confirm("确认提交吗？")) flag = true;
    return flag;
}

//经销商编辑资料
function CheckEditDealer(theForm)
{
    var flag    = false;
    if (theForm.linkman.value == "")
    {
        alert ("请填写联系人！");
        theForm.linkman.focus();
        return false;
    }
    if (theForm.shopname.value == "")
    {
        alert("请填写公司/店铺名称！");
        theForm.shopname.focus();
        return false;
    }
    if (theForm.password.value || theForm.confirmpassword.value)
    {
        if (theForm.password.value.length<6 || theForm.password.value.length>10)
        {
            alert("密码长度要大于5位而小于10位！");
            theForm.password.focus();
            return false;
        }
        if (theForm.confirmpassword.value == "")
        {
            alert ("请填写确认密码！");
            theForm.confirmpassword.focus();
            return false;
        }
        if (theForm.password.value != theForm.confirmpassword.value)
        {
            alert("密码不一致！");
            theForm.password.focus();
            return false;
        }
    }
    if (theForm.shopaddress.value == "")
    {
        alert ("请输入店铺/公司地址！");
        theForm.shopaddress.focus();
        return false;
    }
    if (theForm.postcode.value)
    {
        if (!(chkString(theForm.postcode.value,checkNum)))
        {
            alert("请输入正确的邮政编码！");
            theForm.postcode.focus();
            return false;
        }
    }
    if (theForm.phone.value == ""||!(chkString(theForm.phone.value,checkPhone)))
    {
        alert ("请输入正确格式的电话号码！");
        theForm.phone.focus();
        return false;
    }
    if (theForm.fax.value == ""||!(chkString(theForm.fax.value,checkPhone)))
    {
        alert ("请输入正确格式的传真号码！");
        theForm.fax.focus();
        return false;
    }
    if (!isEmail(theForm.email.value))
    {
        alert ("请输入正确的邮件地址！");
        theForm.email.focus();
        return false;
    }
    if (confirm("确定修改吗？"))flag = true;

    return flag;
}

//检查登录
function CheckLogin(theForm)
{
    if (theForm.name.value == "")
    {
        alert ("请输入用户名!");
        theForm.name.focus();
        return false;
    }
    if (theForm.password.value == "")
    {
        alert ("请输入密码!");
        theForm.password.focus();
        return false;
    }
    if (theForm.checknum.value == "")
    {
        alert("请输入验证码!");
        theForm.checknum.focus();
        return false;
    }
    return true;
}

//填写完整公司资料
function CheckEditBrander(theForm)
{
    var flag    = false;
    if (theForm.password.value || theForm.confirmpassword.value)
    {
        if (theForm.password.value.length<6 || theForm.password.value.length>10)
        {
            alert("密码长度要大于5位而小于10位！");
            theForm.password.focus();
            return false;
        }
        if (theForm.confirmpassword.value == "")
        {
            alert ("请填写确认密码！");
            theForm.confirmpassword.focus();
            return false;
        }
        if (theForm.password.value != theForm.confirmpassword.value)
        {
            alert("密码不一致！");
            theForm.password.focus();
            return false;
        }
    }
    if (theForm.compname.value == "")
    {
        alert("请填写公司名称！");
        theForm.compname.focus();
        return false;
    }
    if (theForm.compaddress.value == "")
    {
        alert ("请输入公司地址！");
        theForm.compaddress.focus();
        return false;
    }
    if (theForm.postcode.value == "" ||!(chkString(theForm.postcode.value,checkNum)))
    {
        alert("请输入正确的邮政编码！");
        theForm.postcode.focus();
        return false;
    }
    if (theForm.phone.value == ""||!(chkString(theForm.phone.value,checkPhone)))
    {
        alert ("请输入正确格式的电话号码！");
        theForm.phone.focus();
        return false;
    }
    if (theForm.fax.value == ""||!(chkString(theForm.fax.value,checkPhone)))
    {
        alert ("请输入正确格式的传真号码！");
        theForm.fax.focus();
        return false;
    }
    if (!isEmail(theForm.email.value))
    {
        alert ("请输入正确的邮件地址！");
        theForm.email.focus();
        return false;
    }
    if (theForm.logo.value)
    {
        if (!CheckPhoto(theForm.logo))
        {
            alert("请输入正确格式的图片!");
            theForm.logo.focus();
            return false;
        }
    }
    if (theForm.lawman.value == "")
    {
        alert("请输入法人！");
        theForm.lawman.focus();
        return false;
    }
    if (theForm.certno.value == "")
    {
        alert ("请输入营业执照证号!");
        theForm.certno.focus();
        return false;
    }
    if (theForm.brand1.value == "" && theForm.brand2.value == ""&& theForm.brand3.value == "")
    {
        alert("至少要输入一个品牌名称！");
        theForm.brand1.focus();
        return false;
    }
    for (var i =0; i<theForm.AddClassList.length;i++)
    {
        theForm.AddClassList.options[i].selected = true;
    }
    if (theForm.AddClassList.value == "")
    {
        alert("请选择公司主要产品类型!");
        theForm.AllClassList.focus();
        return false;
    }
    if (theForm.compproduct.value == "")
    {
        alert ("请填写公司主要产品！");
        theForm.compproduct.focus();
        return false;
    }
    if (theForm.compintro.value == "")
    {
        alert("请填写公司简介！");
        theForm.compintro.focus();
        return false;
    }
    if (confirm("确定资料正确了吗？")) flag = true;
    return flag;
}



//品牌商添加产品分类
function CheckAddClass(theForm)
{
    var flag = false;
    if (theForm.cateid.value == "")
    {
        alert ("请选择一级分类!");
        theForm.cateid.focus();
        return false;
    }
    if (theForm.classid.options.length>1)
    {
        if (theForm.classid.value == "")
        {
            alert ("请选择二级分类");
            theForm.classid.focus();
            return false;
        }
    }
    if (theForm.classname.value == "")
    {
        alert ("请填写分类名称!");
        theForm.classname.focus();
        return false;
    }
    if (confirm("确认添加吗？")) flag =true;
    return flag;
}

function CheckEditClass(theForm)
{
    var flag = false;
    if (theForm.classname.value == "")
    {
        alert ("请填写分类名称!");
        theForm.classname.focus();
        return false;
    }
    if (confirm("确认修改吗？")) flag =true;
    return flag;
}
//品牌商管理产品分类
function ManageProClass(theForm)
{
    var flag = false;
	if(CountCheckBox(theForm)<1)
	{
		alert('请选择您要编辑的产品分类！');
		return false;
	}
	else if(CountCheckBox(theForm)>1)
	{
		alert('请选择一个产品分类进行编辑！');
		return false;
	}
    else if(submit_flag == "dele")
    {
        if(confirm("你确定删除吗?")) flag=true;
    }
    else if (submit_flag == "edit")
    {
        flag = true;
    }
    return flag;
}
//检查上传图片格式
function CheckImg(theForm)
{
    var validType	= new Array("jpg","jpeg","gif","png");
    var i			= theForm.upload_file.value.lastIndexOf(".")+1;
	var clientFile	= theForm.upload_file.value.substring(i).toLowerCase();
	var upload		= false;
    for (var j=0;j<validType.length;j++)
    {
        if (clientFile  == validType[j])
        {
            upload	= true;
            break;
        }
	}
    return upload;
}

function CheckUploadImg(theForm)
{
    var flag = false;
    if (!CheckImg(theForm))
    {
        alert("图片格式不符合!");
        theForm.upload_file.focus();
        return false;
    }
    if (confirm("你确定上传吗?")) flag = true;
    return flag;
}

function CheckPhoto(theFormImg)
{
    var validType	= new Array("jpg","jpeg","gif","png");
    var i			= theFormImg.value.lastIndexOf(".")+1;
	var clientFile	= theFormImg.value.substring(i).toLowerCase();
	var upload		= false;
    for (var j=0;j<validType.length;j++)
    {
        if (clientFile  == validType[j])
        {
            upload	= true;
            break;
        }
	}
    return upload;
}

//价格方式
function ChangePriceType(TheForm)
{
    var fag = false;
    if (TheForm.pricetype[0].checked == true)
        TheForm.price.disabled = true;
    else
    {
        TheForm.price.disabled = false;
        fag = true;
    }
    return fag;
}

//添加产品信息
function CheckAddPro(theForm)
{
    var flag = false;
    if (theForm.cateid.value == "")
    {
        alert("请选择一级分类!");
        theForm.cateid.focus();
        return false;
    }
    if (theForm.classid.value == "")
    {
        alert ("请选择二级分类!");
        theForm.classid.focus();
        return false;
    }
    if (theForm.sortid.value == "")
    {
        alert ("请选择三级分类");
        theForm.sortid.focus();
        return false;
    }
    if (theForm.proname.value == "")
    {
        alert ("请输入产品名称！");
        theForm.proname.focus();
        return false;
    }
    if (theForm.proarea.value == "")
    {
        alert ("请填写产地!");
        theForm.proarea.focus();
        return false;
    }
    if (theForm.brand.value == "")
    {
        alert ("请选择品牌");
        theForm.brand.focus();
        return false;
    }
    if (ChangePriceType(theForm))
    {
        if (theForm.price.value == ""||!(chkString(theForm.price.value,checkPrice)))
        {
            alert("请用数字填写正确的价格!");
            theForm.price.focus();
            return false;
        }
    }
    if (!CheckPhoto(theForm.prophoto))
    {
        alert("图片格式不正确!");
        theForm.prophoto.focus();
        return false;
    }
    if (theForm.prointro.value == "")
    {
        alert("请填写产品介绍!");
        theForm.prointro.focus();
        return false;
    }
    if (confirm("你确定添加吗?")) flag = true;
    return flag;
}
//编辑产品信息
function CheckEditPro(theForm)
{
    var flag = false;
    if (theForm.cateid.value == "")
    {
        alert("请选择一级分类!");
        theForm.cateid.focus();
        return false;
    }
    if (theForm.classid.value == "")
    {
        alert ("请选择二级分类!");
        theForm.classid.focus();
        return false;
    }
    if (theForm.sortid.value == "")
    {
        alert ("请选择三级分类");
        theForm.sortid.focus();
        return false;
    }
    if (theForm.proname.value == "")
    {
        alert ("请输入产品名称！");
        theForm.proname.focus();
        return false;
    }
    if (theForm.proarea.value == "")
    {
        alert ("请填写产地!");
        theForm.proarea.focus();
        return false;
    }
    if (theForm.brand.value == "")
    {
        alert ("请选择品牌");
        theForm.brand.focus();
        return false;
    }
    if (ChangePriceType(theForm))
    {
        if (theForm.price.value == ""||!(chkString(theForm.price.value,checkPrice)))
        {
            alert("请用数字填写正确的价格!");
            theForm.price.focus();
            return false;
        }
    }
    if (!CheckPhoto(theForm.prophoto))
    {
        alert("图片格式不正确!");
        theForm.prophoto.focus();
        return false;
    }
    if (theForm.prointro.value == "")
    {
        alert("请填写产品介绍!");
        theForm.prointro.focus();
        return false;
    }
    if (confirm("确定修改吗?")) flag = true;
    return flag;
}

function CheckProManage(theForm)
{
    var flag = false;

    if(submit_flag == "submit")
    {
        return true;
    }
    if(!SelectSome(theForm))
    {
        alert("你还没有选中任何内容!");
        return false;
    }
    if(submit_flag == "dele")
    {
        if(confirm("你确定删除所选产品吗?!")) flag = true;
    }
    return flag;
}

//品牌商添加商情
function CheckAddBranderNews(theForm)
{
    var flag = false;
    if (theForm.title.value == "")
    {
        alert("请输入标题!");
        theForm.title.focus();
        return false;
    }
    if (theForm.content.value == "")
    {
        alert ("请输入内容！");
        theForm.content.focus();
        return false;
    }
    if (confirm("确定添加吗？")) flag = true;
    return flag;
}

//品牌商管理商情
function CheckBranderManageNews(theForm)
{
    var flag = false;

    if(submit_flag == "submit")
    {
        return true;
    }
    if(!SelectSome(theForm))
    {
        alert("你还没有选中任何内容!");
        return false;
    }
    if(submit_flag == "dele")
    {
        if(confirm("你确定删除所选吗?!")) flag = true;
    }
    return flag;
}

//编辑品牌商商情
function CheckEditBranderNews(theForm)
{
    var flag = false;
    if (theForm.title.value == "")
    {
        alert("请输入标题!");
        theForm.title.focus();
        return false;
    }
    if (theForm.content.value == "")
    {
        alert ("请输入内容！");
        theForm.content.focus();
        return false;
    }
    if (confirm("确定修改吗？")) flag = true;
    return flag;
}

//品牌商留言管理
function CheckBranderMessage(theForm)
{
    var flag = false;

    if(submit_flag == "submit")
    {
        return true;
    }
    if(!SelectSome(theForm))
    {
        alert("你还没有选中任何内容!");
        return false;
    }
    if(submit_flag == "dele")
    {
        if(confirm("你确定删除所选留言吗?")) flag = true;
    }
    return flag;
}

//经销商提交加盟申请
function CheckAddJoin(theForm)
{
    var flag    = false;
    if (theForm.title.value == "")
    {
        alert("请填写标题!");
        theForm.title.focus();
        return false;
    }
    if (theForm.request.value == "")
    {
        alert("请填写内容！");
        theForm.request.focus();
        return false;
    }
    if (confirm("确定提交申请吗？"))flag = true;
    return flag;
}

//管理员审核经销商申请
function CheckDealerJoinList(theForm)
{
    var flag = false;

    if(!SelectSome(theForm))
    {
        alert("你还没有选中任何内容!");
        return false;
    }
    if(submit_flag == "dele")
    {
        if(confirm("你确定删除所选吗?")) flag = true;
    }
    return flag;
}
//品牌商同意经销商申请
function BranderAgreeJoin(theForm)
{
    var flag = false;

    if(!SelectSome(theForm))
    {
        alert("你还没有选中任何内容!");
        return false;
    }
    if(submit_flag == "ok")
    {
        if(confirm("你确定同意所选的经销商加盟吗?")) flag = true;
    }
    if (submit_flag == "refuse")
    {
        if(confirm("你确定拒绝所选的经销商加盟吗?")) flag = true;
    }
    return flag;
}

//品牌商拒绝经销商加盟
function BranderRefuseJoin(theForm)
{
    var flag = false;
    if (TrimStr(theForm.content.value) == "")
    {
        alert("请填写拒绝理由！");
        theForm.content.focus();
        return false;
    }
    if (confirm("确定提交吗？"))flag = true;
    return flag;
}