
$(document).ready
(
	function() 
	{

	}
)

function change_content(page, id)
{
	var file = page.split('.');
	var file_type = file.pop();
	var base_name = file.shift();
	var nav = base_name + '_nav';
	var queryString = $('#the_form').formSerialize(); 
	
	queryString += '&id=' + id;
	
	$('td.nav_selected').attr('class', 'nav_unselected');
	$('#' + nav).attr('class', 'nav_selected');

		$.post
		(page, queryString, function(html)
			{
				$("#content").html(html);
			}
		);
}

function change_class(new_class)
{
	var e = document.getElementById('classifieds_nav');
	
	e.setAttribute("class", new_class);
	e.setAttribute("className", new_class); // for IE which does not recognize "class"

	//$(this).removeClass($(this).attr('class'));
	//$(this).addClass(new_class); 
}

// login

function check_login(login_to) 
{ 
	$("#login_to").val(login_to);

	var submit_form = true;
	
	$('.label').css("color", "black");
	$('.error').innnerHTML = '';
	
	if ($("#username").val() == "")
	{	  
		$("#username_label").css("color", "red");  
		$("#username").focus();  
		submit_form = false;  
	}
	
	if ($("#password").val() == "")
	{	  
		$("#password_label").css("color", "red");  
		$("#password").focus();  
		submit_form = false;  
	}
	
	if (submit_form)
	{
		document.the_form.submit();
	}
	else
	{
		alert('Please correct the areas in red');
	}
}


function register_form() 
{ 

	var submit_form = true;
	
	$('.label').css("color", "black");
	$('.error').innnerHTML = '';
	
	if ($("#firstname").val() == "")
	{	  
		$("#firstname_label").css("color", "red");  
		$("#firstname").focus();  
		submit_form = false;  
	}

	if ($("#lastname").val() == "")
	{	  
		$("#lastname_label").css("color", "red");  
		$("#lastname").focus();  
		submit_form = false;  
	}

	if ($("#username").val() == "")
	{	  
		$("#username_label").css("color", "red");  
		$("#username").focus();  
		submit_form = false;  
	}
	
	if ($("#password").val() == "")
	{	  
		$("#password_label").css("color", "red");  
		$("#password").focus();  
		submit_form = false;  
	}

	if ($("#password2").val() == "")
	{	  
		$("#password2_label").css("color", "red");  
		$("#password2").focus();  
		submit_form = false;  
	}

	if ($("#email").val() == "")
	{	  
		$("#email_label").css("color", "red");  
		$("#email").focus();  
		submit_form = false;  
	}

	if ( $("#password").val() != $("#password2").val() )
	{
		alert ('Passwords do not match');
		$("#password_label").css("color", "red");  
		$("#password").focus();  
		$("#password2_label").css("color", "red");  
		return;  
	}
	
	if (! isValidEmail( $("#email").val() ) )
	{
		alert ('Invalid email address');
		$("#email_label").css("color", "red");  
		$("#email").focus();  
		return; 
	}
	
	if (submit_form)
	{
		document.the_form.submit();
	}
	else
	{
		alert('Please correct the areas in red');
	}
}

	function classified_form() 
	{  
		var submit_form = true;
		
		$('.label').css("color", "black");  

		if ($("#title").val() == "")
		{	  
			$("#title_label").css("color", "red");  
			$("#title").focus();  
			submit_form = false;  
		}

		if ($("#listing").val() == "")
		{	  
			$("#listing_label").css("color", "red");  
			$("#listing").focus();  
			submit_form = false;  
		}

		if (submit_form)
		{
			document.the_form.submit();
		}
		else
		{
			alert('Please correct the areas in red');
		}
	};  

	function edit_classified_form() 
	{  
		var submit_form = true;
		
		$('.label').css("color", "black");  

		if ($("#listing").val() == "")
		{	  
			$("#listing_label").css("color", "red");  
			$("#listing").focus();  
			submit_form = false;  
		}

		if (submit_form)
		{
			change_content('update_classified.php', '');
		}
		else
		{
			alert('Please correct the areas in red');
		}
	}; 	

