/* [RealTime Validation] */

/*
--------------------
E-Mail
--------------------
*/

var check_sender_email = function() {

if($j(":input[name='sender_email'].required").length) { // Required?

$j('#sender_email_error').remove();
$j('#sender_email_error_invalid').remove();
$j('#sender_email_error_min_chars').remove();

 var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i 

if($j('#sender_email').val() == '') {
$j('#sender_email').addClass('error').removeClass('ok').after('<label id="sender_email_error" class="error">Please fill in an e-mail address</label>');

check_status();
} 


else if (!filter.test($j('#sender_email').val())) {

$j('#sender_email').addClass('error').removeClass('ok').after('<label id="sender_email_error_invalid" class="error">Please fill in a valid e-mail address</label>');

check_status();
}



else {
$j('#sender_email').addClass('ok').removeClass('error');
}

}

};

$j('#sender_email').bind('change', check_sender_email);
$j('#sender_email').bind('blur', function() { if($j('#sender_email').val()) { check_sender_email(); } });


/*
-----------------
Security Code
-----------------
*/

var check_security_code = function() {

//if($j('#security_code').val().length == 5) {

if ($j('#captcha_div').is(':visible')) {

$j('#sc_error').remove();
	

if($j('#security_code').val() == '') {

$j('#security_code').addClass('error').removeClass('ok');
$j('#sec_div').after('<label id="sc_error" class="error">Please enter the security code.</label>');

check_status();

} else {

var c_currentTime = new Date();
var c_miliseconds = c_currentTime.getTime();

var validCode = $j('#security_code').val();

/* [Start] AJAX Call */

$j.ajax({ url: 'http://www.cookiecrumbles.co.uk/mailing-app/verify-code.php?x='+ c_miliseconds, 
	     data: "security_code="+ validCode,
	     type: 'post', 
	     datatype: 'html', 
	     success: function(outData) { 

	      	          if(outData != 1) {

	      	            if($j("#sc_error.error").length == 0) {
	      	                $j('#security_code').addClass('error').removeClass('ok');
	      	                $j('#sec_div').after('<label id="sc_error" class="error">The security code is incorrect.</label>');

							check_status();
	      	            }

	      	          } else {

						  //$j('#sc_error').remove();

                      $j('#security_code').remove();
                      $j('#sec_div').css({ margin: '8px 0 0 0' }).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>').fadeIn('slow', function() { $j('#captcha_div').hide(); $j('#submit-button').before('<input type="hidden" name="security_code" id="security_code" value="'+ validCode +'" />'); });
					  
					  //$j('div').removeClass("highlighted");

					  }
					  
		              }, 

	     error: function(errorMsg) { alert('Error occured: ' + errorMsg); }});

/* [End] AJAX Call */

}

}

//}

};

var checkSecurityCodeLive = function() {

var c_currentTime = new Date();
var c_miliseconds = c_currentTime.getTime();

var validCode = $j('#security_code').val();

/* [Start] AJAX Call */

$j.ajax({ url: 'http://www.cookiecrumbles.co.uk/mailing-app/verify-code.php?x='+ c_miliseconds, 
	     data: "security_code="+ validCode,
	     type: 'post', 
	     datatype: 'html', 
	     success: function(outData) { 

	      	          if(outData == 1) {

					  $j('#sc_error').remove();

                      $j('#security_code').remove();
                      $j('#sec_div').css({ margin: '8px 0 0 0' }).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>').fadeIn('slow', function() { $j('#captcha_div').hide(); $j('#submit-button').before('<input type="hidden" name="security_code" id="security_code" value="'+ validCode +'" />'); });
					  
					  $j('div').removeClass("highlighted");

					  check_status();

					  }
					  
		              }, 

	     error: function(errorMsg) { alert('Error occured: ' + errorMsg); }});

/* [End] AJAX Call */

};

//$j('#sc_error').remove();

$j('#security_code').bind('change', check_security_code);
$j('#security_code').bind('blur', function() { if($j('#security_code').val()) { check_security_code(); } });

$j('#security_code').keyup(checkSecurityCodeLive);

$j(':input.required').bind('change blur keyup', check_status);

function check_status() {


if($j("label.error").length > 0) { 
	// Show the top notice error
	$j('#note').html('<div class="notification_error">Please correct the errors and re-submit.</div>').slideDown('slow');
}

if($j("label.error").length == 0) { 
	$j('#note').slideUp('slow'); // Hide the top notice error using a 'slide' effect (if necessary)
}

return true;

}


