$(document).ready(function(){

	$("#contactForm").submit(function(){

		var countTextbox = 0;
		var countValidatedTextboxes = 0;
		var emailRegEx =/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		var textbox = document.getElementsByTagName("input");

		for (var i=0; i < textbox.length; i++)
		{
			if (textbox[i].className.match("required_field"))
			{		
				if(textbox[i].getAttribute('id') == 'name') 
				{
					if((textbox[i].value == '')) 
					{

						textbox[i].style.backgroundColor = '#D9ECFF';

					}
					else 
					{
						textbox[i].style.backgroundColor = '#fff';
						countValidatedTextboxes++;
					}
				}
				if(textbox[i].getAttribute('id') == 'email') 
				{
					if((textbox[i].value == '')  || (emailRegEx.test(textbox[i].value) == false))
					{

						textbox[i].style.backgroundColor = '#D9ECFF';
					}
					else 
					{
						textbox[i].style.backgroundColor = '#fff';
						countValidatedTextboxes++;
					}
				}
				if(textbox[i].getAttribute('id') == 'subject') 
				{
					if((textbox[i].value == '')) 
					{

						textbox[i].style.backgroundColor = '#D9ECFF';
					}
					else 
					{
						textbox[i].style.backgroundColor = '#fff';
						countValidatedTextboxes++;
					}
				}					

			}
			countTextbox++;
		}

		if(countValidatedTextboxes == 3)
		{
			return true;			
		}
		else
		{
			$(".error_message").html("Please fill in all the required fields");
			$(".error_message").show();
			return false;
		}




	});
	$("#journalist_registration").submit(function(){

		var countTextbox = 0;
		var countValidatedTextboxes = 0;

		var countSelectbox = 0;
		var countValidatedSelectboxes = 0;		

		var countCheckbox = 0;


		var emailRegEx =/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		var textbox = $("input");
		var selectbox = $("select");
		var checkbox = $("input[type=checkbox]");

		for (var i=0; i < textbox.length; i++)
		{
			if (textbox[i].className.match("required_field"))
			{		

				if(textbox[i].getAttribute('id') == 'email') 
				{
					if((textbox[i].value == '')  || (emailRegEx.test(textbox[i].value) == false))
					{

						textbox[i].style.backgroundColor = '#D9ECFF';
					}
					else 
					{
						textbox[i].style.backgroundColor = '#fff';
						countValidatedTextboxes++;
					}
				}
				else
				{
					if(textbox[i].value == '')
					{

						textbox[i].style.backgroundColor = '#D9ECFF';
					}
					else 
					{
						textbox[i].style.backgroundColor = '#fff';
						countValidatedTextboxes++;
					}
				}


			}
			countTextbox++;
		}
		for (var i=0; i < selectbox.length; i++)
		{
			if (selectbox[i].className.match("required_field"))
			{		

				if((selectbox[i].value == ''))
				{

					selectbox[i].style.backgroundColor = '#D9ECFF';
				}
				else 
				{
					selectbox[i].style.backgroundColor = '#fff';
					countValidatedSelectboxes++;
				}


			}
			countSelectbox++;
		}
		for (var i=0; i < checkbox.length; i++)
		{

			if(checkbox[i].checked == true)
			{
				if(checkbox[i].getAttribute('id') != 'newsletter_checkbox')
				{
								countCheckbox++;
				}
			}

		}						

		if(($("#password").val() == $("#confirm_password").val()) && (countValidatedTextboxes == 9) && (countValidatedSelectboxes == 3) && (countCheckbox > 0))
		{
			return true;			
		}
		else
		{

			if((countValidatedTextboxes != 9) || (countValidatedSelectboxes != 3))
			{
				$(".error_message").html("Please fill in all the required fields");
				$(".error_message").show();
			}
			else
			{
				$(".error_message").hide();
			}	

			if($("#password").val() != $("#confirm_password").val())
			{
				$("#password").css('background-color' , '#D9ECFF');
				$("#confirm_password").css('background-color' , '#D9ECFF');
				$(".error_message#password_error").html("Please make sure that the passwords you have entered match");
				$(".error_message#password_error").show();
			}
			else
			{
				$(".error_message#password_error").hide();
			}

			if(countCheckbox == 0)
			{
				$(".error_message#category_error").html("Please select at least one category");
				$(".error_message#category_error").show();
			}
			else
			{
				$(".error_message#category_error").hide();
			}			

			return false;
		}

	});	   
	$("#email_password").submit(function(){

		var countTextbox = 0;
		var countValidatedTextboxes = 0;
		var emailRegEx =/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		var textbox = document.getElementsByTagName("input");

		for (var i=0; i < textbox.length; i++)
		{
			if (textbox[i].className.match("required_field"))
			{		
				if(textbox[i].getAttribute('id') == 'name') 
				{
					if((textbox[i].value == '')) 
					{

						textbox[i].style.backgroundColor = '#D9ECFF';
					}
					else 
					{
						textbox[i].style.backgroundColor = '#fff';
						countValidatedTextboxes++;
					}
				}
				if(textbox[i].getAttribute('id') == 'email') 
				{
					if((textbox[i].value == '')  || (emailRegEx.test(textbox[i].value) == false))
					{

						textbox[i].style.backgroundColor = '#D9ECFF';
					}
					else 
					{
						textbox[i].style.backgroundColor = '#fff';
						countValidatedTextboxes++;
					}
				}					

			}
			countTextbox++;
		}

		if(countValidatedTextboxes == 2)
		{
			return true;			
		}
		else
		{
			$(".error_message").html("Please fill in all the required fields");
			$(".error_message").show();
			return false;
		}
	});	
	$("#login_form").submit(function(){

		var countTextbox = 0;
		var countValidatedTextboxes = 0;
		var textbox = document.getElementsByTagName("input");

		for (var i=0; i < textbox.length; i++)
		{
			if (textbox[i].className.match("required_field"))
			{		
				if(textbox[i].getAttribute('id') == 'username') 
				{
					if((textbox[i].value == '')) 
					{

						textbox[i].style.backgroundColor = '#D9ECFF';
					}
					else 
					{
						textbox[i].style.backgroundColor = '#fff';
						countValidatedTextboxes++;
					}
				}
				if(textbox[i].getAttribute('id') == 'password') 
				{
					if((textbox[i].value == '')) 
					{

						textbox[i].style.backgroundColor = '#E2F0FF';
					}
					else 
					{
						textbox[i].style.backgroundColor = '#fff';
						countValidatedTextboxes++;
					}
				}					

			}
			countTextbox++;
		}

		if(countValidatedTextboxes == 2)
		{
			return true;			
		}
		else
		{
			$(".error_message").html("Please fill in all the required fields");
			$(".error_message").show();
			return false;
		}
	});	 
	$(".required_field").focus(function() {

		$(this).css('backgroundColor', '#fff');

	});		

});
