$(function () {
	
	// form validate
	$('#regFrm').validate({
		prompt: true,
		rules: {
			username: {
				fn: function (key, validator){
					value = validator[key].value;
					if (value == ''){
						return validator.show('error', validator[key]);
					}
					if (!/^[a-z]\w{3,14}$/i.test(value)) {
						return validator.show('error', validator[key]);
					}
					
					$.post('/service/index.php', {'m' : 'member', 'a' : 'username', 'username' : encodeURIComponent(value)},function (data) {
						return validator.show(data, validator[key]);
					});
				},
				messages:{
					prompt: '4-15 characters, use letters and numbers, 0-9, a-z only  (do not start with numbers).',
					error: 'please enter 4-15 characters, use letters and numbers, 0-9, a-z only  (do not start with numbers).',
					exist: 'The login name has been used.'
				}
			},
			password: {
				fn: function (key, validator){
					value = validator[key].value;
					if (value == ''){
						return validator.show('error', validator[key]);
					}
					if (!/^\w{6,20}$/i.test(value)) {
						return validator.show('error', validator[key]);
					}
					return validator.show('success', validator[key]);
				},
				messages:{
					prompt: '6-20 characters',
					error: '6-20 characters'
				}
			},
			password1: {
				fn: function (key, validator){
					value = validator[key].value;
					if (value == ''){
						return validator.show('error', validator[key]);
					}
					if (!/^\w{6,20}$/i.test(value)) {
						return validator.show('error', validator[key]);
					}
					if(value != validator.password.value){
						return validator.show('error1', validator[key]);
					}
					return validator.show('success', validator[key]);
				},
				messages:{
					prompt: 'please enter your password again',
					error: '6-20 characters',
					error1: 'Please enter the same password'
				}
			},
			firstname: {
				fn: function (key, validator){
					value = validator[key].value;
					if (value == ''){
						return validator.show('error', validator[key]);
					}
					return validator.show('success', validator[key]);
				},
				messages:{
					prompt: 'please enter the contact person’s first name',
					error: 'please enter the contact person’s first name'
				}
			},
			lastname: {
				fn: function (key, validator){
					value = validator[key].value;
					if (value == ''){
						return validator.show('error', validator[key]);
					}
					return validator.show('success', validator[key]);
				},
				messages:{
					prompt: 'please enter the contact person’s last namen',
					error: 'please enter the contact person’s last name'
				}
			},
			job: {
				fn: function (key, validator){
					value = validator[key].value;
					if (value == ''){
						return;
					}
					return validator.show('success', validator[key]);
				},
				messages:{
					prompt: '',
					error: 'please enter the position of the contact person'
				}
			},
			email: {
				fn: function (key, validator){
					value = validator[key].value;
					if (value == ''){
						return validator.show('error', validator[key]);
					}
					
					$.post('/service/index.php', {'m' : 'member', 'a' : 'email', 'email' : encodeURIComponent(value)},function (data) {
						return validator.show(data, validator[key]);
					});
				},
				messages:{
					prompt: 'please enter the email of the contact person',
					error: 'Please enter a valid email address',
					exist: 'The email has been used.'
				}
			},
			phone: {
				fn: function (key, validator){
					value = validator[key].value;
					if (value == ''){
						return validator.show('error', validator[key]);
					}
					return validator.show('success', validator[key]);
				},
				messages:{
					prompt: '',
					error: 'Please enter numbers, dash or space only'
				}
			},
			mobile: {
				fn: function (key, validator){
					value = validator[key].value;
					if (value == ''){
						return;
					}
					return validator.show('success', validator[key]);
				},
				messages:{
					prompt: '',
					error: 'Please enter numbers, dash or space only'
				}
			},
			corpname: {
				fn: function (key, validator){
					value = validator[key].value;
					if (value == ''){
						return validator.show('error', validator[key]);
					}
					return validator.show('success', validator[key]);
				},
				messages:{
					prompt: 'Please enter your company name',
					error: 'Please enter your company name'
				}
			},
			'corpmodel[]': {
				fn: function (key, validator){
					value = validator[key].value;
					if (value == ''){
						return validator.show('error', validator[key]);
					}
					return validator.show('success', validator[key]);
				},
				messages:{
					prompt: '',
					error: 'Please enter your Company type'
				}
			},
			countrycode: {
				fn: function (key, validator){
					value = validator[key].value;
					if (value == ''){
						return validator.show('error', validator[key]);
					}
					return validator.show('success', validator[key]);
				},
				messages:{
					prompt: '',
					error: 'Please enter your Company Location'
				}
			},
			countrycode: {
				fn: function (key, validator){
					value = validator[key].value;
					if (value == ''){
						return validator.show('error', validator[key]);
					}
					return validator.show('success', validator[key]);
				},
				messages:{
					prompt: '',
					error: 'Please enter your Company Location'
				}
			},
			addr: {
				fn: function (key, validator){
					value = validator[key].value;
					if (value == ''){
						return validator.show('error', validator[key]);
					}
					return validator.show('success', validator[key]);
				},
				messages:{
					prompt: '',
					error: 'Please enter your Company Address'
				}
			},
			corpproductname: {
				fn: function (key, validator){
					value = validator[key].value;
					if (value == ''){
						return validator.show('error', validator[key]);
					}
					return validator.show('success', validator[key]);
				},
				messages:{
					prompt: 'Please select your Company product',
					error: 'Please select your Company product'
				}
			},
			validatecode: {
				fn: function (key, validator){
					value = validator[key].value;
					if (value == ''){
						return validator.show('error', validator[key]);
					}
					$.post('/service/index.php', {'m' : 'validateCode', 'a' : 'validate', 'code' : encodeURIComponent(value)},function (data) {
						return validator.show(data, validator[key]);
					});
				},
				messages:{
					prompt: 'please enter the characters you see in the image',
					error: 'please enter the characters you see in the image',
					illegal: 'enter error!',
					expiration: 'please Load new image'
				}
			
			}
		},
		fn: function () {
			$('input.phone[type=text]').blur();
			$('input.fax[type=text]').blur();
			$('td.location select').blur();
			$('td.location input[name=city]').blur();
		}
	});
	
	$.each(['username', 'password', 'firstname', 'lastname', 'job', 'email', 'phone', 'mobile', 'corpname', 'addr'], function() {
		$('#' + this).css('imeMode', 'disabled');
	});
	
	// Business Location
	var countrycode = $('#countrycode');
	$.each(country, function (key, value){
		countrycode.attr('options').add(new Option(value, key)); 
	});
	countrycode.val('156');
	
	// validate
	$('#validate').click(function () {
		$(this).attr('src', '/service/index.php?m=validateCode&update=' + Math.random());
	});
});
