var issOnlineUrl = "http://127.0.0.1:24010/ZKIDROnline";
var browserFlag = getBrowserType();
/*
$().ready(function(){
	$(document).off("DOMNodeInserted","#cert_message_type");
	$("#cert_message_type").on("DOMNodeInserted",function(e){
		openMessage($("#cert_message_type").text(), $("#cert_message").text());
	});
});
*/
//身份证
//includeScript("baseISSObject.js", function(){
//includeScript("baseISSOnline.js", function(){
var setting = {
    Cert : {
        callBack : function(result){
            setCertificateData(result);
        },
        select : "#button_readID"
    },
    Methods : {
        showMessage : function(type,message){
            if(type=="warning"||type=="error"){
                alert(message);
            }
            $("#cert_message").text(message);
            $("#cert_message_type").text(msgType[type]);
        }
    }
}
createISSonlineDevice(setting);
//});
//});


function setCertificateData(result)
{
    // imgData =result.Certificate.Base64Photo;
    //
    // document.all("photoId").src = IDCard.getViewBase64(imgData);
    // var pic = imgData;
    // var domain = 'http://capture2.zzwtec.com/';
    // var encrypt_data = iDNumberEncrypt(result.Certificate.IDNumber);
    // var key = "hid_"+encrypt_data+".jpg"

    clearHead();

    var picData = result.Certificate.Base64Photo;
    var encrypt_data = iDNumberEncrypt(result.Certificate.IDNumber);
    var fileName="hid_"+encrypt_data+".jpg";
    var fileType="jpg";

    var data={};
    data.picData=picData;
    data.fileName=fileName;
    data.fileType=fileType;
    doDataPost(data,false,ctx+"/common/file/upload/fastdfs",function(repJson){
        if(repJson.code==200){
            $("#photoId").attr("src",repJson.data.uploadFileUrl);
            $("#fileId").val(repJson.data.uploadFileStoreId);
            $("#headUrl").val(repJson.data.uploadFileUrl);
        }

    });
    uploadCallBack(result);


    if(result.code == 0){
        $('#readcard').css('display','none');
    }
}



/**
 * 上传身份证头像回调,暂时只考虑上传成功
 */
