var _date = new Date();

function getYearB() {
  var year = _date.getFullYear();
  return year + "0101"
}

function getYearE() {
  var year = _date.getFullYear();
  return year + "1231"
}

function getLastMonthB() {
  var year = _date.getFullYear();
  var month = "0" + _date.getMonth();
  if(month == "00") {
    return (year - 1) + "1201";
  }
  month = month.substring(month.length-2,month.lenght);
  return "" + year + month + "01";
}

function getLastMonthE() {
  var year = _date.getFullYear();
  var month = _date.getMonth();
  var ss = "31";
  if(month == 2) {
    ss = "28";
    if(year == "2008") ss = "29";
  } else if(month == 4 || month == 6 || month == 9 || month == 11) {
    ss = "30";
  }
  month = "0" + month;
  if(month == "00") {
    return (year - 1) + "1231";
  }
  month = month.substring(month.length-2,month.lenght);
  return "" + year + month + ss;
}

function bto_getElementTop(obj) {
  var t = obj.offsetTop;
  while(obj = obj.offsetParent) t += obj.offsetTop;
  return t;
}

function bto_getElementLeft(obj) {
  var l = obj.offsetLeft;
  while(obj = obj.offsetParent) l += obj.offsetLeft;
  return l;
}

function bto_movePoint(str,n) {
  str = "" + str;
  if(str.indexOf(".") == -1) {
    str = str + ".";
  }
  var flag = "";
  if(str.charAt(0) == "-") {
    flag = "-";
    str = str.substring(1,str.length);
  }
  str = "00000000000000000000000000000000000000000000000000" + str + "00000000000000000000000000000000000000000000000000";
  n = parseInt(n,10);
  var p = str.indexOf(".");
  var s1 = str.substring(0,p);
  var s2 = str.substring(p+1,str.length);
  str = "" + s1 + s2;
  p += n;
  str = str.substring(0,p) + "." + str.substring(p,str.length);
  while(str.substring(0,1) == "0" && str.substring(1,2) != ".") {
    str = str.substring(1,str.length);
  }
  while(str.substring(str.length-1,str.length) == "0") {
    str = str.substring(0,str.length-1);
  }
  if(str.substring(str.length-1,str.length) == ".") {
    str = str.substring(0,str.length-1);
  }
  return flag + str;
}

function bto_num2str(num) {
  var str = "" + num;
  var p = str.indexOf("e");
  if(p != -1) {
    var s1 = str.substring(0,p);
    var s2 = str.substring(p+1,str.length);
    str = bto_movePoint(s1,s2);
  }
  return str;
}

function bto_scale(num, scale) {
  if(num.length == 0) return "";
  var n = parseFloat(num);
  if(n > 0) {
    n += 0.000001;
  }else {
    n -= 0.000001;
  }
  var str = bto_num2str(n);
  str = bto_movePoint(str,scale);
  n = Math.round(parseFloat(str));
  str = bto_movePoint(bto_num2str(n),scale * -1);
  
  if (str.indexOf(".") == -1) {
    str += "."
    for(var i=0;i<scale;i++) {
      str += "0";
    }
  }
  if(str.indexOf(".") == str.length -2) {
    str += "0";
  }
  return str;
}

function atoi(v) {
  var i = parseInt(v, 10);
  if(i == v) return i;
  return 0;
}

function atof(v) {
  var f = parseFloat(v);
  if(f == v) return f;
  return 0;
}

function autoZero(v) {
  if(isNaN(v) || v == "") return "0.00";
  if(parseFloat(v) < 0) return "0.00";
  return v;
}

function autoFloat(em) {
  if(isNaN(em.value) || em.value == "") em.value = "0.00";
}

function autoBlank(em) {
  em.value = "";
}

function makeRead(em) {
  autoBlank(em);
  em.readOnly = true;
  em.setAttribute("_class", "aright bg1");
  em.parentElement.setAttribute("_class", "bg1");
}

function makeWrite(em) {
  autoFloat(em);
  em.readOnly = false;
  em.setAttribute("_class", "aright");
  em.parentElement.setAttribute("_class", "");
}

