
/* 
Author: Gabriel Comarita

Author's Website: http://www.bitrepository.com/

* Keep this notice intact for legal use *
*/

// Ensure that JQuery won't conflict with other libraries

var $j = jQuery.noConflict();


// Important for JS validation
var total_required_inputs = $j(":input.required").length; /* (usually name, email, subject, message & security code) */

// Are we using the basic form with no captcha? Then decrease the number of total required inputs
if(($j("#security_code").length == 0)) {
total_required_inputs--;
}

$j(function() { // When DOM is ready

    // Preload Icons
	// This way they will show instantly without waiting for the browser to load them (for instance the 'ajax loading spinner')
	
	img1 = new Image(18, 15);
    img1.src = 'http://www.cookiecrumbles.co.uk/mailing-app/images/ajax-loader.gif';
    
	img2 = new Image(22, 22);
    img2.src = 'http://www.cookiecrumbles.co.uk/mailing-app/images/icon-dialog-error.png';
	
	img3 = new Image(22, 22);
    img3.src = 'http://www.cookiecrumbles.co.uk/mailing-app/images/icon-button-ok.png';


    /*
    -------------------------------------------------
    Is the form (ID: 'ajax-contact-form') submitted?
    -------------------------------------------------
    */



    $j('#ajax-contact-form').submit(function() {


    check_sender_email();

       check_security_code();
       check_status();


       if($j(".ok").length < total_required_inputs) {
			  return false;
	   }
    
    
	// Hide 'Submit' Button
    $j('#submit-button').hide();

    // Show GIF Spinning Rotator
    $j('#ajax-loading').show();

    var formData = $j(this).serialize(); // Serializes a set of input elements into a string data (example: first_name=John&last_name=Doe)

    $j.ajax({
      type: "POST",
      url: 'http://www.cookiecrumbles.co.uk/mailing-app/parse.php',
      data: formData,
      success: function(response) {

	  //alert(response);

       $j("#note").ajaxComplete(function(event, request, settings) {

	   var possible_error = 'Could not instantiate mail function.';

	   if(response.indexOf(possible_error) != '-1') {
	   var result = '<div class="notification_error">The mail cannot be sent due to an internal error. Please retry later.<br /><br />'+ possible_error +'</div>';

	
	   } else {

        var status = $j.evalJSON(response).status;

       if(status == 0) { // Message sent
         var result = '<div class="notification_ok">Your request has been successfully sent. Thank you.</div>';

         
       }
       else if(status == 1) { // Errors found?

	          var result = '<div class="notification_error">Please correct the errors and re-submit.<br /><br />';
			  
			  
			  // First, remove all errors to avoid adding the same errors twice in the page
			  // If any errors are found, the script will change the 'class' value(s) (error)

		      $j('label.error').remove();

		      if($j.evalJSON(response).sender_email_none) {
			       $j('#sender_email').addClass('error').removeClass('ok');

		            result += 'Please fill in an e-mail address<br />';
			  
			     } else if($j.evalJSON(response).sender_email_invalid) {
			 
			        result += 'Please fill in a valid e-mail address<br />';

			     } else if($j.evalJSON(response).sender_email_min_chars) {
			 
			        result += '<br />';

			     } else  {
                    $j('#sender_email').addClass('ok').removeClass('error');
                 }
			  if($j.evalJSON(response).security_code == 1) {
			  $j('#security_code').addClass('error').removeClass('ok');
			  
              result += 'Please enter the security code.<br />';

			  } else if($j.evalJSON(response).security_code == 2) {
			  $j('#security_code').addClass('error').removeClass('ok');
			  
			  result += 'The security code is incorrect.<br />';

              } else {
              $j('#sec_div').html('<img class="ok" id="verified" src="http://www.cookiecrumbles.co.uk/mailing-app/images/icon-tick-circle-frame.png" width="16" height="16">&nbsp;<font color="green">Verified</font>');
			  $j('#captcha_div').remove();
              $j('#sc_error').remove();

              }

			  			  
			  result += '</div>';

			  // Mail cannot be sent?
       } else if(status == 2) {
	         var result = '<div class="notification_error"></div>';

			 	   }

	   }

              // Hide GIF Spinning Rotator
	          $j('#ajax-loading').hide();
	         
	          // Show 'Submit' Button
	          $j('#submit-button').show();

			  // Could be notification error or a notification that the form has been submitted successfully / Show the notification with a "fade in" effect
	          $j(this).html(result).slideDown();

});

}

});

return false; // prevent the form from being submitted in the classical way

        });

});

function new_captcha()
{
var c_currentTime = new Date();
var c_miliseconds = c_currentTime.getTime();

document.getElementById('captcha').src = 'mailing-app/captcha/image.php?x='+ c_miliseconds;

return false;
}

$j('#captcha-refresh').bind('click', new_captcha);
