$().ready(function() 
{
	$("#phone_work").mask("(999) 999-9999");
	$(".input_date").mask("99/99/9999");
	
	
	$("input[type='text'],input[type='password'],input[type='checkbox'],select").keypress( 
		function (evt) 
		{
			return focus_next(evt);
		} 
	);
	
	/*jQuery.validator.messages.required = "<span class='error_message_sign'>*</span>";
	jQuery.validator.messages.email ="<span class='error_message_sign'>*</span>";*/
	jQuery.validator.messages.required = "Required field(s) cannot be left blank.";
	/*jQuery.validator.messages.email ="";
	jQuery.validator.messages.equalTo ="";
	jQuery.validator.messages.date ="";
	jQuery.validator.messages.minlength ="";*/
	$("#form_login").validate
	({
		rules: {
			login_username: "required",
			login_password: "required"
		},
		showErrors: function(errorMap, errorList){ 
			process_error(errorMap, errorList, "div_login_message" );
			this.defaultShowErrors();
		},
		submitHandler: function() {
			var options= {
				beforeSubmit: function(formData, jqForm, options){ jqForm.block({ message:"<h1><img src='/images/loading.gif'>Sending...</h1>"}); },
				dataType: 'json',
				success: function(data, status, form) {
					form.unblock();
					if( data.status == 0) {
						$('#div_login_message').html(data.message);
						//show_message(data.message, function(){});
					}else if( data.status == 1)	{
						
						window.location.href = data.url;
						
						//$.blockUI({ message: '<h1><img src="/images/success.jpg" />'+data.message+'</h1>' });
						//setTimeout( function(){window.location.href = data.url;}, 1000);
					}
				}
			};
			$('#form_login').ajaxSubmit(options);
		}
	});
	$("#form_signup").validate
	({	
		
		rules: {
			first_name: "required", last_name: "required",
			username: {required: true, minlength: 6},
			password: {required: true, minlength: 6},
			password2: {equalTo: "#password"},
			email: {required: true, email:true},
			phone_work: "required",
			security_question: "required",
			security_answer: "required",
			agree: "required" 
			//birthday: {required:true, date:true }
			
			
		},
		showErrors: function(errorMap, errorList){
			process_error(errorMap, errorList, "div_signup_message" );
			
			this.defaultShowErrors();
		},
		submitHandler: function() { 
			var message="<p style='text-align:left'>Please confirm your information:<br>Name: "+$("#first_name").val()+" "+$("#last_name").val()+"<br>";
			message+="Email: "+$("#email").val()+"<br>";
			message+="Phone: "+$("#phone_work").val()+"<br>";
			message+="</p>";
			show_confirm(message, function(){
					//alert("submitting form");
					//document.getElementById("form_signup").submit();
					var options= {
						beforeSubmit: function(formData, jqForm, options){ jqForm.block({ message:"<h1><img src='/images/loading.gif'>Sending...</h1>"}); },
						dataType: 'json',
						success: function(data, status, form) {
							form.unblock();
							if( data.status == 0) 
							{
								$('#div_signup_message').html(data.message);
								//show_message(data.message, function(){});
							}
							else if( data.status == 1)
							{
								
								show_message(data.message, function(){
									window.location.href = data.url;
								});
							}
						}
					};
					$('#form_signup').ajaxSubmit(options);
					return true;
				});
		}
	
	});
	
	//test button
	$("#button_test").click( function(e) {
		var id= parseInt( Math.random()*10000);
		$("#first_name").val("Test");
		$("#last_name").val("Test");
		$("#email").val("test"+id+"@mlsassistant.com");
		$("#email2").val("test"+id+"@mlsassistant.com");
		$("#phone_work").val("(999) 999-9999");
		$("#username").val("test"+id);
		$("#password").val("test123");
		$("#password2").val("test123");
		$("#security_question").val("What is your pet's name?");
		$("#security_answer").val("Test");
		//$("#birthday").val("12/12/1980");
	});
	
	
	//button_password
	$('#button_password').click(function() {
		$('#div_forget_password').slideToggle("normal");

		$('#div_signup').hide();$('#div_login').hide();
	});
	
	$("#form_password").validate
	({
		rules: {
			email_password:  {required: true, email:true}
		},
		showErrors: function(errorMap, errorList){
			process_error(errorMap, errorList, "div_password_message" );
			this.defaultShowErrors();
		},
		submitHandler: function() {
			var options= {
				beforeSubmit: function(formData, jqForm, options){ jqForm.block({ message:"<h1><img src='/images/loading.gif'>Sending...</h1>"}); },
				dataType: 'json',
				success: function(data) {
					$(this).unblock();
					
					if( data.status == 0) 
					{
						$('#div_password_message').html(data.message);
						show_message(data.message, function(){});
					}
					else if( data.status == 1)
					{
						show_message(data.message, function(){
							$('#div_password_message').html("");
							//$('#div_forget_password').slideToggle("normal");
						});
					}
				}
			};
			$('#form_password').ajaxSubmit(options);
		}
	});
	
	
});

function user_agreement()
{
	window.open('/index.php/tour/user_agreement','scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no');
	return false;
}
