/*
 * 前台核心javaScript文件
 * 作者：沈道林
 * 最后修改时间：2011-04-04
 */



var pageFactory = function(page){

	$("#searchForm").attr("action","?")
	$("#currentPage").val(page);
	$("#searchForm").submit();
}



/*
 *  添加所属城市 1
 */

var CityFieldOnClick = function(){
	
	$("#dialog-city").dialog({
		resizable: false,
		closeOnEscape: true,
		height:340,
		width: 500,
		modal: true,
		buttons: {
			"开始授权": function() {
			CityFieldOnClickDone($(this));
			},
			"取消授权": function() {
			$(this).dialog('close');
			}
		}
	});
	
}


/*
 *  添加所属城市 2
 */

var CityFieldOnClickDone = function(d){
	 
	 $("input[type='radio'][id^='city']").each(function(i){
		 if($(this).attr("checked")){
			 $("#cityId").attr("value", $(this).attr("value"));
			 $("#cityName").attr("value", $.trim($(this).parent().text()));
			 $("#cityName").css("color","#000000");
			 $("#cityName").next().text("正确")
			 $("#cityName").next().css({ 
				 color: "green"
			 });
			 $("#cityName").next().next().attr("checked",false);
		 }
	 });
	 
	 d.dialog('close');
}



/*
 * 预订
 * 
 */

var order = function(id){
	
	login.isLogin(function(data) {
		if(data){
			orderDilog(id);
		}else{
			$("#Kaptcha").attr("src","Kaptcha.jpg");
			loginDilog();
		}
	});
	
}


/*
 * 预订登录窗口 1
 * 
 */


var loginDilog = function(){
	
	$("#dialog-login").dialog({
		resizable: false,
		closeOnEscape: true,
		height:300,
		width: 400,
		modal: true,
		buttons: {
			"登录": function() {
			dologin();
			},
			"取消": function() {
			$(this).dialog('close');
			},
			"没有帐号,赶快注册": function() {
				window.location.href = "reg.action";
			}
		}
	});
	
}

/*
 *  预订登录窗口 2
 * 
 */

var dologin = function(){

	var hasError = false;
	hasError = required(hasError)
	
	if(hasError){
		return;
	}
	
	var access = $("#access").attr("value")
	var password = $("#password").attr("value")
	var vcode = $("#vcode").attr("value")
	
	login.login(access,password,vcode,function(data) {

		if(data != "success"){
			alert("登入失败,请检查帐号密码是否正确。");
		}else{
			window.location.reload();
		}
		
		
	});

	
}

/*
 * 预订窗口窗口  1
 * 
 */

var orderDilog = function(id){
	
	$("#goodsName").text($("#goods" + id).text())
	$("#hsid").attr("value",id);
	$("#dialog-order").dialog({
		resizable: false,
		closeOnEscape: true,
		height:320,
		width: 400,
		modal: true,
		buttons: {
			"预订": function() {
			doOrder();
			},
			"取消": function() {
			$(this).dialog('close');
			}
		}
	});
	
}



/*
 *   预订窗口窗口  2
 * 
 */


var doOrder = function(){
	
	var hasError = false;
	hasError = requiredOrder(hasError)
	
	if(!hasError){
		if(!quantityCheck(true))hasError = true;
	}

	if(hasError){
		return;
	}
	$("#goodsOrder").submit()
	
}

/*
 * 
 * 取消预订
 */


var exitOrder = function(id,order){
	
	var info = confirm("确定要取消预订吗?");
	
	if(info){
		
		orderManage.Delete(id,function(data) {

			if(!data){
				alert("退订失败！");
			}else{
				$("#" + order + id).remove();
				alert("退订成功！");
			}
			
		});
	}
	
	
}



/*
 * 修改景点预订
 */



var modifyOrder = function(index,type){

	
	var orderArray = type?hotelOrderArray:scenicOrderArray;
	$("#id").attr("value",orderArray[index].id)
	$("#orderSerial").text(orderArray[index].orderSerial)
	$("#goodsName").text(orderArray[index].goodsName)
	$("#goodsType").text(orderArray[index].goodsType)
	$("#planDate1").text(orderArray[index].planDate1)
	
	var days = 1
	
	if(type){
	$("#planOverDate").show();
	$("#planDate2").attr("value",orderArray[index].planDate2)
	var planDate1Temp = (orderArray[index].planDate1).split("-");
	var planDate2Temp = (orderArray[index].planDate2).split("-");
	var planDate1 = new Date(planDate1Temp[0],planDate1Temp[1]-1,planDate1Temp[2]);
	var planDate2 = new Date(planDate2Temp[0],planDate2Temp[1]-1,planDate2Temp[2]);
	days =(planDate2.getTime() - planDate1.getTime())/(24*60*60*1000)
	}else{
	$("#planOverDate").hide();
	$("#planDate2").attr("value","")
	}

	$("#price").text("￥：" + (orderArray[index].offprice *  orderArray[index].quantity * days) + "元")
	$("#offprice").text("￥：" + orderArray[index].offprice + "元")
	$("#quantity").attr("value",orderArray[index].quantity)
	$("#name").attr("value",orderArray[index].name)
	$("#sid").attr("value",orderArray[index].sid)
	$("#phone").attr("value",orderArray[index].phone)
	$("#bonus1").text("￥：" + (orderArray[index].bonus1 *  orderArray[index].quantity * days).toFixed("2") + "元")
	$("#dialog-order").attr("title",orderArray[index].goodsName+"预订")

	$("#dialog-order").dialog({
		resizable: false,
		closeOnEscape: true,
		height:400,
		width: 400,
		modal: true,
		buttons: {
			"修改": function() {
			modifyOrderDone(index,type,$(this));
			},
			"取消": function() {
			$(this).dialog('close');
			}
		}
	});
}


