﻿function in_mail(){
	window.open("getemail.php",'window',"Height:500Px;Width:700px;status:no;help:no;");
}
function jsEncode(value) {
	value = value.replace("\\","\\\\").replace("\"","\\\"").replace("\'","\\\'").replace("\r","\\r").replace("\n","\\n").replace("\t","\\t");
}

function delConfirm() {
	return confirm("您確定刪除嗎?");
}

function isWord(value) {
    var pattern = /(^[a-zA-Z0-9]*$)/;
    value = value.toString();
    if (value == "") return true;
    return pattern.test(value);
}

function isInteger(value) {
    var pattern = /(^[0-9]*$)|(^-[0-9]*$)/;
    value = value.toString();
    if (value == "") return true;
    return pattern.test(value);
}

function isFloat(value) {
    var pattern = /(^[0-9]*$)|(^([0-9]*\.[0-9]*)$)|(^-[0-9]*$)|(^(-[0-9]*\.[0-9]*)$)/;
    value = value.toString();
    if (value == "") return true;
    return pattern.test(value);
}

function isDate(value) {
    var pattern = /(^([0-9]{1,4}\/[0-9]{1,2}\/[0-9]{1,2})$)|(^([0-9]{1,4}-[0-9]{1,2}-[0-9]{1,2})$)/;
    var tmpYear = 0;
    var tmpMonth = 0;
    var tmpDay = 0;
    value = value.toString();
    if (value == "") return true;
    if (pattern.test(value)) {
		if (value.indexOf("/")) {
        	tmpYear = parseInt(value.split("/")[0]);
        	tmpMonth = parseInt(value.split("/")[1]);
        	tmpDay = parseInt(value.split("/")[2]);
      	}else{
        	tmpYear = parseInt(value.split("-")[0]);
        	tmpMonth = parseInt(value.split("-")[1]);
        	tmpDay = parseInt(value.split("-")[2]);
      	}
      	if (tmpYear < 1900 || tmpYear > 9999) return false;
      	if (tmpMonth < 1 || tmpMonth > 12) return false;
      	if (tmpDay < 1) return false;
      	if (tmpDay > 31 && (tmpMonth == 1 || tmpMonth == 3 || tmpMonth == 5 || tmpMonth == 7 || tmpMonth == 8 || tmpMonth == 10 || tmpMonth == 12)) return false;
      	if (tmpDay > 30 && (tmpMonth == 4 || tmpMonth == 6 || tmpMonth == 9 || tmpMonth == 11)) return false;
      	if (tmpMonth == 2) {
			if ((tmpYear % 4 == 0 && tmpYear % 100 != 0) || tmpYear % 400 == 0) {
				if (tmpDay > 29) return false;
			}else{
				if(tmpDay > 28) return false;
			}
      	}
      	return true;
    }
    return false;
}

function isDateTime(value) {
    var pattern = /(^([0-9]{1,4}\/[0-9]{1,2}\/[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2})$)|(^([0-9]{1,4}-[0-9]{1,2}-[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2})$)/;
    var tmpYear = 0;
    var tmpMonth = 0;
    var tmpDay = 0;
    var tmpHH = 0;
    var tmpMM = 0;
    var tmpSS = 0;
    value = value.toString();
    if (value == "") return true;
    if (pattern.test(value)) {
      	if (value.indexOf("/")) {
			tmpYear = parseInt(value.split(" ")[0].split("/")[0]);
			tmpMonth = parseInt(value.split(" ")[0].split("/")[1]);
			tmpDay = parseInt(value.split(" ")[0].split("/")[2]);
      	}else{
			tmpYear = parseInt(value.split(" ")[0].split("-")[0]);
			tmpMonth = parseInt(value.split(" ")[0].split("-")[1]);
			tmpDay = parseInt(value.split(" ")[0].split("-")[2]);
		}
      	tmpHH = parseInt(value.split(" ")[1].split(":")[0]);
      	tmpMM = parseInt(value.split(" ")[1].split(":")[1]);
      	tmpSS = parseInt(value.split(" ")[1].split(":")[2]);
      	if (tmpYear < 1900 || tmpYear > 9999) return false;
      	if (tmpMonth < 1 || tmpMonth > 12) return false;
      	if (tmpDay < 1) return false;
      	if (tmpDay > 31 && (tmpMonth == 1 || tmpMonth == 3 || tmpMonth == 5 || tmpMonth == 7 || tmpMonth == 8 || tmpMonth == 10 || tmpMonth == 12)) return false;
      	if (tmpDay > 30 && (tmpMonth == 4 || tmpMonth == 6 || tmpMonth == 9 || tmpMonth == 11)) return false;
      	if (tmpMonth == 2) {
			if ((tmpYear % 4 == 0 && tmpYear % 100 != 0) || tmpYear % 400 == 0) {
			  	if (tmpDay > 29) return false;
			}else{
			  	if (tmpDay > 28) return false;
			}
      	}
      	if (tmpHH < 0 || tmpHH > 59) return false;
      	if (tmpMM < 0 || tmpMM > 59) return false;
      	if (tmpSS < 0 || tmpSS > 59) return false;
      	return true;
    }
    return false;
}

