

function addtag(inputid, htmlid) {
	var tagtext = document.getElementById(htmlid).innerHTML;
	var tag = document.getElementById(inputid).value;
	
	if(checktag(tag)) {
		newtag = '<input type="button" name="tagnamebtn[]" value="'+tag+'" onclick="deletetag(this)"><input type="hidden" name="tagname[]" id="tagnameid'+tag+'" value="'+tag+'">';
		document.getElementById(htmlid).innerHTML += newtag;
	}
	document.getElementById(inputid).value = '';
}

function addtagname(tagname, htmlid) {
	var tagtext = document.getElementById(htmlid).innerHTML;
	var tag = tagname;
	if(checktag(tag)) {
		newtag = '<input type="button" name="tagnamebtn[]" value="'+tag+'" onclick="deletetag(this)"><input type="hidden" name="tagname[]" id="tagnameid'+tag+'" value="'+tag+'">';
		document.getElementById(htmlid).innerHTML += newtag;
	}
}


function deletetag(thebtn) {
	if(confirm('確定刪除TAG: '+thebtn.value+'?')) {
		document.getElementById('tagnameid'+thebtn.value).disabled = true;
		thebtn.disabled = true;
		thebtn.style.display = "none";
	}
}

function checktag(Sting) {

	if(Sting.length < 2 || Sting.length > 10) {
		alert('TAG長度不符合要求');
		return false;
	}
	//常用的符號
	var compStr = "§□☆★○●◎◇◆□■△▲※→←↑↓═＃＆＠＼︿ˍ￣【】【】（）〔〕｛｝．』『」「》《〉〈〕〔『』「」□∼∥︰〞′『｜‥ˇ‾·—…！？：；，、。  ~!@#$%^&*()+={}|[]\\:;\"'<,>?/";
    var length2 = Sting.length;
	
	for (var iIndex=0;iIndex<length2;iIndex++) {
		var temp1 = compStr.indexOf(Sting.charAt(iIndex));
		if(temp1>=0){
			alert('TAG名中包含非法字符');
			return false;
		}
	}
	return true;
}

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 ShowHide(id) {
	if ( itm = document.getElementById(id) ) {
		if (itm.style.display == 'none'){
			itm.style.display = '';
		} else {
			itm.style.display = 'none';
		}
	}
}

function changevalue(id, value) {
	var obj = document.getElementById(id);
	obj.value = value;
}

function validate(theform) {
	var btn = document.getElementById("thevaluesubmit");
	if(btn) btn.disabled = true;
	
	//標題
	var subject = document.getElementById("subject");
	if(subject) {
		if (subject.value.length < 2 || subject.value.length > 80) {
			alert("您輸入的標題長度不符合要求(2~80)，請檢查確認。");
			subject.focus();
			if(btn) btn.disabled = false;
			return false;
		}
	}
	
	//標題
	var catid = document.getElementById("catid");
	if(catid) {
		if (catid.value<1) {
			alert("請為您的信息選擇一個系統分類");
			catid.focus();
			if(btn) btn.disabled = false;
			return false;
		}
	}

	//文件後綴名
	var fileext = document.getElementById("fileext");
	if(fileext) {
		if (fileext.value.length < 1 || fileext.value.length > 15) {
			alert("您輸入的文件後綴名長度不符合要求，請檢查確認。");
			fileext.focus();
			if(btn) btn.disabled = false;
			return false;
		}
	}

	//名稱
	var name = document.getElementById("name");
	if(name) {
		if (name.value.length < 1 || name.value.length > 50) {
			alert("您輸入的名稱長度不符合要求，請檢查確認。");
			name.focus();
			if(btn) btn.disabled = false;
			return false;
		}
	}

	//組名稱
	var groupname = document.getElementById("groupname");
	if(groupname) {
		if (groupname.value.length < 1 || groupname.value.length > 50) {
			alert("您輸入的組名稱長度不符合要求，請檢查確認。");
			groupname.focus();
			if(btn) btn.disabled = false;
			return false;
		}
	}

	//用戶名稱
	var username = document.getElementById("username");
	if(username) {
		if (username.value.length < 1 || username.value.length > 30) {
			alert("您輸入的用戶名稱長度不符合要求，請檢查確認。");
			username.focus();
			if(btn) btn.disabled = false;
			return false;
		}
	}
	
	//類型名稱
	var typename = document.getElementById("typename");
	if(typename) {
		if (typename.value.length < 1 || typename.value.length > 30) {
			alert("您輸入的名稱長度不符合要求，請檢查確認。");
			typename.focus();
			if(btn) btn.disabled = false;
			return false;
		}
	}
	
	//類型名稱
	var field = document.getElementById("field");
	if(field) {
		if (field.value.length < 1 || field.value.length > 30) {
			alert("您輸入的名稱長度不符合要求，請檢查確認。");
			field.focus();
			if(btn) btn.disabled = false;
			return false;
		}
	}
	
	//空間名稱
	var spacename = document.getElementById("spacename");
	if(spacename) {
		if (spacename.value.length < 1 || spacename.value.length > 30) {
			alert("您輸入的空間名稱長度不符合要求，請檢查確認。");
			spacename.focus();
			if(btn) btn.disabled = false;
			return false;
		}
	}
	
	//空間名稱
	var importtext = document.getElementById("importtext");
	if(importtext) {
		if (importtext.value.length < 1 || importtext.value.length > 30) {
			alert("您輸入的導入文本長度不符合要求，請檢查確認。");
			importtext.focus();
			if(btn) btn.disabled = false;
			return false;
		}
	}
	
	//頁面自定義
	if(typeof window.thevalidate == 'function') {
		if(thevalidate(theform)) {
			return true;
		} else {
			if(btn) btn.disabled = false;
			return false;
		}
	}

	return true;
}

