var forcePassowrdRedirect=false;
( function($) {
	if (jQuery.browser.mozilla) {
		// do when DOM is ready
		$( function() {
			// search form, hide it, search labels to modify, filter classes nocmx and error
		});
	}
	;
	$( function() {
		//code
		$("#userAjaxLogin #errorMsg").css("display", "none");
		$("#userAjaxLogin #rememberMe").change($(this).userCheckUp);
		$("#sideBarReLoginBtn").click($(this).sideBarReLogin);

		$().ready( function() {
			// avoid submitting from form directly with incorrect parameters
			$("#userAjaxLogin").submit(function() {
				return false;
			});
			// validate the comment form when it is submitted
			// validate signup form on keyup and submit
			// var container = $("#errorCall");
			var validator = $("#userAjaxLogin").validate( {
				errorClass: "red",
				onfocusin: function(element) {
					this.lastActive = element;
					// this.settings.unhighlight &&
					// this.settings.unhighlight.call( this, element,
					// this.settings.errorClass );
				// this.errorsFor(element).hide();
			},
			onfocusout: function(element) {
			},
			onkeyup: function(element) {
			},
			onclick: function(element) {
				//this.settings.unhighlight && this.settings.unhighlight.call( this, element, this.settings.errorClass );
				// this.errorsFor(element).hide();
			},
			highlight: function(element, errorClass) {
				$("#userAjaxLogin").find("label[for=" + element.name + "]").css("color", "#F00");
				$("#userAjaxLogin").find("label[for=" + element.name + "]").css("display", "block");

				/* set bg yellow */
				$("#userAjaxLogin").find("label[for=" + element.id + "]").parent().css( {
					background: "#FEF7D0",
					height: "80px",
					margin: "5px 0px"
				});
			},
			unhighlight: function(element, errorClass) {
				$("#userAjaxLogin").find("label[for=" + element.name + "]").css("color", "#746C61");
				$("#userAjaxLogin").find("label[for=" + element.name + "]").css("display", "block");
				/* reset bg yellow */
				$("#userAjaxLogin").find("label[for=" + element.id + "]").parent().css( {
					background: "none",
					height: "auto",
					margin: "0px"
				});

			},
			check: function(element) {
				$("#userAjaxLogin").find("label[for=" + element.name + "]").css("display", "block");
			},
			rules: {
				//"username": "required",
				//"password": "required",
				"username": {
					required: true,
					minlength: 1,
					maxlength: 75
				},
				"password": {
					required: true
				}
			},
			messages: {
				"username": {
					required: "Please enter a valid e-mail address.",
					minlength: "Please enter a valid e-mail address.",
					maxlength: "Please enter a valid e-mail address.",
					email: "Please enter a valid e-mail address."
				},
				"password": {
					required: "Please fill in password"
				}

			}
			});

			$("#userAjaxLogin #forgetpassword").click( function() {
				$("#userAjaxLogin #message").html("");
				if (validator.form()) {
					//			    	  $.ajax({
					//		    		       type: "get",
					//		    			   url: ctx+"/member/checkIsEsamplingEnableUser.json?userName="+$("#userAjaxLogin #username").val(),
					//		    			   success: function(msg){
					//			    		        json = eval('('+msg+')');
					// if(json.isEsamplingEnable){
					// document.location.href =json.casLogoutService;
					// }else{
					// $(this).userLoginJsonp();
					// }
					// }
					// });
					$(this).userLoginJsonp();
				}
			});

			// for enter keyboard submit
			$("#userAjaxLogin :input").each( function() {
				$(this).keypress( function(e) {
					var key = window.event ? e.keyCode : e.which;
					if (key.toString() == "13") {
						$("#userAjaxLogin #forgetpassword").click();
					}
				});
			});
		});
	});

	$.fn.userCheckUp = function() {
		$(this).attr('value', $(this).attr('checked'));
	}
	$.fn.userLoginJsonp = function() {
		var url = $("#authCasServerLoginUrl").val();
		var qsData = {
			'isEsamplingLogin': $("#userAjaxLogin #isEsamplingLogin").val(),
			'username': $("#userAjaxLogin #username").val(),
			'password': $("#userAjaxLogin #password").val(),
			'rememberMe': $("#userAjaxLogin #rememberMe").val(),
			'appLogin': $("#userAjaxLogin #appLogin").val(),
			'service': $("#userAjaxLogin #loginService").val(),
			'authVia': "ajaxLogin"
		};
		$.ajax( {
			type: "get",
			url: url,
			data: qsData,
			dataType: "jsonp",
			jsonp: "jsonpcallback",
			beforeSend: function() {
				//alert("beforeSend");
		},
		success: function(msg) {
			if (msg.status == "success") {
				$.ajax( {
					type: "get",
					url: msg.url,
					success: function(msg) {
						if(forcePassowrdRedirect) {
							document.location.href = currentUrl;
						}
						else
							document.location.href = $("#userAjaxLogin #service").val();
					}
				});
			} else if (msg == 'errorLoginValid') {
				$("#userAjaxLogin #password").attr("value", "");
				$("#userAjaxLogin #message").html("The combination of e-mail address and password are invalid. Check that your e-mail address is in the proper format (name@domain.com) and that your password is entered correctly.");
			} else if (msg == 'errorThreeTime') {
				$("#userAjaxLogin #message").html("");
				$("#userAjaxLogin #loginCount").attr("value", "false");
				// set panel
			$("#ajaxLogin #loginCount").attr("value", "false");
			$("#userAjaxLogin #reg-login-sidebar").css("display", "none");
			$("#userAjaxLogin #reg-login-error").css("display", "block");
		} else if (msg == 'esamplingLoginValid') {
			// $("#userAjaxLogin #password").attr("value","");
			document.location.href = ctx + "/member/loginEsamplingFalid.page";
		}
	},
	complete: function(XMLHttpRequest, textStatus) {
		//alert("complete");
		},
		error: function(xhr, status) {
			if (xhr.status > 200 && xhr.status <= 500) {
				alert("error comminucation.");
			}
		}
		});
	}

	$.fn.sideBarReLogin = function() {
		var options = {
			success: function(result) {
				if (result.success == true) {
					$("#userAjaxLogin #reg-login-sidebar").css("display", "block");
					$("#userAjaxLogin #reg-login-error").css("display", "none");
					$("#userAjaxLogin #username").attr("value", "");
					$("#userAjaxLogin #password").attr("value", "");
					$("#userAjaxLogin #loginCount").attr("value", "0");
					$("#ajaxLogin #loginCount").attr("value", "0");
					return;
				}
			},
			error: function(xhr, status) {
				if (xhr.status > 200 && xhr.status <= 500) {
					alert("error comminucation.");
				}
			},
			complete: function(xhr, status) {
				//var height = 130+$("#formPanl3").height(); 
			// $(window.parent.document).find("#submitPage").height(height);
		},
		dataType: "json"
		};
		$("#sideBarReLoginForm").ajaxSubmit(options);
	}
})(jQuery);