var LinePrice,LineChildrenPrice,LineSpecialPrice,LineStartDate,StartDateType=0,orderDate,LineBonus,LineChildrenBonus,WebPath;
	
//价格,儿童价格,特殊价格,发团时间,特殊价格类型
function isTime(str)
{
	var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/); 
	if(r==null)return false; 
	var d= new Date(r[1], r[3]-1, r[4]); 
	return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]);
}
$(function(){
	if('天天发团'!=LineStartDate)
	{
		var Temp=LineStartDate+','; 
		if(!Temp.match(new RegExp('^(((0?[1-9])|(1[12]))-((0?[1-9])|([0-2][0-9])|(3[0-1])),)+$','i'))==false)
		{
			StartDateType = 1;//按日期发团
		}else if(!Temp.match(new RegExp('^([一二三四五六日],)+$','i'))==false){
			StartDateType = 2;//按星期发团
		}else if(!Temp.match(new RegExp('^(((0?[1-9])|([0-2][0-9])|(3[01])),)+$','i'))==false){
			StartDateType = 3//按日发团
		}else{
			alert('发团时间格式错误！');return false;
		}
	}
	var orderDate=$('#o_date').val();
	var D=new Date();
	D=new Date(Date.parse(D.getFullYear()+'/'+(D.getMonth()+1)+'/'+D.getDate())+86400000);
	if(!isTime(orderDate))
	{
		orderDate=D.getFullYear()+'-'+(D.getMonth()+1)+'-'+D.getDate();
	}else if(Date.parse(D)>Date.parse(new Date(orderDate.replace(/-/g,'/')))){
		orderDate=D.getFullYear()+'-'+(D.getMonth()+1)+'-'+D.getDate();
	}
	D=new Date(orderDate.replace(/-/g,'/'));
	CreateCalendar(D.getFullYear(),D.getMonth()+1);
	var Year,Month,Day,MatchValue;
	var TempStartDate;
	if(StartDateType==1)
	{
		TempStartDate = LineStartDate.split(',');
		do{
			var i;
			Year = D.getFullYear();Month = D.getMonth()+1;Day = D.getDate();
			for(i=0;i<TempStartDate.length;i++)
			{
				D = new Date(Date.parse(Year + '/' + TempStartDate[i].replace('-','/')));
				if(Year==D.getFullYear()&&Month==(D.getMonth()+1)&&Day==D.getDate()){AddSelect(Year,Month,Day);break;}
			}
			if(i==TempStartDate.length)break;
			D = new Date(Date.parse((Year+1)+'/1/1'));
		}while(true);
	}else if(StartDateType==2){
		var Week = ['日','一','二','三','四','五','六'];  
		do
		{
			Year = D.getFullYear();Month = D.getMonth()+1;Day = D.getDate();
			if(LineStartDate.indexOf(Week[D.getDay()])!=-1){AddSelect(Year,Month,Day);break;}
			D = new Date(Date.parse(Year + '/' + Month + '/' + (Day+1)));
		}while(true);
	}else if(StartDateType==3){
		TempStartDate = ','+LineStartDate+',';
		TempStartDate = TempStartDate.replace(/,0/g,',');
		do{
			Year = D.getFullYear();Month = D.getMonth()+1;Day = D.getDate();
			if(TempStartDate.indexOf(','+Day+',')!=-1){AddSelect(Year,Month,Day);break;}
			D = new Date(Date.parse(Year + '/' + Month + '/' + (Day+1)));
		}while(true);
	}else{
		AddSelect(D.getFullYear(),D.getMonth()+1,D.getDate());
	}
	$("#TourExForm").validate({
		rules:{
			o_contact:{required:true,IsRealName:true},
			Contact: {required: true,IsRealName:true},
			o_mobile: {required: true,isMobile:true},
			o_email: {email:true},
			o_text:{maxlength:200},
			Proof:{required: true,remote:{url:WebPath+'Sys/Ajax/Check.aspx',type:'get',dataType:'json',data:{'checktext':function(){return $('#Proof').val();}},cache:false}}
		},
		messages: {
			Proof:{required:'请填写验证码',remote:function(){get_Code(WebPath);return '验证码错误!'}}
		}
	}); 
	FeesCount();
	function AddSelect(Year,Month,Day)
	{
		orderDate=D.getFullYear()+'-'+(D.getMonth()+1)+'-'+D.getDate();
		$('#o_date').val(Year+'-'+Month+'-'+Day);
		var MatchValue = LineSpecialPrice.match(new RegExp('(\|)(0?)'+Month+'-(0?)'+Day+'\:([0-9]+)\:([0-9]+)(\|)','i'));
		if(MatchValue!=null)
		{
			$('#o_date').attr('Price',MatchValue[4]).attr('ChildrenPrice',MatchValue[5]);
		}else{
			$('#o_date').attr('Price',LinePrice).attr('ChildrenPrice',LineChildrenPrice);
		}
	}
});
function CreateCalendar(Y,M)
{
	var td,Stop,S,D;
	td = $('tr:gt(1)','#Month_1').find('td');
	$(td).unbind().html('&nbsp;<br />&nbsp;').css('color','#CECFCE').attr('name','').attr('Price','').attr('ChildrenPrice','');
	S = (new Date(Y+'/'+M+'/1')).getDay()-1;
	Stop = (new Date(Y+'/'+(M+1)+'/0')).getDate();
	for(var i=1;i<=Stop;i++)
	{
		$(td[i+S]).html(i + '<br />');
		$(td[i+S]).attr('name',Y+'-'+M+'-'+i);
	}
	$('#Month_YM_1').html(Y+'年'+M+'月');
	D = new Date(Y+'/'+(M-1)+'/1');
	$('#Month_Move_1').html('< '+(D.getMonth()+1)+'月').attr('Y',D.getFullYear()).attr('M',D.getMonth()+1).unbind().bind("click",function(){
		CreateCalendar(parseInt($(this).attr('Y')),parseInt($(this).attr('M')));
	});
	td = $('tr:gt(1)','#Month_2').find('td');
	$(td).unbind().html('&nbsp;<br />&nbsp;').css('color','#CECFCE').attr('name','').attr('Price','').attr('ChildrenPrice','');
	D = new Date(Y+'/'+(M+1)+'/1');
	Y = D.getFullYear();
	M = D.getMonth()+1;
	S = D.getDay()-1;
	Stop = (new Date(Y+'/'+(M+1)+'/0')).getDate();
	for(var i=1;i<=Stop;i++){$(td[i+S]).html(i + '<br />').attr('name',Y+'-'+M+'-'+i);}
	$('#Month_YM_2').html(Y+'年'+M+'月');
	$('#Month_Move_2').html((M==12?1:M+1)+'月 >').attr('Y',Y).attr('M',M).unbind().bind("click",function(){
		CreateCalendar(parseInt($(this).attr('Y')),parseInt($(this).attr('M')));
	});
	D = new Date();
	$('td[name="'+D.getFullYear()+'-'+(D.getMonth()+1)+'-'+D.getDate()+'"]').css('color','').append('今天');
	FillPrice(Y,M);
	D = new Date(Y+'/'+(M-1)+'/1');
	FillPrice(D.getFullYear(),D.getMonth()+1);
}

