/*-----------------------------------------------------------------------------
*
*计算器功能模块 start
*
**----------------------------------------------------------------------------*/
var bShift = false; //记录shift键是否按下
var FKeyPad = null;
var Accum = 0;
var FlagNewNum = false;
var PendingOp = "";
//===============================================================================
//[描述] 浮点数精确计算
//[参数] str1 - 第一个数
// str2 - 第二个数
// type - 运算符
// precision - 小数位精度
//[调用方式] longCount(str1,str2,type,precision);
//[返回值] 计算结果
//===============================================================================
function longCount(str1, str2, type) {
    var comma1 = 0;
    str1 = str1.toString();
    str2 = str2.toString();
    //有科学计数法，转成非科学计数法
    if (str1.indexOf("e") != -1) {
        str1 = parseFloat(str1).toFixed(0);
    }
    if (str2.indexOf("e") != -1) {
        str2 = parseFloat(str2).toFixed(0);
    }
    if (str1.indexOf(".") != -1) {
        str1 = str1.replace(/0*$/, "");
        comma1 = str1.length - str1.indexOf(".") - 1;
    }
    var comma2 = 0;
    if (str2.indexOf(".") != -1) {
        str2 = str2.replace(/0*$/, "");
        comma2 = str2.length - str2.indexOf(".") - 1;
    }
    str1 = str1.replace(/\./, "");
    str2 = str2.replace(/\./, "");
    var value, comma;
    if (type != "*") {
        if (comma1 > comma2) {
            for (var i = 0; i < comma1 - comma2; i++) str2 += "0";
            comma = (type == "/") ? 0 : comma1;
        } else {
            for (var i = 0; i < comma2 - comma1; i++) str1 += "0";
            comma = (type == "/") ? 0 : comma2;
        }
    } else {
        comma = comma1 + comma2;
    }
    if (type == "+") {
        value = parseInt(str1, 10) + parseInt(str2, 10);
    } else if (type == "-") {
        value = parseInt(str1, 10) - parseInt(str2, 10);
    } else if (type == "*") {
        value = parseInt(str1, 10) * parseInt(str2, 10);
    } else if (type == "/") {
        value = parseInt(str1, 10) / parseInt(str2, 10);
    }
    value = String(value);
    //不使用科学计数法计算
    if (value.indexOf("e") != -1) {
        value = (parseFloat(value).toFixed(0)).toString();
    }
    //if (comma > 0) value = value.substring(0, value.length - comma) + "." + value.substring(value.length - comma, value.length);
    if (comma > 0) value = (value / Math.pow(10, comma)).toString();
    if (value.indexOf(".") != -1)
        value = value.replace(/0*$/, "");
    return value;
}
//按下数字键按钮。
function NumPressed(Num) {
    if (FlagNewNum) {
        FKeyPad.ReadOut.value = Num;
        FlagNewNum = false;
    }
    else {
        //计算文本最大录入16位数字
        if (FKeyPad.ReadOut.value.toString().length >= 16) {
            return false;
        }
        if (FKeyPad.ReadOut.value == "0")
            FKeyPad.ReadOut.value = Num;
        else
            FKeyPad.ReadOut.value += Num;
    }
}
//按下运算符
function Operation(Op) {
    var Readout = FKeyPad.ReadOut.value;
    if (FlagNewNum && PendingOp != "=");
    else {
        FlagNewNum = true;
        //除数为0，不允许
        if ('/' == PendingOp && parseInt(Readout, 10) == 0) {
            $("#btnClear").click();
            $('#idWarn').html("除数不能为零。");
            setTimeout(function () {
                $('#idWarn').html("");
            }, 1000);
            return;
        }

        if ('+' == PendingOp || '-' == PendingOp || '/' == PendingOp || '*' == PendingOp)
            Accum = longCount(Accum, Readout, PendingOp);
        else
            Accum = Readout;
        if (isNaN(Accum)) {
            Accum = 0;
        }
        FKeyPad.ReadOut.value = Accum;
        PendingOp = Op;
        FKeyPad.ReadOut.focus();
        FKeyPad.ReadOut.select();
    }
}
//按下小数点
function Decimal() {
    var curReadOut = FKeyPad.ReadOut.value;
    if (FlagNewNum) {
        curReadOut = "0.";
        FlagNewNum = false;
    }
    else {
        if (curReadOut.indexOf(".") == -1)
            curReadOut += ".";
    }
    FKeyPad.ReadOut.value = curReadOut;
}
//按下CE，清空键
function ClearEntry() {
    FKeyPad.ReadOut.value = "0";
    FlagNewNum = true;
}
//按下C，清空键
function Clear() {
    Accum = "0";
    PendingOp = "";
    ClearEntry();
}
//按下正负转换键
function Neg() {
    //alert(FKeyPad.ReadOut.value);
    var strVal = longCount(FKeyPad.ReadOut.value, "-1", "*");
    //FKeyPad.ReadOut.value = isNaN(parseFloat(strVal)) ? strVal: parseFloat(strVal);
    FKeyPad.ReadOut.value = parseFloat(strVal);
}
//按下%
function Percent() {
    if ($("#ReadOut").val()) {
        FKeyPad.ReadOut.value = longCount(FKeyPad.ReadOut.value, Accum, "*");
        var iResult = longCount(FKeyPad.ReadOut.value, 100, "/");
        if (isNaN(iResult)) {
            FKeyPad.ReadOut.value = 0;
        }
        else {
            FKeyPad.ReadOut.value = iResult;
        }
    }
    else {
        FKeyPad.ReadOut.value = 0;
    }
}
//按下backspace，删除数字
function goBackspace() {
    var strVal = $("#ReadOut").val().toString();
    var iLen = strVal.length;
    if (iLen >= 1) {
        iLen -= 1;
    }
    $("#ReadOut").val(strVal.substr(0, iLen));
    //top.returnValue = FKeyPad.ReadOut.value;
    //self.close();
}
//按键check//ciho取消使用
function CheckOut() {
    var keyCode = window.event.keyCode;
    if (keyCode >= 48 && keyCode <= 57) {
        if (FlagNewNum) {
            FKeyPad.ReadOut.value = "";
            //window.event.keyCode = null;
            FlagNewNum = false;
        }
        return true;
    } //42 * ;43 +; 45 _; 47 / ;61 =;
    else if (keyCode == 43 || keyCode == 45 || keyCode == 42 || keyCode == 47 || keyCode == 61) {
        Operation(String.fromCharCode(keyCode));
    }
    else if (keyCode == 46) {//.
        if (FKeyPad.ReadOut.value.indexOf(".") == -1)
            return true;
    }
    else if (keyCode == 13) {
        //回车，触发等号操作
        $("#btnEquals").click();
    }
    window.event.returnValue = false;
    return false;
}
function createCalc(dlgId, strTitle, iHeight, iWidth) {
    if (!iWidth) {
        iWidth = "300";
    }
    if (!iHeight) {
        iHeight = "300";
    }
    var strCalc = "";
    strCalc += '<div id="idWarn" style="height:18px; width:100px; font-size:12px; color:#999;padding-left:20px;padding-top:8px;"></div>';
    //    strCalc += "<div class=\"pop\">";
    //    strCalc += "<div class=\"pop_x\"><a href=\"#\" onclick=\"closeMyDlg('" + dlgId + "')\"></a></div>";
    //    strCalc += "<div id=\"" + dlgId + "_pop_top\" class=\"ui-draggable pop_top\">";
    //    strCalc += "     <h2>" + strTitle + "<font id=\"idMust\" class=\"must\"></font></h2>";
    //    strCalc += "</div>";

    //strCalc += '</br>';
    strCalc += '<form name="Keypad" id="Keypad" action="" style="padding:0px;margin:0px;width:' + iWidth + 'px;height:' + iHeight + 'px;background:#FFF;>';
    strCalc += '<table align="center" class="js_1">';
    strCalc += '<b>';
    strCalc += '<table align="center" border="0" padding="0" margin="0" width="300" height="180" cellpadding="1" cellspacing="5">';
    strCalc += '<tr>';
    strCalc += '<td colspan="3" align="middle" class="js_2">';
    //strCalc += '<input name="ReadOut" id="ReadOut" type="Text" onkeypress="CheckOut()" size="24" value="0" width="100%">';
    strCalc += '<input name="ReadOut" maxlength="16" readonly id="ReadOut" type="Text" size="24" value="0" width="100%">';
    strCalc += '</td>';
    strCalc += '<td>';
    strCalc += '</td>';
    strCalc += '<td>';
    strCalc += '<input name="btnClear" id="btnClear" type="Button" value=" C " onclick="Clear()">';
    strCalc += '</td>';
    strCalc += '<td>';
    strCalc += '<input name="btnClearEntry" id="btnClearEntry" type="Button" value=" CE " onclick="ClearEntry()">';
    strCalc += '</td>';
    strCalc += '</tr>';
    strCalc += '<tr>';
    strCalc += '<td>';
    strCalc += '<input name="btnSeven" id="btnSeven" type="Button" value=" 7 " onclick="NumPressed(7)">';
    strCalc += '</td>';
    strCalc += '<td>';
    strCalc += '<input name="btnEight" id="btnEight" type="Button" value=" 8 " onclick="NumPressed(8)">';
    strCalc += '</td>';
    strCalc += '<td>';
    strCalc += '<input name="btnNine" id="btnNine" type="Button" value=" 9 " onclick="NumPressed(9)">';
    strCalc += '</td>';
    strCalc += '<td>';
    strCalc += '</td>';
    strCalc += '<td>';
    strCalc += '<input name="btnNeg" id="btnNeg" type="Button" value=" +/- " onclick="Neg()">';
    strCalc += '</td>';
    strCalc += '<td>';
    strCalc += '<input name="btnPercent" id="btnPercent" type="Button" value=" % " onclick="Percent()">';
    strCalc += '</td>';
    strCalc += '</tr>';
    strCalc += '<tr>';
    strCalc += '<td>';
    strCalc += '<input name="btnFour" id="btnFour" type="Button" value=" 4 " onclick="NumPressed(4)">';
    strCalc += '</td>';
    strCalc += '<td>';
    strCalc += '<input name="btnFive" id="btnFive" type="Button" value=" 5 " onclick="NumPressed(5)">';
    strCalc += '</td>';
    strCalc += '<td>';
    strCalc += '<input name="btnSix" id="btnSix" type="Button" value=" 6 " onclick="NumPressed(6)">';
    strCalc += '</td>';
    strCalc += '<td>';
    strCalc += '</td>';
    strCalc += '<td align="middle">';
    strCalc += '<input name="btnPlus" id="btnPlus" type="Button" value=" + " onclick="Operation(\'+\')">';
    strCalc += '</td>';
    strCalc += '<td align="middle">';
    strCalc += '<input name="btnMinus" id="btnMinus" type="Button" value=" - " onclick="Operation(\'-\')">';
    strCalc += '</td>';
    strCalc += '</tr>';
    strCalc += '<tr>';
    strCalc += '<td>';
    strCalc += '<input name="btnOne" id="btnOne" type="Button" value=" 1 " onclick="NumPressed(1)">';
    strCalc += '</td>';
    strCalc += '<td>';
    strCalc += '<input name="btnTwo" id="btnTwo" type="Button" value=" 2 " onclick="NumPressed(2)">';
    strCalc += '</td>';
    strCalc += '<td>';
    strCalc += '<input name="btnThree" id="btnThree" type="Button" value=" 3 " onclick="NumPressed(3)">';
    strCalc += '</td>';
    strCalc += '<td>';
    strCalc += '</td>';
    strCalc += '<td align="middle">';
    strCalc += '<input name="btnMultiply" id="btnMultiply" type="Button" value=" * " onclick="Operation(\'*\')">';
    strCalc += '</td>';
    strCalc += '<td align="middle">';
    strCalc += '<input name="btnDivide" id="btnDivide" type="Button" value=" / " onclick="Operation(\'/\')">';
    strCalc += '</td>';
    strCalc += '</tr>';
    strCalc += '<tr>';
    strCalc += '<td colspan="2"  class="js_3">';
    strCalc += '<input name="btnZero" id="btnZero" type="Button" value=" 0 " onclick="NumPressed(0)">';
    strCalc += '</td>';

    strCalc += '<td>';
    strCalc += '<input name="btnDecimal" id="btnDecimal" type="Button" value=" . " onclick="Decimal()">';
    strCalc += '</td>';

    strCalc += '<td>';
    strCalc += '</td>';
    strCalc += '<td>';
    strCalc += '<input name="btnEquals" id="btnEquals" type="Button" value=\" = \" onclick="Operation(\'=\')">';
    strCalc += '</td>';
    strCalc += '<td class="js_4">';
    strCalc += '<input name="btnBackspace" id="btnBackspace" type="Button" value=\"←\" onclick="goBackspace();">';
    strCalc += '</td>';
    strCalc += '</tr>';
    strCalc += '</table>';
    strCalc += '</table>';
    strCalc += '</B>';
    strCalc += '</form>'
    strCalc += '</br>';
    //strCalc += '</div>';
    //*/
    $("#" + dlgId + "_divform").append(strCalc);

    //绑定事件
    bindEvents(dlgId);
}

