// JavaScript Document
var RecaptchaOptions = {
theme : 'clean'
};

function checkEmptyField(fieldToCheck, fieldName) {
	var strForgotPattern = 'อ๊ะๆ ใส่ [fieldname] ด้วยจ้ะ!!!';
	var fieldId = '#'+fieldToCheck.attr('id');

	if ($(fieldId).val() == '') {
		alert(strForgotPattern.replace('[fieldname]', fieldName));
		$('html, body').scrollTo( $(fieldId), 800 );
		fieldToCheck.focus();
		return false;
	} else {
		return true;
	}
}

function validateCaptcha() {
    challengeField 	= $("input#recaptcha_challenge_field").val();
    responseField 	= $("input#recaptcha_response_field").val();
    //alert(challengeField);
    //alert(responseField);
    //return false;
    var html = $.ajax({
    type: "POST",
    url: "ajax/ajax.recaptcha.php",
    data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField,
    async: false
    }).responseText;
 
    if(html == "success")
    {
        $("#captchaStatus").html(" ");
        // Uncomment the following line in your application
        return true;
    }
    else
    {
        $("#captchaStatus").html("Your captcha is incorrect. Please try again");
        Recaptcha.reload();
        return false;
    }
}


$(function () {
	$("#intro_birthdate").datepicker({ 
		dateFormat: "M d, yy",
		changeMonth: true,
		changeYear: true,
		yearRange: '-80:-13'
	});	
	
	$("#interest_99").click(function() {
		if ($("input[@name='interest_99']:checked").size() == 0) {
			$("#interest_others").attr("disabled","disabled");
		} else {
			$("#interest_others").removeAttr("disabled");
			$("#interest_others").focus();
		}
	});

	$("#knowus_99").click(function() {
		if ($("input[@name='knowus_99']:checked").size() == 0) {
			$("#knowus_others").attr("disabled","disabled");
		} else {
			$("#knowus_others").removeAttr("disabled");
			$("#knowus_others").focus();
		}
	});

	$("#shopping_place_99").click(function() {
		if ($("input[@name='shopping_place_99']:checked").size() == 0) {
			$("#shopping_place_others").attr("disabled","disabled");
		} else {
			$("#shopping_place_others").removeAttr("disabled");
			$("#shopping_place_others").focus();
		}
	});

	$("#submit").click(function () {
		var strForgotPattern = 'อ๊ะๆ ใส่ [fieldname] ด้วยจ้ะ!!!';

		if (checkEmptyField($('#username'), 'ชื่อผู้ใช้ (User Name)') == false) {
			return false;
		}
		if (checkEmptyField($('#user_email'), 'Email address') == false) {
			return false;
		}
		
		if ($("#cur_password").size() == 0) {
			if (checkEmptyField($("#new_password"), 'รหัสผ่าน') == false) {
				return false;
			}
			if (checkEmptyField($("#password_confirm"), 'ยืนยันรหัสผ่าน') == false) {
				return false;
			}
		} else {
			if ($("#cur_password").val() != '') {
				if (hex_md5($("#cur_password").val()) != $("#user_password_md5").val()) {
					alert('รหัสผ่านเดิมไม่ถุกต้องนะ');
					$('html, body').scrollTo($("#cur_password"), 800);
					$("#cur_password").focus();
					return false
				}
				if (checkEmptyField($("#new_password"), 'รหัสผ่าน') == false) {
					return false;
				}
			}
			if ($("#new_password").val() != '') {
				if (checkEmptyField($("#cur_password"), 'รหัสผ่านเดิม') == false) {
					return false;
				}
			}
		}
		if ($("#new_password").val() != $("#password_confirm").val()) {
			alert('รหัสผ่านทั้งสองช่องไม่ตรงกัน ตรวจสอบด้วยจ๊ะ');
			$('html, body').scrollTo( $("#password_confirm"), 800 );
			$("#password_confirm").focus();
			return false;
		}
		/*
		if ($("#confirm_code").size() > 0) {
			if (checkEmptyField($("#confirm_code"), 'รหัสยืนยันจากรูป') == false) {
				$('html, body').scrollTo( $("#captcha"), 800 );
				return false;
			}
			
			if (hex_md5($("#confirm_code").val()) != $("#confirm_md5").val()) {
				alert('รหัสยืนยันจากรูปไม่ถูกต้อง!');
				$('html, body').scrollTo( $("#captcha"), 800 );
				$("#confirm_code").focus();
				return false;
			}
		}
		*/
		
		if ($("#recaptcha_response_field").size() > 0) {
			if (checkEmptyField($("#recaptcha_response_field"), 'รหัสยืนยันจากรูป') == false) {
				$('html, body').scrollTo( $("#recaptcha_table"), 800 );
				return false;
			}
			
			if (validateCaptcha() == false) {
				alert('รหัสยืนยันจากรูปไม่ถูกต้อง!');
				$('html, body').scrollTo( $("#recaptcha_table"), 800 );
				return false;
			}
		}
		
		if (checkEmptyField($("#intro_birthdate"), 'วันเกิด') == false) {
			return false;
		}

		if (checkEmptyField($("#personal_firstname"), 'ชื่อ') == false) {
			return false;
		}
		if (checkEmptyField($("#personal_lastname"), 'นามสกุล') == false) {
			return false;
		}
		if (checkEmptyField($("#personal_address"), 'ที่อยู่') == false) {
			return false;
		}
		if (checkEmptyField($("#personal_zipcode"), 'รหัสไปรษณีย์') == false) {
			return false;
		}
		
		if ($("input[@name='skintype']:checked").size() == 0) {
			alert(strForgotPattern.replace('[fieldname]', 'สภาพผิวหน้า'));
			$('html, body').scrollTo( $('#skintype_dry'), 800 );
			return false;
		}

		if ($("input[@name='bodyskin']:checked").size() == 0) {
			alert(strForgotPattern.replace('[fieldname]', 'สภาพผิวกาย'));
			$('html, body').scrollTo( $('#bodyskin_dry'), 800 );
			return false;
		}

		if ($("input[@name='haircondition']:checked").size() == 0) {
			alert(strForgotPattern.replace('[fieldname]', 'สภาพผม'));
			$('html, body').scrollTo( $('#haircondition_normal'), 800 );
			return false;
		}

		if ($("input[@name='shopping_frequency']:checked").size() == 0) {
			alert(strForgotPattern.replace('[fieldname]', 'ความถี่ในการ Shopping ต่อเดือน'));
			$('html, body').scrollTo( $('#shopping_frequency_1'), 800 );
			return false;
		}
		
		if ($(".shopping_place:checked").size() == 0) {
			alert(strForgotPattern.replace('[fieldname]', 'สถานที่ Shopping สุดโปรด'));
			$('html, body').scrollTo( $('#shopping_place_01'), 800 );
			return false;
		}

		/*
		if ($("input[@name='shopping_place']:checked").size() == 0) {
			alert(strForgotPattern.replace('[fieldname]', 'สถานที่ Shopping สุดโปรด'));
			$('html, body').scrollTo( $('#shopping_place_01'), 800 );
			return false;
		}
		*/

		return true;
	});

});