function listsubmitconfirm(theform) {
	theform.listsubmit.disabled = true;
	if(confirm("您確定要執行本操作嗎？")) {
		theform.listsubmit.disabled = false;
		return true;
	} else {
		theform.listsubmit.disabled = false;
		return false;
	}
}

function getid(id) {
	return document.getElementById(id);
}

function uploadFile(mode) {
	var theform = document.getElementById("theform");
	var msg = document.getElementById("divshowuploadmsg");
	var msgok = document.getElementById("divshowuploadmsgok");
	var oldAction = theform.action;
	var oldonSubmit = theform.onSubmit;
	msgok.style.display = 'none';
	msg.style.display = '';
	msg.innerHTML = "正在上傳中，請您稍等......";
	theform.action = siteUrl + "/batch.upload.php?mode=" + mode;
	theform.onSubmit = "";
	theform.target = "phpframe";
	theform.submit();
	theform.action = oldAction;
	theform.onSubmit = oldonSubmit;
	theform.target = "";
	if(mode ==2) {
		delpic();
	}
	return false;
}

function attacheditsubmit(aid) {
	var theform = document.getElementById("theform");
	var oldAction = theform.action;
	var oldonSubmit = theform.onSubmit;
	theform.action = siteUrl + "/batch.upload.php?editaid=" + aid;
	theform.onSubmit = "";
	theform.target = "phpframe";
	theform.submit();
	theform.action = oldAction;
	theform.onSubmit = oldonSubmit;
	theform.target = "";
	return false;
}

/**
 * showmenu/hidemenu 會用hideshowtags取代
 */
function showmenu(id) {
	var thismenu=document.getElementById(id);
	thismenu.style.display="block";
}
function hidemenu(id) {
	var thismenu=document.getElementById(id);
	thismenu.style.display="none";
}
/**
 * 隱藏並顯示上傳DIV中的指定標籤
 * @param string id: 待顯示的標籤ID
 */
function hideshowtags(upid,id) {
	//獲取上傳DIV對像
	var uploadobj = getbyid(upid).getElementsByTagName('div');
	for(var i=0; i< uploadobj.length; i++) {
		if(uploadobj[i].id.indexOf('upload') != -1) {
			uploadobj[i].style.display = "none";
		}
	}
	var showtagobj = getbyid(id);
	showtagobj.style.display = "block";
}
/***************批量上傳調用開始***********************/
/**
 * 插入一個預覽圖片
 * @param object obj: 上傳文本框對像
 */
