
function resize_Content_form(id,board,opt) {
	var obj = document.getElementById(id);
	if(obj) {
		var height = parseInt(obj.style.height.replace("px",""));
		if(opt == 1) {
			height = height+100;
		} else {
			if(height > 100)
			height = height-100;
		}
		obj.style.height = height+'px';
		setCookie(board+"_content_textarea_width_size",height,1);
	}
}

function setCookie(name, value, expireDays) {
  var todayDate = new Date();
	todayDate.setDate(todayDate.getDate() + expireDays);
	document.cookie = name + "=" + value + "; path=/; expires=" + todayDate.toGMTString() + ";";
}

function add_sitelink_num(id,idx,name) {
	var obj = document.getElementById(id);
	if(obj) {
		var lis = obj.getElementsByTagName('li');
		var s_num = lis.length;
		var li = document.createElement('li');
		var s_label = name+s_num+": 이름 ";
		var s_id1 = "article_sitename"+idx+"_"+s_num;
		var s_id2 = "article_sitelink"+idx+"_"+s_num;
		var s_name = "sitelink"+idx+"["+s_num+"][name]";
		var s_link = "sitelink"+idx+"["+s_num+"][link]";
		var l_txt1 = document.createTextNode(s_label);
		var l_txt2 = document.createTextNode(" URL ");
		var label1 = document.createElement("label");
		label1.setAttribute('for',s_id1);
		label1.appendChild(l_txt1);
		li.appendChild(label1);
		var inp = document.createElement('input');
		inp.setAttribute('type','text');
		inp.setAttribute('className','text');
		inp.setAttribute('class','text');
		inp.setAttribute('id',s_id1);
		inp.setAttribute('name',s_name);
		li.appendChild(inp);
		var label2 = document.createElement('label');
		label2.setAttribute('for',s_id2);
		label2.appendChild(l_txt2);
		li.appendChild(label2);
		var inp = document.createElement('input');
		inp.setAttribute('type','text');
		inp.setAttribute('className','text');
		inp.setAttribute('class','text');
		inp.setAttribute('id',s_id2);
		inp.setAttribute('name',s_link);
		li.appendChild(inp);
		obj.appendChild(li);
	}
}

function del_sitelink_num(id) {
	var obj = document.getElementById(id);
	if(obj) {
		var lis = obj.getElementsByTagName('li');
		if(lis.length == 2) {
			alert("2개 이하로 줄일 수 없습니다.");
			return;
		}
		var inp = lis[lis.length-1].getElementsByTagName('input');
		for(i=0; i<inp.length; i++) {
			if(inp[i].getAttribute("value") != "" && inp[i].value != "") {
				alert("입력값이 존재하는 경우에는 삭제할 수 없습니다.");
				return;
			}
		}
		obj.removeChild(lis[lis.length-1]);
	}
}

function iframe_autoresize(fr) {
	fr.height = eval(fr.name+".document.body.scrollHeight");
}

function autoReSizeWin() {
	var width = document.body.scrollWidth;
	var height = document.body.scrollHeight;
		  
	window.saveNavigator = window.navigator;
	navigator.OS    = '';
	navigator.version  = '';
						  
	var platform = "";
	if (typeof(window.navigator.platform) != 'undefined'){
		platform = window.navigator.platform.toLowerCase();
		if (platform.indexOf('win') != -1) navigator.OS = 'win';
	} 

	var i = 0;
	var ua = window.navigator.userAgent.toLowerCase();
																	  
	if ((i = ua.indexOf('msie')) != -1){
		var a_w = 0;
		var a_h = 57;
	} else if((i = ua.indexOf('opera')) != -1) {
		var a_w = 0; 
		var a_h = 67;
	} else {
		var a_w = 0;
		var a_h = 47;
	}

	window.resizeTo((width+a_w > screen.availWidth ? screen.availWidth : width+a_w), ((height+a_h) > screen.availHeight ? screen.availHeight : height+a_h)); 
	if(width+a_w < screen.availWidth && height+a_h < screen.availHeight) {
		document.getElementById('commune_body_wrap').style.overflow='hidden';
	}
}

function image_preview(url) {
	window.open(url,'','width=100,height=100');
}

