ipod825 / AdCaseform

// ==UserScript==
// @name        AdCaseform
// @namespace   https://github.com/ipod825/userjs
// @include     http://adv.epb.taichung.gov.tw/Cases/CasesForm.aspx?GUID=*&ACTION=2
// @version     1
// @grant       none
// ==/UserScript==
function makeField(labelField){
    str='';
    for (var label in labelField) {
        str+=td(label,"",1)+td("",labelField[label],1);   
    }	
    return tr(str);
}

function td(text, id, span){
    str='<td '
    if(id)
        str+='id="'+id+'" ';
    if(span)
        str+='colspan="'+span+'" ';
    str+='>'+text+'</td>';
    return str;   
}
function tr(str){	return '<tr>'+str+'</tr>';}

function table(str){return '<table>'+str+'</table>';}

$("#form1 > div.main > div.rightChild > div:nth-child(1)").html(table(
makeField({'違規電話':'phoneField', '電信業者':'supplierField', '國籍':'nationField', '所有人':'ownerField'})
+makeField({'電信查詢時間':'checktimeField', '電信回傳時間':'backtimeField', '簽收時間':'signtimeField', '簽收人':'signerField'})
+tr(td("案件狀態","",1)+td("","statusField",1)+td("","quickInput",2)+td("證號","",1)+td("","idField",1)+td("地址","",1)+td("","addressField",1))
)
+table(
makeField({'電信裁處文號':'judgenumField', '電信裁處發文日期':'judgeSdateField', '電信裁處送達日期':'judgeAdateField'})
+makeField({'廢清陳述文號':'statenumField', '廢清陳述發文日期':'stateSdateField', '廢清陳述送達日期':'stateAdateField'})
+makeField({'廢清裁處文號':'judge2numField', '廢清裁處發文日期':'judge2SdateField', '廢清裁處送達日期':'judge2AdateField'})
+tr(td("廢清告發裁處書序號","",1)+td("","suenumField",1)+td("","btnField",4))
)
+table(
makeField({'停話':'stopphoneField', '停話時間':'stopphonetimeField', '停話期間':'stopphonedurationField'})
+makeField({'繳款':'payField', '繳款日期':'paydateField', '罰鍰':'paynumField'})
+makeField({'結案':'endField', '結案日期':'enddateField', '備註':'noteField'})    
)
+table(
tr(td("","otherField",8))
));


$("th, td").css({'border': '1px solid black'});
$("table").css("width","100%");
$(".leftChild").remove();
$(".rightChild").css("margin-left",'0px');
$(".header").remove();
$(".main").css("padding","0");


$('#phoneField').append($('#BodyContent_view02lab03'));
$('#supplierField').append($('#BodyContent_EipCodeWuc1_ddlEipCode'));
$('#nationField').append($('#BodyContent_ucNationalCode_ddlEipCode'));
$('#ownerField').append($('#BodyContent_view02txb04'));

$('#checktimeField').append($('#BodyContent_view02txb02'));
$('#backtimeField').append($('#BodyContent_view02txb23'));
$('#signtimeField').append($('#BodyContent_view02txb08'));
$('#signerField').append($('#BodyContent_view02txb07'));

$('#statusField').append($('#BodyContent_ucStatusCode_ddlEipCode'));
$('#idField').append($('#BodyContent_view02txb05'));
$('#addressField').append($('#BodyContent_view02txb06'));

$('#suspendField').append($('#BodyContent_view02rab01'));
$('#backphoneField').append($('#BodyContent_lbVerifyTelAnswer'));

$('#judgenumField').append($('#BodyContent_view02txb12'));
$('#judgeSdateField').append($('#BodyContent_view02txb13'));
$('#judgeAdateField').append($('#BodyContent_view02txb24'));

$('#statenumField').append($('#BodyContent_view02txb14'));
$('#stateSdateField').append($('#BodyContent_view02txb15'));
$('#stateAdateField').append($('#BodyContent_view02txb16'));

$('#judge2numField').append($('#BodyContent_view02txb17'));
$('#judge2SdateField').append($('#BodyContent_view02txb18'));
$('#judge2AdateField').append($('#BodyContent_view02txb19'));

