var parity_code;
var user_key = 0;
var user_id = '';

var my_key = 0;
var my_nick = '';
var my_id = '';
var my_picture = '';

var request = null;

function createRequest()
{
	try
	{
		request = new XMLHttpRequest();
	}
	catch(trymicrosoft)
	{
		try
		{
			request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(othermicrosoft)
		{
			try
			{
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch(failed)
			{
				request = null;
			}
		}
	}

	if(request == null) location.reload();
}




function Keycode(e){
	var result = 0;
	if(window.event)
		result = window.event.keyCode;
	else if(event)
		result = e.which;

	return result;
}

function EditKeyDown(e, EditorID, LengthID, ajaxSend)
{
	var code = Keycode(e);
	if (code == 13 && e.shiftKey == false)
	{
		Submit_Post(EditorID, LengthID, ajaxSend);
		return false;
	}

	return true;
}

function user_login(e)
{
	var code = Keycode(e);
	if (code == 13)
	{
		ajaxLoginSend();
		return false;
	}

	return true;
}

function Edit_Checker(EditorID, LengthID)
{
	var obj = document.getElementById(EditorID);
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : "";

	document.getElementById(LengthID).innerHTML = '('+obj.value.length+'/200)';

	if (obj.getAttribute && obj.value.length > mlength)
		document.getElementById(LengthID).style.color = '#FF0000';
	else
		document.getElementById(LengthID).style.color = '';
}


var post_idx = 0;
var post_title = '';
var post_content = '';
function PostGenerate(writer_nick, user_id, parent, title, content, trackback, picture, isReply)
{
	var result = '';
	result += '<div class="Post">\n';
	result += '	<div id="NewPost_Main_'+post_idx+'" class="Post_Main" style="padding-top:10px;' + (isReply == true? 'padding-bottom:10px;' : '') + '">\n';
	result += '		<img class="Post_Image" src="./personal/' + (picture==''?'anybody':picture) + '.jpg">\n';
	result += '		<div class="Post_Wrapper" ' + (isReply == false?'style="cursor:pointer;" onclick="Post_History(\'' + user_id + '\', '+parent+', -610);"':'') + '>\n';
	result += '			<div class="PostWriter" style="font-weight:bold;">\n';
	result += '				<b>'+(writer_nick==''?'비회원':writer_nick)+'</b> : '+title + '\n';
	result += '			</div>\n';
	result += '			<div class="PostContent smartOutput" style="width:480px;padding-left:10px;">\n';
	result += content;
	result += '			</div>\n';

if (trackback != "") {
	result += '			<div class="PostContent" style="width:480px;padding-left:10px;padding-bottom:10px;">\n';
	result += '				<b>TrackBack</b> : <a href="'+ trackback.replace(/http:\/\/www.gpgstudy.com\/forum\/tb\//g, "http://gpgstudy.com/forum/viewtopic.php?topic=") + '" target="_blank">' + trackback + '</a>\n';
	result += '			</div>\n';
}
	result += '		</div>\n';
	result += '		<div class="FloatClear"></div>\n';
if (isReply != true)
{
	result += '		<div class="Post_Reply" style="float:right;">\n';
	result += '			<div class="PostContent" style="width:490px;height:22px;padding-top:10px;padding-bottom:10px;text-align:right;">\n';
	result += '				<b><a href="#" onclick="this.style.display=\'none\';document.getElementById(\'NewPost_Reply_'+post_idx+'\').style.display = \'\'; return false;">Reply</a></b>\n';
	result += '				<div id="NewPost_Reply_'+post_idx+'" class="PostContent" style="display:none;">\n';
	result += '					<input type="text" style="height:18px;width:420px;border-style:solid;border-width:1px;float:left;" onclick="return false;">\n';
	result += '					<input type="button" class="button blue" value="Reply" style="height:22px;width:60px;" onclick="return false;">\n';
	result += '					<div class="FloatClear"></div>\n';
	result += '				</div>\n';
	result += '			</div>\n';
	result += '		</div>\n';
}
	result += '	</div>\n';
	result += '</div>\n';

	return result;
}

var strText = '';
function ajaxBasicRequest()
{
	if (request == null)
		createRequest();
		
	if(request.readyState == 4)
	{
		if(request.status == 200)
		{
			strText = request.responseText;

			if (strText.indexOf('<script language=\'javascript\'>') != -1 || strText.indexOf('<script language="javascript">') != -1)
			{
				strText = strText.replace(/<script language='javascript'>/g, '');
				strText = strText.replace(/<script language="javascript">/g, '');
				strText = strText.replace(/<\/script>/g, '');

				eval(strText);
			}
			else
			{
				return true;
			}
		}
	}
	return false;
}

function ajaxPostRequest()
{
	if (ajaxBasicRequest())
	{
		var this_key = parseInt(strText);
		document.getElementById("WritedPost").innerHTML = PostGenerate(my_nick, my_id, this_key, post_title, post_content, '', my_picture, false) + document.getElementById("WritedPost").innerHTML;
	}
}
function ajaxReplyRequest()
{
	if (ajaxBasicRequest())
	{
		var this_key = parseInt(strText);
		document.getElementById("WritedReply").innerHTML += PostGenerate(my_nick, my_id, this_key, post_title, post_content, '', my_picture, true);
	}
}


function ajaxJoinSend(joinId, joinPw, joinEmailAccount, joinEmailServer)
{
	if (request == null)
		createRequest();
		
	var url = "./join/lib/join_account.php";
	var postString = "";
	
	postString  = "id=" + encodeURIComponent(joinId);
	postString += "&pw=" + encodeURIComponent(joinPw);
	postString += "&email_account=" + encodeURIComponent(joinEmailAccount);
	postString += "&email_server=" + encodeURIComponent(joinEmailServer);
	postString += "&parity=" + encodeURIComponent(parity_code);
	postString += "&ajax=" + true;


	request.open("POST",url,true);   
	request.onreadystatechange = ajaxBasicRequest;
	request.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
	request.send(postString);
}

function ajaxLoginSend()
{
	if (request == null)
		createRequest();
		
	var url = "./login/creaty_login.php";
	var postString = "";
	
	postString  = "id=" + encodeURIComponent(document.getElementById('id').value);
	postString += "&pw=" + encodeURIComponent(document.getElementById('pw').value);
	postString += "&parity=" + encodeURIComponent(parity_code);
	postString += "&ajax=" + true;

	// GPG 연결시
	//postString += "&bbs_gpglink=" + encodeURIComponent(document.getElementById("Post_Editor").value);

	// 수정시 
	//postString += "&modify_key=" + encodeURIComponent(document.getElementById("userName").value);

	request.open("POST",url,true);   
	request.onreadystatechange = ajaxBasicRequest;
	request.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
	request.send(postString);
}

function ajaxLogoutSend()
{
	if (request == null)
		createRequest();
		
	var url = "./login/creaty_logout.php";
	var postString = "";

		
	postString += "&ajax=" + true;


	// GPG 연결시
	//postString += "&bbs_gpglink=" + encodeURIComponent(document.getElementById("Post_Editor").value);

	// 수정시 
	//postString += "&modify_key=" + encodeURIComponent(document.getElementById("userName").value);

	request.open("POST",url,true);   
	request.onreadystatechange = ajaxBasicRequest;
	request.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
	request.send(postString);
}

function ajaxPostSend()
{
	if (request == null)
		createRequest();
		
	var url = "./social/write.php";
	var postString = "";
	
	var title;
	var content;

	title = content = document.getElementById("Post_Editor").value;

	if (content.indexOf('\n', 0) != -1)
	{
		title = document.getElementById("Post_Editor").value.split('\n')[0];
		content = content.substr(title.length, content.length).replace('\n','');
	}

	post_title = title;
	post_content = content;

	postString  = "id=" + encodeURIComponent(user_id);
	postString += "&writer=" + encodeURIComponent(my_key);
	postString += "&parity=" + encodeURIComponent(parity_code);
	postString += "&bbs_key=" + encodeURIComponent(document.getElementById("PostCart").value);
	postString += "&bbs_title=" + encodeURIComponent(title);
	postString += "&bbs_content=" + encodeURIComponent(content);
	postString += "&bbs_trackback=" + encodeURIComponent(document.getElementById("PostTrackback").value);


	document.getElementById("Post_Editor").value = '';

	// GPG 연결시
	//postString += "&bbs_gpglink=" + encodeURIComponent(document.getElementById("Post_Editor").value);

	// 수정시 
	//postString += "&modify_key=" + encodeURIComponent(document.getElementById("userName").value);

	request.open("POST",url,true);   
	request.onreadystatechange = ajaxPostRequest;
	request.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
	request.send(postString);
}

var Post_Key = 0;
function ajaxReplySend()
{
	if (request == null)
		createRequest();
		
	var url = "./social/reply.php";
	var postString = "";
	
	var title;
	var content;

	title = content = document.getElementById("Reply_Editor").value;

	if (content.indexOf('\n', 0) != -1)
	{
		title = document.getElementById("Reply_Editor").value.split('\n')[0];
		content = content.substr(title.length, content.length).replace('\n','');
	}
	post_title = title;
	post_content = content;

	postString  = "id=" + encodeURIComponent(user_id);
	postString += "&writer=" + encodeURIComponent(my_key);
	postString += "&parity=" + encodeURIComponent(parity_code);
	postString += "&post_key=" + encodeURIComponent(Post_Key);
	postString += "&reply_title=" + encodeURIComponent(title);
	postString += "&reply_content=" + encodeURIComponent(content);
	postString += "&bbs_trackback=" + encodeURIComponent('');


	document.getElementById("Reply_Editor").value = '';

	// GPG 연결시
	//postString += "&bbs_gpglink=" + encodeURIComponent(document.getElementById("Post_Editor").value);

	// 수정시 
	//postString += "&modify_key=" + encodeURIComponent(document.getElementById("userName").value);

	request.open("POST",url,true);   
	request.onreadystatechange = ajaxReplyRequest;
	request.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
	request.send(postString);
}


function Submit_Post(EditorID, LengthID, ajaxSend)
{
	var obj = document.getElementById(EditorID);
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : "";

	document.getElementById(LengthID).innerHTML = '('+obj.value.length+'/200)';

	if (obj.getAttribute && obj.value.length > mlength)
	{
		alert('length of your post more than Limit of post!');
	}
	else
	{
//		document.getElementById('PostCartegory').value = document.getElementById('PostCart').value;
//		document.getElementById('PostEditComment').value = document.getElementById('Post_Editor').value;

		if (ajaxSend == ajaxPostSend)
		{
			if (document.getElementById('PostCart').value == '')
			{
				alert('Please select cartegory of your post!');
				return;
			}

			if (my_key != user_key)
			{
				alert('You have not permission!');
				return;
			}
		}
		else if (ajaxSend == ajaxReplySend)
		{
		}

		if (document.getElementById(EditorID).value == '')
		{
			alert('Please write your post!');
			return;
		}

		if (confirm('Do you want Cast?'))
		{
			ajaxSend();
		}
	}
}


var ToLeftOffset = 0;
var isScrolling = false;
function Post_HistoryProcess(ObjID)
{
	var Left = parseInt(document.getElementById(ObjID).style.left);

	if ( Math.abs(ToLeftOffset - Left) < 10)
	{
		document.getElementById(ObjID).style.left = ToLeftOffset + 'px';
		isScrolling = false;

		if (ToLeftOffset != 0)
			document.getElementById('PostMain').style.display = 'none';
		else
			document.getElementById('PostHistory').style.display = 'none';

		FooterSetting();
	}
	else
	{
		document.getElementById(ObjID).style.left = Left + Math.round((ToLeftOffset - Left) * 0.08) + 'px';
		setTimeout('Post_HistoryProcess(\''+ObjID+'\');', 10);
	}
}

function Post_History(id, PostKey, ToLeft)
{
	Post_Key = PostKey;
	post_loadingText = 
		'<div class="NoPost" style="margin:100px;text-align:center;">'+
		'<div class="NoPostSubject" style="font-size:20px;font-weight:bold;color:#666666;margin-bottom:20px;">Wait a minute!</div>'+
		'<div class="NoPostDescription" style="color:#AAAAAA;">Now loading cast of this page.</div>'+
		'</div>';


	if (ToLeft != 0)
		document.getElementById('PostHistory').style.display = '';
	else
		document.getElementById('PostMain').style.display = '';

	FooterSetting();

	update_SubPostList(id, PostKey, ToLeft != 0);

	ToLeftOffset = ToLeft;
	if (isScrolling == false)
	{
		isScrolling = true;
		Post_HistoryProcess('PostWindow');
	}
}
