/** 
    @ Name : Careff Javascript Library 
    @ Author : 
    @ Date : 2018-12-04
    @ Email : 
    @ Version : 1.0 
    @ Update : 
    功能： 开始建立自己的JS类库，需要按照功能进行分类；
     */
(function () {
    if (!window.ushscard) {
        window.ushscard = {};
    };
    window.ushscard = {
        NAME: "ushscard",
        VERSION: '1.0',
        DATE: '2018-12-04',
        INDEX: 10,
        carderror: "系统目前仅支持IE内核的浏览器使用身份证读卡器；读卡器型号：华视电子CVR-100U，请使用IE浏览器，并安装华视的驱动！",
        cardread: function () {
            var Card = document.getElementById("CVR_IDCard");
            var strReadResult = "";
            try {
                strReadResult = Card.ReadCard();
            }
            catch (err) {
                strReadResult = "1";
            }
            if (strReadResult == "0") {

                ushscard.cardformat(Card);
            }
            else if (strReadResult == "1") {

                alert(ushscard.carderror);
            }
            else {
                alert(strReadResult + "请把身份证离开读卡器后，在重新放到读卡器上面！");
            }
        },
        cardformat: function (Card) {
            var data = "";
            
            //encodeURIComponent(Card.Picture)
            var data = "{'name':'" + Card.Name + "','sex':'" + Card.Sex + "','nation':'" + Card.Nation + "','born':'" + Card.Born
                + "','address':'" + Card.Address + "','cardno':'" + Card.CardNo + "','issuedat':'" + Card.IssuedAt + "','effecteddate':'" + Card.EffectedDate
                + "','expireddate':'" + Card.ExpiredDate + "','samid':'" + Card.SAMID + "','picture':'" + (Card.Picture) + "'}";
            ushttp.UsAjaxJSONV2("/usbasic/skystudent/usgetusercard", data, true, true, function (config) {
                var datajson = data;

                var age = Card.Born;
                age = age.replace("年", "-").replace("月", "-").replace("日", "");

                var datajson = "{'name':'" + Card.Name + "','sex':'" + Card.Sex + "','nation':'" + Card.Nation + "','born':'" + age
                + "','address':'" + Card.Address + "','cardno':'" + Card.CardNo + "','issuedat':'" + Card.IssuedAt + "','effecteddate':'" + Card.EffectedDate
                + "','expireddate':'" + Card.ExpiredDate + "','samid':'" + Card.SAMID
                + "','picture':'" + config.imgpath + "','provid':'" + config.provid + "','cityid':'" + config.cityid + "','areaid':'" + config.areaid + "','imgpath':'" + config.imgpath + "'}";
                //console.debug(datajson);
                var json = eval('(' + datajson + ')');
                //console.debug(json.name);

                //读卡完成
                //function HSCardFinish(card) {
                //    //姓名：name 性别：sex 民族：nation 出生日期：born 住址：address 身份证：cardno 签发机关：issuedat 有效期开始：effecteddate 有效期结束：expireddate 
                //    //卡片号：samid 省份代码：provid 市：cityid 区县：areaid 头像地址：imgpath
                //}

                //外部回调
                if (typeof (cardfinish) == "function") {
                    eval("cardfinish(json)");
                }
            });
        }
        , cardselect: function (selectId, checkValue) {
            var select = document.getElementById(selectId);
            for (var i = 0; i < select.options.length; i++) {
                var txt = select.options[i].text;
                txt = txt.replace("族", "");
                if (txt == checkValue) {
                    select.options[i].selected = true;
                    break;
                }
            }
        }
    };

})();