$('#stopdateField').append($('#BodyContent_ucSuspendTime_txtDate'));
$('#stopdurationField').prepend($('#BodyContent_ucSuspendSDate_txtDate'));
$('#stopdurationField').append($('#BodyContent_ucSuspendEDate_txtDate'));

$('#suenumField').append($('#BodyContent_tbWDRulingNo02'));


$('#stopphoneField').append($('#BodyContent_view02rab01_0'))
					.append($('<label for="BodyContent_view02rab01_0">已停話</label>'))
					.append($('#BodyContent_view02rab01_1'))
					.append($('<label for="BodyContent_view02rab01_1">未停話</label>'));
                    
$('#stopphonetimeField').append($('#BodyContent_ucSuspendTime_txtDate').css({'width':'90px'}))
						.append($('#BodyContent_ucSuspendSDate_ddl_session').css({'width':'80px'}))
						.append($('#BodyContent_ucSuspendSDate_ddl_hour').css({'width':'80px'}))
						.append($('<span>:</span>'))
						.append($('#BodyContent_ucSuspendSDate_ddl_minute').css({'width':'80px'}));

$('#stopphonedurationField').append($('#BodyContent_ucSuspendSDate_txtDate').css({'width':'90px'}))
.append($('#BodyContent_ucSuspendTime_ddl_session').css({'width':'77px'}))
						.append($('#BodyContent_ucSuspendTime_ddl_hour').css({'width':'70px'}))
						.append($('<span>:</span>'))
						.append($('#BodyContent_ucSuspendTime_ddl_minute').css({'width':'70px'}))
						.append($('<span>~</span><br>'))
						.append($('#BodyContent_ucSuspendEDate_txtDate').css({'width':'90px'}))
						.append($('#BodyContent_ucSuspendEDate_ddl_session').css({'width':'77px'}))
						.append($('#BodyContent_ucSuspendEDate_ddl_hour').css({'width':'70px'}))
						.append($('<span>:</span>'))
						.append($('#BodyContent_ucSuspendEDate_ddl_minute').css({'width':'70px'}));

$('#payField').append($('#BodyContent_view02rab02_0'))
			.append($('<label for="BodyContent_view02rab02_0">已繳款</label>'))
			.append($('#BodyContent_view02rab02_1'))
			.append($('<label for="BodyContent_view02rab02_1">未繳款</label>'));
$('#paydateField').append($('#BodyContent_tbPayDate'));
$('#paynumField').append($('#BodyContent_ucFine_ddlEipCode'));


$('#endField').append($('#BodyContent_view02rab03_0'))
			.append($('<label for="BodyContent_view02rab03_0">已結案(結案後無法再修改!)</label>'))
			.append($('#BodyContent_view02rab03_1'))
			.append($('<label for="BodyContent_view02rab03_1">未結案</label>'));
$('#enddateField').append($('#BodyContent_tbCloseDate'));
$('#noteField').append($('#BodyContent_view02txb22'));

$("#btnField").append($("#BodyContent_btnAdminModify")).append($("#BodyContent_btnWord1_btnWord")).append($("#BodyContent_btnBackToCleanTeam"));
$("#otherField").append($("#BodyContent_Panel1")).append($("#BodyContent_view02img01")).append($("#BodyContent_view02img02"));

$("#BodyContent_divViewAdmin").remove();

$('<input/>').attr({
    name: 'whatever',
  type: 'button',
  id: 'exportBtn',
  class: 'btn btn-info',
  value: '轉各資查詢/匯出',
}).click(function () {
  $("#BodyContent_ucStatusCode_ddlEipCode")[0].selectedIndex=2;
  $("#BodyContent_btnWord1_btnWord").click();
}).prependTo($("#btnField"));

$('<input/>').attr({
    name: 'whatever',
  type: 'text',
  id: 'quickSignField',
  size: '25',
}).focus(function(){
  this.select();
}).appendTo($("#quickInput"));

