// JavaScript Document
var processing = false;

$(document).ready(function() {
			
	$('#submit-btn').click(function(e) {
		e.preventDefault();
		$('#contactForm').trigger("submit");
	});	
	
	$('#contactForm').submit(function(e) {
		e.preventDefault();
		if(contactValid()) {
			sendContactForm();
		}
	});
	
	$('#contactname').focus(function() {
		if($(this).val()=='Your name') { 
			$(this).val('');
		}  else {
			$(this).select();
		}
	});
	$('#contactname').blur(function() {
		if($(this).val()=='') { $(this).val('Your name'); } 
	});
	
	$('#contactemail').focus(function() {
		if($(this).val()=='Your email') { 
			$(this).val('');
		} else {
			$(this).select();
		}
	});
	$('#contactemail').blur(function() {
		if($(this).val()=='') { $(this).val('Your email'); } 
	});
	
	$('#contactquery').focus(function() {
		if($(this).val()=='Comments') { 
			$(this).val('');
		} else {
			$(this).select();
		}
	});
	
	$('#contactquery').blur(function() {
		if($(this).val()=='') { $(this).val('Comments'); } 
	});
})

function contactValid() {
	
	var isValid = true;
	
	//check name string
	var name_str = $('input[name=contactname]').val();
	if(name_str=='' || name_str == "Your name") {
		problem($('input[name=contactname]'));
		isValid = false;
	} else {
		$(".contactname_error").remove();
	}	
	//check email string
	var email_val = $('input[name=contactemail]').val();
	if(email_val.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1) {
		problem($('input[name=contactemail]'));
		isValid = false;
	} else {
		$(".contactemail_error").remove();
	}
	
	return isValid;
	
}

function sendContactForm() {

	processing=true;
	
	$('#submit-btn').css({'background-position':'0 -41px'});
	$.ajax({
		type:"get",
		url:"/assets/sendcontact.php",
		data: $('#contactForm').serialize(),
		success: function(r) {
			processing=false;
			if(r!='') { //we have a response
				if(r.substr(0,2)=='0:') {
					//fail?
				} else {
					$('#contactus').css({'background-position':'bottom'});
					$('#contactus form').fadeOut("fast");
					$('#contactus form')[0].reset();
					$('#contactus p').fadeOut("fast",function(){
						$('#contactus p').html("Thank you for your <strong>enquiry</strong>.");
						$('#contactus p').css({'font-size':'16px'});
						Cufon.replace('#contactus p', { fontFamily: 'Helvetica Neue Lt Std Cnd 57' });
						Cufon.replace('#contactus p strong', { fontFamily: 'Helvetica Neue Lt Std Bold Cnd 77' });
						$('#contactus').stop().animate({'height':'43px'},300, function() {
							$('#contactus p').fadeIn();
							setTimeout(function() {
								$('#contactus p').fadeOut("fast", function() {
									$('#contactus p').html('Send us your query and or comment:');
									$('#contactus p').css({'font-size':'10px','font-family':'Verdana'});
									$('#contactus p strong').css({'font-family':'Verdana'});
									$('#contactus').stop().animate({'height':'350px'},300, function() {
										$('#contactus p').fadeIn();
										$('#contactus form').fadeIn();
									});
								});	
							}, 5000);
						});
					});
				}
			} else { //no response
				//fail
			}
		}
	});				

}