function insertimg(obj) {
	var childnum = getbyid("batchdisplay").getElementsByTagName("input");	//獲取上傳控制個數
	var upallowmax = getbyid("uploadallowmax").value;
	//判斷是否超過限制
	if(upallowmax < childnum.length ) {
		alert("一次允許最多上傳"+ upallowmax +"個");
		return false;
	}
	//獲取最高ID
	var id =getmaxid();
	//添加默認的DIV
	var pichtml = '';
	var src = obj.value;
	
	//判斷文件類型
	var patn = /\.jpg$|\.jpeg$|\.gif$|\.png$|\.bmp$/i;
	var filetype = 'file';
	if(patn.test(src)){
		filetype = 'image';
		
	}
	
	var temp_title = src.substr(src.lastIndexOf('\\')+1);
	if(src.lastIndexOf('.') != -1){
		temp_title = temp_title.substr(0,(src.substr(src.lastIndexOf('\\')+1)).lastIndexOf('.'));
	}
	pichtml += '<div class="picspace" id="pic_space_' + id + '">';
	pichtml += '<div class="delimg" onmouseover="mouseoverpic(' + id + ', 1)" id="del_pic_button'  + id + '"><img src="./admin/images/pic_delete.gif" width="18" height="17" border="0" onclick="delpic('+id+');" title="取消上傳"></div>';
	pichtml += '<div class="picbox" onmouseover="mouseoverpic(' + id + ', 1)" onmouseout="mouseoutpic(' + id + ', 1)">';
	if (navigator.userAgent.toLowerCase().indexOf('ie') < 0 || filetype != 'image') {
		pichtml += '<img src="admin/images/upload_file.gif" alt="upload file" class="nopreview" />';
	} else {
		pichtml += '<img src="' + src + '" id="pic_item_id_' + id + '" onload="javascript:if(this.width>105){this.height*=105/this.width;this.width=105};if(this.height>105){this.width*=105/this.height;this.height=105};" />';
	}
	pichtml += '</div>';
	pichtml += '<div class="pictext" id="pic_text_' + id + '">';
	pichtml += '<label id="label_pic_' + id + '" title="'+getStrbylen(temp_title,40)+'" onmouseover="mouseoverpic(' + id + ', 0)" onmouseout="mouseoutpic(' + id + ', 0)"><span id="pic_' + id + '">'+getStrbylen(temp_title,16)+'</span><input id="pic_input_' + id + '" type="text" name="picname[]" value="'+temp_title+'"/></label>';
	pichtml += '</div><div style="display:none;"><input type="text" name="title[]" value="'+ temp_title +'" id="pic_title_' + id + '" /><input type="text" name="thumb[]" value="'+ (getbyid('uploadthumb2') == null?"":getbyid('uploadthumb2').value) +'"/></div>';
	pichtml += '</div>';

	// 把圖片框加到pic_space_main中去
	var picmain = getbyid("batchpreview");
	picmain.innerHTML = picmain.innerHTML + pichtml;
	obj.style.display = 'none';	//隱藏當前的上傳對像
	var newid = id+1;	//獲取下一個最大的上傳ID並創建新的上傳控件
	addupload(newid);	//創建一個新的上傳控制
}
/**
 * 添加一個新的上傳對像控件
 * @param int newid: 新的上傳控件ID後綴
 */
function addupload(newid) {
	//兩種生成方式，解決瀏覽器之間的兼容性問題
	try{
		//IE模式下的創建方式,解決常規setAttribute設置屬性帶來的一些未知的錯誤
		var uploadHTML = document.createElement("<input type=\"file\" size=\"1\" id=\"batch_" + newid + "\" name=\"batchfile[]\" onchange=\"insertimg(this)\" class=\"fileinput\">");
		getbyid("batchdisplay").appendChild(uploadHTML);
	}catch(e) {
		//非IE模式則須要用下列的常規setAttribute設置屬性，否則生成的結果不能正常初始化
		var uploadobj = document.createElement("input");
		uploadobj.setAttribute("name", "batchfile[]");
		uploadobj.setAttribute("onchange", "insertimg(this)");
		uploadobj.setAttribute("type", "file");
		uploadobj.setAttribute("id", "batch_" + newid);
		uploadobj.setAttribute("class", "fileinput")
		getbyid("batchdisplay").appendChild(uploadobj);
	}
}
/**
 * 得到頁面中可用的最大ID號
 * 寫這個函數主要是因為不能通過圖片的個數來計算可用的最大ID號.
 * 圖片是可以取消的,如果取消,則通過圖片個數算出的最大ID號會與已有圖片的ID號重疊.
 */