var modifyOrderDone = function(index,type,d){
	
	var hasError = false;
	hasError = required(hasError)
	
	if(!hasError){
		if(!quantityCheck(true))hasError = true;
	}
	
	if(hasError){
		return;
	}
	
	
	var orderArray = type?hotelOrderArray:scenicOrderArray;
	
	var params = new Array();
	params.push($("#quantity").attr("value"));
	params.push($("#name").attr("value"));
	params.push($("#sid").attr("value"));
	params.push($("#phone").attr("value"));
	params.push(orderArray[index].id);
	params.push(orderArray[index].guest)
	if(type){
		var planDate1Temp = (orderArray[index].planDate1).split("-");
		var planDate2Temp = ($("#planDate2").attr("value")).split("-");
		params.push(new Date(planDate2Temp[0],planDate2Temp[1]-1,planDate2Temp[2]).getTime());
	}
	orderManage.Modify(params, function(data) {
		
		if(!data){
			alert("修改失败！");
		}else{
			
			orderArray[index].quantity = $("#quantity").attr("value")
			orderArray[index].name = $("#name").attr("value")
			orderArray[index].sid = $("#sid").attr("value")
			orderArray[index].phone = $("#phone").attr("value")
			
			
			
			if(type){
				orderArray[index].planDate2 = $("#planDate2").attr("value")
				var planDate1 = new Date(planDate1Temp[0],planDate1Temp[1]-1,planDate1Temp[2]);
				var planDate2 = new Date(planDate2Temp[0],planDate2Temp[1]-1,planDate2Temp[2]);
				var days =(planDate2.getTime() - planDate1.getTime())/(24*60*60*1000)
				$("#hotelQuantity" + index).html(orderArray[index].quantity +"&times;"+days+"天");
				$("#hotelPrice" + index).text("￥" + (orderArray[index].offprice *  orderArray[index].quantity * days));
				$("#hotelBonus1" + index).html("<font color='red'>￥" + (orderArray[index].bonus1 *  orderArray[index].quantity * days) + "</font>");
				
			}else{
				$("#scenicQuantity" + index).text(orderArray[index].quantity);
				$("#scenicPrice" + index).text("￥" + (orderArray[index].offprice *  orderArray[index].quantity));
				$("#scenicBonus1" + index).html("<font color='red'>￥" + (orderArray[index].bonus1 *  orderArray[index].quantity) + "</font>");
			}
				
			d.dialog('close');
			alert("修改成功！");
			
		}
		
	});
	

	
}




var hotelsearchSubmit = function(){
	var hasError = false;
	hasError = requiredHotelSearch(hasError)
	if(hasError){
		return;
	}
	$("#hotelsearch").submit()
}


var cashApplication = function(){

	var cashForm = $("#cashForm");
	cashForm.attr("action","cashApplicationAdd");
	cashForm.submit();
	
}



var GroupFieldOnClick = function(){
	 
	$("#dialog-group").dialog({
		resizable: false,
		closeOnEscape: true,
		height:340,
		width: 500,
		modal: true,
		buttons: {
			"开始授权": function() {
			GroupFieldOnClickDone($(this));
			},
			"取消授权": function() {
			$("#groupId").attr("value", "");
			$("#groupName").attr("value", "");
			$(this).dialog('close');
			}
		}
	});
 
}
 
var GroupFieldOnClickDone = function(d){
	
	 $("input[type='radio'][id^='group']").each(function(i){
		 if($(this).attr("checked")){
			 $("#groupId").attr("value", $(this).attr("value"));
			 $("#groupName").attr("value", $.trim($(this).parent().text()));
			 $("#groupName").css("color","#000000");
		 }
	 });
	 
	 d.dialog('close');
	
}



var showReviewsContent = function(a){

	
	$("#toGood").html($(a).parent().prev().html())
	
	$("#dialog-reviews").dialog({
		resizable: false,
		closeOnEscape: true,
		height:300,
		width: 400,
		modal: true,
		buttons: {
			"关闭": function() {
			$(this).dialog('close');
			}
		}
	});
 
}


var dutiesApply = function(mth,apply){
	
	$("#apply").attr("value",apply)
	$("#mth").attr("value",mth)
	
	$("#dialog-apply").dialog({
		resizable: false,
		closeOnEscape: true,
		height:180,
		width: 300,
		modal: true,
		buttons: {
			"开始申请": function() {
			$("#dutiesApplyForm").submit()
			$(this).dialog('close');
			},
			"取消申请": function() {
			$(this).dialog('close');
			}
		}
	});
 
}