//Dreamweaver js函數
function MM_preloadImages() { //v3.0
  	var d=document;
	if (d.images){
		if(!d.MM_p) d.MM_p=new Array();
    	var i,j=d.MM_p.length,a=MM_preloadImages.arguments;
		for(i=0; i<a.length; i++)
    		if (a[i].indexOf("#")!=0){
				d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];
			}
	}
}

function MM_swapImgRestore() { //v3.0
  	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
	var p,i,x;
	if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  	if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  	var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
    if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  	eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  	if (restore) selObj.selectedIndex=0;
}

//月曆
//textbox-> onclick="calendar()"
function in_calendar(){
	showModalDialog("includess/small_calendar.htm?dd=1",window,"dialogHeight:250Px;dialogWidth:220px;status:no;help:no");
}

function calendar(){
	showModalDialog("includes/small_calendar.htm?dd=3",window,"dialogHeight:320Px;dialogWidth:310px;status:no;help:no");
}
//學生生日
function calendar_s(){
	showModalDialog("includes/calendar_s.htm?dd=3",window,"dialogHeight:320Px;dialogWidth:310px;status:no;help:no");
}

//開特殊新視窗
function open_fit_window(url,w,h){
	var width, height, args;							//宣告變數
	width=(w<window.screen.availWidth-50)?w+50:window.screen.availWidth-50;
	height=(h<window.screen.availHeight-50)?h+50:window.screen.availHeight-50;	
	left=((window.screen.availWidth-50)/2)-(width/2);
	top1=((window.screen.availHeight-50)/2)-(height/2);
	args="width="+width+",height="+height+",left="+left+",top="+top1+",resizable=yes,scrollbars=yes,status=no";			
	window.open(url,"", args);
	return false;
}

//欄位格式檢
function check_basic_field(){
	obj_form=event.srcElement; //document.all("form1");
	for (var i=0;obj_input=obj_form.all[i];i++){
		if (obj_input.validate){
			var validate=obj_input.validate.split(",")			
			var obj_value=((obj_input.value)?obj_input.value:obj_input.innerText).replace(/(^\s+)|(\s+$)/g,"");
			var obj_name=obj_input.alt.substring(5,obj_input.alt.length-2);
			for (var j=0;j<validate.length;j++){					
				switch (validate[j]){
				case "row":
					obj_name+=(obj_input.parentElement.parentElement.rowIndex+1);
					break;		
				case "null":					
					if (((obj_value=="")||(obj_value==obj_input.alt))&&(!obj_input.vdnullold)){	
						obj_input.focus();
						alert("["+obj_name+"]請勿空白");return false;	
					}
					break;
				case "len":
					if (obj_value.length>obj_input.vdlength){
						obj_input.focus();
						alert("["+obj_name+"]請勿超過"+obj_input.vdlength+"個字");return false;		
					}
					break;
				case "num":
					if ((obj_value=="")||(obj_value==obj_input.alt)) break;
					var range=obj_input.vdrange.split(",");
					if ((obj_value<parseInt(range[0]))||(obj_value>parseInt(range[1]))||(obj_value!=parseInt(obj_value))){
						obj_input.focus();
						alert("["+obj_name+"]請輸入"+range[0]+"~"+range[1]+"的整數");return false;
					}	
					break;				
				case "reg":	
					if ((obj_value=="")||(obj_value==obj_input.alt)) break;
					if (obj_value.match(new RegExp("^"+obj_input.vdregular+"$","i"))==null){
						obj_input.focus();
						alert("["+obj_name+"]請使用正確格式");return false;
					}
					break;
				case "ext":		//?!asp$|php$ 不能為程式檔  //jpg$|gif$|bmp   必須為圖片檔	  
					if ((obj_value=="")||(obj_value==obj_input.alt)) break;
					if (obj_value.match(new RegExp("[\.]("+obj_input.vdregular+")","i"))==null){
						alert("["+obj_name+"]請勿上傳不合法的檔案");return false;
					}
					break;
				case "sub":		//vdsub="check_pw"
					//if ((obj_value=="")||(obj_value==obj_input.alt)) break;
					if (errmsg=eval(obj_input.vdsub+"('"+obj_value+"')")){
						alert("["+obj_name+"]"+errmsg);return false;
					}
					break;			
				} 
			}			
		}		
	}
	ischecked();
	return true;
}