// list users
	
	function edit_user(id)
	{
		document.the_form.action = 'edit_user.php';
		document.the_form.user_id.value = id;
		document.the_form.submit();
	}
	function delete_user(user_id)
	{
		if (confirm('Are you sure you want to delete this user?'))
		{
			$.post
			("delete_user.php", {user_id: user_id}, function(html)
				{
					//alert(html);
				}
			);
			change_content('manage_users.php', '');
		}
	}	

	function delete_classified(classified_id)
	{
		if (confirm('Are you sure you want to delete this classified?'))
		{
			$.post
			("delete_classified.php", {classified_id: classified_id}, function(html)
				{
					//alert(html);
				}
			);
			change_content('manage_classifieds.php', '');
		}
	}	
	


	//function to check valid email address
	function isValidEmail(strEmail)
	{
		validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
		if (! strEmail)
			strEmail = document.forms[0].email.value;
	
		 // search email text for regular exp matches
			if (strEmail.search(validRegExp) == -1) 
		 {
				return false;
			} 
			return true; 
	}


	
	
	// add user
	
   function add_user_form() 
	 {  
		var submit_form = true;
		
		$('.label').css("color", "black");  

		if ($("#username").val() == "")
		{	  
			$("#username_label").css("color", "red");  
			$("#username").focus();  
			submit_form = false;  
		}

		if ($("#password").val() == "")
		{	  
			$("#password_label").css("color", "red");  
			$("#password").focus();  
			submit_form = false;  
		}

		if ($("#password2").val() == "")
		{	  
			$("#password2_label").css("color", "red");  
			$("#password2").focus();  
			submit_form = false;  
		}

		if ( $("#password").val() != $("#password2").val() )
		{
			alert ('Passwords do not match');
			$("#password_label").css("color", "red");  
			$("#password").focus();  
			$("#password2_label").css("color", "red");  
			return;  
		}

		if (! isValidEmail( $("#email").val() ) )
		{
			alert ('Invalid email address');
			$("#email_label").css("color", "red");  
			$("#email").focus();  
			return; 
		}

		if ( $("input[name='approved']:checked").val() === undefined)
		{
			$("#approved_yes_label").css("color", "red");
			$("#approved_no_label").css("color", "red");
			$("#approved_label").css("color", "red");
			$("#approved_yes").focus();
			submit_form = false;
		}

		if ( $("input[name='admin']:checked").val() === undefined)
		{
			$("#admin_yes_label").css("color", "red");
			$("#admin_no_label").css("color", "red");
			$("#admin_label").css("color", "red");
			$("#admin_yes").focus();
			submit_form = false;
		}

		if (submit_form)
		{
			document.the_form.submit();
		}
		else
		{
			alert('Please correct the areas in red');
		}
	};  

   function edit_user_form() 
	 {  
		var submit_form = true;
		
		$('.label').css("color", "black");  

		if ($("#username").val() == "")
		{	  
			$("#username_label").css("color", "red");  
			$("#username").focus();  
			submit_form = false;  
		}

		if ( $("#password").val() != $("#password2").val() )
		{
			alert ('Passwords do not match');
			$("#password_label").css("color", "red");  
			$("#password").focus();  
			$("#password2_label").css("color", "red");  
			return;  
		}

		if (! isValidEmail( $("#email").val() ) )
		{
			alert ('Invalid email address');
			$("#email_label").css("color", "red");  
			$("#email").focus();  
			return; 
		}

		if ( $("input[name='approved']:checked").val() === undefined)
		{
			$("#approved_yes_label").css("color", "red");
			$("#approved_no_label").css("color", "red");
			$("#approved_label").css("color", "red");
			$("#approved_yes").focus();
			submit_form = false;
		}

		if ( $("input[name='admin']:checked").val() === undefined)
		{
			$("#admin_yes_label").css("color", "red");
			$("#admin_no_label").css("color", "red");
			$("#admin_label").css("color", "red");
			$("#admin_yes").focus();
			submit_form = false;
		}

		if (submit_form)
		{
			document.the_form.submit();
		}
		else
		{
			alert('Please correct the areas in red');
		}
	};  

	
		
	// edit user
	
	function user_admin_form()
	{
		var submit_form = true;
		
		$('.label').css("color", "black");  

		if ($("#firstname").val() == "")
		{	  
			$("#firstname_label").css("color", "red");  
			$("#firstname").focus();  
			submit_form = false;  
		}

		if ($("#lastname").val() == "")
		{	  
			$("#lastname_label").css("color", "red");  
			$("#lastname").focus();  
			submit_form = false;  
		}

		if ( $("#password").val() != $("#password2").val() )
		{
			alert ('Passwords do not match');
			$("#password_label").css("color", "red");  
			$("#password").focus();  
			$("#password2_label").css("color", "red");  
			return;  
		}

		if (! isValidEmail( $("#email").val() ) )
		{
			alert ('Invalid email address');
			$("#email_label").css("color", "red");  
			$("#email").focus();  
			return; 
		}

		if (submit_form)
		{
			document.the_form.submit();
		}
		else
		{
			alert('Please correct the areas in red');
		}
	}
	



	function delete_attach(attachment_id)
	{
		if (confirm('Are you sure you want to delete this attachment?'))
		{
			var user_id = $("#user_id").val();
	
			//alert('user id=' + user_id + '\nslection id=' + selection_id);

			$.post
			("delete_attachment.php", {user_id: user_id, attachment_id: attachment_id}, function(html)
				{
					//alert(html);
				}
			);

			change_content('edit_user', user_id);
		}
	}

	
	function clear_input(field)
	{
		var element = document.getElementById(field);
		element.innerHTML = '';
	}
	

