/********************************************
 *
 *   Copyright 2010, Dokodemo Design
 *   www.dokodemodesign.com
 *	 Javascript Jquery document for contact checking functions
 *   Authors: Justin Wright
 *   Source code: 
 *******************************************/

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)


 $(document).ready(function() {  
	jQuery.preLoadImages("http://dokodemodesign.com/images/modal/thankyou.png");
   	
	/*---------------------------------
	MODAL
	----------------------------------*/
	
	
	  //select all the a tag with name equal to modal  
     $('a[name=modal]').click(function(e) {  
         //Cancel the link behavior  
         e.preventDefault();  
         //Get the A tag  
         //var id = $(this).attr('href'); 
		 var id = '#contact';
       
         //Get the screen height and width  
         var maskHeight = $(document).height();  
         var maskWidth = $(window).width();  
       
         //Set height and width to mask to fill up the whole screen  
         $('#mask').css({'width':maskWidth,'height':maskHeight});  
           
         //transition effect       
         //$('#mask').fadeIn(1000);      
         $('#mask').fadeTo("slow",0.7);    
       
         //Get the window height and width  
         var winH = $(window).height();  
         var winW = $(window).width();  
                 
         //Set the popup window to center  
         $(id).css('top',  (winH/2-$(id).height()/2)-$(id).height()/4);  
         $(id).css('left', winW/2-$(id).width()/2); 
		 // IE6 Fix change position from fixed to absolute
		 var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : 'fixed'; 
       	 $(id).css('position', pos);
		 
         //transition effect  
         $(id).fadeIn("slow");
		
       	//focus on name
		$('input#name').focus();
		
     });  
       
     //if close button is clicked  
     $('.window .close').click(function (e) {  
         //Cancel the link behavior  
         e.preventDefault();  
         $('#mask, .window').hide();  
     });
	 
	 $('.window .close').live('click', function(e) {
  		// Live handler called.
   		//Cancel the link behavior  
         e.preventDefault();  
         $('#mask, .window').hide();  
	});
       
     //if mask is clicked  
     $('#mask').click(function () {  
         $(this).hide();  
         $('.window').hide();  
     });           
	
	
	
	
	
	
	
	
	
	/*---------------------------------
	CONTACT
	----------------------------------*/
	
	
	var ok = false;
	$('.contactWarning, .sending').hide();
    $('.submitBtn').click(function(e) { 
	  //Cancel the link behavior   
       e.preventDefault();
	  
	  // validate and process form here
	  
	  $('.contactWarning, .sending').hide();
	  
       var name = $("input#name").val();  
       
	   if (name == "") {  
       $("span#name_error").show();  
       $("input#name").focus();   
       }//close name if
	   
	   var email = $("input#email").val();  
       if (email == "") {  
       $("span#email_error").show();  
       $("input#email").focus();   
       }//close email if 
	   
	   
	   var comments = $("textarea#comments").val();  
       if (comments == "") {  
       $("span#comments_error").show();  
       $("textarea#comments").focus();    
       }//close comments
	   
	   
	   var security = $("input#security").val();  
       if (security == "") {  
       $("span#security_error").show();  
       $("input#security").focus();   
       }
	   
	   //Check if all fields have been filled in correctly
	   
	   if(name != "" && email !="" && comments !="" && security != "") {   
		  ok = true;
	   }else{
		  ok = false;
	   }
	   
	 
	   //if all form fields are filled in
	   if(ok){
	   
	   $('.sending').ajaxStart(function() { $(this).show(); $("input[type='submit']").attr("disabled", true).val("Please wait");
	   }).ajaxStop(function() { $(this).hide(); $("input[type='submit']").attr("disabled", false).val("Send message"); });
	
	   //get data values into data string
	   var dataString = 'action=sendmail&name='+ name + '&email=' + email + '&comments=' + comments + '&security=' + security;  
											
 		$.ajax({
		 	url: 'http://dokodemodesign.com/ajax_contact.php',
			data: dataString,
			type: 'post',
			dataType: 'json',
			success: function(data, textStatus) {
				if (data.error) {
            	// data.redirect contains the string URL to redirect to
            	//window.location.href = data.redirect;
				// data.info contains the HTML for the replacement form	
				$(".capatcha").empty().html(data.error);
				$("span#security_error").show();  
       			$("input#security").focus();
        		}else if (data.missing){
				$("span#email_error").show();  
       			$("input#email").focus();
				}else{
				
				//data goes here
				$('#contact-content').empty().hide();
				$('#contact-content').html(data.good).fadeIn("slow");;
									
				//$errorBoxUser.show().html(data.errors);
				}
				//return false;
				
		}//close success
		});//close ajax
	   
	   }//close if flag
	   
	   
	}); //close submitbtn function
       
 });  
