$(document).ready(function() {
	$('#btntakeitnow').css({'cursor':'pointer'});
	 var options = {  
		beforeSubmit:  showQuestionnaireRequest,  // pre-submit callback 
		success:       showQuestionnaireResponse,  // post-submit callback 
		url: 'includes/qrequest.php',
		type: 'post',
		dataType: 'html'
	};
 
	// bind to the form's submit event 
	$('#questionnaireform').submit(function() { 
		$(this).ajaxSubmit(options); 
		return false; 
	});

});

function showQuestionnaireRequest(formData, jqForm, options) {  
	
	clearError();
	var q1 = $('input:radio[name=q1]:checked');if(!q1.val()){setError('1');return false;}
	var q2 = $('#q2');if(!q2.val()){setError('2');return false;}
	var q3 = $('input:radio[name=q3]:checked');if(!q3.val()){setError('3');return false;}
	var q4 = $('input:radio[name=q4]:checked');if(!q4.val()){setError('4');return false;}
	var q5 = $('input:radio[name=q5]:checked');if(!q5.val()){setError('5');return false;}
	var q6 = $('input:radio[name=q6]:checked');if(!q6.val()){setError('6');return false;}
	var q7 = $('input:radio[name=q7]:checked');if(!q7.val()){setError('7');return false;}
	var q8 = $('input:radio[name=q8q1]:checked');if(!q8.val()){setError('8');return false;}
	var q8 = $('input:radio[name=q8q2]:checked');if(!q8.val()){setError('8');return false;}
	var q8 = $('input:radio[name=q8q3]:checked');if(!q8.val()){setError('8');return false;}
	var q8 = $('input:radio[name=q8q4]:checked');if(!q8.val()){setError('8');return false;}
	var q8 = $('input:radio[name=q8q5]:checked');if(!q8.val()){setError('8');return false;}
	var q9 = $('input:radio[name=q9]:checked');if(!q9.val()){setError('9');return false;}
	var q10 = $('input:radio[name=q10]:checked');if(!q10.val()){setError('q10');return false;}
	var q11 = $('#q11');if(!q11.val()){setError('11');return false;}
	var q12 = $('input:radio[name=q12]:checked');if(!q12.val()){setError('q12');return false;}
	var q13 = $('input:radio[name=q13]:checked');if(!q13.val()){setError('q13');return false;}
	var q14 = $('#q14');if(!q14.val()){setError('14');return false;}
	
	$.blockUI({ 
		message: '<span id="processing"><img border="0" src="images/loading.gif" align="absmiddle" /> Processing Questionnaire Form...</span>', 
		css: { 
			border: 'none',
			padding: '15px',
			backgroundColor: '#ffffff',
			'-webkit-border-radius': '10px', 
			'-moz-border-radius': '10px', 
			opacity: .8, 
			color: '#000000',
			width: '20%',
			top:   '40%',
			left:  '35%',
			textAlign:'center',
			cursor: 'wait',
			border:'3px solid #aaa'
		} 
	});
	
    return true; 
}

function showQuestionnaireResponse(responseText, statusText)  { 
	if(responseText == 'success'){
		$.blockUI({ 
			message: '<span id="thanks">Thank you for taking the survey</span>', 
			css: { 
				border: 'none',
				padding: '15px',
				backgroundColor: '#ffffff',
				'-webkit-border-radius': '10px', 
				'-moz-border-radius': '10px', 
				opacity: .8, 
				color: '#000000',
				width: '20%',
				top:   '40%',
				left:  '35%',
				textAlign:'center',
				cursor: 'wait',
				border:'3px solid #aaa'
			} 
		});
		setTimeout($.unblockUI, 2000);
	}else{
		$.blockUI({
			message: '<span id="thanks">Error occured, please try again later...</span>', 
			css: { 
				border: 'none',
				padding: '15px',
				backgroundColor: '#ffffff',
				'-webkit-border-radius': '10px', 
				'-moz-border-radius': '10px', 
				opacity: .8, 
				color: '#000000',
				width: '20%',
				top:   '40%',
				left:  '35%',
				textAlign:'center',
				cursor: 'wait',
				border:'3px solid #aaa'
			} 
		});
		setTimeout($.unblockUI, 2000);
	} 
} 

function setError(num){
	$("#diverror").show();
	$("#diverror").html("Please make sure that you have answered question #"+num);
	$.scrollTo( '#diverror' );
}

function clearError(obj){
	$("#diverror").hide();
}
