$(document).ready(function() {
	
/* 
	$('#bulletin_surname').focus(function() {
		if ($('#bulletin_surname').val() == 'Surname') {
			$('#bulletin_surname').val('');	
		}
	});
	
	$('#bulletin_surname').blur(function() {
		if ($('#bulletin_surname').val() == '') {
			$('#bulletin_surname').val('Surname');	
		}
	});
	
	
	
	$('#bulletin_forename').focus(function() {
		if ($('#bulletin_forename').val() == 'Forename') {
			$('#bulletin_forename').val('');	
		}
	});
	
	$('#bulletin_forename').blur(function() {
		if ($('#bulletin_forename').val() == '') {
			$('#bulletin_forename').val('Forename');	
		}
	});
	
	
	
	$('#bulletin_companyName').focus(function() {
		if ($('#bulletin_companyName').val() == 'Company name') {
			$('#bulletin_companyName').val('');	
		}
	});
	
	$('#bulletin_companyName').blur(function() {
		if ($('#bulletin_companyName').val() == '') {
			$('#bulletin_companyName').val('Company name');	
		}
	});
	
	
	
	$('#bulletin_postCode').focus(function() {
		if ($('#bulletin_postCode').val() == 'Postcode') {
			$('#bulletin_postCode').val('');	
		}
	});
	
	$('#bulletin_postCode').blur(function() {
		if ($('#bulletin_postCode').val() == '') {
			$('#bulletin_postCode').val('Postcode');	
		}
	});
	
	
	
	$('#bulletin_telephone').focus(function() {
		if ($('#bulletin_telephone').val() == 'Telephone number') {
			$('#bulletin_telephone').val('');	
		}
	});
	
	$('#bulletin_telephone').blur(function() {
		if ($('#bulletin_telephone').val() == '') {
			$('#bulletin_telephone').val('Telephone number');	
		}
	});
	
	
	
	$('#bulletin_email').focus(function() {
		if ($('#bulletin_email').val() == 'Your email') {
			$('#bulletin_email').val('');	
		}
	});
	
	$('#bulletin_email').blur(function() {
		if ($('#bulletin_email').val() == '') 
		{
			$('#bulletin_email').val('Your email');	
		}
	});
	
	
	
	$('#bulletin_emailConfirm').focus(function() {
		if ($('#bulletin_emailConfirm').val() == 'Please confirm your email') {
			$('#bulletin_emailConfirm').val('');	
		}
	});
	
	$('#bulletin_emailConfirm').blur(function() {
		if ($('#bulletin_emailConfirm').val() == '') {
			$('#bulletin_emailConfirm').val('Please confirm your email');	
		}
	});
	
	
	
	$('#bulletin_passwordClear').focus(function() {
		//alert("bulletin_passwordClear.focus");
//		if ($('#bulletin_passwordClear').val() == 'Create your password') {
		if ($('#bulletin_passwordClear').val() == 'Create password (6 or more chars)') 
		{
			$('#bulletin_passwordClear').hide();
			$('#bulletin_password').val('');
			$('#bulletin_password').show();
			$('#bulletin_password').focus();	
		}
	});
	
	$('#bulletin_password').blur(function() {
		if ($('#bulletin_password').val() == '') 
		{
			$('#bulletin_password').hide();
			$('#bulletin_passwordClear').show();
		}
	});
	
	
	$('#bulletin_passwordConfirmClear').focus(function() {
		if ($('#bulletin_passwordConfirmClear').val() == 'Please confirm your password') 
		{
			$('#bulletin_passwordConfirmClear').hide();
			document.getElementById('bulletin_passwordConfirm').style.display = 'block';
			//$('#passwordConfirm').show();
			$('#bulletin_passwordConfirm').val('');
			$('#bulletin_passwordConfirm').focus();	
		}
	});
	
	$('#bulletin_passwordConfirm').blur(function() {
		if ($('#bulletin_passwordConfirm').val() == '') 
		{
			$('#bulletin_passwordConfirm').hide();
			$('#bulletin_passwordConfirmClear').show();
			$('#bulletin_password').focus();
		}
	});
	 */
	function defaultTextRemove(){
		
		$('.default').each(function(){			
			var defaultVal = $(this).attr('title');
			
			if ($(this).val() == defaultVal)
			{
				$(this).val('');
			}			
		});
	}
	
	// register button press
	$("#regBulletinButton").click(function(){
				
		var $formId = $(this).parents('form');
//		var $formAction = $formId.attr('action');
		var emailReg = /^([\w-\.']+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		// In preparation for validating the form - Remove any active default text and previous errors
		defaultTextRemove();
		
		$('.error', $formId).each(function(){
			$(this).removeClass('error');
		});
		
		$('.bulletinCheckBoxSpan').removeClass('bulletinCheckBoxError');
		$('#bulletinContactErrs').html("&nbsp;");
		$('#bulletinEmailErrMsg').html("&nbsp;");
		$('#bulletinPwdErrMsg').html("&nbsp;");
		
		var bInvalidEmail = false;
		var bInvalidPwd = false;
		var bValidationErr = false;
		
		// Start validation by selecting all inputs with the class "required"
		$('.required', $formId).each(function(){
			var inputVal = $(this).val();
			
			if (inputVal == '')
			{
				var defaultVal = $(this).attr('title');
				
				$(this).addClass('error').val(defaultVal);
				
				if (!$(this).hasClass('ignoreRequired'))	// ignore password clear fields.
				{
					$('#bulletinContactErrs').html("<strong>All fields are required!</strong>");
					bValidationErr = true;
				}
			}
			
			// Run the email validation using the regex for those input items also having class "email"
			if ($(this).hasClass('email') == true)
			{
				if (!emailReg.test(inputVal))
				{
					bInvalidEmail = true;
					$(this).addClass('error');
					$('#bulletinEmailErrMsg').html("<strong>*Enter a valid email address</strong>");
					bValidationErr = true;
					
					// As the main email is incorrect reset the email confirmation text.
					$('#bulletin_emailConfirm').val($('#bulletin_emailConfirm').attr('title'));
					$('#bulletin_emailConfirm').addClass('error');
				}
			}
			
			// Does the emailConfirm match?
			if ($(this).hasClass('emailConfirm') == true && !bInvalidEmail)
			{
				if ($(this).val() != $('#bulletin_email').val())
				{
					$(this).addClass('error');
					$('#bulletinEmailErrMsg').html("<strong>*Please confirm your email!</strong>");
					bValidationErr = true;
				}
			}
			
			// Is the password the correct length?
			if ($(this).hasClass('bulletin_password'))
			{
				if ($(this).val().length < 6)
				{
					bInvalidPwd = true;
					$(this).addClass('error');
					$('#bulletinPwdErrMsg').html("<strong>*Password must be at least 6 characters.<strong>");
					bValidationErr = true;
					
					$('#bulletin_passwordConfirm').val('');
					//$('#bulletin_passwordConfirm').hide();
					//$('#bulletin_passwordConfirmClear').show();
				}
			}
			
			if ($(this).hasClass('bulletin_passwordConfirm') && !bInvalidPwd)
			{
				if ($(this).val() != $('#bulletin_password').val())
				{
					$(this).addClass('error');
					$('#bulletinPwdErrMsg').html("<strong>*Passwords don't match<strong>");
					bValidationErr = true;
				}
			}
			
		});

		var bulletinSelected = $('.bulletinCheckBox').filter(":checked").length;
		
		if (bulletinSelected == 0)
		{			
			$('.bulletinCheckBoxSpan').addClass('bulletinCheckBoxError');
			bValidationErr = true;
		}
				
		if (bValidationErr)
		{
			return false;
		}
				
		$("#bulletinForm").ajaxSubmit(
			{
				dataType : 'xml',
				type : 'post',
				url : '/cedrec_v2/application/modules/customer/controllers/ajaxRegister.php',
				success : bulletinRegSuccessResponse,
				error : bulletinRegErrorResponse
			}
		);
		
		return false;
		
	});
		
	function bulletinRegErrorResponse(responseXML)
	{
		alert(responseXML.responseText);
	}
		
	function bulletinRegSuccessResponse(responseXML)
	{
//		alert("bulletinRegSuccessResponse");

		var msg = "";
					
		// Find any validation errors
		$(responseXML).find('validate').each(function(){
			msg += $(this).text() + "<br />";
		});
		
		// Find any other errors
		$(responseXML).find('error').each(function(){
			msg += "Error: " + $(this).text() + "<br />";
		});
		
		if (msg.length)
		{
			$("#bulletinRegResult").html(msg);
			return;
		}
		else
		{
			$("#bulletinRegResult").html("");
		}
		
		//--------------------------------------------------------------------------------
		// Is user already in the temp table?
		//--------------------------------------------------------------------------------
		msg += $(responseXML).find('in_temp_table').text();
	
		if (msg.length)
		{
			// The user has already registered.
			// Generate a form so they can get the confirmation email resent.
							
			var strEmail = $(responseXML).find('email_address').text();
			
			msg += "<input type=\"hidden\" name=\"strEmail\" value=\"" + strEmail  +  "\" />";
			msg += "<button type=\"button\" id=\"bulletinResendEmail\">Resend Email</button>";
			
			$("#bulletinRegResult").html(msg);			
			$("#bulletinFormContent").html("");
			
			return;
		}
		
		//--------------------------------------------------------------------------------
		// Is the user already in the user table?
		//--------------------------------------------------------------------------------
		msg += $(responseXML).find('in_user_table').text();
		
		if (msg.length)
		{
			// Hide the form body
			$('#bulletinButton').slideDown('slow');
			$('#bulletinContent').slideUp('slow');
			
			$("#bulletin").html(msg);
//			$("#bulletinRegResult").html(msg);
			$("#bulletinFormContent").html("");
			return;				
		}
		
		//--------------------------------------------------------------------------------
		// Has the activation email been sent?
		//--------------------------------------------------------------------------------
		msg += $(responseXML).find('activation_email_sent').text();
		
		if (msg.length)
		{
			$("#bulletinRegResult").html(msg);
			$("#bulletinFormContent").html("");
			return;
		}

	}
	
	// User has clicked the resend activation email button
	$('#bulletinResendEmail').live('click', function(){
//		$('#bulletinResendEmailForm').ajaxSubmit(
		$('#bulletinForm').ajaxSubmit(
			{
				dataType: 'xml',
				type: 'post',
				url: '/cedrec_v2/application/modules/customer/controllers/ajaxResendEmail.php',
				success: bulletinResendSuccessResponse,
				error: bulletinResendErrorResponse
			}
		);
		
		return false;
	});
	
	function bulletinResendSuccessResponse(responseXML)
	{			
		var msg = "";
		
		// Find any other errors
		$(responseXML).find('error').each(function(){
			msg += "Error: " + $(this).text() + "<br />";
		});
		
		msg += $(responseXML).find('activation_email_sent').text();
		
		$("#bulletinRegResult").html(msg);		
	}
	
	function bulletinResendErrorResponse(responseXML)
	{
		alert(responseXML.responseText);
	}
		
});		// End of $(document).ready(function(){})

function cancelBulletin()
{
	// Set the form back to it's original state.	
	$('.default').each(function(){		
		var strDefault = $(this).attr('title');	// The title value is the default text to be displayed.		
		$(this).val(strDefault);
	});
	
	// Uncheck the bulletin checkboxes.
	$('.bulletinCheckBox').attr('checked', false);
	
	$('#bulletin_password').val('');
	//$('#bulletin_password').hide();
	//$('#bulletin_passwordClear').show();
	
	$('#bulletin_passwordConfirm').val();
	//$('#bulletin_passwordConfirm').hide();
	//$('#bulletin_passwordConfirmClear').show();
	
	// Remove all error classes
	$('.error').each(function(){
		$(this).removeClass('error');
	});
	
	$('.bulletinCheckBoxSpan').removeClass('bulletinCheckBoxError');
	
	// Remove any error messages
	$('#bulletinContactErrs').html("&nbsp;");
	$('#bulletinEmailErrMsg').html("&nbsp;");
	$('#bulletinPwdErrMsg').html("&nbsp;");
		
	// Hide the form body
	$('#bulletinButton').slideDown('slow');
	$('#bulletinContent').slideUp('slow');
}