function help_html() {
  var h = 700;
  var w = 1000;
  var l = (screen.availWidth - w) / 2;
  var t = 10;

  window.open("../help/index.htm","_blank","top="+ t + ",left=" + l + ",width=" + w + ",height=" + h + ",resizable=1,scrollbars=1");
}

function changeBodySize(obj) {
  document.body.style.zoom = obj.value;
}

function getRate0(money) {
  var result = new Array();
  if(money <= 500) {
    result[0] = "0.05";
    result[1] = "0.00";
  } else if(money <= 2000) {
    result[0] = "0.10";
    result[1] = "25.00";
  } else if(money <= 5000) {
    result[0] = "0.15";
    result[1] = "125.00";
  } else if(money <= 20000) {
    result[0] = "0.20";
    result[1] = "375.00";
  } else if(money <= 40000) {
    result[0] = "0.25";
    result[1] = "1375.00";
  } else if(money <= 60000) {
    result[0] = "0.30";
    result[1] = "3375.00";
  } else if(money <= 80000) {
    result[0] = "0.35";
    result[1] = "6375.00";
  } else if(money <= 100000) {
    result[0] = "0.40";
    result[1] = "10375.00";
  } else {
    result[0] = "0.45";
    result[1] = "15375.00";
  }
  return result;
}

function getRate1(money) {
  var result = new Array();
  if(money <= 1500) {
    result[0] = "0.03";
    result[1] = "0.00";
  }else if(money <= 4500) {
    result[0] = "0.10";
    result[1] = "105.00";
  }else if(money <= 9000) {
    result[0] = "0.20";
    result[1] = "555.00";
  }else if(money <= 35000) {
    result[0] = "0.25";
    result[1] = "1005.00";
  }else if(money <= 55000) {
    result[0] = "0.30";
    result[1] = "2755.00";
  }else if(money <= 80000) {
    result[0] = "0.35";
    result[1] = "5505.00";
  }else {
    result[0] = "0.45";
    result[1] = "13505.00";
  }
  return result;
}

function getP(ee) {
  if(ee.tagName == "TR") {
    return ee;
  }else {
    return getP(ee.parentElement);
  }
}

var focusTR = null;
function on_focus() {
  if(focusTR) {
    var elms = focusTR.all;
    for(var i = 0; i < elms.length; i++) {
      elms[i].className = elms[i].getAttribute("_class");
    }
  }

  var oTR = getP(event.srcElement);
  var elms = oTR.all;
  for(var i = 0; i < elms.length; i++) {
    elms[i].setAttribute("_class", elms[i].className);
    if(elms[i].tagName == "BUTTON") continue;

    elms[i].className = elms[i].className + " bg2";
    elms[i].className = elms[i].className.replace("bg1","");
  }
  focusTR = oTR;
}

function help() {
  var elm = event.srcElement;
  elm.select();
  if(elm.tagName != "INPUT") return;
  if(elm.id.indexOf("_") < 0) return;

  var field = elm.id.split("_")[0];
  var index = elm.id.split("_")[1];
  var article = document.getElementById("article_" + elm.id.split("_")[1]);

  var top = bto_getElementTop(elm);
  var left = bto_getElementLeft(elm);

  document.all.tb_help.style.left = left + 6 + "px";
  document.all.tb_help.style.top = top + 19 + "px";

  document.all.text_help.innerHTML = help_text(field, article.value);

  var error = document.getElementById("status_" + index);
  if(error.value != "") {
    document.all.tb_error.style.left = left + 6 + "px";
    document.all.tb_error.style.top = top + 20 + document.all.tb_help.offsetHeight + "px";

    if(error.value.length > 40)
      document.all.text_error.innerHTML = error.value.substring(0, error.value.indexOf("；",40));
    else
      document.all.text_error.innerHTML = error.value;
  }
}

var codeTypeAry = new Array("01","02","03","04","05","06","07","08","09","10","11","90");