String.prototype.mbLength = function() {
	var result = 0;
	for(var i=0; i<this.length; ++i) {
		var c = this.charAt(i);
		var enc = encodeURIComponent(c);
		result++;
		if(enc.length > 3) {
			result++;
		}
	}
	return result;
}

String.prototype.ltrim = function() {
	return this.replace(/^\s+/g, "");
}

String.prototype.rtrim = function() {
	return this.replace(/\s+$/g, "");
}

String.prototype.trim = function() {
	return this.ltrim().rtrim();
}

/*** XMLHttpRequest functions ***/

var xmlHttp;

function xmlRequest(requestURL, callbackFunction) {
	if(window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	// Send Asynchronous Request
	xmlHttp.open("GET", requestURL, true);
	xmlHttp.onreadystatechange = callbackFunction;
	xmlHttp.send(null);
}

function isResponseDone() {
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			return true;
		}
	}
	return false;
}

function getXMLResult(tagName) {
	var nodeList = xmlHttp.responseXML.getElementsByTagName(tagName);
	if(nodeList.length == 0) {
		return null;
	} else if(nodeList.length == 1) {
		return getNodeValue(nodeList[0]);
	} else {
		var result = new Array();
		for(var i=0; i<nodeList.length; ++i) {
			result.push(getNodeValue(nodeList[i]));
		}
		return result;
	}
}

function getRSSResult() {
	var result = new Array();
	var nodeList = xmlHttp.responseXML.getElementsByTagName("item");
	for(var i=0; i<nodeList.length; ++i) {
		var rssItem = new RSSItem();
		rssItem.set(nodeList[i]);
		result.push(rssItem);
	}
	return result;
}

/*** RSSItem : RSS Item Object ***/
RSSItem = function() {
	// members : author, link, desciption, pubDate
	this.author = "";
	this.link = "";
	this.description = "";
	this.pubDate = "";
	this.title = "";
}

RSSItem.prototype.set = function(itemNode) {
	var childs = itemNode.childNodes;
	for(var i=0; i<childs.length; ++i) {
		var nodeName = childs[i].nodeName.trim().toLowerCase();
		if(nodeName == "author") {
			this.author = getNodeValue(childs[i]);
		} else if(nodeName == "title") {
			this.title = getNodeValue(childs[i]);
		} else if(nodeName == "link") {
			this.link = getNodeValue(childs[i]);
		} else if(nodeName == "description") {
			this.description = getNodeValue(childs[i]);
		} else if(nodeName == "pubdate") {
			this.pubDate = getNodeValue(childs[i]);
		}
	}
}

/*** DOM functions ***/
function getNodeValue(node) {
	var childs = node.childNodes;
	for(var i=0; i<childs.length; ++i) {
		if(childs[i].nodeType == 3 || childs[i].nodeType == 4) {
			return childs[i].data.trim();
		}
	}
}

function recommand(url,callbackFunction) {
	xmlRequest(url,callbackVote);
}

function callbackVote() {
	var result = null;
	if(isResponseDone()) {
		result = getRSSResult();
		if(result != null) {
			voteUpdate("_vote",result);
		}
	}
}

function voteUpdate(id,result) {
	var obj = document.getElementById(id);
	if(obj) {
		if(result.length > 0) {
			var itemObj = result[0];
			if(itemObj.title == "vote") {
				obj.innerHTML = itemObj.description;
				alert('이 기사에 대한 추천이 완료되었습니다.');
			}
		}
	}
}

var boxmovie_button = '';

function Reset_BoxMovieList(board,id,type,img) {
	if(id) {
		var requestURL = "./write2_xml.php?board="+board+"&write2="+type+"&id="+id+"&subject_len=68&cont_len=74";
	} else {
		var requestURL = "./write2_xml.php?board="+board+"&write2="+type+"&mode=new&subject_len=68&cont_len=74";
	}
	boxmovie_button = img;
	if(type == 'box')
		xmlRequest(requestURL,callbackBoxList);
	else if(type == 'movie')
		xmlRequest(requestURL,callbackMovieList);
}

function callbackBoxList() {
	var result = null;
	if(isResponseDone()) {
		result = getRSSResult();
		if(result != null) {
			BoxListReset('news-write2-box-ul',result)
		}
	}
}

