/**
 * 页面加载完成后的处理
 */
$(document)
		.ready(
				function() {
					/*
					 * 外站跳转来的价格不能同本地匹配时的处理
					 * 同意：重新查询航班；不同意：跳转回主页
					 */
					if (document.getElementById("sourceSite").value == 'kuxun'
							&& document.getElementById("match").value == 'false') {
						if (confirm('您好，该价格已经售完，是否新选择！')) {
							window.location.href = "../airticket/DomesticAirticketSearchInterim.action?orgCode="
									+ document.getElementById("org").value
									+ "&destCode="
									+ document.getElementById("dest").value
									+ "&orgDate="
									+ document.getElementById("fltDate").value
									+ "&flightType=1&segIndex=1&sourceSite=kuxun";
						} else {
							window.location.href = "../main/InitHomepage.action";
						}
					} else if (document.getElementById("sourceSite").value == 'qunar'
							&& document.getElementById("match").value == 'false') {
						if (confirm('您好，该价格已经售完，是否新选择！')) {
							window.location.href = "../airticket/DomesticAirticketSearchInterim.action?orgCode="
									+ document.getElementById("org").value
									+ "&destCode="
									+ document.getElementById("dest").value
									+ "&orgDate="
									+ document.getElementById("fltDate").value
									+ "&flightType=1&segIndex=1&sourceSite=qunar";
						} else {
							window.location.href = "../main/InitHomepage.action";
						}
					} else if (document.getElementById("sourceSite").value == 'feeyo'
							&& document.getElementById("match").value == 'false') {
						if (confirm('您好，该价格已经售完，是否新选择！')) {
							window.location.href = "../airticket/DomesticAirticketSearchInterim.action?orgCode="
									+ document.getElementById("org").value
									+ "&destCode="
									+ document.getElementById("dest").value
									+ "&orgDate="
									+ document.getElementById("fltDate").value
									+ "&flightType=1&segIndex=1&sourceSite=feeyo";
						} else {
							window.location.href = "../main/InitHomepage.action";
						}
					}

					$('#userName').focus();
				});

/**
 * 登录
 */
function login() {
	$('#userName').css("border", "1px solid #7f9db9");
	$('#password').css("border", "1px solid #7f9db9");
	if ($('#userName').val() == null || $('#userName').val().trim() == '') {
		$('#userName').focus();
		$('#userName').css("border", "2px solid #FF0000");
		return;
	}
	if ($('#password').val() == null || $('#password').val().trim() == '') {
		$('#password').focus();
		$('#password').css("border", "2px solid #FF0000");
		return;
	}
	$('#login-content').ajaxloader('show', 'tata-ajax-loader-img');
	var params = {
		"userName" : $('#userName').val(),
		"password" : $('#password').val()
	}
	$.ajax( {
		type : "post",
		url : "Login.action",
		dataType : "json",
		data : params,
		timeout : 60000,
		error : processLoginReturn,
		global : false,
		success : processLoginReturn
	});
}

/**
 * 处理登录返回
 * @param data	返回的JSON数据
 * @return		无
 */
function processLoginReturn(data) {
	var actionVal = null;
	if ($("#directField").val() == null || $("#directField").val() == '')
		actionVal = "../main/InitHomepage.action";
	else
		actionVal = $("#directField").val();
	if (data.success == 'success') {
		$("#password").val('');
		$("#redirectForm").attr("action", actionVal);
		$('#redirectForm').submit();
	} else if (data.success == 'USERNAME') {
		$("#password").val('');
		alert('用户名不存在！');
		$('#login-content').ajaxloader('hide');
	} else if (data.success == 'PASSWORD') {
		$("#password").val('');
		alert('密码错误！');
		$('#login-content').ajaxloader('hide');
	} else {
		$("#password").val('');
		alert('密码不正确或用户不存在！');
		$('#login-content').ajaxloader('hide');
	}
}

/**
 * 用户注册
 */
function registUser() {
	window.location.href = "../user/InitRegisterPage.action";
}

/**
 * 直接预订
 * @return
 */
function orderFlightDirectly() {
	$("#redirectForm").attr("action", $("#directField").val());
	$('#redirectForm').submit();
}