var articleAry1 = new Array("010001","010002","010003","010005","010006","020101","020103","020104","020105","200524","030001","040000","040005","050001","050002","060101","060201","060301","060401","069999","070101","070104","070105","070106","070107","070108","070109","080101","080201","090101","090102","090103","090301","090401","099901","099902","100001","900001");
var articleAry0 = new Array("001","002","003","004","005","102","103","012");
var articleAry3 = new Array("010001","010090");
var articleAry5 = new Array("001","002","003","004","005");

function contain(str, ary) {
  if(str == "") return true;
  for(var i = 0; i < ary.length; i++) {
    if(str == ary[i]) return true;
  }
  return false;
}

function valid() {
  document.all.tb_help.style.top = "-100px";
  document.all.tb_error.style.top = "-100px";

  var elm = event.srcElement;
  if(elm.tagName != "INPUT") return;

  var field = elm.id.split("_")[0];
  var row = elm.id.split("_")[1];

  var name = document.getElementById("name_" + row).value;
  if(name == "") return;

  var article = document.getElementById("article_" + row).value;

  if(elm.value == "")
    elm.value = default_text(field, article);
  else if((field == "begin" || field == "end") && !validDate(elm.value))
    elm.value = default_text(field, article);

  var status = document.getElementById("status_" + row);
  var statusC = document.getElementById("statusC_" + row);

  var codeType = document.getElementById("codeType_" + row).value;
  if(contain(codeType, codeTypeAry)) {
    status.value = status.value.replace("证件类型[B]填写错误；", "");
  } else {
    if(status.value.indexOf("B") < 0) status.value += "证件类型[B]填写错误；";
  }

  var reportType = document.getElementById("reportType").value;
  if(reportType == 0 || reportType == 1 || reportType == 3) {
    var preferential = document.getElementById("preferential_" + row);
    if(preferential.value != "1" && preferential.value != "0") preferential.value = "0";

    var employee = document.getElementById("employee_" + row);
    if(employee.value != "1" && employee.value != "0") employee.value = "1";
  }

  var articleAry = eval("articleAry" + reportType);

  if(contain(article, articleAry)) {
    status.value = status.value.replace("所得项目代码[K]填写错误；", "");
  } else {
    if(status.value.indexOf("K") < 0) status.value += "所得项目代码[K]填写错误；";
  }

  if(status.value.length > 0 && status.value != "通过") {
    statusC.value = "2";
    status.style.color = "red";
    document.getElementById("name_" + row).style.color = "red";
  } else {
    statusC.value = "0";
    status.style.color = "black";
    document.getElementById("name_" + row).style.color = "black";
    status.value = "通过";
  }

  calc(elm.id);
}

function _valid() {
  alert("数据填写或导入完毕后，可以不用“校验本页”直接点击保存按钮，软件会自动计算并校验全部记录。\r\n");
  for(var j=1;j<=20;j++) {
    document.getElementById("name_" + j).fireEvent("onblur");
  }
}

function validDate(d) {
  var year = parseInt(d.substring(0,4), 10);
  if(isNaN(year)) return false;

  var month = parseInt(d.substring(4,6), 10);
  if(isNaN(month)) return false;

  var day = parseInt(d.substring(6,8), 10);
  if(isNaN(day)) return false;

  var dd = new Date(year, month - 1, day);
  if(year != dd.getFullYear()) return false;
  if(month != dd.getMonth() + 1) return false;
  if(day != dd.getDate ()) return false;

  return true;
}

function main_onclick() {
  var elm = event.srcElement;
  
  if(elm.tagName == "BUTTON") return;
  
  if(!document.getElementById("save_insurance_1")) return;

  for(var i=1;i<=20;i++) {
    document.getElementById("save_insurance_" + i).style.display = "none";
  }
  for(var i=1;i<=20;i++) {
    document.getElementById("show_insurance_" + i).style.display = "inline";
  }
  
  tb_insurance.style.left = "-1000px";
  tb_insurance.style.top = "-1000px";
}