function uploadCallBack(result){

    //姓名
    var sex = -1;
    if('男' == result.Certificate.Sex){
        sex = 1;
    }else if('女' == result.Certificate.Sex){
        sex = 2;
    }
    var name = formName();
    console.log(name);
    //为UI文本框赋值
    $("form[name='visitor_add_from'] input[name='name']").val(result.Certificate.Name);
    $("form[name='visitor_add_from'] input[name='sexStr']").val(result.Certificate.Sex);
    $("form[name='visitor_add_from'] input[name$=sex]").val(sex);
    $("form[name='visitor_add_from'] input[name='nation']").val(result.Certificate.Nation);
    $("form[name='visitor_add_from'] input[name='birthday']").val(result.Certificate.Birthday.replace(/\./g,"-").substr(0,10));
    $("form[name='visitor_add_from'] input[name='domicileAddress']").val(result.Certificate.Address);
    $("form[name='visitor_add_from'] input[name='idNum']").val(result.Certificate.IDNumber);




    $("form[name='auth_add_from'] input[name=img]").val($(this).getFileStorId());
    $("form[name='auth_add_from'] input[name=name]").val(result.Certificate.Name);
    $("form[name='auth_add_from'] input[name=jpgPhoto]").val(result.jpgPhoto);
    $("#jpgPhoto").val(result.jpgPhoto);
    $("form[name='auth_add_from'] input[name$=sex1]").val(result.Certificate.Sex);
    $("form[name='auth_add_from'] input[name$=sex]").val(sex);
    $("form[name='auth_add_from'] input[name$=nation]").val(result.Certificate.Nation);
    $("form[name='auth_add_from'] input[name$=birthday]").val(result.Certificate.Birthday.replace(/\./g,"-").substr(0,10));
    $("form[name='auth_add_from'] input[name$=domicileAddress]").val(result.Certificate.Address);
    $("form[name='auth_add_from'] input[name$=idNum]").val(result.Certificate.IDNumber);
    $("form[name='auth_add_from'] input[name$=dept]").val(result.Certificate.IDIssued);
    $("form[name='auth_add_from'] input[name$=effdate]").val(result.Certificate.IssuedData);
    $("form[name='auth_add_from'] input[name$=expdate]").val(result.Certificate.ValidDate);



    //为UI文本框赋值
    $("form[name='auth_view_from'] input[name$=sex1]").val(result.Certificate.Sex);
    $("form[name='auth_view_from'] input[name$=sex]").val(sex);
    $("form[name='auth_view_from'] input[name$=nation]").val(result.Certificate.Nation);
    $("form[name='auth_view_from'] input[name$=birthday]").val(result.Certificate.Birthday.replace(/\./g,"-").substr(0,10));
    $("form[name='auth_view_from'] input[name$=domicileAddress]").val(result.Certificate.Address);
    $("form[name='auth_view_from'] input[name$=idNum]").val(result.Certificate.IDNumber);
    $("form[name='auth_view_from'] input[name$=dept]").val(result.Certificate.IDIssued);
    $("form[name='auth_view_from'] input[name$=effdate]").val(result.Certificate.IssuedData);
    $("form[name='auth_view_from'] input[name$=expdate]").val(result.Certificate.ValidDate);
    $("form[name='auth_view_from'] input[name$=name]").val(result.Certificate.Name);
    $("form[name='auth_view_from'] input[name=img]").val($(this).getFileStorId());
    console.log($(this).getFileStorId());


    var text = formName();

    if(text == "auth_add_from"){
        //通知验证框架
        var bootstrapValidator = $("form[name='auth_add_from']").data('bootstrapValidator');
        bootstrapValidator.updateStatus('name', 'NOT_VALIDATED').validateField('name');
        bootstrapValidator.updateStatus('sex1', 'NOT_VALIDATED').validateField('sex1');
        bootstrapValidator.updateStatus('nation', 'NOT_VALIDATED').validateField('nation');
        bootstrapValidator.updateStatus('birthday', 'NOT_VALIDATED').validateField('birthday');
        bootstrapValidator.updateStatus('domicileAddress', 'NOT_VALIDATED').validateField('domicileAddress');
        bootstrapValidator.updateStatus('idNum', 'NOT_VALIDATED').validateField('idNum');
        bootstrapValidator.updateStatus('dept', 'NOT_VALIDATED').validateField('dept');
        bootstrapValidator.updateStatus('effdate', 'NOT_VALIDATED').validateField('effdate');
        bootstrapValidator.updateStatus('expdate', 'NOT_VALIDATED').validateField('expdate');
    }else  if(text == "visitor_add_from"){
        var bootstrapValidator = $("form[name='visitor_add_from']").data('bootstrapValidator');
        bootstrapValidator.updateStatus('name', 'NOT_VALIDATED').validateField('name');
        bootstrapValidator.updateStatus('idNum', 'NOT_VALIDATED').validateField('idNum');
        bootstrapValidator.updateStatus('domicileAddress', 'NOT_VALIDATED').validateField('domicileAddress');
        bootstrapValidator.updateStatus('nation', 'NOT_VALIDATED').validateField('nation');
        bootstrapValidator.updateStatus('sexStr', 'NOT_VALIDATED').validateField('sexStr');
    }else if(text == "auth_view_from"){
        //通知验证框架
        var bootstrapValidator = $("form[name='auth_view_from']").data('bootstrapValidator');
        bootstrapValidator.updateStatus('name', 'NOT_VALIDATED').validateField('name');
        bootstrapValidator.updateStatus('sex1', 'NOT_VALIDATED').validateField('sex1');
        bootstrapValidator.updateStatus('nation', 'NOT_VALIDATED').validateField('nation');
        bootstrapValidator.updateStatus('birthday', 'NOT_VALIDATED').validateField('birthday');
        bootstrapValidator.updateStatus('domicileAddress', 'NOT_VALIDATED').validateField('domicileAddress');
        bootstrapValidator.updateStatus('idNum', 'NOT_VALIDATED').validateField('idNum');
        bootstrapValidator.updateStatus('dept', 'NOT_VALIDATED').validateField('dept');
        bootstrapValidator.updateStatus('effdate', 'NOT_VALIDATED').validateField('effdate');
        bootstrapValidator.updateStatus('expdate', 'NOT_VALIDATED').validateField('expdate');
    }
}

