var validate = {
	errcss : "errorcontrol",
	noterrcss : "",
	allerrmsg : '',
	valEmpty : function(txtValid,errMsg) {
 		if(jQuery.trim($('#'+txtValid).val()) == ''){
 			$('#'+txtValid).attr('class',this.errcss);
 			this.allerrmsg += errMsg;
 			return false;
 		}else{
 			$('#'+txtValid).attr('class',this.noterrcss);
 		}
	},
	valEmail:function(txtValid,errMsg) {
		var isnoterror = true;
		validRegExp = /\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\b/i;
    	if ($('#'+txtValid).val().search(validRegExp) == -1){isnoterror =  false;}
 		if (isnoterror){
 			$('#'+txtValid).attr('class',this.noterrcss);
			return true;
		}else{
 			$('#'+txtValid).attr('class',this.errcss);
 			this.allerrmsg += errMsg;
			return false;
		}					
	},
    valDate : function(ddlDate,ddlMonth,ddlYear,errMsg){
 		var isnoterr = false;
		var arrayLookup = { '01' : 31,'03' : 31, 
	                        '04' : 30,'05' : 31,
	                        '06' : 30,'07' : 31,
	                        '08' : 31,'09' : 30,
	                        '10' : 31,'11' : 30,'12' : 31};
	    var intDay = parseInt($('#'+ddlDate).val(),10); 
	    if(arrayLookup[$('#'+ddlMonth).val()] != null) {
	    	if(intDay <= arrayLookup[$('#'+ddlMonth).val()] && intDay != 0){isnoterr =  true;}
	    }
	    var intMonth = parseInt($('#'+ddlMonth).val(),10);
	    if (intMonth == 2) { 
	        var intYear = parseInt($('#'+ddlYear).val());
	        if (intDay > 0 && intDay < 29) {isnoterr =  true;}
	        else if (intDay == 29) {
	        	if ((intYear % 4 == 0) && (intYear % 100 != 0) || (intYear % 400 == 0)) {isnoterr = true;}   
	        }
		}
      	if (isnoterr){
			$('#'+ddlDate).attr('class',this.noterrcss);
			$('#'+ddlMonth).attr('class',this.noterrcss);
			$('#'+ddlYear).attr('class',this.noterrcss);
		}else{
			this.allerrmsg += errMsg;
			$('#'+ddlDate).attr('class',this.errcss);
			$('#'+ddlMonth).attr('class',this.errcss);
			$('#'+ddlYear).attr('class',this.errcss);
			return false;
		}
	},
	valChecked : function(txtValid,errMsg){
	 	if(this.arrChecked(txtValid).length > 0){
 			$("[name = '"+txtValid+"']").attr('class',this.noterrcss);
 		}else{
 			this.allerrmsg += errMsg;
 			$("[name = '"+txtValid+"']").attr('class',this.errcss);
 			return false;
 		}
	 },
	 arrChecked:function(strControl){
		var arrCheck=new Array();
		$(":checked[name = '"+strControl+"']").each(function(){arrCheck.push($(this).val());});return arrCheck;
	},
	valSelect:function(txtValid,errMsg) {
 		if(jQuery.trim($('#'+txtValid).val()) == '0'){
 			this.allerrmsg += errMsg;
 			$('#'+txtValid).attr('class',this.errcss);
 			return false;
 		}else{
 			$('#'+txtValid).attr('class',this.noterrcss);
 		}
	 }
};

function sendQuestionaire(){
	var isnoterror = true;
	validate.allerrmsg = '';
	if (validate.valEmpty('txtName','โปรดใส่ชื่อ.\n\r') == false){ isnoterror = false;}
	if (validate.valEmpty('txtLastname','โปรดใส่นามสกุล.\n\r') == false){ isnoterror = false;}
	if (validate.valEmail('txtEmail','อีเมล์ไม่ถูกต้อง.\n\r') == false){ isnoterror = false;}
	if (validate.valChecked('rdoSex','โปรดระบุเพศ.\n\r') == false){ isnoterror = false;}
	if (validate.valDate('ddlDate', 'ddlMonth', 'ddlYear', 'วันที่ไม่ถูกต้อง.\n\r') == false){ isnoterror = false;}
	if (validate.valChecked('aq1','โปรดตอบข้อ 1.\n\r') == false){ isnoterror = false;}
	if (validate.valChecked('aq2','โปรดตอบข้อ 2.\n\r') == false){ isnoterror = false;}
	if (validate.valChecked('aq3','โปรดตอบข้อ 3.\n\r') == false){ isnoterror = false;}
	if (validate.valChecked('aq4[]','โปรดตอบข้อ 4.\n\r') == false){ isnoterror = false;}
	if (validate.valChecked('aq5[]','โปรดตอบข้อ 5.\n\r') == false){ isnoterror = false;}
	if (validate.valChecked('aq6','โปรดตอบข้อ 6.\n\r') == false){ isnoterror = false;}
	if (validate.valChecked('aq7[]','โปรดตอบข้อ 7.\n\r') == false){ isnoterror = false;}
	if (validate.valChecked('aq8','โปรดตอบข้อ 8.\n\r') == false){ isnoterror = false;}
	if (validate.valChecked('chkAccept','โปรดยอมรับเงื่อนไขและกติกา.\n\r') == false){ isnoterror = false;}
	if(isnoterror){
		$('#frm').submit();
	}else{
		alert(validate.allerrmsg);
	}
}
 
$(document).ready(function(){
	$('#btnSubmit').click(function(){sendQuestionaire();});
});