/***************************/
//@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#support").submit(function(){
		$('.error').empty();
		var name = $("#name");
		var position = $("#position").val();
		var company = $("#company").val();
		var email = $("#e-mail");
		var tel = $("#telephone").val();
		var address1 = $("#address1").val();
		var address2 = $("#address2").val();
		var address3 = $("#address3").val();
		var city = $("#city").val();
		var postcode = $("#postcode").val();
		var country = $("#country").val();
		var details = $("#details").val();
		var product = $("#productName").val();
		var requestType = $("#requestType").val();
				if(validateName(name)&validateEmail(email)&(position!="")&(company!="")&(city!="")&(postcode!="")&(country!="")&(details!="")&(validateCaptcha())){
			$.ajax({  
                type: "POST",
                url: "/enquiry.php",  
                data: "Name="+ name.val() +"& Email="+ email.val() +"& Request Type=" + requestType +" & Product=" + product + "& Position="+ position +"& Company="+ company +"& City="+ city +"& Telephone="+ tel +"& Address Line 1="+ address1 +"& Address Line 2="+ address2 +"& Address Line 3="+ address3 +"& city="+ city +"& postcode="+ postcode +"& country="+ country +"& Details=" + details,  
                success: function(){  
		
					$('form#support').html('<h3>Thank you for your Support Request.</h3>');
                }  
            });        
			return false;
		}else{
		$('.error').append('<h3>Please complete all required fields</h3>');
		$('.required').css('color','#cc0000');
			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");
			//error message div-$('.error').append('<h3>Please complete all required fields</h3>');
			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;
		}
	}
	
function validateCaptcha()
{
    challengeField = $("input#recaptcha_challenge_field").val();
    responseField = $("#recaptcha_response_field").val();
    //alert(challengeField);
    //alert(responseField);
    //return false;
    var html = $.ajax({
    type: "POST",
    url: "/ajax.recaptcha.php",
    data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField,
    async: false
    }).responseText;
 
    if(html == "success")
    {
        $("#captchaStatus").html(" ");
        // Uncomment the following line in your application
        return true;
    }
    else
    {
        $("#captchaStatus").html("Your captcha is incorrect. Please try again");
        Recaptcha.reload();
        return false;
    }
}
});


/* 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();        
});