//檢查身分證字號
function check_pid(id){
	errmsg="請使用正確格式";
	id=id.toUpperCase();
	if (id.length != 10) return errmsg;

	if (isNaN(id.substr(1,9)) || (id.substr(0,1)<"A" ||id.substr(0,1)>"Z"))	return errmsg;

	var head="ABCDEFGHJKLMNPQRSTUVXYWZIO";
	id = (head.indexOf(id.substring(0,1))+10) +''+ id.substr(1,9)
	s =parseInt(id.substr(0,1)) + 
	parseInt(id.substr(1,1)) * 9 + 
	parseInt(id.substr(2,1)) * 8 + 
	parseInt(id.substr(3,1)) * 7 + 			
	parseInt(id.substr(4,1)) * 6 + 
	parseInt(id.substr(5,1)) * 5 + 
	parseInt(id.substr(6,1)) * 4 + 
	parseInt(id.substr(7,1)) * 3 + 
	parseInt(id.substr(8,1)) * 2 + 
	parseInt(id.substr(9,1)) + 
	parseInt(id.substr(10,1));

	//判斷是否可整除
	if ((s % 10) != 0)	return false;

	//身分證字號正確		
	return true;	  
}
//=========================欄位格式檢核====================================================
//============================動態產生輸入附檔欄位==========================
//更換多筆的資料欄位型態
function changeField(){
	obj_file=event.srcElement.parentElement.children(1);
	obj_data=event.srcElement.parentElement.children(2);
	if (event.srcElement.value=="1"){
		obj_file.style.display="inline";
		obj_data.style.display="none";
	}else if (event.srcElement.value=="2"){
		obj_file.style.display="none";
		obj_data.style.display="inline";
	}	
	obj_file.outerHTML=obj_file.outerHTML;	obj_data.value="";	
}


//新增多欄資料列
function InsertRow(){
	obj=document.getElementById('txt_fieldnum')
	obj_value=obj.value.replace(/(^\s+)|(\s+$)/g,"").replace(/[,]+/g,"");
	if (obj_value.length==0){
		alert("附檔數量請勿空白!!");return false;
	}else if ((obj_value<0)||(obj_value>100)||(obj_value!=parseInt(obj_value))){			
		alert("附檔數量請輸入0~100的正整數");return false;
	}else{
		rownum=obj_value;
	}	

	var tab_field=document.getElementById("tab_field");
	var	row_exist=tab_field.innerHTML;	
	var row_new,cell_del,cell_title,cell_data,cell_order;
	for (i=0;i<rownum;i++){
	new_row=tab_field.insertRow();
	cell_del=new_row.insertCell();	cell_title=new_row.insertCell();
	cell_data=new_row.insertCell();	cell_order=new_row.insertCell();
	cell_del.innerHTML="<input name='btn_fielddel' type='button' id='btn_fielddel' onclick='js:DeleteRow()' value='刪除'>";
	cell_title.innerHTML="標題:<input name='txt_fieldtitle"+ i +"' type='text' size='20' alt='--請輸入多欄資料-標題--' validate='row,len' vdlength='100'>";	
	cell_data.innerHTML="<select name='ddl_fieldtype"+ i +"' onchange='js:changeField()'><option value='1' selected>檔案</option><option value='2'>連結</option></select>"
					   +"<input name='txt_fieldfile"+ i +"' type='file' size='15' onchange='js:this.nextSibling.value=this.value;'>"
					   +"<input name='txt_fielddata"+ i +"' type='text' size='23' alt='--請輸入多欄資料-資料--' validate='row,null,len' vdlength='100' style='display=none'>"
	cell_order.innerHTML="順序:<input name='txt_fieldorders"+ i +"' type='text' size='5' alt='--請輸入多欄資料-順序--' validate='row,num' vdrange='-100,100'>";	
	}
	x = document.getElementById('hid_tab_field').value;
	x = parseInt(x) + parseInt(rownum);
	document.getElementById('hid_tab_field').value = x;
	//row = document.getElementById('tab_field').rows //全部有多少列
	//document.getElementById('hid_tab_field').value=row.length
}

//刪除多欄資料列
function DeleteRow(){
	var obj_table=event.srcElement.parentElement.parentElement.parentElement;
	var del_index=event.srcElement.parentElement.parentElement.rowIndex;
	//document.getElementById('hid_tab_field').value -= 1;
	obj_table.deleteRow(del_index);
}

