﻿/**
 * CommonCheck.js
 * 用于页面共通校验
 * 2008-05-07 dengjs by YuanTel
 */

//**********************************************************
//* 全局变量定义区域
//**********************************************************

//**********************************************************
//* 正则表达式
//**********************************************************
// 国内邮政编码
var zipRegex = /\d{6}/;

// 移动电话(13xxxxxxxxx,15xxxxxxxxx)，支持3G（18*系列号）以及天翼号
//var mobileRegex =  /^(((13[0-9]{1})|(15[0-9]{1}))+\d{8})$/;
var mobileRegex =  /^(1{1}\d{10})$/;

//固定电话或传真(3位或4位区号+‘-或没有’+7位或8位号码+1-8位分机号或没有)
//如：01012345678、010-12345678、0101234567、010-1234567、071612345678、0716-12345678、07161234567、0716-12345678
//    01012345678,0001、010-12345678,0001都是合法的
var teleRegex = /^(((0\d{2,3})((-)|()))\d{7,8})(($)|(,\d{1,8}$))/;

//网址
//如：http://www.fax99.com https://www.fax99.com ftp://www.fax99.com http://mail.fax99.com
//    http://www.fax99-fax99.com http://www.fax99.fax99.com http://www.fax99_fax99.com都是合法的。
var urlRegex = /^((http|https|ftp):(\/\/|\\\\)|())(((\w)|(-)(.))+[.]){1,}(com|net|cn|org|edu|gov|mil|arts|web)$/;

//Email
//第一位必须是字母，数字或_，中间可以包含字母，数字，_，.，-，必须有‘@’，‘@’以后可以包含字母，数字，_，-，
//然后是‘.’，‘.’后面只能是字母
//如：djs-007.1@djs-007_1.com
//var emailRegex = /^(\w)+((\w)|(-)|(.))+(@+((\w)|(-))+.)+((\w)|(-))/;
//var emailRegex = /^((\w)|-)+(@+)((\w)|-)+.+((\w)|-)+$/;
//var emailRegex = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{1,}){1,2})$/;
var emailRegex = /^([a-zA-Z0-9_\-])+([a-zA-Z0-9_\-\.])*@([a-zA-Z0-9_\-])+([a-zA-Z0-9_\-\.])*((\.[a-zA-Z0-9_\-]{1,}){1,})$/;

//身份证号 15位或18位数字 待修改
var idCardRegex = /^[1-9]{1}+[0-9]{13}?(([0-9X]$)|((\d){3}([0-9X])$))/;

var OnlyNum = /^[\d]+$/;                //纯数字

//**********************************************************
//* 共通方法区域
//**********************************************************

$ = function(obj){return document.getElementById(obj);}

//返回浏览历史页面
//Index -1：前一个页面；-2：前2个页面；依次类推
function GoHistory(Index)
{
    window.history.back(Index);
}

//验证失败时对象被选中，背景变色
//CheckedObj    要验证的对象
//DivObj        显示提醒信息的DIV
//ShowObj       显示提示信息的对象
//ShowMsg       显示的提示信息
function Failure(CheckedObj, DivObj, ShowObj, ShowMsg)
{
    if(CheckedObj != null)
    {
        CheckedObj.focus();
        CheckedObj.select();
        CheckedObj.style.backgroundColor="red";
    }

    if(ShowObj != null)
    {
        DivObj.style.display = "";
        ShowObj.innerHTML = ShowMsg;
    }
}

function Failure_1(CheckedObj, DivObj, ShowObj, ShowMsg)
{
    if(CheckedObj != null)
    {
        CheckedObj.focus();
        CheckedObj.style.backgroundColor="red";
    }

    if(ShowObj != null)
    {
        DivObj.style.display = "";
        ShowObj.innerHTML = ShowMsg;
    }
}

//验证失败时对象被选中，背景变色
//CheckedObj    要验证的对象
//ShowObj       显示提示信息的对象
function Success(CheckedObj, DivObj, ShowObj)
{
    if(CheckedObj != null)
    {
        CheckedObj.style.backgroundColor="";
    }
    
    if(ShowObj != null)
    {
        DivObj.style.display = "none";
        ShowObj.innerHTML = "";
    }
}

//检查输入的是否是在[min, max]之间的数字
function CheckNum(str, min, max)
{
    if(!OnlyNum.test(str) || parseInt(str) < min || parseInt(str) > max || parseInt(str).toString().length < str.length)
    {
        return true;
    }
    else
    {
        return false;
    }
}

//让对象显示或不显示
function DisPlayWarmInfo(obj)
{
    if(obj != null)
    {
        if(obj.style.display == "none")
        {
            obj.style.display = "";
        }
        else
        {
            obj.style.display = "none";
        }
    }
}

//初始化下拉框对象
function InitSelectOpt(opt)
{
    opt.length=0;
    opt.options.add(new Option("- 请选择","0"));
}

//检查密码是否是数字+字符的组合
//计算输入密码中数字和非数字字符的个数，如果个数都大于0，表示是数字+字符的组合，返回true；否则不是，返回false。
function CheckPassword(password)
{
    var count = password.length;
    var numCount = 0;
    var charCount = 0;
    for(i=0; i<count; i++)
    {
        if(OnlyNum.test(password.charAt(i)))
        {
            numCount = numCount + 1;
        }
        else
        {
            charCount = charCount + 1;
        }
    }
    if(numCount != 0 && charCount != 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

//返回字符串真实的长度（全角字符算2个半角字符计算）
function GetCharLength(str)
{
	return str.replace(/[^\x00-\xff]/g,"**").length;
}

//判断str中是否包含有specialCharacter中的字符
//specialCharacter为特殊(非法)字符
function contain(str, specialChar)
{ 
    var i;
    for(i=0;i<specialChar.length;i++){
        if(str.indexOf(specialChar.charAt(i)) >= 0)
        {
            return true;
        }
    }
}

//检查输入的字符串是否合法
function CheckChar(CheckedObj, MaxLength)
{
    var len = CheckedObj.length;
    //判断字符串的长度
    if (GetCharLength(CheckedObj) > MaxLength)
    {
        return "只能输入" + MaxLength + "个半角字符或" + Math.round(MaxLength/2)  +"个全角字符";
    }
    //如果有特殊字符
    var specialChar = "\%\~\*\<\>\`\&\'\"";
    if(contain(CheckedObj, specialChar))
    {
        return "不能输入如：" + "【\%】【\~】【\*】【\<】【\>】【\`】【\&】【\'】【\"】" + "特殊字符";
    }
    return "";
}