var passwordReg = /^(?![a-zA-Z]+$)(?![0-9]+$)[a-zA-Z0-9]{8,16}$/g;
var accessReg = /^[a-zA-Z0-9]{4,16}$/g;
var priceReg = /^[0-9]{1,6}(?:\.[0-9]{1,2})?$/g;
var emailReg = /^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/g;


/*
 * 帐号校验
 */

var guestAccessCheck = function(alertBoolean){
	
	var access = $("#access");
	var value = access.attr("value");
	var next = access.next();
	
 	if(!value.match(accessReg)){
		next.text("帐号格式不符合规则！")
		next.css({ 
			color: "red"
		});
		if(alertBoolean){alert(next.text());access.focus()};
		return false;
 	}
	
 	next.text("loading...")
 	
	guestDAO.alreadyIn(value, function(data) {
		if(data){
			next.text("该帐号已经存在！")
			next.css({ 
				color: "red"
			});
		}else{
			next.text("该帐号可用！")
			next.css({ 
				color: "green"
			});
		}
	});
	
 	return true;
}


/*
 * 密码校验
 */

var passwordCheck = function(alertBoolean){
	
	var password = $("#password");
	var value = password.attr("value");
	var next = password.next();
 	if(!value.match(passwordReg)){
 		next.text("密码格式不符合规则！")
		next.css({ 
			color: "red"
		});
 		if(alertBoolean){alert(next.text());password.focus()};
		return false;
 	}
 	
	next.text("正确")
	next.css({ 
		color: "green"
	});
	
	if(!alertBoolean)passwordAgainCheck(alertBoolean)
	
	return true;


}



/*
 * 密码二次校验
 */

var passwordAgainCheck = function(alertBoolean){
	
	var password = $("#password");
	var passwordagain = $("#passwordagain");
	var next = passwordagain.next();
	if(passwordagain.attr("value") != password.attr("value")){
		next.text("两次密码输入不一样！")
		next.css({ 
			color: "red"
		});
		if(alertBoolean){alert(next.text());passwordagain.focus()};
		return false;
	};
	next.text("正确")
	next.css({ 
		color: "green"
	});
	
	return true;
	
}



var requiredCheck = function(required){
	var next = required.next();
	if(required.attr("value") == ""){
		next.text("必须填写！");
		next.css({ 
			color: "red"
		});
		return;
	}
	next.text("正确")
	next.css({ 
		color: "green"
	});
}


var emailCheck = function(alertBoolean){
	
	
	var email = $("#email");
	var value = email.attr("value");
	var next = email.next();
	
 	if(!value.match(emailReg)){
		next.text("email格式不符合规则！")
		next.css({ 
			color: "red"
		});
		if(alertBoolean){alert(next.text());email.focus()};
		return false;
 	}
	
 	return true;
	
}

var mobileCheck = function(alertBoolean){
	

	
	var mobile = $("#mobile");
	var value = mobile.attr("value");
	var next = mobile.next();
	
	
 	if(!value.match(/^\d+$/g)){
		next.text("手机格式不符合规则！")
		next.css({ 
			color: "red"
		});
		if(alertBoolean){alert(next.text());mobile.focus()};
		return false;
 	}
	
 	return true;
	
}




var quantityCheck = function(alertBoolean){
	

	
	var quantity = $("#quantity");
	var value = quantity.attr("value");
	var next = quantity.next();
	
 	if(!value.match(/^[1-9]\d*$/g)){
		next.text("数量只允许整数！")
		next.css({ 
			color: "red"
		});
		if(alertBoolean){alert(next.text());quantity.focus()};
		return false;
 	}
	
 	return true;
	
}

// ===================================================================================================



/*
 * 前台登入校验
 */


var loginSubmit = function(){
	
	var form = $("#loginform");
		form.attr("action","login.action");
		form.attr("method","post");
		form.submit();
	
};


var loginReset = function(){
	$("form")[0].reset();  
};



/*
 * 客户管理-表单提交
 */

var required = function(hasError){
	
	$("input[class='required']").each(function(){
		if(!hasError){
			if($(this).attr("value") == ""){
				alert("带红色星号的项目必须填写");
				$(this).focus();
				hasError = true;
			}
		}
	})
	
	return hasError
}

var requiredOrder = function(hasError){
	
	$("input[class*='required order']").each(function(){
		if(!hasError){
			if($(this).attr("value") == ""){
				alert("带红色星号的项目必须填写");
				$(this).focus();
				hasError = true;
			}
		}
	})
	
	return hasError
}

var requiredHotelSearch = function(hasError){

	$("input[class*='required hotelsearch']").each(function(i){
		if(!hasError){
			if($(this).attr("value") == ""){
				alert("带红色星号的项目必须填写");
				$(this).focus();
				hasError = true;
			}
		}
	})
	
	return hasError
}

var guestFormSubmit = function(){

	var hasError = false;
	hasError = required(hasError)

	if(!hasError){
		if(!guestAccessCheck(true))hasError = true;
	}
	
	if(!hasError){
		if(!passwordCheck(true))hasError = true;
	}
	
	if(!hasError){
		if(!passwordAgainCheck(true))hasError = true;
	}
	
	if(!hasError){
		if(!emailCheck(true))hasError = true;
	}
	
	if(!hasError){
		if(!mobileCheck(true))hasError = true;
	}
	
	
	if(hasError){
		$("#submitButton").attr("disabled",false);
		return;
	}
	$("#guestForm").submit();
}



/*
 * 登录表单提交
 */

var loginFormSubmit = function(){

	var hasError = false;
	hasError = required(hasError)
	if(hasError){
		$("#submitButton").attr("disabled",false);
		return;
	}
	$("#guestForm").submit();
}


/*
 * 密码修改表单提交
 */

var passwordModifyFormSubmit = function(){
	
	var hasError = false;
	hasError = required(hasError)
	
	if(!hasError){
		if(!passwordCheck(true))hasError = true;
	}
	
	if(!hasError){
		if(!passwordAgainCheck(true))hasError = true;
	}
	
	if(hasError){
		$("#submitButton").attr("disabled",false);
		return;
	}
	$("#guestForm").submit();
	
	
}



/*
 * 基本信息修改
 */



var guestModifyFormSubmit = function(){
	
	
	var hasError = false;
	hasError = required(hasError)

	
	if(!hasError){
		if(!emailCheck(true))hasError = true;
	}
	
	if(!hasError){
		if(!mobileCheck(true))hasError = true;
	}
	
	
	if(hasError){
		$("#submitButton").attr("disabled",false);
		return;
	}
	$("#guestForm").submit();
	
	
	
}


var updatePassword = function(){
	
	
	var hasError = false;
	hasError = required(hasError)
	
	if(!hasError){
		if(!passwordCheck(true))hasError = true;
	}
	
	if(!hasError){
		if(!passwordAgainCheck(true))hasError = true;
	}
	
	if(hasError){
		$("#submitButton").attr("disabled",false);
		return;
	}
	$("#guestForm").submit();
	
	
}





