//建立已存在多欄資料表格
function ExistRow(num, title, path, format, i){
	//判斷物件型態
	var obj_format,tab_exist,last_row,cell_del,cell_file,cell_rename,cell_reupload;
	if (format=='2'){
		obj_format='連結：<a href="'+ path +'" target="_blank" title="'+ title +'">'+ title +'</a>';
		upload = "更改連結：<input type='text' size = 10 name='reupload"+ i +"'>";
	}else{
		obj_format='附檔：<a href="'+ path +'" target="_blank" title="'+ title +'">'+ title +'</a>';
		upload = "重新上傳：<input type='file' size = 10 name='reupload"+ i +"'>";
	}
	//判斷放置位置
	tab_exist=document.getElementById('tab_exist');
	last_row=tab_exist.rows[tab_exist.rows.length-1];
	if ((!last_row) || (last_row.cells.length==4))
		last_row=tab_exist.insertRow();
	//放入格子中
	cell_del=last_row.insertCell();
	cell_rename=last_row.insertCell();
	cell_reupload=last_row.insertCell();
	cell_file=last_row.insertCell();
	cell_del.width="10%"
	cell_rename.width="25%"
	cell_reupload.width="30%"
	cell_file.width="15%"
	cell_del.innerHTML='<input type="checkbox" name="chk_fielddel"><label for="chk_del">刪除</label>';
	cell_rename.innerHTML="更改標題：<input type='text' size = 12 name='f_title"+ i +"' value='"+ title +"' alt='--修改的標題請勿空白--' validate='null'>";
	cell_reupload.innerHTML=upload + "<input type='hidden' name='f_id"+ i +"' value='"+ num +"'><input type='hidden' id='status"+ i +"' name='status"+ i +"' value='0'>";
	cell_file.innerHTML=obj_format;
}

//判斷刪除檔案的checkbox有沒有被打勾
function ischecked()
{
	var obj = document.getElementsByName("chk_fielddel");
	var len = obj.length;

	for (i = 0; i < len; i++){
		if (obj[i].checked == true){
			document.getElementById("status"+i).value = "1";
		}else{
			document.getElementById("status"+i).value = "0";
		}
	} 
}

function checkall(form, prefix, checkall) {
	var checkall = checkall ? checkall : 'chkall';
	for(var i = 0; i < form.elements.length; i++){
		var e = form.elements[i];
		if(e.name != checkall && (!prefix || (prefix && e.name.match(prefix)))) {
			e.checked = form.elements(checkall).checked;
		}
	}
}

function is_checked() {
	var flag = 0;
	for (var i = 0; i < document.form.elements.length; i++)  {
		if (document.form.elements[i].checked){
			flag = 1;
		}
	}
	if (flag == 0){
		alert("請選擇欲刪除的資料!");
		return false;
	}else{
		return true;
	}
}

//============================動態產生輸入附檔欄位==========================
//鄉鎮市跟著縣市變動
function chgdll_town(){		
	var obj_county=document.getElementById("ddl_county");
	var obj_town=document.getElementById("ddl_town");
	var obj_towndata=document.getElementById("ddl_towndata");
	obj_town.length=0;
	for(var i=0;i<obj_towndata.length;i++){	
		if ((obj_towndata.options[i].county==obj_county.value) || (obj_towndata.options[i].text=="請選擇")){		
			obj_town.options[obj_town.options.length]=new Option(obj_towndata.options[i].text,obj_towndata.options[i].value);									
			if (obj_towndata.options[i].selected) obj_town.options[obj_town.options.length-1].selected=true; 	
		}
	}	
	//連動子層
	chgdll_village();
}
//村里跟著鄉鎮市變動

function chgdll_village(){	
	obj_county=document.getElementById("ddl_county");	
	obj_town=document.getElementById("ddl_town");
	obj_village=document.getElementById("ddl_village");
	obj_villagedata=document.getElementById("ddl_villagedata");
	obj_village.length=0;
	for(var i=0;i<obj_villagedata.length;i++){
		if (((obj_villagedata.options[i].county==obj_county.value)&&(obj_villagedata.options[i].town==obj_town.value)) || (obj_villagedata.options[i].text=="請選擇")){
			obj_village.options[obj_village.options.length]=new Option(obj_villagedata.options[i].text,obj_villagedata.options[i].value);									

			if ( obj_villagedata.options[i].selected) obj_village.options[obj_village.options.length-1].selected=true; 
		}
	}
}
//============================使用說明==========================
var obj_help_window, obj_help_x, obj_help_y;

