/***************************/
//@Author: Adrian "yEnS" Mato Gondelle & Ivan Guardado Castro
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

$(document).ready(function(){


	//On Submitting
	$("form#contact").submit(function(){
		var name = $("#name");
		var company = $("#company");
		var email = $("#email");
		var tel = $("#tel");
		var noe = $("#noe");
		//alert("Name="+ name.val() +"& Email="+ email.val() +"& Telephone=" + tel.val() +"& Notes="+ notes.val());
		if(validateName(name)&validateEmail(email)&validateTel(tel)){
			$.ajax({
                type: "POST",  
                url: "/enquiry.php",  
                data: "Name="+ name.val() +"& Company="+ company.val() +"& Email="+ email.val() +"& Telephone=" + tel.val() +"& Nature of Enquiry="+ noe.val(),  
                success: function(){  
		          /*$('form#wsrform').html('<h3>Thank you for your enquiry and requesting the Brochure, If you have any further questions please contact us on <a href="mailto:info@whitesandsresorts.co.uk">info@whitesandsresorts.co.uk</a> or call: +44 (0)207 243 200</h3><script type=\"text/javascript\">window.onload = function(){document.location = \'/white_sands_brochure.pdf\';}</script>');*/
					$('form#contact').html('<h3>Thank you for your enquiry, we will get back to you as soon as possible.</h3>');
                }  
            });        
			return false;
		}else{
			alert('All these fields are required, they also must be valid.');
			return false;
		}
	});	

	//validation functions
	function validateEmail(email){
		//testing regular expression
		var rawa = email.val();
		if(rawa.match('@')){
		  var splita = rawa.split('@');
		  var a = splita[0];
		  var b = splita[1];
		  var b = b.toLowerCase();
		  var a = a +'@'+ b;
		}else{
		  var a="";
		}
		var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
		//if it's valid email
		if(a == ""){
			return false;
		}
		if(filter.test(a)){
			return true;
		//if it's NOT valid
		  }else{
			return false;
		}
	  }
	function validateName(name){
		// Ignore default value / label
		if(name.val().length == 0) {
			name.removeClass("ok");
			name.removeClass("error");
			return false;
		}
		//if it's NOT valid
		else if(name.val().length < 4){
			name.removeClass("ok");
			name.addClass("error");
			return false;
		}
		//if it's valid
		else{
			name.removeClass("error");
			name.addClass("ok");
			return true;
		}
	}
	function validateSurname(surname){
		//if it's NOT valid
		if(surname.val().length < 4){
			surname.removeClass("ok");
			surname.addClass("error");
			return false;
		}
		//if it's valid
		else{
			surname.removeClass("error");
			surname.addClass("ok");
			return true;
		}
	}
	function validateTel(tel){
		// Ignore default value / label
		if(tel.val().length == 0){
			tel.removeClass("ok");
			tel.removeClass("error");
			return false;
		}
		//if it's NOT valid
		else if(tel.val().length < 4){
			tel.removeClass("ok");
			tel.addClass("error");
			return false;
		}
		//if it's valid
		else{
			tel.removeClass("error");
			tel.addClass("ok");
			return true;
		}
	}	
});

/* Javascript for default text on input fields */

/* http://www.dailycoding.com/Posts/default_text_fields_using_simple_jquery_trick.aspx */

/* 

To use this code, include the validation.js in the page and include some CSS for the default text, i.e.: 

    .defaultText { width: 300px; }
    .defaultTextActive { color: #a1a1a1; font-style: italic; }
	
And then for any fields you want to include detault text, add the class .defaultText and in the Title parameter, 
add what default text you want to include.

*/

$(document).ready(function()
{
    $(".defaultText").focus(function(srcc)
    {
        if ($(this).val() == $(this)[0].title)
        {
            $(this).removeClass("defaultTextActive");
            $(this).val("");
        }
    });
    
    $(".defaultText").blur(function()
    {
        if ($(this).val() == "")
        {
            $(this).addClass("defaultTextActive");
            $(this).val($(this)[0].title);
        }
    });
    
    $(".defaultText").blur();
	
	$("form.propertysearch").submit(function(){
	  if($("form.propertysearch .defaultTextActive").length > 0){
		$("form.propertysearch .defaultTextActive").val('');
	  }
	});
	
});