function show_insurance() {
  var elm = event.srcElement;
  var row = elm.id.split("_")[2];

  var name = document.getElementById("name_" + row).value;
  var codeType = document.getElementById("codeType_" + row).value;
  var code = document.getElementById("code_" + row).value;
  var article = document.getElementById("article_" + row).value;
  
  if(name == "" || codeType == "" || code == "") {
    alert("姓名，证件类型，证件号码 不能为空！");
    event.cancelBubble = true;
    return;
  }
  
  if(article != "002"
    && article.substring(0,2) != "01"
    && article.substring(0,2) != "02"
    && article.substring(0,2) != "20") {
    alert("所得项目代码不适用！");
    event.cancelBubble = true;
    return;
  }

  for(var i=1;i<=20;i++) {
    document.getElementById("save_insurance_" + i).style.display = "none";
  }
  for(var i=1;i<=20;i++) {
    document.getElementById("show_insurance_" + i).style.display = "inline";
  }

  elm.style.display = "none";
  elm.nextSibling.style.display = "inline";

  var top = bto_getElementTop(elm);
  var left = bto_getElementLeft(elm);
  
  tb_insurance.style.left = left - 402 + "px";
  tb_insurance.style.top = top - 90 + "px";
  
  insurance_total = 0;
  for(var i = 1; i <= 5; i++) {
    document.getElementById("insurance_a" + i).value = "";
    document.getElementById("insurance_b" + i).value = "";
    document.getElementById("insurance_c" + i).value = "";
    document.getElementById("insurance_d" + i).value = "";
  }
}

function save_insurance() {
  tb_insurance_onchange();

  var elm = event.srcElement;
  elm.style.display = "none";
  elm.previousSibling.style.display = "inline";

  tb_insurance.style.left = "-1000px";
  tb_insurance.style.top = "-1000px";

  var row = elm.id.split("_")[2];

  document.getElementById("insurance_" + row).value = bto_scale(insurance_total, 2);
  document.getElementById("insurance_" + row).fireEvent("onblur");
}

var insurance_total = 0;
function tb_insurance_onchange() {
  var total = 0;
  for(var i = 1; i <= 5; i++) {
    var a = document.getElementById("insurance_a" + i);
    var b = document.getElementById("insurance_b" + i);
    var c = document.getElementById("insurance_c" + i);
    var d = document.getElementById("insurance_d" + i);
    
    if(a.value == "") {
      d.value = "";
      continue;
    }
    
    if(b.value != "") b.value = bto_scale(Math.max(0, atof(b.value)), 2);
    if(c.value != "") c.value = bto_scale(Math.max(0, atof(c.value)), 2);
    
    var reportType = document.getElementById("reportType").value;
    
    if(reportType == "0") {
      d.value = bto_scale(Math.max(0, atof(d.value)), 2);
      total += atof(d.value);
    } else {
      d.value = bto_scale(Math.min(c.value, 200 - total), 2);
      total += atof(c.value);
      if(total > 200) total = 200;
    }
  }
  insurance_total = total;
}
function _select() {
  event.srcElement.select();
}

