$(document).ready(function() {
	$.validator.addMethod("check_phone", function(value, element) { 
		return this.optional(element) || /^\+([0-9]{1,3})([0-9]{8,10})([\s][Ext.]{4}([0-9]{1,6})){0,1}$/i.test(value);
	}, "Phone: +6612345678 Ext.999");
	
	$.validator.addMethod("check_fax", function(value, element) { 
		return this.optional(element) || /^\+([0-9]{1,3})([0-9]{8,10})$/i.test(value);
	}, "Fax: +6612345678");

	$.validator.addMethod("password",function(value, element) {
		return this.optional(element) || /^[A-Za-z0-9~!?@#$%^&*+-=:.;_\(\)\[\]]{4,16}$/i.test(value);
	}, "(, ), [, ], ~, !, @, #, $, %, ^, &, *, _, + , -, =, :, ., ;, ? can be used");

	// validate login form
	$("#form_signup").validate({
		rules: {
			firstname: { required: true, minlength: 2, maxlength: 128 },
			lastname: { required: true, minlength: 2, maxlength: 128 },
			organizename: { required: true, minlength: 2, maxlength: 256 },
			department: { minlength: 2, maxlength: 256 },
			address: { required: true, minlength: 4, maxlength: 256 },
			//amphur: { required: true, minlength: 2, maxlength: 128 },
			//province: { required: true },
			//zip_code: { required: true, maxlength: 5 },
			country: { required: true },
			phone: { required: true, check_phone: "check_phone", minlength: 10, maxlength: 32 },
			fax: { check_fax: "check_fax", minlength: 10, maxlength: 24 },
			email: { required: true, email: true, maxlength: 80 },
			password: { required: true, minlength: 4, maxlength: 16, password: "password" },
			password2: { required: true, minlength: 4, maxlength: 16, password: "password", equalTo: "#password" }
		}
	});
	$("#zipcode").setMask({mask:"99999",selectCharsOnFocus: true,
		onValid:function(c, nKey){ $(this).css("background", "white"); },
		onInvalid:function(c, nKey){	$(this).css("background", "red"); },
		onOverflow: function(c, nKey){ $(this).css("background", "yellow"); }
	});
});
