// JavaScript Document

function ClsForums(){
}

ClsForums.prototype = {

	downloadFile: function(sfile, ufile){
		var browser=getBrowser();
		document.form_forum.file.value = sfile;
		document.form_forum.file_upload.value = ufile;
		document.form_forum.tempfilename.value = sfile;
		document.form_forum.browser.value=browser;
		document.form_forum.action = "download_files.php";
		document.form_forum.submit();		
	},
	functest: function()
	{
		alert('func');
	},
	
	search_title: function(){
		
		document.form_forum.action = "forum.php";
		document.form_forum.submit();
	},
	
	validateForm: function(id){
		
		var lang	= document.form_forum.session_lang.value;
		var name 	= document.form_forum.name;
		var email 	= document.form_forum.email;
		var title	= document.form_forum.forum_title;
		
		if( trim(name.value) == '' ){
			if(lang == 'KO') alert('이름을 적어주세요!');
			else alert('Please input your name!');
			name.focus();
			return false;
		}
		
		if( trim(email.value) == '' ){
			if(lang == 'KO') alert('이메일 주소를 적어주세요!');
			else alert('Please input your email!');
			email.focus();
			return false;
		}
		
		if(! validateEmail(email) ){
			if(lang == 'KO') alert('이메일 주소 확인 후 다시  적어주세요!');
			else alert('Please enter a valid email address!');
			email.focus();
			return false;
		}
		
		if( trim(title.value) == '' ){
			if(lang == 'KO') alert('제목을 적어주세요!');
			else alert('Please input the title!');
			title.focus();
			return false;
		}
		
		if( trim(this.getEditorContent()) == '' ){
			if(lang == 'KO') alert('질문 내용을  forum에 적어주세요!');
			else alert('Please enter the content of your forum!');
			FCKeditor.focus();
			alert(1);
			return false;
		}
		
		document.form_forum.submit();
	},
	

	getEditorContent: function(){
		var oEditorHTML = FCKeditorAPI.GetInstance('FCKeditor').GetHTML();
		return oEditorHTML;
	},
	
	validateReply: function(){
		
		if( trim(this.getEditorContent()) == '' ){
			alert('Please enter your reply!');
			FCKeditor.focus();
			return false;
		}
		
		document.form_forum.action = "forum_reply_confirm.php";
		document.form_forum.submit();
	},
	
	edit_file: function(id){	
		document.form_forum.action = "forum_edit.php?id="+id;
		document.form_forum.submit();
	},
	
	reset_page: function(){
	
		document.form_forum.reset();
		// If the editor API does not exist, there are no editors 
		if (typeof FCKeditorAPI == "undefined") return; 
	
		// Loop through all FCK instances, in case there are several editors 
		for (var sEditorName in FCKeditorAPI.__Instances) 
		{ 
			if (document.getElementById(sEditorName)) 
			{ 
				// Get the initial value 
				var sInitialValue = document.getElementById(sEditorName).value; 
	
				// Overwrite the editor's current value 
				FCKeditorAPI.__Instances[sEditorName].SetHTML(sInitialValue); 
			} 
		}
	},
	
	forum_delete: function(){
		
		var del = document.getElementsByName('check[]');
		var check = 0;
		
		for(var i=0; i<del.length; i++){

			if(del[i].checked){
				check = 1;
				break;
			}
		}

		if( check == 0 ){
			alert('Select at least 1 forum to be deleted!');
			return false;
		}
		
		document.form_forum.action = 'forum_delete.php';
		document.form_forum.submit();
	},
	
	forum_modify: function(id, admin){
		var del = document.getElementsByName('check[]');
		var fuid = document.getElementsByName('fuid[]');
		var check = 0;
		var cnt = 0;
		var permit=false;
		var forum_id;
		
		for(var i=0; i<del.length; i++){
			if(del[i].checked){
				cnt++;
			}
			if(del[i].checked && fuid[i].value==id)
				forum_id = del[i].value;
				permit=true;
		}
		if(admin == '1')
		permit = true;
		
	    if(cnt==0){
	    	alert('Select 1 forum to be modified!');
	    }  
	    else if(cnt>1){
	    	alert('Select only 1 forum to be modified!');
	    }
	    else if(!permit){
			alert('Sorry, you are not permitted to modify this forum!');
			return false;
		}	    
	    else{
	    	document.form_forum.action = 'forum_reply_modify.php?id='+forum_id;
	    	document.form_forum.submit();
	    }
	}
}

var JSForums = new ClsForums();