function init() {
  document.getElementById("main").onclick = main_onclick;

  var rows = document.all.main.rows;
  for(var i = 1; i < rows.length; i++) {
    rows[i].onclick = on_focus;
  }

  var ipts = document.all.tags("INPUT");
  for(var i = 0; i < ipts.length; i++) {
    if(ipts[i].id.indexOf("_") < 0) continue;
    var elm = ipts[i];
    elm.onfocus = help;
    elm.onblur = valid;
  }

  document.write("<table id='tb_help' style='z-index:99;position:absolute;top:-100px;left:-100px;' cellspacing='0'>");
  document.write("<tr><td style='padding:0px;'><img id='img_help' src='arrow.png'></td></tr>");
  document.write("<tr><td id='text_help' style='background:greenyellow;padding:4px 4px 2px 4px;'></td></tr>");
  document.write("</table>");

  document.write("<table id='tb_error' style='z-index:99;position:absolute;top:-100px;left:-100px;' cellspacing='0'>");
  document.write("<tr><td style='padding:0px;'><img id='img_error' src='arrow.png'></td></tr>");
  document.write("<tr><td id='text_error' style='background:greenyellow;padding:4px 4px 2px 4px;color:red;'></td></tr>");
  document.write("</table>");

  document.write("<object id='WebBrowser' width='0' height='0' classid='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'></object>");

  var reportType = document.getElementById("reportType").value;
  
  if(reportType == "0") {
    document.write("<table id='tb_insurance' cellspacing='1'>");
    document.write("<tr><th nowrap>序号</th><th style='width:100%'>税优识别码</th><th nowrap>年度保费</th><th nowrap>月度保费</th><th nowrap>本期扣除金额</th></tr>");
    document.write("<tr><td class='bg1'>1</td><td><input id='insurance_a1'></td><td><input id='insurance_b1' class='aright'></td><td><input id='insurance_c1' class='aright'></td><td><input id='insurance_d1' class='aright'></td></tr>");
    document.write("<tr><td class='bg1'>2</td><td><input id='insurance_a2'></td><td><input id='insurance_b2' class='aright'></td><td><input id='insurance_c2' class='aright'></td><td><input id='insurance_d2' class='aright'></td></tr>");
    document.write("<tr><td class='bg1'>3</td><td><input id='insurance_a3'></td><td><input id='insurance_b3' class='aright'></td><td><input id='insurance_c3' class='aright'></td><td><input id='insurance_d3' class='aright'></td></tr>");
    document.write("<tr><td class='bg1'>4</td><td><input id='insurance_a4'></td><td><input id='insurance_b4' class='aright'></td><td><input id='insurance_c4' class='aright'></td><td><input id='insurance_d4' class='aright'></td></tr>");
    document.write("<tr><td class='bg1'>5</td><td><input id='insurance_a5'></td><td><input id='insurance_b5' class='aright'></td><td><input id='insurance_c5' class='aright'></td><td><input id='insurance_d5' class='aright'></td></tr>");
    document.write("</table>");
  } else {
  document.write("<table id='tb_insurance' cellspacing='1'>");
  document.write("<tr><th nowrap>序号</th><th style='width:100%'>税优识别码</th><th nowrap>年度保费</th><th nowrap>月度保费</th><th nowrap>本期扣除金额</th></tr>");
  document.write("<tr><td class='bg1'>1</td><td><input id='insurance_a1'></td><td><input id='insurance_b1' class='aright'></td><td><input id='insurance_c1' class='aright'></td><td class='bg1'><input id='insurance_d1' class='aright bg1' readonly></td></tr>");
  document.write("<tr><td class='bg1'>2</td><td><input id='insurance_a2'></td><td><input id='insurance_b2' class='aright'></td><td><input id='insurance_c2' class='aright'></td><td class='bg1'><input id='insurance_d2' class='aright bg1' readonly></td></tr>");
  document.write("<tr><td class='bg1'>3</td><td><input id='insurance_a3'></td><td><input id='insurance_b3' class='aright'></td><td><input id='insurance_c3' class='aright'></td><td class='bg1'><input id='insurance_d3' class='aright bg1' readonly></td></tr>");
  document.write("<tr><td class='bg1'>4</td><td><input id='insurance_a4'></td><td><input id='insurance_b4' class='aright'></td><td><input id='insurance_c4' class='aright'></td><td class='bg1'><input id='insurance_d4' class='aright bg1' readonly></td></tr>");
  document.write("<tr><td class='bg1'>5</td><td><input id='insurance_a5'></td><td><input id='insurance_b5' class='aright'></td><td><input id='insurance_c5' class='aright'></td><td class='bg1'><input id='insurance_d5' class='aright bg1' readonly></td></tr>");
  document.write("</table>");
  }


  document.write("<input id='fire_main_onclick' type='hidden' onpropertychange='main_onclick()'>");
  
  ipts = document.getElementById("tb_insurance").all.tags("INPUT");
  for(var i = 0; i < ipts.length; i++) {
    ipts[i].onchange = tb_insurance_onchange;
    ipts[i].onfocus = _select;
  }
}
init();

var tb_insurance = document.getElementById("tb_insurance");
