/***************************/
//@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(){
	//global vars
	var form = $("#mmform");
	var name = $("#name");
	//var surname = $("#surname");
	var tel = $("#tel");
	var email = $("#email");
	var question = $("#question");
	
	//validateName();
	//$('#formtitletext').html('<h1>'.$('#formtexttitle').html.'<h1>');
	//$('#formtitletext').replaceWith("<h1>" + $('#formtexttitle').text() + "</h1>");
	
	//On blur
	//name.blur(validateName);
	//surname.blur(validateSurname);
	//tel.blur(validateTel);
	//email.blur(validateEmail);

	//On key press
	//name.keyup(validateName);
	//surname.keyup(validateSurname);
	//tel.keyup(validateTel);
	//email.keyup(validateEmail);

	//On Submitting
	$("form#brochure").submit(function(){
		var name = $("#bname");
		var email = $("#bemail");
		var tel = $("#btelephone");
		var property = $("#bproperty");
		property.val(property.val().replace(/[^a-zA-Z0-9, ]/g, ''));
		if(validateName(name)&validateEmail(email)&validateTel(tel)){
			$.ajax({  
                type: "POST",
                url: "/form-post/",  
                data: "contact_name="+ name.val() +"& contact_subject=Brochure Download - Footer Form& contact_email="+ email.val() +"& contact_phone="+ tel.val() +"& note_Property="+ property.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#brochure').html('<h3>Thank you for your brochure enquiry.</h3>');
                }  
            });        
			return false;
		}else{
			alert('All these fields are required, they also must be valid.');
			return false;
		}
	});
		$("form#brochure2").submit(function(){
		var name = $("#b2name");
		var email = $("#b2email");
		var tel = $("#b2telephone");
        var country = $("#b2country");		
		if(validateName(name)&validateEmail(email)&validateTel(tel)){
			$.ajax({  
                type: "POST",  
                url: "/form-post/",  
                data: "contact_name="+ name.val() +"& contact_subject=Brochure Download - Country Form& contact_email="+ email.val() +"& contact_phone=" + tel.val() +"& note_country="+ country.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#brochure2').html('<h3>Thank you for your brochure enquiry.</h3>');
                }  
            });        
			return false;
		}else{
			alert('All these fields are required, they also must be valid.');
			return false;
		}
	});
	
	$("form#newsletter").submit(function(){
		var name = $("#name");
		//var surname = $("#surname");
		var email = $("#email");
		if(validateName(name)&validateEmail(email)){
			$.ajax({  
                type: "POST",  
                url: "/form-post/",  
                data: "contact_name="+ name.val() +"& contact_subject=Newsletter Signup& contact_email="+ email.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#newsletter').html('<h3>Thank you for joining our newsletter.</h3>');
                }  
            });        
			return false;
		}else{
			alert('All these fields are required, they also must be valid.');
			return false;
		}
	});
	
	$("form#enquiry").submit(function(){
		var name = $("#enqname");
		//var surname = $("#surname");
		var email = $("#enqemail");
		var tel = $("#enqtel");
		var notes = $("#enqnotes");
		var property = $("#enqproperty");
		if(validateName(name)&validateEmail(email)&validateTel(tel)){
			$.ajax({  
                type: "POST",  
                url: "/form-post/",  
                data: "contact_name="+ name.val() +"& contact_email="+ email.val() +"& contact_subject=Property Enquiry& contact_phone=" + tel.val() +"& contact_message="+ notes.val() +"& note_Property="+ property.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#enquiry').html('<h3>Thank you for your enquiry.</h3>');
                }  
            });        
			return false;
		}else{
			alert('All these fields are required, they also must be valid.');
			return false;
		}
	});
	
	$("form#contact").submit(function(){
		var name = $("#contname");
		//var surname = $("#surname");
		var email = $("#contemail");
		var tel = $("#conttel");
		var notes = $("#contnotes");		
		if(validateName(name)&validateEmail(email)&validateTel(tel)){
			$.ajax({  
                type: "POST",  
                url: "/form-post/",  
                data: "contact_name="+ name.val() +"& contact_email="+ email.val() +"& contact_subject=Contact Form& contact_phone=" + tel.val() +"& contact_message="+ notes.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 == ""){
			email.removeClass("ok");
			email.removeClass("error");
			return false;
		}
		if(filter.test(a)){
			email.addClass("ok");
			email.removeClass("error");
			return true;
		//if it's NOT valid
		  }else{
			name.removeClass("ok");
			email.addClass("error");
			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();        
});
