$(function() {

	// load the modal window
	$('a.modal').click(function(){

		// scroll to top
		$('html, body').animate({scrollTop:0}, 'fast');

		// before showing the modal window, reset the form incase of previous use.
		$('.success, .error').hide();
		$('form#contactForm').show();
		
		// Reset all the default values in the form fields
		$('#name').val('Your name');
		$('#email').val('Your email address');
		$('#company').val('Enter Your company name');
		$('#phone').val('Enter Your phone number');
		$('#comment').val('Enter your comment or query...');

		//show the mask and contact divs
		$('#mask').show().fadeTo('', 0.7);
		$('div#contact').fadeIn();

		// stop the modal link from doing its default action
		return false;
	});

	// close the modal window is close div or mask div are clicked.
	$('div#close, div#mask').click(function() {
		$('div#contact, div#mask').stop().fadeOut('slow');

	});
	
	$('#name').focus(function() {
	if($(this).attr('value')=='Your name')
		{
		
			$('#name').val('');
		}
	});
	
	$('#email').focus(function() {
	if($(this).attr('value')=='Your email address')
		{
		
			$('#email').val('');
		}
	});
	
		$('#company').focus(function() {
	if($(this).attr('value')=='Enter Your company name')
		{
		
			$('#company').val('');
		}
	});
		
		$('#phone').focus(function() {
	if($(this).attr('value')=='Enter Your phone number')
		{
		
			$('#phone').val('');
		}
	});
		
		$('#comment').focus(function() {
	if($(this).attr('value')=='Enter your comment or query...')
		{
		
			$('#comment').val('');
		}
	});
	
	
	
	
	$('#name').blur(function() {
	
		if($(this).attr('value')=='')
		{
		
			$('#name').attr('value','Your name');
		}
	});
	
	
		$('#email').blur(function() {
		
		if($(this).attr('value')=='')
		{
			$('#email').val('Your email address');
		}
	});
		
		
		$('#company').blur(function() {
		
		if($(this).attr('value')=='')
		{
			$('#company').val('Enter Your company name');
		}
	});
		
		
		$('#phone').blur(function() {
		
		if($(this).attr('value')=='')
		{
			$('#phone').val('Enter Your phone number');
		}
	});
		
	$('#comment').blur(function() {
		
		if($(this).attr('value')=='')
		{
			$('#comment').val('Enter your comment or query...');
		}
	});
	
	
	$('#contactForm textarea').focus(function() {
        $(this).val('');
    });

	// when the Submit button is clicked...
	$('input#submit').click(function() {
	$('.error').hide().remove();
		//Inputed Strings
		var username = $('#name').val();
		var	email = $('#email').val();
		var	company = $('#company').val();
		var	phone = $('#phone').val();
		var	comment = $('#comment').val();
		

		//Error Count
		var error_count=0;
		
		//Regex Strings
			var usernm=/^[a-zA-Z]+$/;
			var email_regex = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
			var phno=/^[0-9]{10}$/;

			//Test Username
			
			if(username=='' || username=='Your name' || !usernm.test(username)) {
				$('#contact_header').after('<p class=error>Invalid username entered!</p>');
				error_count += 1;
			}
			
			//Test Email
			if(email=='' || email=='Your email address' || !email_regex.test(email)) {
				$('#contact_header').after('<p class=error>Invalid email entered!</p>');
				error_count += 1;
			}
			
				if(phone=='' || phone=='Enter Your phone number' || !phno.test(phone)) {
				$('#contact_header').after('<p class=error>Invalid Mobile no!</p>');
				error_count += 1;
			}
			
			//Blank Comment?
			if(comment=='' || comment=='Enter your comment or query...') {
				$('#contact_header').after('<p class=error>No Comment was entered!</p>');
				error_count += 1;
			}
			
			if(error_count>=1)
			{
				$('.error').fadeIn('slow');
			return false;	
			}
			else
			{
			
			var postdata="name=" + username + "&email=" + email + "&comment=" + comment +"&phone="+phone+"&company="+company;
		
		
			//No Errors?
			
				$.ajax({
					type: "POST",
					url: "send.php",
					data: postdata,
					error: function() {
						$('.error').hide();
						$('#sendError').slideDown('slow');
					},
					success: function () {
						$('.error').hide();
						$('.success').slideDown('slow');
						$('form#contactForm').fadeOut('slow');
					return true;
					}				
				});	
		
			}
		return false;
	});
	
});