function callbackMovieList() {
	var result = null;
	if(isResponseDone()) {
		result = getRSSResult();
		if(result != null) {
			MovieListReset('news-write2-movie-ul',result)
		}
	}
}

function BoxListReset(id,result) {
	var obj = document.getElementById(id);
	var reg = /\[%=박스\d+%\]/ig;
	if(obj) {
		var lis = obj.getElementsByTagName('li');
		for(var i=lis.length-1; i>=0; i--) {
			obj.removeChild(lis[i]);
		}
		for(i=0; i<result.length; i++) {
			var itemObj = result[i];
			if(itemObj.author.match(reg)) {
				var li = document.createElement('li');
				var sp = document.createElement('span');
				sp.innerHTML = itemObj.author;
				li.appendChild(sp);
				var txt = document.createTextNode(itemObj.title);
				li.appendChild(txt);
				var p = document.createElement('p');
				var a = document.createElement('a');
				var sp1 = document.createElement('span');
				sp1.setAttribute('className','button');
				sp1.setAttribute('class','button');
				var txt1 = document.createTextNode("수정");
				sp1.appendChild(txt1);
				a.setAttribute("href","javascript://");
				a.onclick= function () {
					pop_open(itemObj.link);
				}
				a.appendChild(sp1);
				p.appendChild(a);
				li.appendChild(p);
				obj.appendChild(li);
			}
		}
		var height = document.getElementById('cmedia_write2').scrollHeight;
		parent.document.getElementById('write2_frame').height = height;
	}
}

function MovieListReset(id,result) {
	var obj = document.getElementById(id);
	var reg = /\[%=영상\d+%\]/ig;
	if(obj) {
		var lis = obj.getElementsByTagName('li');
		for(var i=lis.length-1; i>=0; i--) {
			obj.removeChild(lis[i]);
		}
		for(i=0; i<result.length; i++) {
			var itemObj = result[i];
			if(itemObj.author.match(reg)) {
				var li = document.createElement('li');
				var sp = document.createElement('span');
				sp.innerHTML = itemObj.author;
				li.appendChild(sp);
				var p = document.createElement('p');
				var a = document.createElement('a');
				var sp1 = document.createElement('span');
				sp1.setAttribute('className','button');
				sp1.setAttribute('class','button');
				var txt1 = document.createTextNode("수정");
				sp1.appendChild(txt1);
				a.setAttribute("href","javascript://");
				a.onclick= function () {
					pop_open(itemObj.link);
				}
				a.appendChild(sp1);
				p.appendChild(a);
				li.appendChild(p);
				obj.appendChild(li);
			}
		}
		var height = document.getElementById('cmedia_write2').scrollHeight;
		parent.document.getElementById('write2_frame').height = height;
	}
}

function ShowHideWrite2() {
	var i,id,v,t_obj,obj,argc,args=ShowHideWrite2.arguments;
	argc = args.length;
	for(i=0; i<argc; i++) {
		var id= args[i]+"_title";
		if((t_obj = document.getElementById(id)) && (obj = document.getElementById(args[i]))) {
			v = args[i+1];
			if(v == 'show') {
				t_obj.setAttribute("className","focus");
				t_obj.setAttribute("class","focus");
				obj.style.display = 'block';
			} else {
				t_obj.setAttribute("className","");
				t_obj.setAttribute("class","");
				obj.style.display = 'none';
			}
		}
		i++;
	}
	var height = document.getElementById('cmedia_write2').scrollHeight;
	parent.document.getElementById('write2_frame').height = height+10;
}

function pop_open(URL) {
	window.saveNavigator = window.navigator;
	navigator.OS    = '';
	navigator.version  = '';

	var platform = "";
	if (typeof(window.navigator.platform) != 'undefined'){
		platform = window.navigator.platform.toLowerCase();
		if (platform.indexOf('win') != -1) navigator.OS = 'win';
	}
	
	var i = 0;
	var ua = window.navigator.userAgent.toLowerCase();

	if ((i = ua.indexOf('msie')) != -1){
		var a_w =	700;
	} else if((i = ua.indexOf('opera')) != -1) {
		var a_w =	600;
	} else {
		var a_w =	630;
	}

	window.open(URL,'_write_pop','width='+a_w+',height=500');
}

