function LoadComments(page)
{
	$('#area_comments').html('<img src="/images/loading.gif" />');
	$('#area_comments').load("/comments.php?fid="+work_id+"&type="+work_type+"&page="+page);
}
function LoadMoreComment(id)
{
	$('#comments_'+id).append('<img src="/images/loading.gif" />');
	$('#comments_'+id).load("/comments.php?act=loadmore&id="+id);
}
function OpenPostDialog(apply_to)
{
	if(apply_to>0)
		var titleString="Post a reply";
	else
		var titleString="Post a comment";
	Boxy.load("/comments.php?act=show_post&replay_to="+apply_to, 
			  {cache:false,modal: true,borderWidth:0,title:titleString,unloadOnHide:true,
			  	afterShow:	function()
							{
								 var options = {
									url:       		"/comments.php?act=post&fid="+work_id+"&type="+work_type, // override for form's 'action' attribute 
									type:      		'post', // 'get' or 'post', override for form's 'method' attribute 
									dataType:  		'json', // 'xml', 'script', or 'json' (expected server response type) 
									timeout:   		30000,
									beforeSubmit:	ValidatePost,
									success:       	AfterPost
								}; 
								$('#formPostComment').submit(function() {
									$(this).ajaxSubmit(options); 
									return false; 
								}); 
								onShareToFBChange($$('send_to_facebook'));
							}
	});
}
function ValidatePost(formData, jqForm, options)
{
	var form = jqForm[0];
	if(form.not_login.value=="none")
	{
		if (!form.email_address.value || form.email_address.value==form.email_address.title)
		{
			$('#error_msg').html('Please enter your email address.'); 
			return false; 		
		}
		if($('#panel_hasnot_login input:radio:checked').val()=='1')
		{
			if (!form.password.value) { 
				$('#error_msg').html('Please enter your password.'); 
				return false; 
			} 
		}
		else
		{
			if (!form.full_name.value || form.email_address.value==form.full_name.title) { 
				$('#error_msg').html('Please enter your full name.'); 
				return false; 
			} 	
			if (!form.screen_name.value || form.email_address.value==form.screen_name.title) { 
				$('#error_msg').html('Please enter your screen name.'); 
				return false; 
			} 		
		}
	}
	if (!form.contents.value|| form.contents.value==form.contents.title)
	{
        $('#error_msg').html('Please enter your comments / feedback.'); 
        return false;
	}
	$('#post_comment').attr('disabled',true);
    return true; 
}
function AfterPost(result)
{
	$('#post_comment').attr('disabled',false);
	switch(result.status)
	{
		case -1: //login expired
			$('#panel_already_login').hide();
			$('#panel_hasnot_login').show();
			$("#has_account_1").attr("checked","checked");
			$('#error_msg').html(result.msg.replace(/\n/g,'<br />'));
			SwitchHasAccountOrNot();
			break;
		case -2: //has bad words
			alert(result.msg);
			//$('#error_msg').html(result.msg.replace(/\n/g,'<br />'));
			Boxy.get('.close').hide();
			break;
		case 1: // post successfully
			if($$('send_to_facebook').checked)
			{
				LoadComments(1);
				PostToFBWall(result.commentId);
			}
			else
			{
				alert(result.msg);
				LoadPage(1);
			}
			Boxy.get('.close').hide();
			break;
		default: // error..
			$('#error_msg').html(result.msg.replace(/\n/g,'<br />'));
			break;
	}
}
function SwitchHasAccountOrNot()
{
	if( $("#panel_hasnot_login input:radio:checked").val()=='1')
	{
		$(".show_no_account").hide();
		$(".show_has_account").show();
	}
	else
	{
		$(".show_no_account").show();
		$(".show_has_account").hide();		
	}
}

//get the queryString value of the thisURL 
function Request(QueryString,strHref)
{
	if(strHref==null)
		strHref=window.location.href;
	var strParm="";
	if(strHref.search(/\?/)!=-1)
	{
		strHref=strHref.substr(strHref.search(/\?/)+1);
		strHref=strHref.split(/&/);
		for(var icount = 0; icount<strHref.length; icount++)
		{
			if(strHref[icount].search("^"+QueryString+"=")!=-1)
			{
				strParm=strHref[icount].substr(QueryString.length+1)
			}
		}
		return(strParm);
	}
	else
		return 0;
} 

//set the QueryString's new value of the URL 
// this function need function Request(QueryString)
function SetQuery(keys,values,url)
{
	if(url==null)
		url =location.href;
	if(keys.constructor ==Array)
	{
		for(var i=0;i<keys.length;i++)
		{
			if(!Request(keys[i],url))
			{
				if(url.indexOf("?")<1)
					url+="?"+keys[i]+"="+values[i];
				else   
				{
					url = url.replace('&'+keys[i]+"=",'');
					url+="&"+keys[i]+"="+values[i];
				}
			}
			else
				url=url.replace(keys[i]+"="+Request(keys[i],url),keys[i]+"="+values[i]);
		}
	}
	else
	{		
		if(!Request(keys,url))
		{
			if(url.indexOf("?")<1)
				url+="?"+keys+"="+values;
			else   
			{
				url = url.replace('&'+keys+"=",'');
				url+="&"+keys+"="+values;
			}
		}
		else
			url=url.replace(keys+"="+Request(keys,url),keys+"="+values);
	}
	return url;
}
function onShareToFBChange(o)
{
	if(!o) return;
	if(!o.checked)	return true;
	if(!FB.Connect)
		fb_init();
}
function PostToFBWall(cid)
{
	if(!FB.Connect)
		fb_init();
	var c_url = location.href.replace(/#.*/g,'')+"#comment"+cid;
	var data = {
		"images":[{"src":"http://www.teenink.com/images/logo_for_fb_large.gif", "href":"http://www.teenink.com","style":"float:left;margin-top:5px;padding-top:5px;"}],
		"type":(work_type==1)?"article":"image",
		"url":location.href,
		"title":$('#word_title').html(),
		"comment":$('#comment_content').val(),
		"comment_url":c_url
	}
	FB.Connect.showFeedDialog(110825854882,data,null,null,null,FB.RequireConnect.promptConnect,PostToFBWallSucceed,'Share something about your post.',null);	
}
function PostToFBWallSucceed(r)
{
	if(r)
	{
		alert("Thanks! Your comment has been submitted successfully. Please note that some comments are subject to prior approval by our editors, and we thank you for your patience.");
	}
}