function help_object_create() {
	window.onresize=help_button_move;					
	window.onscroll=help_button_move;	
	document.onmousemove=help_window_onmousemove;
	document.onmouseup=help_window_onmouseup;			
}				
function help_button_move() {				
	document.getElementById("help_button").style.top=30+((document.body.scrollTop!=0)?document.body.scrollTop:document.documentElement.scrollTop);
	
}
function help_window_show() {
	var obj_body=((document.documentElement)?document.documentElement:document.body);
	document.getElementById('help_button').style.display="none";
	document.getElementById('help_cover').style.display="block";
	document.getElementById('help_window').style.display="block";
	document.getElementById('help_cover').style.height=obj_body.scrollHeight -10;
	document.getElementById('help_cover').style.width=obj_body.scrollWidth -25;
	document.getElementById('help_window').style.top=obj_body.scrollTop+20;
	document.getElementById('help_window').style.left=obj_body.scrollWidth-530;
}
function help_window_close() {
	document.getElementById('help_button').style.display="block";
	document.getElementById('help_cover').style.display='none';
	document.getElementById('help_window').style.display='none';
}
function help_window_onmousedown() {
	obj_help_window = document.getElementById('help_window');				
	obj_help_x= parseInt(obj_help_window.style.left) - event.x;
	obj_help_y = parseInt(obj_help_window.style.top) - event.y;				
}
function help_window_onmousemove() {
	if(obj_help_window){
		obj_help_window.style.left=obj_help_x+event.x;
		obj_help_window.style.top =obj_help_y+event.y;
	}
}								
function help_window_onmouseup() {
	obj_help_window="";
} 
//============================使用說明==========================




///////////////////////////////////////////////////MD5/////////////////////////////////////
var hex_chr = "0123456789abcdef";
function rhex(num)
{
  str = "";
  for(j = 0; j <= 3; j++)
    str += hex_chr.charAt((num >> (j * 8 + 4)) & 0x0F) +
           hex_chr.charAt((num >> (j * 8)) & 0x0F);
  return str;
}

/*
 * Convert a string to a sequence of 16-word blocks, stored as an array.
 * Append padding bits and the length, as described in the MD5 standard.
 */
function str2blks_MD5(str)
{
  nblk = ((str.length + 8) >> 6) + 1;
  blks = new Array(nblk * 16);
  for(i = 0; i < nblk * 16; i++) blks[i] = 0;
  for(i = 0; i < str.length; i++)
    blks[i >> 2] |= str.charCodeAt(i) << ((i % 4) * 8);
  blks[i >> 2] |= 0x80 << ((i % 4) * 8);
  blks[nblk * 16 - 2] = str.length * 8;
  return blks;
}

/*
 * Add integers, wrapping at 2^32. This uses 16-bit operations internally 
 * to work around bugs in some JS interpreters.
 */
function add(x, y)
{
  var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  return (msw << 16) | (lsw & 0xFFFF);
}

/*
 * Bitwise rotate a 32-bit number to the left
 */
function rol(num, cnt)
{
  return (num << cnt) | (num >>> (32 - cnt));
}

/*
 * These functions implement the basic operation for each round of the
 * algorithm.
 */
