
$(document).ready(function(){
	    
	    $(".check_mail").click(function (){
	    	
			var error = 0;
			var set_focus = "";
			$(this).attr("disabled", true);
	
			var name = $("#form_name").val();
			if(name == ""){
				error = 1;
				$("#p_form_name").addClass("error");
				if(set_focus == ""){
					set_focus = 'form_name';
				}
			}
	
			var email = $("#form_email").val();
			var valid = Validate_Email_Address(email);
			if(valid == false){
				error = 1;
				$("#p_form_email").addClass("error");
				if(set_focus == ""){
					set_focus = 'form_email';
				}
			}
			
			var phone = $("#form_phone").val();
			if(phone == ""){
				error = 1;
				$("#p_form_phone").addClass("error");
				if(set_focus == ""){
					set_focus = 'form_phone';
				}
			}
	
			var postcode = $("#form_postcode").val();
			if(postcode == ""){
				error = 1;
				$("#p_form_postcode").addClass("error");
				if(set_focus == ""){
					set_focus = 'form_postcode';
				}
			}
	
			var message = $("#form_message").val();
			if(message == ""){
				error = 1;
				$("#p_form_message").addClass("error");
				if(set_focus == ""){
					set_focus = 'form_message';
				}
			}
	
			if(error == 1){
				$(this).attr("disabled", false);
				if(set_focus != ""){
					$("#"+set_focus).focus();
				}
			}
			else{
				
				var html = $.ajax({
					type: "POST",
					url: "/ajax/ajax_send_contact_mail.php",
					data: "name="+name+"&email="+email+"&phone="+phone+"&postcode="+postcode+"&message="+message,
					async: false
				}).responseText;
	
				$(".big_form").html(html);

			}
	    	
	    });
	    
	$(".banner").click(function () {
		$(".big_form").slideToggle("slow");
	});
});

function Validate_String(string, return_invalid_chars){
	valid_chars = '1234567890-_.^~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	invalid_chars = '';

	if(string == null || string == '')
	return(true);

	//For every character on the string.
	for(index = 0; index < string.length; index++){
		char = string.substr(index, 1);

		//Is it a valid character?
		if(valid_chars.indexOf(char) == -1){
			//If not, is it already on the list of invalid characters?
			if(invalid_chars.indexOf(char) == -1){
				//If it's not, add it.
				if(invalid_chars == '')
				invalid_chars += char;
				else
				invalid_chars += ', ' + char;
			}
		}
	}

	//If the string does not contain invalid characters, the function will return true.
	//If it does, it will either return false or a list of the invalid characters used
	//in the string, depending on the value of the second parameter.
	if(return_invalid_chars == true && invalid_chars != ''){
		last_comma = invalid_chars.lastIndexOf(',');

		if(last_comma != -1){
			invalid_chars = invalid_chars.substr(0, $last_comma) + ' and ' + invalid_chars.substr(last_comma + 1, invalid_chars.length);
		}
		return(invalid_chars);
	}
	else
	return(invalid_chars == '');
}


function Validate_Email_Address(email_address){
	//Assumes that valid email addresses consist of user_name@domain.tld
	at = email_address.indexOf('@');
	dot = email_address.indexOf('.');

	if(at == -1 ||
	dot == -1 ||
	dot == 0 ||
	dot == email_address.length - 1)
	return(false);

	user_name = email_address.substr(0, at);
	domain_name = email_address.substr(at + 1, email_address.length);

	if(Validate_String(user_name) === false ||
	Validate_String(domain_name) === false)
	return(false);

	return(true);
}