$('<input/>').attr({
    name: 'whatever',
  type: 'button',
  class: 'btn btn-info',
  value: '簽搞',
  title: '電信公司|簽收人|國籍|傳真時間|回傳時間|所有人'
}).click(function(){  
  var arr=$("#quickSignField")[0].value.split("|");
    
  var supplierStr=arr[0];
  
  if(supplierStr=="台灣之星"){
   $('#BodyContent_EipCodeWuc1_ddlEipCode')[0].selectedIndex=3;
  }
  else{
    supplierStr=supplierStr.substr(0,2);
     $('#BodyContent_EipCodeWuc1_ddlEipCode').find('option').each(function(){       
       if(supplierStr==this.text.substr(0,2)){
         $('#BodyContent_EipCodeWuc1_ddlEipCode')[0].selectedIndex=this.index;
         return;
       }
    });
  }
  var nationStr=(arr[2]);
  $('#BodyContent_ucNationalCode_ddlEipCode').find('option').each(function(){       
       if(nationStr==this.text.substr(0,1)){
         $('#BodyContent_ucNationalCode_ddlEipCode')[0].selectedIndex=this.index;
         return;
       }
    });
  $('#BodyContent_view02txb07')[0].value=arr[1];
  $('#BodyContent_view02txb02')[0].value=arr[3];
  $('#BodyContent_view02txb23')[0].value=arr[4];
  $('#BodyContent_view02txb08')[0].value=arr[4];
  $('#BodyContent_view02txb04')[0].value=arr[5];
  $('#BodyContent_ucStatusCode_ddlEipCode')[0].selectedIndex=3;
}).appendTo($("#quickInput"));

$('<input/>').attr({
    name: 'whatever',
  type: 'button',
  class: 'btn btn-danger',
  value: 'paste',
}).click(function(){
    if($("#BodyContent_view02lab03").html()!=window.localStorage.getItem("phone")){
    	alert("此案件與複製案件之號碼不相符");
    }
    else{
        $("input:enabled:not([type='hidden']):not([type='submit']):not([type='radio']):not([type='checkbox']):not([type='button'])").each(function(){
            this.value=window.localStorage.getItem(this.id);   
        });
        $("textarea:enabled").each(function(){
            this.value=window.localStorage.getItem(this.id);   
        });
        $("select").each(function(){
            this.selectedIndex=window.localStorage.getItem(this.id);   
        })
        $("input[type='radio']").each(function(){
            if(window.localStorage.getItem(this.id)=="true")
                this.checked=true;
        });
    }
}).prependTo($("#btnField"));


$('<input/>').attr({
  type: 'button',
  class: 'btn btn-warning',
  value: 'Copy',
}).click(function(){
    
    window.localStorage.clear();
    window.localStorage.setItem("phone",$("#BodyContent_view02lab03").html())
    $("input:enabled:not([type='hidden']):not([type='submit']):not([type='radio']):not([type='checkbox']):not([type='button'])").each(function(){
  		window.localStorage.setItem(this.id,this.value);   
    });
    $("textarea:enabled").each(function(){
  		window.localStorage.setItem(this.id,$(this).val());   
    });
    $("select").each(function(){
    	window.localStorage.setItem(this.id,this.selectedIndex);   
    })
    $("input:checked").each(function(){
  		window.localStorage.setItem(this.id,this.checked);   
    });
    
}).prependTo($("#btnField"));




function getFormContent(){
    var a=getFormParStr($("input:enabled:not([type='submit']):not([type='radio']):not([type='checkbox'])"));
    var b=getFormParStr($("#BodyContent_btnAdminModify"));
    var c=getFormParStr($("input:checked"));
    var d=getFormParStr($("select:enabled"));
    //console.log(a);
    return a+"&"+b+"&"+c+"&"+d;
}

function getFormParStr(inputs){
    return inputs.map(function(){
    	var v=this.value;
        v=encodeURI(v);
        return this.name.replace(/\$/g,"%24")+"="+v.replace(/\//g,"%2F").replace(/\+/g,"%2B").replace(/=/g,"%3D");
    }).get().join("&");
}


/*
$('#form1').submit(function() {
    event.preventDefault();
    
	$.ajax({
        type: 'POST',
        url: location.href,
        data: getFormContent(),
        success: function (res) {
            event.preventDefault();
            console.log(res);
        }
    });
    location.reload();
    return false;
}); 
*/