//JS操作cookies方法!
//写cookies
function setCookie(name, value) {
    // var Days = 30;
    var exp = new Date();
    exp.setTime(exp.getTime() + 5 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}
function setCookieEx(name, value) {
    var Days = 7;
    var exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}
function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return null;
}
function delCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null)
        document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}

//接收winform消息
function winformmessage(type, msg) {
    //  alert(type + ":" + msg);
    switch (type) {
        case "startservice":
            startservice(msg);
            break;
        case "vaildsuccess":
            printpage(msg);
            break;
        case "vaildtranw":
            vaildtranw(msg);
            break;
        case "printsuccess":
            printsuccesspage(msg);
            break;
        case "printfail":
            printfailpage(msg);
            break;
        case "qrfail":
            qrfailpage(msg);
            break;
        case "barcodefail":
            barcodefailpage(msg);
            break;
        case "lastpages":
            setCookieEx("lastpages", msg);
            break;
        case "stopservice":
            stopservice(msg);
            break;
        case "accountname":
            //  alert(msg);
            document.getElementById('accountname').innerHTML = msg;
            break;
        default:
            qrfailpage(msg);
            break;
    }

}

//调用WINFORM 事件
function scancodetype(codetype) {

    // alert(codetype);
    window.external.scancodetype(codetype);
}

function logowhichbutton(event) {
    var btnNum = event.button;
    if (btnNum == 2) {
        //          alert("您点击了鼠标右键！")
        window.external.jsmmessage("logorightcheck", "控制菜单");
    }
    else if (btnNum == 0) {
        //        alert("您点击了鼠标左键！")
    }
    else if (btnNum == 1) {
        //        alert("您点击了鼠标中键！");
    }
    else {
        //        alert("您点击了" + btnNum + "号键，我不能确定它的名称。");
    }
}


function submitcode() {

    var obj = document.getElementById("inputcode");
    if (obj.value.length > 0) {

        // alert(obj.value);
        window.external.submitcode(obj.value, true);


    } else {
        //    alert("请输入取票码");
    }

}

//选择调用打印事件
function selectprintticket() {

    var obj = document.getElementsByName("sample-checkbox");
    var check_val = [];

    for (var i = 0; i < obj.length; i++) {

        if (obj[i].checked) {
            check_val.push(obj[i].value);
        }
    }

    //  alert(check_val);

    if (check_val.length > 0) {

        var ticketids = check_val.join(',');

        //    alert(ticketids);
        window.external.printticket(ticketids);
    } else {
        $(".popOutBg").fadeIn();
    }
}

//调用WINFORM 打印事件
function printticket(ticketids) {

    // alert(codetype);
    window.external.printticket(ticketids);
}

//显示打印页面
function printpage(msg) {
    var oIframe = document.getElementById('iframeticket');

    // oIframe.contentWindow.location.reload(true);
    oIframe.src = "get_my_ticket_success.html";

    //
    setCookie("validjson", msg);

    //  oIframe.contentWindow.printview(msg);
}

//已检票页面
function vaildtranw(msg) {
    var oIframe = document.getElementById('iframeticket');

    oIframe.src = "get_my_ticket_trawn.html";
    setCookie("validjson", msg);
}

//打印成功
function printsuccesspage(msg) {
    var oIframe = document.getElementById('iframeticket');
    oIframe.src = "get_my_ticket_printsuccess.html";

}

//打印失败
function printfailpage(msg) {
    var oIframe = document.getElementById('iframeticket');
    oIframe.src = "get_my_ticket_printfail.html";

}

//二维码验证失败
function qrfailpage(msg) {
    //    alert(msg);
    var oIframe = document.getElementById('iframeticket');
    oIframe.src = "get_my_ticket_qrfail.html";

}


//串码验证失败
function barcodefailpage(msg) {
    //    alert(msg);
    var oIframe = document.getElementById('iframeticket');
    oIframe.src = "get_my_ticket_barcodefail.html";

}

//暂停服务
function stopservice(msg) {
    var oIframe = document.getElementById('iframeticket');
    oIframe.src = "get_my_ticket_pause.html";

}


//启动服务
function startservice(msg) {
    var oIframe = document.getElementById('iframeticket');
    oIframe.src = "get_my_ticket.html";

}


function setLabel() {
    if ($('.label_check input').length) {
        $('.label_check').each(function () {
            $(this).removeClass('c_on');
        });
        $('.label_check input:checked').each(function () {
            $(this).parent('label').addClass('c_on');
        });
    };
};

//显示打印页面
function validview() {

    var validmessage = getCookie("validjson")
    //    alert("josn:"+validmessage);

    var obj = JSON.parse(validmessage);

    // alert(obj.ticketname);

    var imgUrl = obj.ticketimgurl;
    if (imgUrl.length > 0) {
        document.getElementById('ticketimg').src = imgUrl;
    }

    var objorderstatename = document.getElementById('orderstatename');
    if (objorderstatename != null) {
        objorderstatename.innerHTML = obj.orderstatename;
    }

    var objorderstatename2 = document.getElementById('orderstatename2');
    if (objorderstatename2 != null) {
        objorderstatename2.innerHTML = obj.orderstatename;
    }
    document.getElementById('ticketname').innerHTML = obj.ticketname;
    document.getElementById('venuename').innerHTML = obj.venuename;
    document.getElementById('performdate').innerHTML = obj.performdate;

    var div_ticket_content = "";
    for (var i in obj.items) {

        if (obj.items[i].isprint == 0) {
            div_ticket_content += '<label class="label_check c_on" for="checkbox-0' + obj.items[i].itemid + '" onclick="setLabel();"><input name="sample-checkbox" id="checkbox-0' + obj.items[i].itemid + '" value="' + obj.items[i].itemid + '" type="checkbox" checked />' + obj.items[i].seat + '座' + obj.items[i].price + obj.items[i].desc + '</label>';
        } else {
            div_ticket_content += '<label class="label_check c_out" for="checkbox-2' + obj.items[i].itemid + '" ><input name="sample-checkbox" id="checkbox-2' + obj.items[i].itemid + '" value="' + obj.items[i].itemid + '" type="checkbox" onclick="return false"  />' + obj.items[i].seat + '座' + obj.items[i].price + obj.items[i].desc + '</label>';
        }
    }

    document.getElementById('ticketview').innerHTML = div_ticket_content;

    delCookie("validjson");


    //     $('.label_check').click(function () {
    //         setLabel();
    //      });

    setLabel();

    // alert("viewsuccessed");

}



//当前剩余票纸显示
function updatelastticketpages() {
    var lastpages = window.external.getlastpages();
    document.getElementById('lastpages').innerHTML = lastpages;
}