function FillPrice(Y,M)
{
	var D = new Date(Y+'/'+(M+1)+'/0');
	var DayLength = D.getDate();
	var Week = ['日','一','二','三','四','五','六'];  
	var Year,Month,Day,MatchValue,TempStartDate,Td,IsStartDate,Price_1,ChildrenPrice_1;
	D = new Date();
	Year = D.getFullYear();
	Month = D.getMonth()+1;
	Day = D.getDate();
	TempStartDate = ','+LineStartDate+',';
	for(var j=1;j<=DayLength;j++)
	{
		D = new Date(Date.parse(Y+'/'+M+'/'+j));
		if(Y==Year && Month==M && Day==j){continue;}
		IsStartDate = true;
		if(StartDateType==1 && !TempStartDate.match(new RegExp(',(0?)'+M+'-(0?)'+j+',','i')))
		{
			IsStartDate = false;
		}else if(StartDateType==2 && TempStartDate.indexOf(Week[D.getDay()])==-1){
			IsStartDate = false;
		}else if(StartDateType==3 && TempStartDate.indexOf(','+j+',')==-1){
			IsStartDate = false;
		}
		Td = $('td[name="'+Y+'-'+M+'-'+j+'"]');
		if(StartDateType==0 || IsStartDate == true)
		{
			MatchValue = LineSpecialPrice.match(new RegExp('(\|)(0?)'+M+'-(0?)'+j+'\:([0-9]+)\:([0-9]+)(\|)','i'));
			if(MatchValue!=null)
			{
				Price_1=MatchValue[4];ChildrenPrice_1=MatchValue[5];
			}else{
				Price_1=LinePrice;ChildrenPrice_1=LineChildrenPrice;
			}
			if(Date.parse(Y+'/'+M+'/'+j)>Date.parse(new Date()))
			{
				Td.html(Td.html()+'<a href="javascript:;">'+Price_1+'</a>'); 
				Td.attr('Price',Price_1).attr('ChildrenPrice',ChildrenPrice_1).css('color','').unbind().bind("click",function(){ClickCalendar($(this));HideWindow();});
			}else{
				Td.html(Td.html()+Price_1); 
			}
		}else{
			Td.html(Td.html()+'&nbsp;'); 
		}
	}
}
function o_adult_onchange(T)
{
	var v = parseInt($('#o_adult').val());
	if(v==-1)
	{
		v = parseInt($('#o_adult>option:selected').prev().val())+1;
		$('#o_adult').children().remove();
		for(var i=v;i<v+10;i++)
		{
			$('#o_adult').append('<option value="'+i+'">'+i+'</option>'); 
		}
		$('#o_adult').append('<option value="-1">更多</option>'); 
		$('#o_adult').val([v]);
	}
	FeesCount();
}
function o_children_onchange(T)
{
	var v = parseInt($('#o_children').val());
	if(v==-1)
	{
		v = parseInt($('#o_children>option:selected').prev().val())+1;
		$('#o_children').children().remove();
		for(var i=v;i<v+10;i++)
		{
			$('#o_children').append('<option value="'+i+'">'+i+'</option>'); 
		}
		$('#o_children').append('<option value="-1">更多</option>'); 
		$('#o_children').val([v]);
	}
	FeesCount();
}
function ClickCalendar(td)
{
	$('#o_date').attr('Price',td.attr('Price')).attr('ChildrenPrice',td.attr('ChildrenPrice')).val(td.attr('name'));
	FeesCount();
}
function FeesCount()
{
	var c=parseInt($('#o_date').attr('Price'))*$('#o_adult').val();
	c+=parseInt($('#o_date').attr('ChildrenPrice'))*$('#o_children').val();
	$('#feescount').html(c);
	$('#bonuscount').html(LineBonus*$('#o_adult').val()+LineChildrenBonus*$('#o_children').val());
	var s=parseInt($('#o_adult').val())+parseInt($('#o_children').val());
	var l=$('#visitorinfo tr').length/4;
	if(l==s)return;
	if(l>s)
	{
		for(var i=l-1;i>s;i--)
		{
		$('#o_realname_'+i+',#o_idcard_'+i).rules('remove');
		}
		$('#visitorinfo tr:gt('+(s*4-1)+')').remove();
	}else{
		for(var i=l;i<s;i++)
		{
			var j=i+1;
			$('#visitorinfo').append('<tr><td class="name"><b>第'+j+'位游客</b></td><td>&nbsp;</td></tr>'
			+'<tr><td class="name">真实姓名：</td><td><input type="text" name="o_realname_'+j+'" id="o_realname_'+j+'" /></td></tr>'
			+'<tr><td class="name">性别：</td><td><input type="radio" name="o_sex_'+j+'" value="男" checked="checked" />男'
	        +'<input type="radio" name="o_sex_'+j+'" name="radiobutton" value="女" />女</td></tr>'
			+'<tr><td class="name">身份证号码：</td><td><input name="o_idcard_'+j+'" id="o_idcard_'+j+'" type="text" size="40" />'
			+'<input type="checkbox" name="o_ischildren_'+j+'" id="o_ischildren_'+j+'" value="'+j+'" />儿童无证件</td></tr>');
			$('#o_realname_'+j).rules('add',{required:true,IsRealName:true,messages:{required:'请填写真实姓名!'}});
			$('#o_idcard_'+j).rules('add',{required:true,isIDCard:true,messages:{required:'请填写身份证号码!'}});
			$('#o_ischildren_'+j).click(function(){
				if($(this).attr("checked")==true)
				{
					$('#o_idcard_'+$(this).val()).hide().rules('remove');
					$('#o_idcard_'+$(this).val()).valid();
				}else{
					$('#o_idcard_'+$(this).val()).show().rules('add',{required:true,isIDCard:true,messages:{required:'请填写身份证号码!'}});
				}
			});
		}	
	}
}