$(document).ready(function(){		
	// called when the user logs in from the vlog page		
	//displays the add comment box if login was successful or an error message if login failed
	$('#lForm').submit(function(){
		var theUser = $('input[name="username"]').val();
		var thePass = $('input[name="password"]').val();	
		var theVidID = $('input[name="vidId"]').val();
		var ses = Get_Cookie('PHPSESSID');
				
		$.post('../testing.php',
			{username: theUser, password: thePass, sess: ses},
			function(result)
			{
				result = result.split('+');

				if(result[0] == 'success')
				{
					$('#logFormPost').slideUp('slow', function(){
					
						var theHTML = '<div id="commFormPost"><h3>Add a Comment</h3><form method="post" id="commAddForm"><input type="hidden" value="'+ result[3] +'" id="pid" name="pid" /><input type="hidden" value="' + result[2] + '" id="uid" name="uid" /><input type="hidden" value="'+ theVidID +'" id="vId" name="vId" /><input type="text" name="commTitle" id="commTitle" value="Comment Title" onblur="if(this.value==\'\') this.value=\'Comment Title\';" onfocus="if(this.value==\'Comment Title\') this.value=\'\';"/><textarea name="commBody" id="commBody" rows="10" cols="25"></textarea><Br/><input type="submit" value="Submit" class="commSub" /></form></div>';
						
						$('#hadd').html(theHTML, function(){
							$(this).slideDown('slow');
						});
						
						$('#commAddForm').submit(function()
						{
							addComment();
										
							return false;
						});
						
						$('#commTitle').keyup(function(event){
						    limitText(this,30);
						});
						
						$('#commTitle').keydown(function(event){
						    limitText(this,30);
						});
						
						$('#commBody').keyup(function(event){
						    limitText(this,250);
						});
						
						$('#commBody').keydown(function(event){
						    limitText(this,250);
						});
					});
										
				}
				else
				{
					var curForm = $('#logFormPost').html();
					var error = '<p class="error">Login failed. Please try again.</p>';
					
					$('#logFormPost h3').after(error);
				}
			}
		);
		
		/*$.ajax({
			url: "testing.php",
			global: false,
			type: "POST",
			dataType: "html",
			error:
				function(request, status, err)
				{
					alert(request + " " + status + " " + err);
				}	
			,
			success: 
				function(msg, status)
				{
					alert(msg + " " + status);
				}
		});	*/
		
		return false;
	});
});

function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}
