function submitEntryFormLink() {
	$('#submitEntryFormLink').click();
}

$(document).ready(function() {

var charlimit = 500;
var reasonrequired = false;



    $('#submitEntryFormLink').click(function() {

		$.ajax({
			url: '/submitEntryForm.html', 
			type: 'get',
			cache: false,
			dataType: 'html',
			complete: function(jqXHR, textStatus) {
				//alert('complete:'+textStatus);

			},
			success: function(data, textStatus, jqXHR) {
				success_data = data;
				success_textStatus = textStatus;
				success_jqXHR = jqXHR;
				
				//alert($('#submitEntryFormContainer').html());
				$('#submitEntryFormContainer').html(data);
				
				countAreaChars(document.getElementById('entryreason'),document.getElementById('countchars'),charlimit);
				document.getElementById('maxchars').innerHTML = charlimit;
				
				
				$.blockUI({
		        message: $('#submitEntryFormContainer'),
		        css: {
		            border: 'solid', 
		            borderWidth: '8px', 
		            borderColor: '#000', 
		            padding: '5px 10px 25px 10px', 
		            backgroundColor: '#fff', 
		            '-webkit-border-radius': '12px', 
		            '-moz-border-radius': '12px', 
		            color: '#000',
		            textAlign: 'center',
		            width: '480px',
		            height: '300px'
		        },
		        overlayCSS: {
		        backgroundColor: '#fff'
		        },
		        showOverlay: true,
		        fadeIn: 500
				});
				
				
				$('.blockUI.blockMsg').centerDialogue();
				
				

			},
			error: function(jqXHR, textStatus, errorThrown) {
				// errorThrown = the error message.
				//alert('error:'+errorThrown);
				alert('There was an error. Please try again.');
				
			}
		});

    });
	
	$('#submitEntryFormBtn').live('click', function() {
		
		var myform = this.form;
		var err = [];
		
		if (myform.contactname.value.trim().length == 0) {
			err[err.length] = 'Contact name required.';
		}
		
		if (myform.contactemail.value.trim().length == 0) {
			err[err.length] = 'Contact email required.';
		} else if (validateEmail(myform.contactemail.value.trim()) == false) {
			err[err.length] = 'Invalid email.';
		}
			
		if (myform.contactphone.value.trim().length == 0) {
			err[err.length] = 'Contact phone required.';
		}
		
		if (reasonrequired == true && myform.entryreason.value.trim().length == 0) {
			err[err.length] = 'Reason for winning required.';
		} else if (myform.entryreason.value.trim().length > charlimit) {
			err[err.length] = 'Reason for winning must be ' + charlimit + ' characters or less.';
		}
		
		if (err.length > 0) {
			alert(err.join(';').replace(/;/gi, '\n'));
		} else {


		$.ajax({
			url: '/submitEntryForm.cfm', 
			type: 'post',
			cache: false,
			dataType: 'json',
			data: {
				contactname: myform.contactname.value,
				contactemail: myform.contactemail.value,
				contactphone: myform.contactphone.value,
				entryname: myform.entryname.value,
				entryreason: myform.entryreason.value
			},
			complete: function(jqXHR, textStatus) {
				//alert('complete:'+textStatus);

			},
			success: function(data, textStatus, jqXHR) {
				success_data = data;
				success_textStatus = textStatus;
				success_jqXHR = jqXHR;
				
				
				if (data.error == false) {
				
				var myhtml = '<p><b>YOUR ENTRY WAS SUBMITTED!</b></p><p><a href="javascript:void(0)" id="closemodalwindow2" class="ezcart">Close Window</a></p>';
				
					$('#submitEntryFormDiv').fadeOut(300, function() {
					
						//var w = $('#submitEntryFormDiv').width() + 'px';
						//var h = $('#submitEntryFormDiv').height() + 'px';
						//$('#submitEntryFormDiv').css({
							//width: w,
							//height: h
						//});
					
						$('#submitEntryFormDiv').html(myhtml);
						$('#submitEntryFormDiv').fadeIn(300, function() {});
					});
					
				} else {
					showerror(data.message);
				}

			},
			error: function(jqXHR, textStatus, errorThrown) {
				// errorThrown = the error message.
				//alert('error:'+errorThrown);
				
				showerror(errorThrown);
				
			}
		});


		}
		
		return false;
	});
	
	$('#closemodalwindow').live('click', function() {
		$.unblockUI({
        	fadeOut: 500
        });
		
		return false;
	});
	
	$('#closemodalwindow2').live('click', function() {
		
		$('#closemodalwindow').click();
		
		return false;
	});
	
	$('#entryreason').live('keydown', function() {
		countAreaChars(this,document.getElementById('countchars'),charlimit);
	});
	$('#entryreason').live('keyup', function() {
		countAreaChars(this,document.getElementById('countchars'),charlimit);
	});
	

	$(window).resize(function() {
		$('.blockUI.blockMsg').centerDialogue();
	});


	
	var countAreaChars = function(areaName,counter,limit) {
	
		if (areaName.value.length>limit) {
			areaName.value=$(areaName).attr('value').substring(0,limit);
		}
		
		$(counter).html(limit - areaName.value.length);
	}
	
	var showerror = function(msg) {
		alert(msg);
	}


	$.fn.centerDialogue = function () {
		this.css("position","absolute");
		this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
		this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
		return this;
	}

});
    
