$(document).ready(function(){
	
	$("#compFrm").submit(function(index, domEle) {
		
		var thisObj = this;
		var compid = $("input[name='compid']",thisObj).attr('value');
		
		$.ajax({
			type: "POST",
			url: "competitions/"+compid+"/form.process.php",
			data: "func=processComp&"+$('#compFrm').serialize(),
			cache: false,
			dataType: "json",
			beforeSend: function(result) {
				$('input[name=submit]',thisObj).buttonStatus('reset');
				$('input[name=submit]',thisObj).buttonStatus('busy');
			},
			success: function(data, textStatus) {
				if (!data.success) {
					var msg = '';
					for (var i in data.errors) {
						msg += ''+data.errors[i]+'\n';
					}
					$('#competition #response').html('<span class="error">'+msg+'</span>').show('fast');
					$('input[name=submit]',thisObj).buttonStatus('fail');
				} else {
					$('input[name=submit]',thisObj).buttonStatus('done');
					$(thisObj).hide().replaceWith('You have successfully enter this competition. Good luck!').fadeIn('fast');
					//setTimeout(function() { $('.submit_btn',thisObj).buttonStatus('reset'); }, 500);
				}
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				alert('Error: '+textStatus);
			}
		});
		
		return false;
	});
	
});