function cmn(q, a, b, x, s, t)
{
  return add(rol(add(add(a, q), add(x, t)), s), b);
}
function ff(a, b, c, d, x, s, t)
{
  return cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
function gg(a, b, c, d, x, s, t)
{
  return cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
function hh(a, b, c, d, x, s, t)
{
  return cmn(b ^ c ^ d, a, b, x, s, t);
}
function ii(a, b, c, d, x, s, t)
{
  return cmn(c ^ (b | (~d)), a, b, x, s, t);
}

/*
 * Take a string and return the hex representation of its MD5.
 */
function md5(str){
  x = str2blks_MD5(str);
  var a =  1732584193;
  var b = -271733879;
  var c = -1732584194;
  var d =  271733878;
 
  for(i = 0; i < x.length; i += 16){
    var olda = a;
    var oldb = b;
    var oldc = c;
    var oldd = d;

    a = ff(a, b, c, d, x[i+ 0], 7 , -680876936);
    d = ff(d, a, b, c, x[i+ 1], 12, -389564586);
    c = ff(c, d, a, b, x[i+ 2], 17,  606105819);
    b = ff(b, c, d, a, x[i+ 3], 22, -1044525330);
    a = ff(a, b, c, d, x[i+ 4], 7 , -176418897);
    d = ff(d, a, b, c, x[i+ 5], 12,  1200080426);
    c = ff(c, d, a, b, x[i+ 6], 17, -1473231341);
    b = ff(b, c, d, a, x[i+ 7], 22, -45705983);
    a = ff(a, b, c, d, x[i+ 8], 7 ,  1770035416);
    d = ff(d, a, b, c, x[i+ 9], 12, -1958414417);
    c = ff(c, d, a, b, x[i+10], 17, -42063);
    b = ff(b, c, d, a, x[i+11], 22, -1990404162);
    a = ff(a, b, c, d, x[i+12], 7 ,  1804603682);
    d = ff(d, a, b, c, x[i+13], 12, -40341101);
    c = ff(c, d, a, b, x[i+14], 17, -1502002290);
    b = ff(b, c, d, a, x[i+15], 22,  1236535329);    

    a = gg(a, b, c, d, x[i+ 1], 5 , -165796510);
    d = gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
    c = gg(c, d, a, b, x[i+11], 14,  643717713);
    b = gg(b, c, d, a, x[i+ 0], 20, -373897302);
    a = gg(a, b, c, d, x[i+ 5], 5 , -701558691);
    d = gg(d, a, b, c, x[i+10], 9 ,  38016083);
    c = gg(c, d, a, b, x[i+15], 14, -660478335);
    b = gg(b, c, d, a, x[i+ 4], 20, -405537848);
    a = gg(a, b, c, d, x[i+ 9], 5 ,  568446438);
    d = gg(d, a, b, c, x[i+14], 9 , -1019803690);
    c = gg(c, d, a, b, x[i+ 3], 14, -187363961);
    b = gg(b, c, d, a, x[i+ 8], 20,  1163531501);
    a = gg(a, b, c, d, x[i+13], 5 , -1444681467);
    d = gg(d, a, b, c, x[i+ 2], 9 , -51403784);
    c = gg(c, d, a, b, x[i+ 7], 14,  1735328473);
    b = gg(b, c, d, a, x[i+12], 20, -1926607734);
    
    a = hh(a, b, c, d, x[i+ 5], 4 , -378558);
    d = hh(d, a, b, c, x[i+ 8], 11, -2022574463);
    c = hh(c, d, a, b, x[i+11], 16,  1839030562);
    b = hh(b, c, d, a, x[i+14], 23, -35309556);
    a = hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
    d = hh(d, a, b, c, x[i+ 4], 11,  1272893353);
    c = hh(c, d, a, b, x[i+ 7], 16, -155497632);
    b = hh(b, c, d, a, x[i+10], 23, -1094730640);
    a = hh(a, b, c, d, x[i+13], 4 ,  681279174);
    d = hh(d, a, b, c, x[i+ 0], 11, -358537222);
    c = hh(c, d, a, b, x[i+ 3], 16, -722521979);
    b = hh(b, c, d, a, x[i+ 6], 23,  76029189);
    a = hh(a, b, c, d, x[i+ 9], 4 , -640364487);
    d = hh(d, a, b, c, x[i+12], 11, -421815835);
    c = hh(c, d, a, b, x[i+15], 16,  530742520);
    b = hh(b, c, d, a, x[i+ 2], 23, -995338651);

    a = ii(a, b, c, d, x[i+ 0], 6 , -198630844);
    d = ii(d, a, b, c, x[i+ 7], 10,  1126891415);
    c = ii(c, d, a, b, x[i+14], 15, -1416354905);
    b = ii(b, c, d, a, x[i+ 5], 21, -57434055);
    a = ii(a, b, c, d, x[i+12], 6 ,  1700485571);
    d = ii(d, a, b, c, x[i+ 3], 10, -1894986606);
    c = ii(c, d, a, b, x[i+10], 15, -1051523);
    b = ii(b, c, d, a, x[i+ 1], 21, -2054922799);
    a = ii(a, b, c, d, x[i+ 8], 6 ,  1873313359);
    d = ii(d, a, b, c, x[i+15], 10, -30611744);
    c = ii(c, d, a, b, x[i+ 6], 15, -1560198380);
    b = ii(b, c, d, a, x[i+13], 21,  1309151649);
    a = ii(a, b, c, d, x[i+ 4], 6 , -145523070);
    d = ii(d, a, b, c, x[i+11], 10, -1120210379);
    c = ii(c, d, a, b, x[i+ 2], 15,  718787259);
    b = ii(b, c, d, a, x[i+ 9], 21, -343485551);

    a = add(a, olda);
    b = add(b, oldb);
    c = add(c, oldc);
    d = add(d, oldd);
  }
  return rhex(a) + rhex(b) + rhex(c) + rhex(d);
}

//判斷數值
function is_numerical (num_string) {
	var re =/\d{1,}/ 
	var re_result=re.exec(num_string)
	if (re_result== num_string )
	{
		return true
	}
	return false
}

//+動態產生輸入附檔欄位==========================
function add_dynamicRow(){
	var NewBoxNums=document.getElementById('NewBoxNum')
	if (is_numerical(NewBoxNums.value) == false)	{
		alert('"附檔數量"的值必需要為整數!!');
		return false
	}
	/*
	if (NewBoxNums.value.length<1){
		alert ('請輸入"附檔數量"的值!!')
		return false
	} else {
		for(var i=0;i<NewBoxNums.value.length;i++){  
			var c= NewBoxNums.value.charAt(i);
			if(!(c>="0" && c<="9")){
				alert ('"附檔數量"的值必需要為整數!!')
				return false
			}
		}
	}
	*/
	InsertRow(NewBoxNums.value)
}

function InsertRow(BoxNum){
	var i
	//+若是使用修改功能，需取得目前附檔中，順序最大的數子
	var max_sq=0
	if (document.getElementById('exist_file_table')!=null)
	{
		var exist_file_row=document.getElementById('exist_file_table').rows //全部有多少列
		for (i=0;i< exist_file_row.length ;i++)
		{
			document.getElementById('txt_existfile_sq'+i).value=document.getElementById('txt_existfile_sq'+i).value.replace(/(^\s+)|(\s+$)/g,"")
			
			if (parseInt(document.getElementById('txt_existfile_sq'+i).value) > max_sq)
			{
				max_sq=parseInt(document.getElementById('txt_existfile_sq'+i).value) 
			}
		}
	}

	if (document.getElementById('file_table')!=null)
	{
		var exist_file_row=document.getElementById('file_table').rows //全部有多少列
		for (i=0;i< exist_file_row.length ;i++)
		{
			document.getElementById('txt_sq'+i).value=document.getElementById('txt_sq'+i).value.replace(/(^\s+)|(\s+$)/g,"")
			
			if (parseInt(document.getElementById('txt_sq'+i).value) > max_sq)
			{
				max_sq=parseInt(document.getElementById('txt_sq'+i).value)
			}
		}
	}
	//-若是使用修改功能，需取得目前附檔中，順序最大的數子
	
	var row=document.getElementById('file_table').rows
	
	var totalrow=row.length + parseInt(BoxNum)
	
	var initial_sq 
	initial_sq=parseInt(max_sq)
	for (i=row.length;i< totalrow ;i++){
		initial_sq=parseInt(initial_sq)+1
		var dynamicNewRow=document.getElementById('file_table').insertRow()
				
		var dynamidatacol=dynamicNewRow.insertCell(0)
					
		//第一個表格
//		dynamidatacol.innerHTML='<table width="100%" border="1" cellpadding="0" cellspacing="1" bordercolor="#efefef"><tr><td><input name="BoxDel'+i+'" type="button" id="BoxDel'+i+'" onclick="javascript:DeleteRow('+i+')" value="刪除"></td><td width="14%"><font size="2">順序</font><input name="txt_sq'+i+'" type="text" id="txt_sq'+i+'" size="1" value="'+initial_sq+'" maxlength="3" title="請輸入排序"></td><td width="26%"><font size="2">名稱</font><input name="txt_file_title'+i+'" type="text" size="12" id="txt_file_title'+i+'" maxlength="22" title="請輸入檔案名稱"></td><td width="20%"><input name="file'+i+'" type="file" id="file'+i+'" size="10"></td><td width="35%"><font size="2">簡述</font><input name="txt_file_intro'+i+'" type="text" size="20" id="txt_file_intro'+i+'" maxlength="200" title="請輸入檔案簡述"></td></tr></table>'
		dynamidatacol.innerHTML='<table width="100%" border="1" cellpadding="0" cellspacing="1" bordercolor="#efefef"><tr><td width="1%"><input name="BoxDel'+i+'" type="button" id="BoxDel'+i+'" onclick="javascript:DeleteRow('+i+')" value="刪除"></td><td width="5%" nowrap>順序<input name="txt_sq'+i+'" type="text" id="txt_sq'+i+'" size="1" value="'+initial_sq+'" maxlength="3" title="請輸入排序"></td><td width="26%" nowrap>名稱<input name="txt_file_title'+i+'" type="text" size="30" id="txt_file_title'+i+'" maxlength="30" title="請輸入檔案名稱"></td><td width="20%"><input name="file'+i+'" type="file" id="file'+i+'" size="15"></td></tr></table>'
	}
	//每新增一個檔就算一次目前檔案數量
	row=document.getElementById('file_table').rows //全部有多少列
	document.getElementById('hid_file_totalrow').value=row.length
	//renum()
}

function DeleteRow(rowIndex){
	var row=document.getElementById('file_table').rows //全部有多少列
	
	//將資料往上移，並將最後一行砍掉
	for (var i=rowIndex;i< row.length-1;i++){
		var rownum_temp=i+1
		//檔案順序
		document.getElementById('txt_sq'+i).value=document.getElementById('txt_sq'+rownum_temp).value
		//檔案路徑
		document.getElementById('file'+i).value =document.getElementById('file'+rownum_temp).value
		//檔案名稱
		document.getElementById('txt_file_title'+i).value=document.getElementById('txt_file_title'+rownum_temp).value
		//檔案簡述
		//document.getElementById('txt_file_intro'+i).value=document.getElementById('txt_file_intro'+rownum_temp).value
	}
	document.getElementById('file_table').deleteRow(row.length-1)	
	
	//每新增一個檔就算一次目前檔案數量
	row=document.getElementById('file_table').rows //全部有多少列
	document.getElementById('hid_file_totalrow').value=row.length
	//renum()
}

function renum(){
	var row=document.getElementById('file_table').rows //全部有多少列
	for (var i=0;i<row.length;i++){
		document.getElementById('num'+i).innerHTML=i+1
	}
}

//動態產生現有檔
function exist_InsertRow(BoxNum){
	var i
	var row=document.getElementById('exist_file_table').rows
	var totalrow=row.length + parseInt(BoxNum)
	for (i=row.length;i< totalrow ;i++){
		var dynamicNewRow=document.getElementById('exist_file_table').insertRow()
		var dynamidatacol=dynamicNewRow.insertCell(0)
		//第一個表格
		//dynamidatacol.innerHTML='<table width="100%" border="1" cellpadding="0" cellspacing="1"><tr><td><input name="existBoxDel'+i+'" type="button" id="existBoxDel'+i+'" onclick="javascript:exist_DeleteRow('+i+')" value="刪除"></td><td width="15%"><font size="2">順序</font><input name="txt_existfile_sq'+i+'" type="text" id="txt_existfile_sq'+i+'" size="2" maxlength="5"></td><td width="42%"><font size="2">名稱</font><input name="txt_existfile_title'+i+'" type="text" size="18" id="txt_existfile_title'+i+'" maxlength="20"><input name="hid_existfile_num'+i+'" type="hidden" id="hid_existfile_num'+i+'"><span id="hid_existfile_file'+i+'">檔案</span></td><td width="43%"><font size="2">簡述</font><input name="txt_existfile_intro'+i+'" type="text" size="25" id="txt_existfile_intro'+i+'" maxlength="200"></td></tr></table>'
		dynamidatacol.innerHTML='<table width="100%" border="1" cellpadding="0" cellspacing="1" bordercolor="#efefef"><tr><td width="1%"><input name="existBoxDel'+i+'" type="button" id="existBoxDel'+i+'" onclick="javascript:exist_DeleteRow('+i+')" value="刪除"></td><td width="5%" nowrap>順序<input name="txt_existfile_sq'+i+'" type="text" id="txt_existfile_sq'+i+'" size="2" maxlength="5"></td><td width="26%" nowrap>名稱<input name="txt_existfile_title'+i+'" type="text" size="30" id="txt_existfile_title'+i+'" maxlength="30"><input name="hid_existfile_num'+i+'" type="hidden" id="hid_existfile_num'+i+'"></td><td><span id="hid_existfile_file'+i+'">檔案</span></td></tr></table>'
	}
	//每新增一個檔就算一次目前檔案數量
	row=document.getElementById('exist_file_table').rows //全部有多少列
	document.getElementById('hid_existfile_totalrow').value=row.length
}

function exist_DeleteRow(rowIndex){
	var row=document.getElementById('exist_file_table').rows //全部有多少列
	//將資料往上移，並將最後一行砍掉
	for (var i=rowIndex;i< row.length-1;i++){
		var rownum_temp=i+1
		//檔案順序
		document.getElementById('txt_existfile_sq'+i).value=document.getElementById('txt_existfile_sq'+rownum_temp).value
		//檔案編號
		document.getElementById('hid_existfile_num'+i).value=document.getElementById('hid_existfile_num'+rownum_temp).value
		//檔案資訊
		document.getElementById('hid_existfile_file'+i).innerHTML=document.getElementById('hid_existfile_file'+rownum_temp).innerHTML
		//檔案名稱
		document.getElementById('txt_existfile_title'+i).value=document.getElementById('txt_existfile_title'+rownum_temp).value
		//檔案備註
		//document.getElementById('txt_existfile_intro'+i).value=document.getElementById('txt_existfile_intro'+rownum_temp).value
	}
	document.getElementById('exist_file_table').deleteRow(row.length-1)	
	
	//每刪一個檔就算一次目前檔案數量
	row=document.getElementById('exist_file_table').rows //全部有多少列
	document.getElementById('hid_existfile_totalrow').value=row.length
}

