/* Author: 

*/

$(document).ready(function() {
	$('.fader ul').innerfade({
		speed: 1000,
		timeout: 7000,
		type: 'random',
		containerheight: '300px'
	});

	//if submit button is clicked
    $('#submit').click(function () {
    
        //Get the data from all the fields
        var name = $('input[name=name]');
        var email = $('input[name=email]');
        var tel = $('input[name=tel]');
        var brief = $('textarea[name=brief]');
        var human = $('input[name=website]');
        
        var error = 0;
 
        //Simple validation to make sure user entered something
        //If error found, add hightlight class to the text field
        if (name.val()=='') {
            name.addClass('highlight');
            //return false;
            error = 1;
        } else name.removeClass('highlight');
         
        if (email.val()=='') {
            email.addClass('highlight');
            //return false;
            error = 1;
        } else email.removeClass('highlight');
        
        if (human.val()!='') {
            //return false;
        	$('.sent').remove();  
        	$('.error').remove(); 
        	$('#submit').before('<p class="error">Sorry but the tests we have in place to check that you are human have failed. Please double check then try again.</p>');
        }
         
//        if (tel.val()=='') {
//            tel.addClass('highlight');
            //return false;
//            error = 1;
//        } else tel.removeClass('highlight');
         
        //organize the data properly
        var data = 'name=' + name.val() + '&email=' + email.val() + '&tel='
        + tel.val() + '&brief='  + encodeURIComponent(brief.val());
         
        //disabled all the text fields
        $('.text').attr('disabled','true');
         
        //show the loading sign
        $('.loading').show();
        
        if(!error) {
	        //start the ajax
	        $.ajax({
	            //this is the php file that processes the data and send mail
	            url: "process.php", 
	             
	            //GET method is used
	            type: "GET",
	 
	            //pass the data         
	            data: data,     
	             
	            //Do not cache the page
	            cache: false,
	             
	            //success
	            success: function (html) {              
	                //if process.php returned 1/true (send mail success)
	                if (html==1) {
	                	$('.error').remove();
	                	$('.sent').remove();  
	                    $('#submit').before('<p class="sent">Your message has been sent. Someone will contact you as soon as possible.</p>');
	                     
	                //if process.php returned 0/false (send mail failed)
	                } else {
	                	$('#submit').before('<p class="error">There was an error. Please contact us by emailing us <a href="mailto:hello@inknpixel.co.uk">here</a></p>'); 
	                }             
	            }       
	        });
	    } else {
	        $('.sent').remove();  
	        $('.error').remove(); 
	    	$('#submit').before('<p class="error">Please make sure you have a filled in all required fields and try again.</p>');
	    }
        //cancel the submit button default behaviours
        return false;
    });
    
    
});

