function  clearHead() {
    var data={};
    var fileStoreId=$("#fileId").val();
    var fileUrl=$("#headUrl").val();

    if(fileStoreId!=""|| fileUrl!="" ){
        data.fileStoreId=fileStoreId;
        data.fileUrl=fileUrl;
        doDataPost(data,false,ctx+"/common/file/del/fastdfs",function(repJson){

        });
    }


}

function getRandomNum()
{
    var random = parseInt(Math.random() * 10000);
    return random;
}

//消息控件的使用类型的类
var msgType =
    {
        info : "info",
        success : "success",
        warning : "warning",
        error : "error",
        loading : "loading"
    };

function getBrowserType()
{
    var browserFlag = "";
    //是否支持html5的cors跨域
    if (typeof(Worker) !== "undefined")
    {
        browserFlag = "html5";
    }
    //此处判断ie8、ie9
    else if(navigator.userAgent.indexOf("MSIE 7.0")>0||navigator.userAgent.indexOf("MSIE 8.0")>0 || navigator.userAgent.indexOf("MSIE 9.0")>0)
    {
        browserFlag = "simple";
    }
    else
    {
        browserFlag = "upgradeBrowser";//当前浏览器不支持该功能,请升级浏览器
    }
    return browserFlag;
}


function openMessage(type, text, ptimeout)
{
    text = (text == "" ? null : text);
    if(type == msgType.warning || type == msgType.info)//警告
    {
    }
    else if(type == msgType.success)//成功
    {

        text = (text && text != null ? text : "操作成功");//${common_op_succeed}:操作成功
        var num = strlen(text)/30;
        num = num > 8 ? 8 : num;
    }
    else if(type == msgType.error)//失败
    {
        text = (text && text != null) ? text : "操作失败";//${common_op_failed}:操作失败，程序出现异常
    }
    else if(type == msgType.loading)//处理中
    {
        text = text && text != null ? text : "处理中";//${common_op_processing}:处理中
    }
    myAlert(text);
}


function closeMessage() {}

function strlen(str)
{
    var len = 0;
    if(str != null)
    {
        for (var i=0; i<str.length; i++)
        {
            var c = str.charCodeAt(i);
            if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f))
            {
                len++;
            }
            else
            {
                len+=2;
            }
        }
    }
    return len;
}

function tipDownloadDrive(){
    myAlert(
        '请根据实际情况安装相关硬件驱动！安装完成后请重启电脑<br>'+
        '<a style="color:#337ab7;" href="http://files.zzwtec.com/HXJ15SGoogleControlInstaller.zip" target="_blank">点击下载华旭身份证读卡器驱动</a><br>'+
        '<a style="color:#337ab7;" href="http://files.zzwtec.com/ZKIDROnline.exe" target="_blank">点击下载中控身份证读卡器驱动</a>',
        300,
        200);
}
function iDNumberEncrypt(idNumber){
    var encrypt_data = xxtea.encrypt(xxtea.toBytes(idNumber),xxtea.toBytes("zzwtec"));
    encrypt_data = btoa(String.fromCharCode.apply(String, encrypt_data));
    return encrypt_data;
}


function formName(){
    if($("form[name='visitor_add_from'] input[name='idNum']").val()!=null){
        return "visitor_add_from";
    }else if($("form[name='auth_add_from'] input[name='idNum']").val()!=null){
        return "auth_add_from";
    }else if($("form[name='auth_view_from'] input[name='idNum']").val()!=null){
        return "auth_view_from";
    }
    return null;
}