storage.load('ce_comments');
var MAX_LENGTH = 1000;

function submitComment()
{
	var body = document.getElementById('comment_body');
	if((String(body.value.length) > 0)&&(body.value.length <= MAX_LENGTH))
		document.getElementById('CommentForm').submit();
}

function updateCommentLength()
{
	var body = document.getElementById('comment_body');
	var counter = document.getElementById('comment_length');
	
	if(body.value.length > MAX_LENGTH)
		document.getElementById('comment_overchar').style.display = '';
	else
		document.getElementById('comment_overchar').style.display = 'none';
	
	if(body.value.length >= MAX_LENGTH)		// When the max length is reached change the counter to red (#C80000)
	{
		counter.style.color = '#C80000';
	}
	else
	{
		counter.style.color = '#000000';
	}
	
	counter.innerHTML = body.value.length;
}

function commentRating(direction,comment_id)
{
	if(!document.cookie)	// no cookies, no milk.
		return;
	
	var storedRating = storage.get(comment_id);
	
	if(storedRating != null)
	{
		if(Number(direction) == Number(storedRating))
			return ;
		else
		{
			updateRating(direction == -1 ? 1 : -1,comment_id,false);
			updateRating(direction,comment_id,true);
			
			storage.add(comment_id,direction);
			storage.commit('ce_comments');
		}
			
	}
	else
	{
		storage.add(comment_id,direction);
		storage.commit('ce_comments');
		updateRating(direction,comment_id);
	}
}
function updateRating(direction,comment_id, increment)
{
	if((increment == null)||(increment == true))
		increment = 1;
	else
		increment = 0;
	
	var conn = new ajax_connect(ROOT_PATH+'scripts/script_ajax_comment.php','POST');
	conn.setVars('func=update_thumbs');
	conn.setVars('direction='+direction);
	conn.setVars('comment_id='+comment_id);
	conn.setVars('increment='+increment);
	
	conn.onComplete(
			function(return_data)
			{
				if(return_data['type'] ==  'xml')
				{
					var thumbs_div = document.getElementById('thumbs_'+comment_id);
					
					thumbs_div.innerHTML = '<b>'+_xmlfetch(return_data['data'],'thumbs_up',0)+'</b> Up, ';
					thumbs_div.innerHTML += '<b>'+_xmlfetch(return_data['data'],'thumbs_down',0)+'</b> Down';
				}
			}
		);
	
	conn.sendData();
}

function commentFlag(comment_id)
{
	var conn = new ajax_connect(ROOT_PATH+'scripts/script_ajax_comment.php','POST');
	conn.setVars('func=flag');
	conn.setVars('comment_id='+comment_id);
	
	var flag_anchor = document.getElementById('flagged_'+comment_id);
	flag_anchor.onclick = null;
	flag_anchor.style.color = '#000000';
	flag_anchor.innerHTML = 'Flagged';
	
	conn.onComplete(
			function(return_data)
			{
				
			}
		);
	
	conn.sendData();
}

function updateIdentity()
{
	var conn = new ajax_connect(ROOT_PATH+'scripts/script_ajax_comment.php','POST');
	conn.setVars('func=identity');
	conn.setVars('name_first,name_last,address_city,user_id');
	
	var bError = false;
	
	if(conn.getVar('name_first').trim() == '')
		bError = validateError('name_first',true);
	else
		validateError('name_first',false);
	
	if(conn.getVar('name_last').trim() == '')
		bError = validateError('name_last',true);
	else
		validateError('name_last',false);
	
	if(conn.getVar('address_city').trim() == '')
		bError = validateError('address_city',true);
	else
		validateError('address_city',false);
	
	if(bError)
		return;
	
	conn.onComplete(
			function(return_data)
			{
				var lastName = conn.getVar('name_last');
				var firstName = conn.getVar('name_first');
				var city = conn.getVar('address_city');
				
				var identity1 = firstName+' '+lastName+', '+city;
				var identity2 = firstName +' '+ lastName.substr(0,1)+'., '+city;
				var identity3 = firstName.substr(0,1)+'. '+lastName+', '+city;
				
				document.getElementById('comment_identity_1_div').innerHTML = identity1;
				document.getElementById('comment_identity_2_div').innerHTML = identity2;
				document.getElementById('comment_identity_3_div').innerHTML = identity3;
				
				document.getElementById('comment_identity_1_radio').value = identity1;
				document.getElementById('comment_identity_2_radio').value = identity2;
				document.getElementById('comment_identity_3_radio').value = identity3;
				
				document.getElementById('comment_identity_update').style.display = 'none';
				document.getElementById('comment_identities').style.display = '';
				
			}
		);
	
	conn.sendData();
}

function validateError(element_id,bError)
{
	var obj = document.getElementById(element_id);
	
	if(bError)
	{
		obj.style.borderColor = '#C80000';
		obj.style.borderWidth = '2px';
		obj.style.borderStyle = 'solid';
	}
	else
	{
		obj.style.borderColor = '#bbbbbb';
		obj.style.borderWidth = '1px';
		obj.style.borderStyle = 'solid';
	}
	
	return bError;
}
