$(function() {

	// load the modal window
	var default_values = new Array();
	$('a.modal').click(function(){
		default_values = new Array();
		// scroll to top
		$('html, body').animate({scrollTop:0}, 'fast');

		// before showing the modal window, reset the form incase of previous use.
		$('.success, .error, .sendError').hide();
		$('form#contactForm').show();
		
		// Reset all the default values in the form fields
		$('#name').val('Your name');
		$('#address').val('Your Address');
		$('#time_to_contact').val('Suitable time to contact');
		$('#telephone').val('Your Contact Number');
		$('#email').val('Your email address');
		$('#comment').val('Enter your request or query...');
		$('#captavalue').val('');
		$('#cvalue1').html(Math.ceil(Math.random()*5));
		$('#cvalue2').html(Math.ceil(Math.random()*5));
		$('#tvalue1').val($('#cvalue1').text());
		$('#tvalue2').val($('#cvalue2').text());
		//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');

	});

	
	$('#contactForm input').focus(function() {
		if (this.id =='submit') return;
		    if (!default_values[this.id]) {
		      default_values[this.id] = this.value;
		    }
		    if (this.value == default_values[this.id]) {
		      this.value = '';
    		    }
	});
	
	$('#contactForm input').focusout(function() {
			      if (this.value == '') {
			        this.value = default_values[this.id];
      				}
	});
	
	$('#contactForm textarea').focus(function() {
		    if (!default_values[this.id]) {
		      default_values[this.id] = this.value;
		    }
		    if (this.value == default_values[this.id]) {
		      this.value = '';
    		    }
    	});
    	
    	$('#contactForm textarea').focusout(function() {
    			      if (this.value == '') {
				        this.value = default_values[this.id];
      				}
    	});

	// when the Submit button is clicked...
	$('input#submit').click(function() {
	$('.error').hide().remove();
		//Inputed Strings
			var username = $('#name').val(),
			email = $('#email').val(),
			comment = $('#comment').val();
			address = $('#address').val();
			time_to_contact = $('#time_to_contact').val();
			telephone = $('#telephone').val();
			
		//Error Count
		var error_count = 0;
		
		//Regex Strings
			email_regex = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
		
			//Test Username
			if(username == '' || username == 'Your name') {
				$('#name').after('<span class=error>Invalid name entered!</span>');
				error_count += 1;
			}
			
			//Test Email
			if(!email_regex.test(email)) {
				$('#email').after('<span class=error>Invalid email entered!</span');
				error_count += 1;
			}
			
			//Blank Comment?
			if(comment == '' || comment == 'Enter your request or query...') {
				$('#comment').after('<span class=error>No request was entered!</span>');
				error_count += 1;
			}
			//Blank Comment?
			if(address == '' || address == 'Your Address') {
				$('#address').after('<span class=error>No Address was entered!</span>');
				error_count += 1;
			}
			//Blank Comment?
			if(time_to_contact == '' || time_to_contact == 'Suitable time to contact') {
				$('#time_to_contact').after('<span class=error>No Time to contact was entered!</span>');
				error_count += 1;
			}
			//Blank Comment?
			if(telephone == '' || telephone == 'Your Contact Number') {
				$('#telephone').after('<span class=error>No Contact Number was entered!</span>');
				error_count += 1;
			}
			
			if ((parseInt($('#tvalue1').val()) + parseInt($('#tvalue2').val()))!= $('#captavalue').val()){
				$('#captavalue').after('<span class=error>Calculate Correct value!</span>');
				error_count += 1;
			}
			//No Errors?
			// $('#debugtext').text("<span>name=" + username + "&email=" + email + "&comment=" + comment + "&address=" + address + "&time_to_contact=" + time_to_contact + "&telephone=" + telephone + "&tvalue1=" + $('#tvalue1').val() + "&tvalue2=" + $('#tvalue2').val() + "&captavalue=" + $('#captavalue').val() + "</span>")
			if(error_count === 0) {
				//alert('sending e-mail');
				$.ajax({
					type: "post",
					url: "sendm.php",
					data: "name=" + username + "&email=" + email + "&comment=" + comment + "&address=" + address + "&time_to_contact=" + time_to_contact + "&telephone=" + telephone + "&tvalue1=" + $('#tvalue1').val() + "&tvalue2=" + $('#tvalue2').val() + "&captavalue=" + $('#captavalue').val(),
					error: function() {
						//alert('error');
						$('.error').hide();
						$('.sendError').slideDown('slow');
					},
					success: function () {
						//alert('success');
						$('.error').hide();
						$('.success').slideDown('slow');
						$('form#contactForm').fadeOut('slow');
					}				
				});	
			}
			
			else {
                $('.error').show();
            }
			
		return false;
	});
	
});