function bindEvents(dlgId) {
    $("#" + dlgId).unbind('keydown').bind('keydown', function (e) {
        //功能键
        switch (e.keyCode) {
            case 16: // SHIFT
                bShift = true;
                break;
            case 27: // Esc
                $("#btnClear").click();
                break;
            //小键盘
            case 111: // /
                $("#btnDivide").click();
                break;
            case 106: // *
                $("#btnMultiply").click();
                break;
            case 109: // -
                $("#btnMinus").click();
                break;
            case 107: // +
                $("#btnPlus").click();
                break;
            case 13: // = //回车
                $("#btnEquals").click();
                break;
            case 8: // backspace
                $("#btnBackspace").click();
                break;
            //大键盘
            case 191: // /
                $("#btnDivide").click();
                break;
            case 189: // -
                $("#btnMinus").click();
                break;
            case 187: // = +
                if (bShift) {
                    $("#btnPlus").click();
                }
                else {
                    $("#btnEquals").click();
                }
                break;
            //            case 53: //  shift + %
            //                if (bShift) {
            //                    $("#btnPercent").click();
            //                }
            //                break;
            //            case 56: // shift + *
            //                if (bShift) {
            //                    $("#btnMultiply").click();
            //                }
            //                break;
        }

        //        //限制录入最大长度为16位
        //        if ($("#ReadOut").val().toString().length >= 16) {
        //            return false;
        //        }

        //数字键
        switch (e.keyCode) {
            case 110: // .
                $("#btnDecimal").click();
                break;
            case 96: // 0
                $("#btnZero").click();
                break;
            case 97: // 1
                $("#btnOne").click();
                break;
            case 98: // 2
                $("#btnTwo").click();
                break;
            case 99: // 3
                $("#btnThree").click();
                break;
            case 100: // 4
                $("#btnFour").click();
                break;
            case 101: // 5
                $("#btnFive").click();
                break;
            case 102: // 6
                $("#btnSix").click();
                break;
            case 103: // 7
                $("#btnSeven").click();
                break;
            case 104: // 8
                $("#btnEight").click();
                break;
            case 105: // 9
                $("#btnNine").click();
                break;
            //大键盘
            case 190: // .
                $("#btnDecimal").click();
                break;
            case 48: // 0
                $("#btnZero").click();
                break;
            case 49: // 1
                $("#btnOne").click();
                break;
            case 50: // 2
                $("#btnTwo").click();
                break;
            case 51: // 3
                $("#btnThree").click();
                break;
            case 52: // 4
                $("#btnFour").click();
                break;
            case 53: // 5
                if (bShift) { //+shift %
                    $("#btnPercent").click();
                }
                else {
                    $("#btnFive").click();
                }
                break;
            case 54: // 6
                $("#btnSix").click();
                break;
            case 55: // 7
                $("#btnSeven").click();
                break;
            case 56: // 8
                if (bShift) {// + shift *
                    $("#btnMultiply").click();
                }
                else {
                    $("#btnEight").click();
                }
                break;
            case 57: // 9
                $("#btnNine").click();
                break;
        }
        return false;
    });

    $("#" + dlgId).unbind('keyup').bind('keyup', function (e) {
        if (e.keyCode == 16) {
            bShift = false;
        }
        return false;
    });

    $("#" + dlgId).unbind('keypress').bind('keypress', function (e) {
        if (13 == e.keyCode || 8 == e.keyCode) {
            //停止响应回车事件。
            //window.event.cancelBubble = true;
            //window.event.returnValue = false;
            return false;
        }
    });
}
/*-----------------------------------------------------------------------------
*
*计算器功能模块 End
*
**----------------------------------------------------------------------------*/
//加载万年历
function createWnl(dlgId, strTitle, iHeight, iWidth) {
    if (!iWidth) {
        iWidth = "300";
    }
    if (!iHeight) {
        iHeight = "300";
    }
    var strWnl = "";
    strWnl += '<iframe scrolling=\"no\" id=\"idIframe-wnl\" frameborder=\"0\" align=\"center\"  src=\"\"';
    //strWnl += ' style=\"width:' + iWidth + 'px;height:\"' + iHeight + 'px;\"></iframe>';
    strWnl += ' width=\"' + iWidth + 'px\" height=\"' + iHeight + 'px\"></iframe>';
    //debugger
    $("#" + dlgId + "_divform").append(strWnl);
    $("#idIframe-wnl").attr("src", "p04.htm");
}

//加载快递
function createKd(dlgId, strTitle, iHeight, iWidth) {
    var strKd = "";
    strKd += '<iframe scrolling=\"no\" id=\"idIframe-kd\" frameborder=\"0\" align=\"center\"  src=\"\"';
    strKd += ' width=\"' + iWidth + 'px\" height=\"' + iHeight + 'px\"></iframe>';

    $("#" + dlgId + "_divform").append(strKd);
    $("#idIframe-kd").attr("src", "p05.htm");
}