/*-----------------------------------------------------------------------------
Flow Interactive Validation Script

version:   1.0
author:    Flow Interactive
/*----------------------------------------------------------------------------
COPYRIGHT, ALL RIGHTS RESERVED. THIS FILE MAY NOT BE COPIED OR ALTERED IN ANY WAY
-----------------------------------------------------------------------------*/

function validate(thisform) {
	with (thisform) {
		if ($("#contactform")) {
			//check all form values completed
			$("input.val").each(function(i){
				if ($(this).attr("id") == "email") { 
					if (! validateEmail(this)) {
						$("~ span", this).show(); 
						$(this).css({ border: "1px solid #f00" });
						$("#errMess").show();
						this.focus();
						subfrm = 'false';
						return false;
					}
				}
				
				if (this.value == ""){ 
					$("~ span", this).show(); 
					$(this).css({ border: "1px solid #f00" });
					$("#errMess").show();
					this.focus();
					subfrm = 'false';
					return false;
				} else {
					$("~ span", this).hide();
					$("#errMess").hide();
					$(this).css({ border: "1px solid #069" });
					subfrm = 'true';
				}
			});
			var telNo = $("#telNo").value;
			if (telNo != "") {
				if (! validateNumber(telNo)) {
					$("~ span", $("#telNo")).show(); 
					$("#telNo").css({ border: "1px solid #f00" });
					$("#errMess").show();
					$("#telNo").focus();
					subfrm = 'false';
					return false;
				}
			}
		}
		if (subfrm == 'false') {
			return false;
		}else{
			$("#formdata").hide();
			$("#feedback").append("<p class='cntr'><img src='/i/ajax-loader.gif' /><br />Please wait, sending data</p>");
			
			var frmData = $("form").serialize();
			$.ajax({
			   type: "POST",
			   url: "process.php",
			  data: frmData,
			  
			   timeout: 10000,
				error: function(msg){
					$("#feedback > p").remove();
				 	$("#feedback").append(msg);
				},
			   success: function(msg){
				 $("#feedback > p").remove();
				 $("#feedback").html("<h3>Thank you</h3><p><strong>Your message has been successfully sent to us<br /></strong> and we will reply as soon as possible.</p><p align='center'>Thank you for contacting us.</p>");
			   }
			 });
			return false;
		}		
	}
 }

function resetForm() {
	$("#formdata").show();
	$("#feedback > p").remove();
	$("#feedback > h3").remove();
}

function validateNumber(field) {
	var stripped = field.value.replace(/[\s()+-]|ext\.?/gi, "");
	var regex = /\d{8}/;
	if (! stripped.match(regex)) {
        return (false);
    }
    return(true);
}

 function validateEmail(field) {
    //Validating the email field
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    if (! field.value.match(re)) {
        return (false);
    }
    return(true);
}