function getmaxid() {
	var items = getbyid("batchdisplay").getElementsByTagName("input");
	var count = items.length;
	var id = 0;
	for (var i=0; i<count; i++) {
		if(items[i].id.substr(0, 6) == "batch_") {
			tmp_id = new Number(items[i].id.substr(6));
			if(id < tmp_id) {
				id = tmp_id;
			}
		}
	}
	if(id == 0) {
		return 1;
	}
	id = new Number(id);
	return id;
}
/**
 * 截取指定字符串長度
 * @param string str: 要截取的字符串
 * @param int len: 要截取的長度
 * @return 
 */
function getStrbylen(str, len) {
	var num = 0;
	var strlen = 0;
	var newstr = "";
	var laststrlen = 1;
	var obj_value_arr = str.split("");
	for(var i = 0; i < obj_value_arr.length; i ++) {
		if(i < len && num + byteLength(obj_value_arr[i]) <= len) {
			num += byteLength(obj_value_arr[i]);
			strlen = i + 1;
		}
	}
	if(str.length > strlen) {
		if(byteLength(str.charAt(strlen-1)) == 1){
			laststrlen = 2;
		}
		newstr = str.substr(0, strlen-laststrlen) + '…';
	} else {
		newstr = str;
	}
	return newstr;
}
/**
 * 判斷中英問混排時候的長度
 * @param string sStr: 混排的字符串
 */
function byteLength (sStr) {
	aMatch = sStr.match(/[^\x00-\x80]/g);
	return (sStr.length + (! aMatch ? 0 : aMatch.length));
}
/**
 * 鼠標移到預覽圖事件
 * @param int id: 操作對像ID反綴
 * @param int optype: 操作類型
 */
function mouseoverpic(id,optype) {
	if(optype == 1) {
		var delpicbutton = getbyid("del_pic_button" + id);
		if(delpicbutton.style.display != "block") {
			delpicbutton.style.display="block";
		}
	} else if(optype == 0) {
		getbyid("pic_" + id).style.display = "none";
		getbyid("pic_input_" + id).style.display = "block";
	}
}
/**
 * 鼠標移出預覽圖事件
 * @param int id: 操作對像ID反綴
 * @param int optype: 操作類型
 */
function mouseoutpic(id, optype) {
	if(optype == 1) {
		var delpicbutton = getbyid("del_pic_button" + id);
		delpicbutton.style.display="none";
	} else if(optype == 0) {
		var picobj = getbyid("pic_" + id);
		var inputobj = getbyid("pic_input_" + id);
		var labelobj = getbyid("label_pic_" + id);
		picobj.style.display = "block";
		inputobj.style.display = "none";
		//判斷是否為空，如果為空則取默認的文件名稱
		var re = /\s/ig;
   		var result = inputobj.value.replace(re, "");
		if(result == "") {
			inputobj.value = getbyid("pic_title_" + id).value;
		}
		picobj.innerText = inputobj.value;
		labelobj.title = inputobj.value;
	}
}
/**
 * 刪除全部預覽圖或其中的某一張
 * @param int pid: 要刪除的預覽圖ID後綴，如沒有填寫則刪除全部的預覽圖
 */
function delpic(pid) {
	//判斷是否有傳刪除的圖片ID，如果沒有則刪除全部的圖片
	if(typeof pid != "undefined") {
		getbyid("batchpreview").removeChild(getbyid("pic_space_"+pid));
		//刪除相對應的上傳控件
		getbyid("batchdisplay").removeChild(getbyid("batch_" + pid));
	} else {
		//獲取全部圖片對像
		var previewobj = getbyid("batchpreview");
		var allobj = previewobj.getElementsByTagName("div");
		for(var i = allobj.length - 1; 0<=i; i--) {
			if(allobj[i].id.indexOf("pic_space_") != -1) {
				previewobj.removeChild(allobj[i]);
			}
		}
		var bdisplay = getbyid("batchdisplay");
		var allupobj = bdisplay.getElementsByTagName("input");
		for(var i = allupobj.length - 1; 0<=i; i--) {
			bdisplay.removeChild(allupobj[i]);
		}
		addupload(1);
	}
	return false;
}
/***************批量上傳調用結束***********************/
//顯示工具條
function hidetoolbar() {
	window.parent.document.getElementById("toolbarframe").style.display="none";
}
function hidetoolbarOpera() {
	if (navigator.userAgent.toLowerCase().indexOf('opera') > -1) {
		window.parent.document.getElementById("toolbarframe").style.display="none";
	}
}

function showtoolbar() {
	document.getElementById("toolbarframe").style.display = "block";
}
