function maximumLength(ele,number){
	if (ele.value.length > number) {
		ele.value = ele.value.substr(0,200);
	}
}
function goUp(vThis){
	var all = document.getElementById("post");
	var parent = all;
	all = document.getElementsByTagName("li");
	var now;
	if(typeof(this.parentNode) == 'undefined') now = vThis.parentNode.parentNode;
	else now = this.parentNode.parentNode;	
	for(var i=1;i<all.length;i++){		
		if(all[i] == now) {
			var img = document.getElementsByName("img[]");
			var isi = document.getElementsByName("isi[]");
			var imgShow = document.getElementsByName("imgShow[]");
			var imgHide = document.getElementsByName("imgHide[]");
			if(imgShow.length>1){
				var tem3 = imgShow[i].cloneNode(true);
				var tem4 = imgShow[i-1].cloneNode(true);
				imgShow[i-1].parentNode.replaceChild(tem3, imgShow[i-1]);
				imgShow[i].parentNode.replaceChild(tem4, imgShow[i]);
				
				tem3 = imgHide[i].value;
				imgHide[i].value = imgHide[i-1].value;
				imgHide[i-1].value = tem3;
			}
			var tem = img[i].cloneNode(true);		
			var tem2 = img[i-1].cloneNode(true);
			img[i-1].parentNode.replaceChild(tem, img[i-1]);
			img[i].parentNode.replaceChild(tem2, img[i]);
			
			tem = isi[i-1].value;
			isi[i-1].value = isi[i].value;
			isi[i].value = tem;
			break;
		}
	}
}
function goDown(vThis){
	var all = document.getElementById("post");
	var parent = all;
	all = document.getElementsByTagName("li");
	var now;
	if(typeof(this.parentNode) == 'undefined') now = vThis.parentNode.parentNode;
	else now = this.parentNode.parentNode;	
	for(var i=0;i<all.length-1;i++){		
		if(all[i] == now) {
			var img = document.getElementsByName("img[]");
			var isi = document.getElementsByName("isi[]");
			var imgShow = document.getElementsByName("imgShow[]");
			var imgHide = document.getElementsByName("imgHide[]");
			if(imgShow.length>1){
				var tem3 = imgShow[i].cloneNode(true);
				var tem4 = imgShow[i+1].cloneNode(true);
				imgShow[i+1].parentNode.replaceChild(tem3, imgShow[i+1]);
				imgShow[i].parentNode.replaceChild(tem4, imgShow[i]);
				
				tem3 = imgHide[i].value;
				imgHide[i].value = imgHide[i+1].value;
				imgHide[i+1].value = tem3;
			}
			var tem = img[i].cloneNode(true);		
			var tem2 = img[i+1].cloneNode(true);
			img[i+1].parentNode.replaceChild(tem, img[i+1]);
			img[i].parentNode.replaceChild(tem2, img[i]);
			
			tem = isi[i+1].value;
			isi[i+1].value = isi[i].value;
			isi[i].value = tem;
			break;
		}
	}
}
function deletePost(vThis){
	var toDel;
	if(typeof(this.parentNode) == 'undefined') toDel = vThis.parentNode.parentNode;
	else toDel = this.parentNode.parentNode;	
	toDel.parentNode.removeChild(toDel);
}
function addPost(mode){
	/*
	<li class="postItem" name="post[]">
		Image : <input name="img[]" type="file"><br>
		Isi (5000 karakter) : <br>
		<textarea name="isi[]"></textarea>
		<div class="arrow">
			<img src="index.php_files/close.png">
			<img src="index.php_files/arrowUp.png">
			<img src="index.php_files/arrowDown.png">
		</div>
	</li>
	*/
	var toAdd = document.getElementById("post");
	
	var post = document.createElement("li");
	post.setAttribute('name','post[]');
	post.setAttribute('class','postItem');
	
	var img = document.createElement("input");
	img.setAttribute("type","file");
	img.setAttribute("name","img[]");
	
	var arrowUp = document.createElement("img");
	arrowUp.setAttribute("src","style/arrowUp.png");
	arrowUp.addEventListener('click', goUp, false);	
	
	var arrowDown = document.createElement("img");
	arrowDown.setAttribute("src","style/arrowDown.png");
	arrowDown.addEventListener('click', goDown, false);
	
	var close = document.createElement("img");
	close.setAttribute("src", "style/close.png");
	close.addEventListener('click', deletePost, false);
	
	var arrow = document.createElement("div");
	arrow.setAttribute("class","arrow");
	arrow.appendChild(close);
	arrow.appendChild(arrowUp);
	arrow.appendChild(arrowDown);	
	
	var ta = document.createElement("textarea");
	ta.setAttribute("name","isi[]");	
	if(mode == 'edit'){
		var imgShow = document.createElement("img");
		imgShow.setAttribute("src","image/no_img.png")
		imgShow.setAttribute("name","imgShow[]")
		post.appendChild(imgShow);
		post.appendChild(document.createElement("br"));
		
		var imgHide = document.createElement('input');
		imgHide.setAttribute("type","hidden");
		imgHide.setAttribute("name","imgHide[]");
		imgHide.setAttribute("value","image/no_img.png");
		post.appendChild(imgHide);
	}
	post.appendChild(document.createTextNode("Image : "));
	post.appendChild(img);
	post.appendChild(document.createElement("br"));
	post.appendChild(document.createTextNode("Isi (5000 karakter) : "));
	post.appendChild(document.createElement("br"));
	post.appendChild(ta);
	post.appendChild(arrow);
	toAdd.appendChild(post);
}
function newPostValidation(){
	var err = '';
	var mode = "new_post";
	if(typeof(document.forms[mode]) == 'undefined') mode = "edit_post";
	if (trim(document.forms[mode]['judul'].value) == '') err+="Judul harus diisi \n";
	if (trim(document.forms[mode]['deskripsi'].value) == '') err+="Deskripsi harus diisi";
	if(err == ''){
		return true;
	}else{
		alert(err);
		return false;
	}
}
function trim(str, chars){
	return ltrim(rtrim(str, chars), chars);
}
function ltrim(str, chars){
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
function rtrim(str, chars){
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
